diff --git a/query/common_test.go b/query/common_test.go index ec7951a6370..90d38d5919f 100644 --- a/query/common_test.go +++ b/query/common_test.go @@ -217,6 +217,11 @@ type Person { alive } +type Person2 { + name2 + age2 +} + type Animal { name } @@ -332,6 +337,8 @@ tweet-a : string @index(trigram) . tweet-b : string @index(term) . tweet-c : string @index(fulltext) . tweet-d : string @index(trigram) . +name2 : string @index(term) . +age2 : int @index(int) . ` func populateCluster() { @@ -629,6 +636,7 @@ func populateCluster() { <5> "Pet" . <6> "Animal" . <6> "Pet" . + <23> "Person" . <24> "Person" . <25> "Person" . @@ -638,6 +646,8 @@ func populateCluster() { <34> "SchoolInfo" . <35> "SchoolInfo" . <36> "SchoolInfo" . + <40> "Person2" . + <41> "Person2" . <11100> "Node" . <2> <5> . @@ -851,6 +861,9 @@ func populateCluster() { <61> "aaabxxx" . <62> "aaacdxx" . <63> "aaabcd" . + + <40> "Alice" . + <41> "20" . `) if err != nil { panic(fmt.Sprintf("Could not able add triple to the cluster. Got error %v", err.Error())) diff --git a/query/query0_test.go b/query/query0_test.go index cdc381e8c10..21bc33b58bc 100644 --- a/query/query0_test.go +++ b/query/query0_test.go @@ -546,6 +546,20 @@ func TestCascadeWithSort(t *testing.T) { require.JSONEq(t, `{"data":{"me":[{"name": "Daryl Dixon","alive": false},{"name": "Rick Grimes","alive": true}]}}`, js) } +// Regression test for issue described in https://github.com/dgraph-io/dgraph/pull/8441 +func TestNegativeOffset(t *testing.T) { + query := ` + { + me(func: type(Person2), offset: -1, orderasc: age2) { + name2 + age2 + } + } + ` + js := processQueryNoErr(t, query) + require.JSONEq(t, `{"data":{"me":[{"age2":20},{"name2":"Alice"}]}}`, js) +} + func TestLevelBasedFacetVarAggSum(t *testing.T) { query := ` { diff --git a/worker/sort.go b/worker/sort.go index 497a4279dc8..6392d51b9b7 100644 --- a/worker/sort.go +++ b/worker/sort.go @@ -329,7 +329,9 @@ BUCKETS: // Apply the offset on null nodes, if the nodes with value were not enough. if out[i].offset < len(nullNodes) { - nullNodes = nullNodes[out[i].offset:] + if out[i].offset >= 0 { + nullNodes = nullNodes[out[i].offset:] + } } else { nullNodes = nullNodes[:0] }