Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filtering on nonindexed predicate #4531

Merged
merged 7 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ value : string @index(trigram) .
full_name : string @index(hash) .
nick_name : string @index(term) .
royal_title : string @index(hash, term, fulltext) @lang .
noindex_name : string .
school : [uid] @count .
lossy : string @index(term) @lang .
occupations : [string] @index(term) .
Expand Down Expand Up @@ -288,6 +287,11 @@ boss : uid .
newfriend : [uid] .
owner : [uid] .
noconflict_pred : string @noconflict .
noindex_name : string .
noindex_age : int .
noindex_dob : datetime .
noindex_alive : bool .
noindex_salary : float .
language : [string] .
`

Expand Down Expand Up @@ -390,6 +394,29 @@ func populateCluster() {
<1> <full_name> "Michonne's large name for hashing" .

<1> <noindex_name> "Michonne's name not indexed" .
<2> <noindex_name> "King Lear's name not indexed" .
<3> <noindex_name> "Margaret's name not indexed" .
<4> <noindex_name> "Leonard's name not indexed" .

<1> <noindex_age> "21" .
<2> <noindex_age> "22" .
<3> <noindex_age> "23" .
<4> <noindex_age> "24" .

<1> <noindex_dob> "1810-11-01" .
<2> <noindex_dob> "1710-11-01" .
<3> <noindex_dob> "1610-11-01" .
<4> <noindex_dob> "1510-11-01" .

<1> <noindex_alive> "true" .
<2> <noindex_alive> "false" .
<3> <noindex_alive> "false" .
<4> <noindex_alive> "true" .

<1> <noindex_salary> "501.23" .
<2> <noindex_salary> "589.04" .
<3> <noindex_salary> "459.47" .
<4> <noindex_salary> "967.68" .

<1> <friend> <23> .
<1> <friend> <24> .
Expand Down
264 changes: 264 additions & 0 deletions query/query0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2674,6 +2674,270 @@ func TestCountUidWithAlias(t *testing.T) {
js)
}

func TestFilterNonIndexedPredicate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
query string
result string
}{
{
`Test ge filter on non-indexed string`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(ge(noindex_name, "Leonard's name not indexed")) {
noindex_name
}
}
`,
`{"data":{"me":[{"noindex_name":"Michonne's name not indexed"},{"noindex_name":"Margaret's name not indexed"},{"noindex_name":"Leonard's name not indexed"}]}}`,
},
{
`Test gt filter on non-indexed string`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(gt(noindex_name, "Leonard's name not indexed")) {
noindex_name
}
}
`,
`{"data":{"me":[{"noindex_name":"Michonne's name not indexed"},{"noindex_name":"Margaret's name not indexed"}]}}`,
},
{
`Test le filter on non-indexed string`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(le(noindex_name, "Leonard's name not indexed")) {
noindex_name
}
}
`,
`{"data":{"me":[{"noindex_name":"King Lear's name not indexed"},{"noindex_name":"Leonard's name not indexed"}]}}`,
},
{
`Test lt filter on non-indexed string`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(lt(noindex_name, "Leonard's name not indexed")){
noindex_name
}
},
`,
`{"data":{"me":[{"noindex_name":"King Lear's name not indexed"}]}}`,
},
{
`Test eq filter on non-indexed string`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(eq(noindex_name, "King Lear's name not indexed")) {
noindex_name
}
}
`,
`{"data":{"me":[{"noindex_name":"King Lear's name not indexed"}]}}`,
},
{
`Test ge filter on non-indexed int`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(ge(noindex_age, "22")) {
noindex_age
}
}
`,
`{"data":{"me":[{"noindex_age":22},{"noindex_age":23},{"noindex_age":24}]}}`,
},
{
`Test gt filter on non-indexed int`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(gt(noindex_age, "22")) {
noindex_age
}
}
`,
`{"data":{"me":[{"noindex_age":23},{"noindex_age":24}]}}`,
},
{
`Test le filter on non-indexed int`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(le(noindex_age, "22")) {
noindex_age
}
}
`,
`{"data":{"me":[{"noindex_age":21},{"noindex_age":22}]}}`,
},
{
`Test lt filter on non-indexed int`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(lt(noindex_age, "22")){
noindex_age
}
},
`,
`{"data":{"me":[{"noindex_age":21}]}}`,
},
{
`Test eq filter on non-indexed int`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(eq(noindex_age, "22")) {
noindex_age
}
}
`,
`{"data":{"me":[{"noindex_age":22}]}}`,
},
{
`Test ge filter on non-indexed datetime`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(ge(noindex_dob, "1610-11-01")) {
noindex_dob
}
}
`,
`{"data":{"me":[{"noindex_dob":"1810-11-01T00:00:00Z"},{"noindex_dob":"1710-11-01T00:00:00Z"},{"noindex_dob":"1610-11-01T00:00:00Z"}]}}`,
},
{
`Test gt filter on non-indexed datetime`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(gt(noindex_dob, "1610-11-01")) {
noindex_dob
}
}
`,
`{"data":{"me":[{"noindex_dob":"1810-11-01T00:00:00Z"},{"noindex_dob":"1710-11-01T00:00:00Z"}]}}`,
},
{
`Test le filter on non-indexed datetime`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(le(noindex_dob, "1610-11-01")) {
noindex_dob
}
}
`,
`{"data":{"me":[{"noindex_dob":"1610-11-01T00:00:00Z"},{"noindex_dob":"1510-11-01T00:00:00Z"}]}}`,
},
{
`Test lt filter on non-indexed datetime`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(lt(noindex_dob, "1610-11-01")){
noindex_dob
}
},
`,
`{"data":{"me":[{"noindex_dob":"1510-11-01T00:00:00Z"}]}}`,
},
{
`Test eq filter on non-indexed datetime`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(eq(noindex_dob, "1610-11-01")) {
noindex_dob
}
}
`,
`{"data":{"me":[{"noindex_dob":"1610-11-01T00:00:00Z"}]}}`,
},
{
`Test ge filter on non-indexed float`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(ge(noindex_salary, "589.04")) {
noindex_salary
}
}
`,
`{"data":{"me":[{"noindex_salary":589.040000},{"noindex_salary":967.680000}]}}`,
},
{
`Test gt filter on non-indexed float`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(gt(noindex_salary, "589.04")) {
noindex_salary
}
}
`,
`{"data":{"me":[{"noindex_salary":967.680000}]}}`,
},
{
`Test le filter on non-indexed float`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(le(noindex_salary, "589.04")) {
noindex_salary
}
}
`,
`{"data":{"me":[{"noindex_salary":501.230000},{"noindex_salary":589.040000},{"noindex_salary":459.470000}]}}`,
},
{
`Test lt filter on non-indexed float`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(lt(noindex_salary, "589.04")){
noindex_salary
}
},
`,
`{"data":{"me":[{"noindex_salary":501.230000},{"noindex_salary":459.470000}]}}`,
},
{
`Test eq filter on non-indexed float`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(eq(noindex_salary, "589.04")) {
noindex_salary
}
}
`,
`{"data":{"me":[{"noindex_salary":589.040000}]}}`,
},
{
`Test eq filter on non-indexed bool`,
`
{
me(func: uid(1, 2, 3, 4)) @filter(eq(noindex_alive, true)) {
uid
noindex_name
noindex_alive
}
}
`,
`{"data":{"me":[{"uid":"0x1","noindex_name":"Michonne's name not indexed","noindex_alive":true},{"uid":"0x4","noindex_name":"Leonard's name not indexed","noindex_alive":true}]}}`,
},
{
`Test filtering of non indexed predicate inside query`,
`
{
me(func: uid(0x01)) {
friend @filter(ge(survival_rate, 1.6)) {
name
survival_rate
}
}
}
`,
`{"data":{"me":[{"friend":[{"name":"Rick Grimes","survival_rate":1.600000},{"name":"Glenn Rhee","survival_rate":1.600000},{"name":"Daryl Dixon","survival_rate":1.600000},{"name":"Andrea","survival_rate":1.600000}]}]}}`,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
js := processQueryNoErr(t, tc.query)
require.JSONEq(t, js, tc.result)
})
}
}

var client *dgo.Dgraph

func TestMain(m *testing.M) {
Expand Down
17 changes: 6 additions & 11 deletions query/query1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,17 @@ func TestSchemaBlock5(t *testing.T) {
require.JSONEq(t, `{"data":{"schema":[{"predicate":"name","type":"string","index":true,"tokenizer":["term","exact","trigram"],"count":true,"lang":true}]}}`, js)
}

func TestFilterNonIndexedPredicateFail(t *testing.T) {

// filtering on non indexing predicate fails
func TestNonIndexedPredicateAtRoot(t *testing.T) {
query := `
{
me(func: uid(0x01)) {
friend @filter(le(survival_rate, 30)) {
uid
name
age
}
}
{
me(func: ge(noindex_name, "Michonne")) {
noindex_name
}
}
`
_, err := processQuery(context.Background(), t, query)
require.Error(t, err)
require.Contains(t, err.Error(), "Predicate noindex_name is not indexed")
}

func TestMultipleSamePredicateInBlockFail(t *testing.T) {
Expand Down
Loading