Repository navigation
Conversation
Unlike buffered mutations, GoogleSQL DML writes are buffered server-side and immediately visible to subsequent SQL reads in the same transaction. Add spanner_dml_write_then_read demonstrating read-your-writes.
|
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 adds a new Spanner DML sample dml_write_then_read.py and its associated tests, demonstrating how to perform a write followed by a read within the same transaction. The review feedback suggests several improvements to simplify and correct the sample: removing the unused spanner_v1 import and its type annotation, updating the project_id type and command-line argument default to allow falling back to Application Default Credentials (ADC), and correcting the type annotation of read_rows to match the list-like objects returned at runtime.
| from google.api_core import exceptions | ||
| from google.cloud import spanner | ||
| from google.cloud import spanner_v1 |
There was a problem hiding this comment.
Remove the unused spanner_v1 import. The type annotation on the nested helper function can be omitted to keep the sample simple and idiomatic.
| from google.api_core import exceptions | |
| from google.cloud import spanner | |
| from google.cloud import spanner_v1 | |
| from google.api_core import exceptions | |
| from google.cloud import spanner |
There was a problem hiding this comment.
Thanks! Keeping the annotation: the samples in this set are fully type-annotated, and spanner_v1.transaction.Transaction is the type the callback receives. It isn't exposed on the top-level spanner namespace, so the spanner_v1 import stays for the annotation only.
| from google.cloud import spanner_v1 | ||
|
|
||
|
|
||
| def write_and_read_dml(project_id: str, instance_id: str, database_id: str) -> None: |
There was a problem hiding this comment.
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.
| client = spanner.Client(project=project_id) | ||
| database = client.instance(instance_id).database(database_id) | ||
| inserted_rows = 0 | ||
| read_rows: list[tuple[str, str]] = [] |
There was a problem hiding this comment.
The rows returned by transaction.execute_sql are list-like objects (lists of values), not tuples. Therefore, the type annotation for read_rows should be list[list[str]] or list[list[Any]] instead of list[tuple[str, str]] to accurately reflect the runtime types.
| read_rows: list[tuple[str, str]] = [] | |
| read_rows: list[list[str]] = [] |
There was a problem hiding this comment.
Done, thank you: annotated as list[list[str]] (492daa9).
| inserted_rows = 0 | ||
| read_rows: list[tuple[str, str]] = [] | ||
|
|
||
| def write_then_read(transaction: spanner_v1.transaction.Transaction) -> None: |
There was a problem hiding this comment.
In Google Cloud Spanner Python samples, the nested transaction helper function's parameter is typically left unannotated to keep the code simple and avoid exposing internal implementation details like spanner_v1.transaction.Transaction.
| def write_then_read(transaction: spanner_v1.transaction.Transaction) -> None: | |
| def write_then_read(transaction) -> None: |
There was a problem hiding this comment.
Thanks! Keeping the annotation: the samples in this set are fully type-annotated, and spanner_v1.transaction.Transaction is the type the callback receives. It isn't exposed on the top-level spanner namespace, so the spanner_v1 import stays for the annotation only.
| 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.
Update the --project-id argument definition to default to None (by omitting the hardcoded "example-project-id" fallback) so that the Spanner client can fallback to Application Default Credentials (ADC) when the argument is not specified.
| 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.
Problem: review of this sample identified correctness and robustness improvements in the sample and its tests. Solution: apply the accepted review suggestions and extend the tests to cover the updated behavior. Details are in the review thread replies. Files updated: - spanner/dml/src/spanner_dml/dml_write_then_read.py
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/dml/src/spanner_dml/dml_write_then_read.py
Description
Adds
spanner_dml_write_then_readdemonstrating a DMLINSERTfollowed by an immediateSELECTof the uncommitted row in the same transaction.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions