Repository navigation
Conversation
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.
|
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 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.
| def query_postgresql_with_parameter( | ||
| project_id: str, instance_id: str, database_id: str | ||
| ) -> None: |
There was a problem hiding this comment.
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.
| 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
- 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.
There was a problem hiding this comment.
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.
| parser.add_argument( | ||
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | ||
| ) |
There was a problem hiding this comment.
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.
| 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
- 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.
There was a problem hiding this comment.
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
Description
Adds
spanner_postgresql_query_with_parameterbinding positional$1(p1) query parameters in PostgreSQL-dialect SQL.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions