Skip to content

Commit

Permalink
Fix _count_ for queries with sort order and filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawan Rawal committed Dec 26, 2016
1 parent 6fb7ab3 commit e03a584
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
12 changes: 8 additions & 4 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,13 @@ func ProcessGraph(ctx context.Context, sg, parent *SubGraph, rch chan error) {
return
}
} else {
// We need to sort first before pagination.
if err = sg.applyOrderAndPagination(ctx); err != nil {
rch <- err
return
// If we are asked for count, we don't need to change the order of results.
if !sg.Params.DoCount {
// We need to sort first before pagination.
if err = sg.applyOrderAndPagination(ctx); err != nil {
rch <- err
return
}
}
}

Expand All @@ -608,6 +611,7 @@ func ProcessGraph(ctx context.Context, sg, parent *SubGraph, rch chan error) {
for i, ul := range sg.uidMatrix {
// A possible optimization is to return the size of the intersection
// without forming the intersection.

algo.IntersectWith(ul, sg.DestUIDs)
sg.counts[i] = uint32(len(ul.Uids))
}
Expand Down
31 changes: 27 additions & 4 deletions query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func TestToJSON(t *testing.T) {
me(_uid_:0x01) {
name
gender
alive
alive
friend {
name
}
Expand Down Expand Up @@ -1514,7 +1514,6 @@ func TestToJSONOrder(t *testing.T) {
js)
}

/*
// Test sorting / ordering by dob.
func TestToJSONOrderDesc(t *testing.T) {
dir, dir2, _ := populateGraph(t)
Expand All @@ -1536,10 +1535,34 @@ func TestToJSONOrderDesc(t *testing.T) {

js := processToJSON(t, query)
require.JSONEq(t,
`{"me":[{"friend":[{"dob":"1901-01-15","name":"Andrea"},{"dob":"1909-01-10","name":"Daryl Dixon"},{"dob":"1909-05-05","name":"Glenn Rhee"},{"dob":"1910-01-02","name":"Rick Grimes"}],"gender":"female","name":"Michonne"}]}`,
`{"me":[{"friend":[{"dob":"1910-01-02","name":"Rick Grimes"},{"dob":"1909-05-05","name":"Glenn Rhee"},{"dob":"1909-01-10","name":"Daryl Dixon"},{"dob":"1901-01-15","name":"Andrea"}],"gender":"female","name":"Michonne"}]}`,
string(js))
}
*/

// Test sorting / ordering by dob and count.
func TestToJSONOrderDescCount(t *testing.T) {
dir, dir2, _ := populateGraph(t)
defer os.RemoveAll(dir)
defer os.RemoveAll(dir2)

query := `
{
me(_uid_:0x01) {
name
gender
friend @filter(anyof("name", "Rick")) (order: dob) {
_count_
}
}
}
`

js := processToJSON(t, query)
require.JSONEq(t,
`{"me":[{"friend":[{"_count_":1}],"gender":"female","name":"Michonne"}]}`,
string(js))
}

// Test sorting / ordering by dob.
func TestToJSONOrderOffset(t *testing.T) {
dir, dir2, ps := populateGraph(t)
Expand Down

0 comments on commit e03a584

Please sign in to comment.