Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add _keys attribute to selectNode simple explain #1546

1 change: 1 addition & 0 deletions planner/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
dataLabel = "data"
fieldNameLabel = "fieldName"
filterLabel = "filter"
keysLabel = "_keys"
idsLabel = "ids"
limitLabel = "limit"
offsetLabel = "offset"
Expand Down
3 changes: 1 addition & 2 deletions planner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type scanNode struct {
desc client.CollectionDescription

fields []*client.FieldDescription
docKey []byte

showDeleted bool

Expand Down Expand Up @@ -106,7 +105,7 @@ func (n *scanNode) Next() (bool, error) {
// keep scanning until we find a doc that passes the filter
for {
var err error
n.docKey, n.currentValue, err = n.fetcher.FetchNextDoc(n.p.ctx, n.documentMapping)
_, n.currentValue, err = n.fetcher.FetchNextDoc(n.p.ctx, n.documentMapping)
if err != nil {
return false, err
}
Expand Down
17 changes: 12 additions & 5 deletions planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type selectNode struct {
// are defined in the subtype scan node.
filter *mapper.Filter

docKeys immutable.Option[[]string]
keys immutable.Option[[]string]

selectReq *mapper.Select
groupSelects []*mapper.Select
Expand Down Expand Up @@ -167,9 +167,9 @@ func (n *selectNode) Next() (bool, error) {

n.execInfo.filterMatches++

if n.docKeys.HasValue() {
if n.keys.HasValue() {
docKey := n.currentValue.GetKey()
for _, key := range n.docKeys.Value() {
for _, key := range n.keys.Value() {
if docKey == key {
return true, nil
}
Expand Down Expand Up @@ -200,6 +200,13 @@ func (n *selectNode) simpleExplain() (map[string]any, error) {
simpleExplainMap[filterLabel] = n.filter.ToMap(n.documentMapping)
}

// Add the keys attribute if it exists.
if !n.keys.HasValue() {
simpleExplainMap[keysLabel] = nil
} else {
simpleExplainMap[keysLabel] = n.keys.Value()
}

return simpleExplainMap, nil
}

Expand Down Expand Up @@ -406,7 +413,7 @@ func (p *Planner) SelectFromSource(
selectReq: selectReq,
docMapper: docMapper{selectReq.DocumentMapping},
filter: selectReq.Filter,
docKeys: selectReq.DocKeys,
keys: selectReq.DocKeys,
}
limit := selectReq.Limit
orderBy := selectReq.OrderBy
Expand Down Expand Up @@ -461,7 +468,7 @@ func (p *Planner) Select(selectReq *mapper.Select) (planNode, error) {
s := &selectNode{
planner: p,
filter: selectReq.Filter,
docKeys: selectReq.DocKeys,
keys: selectReq.DocKeys,
selectReq: selectReq,
docMapper: docMapper{selectReq.DocumentMapping},
}
Expand Down
1 change: 1 addition & 0 deletions tests/integration/explain/default/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestDefaultExplainRequestWithFullBasicGraph(t *testing.T) {
"explain": dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"_keys": nil,
"filter": nil,
"scanNode": dataMap{
"filter": nil,
Expand Down
1 change: 1 addition & 0 deletions tests/integration/explain/default/type_join_many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestDefaultExplainRequestWithAOneToManyJoin(t *testing.T) {
ExpectedAttributes: dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"_keys": nil,
"filter": nil,
"scanNode": dataMap{
"filter": nil,
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/explain/default/type_join_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestDefaultExplainRequestWithAOneToOneJoin(t *testing.T) {
ExpectedAttributes: dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"_keys": nil,
"filter": nil,
"scanNode": dataMap{
"filter": nil,
Expand Down Expand Up @@ -224,6 +225,7 @@ func TestDefaultExplainRequestWithTwoLevelDeepNestedJoins(t *testing.T) {
ExpectedAttributes: dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"_keys": nil,
"filter": nil,
"scanNode": dataMap{
"filter": nil,
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/explain/default/type_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestDefaultExplainRequestWith2SingleJoinsAnd1ManyJoin(t *testing.T) {
ExpectedAttributes: dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"_keys": nil,
"filter": nil,
"scanNode": dataMap{
"filter": nil,
Expand Down Expand Up @@ -176,6 +177,7 @@ func TestDefaultExplainRequestWith2SingleJoinsAnd1ManyJoin(t *testing.T) {
ExpectedAttributes: dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"_keys": nil,
"filter": nil,
"scanNode": dataMap{
"filter": nil,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// Copyright 2023 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_default

import (
"testing"

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

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

Description: "Explain (default) request with related and regular filter + keys.",

Actions: []any{
explainUtils.SchemaForExplainTests,

testUtils.ExplainRequest{

Request: `query @explain {
Author(
filter: {
name: {_eq: "John Grisham"},
books: {name: {_eq: "Painted House"}}
},
dockeys: [
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f8e"
]
) {
name
age
}
}`,

ExpectedPatterns: []dataMap{
{
"explain": dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"typeIndexJoin": normalTypeJoinPattern,
},
},
},
},
},

ExpectedTargets: []testUtils.PlanNodeTargetCase{
{
TargetNodeName: "selectNode",
ExpectedAttributes: dataMap{
"_keys": []string{
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f8e",
},
"filter": dataMap{
"books": dataMap{
"name": dataMap{
"_eq": "Painted House",
},
},
},
},
},
{
TargetNodeName: "scanNode",
IncludeChildNodes: true, // should be last node, so will have no child nodes.
ExpectedAttributes: dataMap{
"collectionID": "3",
"collectionName": "Author",
"filter": dataMap{
"name": dataMap{
"_eq": "John Grisham",
},
},
"spans": []dataMap{
{
"start": "/3/bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"end": "/3/bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9e",
},
{
"start": "/3/bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f8e",
"end": "/3/bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f8f",
},
},
},
},
},
},
},
}

explainUtils.ExecuteTestCase(t, test)
}

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

Description: "Explain (default) request with many related filters + key.",

Actions: []any{
explainUtils.SchemaForExplainTests,

testUtils.ExplainRequest{

Request: `query @explain {
Author(
filter: {
name: {_eq: "Cornelia Funke"},
articles: {name: {_eq: "To my dear readers"}},
books: {name: {_eq: "Theif Lord"}}
},
dockeys: ["bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d"]
) {
name
age
}
}`,

ExpectedPatterns: []dataMap{
{
"explain": dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"parallelNode": []dataMap{
{
"typeIndexJoin": normalTypeJoinPattern,
},
{
"typeIndexJoin": normalTypeJoinPattern,
},
},
},
},
},
},
},

ExpectedTargets: []testUtils.PlanNodeTargetCase{
{
TargetNodeName: "selectNode",
ExpectedAttributes: dataMap{
"_keys": []string{
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
},
"filter": dataMap{
"articles": dataMap{
"name": dataMap{
"_eq": "To my dear readers",
},
},
"books": dataMap{
"name": dataMap{
"_eq": "Theif Lord",
},
},
},
},
},
{
TargetNodeName: "scanNode",
IncludeChildNodes: true, // should be last node, so will have no child nodes.
ExpectedAttributes: dataMap{
"collectionID": "3",
"collectionName": "Author",
"filter": dataMap{
"name": dataMap{
"_eq": "Cornelia Funke",
},
},
"spans": []dataMap{
{
"start": "/3/bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"end": "/3/bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9e",
},
},
},
},
},
},
},
}

explainUtils.ExecuteTestCase(t, test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestDefaultExplainRequestWithRelatedAndRegularFilter(t *testing.T) {
{
TargetNodeName: "selectNode",
ExpectedAttributes: dataMap{
"_keys": nil,
"filter": dataMap{
"books": dataMap{
"name": dataMap{
Expand Down Expand Up @@ -137,6 +138,7 @@ func TestDefaultExplainRequestWithManyRelatedFilters(t *testing.T) {
{
TargetNodeName: "selectNode",
ExpectedAttributes: dataMap{
"_keys": nil,
"filter": dataMap{
"articles": dataMap{
"name": dataMap{
Expand Down
Loading