Skip to content

feat(spanner): Add approximate nearest neighbors search sample - #14841

Open
iennae wants to merge 4 commits into
mainfrom
feat/spanner-vector-search-ann
Open

iennae wants to merge 4 commits into
mainfrom
feat/spanner-vector-search-ann

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Approximate nearest neighbors (ANN) queries in Cloud Spanner require pairing @{FORCE_INDEX=...} with APPROX_COSINE_DISTANCE(..., options => JSON '{"num_leaves_to_search": ...}') and matching the WHERE Embedding IS NOT NULL predicate defined on the vector index.

Adds spanner_vector_search_ann showing parameterized FLOAT32 query vector binding against a ScaNN vector index.

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

@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 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.

Comment on lines +38 to +49
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
"""

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, 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.

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

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 query includes the Embedding IS NOT NULL predicate alongside FORCE_INDEX.

Comment on lines +49 to +51
sql = snapshot.execute_sql.call_args.args[0]
assert "APPROX_COSINE_DISTANCE" in sql
assert "FORCE_INDEX=CatalogEmbeddingIndex" in sql

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.

medium

Update the test assertions to verify that the query includes the required Embedding IS NOT NULL predicate.

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

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 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.
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