Skip to content

feat(spanner): Add PostgreSQL-dialect $1 parameterized query sample - #14937

Open
iennae wants to merge 2 commits into
feat/spanner-postgresql-dml-last-statementfrom
feat/spanner-postgresql-query-with-parameter
Open

iennae wants to merge 2 commits into
feat/spanner-postgresql-dml-last-statementfrom
feat/spanner-postgresql-query-with-parameter

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_postgresql_query_with_parameter binding positional $1 (p1) query parameters in PostgreSQL-dialect SQL.

Note: Stacked on #14851 (feat/spanner-postgresql-dml-last-statement) so this PR shows only this sample's diff. It will merge into main after #14851.

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

PostgreSQL-dialect Spanner queries bind positional parameters ($1 -> p1) via params and param_types on snapshot.execute_sql(). Add a standalone spanner_postgresql_query_with_parameter sample.
@iennae
iennae requested review from a team as code owners November 18, 2026 05:14
@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

@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

@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 sample script postgresql_query_with_parameter.py and its corresponding tests to demonstrate executing parameterized PostgreSQL-dialect SQL queries on Cloud Spanner using the $1 syntax. The feedback suggests improving the script's support for Application Default Credentials (ADC) by allowing the project_id parameter to be optional. Specifically, it is recommended to update the type annotation of project_id to str | None and to remove the hardcoded fallback default string "example-project-id" from the command-line argument parser, enabling spanner.Client to automatically infer the project ID when it is not explicitly provided.

Comment on lines +23 to +25
def query_postgresql_with_parameter(
project_id: str, instance_id: str, database_id: str
) -> None:

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

To support project ID auto-inference from Application Default Credentials (ADC) when --project-id is omitted, update the type annotation of project_id to accept None (i.e., str | None). This allows spanner.Client(project=project_id) to be called with None, triggering the auto-inference behavior.

Suggested change
def query_postgresql_with_parameter(
project_id: str, instance_id: str, database_id: str
) -> None:
def query_postgresql_with_parameter(
project_id: str | None, instance_id: str, database_id: str
) -> None:
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.

Comment on lines +75 to +79
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

According to the repository's general rules, the --project-id argument should be optional and allow the project to be inferred from Application Default Credentials (ADC) if not provided. Currently, it defaults to the hardcoded string "example-project-id" when the GOOGLE_CLOUD_PROJECT environment variable is not set, which overrides and breaks ADC auto-inference.

We should remove the fallback default string so that it defaults to None when neither the argument nor the environment variable is provided, allowing spanner.Client to automatically infer the project ID.

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.

…h_parameter

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/postgresql/src/spanner_postgresql/postgresql_query_with_parameter.py
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