Skip to content

Commit

Permalink
feat: Add explaination of spans for scanNode. (#492)
Browse files Browse the repository at this point in the history
- RELEVANT ISSUE(S)

Resolves #474 

- DESCRIPTION

Adds the explanation of the `spans` attribute inside the `scanNode`.

Request:
```
query @Explain {
	users(dockeys: ["bae-52b9170d-b77a-5887-b877-cbdbb99b009f","bae-1378ab62-e064-5af4-9ea6-49941c8d8f94"]) {
 		Name
 		Age
 	}
 }
```

Response:
```
{
	"explain": {
		"selectTopNode": {
			"selectNode": {
				"filter": nil,
				"scanNode": {
					"collectionID":   "1",
					"collectionName": "users",
					"filter":         nil,
					"spans": {
						{
							"start": "/1/bae-52b9170d-b77a-5887-b877-cbdbb99b009f",
							"end":   "/1/bae-52b9170d-b77a-5887-b877-cbdbb99b009g"
						},
						{
							"start": "/1/bae-1378ab62-e064-5af4-9ea6-49941c8d8f94",
							"end":   "/1/bae-1378ab62-e064-5af4-9ea6-49941c8d8f95"
						}
					}
				}
			}
		}
	}
}
```
  • Loading branch information
shahzadlone authored Jun 3, 2022
1 parent 257bf09 commit 45978e0
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 30 deletions.
2 changes: 1 addition & 1 deletion query/graphql/planner/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const (
dataLabel = "data"
filterLabel = "filter"
idsLabel = "ids"
// spansLabel = "spans"
spansLabel = "spans"
)

// buildExplainGraph builds the explainGraph from the given top level plan.
Expand Down
19 changes: 16 additions & 3 deletions query/graphql/planner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ func (n *scanNode) Close() error {

func (n *scanNode) Source() planNode { return nil }

// explainSpans explains the spans attribute.
func (n *scanNode) explainSpans() []map[string]interface{} {
spansExplainer := []map[string]interface{}{}
for _, span := range n.spans {
spanExplainer := map[string]interface{}{
"start": span.Start().ToString(),
"end": span.End().ToString(),
}

spansExplainer = append(spansExplainer, spanExplainer)
}

return spansExplainer
}

// 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 *scanNode) Explain() (map[string]interface{}, error) {
Expand All @@ -127,10 +142,8 @@ func (n *scanNode) Explain() (map[string]interface{}, error) {
explainerMap[collectionNameLabel] = n.desc.Name
explainerMap[collectionIDLabel] = n.desc.IDString()

// @TODO: {defradb/issues/474} Add explain attributes.
// Add the spans attribute.
// explainerMap[spansLabel] = n.spans
// Add the index attribute.
explainerMap[spansLabel] = n.explainSpans()

return explainerMap, nil
}
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/query/explain/with_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func TestExplainQueryOneToManyWithACount(t *testing.T) {
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
},
},
},
},
Expand Down Expand Up @@ -186,6 +192,12 @@ func TestExplainQueryOneToManyMultipleWithCounts(t *testing.T) {
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
},
},
},
},
Expand All @@ -195,6 +207,12 @@ func TestExplainQueryOneToManyMultipleWithCounts(t *testing.T) {
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
},
},
},
},
Expand Down
Loading

0 comments on commit 45978e0

Please sign in to comment.