Skip to content

feat(spanner): Add GoogleSQL DELETE DML THEN RETURN sample - #14862

Open
iennae wants to merge 3 commits into
feat/spanner-dml-last-statementfrom
feat/spanner-delete-dml-returning
Open

iennae wants to merge 3 commits into
feat/spanner-dml-last-statementfrom
feat/spanner-delete-dml-returning

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Using DELETE ... THEN RETURN captures deleted row keys and generated columns atomically as rows are removed.

Adds spanner_delete_dml_returning using the canonical cross-language region tag.

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

Capturing deleted primary keys and generated columns during a DELETE statement requires DELETE ... THEN RETURN inside run_in_transaction.
@iennae
iennae requested review from a team as code owners November 17, 2026 23:48
@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 17, 2026
@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 a new sample script delete_dml_returning.py and its associated tests to demonstrate deleting rows in Google Cloud Spanner using DML with a THEN RETURN clause. The feedback suggests replacing an obsolete Python 2 pattern—using a single-element list (row_counts) to emulate a mutable reference across nested scopes—with the modern Python 3 nonlocal keyword, and updating the corresponding print statement to use this new variable.

Comment on lines +33 to +45
deleted_singers: list[tuple[int, str]] = []
row_counts: list[int] = []

def delete_singers(transaction: spanner_v1.Transaction) -> None:
deleted_singers.clear()
row_counts.clear()
results = transaction.execute_sql(
"DELETE FROM Singers WHERE FirstName = 'David' "
"THEN RETURN SingerId, FullName"
)
for singer_id, full_name in results:
deleted_singers.append((int(singer_id), str(full_name)))
row_counts.append(int(results.stats.row_count_exact))

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

Using a single-element list (row_counts) to emulate a mutable reference for an outer scope variable is an obsolete pattern from Python 2. In Python 3, the nonlocal keyword should be used instead to declare that a variable inside a nested function refers to a variable in the nearest enclosing scope.

Suggested change
deleted_singers: list[tuple[int, str]] = []
row_counts: list[int] = []
def delete_singers(transaction: spanner_v1.Transaction) -> None:
deleted_singers.clear()
row_counts.clear()
results = transaction.execute_sql(
"DELETE FROM Singers WHERE FirstName = 'David' "
"THEN RETURN SingerId, FullName"
)
for singer_id, full_name in results:
deleted_singers.append((int(singer_id), str(full_name)))
row_counts.append(int(results.stats.row_count_exact))
deleted_singers: list[tuple[int, str]] = []
row_count = 0
def delete_singers(transaction: spanner_v1.Transaction) -> None:
nonlocal row_count
deleted_singers.clear()
results = transaction.execute_sql(
"DELETE FROM Singers WHERE FirstName = 'David' "
"THEN RETURN SingerId, FullName"
)
for singer_id, full_name in results:
deleted_singers.append((int(singer_id), str(full_name)))
row_count = int(results.stats.row_count_exact)

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.

)
else:
print(f"Resource ID: {database.name}")
print(f"Deleted Rows: {row_counts[-1]}")

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 this print statement to use the row_count variable instead of the row_counts list.

Suggested change
print(f"Deleted Rows: {row_counts[-1]}")
print(f"Deleted Rows: {row_count}")

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.

Uses a nonlocal row counter with a defensive results.stats check and omits project identifiers from stdout and stderr.
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/delete_dml_returning.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.

2 participants