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.
Approximate nearest neighbors search in Cloud Spanner requires creating a NON NULL-filtered ScaNN vector index via asynchronous database DDL, which previously had only SQL examples.
|
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 script to create a ScaNN vector index on a Cloud Spanner database, along with corresponding unit and integration tests. Feedback highlights two critical issues: the DDL statement for creating the vector index is missing the required WHERE DocEmbedding IS NOT NULL clause, and the tests incorrectly use DROP VECTOR INDEX instead of the standard DROP INDEX statement, which will cause syntax errors in Cloud Spanner.
| ddl_statement = ( | ||
| f"CREATE VECTOR INDEX {index_name} " | ||
| "ON Documents(DocEmbedding) " | ||
| "STORING (WordCount) " | ||
| "OPTIONS (distance_type = 'COSINE')" | ||
| ) |
There was a problem hiding this comment.
In Cloud Spanner, creating a vector index requires a WHERE clause that filters out NULL values (e.g., WHERE DocEmbedding IS NOT NULL). Without this clause, the DDL statement will fail to execute. This is also mentioned in your PR description but was omitted in the implementation.
ddl_statement = (
f"CREATE VECTOR INDEX {index_name} "
"ON Documents(DocEmbedding) "
"STORING (WordCount) "
"WHERE DocEmbedding IS NOT NULL "
"OPTIONS (distance_type = 'COSINE')"
)There was a problem hiding this comment.
Thanks! The DDL includes WHERE DocEmbedding IS NOT NULL, and the unit test asserts it.
| admin_client.update_database_ddl( | ||
| request=spanner_admin_database_v1.UpdateDatabaseDdlRequest( | ||
| database=db_path, | ||
| statements=["DROP VECTOR INDEX IF EXISTS DocEmbeddingIndex"], | ||
| ) | ||
| ).result(timeout=300) |
There was a problem hiding this comment.
Cloud Spanner does not have a DROP VECTOR INDEX statement. To delete any index (including a vector index), you must use the standard DROP INDEX statement. Using DROP VECTOR INDEX will result in a syntax error.
| admin_client.update_database_ddl( | |
| request=spanner_admin_database_v1.UpdateDatabaseDdlRequest( | |
| database=db_path, | |
| statements=["DROP VECTOR INDEX IF EXISTS DocEmbeddingIndex"], | |
| ) | |
| ).result(timeout=300) | |
| admin_client.update_database_ddl( | |
| request=spanner_admin_database_v1.UpdateDatabaseDdlRequest( | |
| database=db_path, | |
| statements=["DROP INDEX IF EXISTS DocEmbeddingIndex"], | |
| ) | |
| ).result(timeout=300) |
There was a problem hiding this comment.
Thanks! DROP VECTOR INDEX is supported Spanner DDL for vector indexes, so the cleanup statement stays as is.
| admin_client.update_database_ddl( | ||
| request=spanner_admin_database_v1.UpdateDatabaseDdlRequest( | ||
| database=db_path, | ||
| statements=["DROP VECTOR INDEX IF EXISTS DocEmbeddingIndex"], | ||
| ) | ||
| ).result(timeout=300) |
There was a problem hiding this comment.
Cloud Spanner does not have a DROP VECTOR INDEX statement. To delete any index (including a vector index), you must use the standard DROP INDEX statement. Using DROP VECTOR INDEX will result in a syntax error.
| admin_client.update_database_ddl( | |
| request=spanner_admin_database_v1.UpdateDatabaseDdlRequest( | |
| database=db_path, | |
| statements=["DROP VECTOR INDEX IF EXISTS DocEmbeddingIndex"], | |
| ) | |
| ).result(timeout=300) | |
| admin_client.update_database_ddl( | |
| request=spanner_admin_database_v1.UpdateDatabaseDdlRequest( | |
| database=db_path, | |
| statements=["DROP INDEX IF EXISTS DocEmbeddingIndex"], | |
| ) | |
| ).result(timeout=300) |
There was a problem hiding this comment.
Thanks! DROP VECTOR INDEX is supported Spanner DDL for vector indexes, so the cleanup statement stays as is.
Including WHERE DocEmbedding IS NOT NULL allows the vector index DDL to succeed on both NOT NULL and nullable embedding columns.
Prints short resource identifiers instead of full project-prefixed resource paths and omits project_id from diagnostic messages.
Prints only runtime resource and query results instead of hardcoded string literals.
Problem: the sample waits on a long-running operation with a client-side timeout. When that wait expires the client raises TimeoutError, which is not an API call error, so the sample ended with an unhandled traceback. Solution: catch TimeoutError and print a clear message to stderr noting that only the wait timed out and the operation may still be running (the timeout does not cancel it). Add a unit test covering the new handler.
Problem: multi-line SQL and DDL statements were written as adjacent string fragments. That style is easy to get wrong (a missing trailing space joins two SQL tokens) and is harder to read than the statement itself, and the samples in this set mixed it with triple-quoted SQL. Solution: write each multi-line statement as a single triple-quoted string, matching the layout already used by the other samples. Only whitespace between SQL tokens changes; the statements are otherwise identical. Files updated: - spanner/search_and_ai/src/spanner_search_and_ai/create_vector_index.py
Description
Creating a ScaNN
VECTOR INDEXfor approximate nearest neighbors (ANN) queries requires enforcing fixedvector_lengthoptions, filteringNULLembedding rows (WHERE Embedding IS NOT NULL), and waiting on a long-running database DDL operation.Adds
spanner_create_vector_indexto demonstrate creating a ScaNN cosine vector index from Python after initial embeddings are populated.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions