Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
wip: Add tests for groupBy with average.

temp: bunch of tests
  • Loading branch information
shahzadlone committed Jul 22, 2022
1 parent cad7355 commit 6f3aabe
Show file tree
Hide file tree
Showing 7 changed files with 1,806 additions and 1 deletion.
105 changes: 104 additions & 1 deletion query/graphql/planner/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package planner

import (
"fmt"

"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/query/graphql/mapper"

Expand Down Expand Up @@ -173,5 +175,106 @@ func (n *groupNode) Next() (bool, error) {
// 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() (map[string]interface{}, error) {
return map[string]interface{}{}, nil
explainerMap := map[string]interface{}{}

// Get the parent level groupBy attribute(s).
groupByFields := []string{}
for _, field := range n.groupByFields {
groupByFields = append(
groupByFields,
field.Name,
)
}
explainerMap["groupByFields"] = groupByFields

// Get the inner group (child) selection attribute(s).
if len(n.childSelects) == 0 {
explainerMap["childSelects"] = nil
} else {
childSelects := make([]map[string]interface{}, 0, len(n.childSelects))
for _, child := range n.childSelects {
if child == nil {
continue
}

childExplainGraph := map[string]interface{}{}

childExplainGraph[collectionNameLabel] = child.CollectionName

c := child.Targetable

// Get targetable attribute(s) of this child.

if c.DocKeys.HasValue {
childExplainGraph["docKeys"] = c.DocKeys.Value
} else {
childExplainGraph["docKeys"] = nil
}

if c.Filter == nil || c.Filter.ExternalConditions == nil {
childExplainGraph[filterLabel] = nil
} else {
childExplainGraph[filterLabel] = c.Filter.ExternalConditions
}

if c.Limit != nil {
childExplainGraph[limitLabel] = map[string]interface{}{
limitLabel: c.Limit.Limit,
offsetLabel: c.Limit.Offset,
}
} else {
childExplainGraph[limitLabel] = nil
}

if c.OrderBy != nil {
innerOrderings := []map[string]interface{}{}
for _, condition := range c.OrderBy.Conditions {
orderFieldNames := []string{}

for _, orderFieldIndex := range condition.FieldIndexes {
// Try to find the name of this index.
fieldName, found := n.documentMapping.TryToFindNameFromIndex(orderFieldIndex)
if !found {
return nil, fmt.Errorf(
"No order field name (for grouping) was found for index =%d",
orderFieldIndex,
)
}

orderFieldNames = append(orderFieldNames, fieldName)
}
// Put it all together for this order element.
innerOrderings = append(innerOrderings,
map[string]interface{}{
"fields": orderFieldNames,
"direction": string(condition.Direction),
},
)
}

childExplainGraph["orderBy"] = innerOrderings
} else {
childExplainGraph["orderBy"] = nil
}

if c.GroupBy != nil {
innerGroupByFields := []string{}
for _, fieldOfChildGroupBy := range c.GroupBy.Fields {
innerGroupByFields = append(
innerGroupByFields,
fieldOfChildGroupBy.Name,
)
}
childExplainGraph["groupBy"] = innerGroupByFields
} else {
childExplainGraph["groupBy"] = nil
}

childSelects = append(childSelects, childExplainGraph)
}

explainerMap["childSelects"] = childSelects
}

return explainerMap, nil
}
161 changes: 161 additions & 0 deletions tests/integration/query/explain/group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// 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

import (
"testing"

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

func TestExplainSimpleGroupByOnParent(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Explain a grouping on parent.",
Query: `query @explain {
author (groupBy: [age]) {
age
_group {
name
}
}
}`,

Docs: map[int][]string{
//authors
2: {
`{
"name": "John Grisham",
"age": 65
}`,

`{
"name": "Cornelia Funke",
"age": 62
}`,

`{
"name": "John's Twin",
"age": 65
}`,
},
},

Results: []dataMap{
{
"explain": dataMap{
"selectTopNode": dataMap{
"groupNode": dataMap{
"groupByFields": []string{"age"},
"childSelects": []dataMap{
{
"collectionName": "author",
"docKeys": nil,
"groupBy": nil,
"limit": nil,
"orderBy": nil,
"filter": nil,
},
},
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
},
},
},
},
},
},
},
},
}

executeTestCase(t, test)
}

func TestExplainGroupByTwoFieldsOnParent(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Explain a grouping by two fields.",
Query: `query @explain {
author (groupBy: [age, name]) {
age
_group {
name
}
}
}`,

Docs: map[int][]string{
//authors
2: {
`{
"name": "John Grisham",
"age": 65
}`,

`{
"name": "Cornelia Funke",
"age": 62
}`,

`{
"name": "John's Twin",
"age": 65
}`,
},
},

Results: []dataMap{
{
"explain": dataMap{
"selectTopNode": dataMap{
"groupNode": dataMap{
"groupByFields": []string{"age", "name"},
"childSelects": []dataMap{
{
"collectionName": "author",
"docKeys": nil,
"groupBy": nil,
"limit": nil,
"orderBy": nil,
"filter": nil,
},
},
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
},
},
},
},
},
},
},
},
}

executeTestCase(t, test)
}
Loading

0 comments on commit 6f3aabe

Please sign in to comment.