Skip to content

Commit

Permalink
Add group-by functionality to defra db
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Nov 17, 2021
1 parent 6dc28c1 commit 82bf22e
Show file tree
Hide file tree
Showing 11 changed files with 1,753 additions and 282 deletions.
121 changes: 121 additions & 0 deletions db/tests/query/one_to_many/with_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2020 Source Inc.
//
// 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 one_to_many

import (
"testing"

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

func TestQueryOneToManyWithInnerJoinGroupNumber(t *testing.T) {
tests := []testUtils.QueryTestCase{
{
Description: "One-to-many relation query from many side with group inside of join",
Query: `query {
author {
name
age
published (groupBy: [rating]){
rating
_group {
name
}
}
}
}`,
Docs: map[int][]string{
//books
0: { // bae-fd541c25-229e-5280-b44b-e5c2af3e374d
(`{
"name": "Painted House",
"rating": 4.9,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "A Time for Mercy",
"rating": 4.5,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "The Client",
"rating": 4.5,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "Theif Lord",
"rating": 4.8,
"author_id": "bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04"
}`),
},
//authors
1: {
// bae-41598f0c-19bc-5da6-813b-e80f14a10df3
(`{
"name": "John Grisham",
"age": 65,
"verified": true
}`),
// bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04
(`{
"name": "Cornelia Funke",
"age": 62,
"verified": false
}`),
},
},
Results: []map[string]interface{}{
{
"name": "John Grisham",
"age": uint64(65),
"published": []map[string]interface{}{
{
"rating": 4.5,
"_group": []map[string]interface{}{
{
"name": "The Client",
},
{
"name": "A Time for Mercy",
},
},
},
{
"rating": 4.9,
"_group": []map[string]interface{}{
{
"name": "Painted House",
},
},
},
},
},
{
"name": "Cornelia Funke",
"age": uint64(62),
"published": []map[string]interface{}{
{
"rating": 4.8,
"_group": []map[string]interface{}{
{
"name": "Theif Lord",
},
},
},
},
},
},
},
}

for _, test := range tests {
executeTestCase(t, test)
}
}
206 changes: 206 additions & 0 deletions db/tests/query/simple/with_group_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Copyright 2020 Source Inc.
//
// 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 TestQuerySimpleWithGroupByStringWithGroupNumberFilter(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query with group by with child filter",
Query: `query {
users(groupBy: [Name]) {
Name
_group (filter: {Age: {_gt: 26}}){
Age
}
}
}`,
Docs: map[int][]string{
0: {
(`{
"Name": "John",
"Age": 25
}`),
(`{
"Name": "John",
"Age": 32
}`),
(`{
"Name": "Carlo",
"Age": 55
}`),
(`{
"Name": "Alice",
"Age": 19
}`)},
},
Results: []map[string]interface{}{
{
"Name": "Alice",
"_group": []map[string]interface{}{},
},
{
"Name": "John",
"_group": []map[string]interface{}{
{
"Age": uint64(32),
},
},
},
{
"Name": "Carlo",
"_group": []map[string]interface{}{
{
"Age": uint64(55),
},
},
},
},
}

executeTestCase(t, test)
}

func TestQuerySimpleWithGroupByStringWithGroupNumberWithParentFilter(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query with group by with number filter",
Query: `query {
users(groupBy: [Name], filter: {Age: {_gt: 26}}) {
Name
_group {
Age
}
}
}`,
Docs: map[int][]string{
0: {
(`{
"Name": "John",
"Age": 25
}`),
(`{
"Name": "John",
"Age": 32
}`),
(`{
"Name": "Carlo",
"Age": 55
}`),
(`{
"Name": "Alice",
"Age": 19
}`)},
},
Results: []map[string]interface{}{
{
"Name": "John",
"_group": []map[string]interface{}{
{
"Age": uint64(32),
},
},
},
{
"Name": "Carlo",
"_group": []map[string]interface{}{
{
"Age": uint64(55),
},
},
},
},
}

executeTestCase(t, test)
}

func TestQuerySimpleWithGroupByStringWithInnerGroupBooleanThenInnerNumberFilterThatExcludesAll(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query with group by string, with child group by boolean, with child number filter that excludes all records",
Query: `query {
users(groupBy: [Name]) {
Name
_group (groupBy: [Verified]){
Verified
_group (filter: {Age: {_gt: 260}}) {
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",
"_group": []map[string]interface{}{
{
"Verified": true,
"_group": []map[string]interface{}{},
},
{
"Verified": false,
"_group": []map[string]interface{}{},
},
},
},
{
"Name": "Alice",
"_group": []map[string]interface{}{
{
"Verified": false,
"_group": []map[string]interface{}{},
},
},
},
{
"Name": "Carlo",
"_group": []map[string]interface{}{
{
"Verified": true,
"_group": []map[string]interface{}{},
},
},
},
},
}

executeTestCase(t, test)
}
Loading

0 comments on commit 82bf22e

Please sign in to comment.