Repository navigation
Conversation
PostgreSQL JSONB columns support field extraction via ->> combined with positional $1 query parameters. Add a standalone spanner_postgresql_jsonb_query_parameter sample querying venues.
|
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 and tests for querying a PostgreSQL-dialect Spanner database using JSONB extraction and parameterized queries. The implementation includes a CLI entrypoint and comprehensive tests. The feedback recommends updating the project_id parameter and command-line argument to be optional (defaulting to None instead of a hardcoded fallback string) to allow the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).
| def query_postgresql_jsonb_parameter( | ||
| project_id: str, instance_id: str, database_id: str | ||
| ) -> None: |
There was a problem hiding this comment.
To allow the project ID to be automatically inferred from Application Default Credentials (ADC) when not explicitly provided, update the type annotation of project_id to accept None.
| def query_postgresql_jsonb_parameter( | |
| project_id: str, instance_id: str, database_id: str | |
| ) -> None: | |
| def query_postgresql_jsonb_parameter( | |
| project_id: str | None, instance_id: str, database_id: str | |
| ) -> None: |
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.
Avoid hardcoding a fallback dummy project ID like 'example-project-id' as the default value. If GOOGLE_CLOUD_PROJECT is not set, defaulting to None allows the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).
| 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
argparsefor a command-line script, the--project_idargument 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.
…ry_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_jsonb_query_parameter.py - spanner/postgresql/tests/postgresql_jsonb_query_parameter_test.py
Description
Adds
spanner_postgresql_jsonb_query_parameterfiltering a PostgreSQLJSONBcolumn using->>extraction and a bound$1parameter.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions