Skip to content

Commit

Permalink
PR: Add group node execute datapoints
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Apr 1, 2023
1 parent 01e12ff commit 491c86a
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
42 changes: 41 additions & 1 deletion planner/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ type groupNode struct {

values []core.Doc
currentIndex int

execInfo groupExecInfo
}

type groupExecInfo struct {
// Total number of times groupNode was executed.
iterations uint64

// Total number of groups.
groups uint64

// Total number of child selections (hidden and visible).
totalChildSelections uint64

// Total number of child selections hidden before offset.
hiddenBeforeOffset uint64

// Total number of child selections hidden after offset and limit.
hiddenAfterLimit uint64
}

// Creates a new group node.
Expand Down Expand Up @@ -127,6 +146,8 @@ func (n *groupNode) Close() error {
func (n *groupNode) Source() planNode { return n.dataSources[0].Source() }

func (n *groupNode) Next() (bool, error) {
n.execInfo.iterations++

if n.values == nil {
values, err := join(n.dataSources, n.groupByFields, n.documentMapping)
if err != nil {
Expand All @@ -136,7 +157,11 @@ func (n *groupNode) Next() (bool, error) {
n.values = values.values

for _, group := range n.values {
n.execInfo.groups++

for _, childSelect := range n.childSelects {
n.execInfo.totalChildSelections++

subSelect := group.Fields[childSelect.Index]
if subSelect == nil {
// If the sub-select is nil we need to set it to an empty array and continue
Expand All @@ -151,11 +176,15 @@ func (n *groupNode) Next() (bool, error) {
// We must hide all child documents before the offset
for i := uint64(0); i < childSelect.Limit.Offset && i < l; i++ {
childDocs[i].Hidden = true

n.execInfo.hiddenBeforeOffset++
}

// We must hide all child documents after the offset plus limit
for i := childSelect.Limit.Limit + childSelect.Limit.Offset; i < l; i++ {
childDocs[i].Hidden = true

n.execInfo.hiddenAfterLimit++
}
}
}
Expand Down Expand Up @@ -273,6 +302,17 @@ func (n *groupNode) simpleExplain() (map[string]any, error) {
return simpleExplainMap, nil
}

func (n *groupNode) excuteExplain() map[string]any {
return map[string]any{
"iterations": n.execInfo.iterations,
"groups": n.execInfo.groups,
"totalChildSelections": n.execInfo.totalChildSelections,
"hiddenBeforeOffset": n.execInfo.hiddenBeforeOffset,
"hiddenAfterLimit": n.execInfo.hiddenAfterLimit,
"hiddenChildSelections": n.execInfo.hiddenBeforeOffset + n.execInfo.hiddenAfterLimit,
}
}

// Explain method returns a map containing all attributes of this node that
// are to be explained, subscribes / opts-in this node to be an explainablePlanNode.
func (n *groupNode) Explain(explainType request.ExplainType) (map[string]any, error) {
Expand All @@ -281,7 +321,7 @@ func (n *groupNode) Explain(explainType request.ExplainType) (map[string]any, er
return n.simpleExplain()

case request.ExecuteExplain:
return map[string]any{}, nil
return n.excuteExplain(), nil

default:
return nil, ErrUnknownExplainRequestType
Expand Down
74 changes: 74 additions & 0 deletions tests/integration/explain/execute/group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package test_explain_execute

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestExecuteExplainRequestWithGroup(t *testing.T) {
test := testUtils.TestCase{

Description: "Explain (execute) request with groupBy.",

Actions: []any{
gqlSchemaExecuteExplain(),

// Books
create2AddressDocuments(),

testUtils.Request{
Request: `query @explain(type: execute) {
ContactAddress(groupBy: [country]) {
country
_group {
city
}
}
}`,

Results: []dataMap{
{
"explain": dataMap{
"executionSuccess": true,
"sizeOfResult": 1,
"planExecutions": uint64(2),
"selectTopNode": dataMap{
"groupNode": dataMap{
"iterations": uint64(2),
"groups": uint64(1),
"totalChildSelections": uint64(1),
"hiddenBeforeOffset": uint64(0),
"hiddenAfterLimit": uint64(0),
"hiddenChildSelections": uint64(0),
"selectNode": dataMap{
"iterations": uint64(3),
"filterMatches": uint64(2),
"dockeyMismatches": uint64(0),
"scanNode": dataMap{
"iterations": uint64(4),
"docFetches": uint64(4),
"filterMatches": uint64(2),
},
},
},
},
},
},
},
},
},
}

executeTestCase(t, test)
}

0 comments on commit 491c86a

Please sign in to comment.