Repository navigation
Conversation
Python developers implementing semantic similarity search on Cloud Spanner lacked a client library example showing parameterized FLOAT32 vector bindings with COSINE_DISTANCE.
Querying a Cloud Spanner ScaNN vector index requires forcing the index on the base table and passing num_leaves_to_search in a JSON options struct, which lacked a Python client example.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new module vector_search_ann.py along with its unit and live tests to perform approximate nearest neighbor (ANN) vector searches in Cloud Spanner. The feedback highlights a critical requirement for Cloud Spanner vector indexes when using FORCE_INDEX: the SQL query must include a WHERE Embedding IS NOT NULL predicate to match the index definition. Suggestions are provided to update both the SQL query in the main code and the corresponding assertions in the test suite to ensure the query executes successfully.
| sql = """ | ||
| SELECT ItemId, Title, | ||
| APPROX_COSINE_DISTANCE( | ||
| @query_vector, | ||
| Embedding, | ||
| options => JSON '{"num_leaves_to_search": 10}' | ||
| ) AS approx_distance | ||
| FROM CatalogItems@{FORCE_INDEX=CatalogEmbeddingIndex} | ||
| WHERE WordCount > @min_words | ||
| ORDER BY approx_distance | ||
| LIMIT @top_k | ||
| """ |
There was a problem hiding this comment.
In Cloud Spanner, vector indexes are filtered indexes that require a WHERE column IS NOT NULL clause (e.g., WHERE Embedding IS NOT NULL). When querying using FORCE_INDEX on such an index, the query's WHERE clause must also include the Embedding IS NOT NULL predicate to match the index definition. Otherwise, Spanner will return an error indicating that the index cannot be used.
| sql = """ | |
| SELECT ItemId, Title, | |
| APPROX_COSINE_DISTANCE( | |
| @query_vector, | |
| Embedding, | |
| options => JSON '{"num_leaves_to_search": 10}' | |
| ) AS approx_distance | |
| FROM CatalogItems@{FORCE_INDEX=CatalogEmbeddingIndex} | |
| WHERE WordCount > @min_words | |
| ORDER BY approx_distance | |
| LIMIT @top_k | |
| """ | |
| sql = """ | |
| SELECT ItemId, Title, | |
| APPROX_COSINE_DISTANCE( | |
| @query_vector, | |
| Embedding, | |
| options => JSON '{"num_leaves_to_search": 10}' | |
| ) AS approx_distance | |
| FROM CatalogItems@{FORCE_INDEX=CatalogEmbeddingIndex} | |
| WHERE WordCount > @min_words AND Embedding IS NOT NULL | |
| ORDER BY approx_distance | |
| LIMIT @top_k | |
| """ |
There was a problem hiding this comment.
Thanks! The query includes the Embedding IS NOT NULL predicate alongside FORCE_INDEX.
| sql = snapshot.execute_sql.call_args.args[0] | ||
| assert "APPROX_COSINE_DISTANCE" in sql | ||
| assert "FORCE_INDEX=CatalogEmbeddingIndex" in sql |
There was a problem hiding this comment.
Update the test assertions to verify that the query includes the required Embedding IS NOT NULL predicate.
| sql = snapshot.execute_sql.call_args.args[0] | |
| assert "APPROX_COSINE_DISTANCE" in sql | |
| assert "FORCE_INDEX=CatalogEmbeddingIndex" in sql | |
| sql = snapshot.execute_sql.call_args.args[0] | |
| assert "APPROX_COSINE_DISTANCE" in sql | |
| assert "FORCE_INDEX=CatalogEmbeddingIndex" in sql | |
| assert "Embedding IS NOT NULL" in sql |
There was a problem hiding this comment.
Thanks! The test asserts that the query contains Embedding IS NOT NULL.
Queries forcing a NULL-filtered ScaNN vector index must include the matching IS NOT NULL predicate in their WHERE clause.
Prints short resource identifiers instead of full project-prefixed resource paths and omits project_id from diagnostic messages.
Description
Approximate nearest neighbors (ANN) queries in Cloud Spanner require pairing
@{FORCE_INDEX=...}withAPPROX_COSINE_DISTANCE(..., options => JSON '{"num_leaves_to_search": ...}')and matching theWHERE Embedding IS NOT NULLpredicate defined on the vector index.Adds
spanner_vector_search_annshowing parameterizedFLOAT32query vector binding against a ScaNN vector index.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions