From 82999154a11b518001002d07c8bc71daa1fcf091 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Mon, 14 Oct 2019 10:33:37 -0700 Subject: [PATCH] Cherry pick PR #3205 into release/v1.0 branch. (#4143) This fixes #3807. --- query/common_test.go | 4 +++ query/query.go | 35 +++++++++--------- query/query3_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 17 deletions(-) diff --git a/query/common_test.go b/query/common_test.go index 0c6bf933821..11abda6c940 100644 --- a/query/common_test.go +++ b/query/common_test.go @@ -329,6 +329,7 @@ symbol : string @index(exact) . room : string @index(term) . office.room : uid . number : int @index(int) . +node : uid . ` err := schema.ParseBytes([]byte(schemaStr), 1) @@ -673,4 +674,7 @@ number : int @index(int) . addEdgeToUID(t, "office.room", 4001, 4002, nil) addEdgeToUID(t, "office.room", 4001, 4003, nil) addEdgeToUID(t, "office.room", 4001, 4004, nil) + + addEdgeToValue(t, "name", 11100, "expand", nil) + addEdgeToUID(t, "node", 11100, 11100, nil) } diff --git a/query/query.go b/query/query.go index c93c4cc3f64..a45c38d4c1c 100644 --- a/query/query.go +++ b/query/query.go @@ -1969,23 +1969,6 @@ func ProcessGraph(ctx context.Context, sg, parent *SubGraph, rch chan error) { } } - if sg.DestUIDs == nil || len(sg.DestUIDs.Uids) == 0 { - // Looks like we're done here. Be careful with nil srcUIDs! - if span != nil { - span.Annotatef(nil, "Zero uids for %q", sg.Attr) - } - out := sg.Children[:0] - for _, child := range sg.Children { - if child.IsInternal() && child.Attr == "expand" { - continue - } - out = append(out, child) - } - sg.Children = out // Remove any expand nodes we might have added. - rch <- nil - return - } - // Run filters if any. if len(sg.Filters) > 0 { // Run all filters in parallel. @@ -2135,6 +2118,24 @@ func ProcessGraph(ctx context.Context, sg, parent *SubGraph, rch chan error) { childErr = err } } + + if sg.DestUIDs == nil || len(sg.DestUIDs.Uids) == 0 { + // Looks like we're done here. Be careful with nil srcUIDs! + if span != nil { + span.Annotatef(nil, "Zero uids for %q", sg.Attr) + } + out := sg.Children[:0] + for _, child := range sg.Children { + if child.IsInternal() && child.Attr == "expand" { + continue + } + out = append(out, child) + } + sg.Children = out // Remove any expand nodes we might have added. + rch <- nil + return + } + rch <- childErr } diff --git a/query/query3_test.go b/query/query3_test.go index 53625a1b4bf..251782b968d 100644 --- a/query/query3_test.go +++ b/query/query3_test.go @@ -1778,3 +1778,88 @@ func TestFilterRegex16(t *testing.T) { `{"data": {"me":[{"name@ru":"Артём Ткаченко"}]}}`, js) } + +func TestNestedExpandAll(t *testing.T) { + query := `{ + q(func: has(node)) { + uid + expand(_all_) { + uid + node { + uid + expand(_all_) + } + } + } + }` + js := processToFastJsonNoErr(t, query) + require.JSONEq(t, `{"data": { + "q": [ + { + "uid": "0x2b5c", + "name": "expand", + "node": [ + { + "uid": "0x2b5c", + "node": [ + { + "uid": "0x2b5c", + "name": "expand" + } + ] + } + ] + } + ]}}`, js) +} + +func TestNoResultsFilter(t *testing.T) { + query := `{ + q(func: has(nonexistent_pred)) @filter(le(name, "abc")) { + uid + } + }` + js := processToFastJsonNoErr(t, query) + require.JSONEq(t, `{"data": {"q": []}}`, js) +} + +func TestNoResultsPagination(t *testing.T) { + query := `{ + q(func: has(nonexistent_pred), first: 50) { + uid + } + }` + js := processToFastJsonNoErr(t, query) + require.JSONEq(t, `{"data": {"q": []}}`, js) +} + +func TestNoResultsGroupBy(t *testing.T) { + query := `{ + q(func: has(nonexistent_pred)) @groupby(name) { + count(uid) + } + }` + js := processToFastJsonNoErr(t, query) + require.JSONEq(t, `{"data": {}}`, js) +} + +func TestNoResultsOrder(t *testing.T) { + query := `{ + q(func: has(nonexistent_pred), orderasc: name) { + uid + } + }` + js := processToFastJsonNoErr(t, query) + require.JSONEq(t, `{"data": {"q": []}}`, js) +} + +func TestNoResultsCount(t *testing.T) { + query := `{ + q(func: has(nonexistent_pred)) { + uid + count(friend) + } + }` + js := processToFastJsonNoErr(t, query) + require.JSONEq(t, `{"data": {"q": []}}`, js) +}