Skip to content

Commit

Permalink
feat(Query): Allow filters in expand(_all_) queries on predicates poi…
Browse files Browse the repository at this point in the history
…nting to nodes (#6752)
  • Loading branch information
all-seeing-code authored Oct 19, 2020
1 parent 5294fb9 commit e64afe1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
11 changes: 11 additions & 0 deletions query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ const testSchema = `
type Person {
name
pet
friend
gender
alive
}
type Animal {
Expand Down Expand Up @@ -243,6 +246,9 @@ type SchoolInfo {
type User {
name
password
gender
friend
alive
}
type Node {
Expand Down Expand Up @@ -318,6 +324,7 @@ noindex_salary : float .
language : [string] .
score : [int] @index(int) .
average : [float] @index(float) .
gender : string .
`

func populateCluster() {
Expand Down Expand Up @@ -615,6 +622,10 @@ func populateCluster() {
<5> <dgraph.type> "Pet" .
<6> <dgraph.type> "Animal" .
<6> <dgraph.type> "Pet" .
<23> <dgraph.type> "Person" .
<24> <dgraph.type> "Person" .
<25> <dgraph.type> "Person" .
<31> <dgraph.type> "Person" .
<32> <dgraph.type> "SchoolInfo" .
<33> <dgraph.type> "SchoolInfo" .
<34> <dgraph.type> "SchoolInfo" .
Expand Down
8 changes: 8 additions & 0 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ func (sg *SubGraph) isSimilar(ssg *SubGraph) bool {
}
return false
}
// Below check doesn't differentiate between different filters.
// It is added to differential between `hasFriend` and `hasFriend @filter()`
if sg.Filters != nil {
if ssg.Filters != nil && len(sg.Filters) == len(ssg.Filters) {
return true
}
return false
}
return true
}

Expand Down
21 changes: 12 additions & 9 deletions query/query3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2341,7 +2341,7 @@ func TestPasswordExpandAll1(t *testing.T) {
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data":{"me":[{"name":"Michonne"}]}}`, js)
require.JSONEq(t, `{"data":{"me":[{"alive":true, "gender":"female","name":"Michonne"}]}}`, js)
}

func TestPasswordExpandAll2(t *testing.T) {
Expand All @@ -2354,7 +2354,8 @@ func TestPasswordExpandAll2(t *testing.T) {
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data":{"me":[{"name":"Michonne", "checkpwd(password)":false}]}}`, js)
require.JSONEq(t, `{"data":{"me":[{"alive":true, "checkpwd(password)":false,
"gender":"female", "name":"Michonne"}]}}`, js)
}

func TestPasswordExpandError(t *testing.T) {
Expand Down Expand Up @@ -3069,7 +3070,8 @@ func TestTypeFunction(t *testing.T) {
`
js := processQueryNoErr(t, query)
require.JSONEq(t,
`{"data": {"me":[{"uid":"0x2"}, {"uid":"0x3"}, {"uid":"0x4"}, {"uid":"0xcb"}]}}`,
`{"data": {"me":[{"uid":"0x2"}, {"uid":"0x3"}, {"uid":"0x4"},{"uid":"0x17"},
{"uid":"0x18"},{"uid":"0x19"}, {"uid":"0x1f"}, {"uid":"0xcb"}]}}`,
js)
}

Expand Down Expand Up @@ -3143,17 +3145,17 @@ func TestQueryUnknownType(t *testing.T) {
func TestQuerySingleType(t *testing.T) {
query := `schema(type: Person) {}`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data": {"types":[{"name":"Person",
"fields":[{"name":"name"}, {"name":"pet"}]}]}}`,
require.JSONEq(t, `{"data":{"types":[{"fields":[{"name":"name"},{"name":"pet"},
{"name":"friend"},{"name":"gender"},{"name":"alive"}],"name":"Person"}]}}`,
js)
}

func TestQueryMultipleTypes(t *testing.T) {
query := `schema(type: [Person, Animal]) {}`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data": {"types":[{"name":"Animal",
"fields":[{"name":"name"}]},
{"name":"Person", "fields":[{"name":"name"}, {"name":"pet"}]}]}}`, js)
require.JSONEq(t, `{"data":{"types":[{"fields":[{"name":"name"}],"name":"Animal"},
{"fields":[{"name":"name"},{"name":"pet"},{"name":"friend"},{"name":"gender"},
{"name":"alive"}],"name":"Person"}]}}`, js)
}

func TestRegexInFilterNoDataOnRoot(t *testing.T) {
Expand Down Expand Up @@ -3193,7 +3195,8 @@ func TestMultiRegexInFilter(t *testing.T) {
}
`
res := processQueryNoErr(t, query)
require.JSONEq(t, `{"data": {"q": [{"name": "Michonne"}]}}`, res)
require.JSONEq(t, `{"data": {"q": [{"alive":true, "gender":"female",
"name":"Michonne"}]}}`, res)
}

func TestMultiRegexInFilter2(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions query/query4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,20 @@ func TestTypeFilterAtExpandEmptyResults(t *testing.T) {
require.JSONEq(t, `{"data": {"q":[]}}`, js)
}

func TestFilterAtSameLevelOnUIDWithExpand(t *testing.T) {
query := `{
q(func: eq(name, "Michonne")) {
expand(_all_)
friend @filter(eq(alive, true)){
expand(_all_)
}
}
}`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data":{"q":[{"name":"Michonne","gender":"female","alive":true,
"friend":[{"gender":"male","alive":true,"name":"Rick Grimes"}]}]}}`, js)
}

// Test Related to worker based pagination.

func TestHasOrderDesc(t *testing.T) {
Expand Down

0 comments on commit e64afe1

Please sign in to comment.