Skip to content

Commit

Permalink
fix(vector):fix similar_to() error return when data is not present (#…
Browse files Browse the repository at this point in the history
…9084)

When no elements are present in the HNSW tree, we return an error saying so. However, we only need to return nil result.
  • Loading branch information
shivaji-dgraph committed May 9, 2024
1 parent 5451b77 commit 2a3dacd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
7 changes: 6 additions & 1 deletion query/vector/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ func querySingleVectorError(t *testing.T, vector, pred string, validateError boo
return []float32{}, err
}

if len(data.Vector) == 0 {
return []float32{}, nil
}

return data.Vector[0].VTest, nil
}

Expand Down Expand Up @@ -569,8 +573,9 @@ func TestVectorDelete(t *testing.T) {
}

triple := deleteTriple(len(triples) - 2)
// after deleteing all vectors, we should get an empty array of vectors in response when we do silimar_to query
_, err = querySingleVectorError(t, strings.Split(triple, `"`)[1], "vtest", false)
require.NotNil(t, err)
require.NoError(t, err)
}

func TestVectorUpdate(t *testing.T) {
Expand Down
26 changes: 26 additions & 0 deletions systest/vector/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,29 @@ func TestVectorIndexRebuilding(t *testing.T) {

testVectorQuery(t, gc, vectors, rdfs, pred, numVectors)
}

func TestVectorIndexOnVectorPredWithoutData(t *testing.T) {
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1).WithACL(time.Hour)
c, err := dgraphtest.NewLocalCluster(conf)
require.NoError(t, err)
defer func() { c.Cleanup(t.Failed()) }()
require.NoError(t, c.Start())

gc, cleanup, err := c.Client()
require.NoError(t, err)
defer cleanup()
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphtest.DefaultUser, dgraphtest.DefaultPassword, x.GalaxyNamespace))

hc, err := c.HTTPClient()
require.NoError(t, err)
require.NoError(t, hc.LoginIntoNamespace(dgraphtest.DefaultUser,
dgraphtest.DefaultPassword, x.GalaxyNamespace))

require.NoError(t, gc.SetupSchema(testSchema))
pred := "project_discription_v"

vector := []float32{1.0, 2.0, 3.0}
_, err = gc.QueryMultipleVectorsUsingSimilarTo(vector, pred, 10)
require.NoError(t, err)
}
1 change: 1 addition & 0 deletions tok/hnsw/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
DotProd = "dotproduct"
plError = "\nerror fetching posting list for data key: "
dataError = "\nerror fetching data for data key: "
EmptyHNSWTreeError = "HNSW tree has no elements"
VecKeyword = "__vector_"
visitedVectorsLevel = "visited_vectors_level_"
distanceComputations = "vector_distance_computations"
Expand Down
4 changes: 2 additions & 2 deletions tok/hnsw/persistent_hnsw.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ func (ph *persistentHNSW[T]) calculateNewEntryVec(
})

if err != nil {
return 0, errors.Wrapf(err, "HNSW tree has no elements")
return 0, errors.Wrapf(err, EmptyHNSWTreeError)
}
if itr == 0 {
return itr, errors.New("HNSW tree has no elements")
return itr, errors.New(EmptyHNSWTreeError)
}

return itr, nil
Expand Down
2 changes: 1 addition & 1 deletion worker/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (qs *queryState) handleValuePostings(ctx context.Context, args funcArgs) er
int(numNeighbors), index.AcceptAll[float32])
}

if err != nil {
if err != nil && !strings.Contains(err.Error(), hnsw.EmptyHNSWTreeError+": "+badger.ErrKeyNotFound.Error()) {
return err
}
sort.Slice(nnUids, func(i, j int) bool { return nnUids[i] < nnUids[j] })
Expand Down

0 comments on commit 2a3dacd

Please sign in to comment.