Repository navigation
[4.2-dev] Enforce CHECK on final UPDATE and upsert rows - #33324
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
This pull request is merged. |
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: d726f5666f25aaefcca55f8f77880b48f6de4c0b
Assessment: request_changes
The change reuses the existing CHECK helper and preserves the tested direct UPDATE and upsert behavior, but it leaves a foreign-key cascade path outside the linked issue’s final-row validation requirement. This is a pre-existing bypass that remains unaddressed by the fix.
Model: gpt-6.1-terra / max
Session: 01a2172e-434d-7533-8bcf-86e4c749a300 · slot 1 · generation 20
- P2
pkg/sql/plan/bind_update.go:428: A parent UPDATE changes a referenced key with ON UPDATE CASCADE, and the resulting child value violates the child table’s CHECK constraint.
Impact: The statement can succeed and commit a child row that violates its persisted CHECK constraint.
Evidence: The new check covers directly updated targets before foreign-key actions are planned. The modern child mutation in pkg/sql/plan/bind_update_fk.go writes its projected row without that check; legacy FK recursion also creates contexts with the new check mode left at its skip default. In control-tests.log, a direct update of child.pid from 1 to 6 was rejected with error 3819 for CHECK(pid < 5), but updating the parent key from 1 to 6 succeeded and committed child.pid=6. Apply final-image CHECK enforcement to cascaded child writes in both planner paths and preserve statement rollback when a child fails.
Validation:
-
Private /tmp and loopback preflight passed; receipt checks/1795708417436636854-5.json.
-
The selected planner tests passed, as did public SQL controls for direct and prepared updates, UPDATE IGNORE, repeated targets, outer joins, generated columns, UPDATE FROM, and indexed upserts. The two combined Go test commands exited 1 because their cascade assertions failed; see focused-tests.log and control-tests.log and receipts checks/1795708895824695149-5.json and checks/1795709037195094969-5.json.
-
The retained native build inputs matched the frozen checkout; receipt checks/1795708531072638152-5.json and native-provenance.log.
Limitations:
- The full distributed SQL suite was not run locally. Relevant CI checks were still in progress in the supplied evidence; the author-reported suite runs did not include retained logs in this assignment.
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 944e68e2232fd9325ce0dd6bdc7c6fb7a2022559
Assessment: request_changes
The previously reported ON UPDATE CASCADE bypass now rejects the invalid child update and rolls back. The shared CHECK evaluator is reused without a new execution protocol, but legacy SET NULL actions can still validate a stale generated-column value rather than the final child image.
Model: gpt-6.1-terra / max
Session: 01a21456-006d-7b41-b150-908a3e9d9152 · slot 3 · generation 6
- P2
pkg/sql/plan/build_dml_util.go:4376: An ON DELETE SET NULL action updates a child foreign-key column on which a stored generated column depends, and a CHECK references that generated column.
Impact: The new lookup falls back to the old generated value. A parent DELETE can therefore succeed when recomputing the generated value would violate CHECK, and it leaves the child with a stale stored generated value. This underlying row-construction deficiency predates the PR, but leaves the PR’s child final-row enforcement incomplete.
Evidence: With child g GENERATED ALWAYS AS (ifnull(pid,0)) STORED, CHECK(g>0), and pid referencing the parent ON DELETE SET NULL, a direct UPDATE setting pid to NULL returns 3819. Deleting the parent instead succeeds and leaves the child at pid=NULL, g=1; the recomputed g would be 0. See generated-fk-v2.log and receipt checks/1795712691023135969-2.json. The legacy action only adds the changed FK column to updateColPosMap. Recompute dependent generated columns before CHECK and the child write, or reject this action when a correct final image cannot be constructed.
Validation:
-
Private /tmp and loopback preflight passed; receipt checks/1795711995894079885-3.json.
-
The selected planner checks and direct UPDATE, upsert, repeated-target, generated-column and earlier cascade controls passed. Their combined command exited 1 because the generated SET NULL diagnostic failed; see focused-review.log and receipt checks/1795712573542982072-2.json.
-
A separate generated-column SET NULL probe reproduced the failure and exited 1; see generated-fk-v2.log and receipt checks/1795712691023135969-2.json.
-
INSERT SELECT, INSERT IGNORE, REPLACE and zero-match UPDATE controls passed; see consumer-review.log and receipt checks/1795712801201251911-2.json.
-
git diff --check passed; receipt checks/1795712658710448878-2.json.
Limitations:
-
The full distributed BVT and author-reported performance measurements were not independently rerun.
-
A self-referential DELETE probe was inconclusive for this PR: its unchecked control already left the deleted row present, and a later fixture reset failed. It was not used as a new regression finding.
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 79d0df516f6f99332a8d3420e80fc9bb26d225a8
Assessment: request_changes
Two final-row defects remain open from the previous review. The change appropriately reuses the CHECK and foreign-key machinery, but the affected upsert and joined-update paths can construct inconsistent generated values before the new CHECK assertions run.
Model: gpt-6.1-terra / max
Session: 01a21fca-22ff-76e2-85da-e9ad78f4f51d · slot 3 · generation 8
-
P2
pkg/sql/plan/bind_insert.go:2705: An ON DUPLICATE KEY UPDATE assigns a volatile expression such as nextval('s') to x, where g is generated as x * 2 and CHECK requires g = x * 2.
Impact: A valid upsert fails with error 3819 because the assignment can be evaluated separately for x and g. The newly added CHECK exposes an existing inconsistent row-construction path.
Evidence: The retained public SQL probe stored x=10 and g=22 without CHECK, while the constrained upsert failed and a direct UPDATE control stored x=10 and g=20. The affected production path is unchanged since that probe. Materialize each assignment once before computing generated columns and validating the merged row. -
P2
pkg/sql/plan/build_update.go:115: A multi-target UPDATE JOIN matches one target row to source values NULL and 5, with a generated column h defined as IF(x IS NULL, 1, NULL).
Impact: The statement can fail a valid CHECK because independently applied ANY_VALUE aggregates combine x and h from different matches, producing an image that no source row supplied.
Evidence: The retained probe's single-match control succeeded, but the two-match CHECK case failed with 3819; without CHECK it stored x=5 and h=1. Select one coherent candidate row before recomputing generated columns and applying CHECK.
Validation:
-
Verified the earlier public SQL probe, its source fingerprint, native inputs, retained log and exit-1 receipt. The only intervening production change is an unrelated foreign-key assertion slice preallocation; current-head verification receipt 1795881214499859627-2.json.
-
Reused the earlier same-PR full pkg/sql/plan and pkg/sql/plan/function package passes; prior receipt 1795852178991835107-3.json and retained owner-packages.log.
-
git diff --check passed for the assigned range; receipt 1795881213803791827-2.json.
Limitations:
-
The public SQL probe and package tests were not repeated on the current head because their relevant production inputs are unchanged.
-
Distributed BVT and performance measurements were not independently rerun. No v4.2.9 release artifact was available for an upgrade-artifact check.
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 067754e9f25333b7cb66364736f789a1df669bae
Assessment: request_changes
Two previously reported final-row defects remain open. Reusing the existing CHECK and foreign-key machinery is appropriate, but upsert and multi-target UPDATE can construct inconsistent generated values before the new CHECK assertions run.
Model: gpt-6.1-terra / max
Session: 01a226fb-40e1-7c82-8448-1a1fd4af5968 · slot 1 · generation 36
-
P2
pkg/sql/plan/bind_insert.go:2705: An ON DUPLICATE KEY UPDATE assigns a volatile expression such as nextval('s') to x on a table with g generated as x * 2 and CHECK(g = x * 2).
Impact: A valid upsert can fail with CHECK error 3819 and evaluate the sequence assignment more than once.
Evidence: bind_insert.go:1703–1732 substitutes the assignment expression into the generated expression, and dedupjoin/join.go:984–990 evaluates the resulting update expressions independently. The prior review reports an unchecked control storing x=10 and g=22, while the constrained upsert failed; the relevant planner and executor files are unchanged from the last reviewed head. Materialize each assignment once before deriving generated columns and validating the merged row. -
P2
pkg/sql/plan/build_update.go:115: A multi-target UPDATE joins one target row to source rows with x values NULL and 5, while a generated column h is IF(x IS NULL, 1, NULL) and a CHECK requires h to match x.
Impact: The valid statement can fail with CHECK error 3819 even though either source row would produce a valid final image.
Evidence: Generated values are computed before build_dml_util.go:4204–4254 independently aggregates each assignment slot with ANY_VALUE; aggexec/any2.go:43–45 skips NULL inputs. The prior review reports an unchecked control storing the impossible combination x=5, h=1 and the constrained statement failing. Select one coherent candidate row per target before recomputing generated columns and applying CHECK.
Validation:
-
Private /tmp and loopback preflight passed: checks/1795953917382697072-3.json and preflight.log.
-
git diff --check passed for the assigned range: checks/1795953965158442965-2.json and diff-check.log.
-
The relevant planner, dedup-join, and aggregate files match the prior reviewed head: checks/1795953965129997852-2.json and prior-path-diff.log.
-
All 24 discussion keys were accounted for in discussion-assessment.json: checks/1795954548539632983-2.json and assessment-verification.log.
Limitations:
-
The focused local Go check timed out while downloading dependencies, before any tests ran: checks/1795954500611711641-2.json and planner-focused-bounded.log. An earlier attempt was interrupted during the same setup phase. Neither is a product-test result.
-
The earlier public SQL probes are described in the supplied review discussion, but their raw logs were not supplied in this job. The prepared diagnostic probe was not executed.
-
The supplied CI snapshot reports a successful shared build; relevant UT and multi-CN BVT checks were still in progress. The full distributed BVT was not rerun locally.
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 26ac65edd5eac209b7621aac175bc0b50f26822b
Assessment: request_changes
The earlier volatile-assignment and joined-update findings are addressed, but repeated conflicts in one upsert can still bypass CHECK enforcement. The design reuses the existing CHECK and foreign-key machinery; the added generated-column materialization and key guards protect the row images consumed by those paths.
Model: gpt-6.1-terra / max
Session: 01a22773-8d14-7b22-81c6-d83c8451f8ba · slot 0 · generation 41
- P2
pkg/sql/plan/bind_insert.go:2814: For a table with CHECK(x >= 0) containing (id, x) = (1, 1), run INSERT INTO t VALUES (1, 1), (1, 1) ON DUPLICATE KEY UPDATE x = IF(x > 0, -x, 2).
Impact: The first conflict produces x = -1 and should fail the CHECK and roll back the statement. The second conflict changes it to 2, so the plan can accept the statement after checking only that later image.
Evidence: The dedup join applies every matching update in a loop before emitting one folded row (pkg/sql/colexec/dedupjoin/join.go:992). The CHECK filter is attached after that join and its final projection, so it cannot see the earlier image. The existing repeated-conflict regression confirms that successive conflicts are folded, but exercises only valid intermediate images. Validate each insert or update image before a later conflict can overwrite it, and add a regression covering rollback and a valid control.
Validation:
-
Private /tmp and loopback preflight passed; receipt checks/1795958439850520089-2.json and preflight.log.
-
git diff --check passed for the assigned range; receipt checks/1795958439850186340-2.json and diff-check.log.
-
Native prerequisites built in the diagnostic directory from 170 matching tracked inputs; native build and provenance receipts and logs were verified.
-
All 26 discussion keys were accounted for; receipt checks/1795959073559281372-2.json and discussion-coverage.log.
Limitations:
-
The focused Go check timed out during dependency downloads before tests ran; receipt checks/1795959158222905750-2.json and focused-tests.log.
-
The SQL counterexample was not executed locally. The finding follows from the planner and executor source paths.
-
The full distributed BVT was not independently run. The PR reports current-head test results, but their retained logs were not supplied.
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: bf37994bf3455e13d00d50f1a56a90c8042c23e5
Assessment: request_changes
The change reuses the existing CHECK and foreign-key machinery and addresses the previously reported intermediate-CHECK case. Two repeated-upsert interactions remain: no-op detection can discard a value needed by a later conflict, and the generated-key guard can miss an intermediate key transition that changes conflict resolution.
Model: gpt-6.1-terra / max
Session: 01a22797-0703-79f1-91eb-e470e6f35dfb · slot 0 · generation 42
-
P2
pkg/sql/colexec/dedupjoin/join.go:873: With t(id INT PRIMARY KEY, x DOUBLE, s INT CHECK(s < 2)) initially containing (1, 0, 0), execute INSERT INTO t VALUES (1, 0, 1), (1, 0, 2) ON DUPLICATE KEY UPDATE x = IF(VALUES(s) = 1, CAST('-0' AS DOUBLE), x), s = IF(VALUES(s) = 1, s, IF(serial_full(x) = serial_full(CAST('-0' AS DOUBLE)), 1, 2)).
Impact: The first conflict produces negative zero, but the fold restores positive zero. The second conflict therefore selects s=2 and fails CHECK, although retaining the first assignment would select s=1.
Evidence: bindOdkuUnchanged uses SQL <=>, which considers the two zeros equal, and this branch restores every assignment slot before the next conflict. The signed-zero fixture at test/distributed/cases/dml/insert/check_constraint.test:391 demonstrates that their physical encodings are distinguishable. Preserve assigned values that later expressions can observe while suppressing only legitimate implicit ON UPDATE changes, and cover this case with a regression. -
P2
pkg/sql/plan/bind_insert.go:2593: For t(id INT PRIMARY KEY, x INT, v INT CHECK(v >= 0), g INT GENERATED ALWAYS AS (x * 2) STORED, UNIQUE KEY(g)) containing (id,x,v)=(1,1,0), insert (1,1,0), (3,2,0), (1,1,0) ON DUPLICATE KEY UPDATE x=IF(x=1,2,1), v=IF(VALUES(id)=3,-1,v+1).
Impact: The first conflict temporarily changes g from 2 to 4. In input order, the second row then conflicts with that key and its v=-1 update must fail CHECK. The plan can instead treat that row as a fresh insert and commit it, because the first and third inputs fold the original row back to g=2.
Evidence: buildOnDupTargetPkResolution resolves incoming targets against the original snapshot before folding. The new guard compares only the original and final generated keys, and the per-image fold checks only CHECK assertions. This leaves a pre-existing conflict-resolution gap open in the CHECK fix. Reject each unsupported intermediate generated-key transition, or maintain sequential key ownership, and add a regression for the cross-target case.
Validation:
-
Private /tmp and loopback preflight passed: preflight.log and checks/1795962012421652701-2.json.
-
git diff --check passed for the assigned base and head: diff-check.log and checks/1795962012421849445-2.json.
-
All 28 discussion keys were assessed exactly once: discussion-assessment.json and review-verification-final.log, with checks/1795962764294884763-2.json.
-
Both findings were traced through the frozen planner and executor sources; the SQL cases are retained in review-cases.md.
Limitations:
-
The SQL counterexamples were not executed locally.
-
Two focused dedup-join Go test attempts timed out during dependency downloads before any test ran: dedup-tests.log and dedup-tests-retry.log, with checks/1795962381212634510-2.json and checks/1795962730152236778-2.json.
-
The author reports current-head SQL and race-suite passes, but their retained logs were not supplied; the relevant CI checks were pending in the supplied snapshot.
Validate each writable row after assignment and match selection. Keep CHECK above the upsert merge so predicates observe the values that will be written.
Validate the projected child row before modern cascade writes and use strict CHECK by default for legacy updates. Preserve statement rollback when a cascaded child violates a constraint.
Build the final legacy child row before CHECK, locks and index writes. Reuse the final-value column map for the table projection. Reject generated-column actions on matched rows when the existing writer cannot maintain dependent constraints.
Keep the complete child projection through CHECK and the old-key lock. Reuse the lock projection preservation path, so positional consumers read the same row the constraint validated.
Give child images distinct binding tags before column remapping. The CHECK filter and old-key lock refer to the same image, so the generated projection cannot alias its input when the complete child row is preserved.
Compare the computed key against the stored value before rejecting a change the legacy writer cannot maintain. Carry validated keys through the usual child write layout. Bind Datalink admission to the tagged child projection when REPLACE plans are remapped. Preserve the lock input through validation and keep active scan column references when pinning the source layout.
Detect changes to referenced keys with NULL-safe equality so recursive UPDATE restrictions and cascades receive NULL transitions. Validate nullable outgoing key tuples after generated assignments are finalized, retaining the existing guard for unsupported changed non-NULL keys. Cover atomic rollback, nullable generated keys, index maintenance, and composite outgoing keys.
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 57a2efa71c87fc33c3c6acbe5158442ed4eb37a8
Assessment: request_changes
Repeated upserts can still skip CHECK validation for an intermediate value that is SQL-equal to the previous value but has a distinguishable representation. The earlier integer and representation-retention examples have current fixes, but their interaction remains open. Reusing the CHECK and foreign-key machinery is appropriate; the per-group fold and key guards are needed to handle intermediate rows and key ownership.
Model: gpt-6.1-terra / max
Session: 01a229e8-c998-7480-862c-3017417a9175 · slot 1 · generation 38
- P2
pkg/sql/colexec/dedupjoin/join.go:922: A repeated upsert assigns negative zero to a DOUBLE initially holding positive zero, then assigns positive zero on a later conflict. A CHECK using serial_full(x) distinguishes the representations and rejects negative zero.
Impact: The first effective update violates CHECK, but the statement succeeds instead of returning error 3819 and rolling back. The later conflict hides the invalid intermediate image.
Evidence: The unchanged test uses SQL <=>, which considers the zeros equal. This branch preserves the explicit assignment but continues before checkFoldedRow. The same-head public SQL probe in attempt-1/review-signedzero.log shows the direct negative-zero UPDATE returning 3819 while the repeated upsert succeeds; attempt-1/checks/1795999387191382297-3.json records the failing probe. After restoring implicit ON UPDATE values and recomputing generated columns, validate the resulting row before continuing, and add a representation-sensitive regression with a valid control.
Validation:
-
Reused the attempt-1 isolated-executor preflight for the frozen head; attempt-1/checks/1795998919894777657-3.json records exit 0.
-
Reused TestReviewFoldRepresentationCheck through the MySQL frontend on the frozen head. The test exited 1 on the reproduced CHECK bypass; its command, overlay and output are retained in attempt-1/checks/1795999387191382297-3.json, attempt-1/review_signedzero_test.go and attempt-1/review-signedzero.log.
-
Saved the review plan and discussion assessment for attempt 3. Coverage of all 30 discussion keys, the clean frozen source, the retained probe receipts and logs, 173 native inputs and five artifact hashes were verified; see attempt-3/review-verification-final.log and attempt-3/checks/1796000309798676318-4.json.
Limitations:
- The author reports broader Go and BVT passes, but matching retained logs were not supplied in local_validation. The full distributed suite was not independently rerun.
|
Addressed the latest CHECK finding in c590b9b. The previous head returned 3819 for a direct UPDATE and accepted the same invalid signed-zero image in single and repeated upserts. Each overwritten logical row is now checked in the fold and the emitted row is checked before the zero-affected-row filter. Both use the existing CHECK machinery. The regression also exercises implicit timestamps. A legal no-op, including a generated-column-only DEFAULT assignment, preserves its timestamp and avoids an out-of-range speculative cast. Real changes still enforce the cast. The final filter preserves an implicit timestamp changed by an earlier conflict even when the explicit value returns to its starting value. Verified on the published tree with nine MySQL TCP cases, 704/704 statements across five BVT fixtures and race tests in ten packages. The prepared-statement case checks error 3819, continued use of the same transaction and a successful subsequent execution. The PR description includes the local timing comparison. |
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: c590b9b2385aaf0cfa8b77b937e28e7b7e13ffdb
Assessment: approve
No actionable defects found. The change reuses the existing CHECK and foreign-key validation owners while staging row images and guarding generated keys during repeated conflicts. The previously reported signed-zero CHECK bypass is resolved. Generated-key changes that the existing writer cannot maintain remain explicitly guarded.
Model: gpt-6.1-terra / max
Session: 01a22868-9ecc-7250-9120-16606b40e9e2 · slot 0 · generation 44
Validation:
-
Preflight passed. The retained native libraries matched 170 source inputs and 43 artifacts.
-
Focused deduplication and planner tests passed, including intermediate CHECK failures, generated foreign keys, row-image preservation, and affected-row planning. Logs and receipts: focused-tests.log, row-image-tests.log, checks/1796006435427054774-3.json, and checks/1796006836793978920-3.json.
-
A single-CN MySQL probe confirmed error 3819 and rollback for the prior signed-zero counterexample, rejection of a generated column placed before its dependency, and correct CHECK and generated values with UPDATE RETURNING. Log and receipt: returning-probe.log and checks/1796007366366278558-3.json.
-
A reviewer probe passed a pipeline codec round trip with nonzero OdkuFold fields. Log and receipt: fold-wire-test.log and checks/1796007156411478562-3.json.
Limitations:
-
The full BVT and race runs were author-reported and were not independently repeated. No end-to-end multi-CN run was performed.
-
The first public SQL probe failed before executing SQL because HAKeeper timed out allocating a connection ID; the retry passed.
-
The original attempt directory was read-only after the model switch. The complete discussion assessment, supplemental probes, logs, and receipts are retained under /mnt/nvme/mo-bot-55-live/state/jobs/3d70691e93a6807ee65a0785/model-switch-1796006252715441587.
What type of PR is this?
Which issue(s) this PR fixes:
Related to #26820. Applies the final-row CHECK behavior from #26912 to
4.2-dev.What this PR does / why we need it:
Enforce CHECK on the rows that ordinary and joined UPDATE, UPDATE IGNORE, upserts, and foreign-key actions actually write. Evaluate explicit assignments once, select a complete source row per update target, and derive generated values in dependency order. CHECK, generated keys, and the write use the same row values.
ON UPDATEvalues and dependent generated columns are finalized before CHECK. A generated column is evaluated once, including in a no-op where a provisional timestamp would have caused a cast error. Final write suppression compares the materialized values, including a timestamp changed by an earlier conflict.ON UPDATE. Both fold and final-row validation use the same key encoding and return 20313 for unsupported movement.The rebase uses the shared CDC task-store readiness check from #33451 and removes the temporary local task-service setup. Intermediate generated-key checks reuse one encoded key snapshot per repeated group that needs it. The final plan checks the emitted key. The change reuses the existing plan fields, per-worker scratch and write path.
Validation
Hosted checks