From 69cf1e9eb6d12dd5fdbbfe8d73e27bc3ca0ec545 Mon Sep 17 00:00:00 2001 From: Shahzad Lone Date: Thu, 11 Aug 2022 03:41:31 -0400 Subject: [PATCH 1/2] PR: make topLevelNode explainable. --- query/graphql/planner/top.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/query/graphql/planner/top.go b/query/graphql/planner/top.go index 976e370c3d..3bf4b01e86 100644 --- a/query/graphql/planner/top.go +++ b/query/graphql/planner/top.go @@ -115,6 +115,10 @@ func (n *topLevelNode) Source() planNode { return nil } +func (n *topLevelNode) Explain() (map[string]any, error) { + return map[string]any{}, nil +} + func (n *topLevelNode) Next() (bool, error) { if n.isdone { return false, nil From 3a5bb8524547f4cbdc77a19f1b730e41088e6b4c Mon Sep 17 00:00:00 2001 From: Shahzad Lone Date: Fri, 12 Aug 2022 03:46:13 -0400 Subject: [PATCH 2/2] PR: Add tests for current `topLevelNode` explained with default Source(). --- .../query/explain/top_with_average_test.go | 106 +++++++++++++++++ .../query/explain/top_with_count_test.go | 102 +++++++++++++++++ .../query/explain/top_with_sum_test.go | 107 ++++++++++++++++++ 3 files changed, 315 insertions(+) create mode 100644 tests/integration/query/explain/top_with_average_test.go create mode 100644 tests/integration/query/explain/top_with_count_test.go create mode 100644 tests/integration/query/explain/top_with_sum_test.go diff --git a/tests/integration/query/explain/top_with_average_test.go b/tests/integration/query/explain/top_with_average_test.go new file mode 100644 index 0000000000..85d2b4aaa7 --- /dev/null +++ b/tests/integration/query/explain/top_with_average_test.go @@ -0,0 +1,106 @@ +// 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 TestExplainTopLevelAverageQuery(t *testing.T) { + test := testUtils.QueryTestCase{ + Description: "Explain top-level average query.", + + Query: `query @explain { + _avg( + author: { + field: age + } + ) + }`, + + Docs: map[int][]string{ + //authors + 2: { + `{ + "name": "John", + "verified": false, + "age": 28 + }`, + `{ + "name": "Bob", + "verified": true, + "age": 30 + }`, + }, + }, + + Results: []dataMap{ + { + "explain": dataMap{ + "topLevelNode": dataMap{}, + }, + }, + }, + } + + executeTestCase(t, test) +} + +func TestExplainTopLevelAverageQueryWithFilter(t *testing.T) { + test := testUtils.QueryTestCase{ + Description: "Explain top-level average query with filter.", + Query: `query @explain { + _avg( + author: { + field: age, + filter: { + age: { + _gt: 26 + } + } + } + ) + }`, + + Docs: map[int][]string{ + //authors + 2: { + `{ + "name": "John", + "verified": false, + "age": 21 + }`, + `{ + "name": "Bob", + "verified": false, + "age": 30 + }`, + `{ + "name": "Alice", + "verified": false, + "age": 32 + }`, + }, + }, + + Results: []dataMap{ + { + "explain": dataMap{ + "topLevelNode": dataMap{}, + }, + }, + }, + } + + executeTestCase(t, test) +} diff --git a/tests/integration/query/explain/top_with_count_test.go b/tests/integration/query/explain/top_with_count_test.go new file mode 100644 index 0000000000..55e618d88f --- /dev/null +++ b/tests/integration/query/explain/top_with_count_test.go @@ -0,0 +1,102 @@ +// 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 TestExplainTopLevelCountQuery(t *testing.T) { + test := testUtils.QueryTestCase{ + Description: "Explain top-level count query.", + + Query: `query @explain { + _count(author: {}) + }`, + + Docs: map[int][]string{ + //authors + 2: { + `{ + "name": "John", + "verified": true, + "age": 21 + }`, + `{ + "name": "Bob", + "verified": false, + "age": 30 + }`, + }, + }, + + Results: []dataMap{ + { + "explain": dataMap{ + "topLevelNode": dataMap{}, + }, + }, + }, + } + + executeTestCase(t, test) +} + +func TestExplainTopLevelCountQueryWithFilter(t *testing.T) { + test := testUtils.QueryTestCase{ + Description: "Explain top-level count query with filter.", + + Query: `query @explain { + _count( + author: { + filter: { + age: { + _gt: 26 + } + } + } + ) + }`, + + Docs: map[int][]string{ + //authors + 2: { + `{ + "name": "John", + "verified": false, + "age": 21 + }`, + `{ + "name": "Bob", + "verified": false, + "age": 30 + }`, + `{ + "name": "Alice", + "verified": true, + "age": 32 + }`, + }, + }, + + Results: []dataMap{ + { + "explain": dataMap{ + "topLevelNode": dataMap{}, + }, + }, + }, + } + + executeTestCase(t, test) +} diff --git a/tests/integration/query/explain/top_with_sum_test.go b/tests/integration/query/explain/top_with_sum_test.go new file mode 100644 index 0000000000..77afd0265e --- /dev/null +++ b/tests/integration/query/explain/top_with_sum_test.go @@ -0,0 +1,107 @@ +// 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 TestExplainTopLevelSumQuery(t *testing.T) { + test := testUtils.QueryTestCase{ + Description: "Explain top-level sum query.", + + Query: `query @explain { + _sum( + author: { + field: age + } + ) + }`, + + Docs: map[int][]string{ + //authors + 2: { + `{ + "name": "John", + "verified": true, + "age": 21 + }`, + `{ + "name": "Bob", + "verified": true, + "age": 30 + }`, + }, + }, + + Results: []dataMap{ + { + "explain": dataMap{ + "topLevelNode": dataMap{}, + }, + }, + }, + } + + executeTestCase(t, test) +} + +func TestExplainTopLevelSumQueryWithFilter(t *testing.T) { + test := testUtils.QueryTestCase{ + Description: "Explain top-level sum query with filter.", + + Query: `query @explain { + _sum( + author: { + field: age, + filter: { + age: { + _gt: 26 + } + } + } + ) + }`, + + Docs: map[int][]string{ + //authors + 2: { + `{ + "name": "John", + "verified": false, + "age": 21 + }`, + `{ + "name": "Bob", + "verified": false, + "age": 30 + }`, + `{ + "name": "Alice", + "verified": true, + "age": 32 + }`, + }, + }, + + Results: []dataMap{ + { + "explain": dataMap{ + "topLevelNode": dataMap{}, + }, + }, + }, + } + + executeTestCase(t, test) +}