Skip to content

feat(spanner): Add DirectedReadOptions replica routing sample - #14923

Open
iennae wants to merge 1 commit into
feat/spanner-select-for-updatefrom
feat/spanner-directed-read
Open

iennae wants to merge 1 commit into
feat/spanner-select-for-updatefrom
feat/spanner-directed-read

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_directed_read configuring DirectedReadOptions on spanner.Client and overriding replica selection on snapshot.execute_sql().

Note: Stacked on #14839 (feat/spanner-select-for-update) so this PR shows only this sample's diff. It will merge into main after #14839.

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

Directed reads route single-use read-only queries to specific replica types or regions via DirectedReadOptions. Add a standalone spanner_directed_read sample demonstrating client and per-query options.
@iennae
iennae requested review from a team as code owners November 18, 2026 04:54
@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 18, 2026
@snippet-bot

snippet-bot Bot commented Nov 18, 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 directed_read.py and corresponding tests to demonstrate executing snapshot SQL reads in Google Cloud Spanner using client-level and request-level DirectedReadOptions. The feedback suggests improving the usability of the script by allowing the project_id parameter and CLI argument to default to None instead of a hardcoded fallback string, enabling the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).

Comment on lines +23 to +30
def directed_read(project_id: str, instance_id: str, database_id: str) -> None:
"""Executes a snapshot SQL read using client and per-request DirectedReadOptions.

Args:
project_id: The Google Cloud project ID.
instance_id: The Spanner instance ID.
database_id: The Spanner database ID containing Albums.
"""

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

Allow project_id to be optional (str | None) so that the Spanner client can automatically infer the project ID from Application Default Credentials (ADC) when it is not explicitly provided.

def directed_read(
    project_id: str | None, instance_id: str, database_id: str
) -> None:
    """Executes a snapshot SQL read using client and per-request DirectedReadOptions.

    Args:
        project_id: The Google Cloud project ID. If None, it is inferred from ADC.
        instance_id: The Spanner instance ID.
        database_id: The Spanner database ID containing Albums.
    """

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 for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.

Comment on lines +94 to +98
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)

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

Change the default value of --project-id to os.environ.get("GOOGLE_CLOUD_PROJECT") (which defaults to None if not set) instead of a hardcoded fallback string like "example-project-id". This allows the Spanner client to fall back to Application Default Credentials (ADC) to infer the project ID when neither the command-line argument nor the environment variable is provided.

Suggested change
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)
References
  1. When using argparse for a command-line script, the --project_id argument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.

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 for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.

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.

1 participant