Skip to content

Commit

Permalink
Add summation of aggregates support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Apr 12, 2022
1 parent 1101b75 commit 5e3637e
Show file tree
Hide file tree
Showing 6 changed files with 482 additions and 14 deletions.
99 changes: 99 additions & 0 deletions db/tests/query/simple/with_group_count_sum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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 simple

import (
"testing"

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

func TestQuerySimpleWithGroupByStringWithInnerGroupBooleanAndSumOfCount(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query with group by string, with child group by boolean, and sum of count",
Query: `query {
users(groupBy: [Name]) {
Name
_sum(field: {_group: _count})
_group (groupBy: [Verified]){
Verified
_count(field: _group)
}
}
}`,
Docs: map[int][]string{
0: {
(`{
"Name": "John",
"Age": 25,
"Verified": true
}`),
(`{
"Name": "John",
"Age": 32,
"Verified": true
}`),
(`{
"Name": "John",
"Age": 34,
"Verified": false
}`),
(`{
"Name": "Carlo",
"Age": 55,
"Verified": true
}`),
(`{
"Name": "Alice",
"Age": 19,
"Verified": false
}`)},
},
Results: []map[string]interface{}{
{
"Name": "John",
"_sum": int64(3),
"_group": []map[string]interface{}{
{
"Verified": true,
"_count": int(2),
},
{
"Verified": false,
"_count": int(1),
},
},
},
{
"Name": "Alice",
"_sum": int64(1),
"_group": []map[string]interface{}{
{
"Verified": false,
"_count": int(1),
},
},
},
{
"Name": "Carlo",
"_sum": int64(1),
"_group": []map[string]interface{}{
{
"Verified": true,
"_count": int(1),
},
},
},
},
}

executeTestCase(t, test)
}
283 changes: 283 additions & 0 deletions db/tests/query/simple/with_group_sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,88 @@ func TestQuerySimpleWithGroupByStringWithoutRenderedGroupAndChildNilSum(t *testi
executeTestCase(t, test)
}

func TestQuerySimpleWithGroupByStringWithInnerGroupBooleanAndSumOfSumOfInt(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query with group by string, with child group by boolean, and sum of sum on int",
Query: `query {
users(groupBy: [Name]) {
Name
_sum(field: {_group: _sum})
_group (groupBy: [Verified]){
Verified
_sum(field: {_group: Age})
}
}
}`,
Docs: map[int][]string{
0: {
(`{
"Name": "John",
"Age": 25,
"Verified": true
}`),
(`{
"Name": "John",
"Age": 32,
"Verified": true
}`),
(`{
"Name": "John",
"Age": 34,
"Verified": false
}`),
(`{
"Name": "Carlo",
"Age": 55,
"Verified": true
}`),
(`{
"Name": "Alice",
"Age": 19,
"Verified": false
}`)},
},
Results: []map[string]interface{}{
{
"Name": "John",
"_sum": int64(91),
"_group": []map[string]interface{}{
{
"Verified": true,
"_sum": int64(57),
},
{
"Verified": false,
"_sum": int64(34),
},
},
},
{
"Name": "Alice",
"_sum": int64(19),
"_group": []map[string]interface{}{
{
"Verified": false,
"_sum": int64(19),
},
},
},
{
"Name": "Carlo",
"_sum": int64(55),
"_group": []map[string]interface{}{
{
"Verified": true,
"_sum": int64(55),
},
},
},
},
}

executeTestCase(t, test)
}

func TestQuerySimpleWithGroupByStringWithoutRenderedGroupAndChildEmptyFloatSum(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query with group by string, sum on non-rendered group float (default) value",
Expand Down Expand Up @@ -193,3 +275,204 @@ func TestQuerySimpleWithGroupByStringWithoutRenderedGroupAndChildFloatSum(t *tes

executeTestCase(t, test)
}

func TestQuerySimpleWithGroupByStringWithInnerGroupBooleanAndSumOfSumOfFloat(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query with group by string, with child group by boolean, and sum of sum on float",
Query: `query {
users(groupBy: [Name]) {
Name
_sum(field: {_group: _sum})
_group (groupBy: [Verified]){
Verified
_sum(field: {_group: HeightM})
}
}
}`,
Docs: map[int][]string{
0: {
(`{
"Name": "John",
"HeightM": 1.82,
"Verified": true
}`),
(`{
"Name": "John",
"HeightM": 1.61,
"Verified": true
}`),
(`{
"Name": "John",
"HeightM": 2.22,
"Verified": false
}`),
(`{
"Name": "Carlo",
"HeightM": 1.74,
"Verified": true
}`),
(`{
"Name": "Alice",
"HeightM": 2.04,
"Verified": false
}`)},
},
Results: []map[string]interface{}{
{
"Name": "John",
"_sum": float64(5.65),
"_group": []map[string]interface{}{
{
"Verified": false,
"_sum": float64(2.22),
},
{
"Verified": true,
"_sum": float64(3.43),
},
},
},
{
"Name": "Alice",
"_sum": float64(2.04),
"_group": []map[string]interface{}{
{
"Verified": false,
"_sum": float64(2.04),
},
},
},
{
"Name": "Carlo",
"_sum": float64(1.74),
"_group": []map[string]interface{}{
{
"Verified": true,
"_sum": float64(1.74),
},
},
},
},
}

executeTestCase(t, test)
}

func TestQuerySimpleWithGroupByStringWithInnerGroupBooleanAndSumOfSumOfSumOfFloat(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query with group by string, with child group by boolean, and sum of sum of sum of float",
Query: `query {
users(groupBy: [Name]) {
Name
_sum(field: {_group: _sum})
_group (groupBy: [Verified]){
Verified
_sum(field: {_group: HeightM})
_group (groupBy: [Age]){
Age
_sum(field: {_group: HeightM})
}
}
}
}`,
Docs: map[int][]string{
0: {
(`{
"Name": "John",
"HeightM": 1.82,
"Age": 25,
"Verified": true
}`),
(`{
"Name": "John",
"HeightM": 1.61,
"Age": 32,
"Verified": true
}`),
(`{
"Name": "John",
"HeightM": 2.22,
"Age": 34,
"Verified": false
}`),
(`{
"Name": "Carlo",
"HeightM": 1.74,
"Age": 55,
"Verified": true
}`),
(`{
"Name": "Alice",
"HeightM": 2.04,
"Age": 19,
"Verified": false
}`)},
},
Results: []map[string]interface{}{
{
"Name": "John",
"_sum": float64(5.65),
"_group": []map[string]interface{}{
{
"Verified": false,
"_sum": float64(2.22),
"_group": []map[string]interface{}{
{
"Age": uint64(34),
"_sum": float64(2.22),
},
},
},
{
"Verified": true,
"_sum": float64(3.43),
"_group": []map[string]interface{}{
{
"Age": uint64(32),
"_sum": float64(1.61),
},
{
"Age": uint64(25),
"_sum": float64(1.82),
},
},
},
},
},
{
"Name": "Alice",
"_sum": float64(2.04),
"_group": []map[string]interface{}{
{
"Verified": false,
"_sum": float64(2.04),
"_group": []map[string]interface{}{
{
"Age": uint64(19),
"_sum": float64(2.04),
},
},
},
},
},
{
"Name": "Carlo",
"_sum": float64(1.74),
"_group": []map[string]interface{}{
{
"Verified": true,
"_sum": float64(1.74),
"_group": []map[string]interface{}{
{
"Age": uint64(55),
"_sum": float64(1.74),
},
},
},
},
},
},
}

executeTestCase(t, test)
}
Loading

0 comments on commit 5e3637e

Please sign in to comment.