Skip to content

fix(spanner): support parameters and preserve safety in partitioned DML - #19035

Merged
olavloite merged 2 commits into
mainfrom
spanner-preserve-params-in-pdml
Nov 19, 2026
Merged

olavloite merged 2 commits into
mainfrom
spanner-preserve-params-in-pdml

Conversation

@olavloite

@olavloite olavloite commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Partitioned DML executed through the Cloud Spanner DB-API driver previously failed whenever query parameters were used. The driver passed raw positional placeholders and parameter lists directly to the database rather than converting them into the named parameters and data types expected by Cloud Spanner.

Additionally, statement parsing historically mutated SQL statements in place to automatically append a dummy "WHERE 1=1" clause to update and delete statements. Injecting this dummy clause into Partitioned DML is dangerous because Partitioned DML is non-atomic, irreversible, and cannot be rolled back. Bypassing Cloud Spanner's requirement for an explicit WHERE clause risks unintended full-table modifications.

This change addresses these issues by:

  • Properly translating and passing query parameters and their data types during Partitioned DML execution.
  • Making statement parsing pure and side-effect free so SQL statements are no longer mutated during classification.
  • Ensuring Partitioned DML statements never receive an automatic dummy WHERE clause, keeping Cloud Spanner's safety checks intact.
  • Guarding Partitioned DML mode so that statements executed inside an explicit transaction always run as atomic transactional DML.
  • Retaining automatic dummy WHERE clause injection exclusively for transactional and batch DML to preserve full backwards compatibility for existing applications.

@olavloite
olavloite requested a review from a team as a code owner November 10, 2026 12:23

@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 refactors the Spanner DB-API to prevent dummy WHERE clauses from being automatically added to partitioned DML statements. The logic for adding dummy WHERE clauses to UPDATE and DELETE statements is moved from the classification phase in parse_utils.py to specific cursor execution paths, and comprehensive tests are added to verify the new behavior. The review feedback suggests using the NamedTuple._replace() method in _ensure_where_clause to update the ParsedStatement and Statement objects more cleanly and safely.

Partitioned DML executed through the Cloud Spanner DB-API driver previously
failed whenever query parameters were used. The driver passed raw positional
placeholders and parameter lists directly to the database rather than converting
them into the named parameters and data types expected by Cloud Spanner.

Additionally, statement parsing historically mutated SQL statements in place
to automatically append a dummy "WHERE 1=1" clause to update and delete statements.
Injecting this dummy clause into Partitioned DML is dangerous because Partitioned
DML is non-atomic, irreversible, and cannot be rolled back. Bypassing Cloud
Spanner's requirement for an explicit WHERE clause risks unintended full-table
modifications.

This change addresses these issues by:
- Properly translating and passing query parameters and their data types
  during Partitioned DML execution.
- Making statement parsing pure and side-effect free so SQL statements are
  no longer mutated during classification.
- Ensuring Partitioned DML statements never receive an automatic dummy WHERE
  clause, keeping Cloud Spanner's safety checks intact.
- Guarding Partitioned DML mode so that statements executed inside an explicit
  transaction always run as atomic transactional DML.
- Retaining automatic dummy WHERE clause injection exclusively for transactional
  and batch DML to preserve full backwards compatibility for existing applications.
@olavloite
olavloite force-pushed the spanner-preserve-params-in-pdml branch from 47b8ba8 to 218d44b Compare November 10, 2026 12:39
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 refactors how dummy WHERE clauses are added to unqualified UPDATE and DELETE statements in the Spanner DB API. Instead of automatically appending 'WHERE 1=1' during statement classification in parse_utils.py, the logic is moved to the cursor execution level via a new _ensure_where_clause helper. This ensures that partitioned DML statements do not receive a dummy WHERE clause, while transactional DML, batch DML, and executemany statements still do. Comprehensive unit and mock server tests have been added to verify these behaviors, including idempotency during transaction retries. I have no further feedback to provide.

