Skip to content

feat(spanner): Add GoogleSQL DML last_statement optimization sample - #14850

Open
iennae wants to merge 3 commits into
mainfrom
feat/spanner-dml-last-statement
Open

iennae wants to merge 3 commits into
mainfrom
feat/spanner-dml-last-statement

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Setting last_statement=True on the final DML statement in a read-write transaction piggybacks commit onto the ExecuteSql RPC and saves a round trip, but the DML best-practices guide previously relied on an untested inline Python snippet.

Establishes the spanner/dml Python 3.13+ sample package and adds spanner_dml_last_statement.

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

Passing last_statement=True on the final DML call in a transaction combines ExecuteSql and Commit into one round trip, replacing a broken inline documentation snippet.
@iennae
iennae requested review from a team as code owners November 17, 2026 23:47
@snippet-bot

snippet-bot Bot commented Nov 17, 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

@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 Python samples for Cloud Spanner DML, specifically demonstrating the last_statement=True transaction optimization. It includes implementation code, unit and integration tests, and configuration files. The review feedback highlights a few key improvements: clearing the side-effect list inside the transaction callback to handle retries correctly, simplifying a redundant nested environment variable lookup in the test configuration, and refactoring the live integration test to use a setup/teardown fixture instead of relying on pre-existing database state.

out, err = capsys.readouterr()
assert err == ""
assert "Last Statement Optimization: True" in out
assert "Updated Rows: 1" in out

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.

high

Asserting Updated Rows: 1 in the live integration test is fragile because it assumes a singer with ID 54213 already exists in the live database. If the row does not exist, the update will succeed but affect 0 rows, causing the test to fail. Consider creating/inserting the test row as part of a test fixture or setup step, and ensure you use a try...finally block with a teardown function for resource cleanup to maintain consistency with neighboring tests in this directory.

References
  1. Maintain consistency in test structures by matching the patterns employed by existing or neighboring tests in the same directory, such as using a try...finally block with a teardown function for resource cleanup.

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! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.

Comment thread spanner/dml/tests/conftest.py Outdated
Comment on lines +23 to +25
LIVE_DATABASE_ID = os.environ.get(
"SPANNER_DATABASE_ID", os.environ.get("SPANNER_DATABASE_ID", "")
)

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 nested os.environ.get call for SPANNER_DATABASE_ID is redundant. It can be simplified to a single os.environ.get call.

LIVE_DATABASE_ID = os.environ.get("SPANNER_DATABASE_ID", "")

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! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.

Comment on lines +41 to +42
def update_singers(transaction: spanner_v1.Transaction) -> None:
row_count = transaction.execute_update(

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

Because run_in_transaction can retry the transaction callback multiple times upon encountering transient errors (such as Aborted), any side effects like appending to updated_counts will accumulate duplicate entries across attempts. Clearing the list at the start of the callback ensures that only the final, successful attempt's row count is retained.

Suggested change
def update_singers(transaction: spanner_v1.Transaction) -> None:
row_count = transaction.execute_update(
def update_singers(transaction: spanner_v1.Transaction) -> None:
updated_counts.clear()
row_count = transaction.execute_update(

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! The callback now assigns a single nonlocal value instead of appending to a list, so retries can no longer accumulate duplicate entries.

Replaces the outer list cell with a nonlocal integer, isolates live test row setup/teardown, simplifies conftest lookup, and omits project_id from output.
Prints only runtime resource and query results instead of hardcoded string literals.
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.

2 participants