Skip to content

feat(spanner): Add Partitioned DML bulk DELETE sample - #14915

Open
iennae wants to merge 2 commits into
feat/spanner-dml-last-statementfrom
feat/spanner-dml-partitioned-delete
Open

iennae wants to merge 2 commits into
feat/spanner-dml-last-statementfrom
feat/spanner-dml-partitioned-delete

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_dml_partitioned_delete demonstrating bulk row deletion via database.execute_partitioned_dml().

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

Bulk data retention and cleanup jobs delete matching rows across key-range partitions via database.execute_partitioned_dml(). Add a standalone spanner_dml_partitioned_delete sample and test suite.
@iennae
iennae requested review from a team as code owners November 18, 2026 04:53
@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
@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

@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 module dml_partitioned_delete.py to execute bulk Partitioned DML DELETE statements on a Google Cloud Spanner database, along with its corresponding unit and live tests. The feedback suggests improving the live test's backup and restore logic by dynamically querying and restoring all columns (SELECT *) rather than hardcoding specific columns, which prevents potential data loss or corruption of other columns in a shared test database.

Comment on lines +117 to +128
with db.snapshot(multi_use=True) as snap:
saved_singers = list(
snap.execute_sql(
"SELECT SingerId, FirstName, LastName FROM Singers WHERE SingerId > 10"
)
)
saved_albums = list(
snap.execute_sql(
"SELECT SingerId, AlbumId, AlbumTitle, MarketingBudget "
"FROM Albums WHERE SingerId > 10"
)
)

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

To prevent data loss or corruption of other columns (such as BirthDate or SingerInfo in Singers, or other custom columns in Albums) in the shared test database, it is safer to query all columns using SELECT * and dynamically retrieve the column names from the cursor's fields. This ensures that when the rows are restored in the finally block, all column values are preserved.

Suggested change
with db.snapshot(multi_use=True) as snap:
saved_singers = list(
snap.execute_sql(
"SELECT SingerId, FirstName, LastName FROM Singers WHERE SingerId > 10"
)
)
saved_albums = list(
snap.execute_sql(
"SELECT SingerId, AlbumId, AlbumTitle, MarketingBudget "
"FROM Albums WHERE SingerId > 10"
)
)
with db.snapshot(multi_use=True) as snap:
singers_cursor = snap.execute_sql(
"SELECT * FROM Singers WHERE SingerId > 10"
)
saved_singers = list(singers_cursor)
singers_columns = [field.name for field in singers_cursor.fields]
albums_cursor = snap.execute_sql(
"SELECT * FROM Albums WHERE SingerId > 10"
)
saved_albums = list(albums_cursor)
albums_columns = [field.name for field in albums_cursor.fields]
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.

Done, thank you: the live test saves all columns with SELECT * and restores them using the column names from the result set (537908f).

Comment on lines +146 to +163
if saved_singers:
with db.batch() as batch:
batch.insert_or_update(
table="Singers",
columns=("SingerId", "FirstName", "LastName"),
values=saved_singers,
)
if saved_albums:
batch.insert_or_update(
table="Albums",
columns=(
"SingerId",
"AlbumId",
"AlbumTitle",
"MarketingBudget",
),
values=saved_albums,
)

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 restore logic in the finally block to use the dynamically retrieved column names (singers_columns and albums_columns). This ensures that all columns (including any nullable or default columns not hardcoded here) are correctly restored to their original values.

        if saved_singers:
            with db.batch() as batch:
                batch.insert_or_update(
                    table="Singers",
                    columns=singers_columns,
                    values=saved_singers,
                )
                if saved_albums:
                    batch.insert_or_update(
                        table="Albums",
                        columns=albums_columns,
                        values=saved_albums,
                    )
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.

Done, thank you: the restore uses the column names captured from the result set (537908f).

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/tests/dml_partitioned_delete_test.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