Skip to content

feat(spanner): Add GoogleSQL DML write-then-read transaction sample - #14909

Open
iennae wants to merge 3 commits into
feat/spanner-dml-last-statementfrom
feat/spanner-dml-write-then-read
Open

iennae wants to merge 3 commits into
feat/spanner-dml-last-statementfrom
feat/spanner-dml-write-then-read

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_dml_write_then_read demonstrating a DML INSERT followed by an immediate SELECT of the uncommitted row in the same transaction.

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

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

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.
@iennae
iennae requested review from a team as code owners November 18, 2026 04:53
@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 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.

Comment on lines +18 to +20
from google.api_core import exceptions
from google.cloud import spanner
from google.cloud import spanner_v1

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

Remove the unused spanner_v1 import. The type annotation on the nested helper function can be omitted to keep the sample simple and idiomatic.

Suggested change
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

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! 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:

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 type of project_id to str | None to allow it to be optional. When project_id is None, spanner.Client will automatically infer the project from Application Default Credentials (ADC).

def write_and_read_dml(
    project_id: str | None, instance_id: str, database_id: str
) -> None:

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.

client = spanner.Client(project=project_id)
database = client.instance(instance_id).database(database_id)
inserted_rows = 0
read_rows: list[tuple[str, str]] = []

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

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.

Suggested change
read_rows: list[tuple[str, str]] = []
read_rows: list[list[str]] = []

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.

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:

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

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.

Suggested change
def write_then_read(transaction: spanner_v1.transaction.Transaction) -> None:
def write_then_read(transaction) -> None:

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! 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.

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

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.

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.

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
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