Skip to content

Commit

Permalink
fix: Filtering on unique index if there is no match (sourcenetwork#2177)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves sourcenetwork#2176

## Description

Make unique index distinguish between key-not-found and other errors.
  • Loading branch information
islamaliev authored and shahzadlone committed Jan 20, 2024
1 parent 9351f27 commit 883c9c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions db/fetcher/indexer_iterators.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"

"github.com/fxamacker/cbor/v2"
ds "github.com/ipfs/go-datastore"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/connor"
Expand Down Expand Up @@ -141,6 +142,9 @@ func (i *eqSingleIndexIterator) Next() (indexIterResult, error) {
i.indexKey.FieldValues = [][]byte{i.value}
val, err := i.store.Get(i.ctx, i.indexKey.ToDS())
if err != nil {
if errors.Is(err, ds.ErrNotFound) {
return indexIterResult{key: i.indexKey}, nil
}
return indexIterResult{}, err
}
i.store = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,36 @@ func TestQueryWithUniqueIndex_WithNotLikeFilter_ShouldFetch(t *testing.T) {

testUtils.ExecuteTestCase(t, test)
}

func TestQueryWithUniqueIndex_IfNoMatch_ReturnEmptyResult(t *testing.T) {
test := testUtils.TestCase{
Description: "If filter does not match any document, return empty result",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type User {
name: String
age: Int @index(unique: true)
}
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: ` {
"name": "Shahzad",
"age": 23
}`,
},
testUtils.Request{
Request: `query {
User(filter: {age: {_eq: 20}}) {
name
}
}`,
Results: []map[string]any{},
},
},
}

testUtils.ExecuteTestCase(t, test)
}

0 comments on commit 883c9c6

Please sign in to comment.