@olavloite
olavloite merged commit 6e48f41 into main Nov 19, 2026
57 checks passed
@olavloite
olavloite deleted the spanner-preserve-params-in-pdml branch November 19, 2026 12:43
sakthivelmanii added a commit that referenced this pull request Nov 22, 2026
…atch release (#19178)

Temporarily rolls back unreleased `google-cloud-spanner` commits since
`v3.72.0` so `3.72.1` can be released containing only #19035:

- #19172 (`6160b0d0fd`)
- #19110 (`52dbb981ec`)
- #19046 (`0639880aca`)
- #19054 (`3952b8886d`)
- #19053 (`97a00d50fe`)
- #19052 (`f159262721`)

All 6 commits will be restored immediately after
`google-cloud-spanner-v3.72.1` is tagged.
sakthivelmanii added a commit that referenced this pull request Nov 22, 2026
Adjusts the temporary `3.72.1` patch release state on `main` so
`google-cloud-spanner` contains only #19172 on top of `v3.72.0`:

- Temporarily reverts #19035 (`6e48f41cce`)
- Restores #19172 (`6160b0d0fd`)

All temporarily excluded commits (#19052, #19053, #19054, #19046,
#19035, #19110) will be restored immediately after
`google-cloud-spanner-v3.72.1` is tagged.
sakthivelmanii added a commit that referenced this pull request Nov 23, 2026
#19184)

Restores the 6 `google-cloud-spanner` commits that were temporarily
excluded for the `3.72.1` patch release (#19178, #19179):

- #19052 (`f159262721`) — `fix(spanner): escape identifier delimiters
and validate table names`
- #19053 (`97a00d50fe`) — `fix(spanner): quote database names when
creating databases`
- #19054 (`3952b8886d`) — `fix(spanner): harden identifier escaping in
dbapi driver`
- #19046 (`0639880aca`) — `fix(spanner): eliminate silent SQL truncation
in DB-API`
- #19035 (`6e48f41cce`) — `fix(spanner): support parameters and preserve
safety in partitioned DML`
- #19110 (`52dbb981ec`) — `test(spanner): add mid-stream errors and
chunking to mock server`

BEGIN_COMMIT_OVERRIDE
fix(spanner): escape identifier delimiters and validate table names
(#19052)

fix(spanner): quote database names when creating databases (#19053)

fix(spanner): harden identifier escaping in dbapi driver (#19054)

fix(spanner): eliminate silent SQL truncation in DB-API (#19046)

fix(spanner): support parameters and preserve safety in partitioned DML
(#19035)
END_COMMIT_OVERRIDE

---------

Co-authored-by: Knut Olav Løite <koloite@gmail.com>
daniel-sanche pushed a commit that referenced this pull request Nov 25, 2026
…ML (#19035)

Partitioned DML executed through the Cloud Spanner DB-API driver
previously failed whenever query parameters were used. The driver passed
raw positional placeholders and parameter lists directly to the database
rather than converting them into the named parameters and data types
expected by Cloud Spanner.

Additionally, statement parsing historically mutated SQL statements in
place to automatically append a dummy "WHERE 1=1" clause to update and
delete statements. Injecting this dummy clause into Partitioned DML is
dangerous because Partitioned DML is non-atomic, irreversible, and
cannot be rolled back. Bypassing Cloud Spanner's requirement for an
explicit WHERE clause risks unintended full-table modifications.

This change addresses these issues by:
- Properly translating and passing query parameters and their data types
during Partitioned DML execution.
- Making statement parsing pure and side-effect free so SQL statements
are no longer mutated during classification.
- Ensuring Partitioned DML statements never receive an automatic dummy
WHERE clause, keeping Cloud Spanner's safety checks intact.
- Guarding Partitioned DML mode so that statements executed inside an
explicit transaction always run as atomic transactional DML.
- Retaining automatic dummy WHERE clause injection exclusively for
transactional and batch DML to preserve full backwards compatibility for
existing applications.
daniel-sanche pushed a commit that referenced this pull request Nov 25, 2026
…atch release (#19178)

Temporarily rolls back unreleased `google-cloud-spanner` commits since
`v3.72.0` so `3.72.1` can be released containing only #19035:

- #19172 (`6160b0d0fd`)
- #19110 (`52dbb981ec`)
- #19046 (`0639880aca`)
- #19054 (`3952b8886d`)
- #19053 (`97a00d50fe`)
- #19052 (`f159262721`)

All 6 commits will be restored immediately after
`google-cloud-spanner-v3.72.1` is tagged.
daniel-sanche pushed a commit that referenced this pull request Nov 25, 2026
Adjusts the temporary `3.72.1` patch release state on `main` so
`google-cloud-spanner` contains only #19172 on top of `v3.72.0`:

- Temporarily reverts #19035 (`6e48f41cce`)
- Restores #19172 (`6160b0d0fd`)

All temporarily excluded commits (#19052, #19053, #19054, #19046,
#19035, #19110) will be restored immediately after
`google-cloud-spanner-v3.72.1` is tagged.
daniel-sanche pushed a commit that referenced this pull request Nov 25, 2026
#19184)

Restores the 6 `google-cloud-spanner` commits that were temporarily
excluded for the `3.72.1` patch release (#19178, #19179):

- #19052 (`f159262721`) — `fix(spanner): escape identifier delimiters
and validate table names`
- #19053 (`97a00d50fe`) — `fix(spanner): quote database names when
creating databases`
- #19054 (`3952b8886d`) — `fix(spanner): harden identifier escaping in
dbapi driver`
- #19046 (`0639880aca`) — `fix(spanner): eliminate silent SQL truncation
in DB-API`
- #19035 (`6e48f41cce`) — `fix(spanner): support parameters and preserve
safety in partitioned DML`
- #19110 (`52dbb981ec`) — `test(spanner): add mid-stream errors and
chunking to mock server`

BEGIN_COMMIT_OVERRIDE
fix(spanner): escape identifier delimiters and validate table names
(#19052)

fix(spanner): quote database names when creating databases (#19053)

fix(spanner): harden identifier escaping in dbapi driver (#19054)

fix(spanner): eliminate silent SQL truncation in DB-API (#19046)

fix(spanner): support parameters and preserve safety in partitioned DML
(#19035)
END_COMMIT_OVERRIDE

---------

Co-authored-by: Knut Olav Løite <koloite@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants