Skip to content

feat(spanner): Add ScaNN vector index DDL sample - #14840

Open
iennae wants to merge 7 commits into
mainfrom
feat/spanner-create-vector-index
Open

iennae wants to merge 7 commits into
mainfrom
feat/spanner-create-vector-index

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Creating a ScaNN VECTOR INDEX for approximate nearest neighbors (ANN) queries requires enforcing fixed vector_length options, filtering NULL embedding rows (WHERE Embedding IS NOT NULL), and waiting on a long-running database DDL operation.

Adds spanner_create_vector_index to demonstrate creating a ScaNN cosine vector index from Python after initial embeddings are populated.

Note: Stacked on #14836 (feat/spanner-vector-search-knn) so this PR shows only this sample's diff. It will merge into main after #14836.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

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.
@iennae
iennae requested review from a team as code owners November 17, 2026 18:48
@snippet-bot

snippet-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 17, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +35 to +40
ddl_statement = (
f"CREATE VECTOR INDEX {index_name} "
"ON Documents(DocEmbedding) "
"STORING (WordCount) "
"OPTIONS (distance_type = 'COSINE')"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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')"
    )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The DDL includes WHERE DocEmbedding IS NOT NULL, and the unit test asserts it.

Comment on lines +119 to +124
admin_client.update_database_ddl(
request=spanner_admin_database_v1.UpdateDatabaseDdlRequest(
database=db_path,
statements=["DROP VECTOR INDEX IF EXISTS DocEmbeddingIndex"],
)
).result(timeout=300)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! DROP VECTOR INDEX is supported Spanner DDL for vector indexes, so the cleanup statement stays as is.

Comment on lines +147 to +152
admin_client.update_database_ddl(
request=spanner_admin_database_v1.UpdateDatabaseDdlRequest(
database=db_path,
statements=["DROP VECTOR INDEX IF EXISTS DocEmbeddingIndex"],
)
).result(timeout=300)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Base automatically changed from feat/spanner-vector-search-knn to main December 1, 2026 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants