Repository navigation
fix: align REPLACE/ODKU affected-rows with MySQL - #25318
Conversation
REPLACE and INSERT ... ON DUPLICATE KEY UPDATE now return MySQL-compatible affected-rows values: - REPLACE over existing key: 2 (was 1) - REPLACE new key: 1 (unchanged) - ODKU real update: 2 (was 1) - ODKU no-op (same values): 0 (was 1) - ODKU insert new: 1 (unchanged) Changes: - proto/plan.proto: add count_delete_affect_rows field to UpdateCtx - multi_update/types.go: count DELETE for actionUpdate when flagged - compile/operator.go, multi_update_partition.go: propagate flag - bind_insert.go: set flag + add NOT(<=>) no-op FILTER for ODKU - bind_replace.go: set flag on main-table UpdateCtx for REPLACE - Unit tests: 7 executor-level + 3 plan-level tests Co-Authored-By: Claude <[email protected]>
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? |
…gular index tables The no-op FILTER referencing scanTag columns was placed after the PROJECT node, which remaps all columns to its own binding tag. This caused a "Column remapping failed" error on tables with irregular indexes (fulltext, vector/ivfflat). Move the FILTER right after the DEDUP JOIN chain, before the PROJECT, so scanTag columns are directly accessible during column remapping. Also remove the outdated TestReplaceIntoAffectedRows which tested the old (pre-fix) REPLACE behavior. Co-Authored-By: Claude <[email protected]>
aunjgr
left a comment
There was a problem hiding this comment.
No-op detection runs AFTER ON UPDATE CURRENT_TIMESTAMP fires, so UPDATE v=v still counts as a real change. Must build the equality predicate before implicit auto-update columns are applied.
…derived generated column End-to-end coverage for the two review scenarios: a no-op ON DUPLICATE KEY UPDATE must neither advance an implicit ON UPDATE CURRENT_TIMESTAMP column nor a stored generated column derived from it, while a real change must advance both. Verified with mo-tester (170/170 pass). Co-Authored-By: Claude <[email protected]>
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-reviewing the latest head: the earlier blocker around implicit ON UPDATE columns defeating the ODKU no-op filter is fixed now, including the generated-column case you asked about earlier.
However, I still see one remaining substantive blocker on the latest head:
CountDeleteAffectRows is not preserved when MultiUpdate is duplicated or rebuilt for remote execution.
That flag is what makes the executor count the delete side for REPLACE / real-update ODKU so affected rows become MySQL-compatible (2 instead of 1). The current head sets it when the operator is first built from the plan, but then drops it in both:
- the operator clone path (
pkg/sql/compile/operator.go,vm.MultiUpdatecase), and - the remote pipeline serialize / rebuild path (
pkg/sql/compile/remoterun.go,vm.MultiUpdatecase).
So any execution path that clones or ships a MultiUpdate can silently fall back to the old behavior, even though the local/direct path is fixed.
Suggested fix:
- preserve
CountDeleteAffectRowsin thedupOperatorpath forvm.MultiUpdate; - preserve it across remote pipeline round-trip as well — either as a top-level field or by reconstructing it from the incoming
plan.UpdateCtxentries; - add a regression that duplicates / remote-round-trips a
MultiUpdatewithCountDeleteAffectRows=trueand verifies the delete side is still counted.
…te pipeline round-trip CountDeleteAffectRows was only set when MultiUpdate is first built from the plan (constructMultiUpdate), so it was silently dropped when the operator is duplicated for parallel scopes (dupOperator) or shipped to a remote CN (pipeline serialize/rebuild), falling back to the old affected-rows behavior on those paths. - dupOperator now copies CountDeleteAffectRows, matching what the PartitionMultiUpdate clone path already does. - convertToPipelineInstruction carries the flag in each serialized plan.UpdateCtx (the proto field already exists), and convertToVmOperator ORs it back, matching constructMultiUpdate. - Regression tests: dupOperator clone keeps the flag; serialize/rebuild round-trip keeps the flag (and stays false when unset). Co-Authored-By: Claude Fable 5 <[email protected]>
…eep-copy CountDeleteAffectRows Two fixes for the ODKU affected-rows work: 1. The no-op guard FILTER compared old vs new images with NOT(all <=>), but the dedup-join output also carries non-conflicting rows whose old image is all-NULL. Inserting an all-NULL row into a nullable UNIQUE key never conflicts (SQL NULL semantics), yet every NULL <=> NULL comparison is true, so the row was silently dropped instead of inserted. The keep predicate is now isnull(old __mo_rowid) OR NOT(all <=>): only rows that actually hit an existing conflict are subject to the no-op check. 2. DeepCopyUpdateCtxList did not copy CountDeleteAffectRows, so any deep-copied MULTI_UPDATE (e.g. prepared-statement plan copies) lost the flag and conflicting-row affected rows fell back from 2 to 1. Plan UTs updated for the new filter shape, plus a DeepCopyNode round-trip test asserting the flag survives. Co-Authored-By: Claude Fable 5 <[email protected]>
…able unique key An all-NULL row never conflicts on a nullable UNIQUE key, so ON DUPLICATE KEY UPDATE must insert it; the case also re-checks normal conflict update and no-op semantics on the same table. Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
XuPeng-SH
left a comment
There was a problem hiding this comment.
Requesting changes on current head 35e061c.
There is still a correctness gap in the ODKU no-op guard for real-PK tables when the conflict is resolved through a secondary UNIQUE key and the incoming PK is different from the existing row's PK.
Repro shape:
create table t(id int primary key, u int unique, v int);
insert into t values (1, 10, 5);
insert into t values (2, 10, 5) on duplicate key update v = v;
select row_count(); -- MySQL: 0The update clause is a no-op against the conflicting row, so affected rows should be 0. On this PR the no-op FILTER can keep the row as a real update because it compares immutable key columns from the old row against the incoming row image.
The relevant planner path is:
bind_insert.goresolves a secondary-unique conflict totarget_pkand keys the main DEDUP UPDATE join on that value (useTargetPk, thenrightPkPos = targetPkPos).- But the no-op guard later iterates every visible column and builds
old scanTag col <=> new selectTag colusingcolName2Idx[table.col](bind_insert.go:2033-2062). For the PK column this is still the incoming PK position, not the resolved target row's existing PK/final immutable value.
I verified this with a temporary planner test using:
insert into constraint_test.dept(deptno, dname, loc)
values (999, 'Sales', 'NY')
on duplicate key update loc = loc;The generated no-op predicate included:
old dept.deptno <=> col_pos:4
where col_pos:4 is the incoming-side deptno from the dedup output. So an incoming PK like 999 makes the all-cols-equal predicate false even though the user update does not change the conflicting row.
Suggested fix:
- Build the no-op predicate from the actual final row image that will be written, not from the raw incoming row image; or explicitly exclude immutable PK/unique conflict-resolution columns that are not user-updated and compare only columns whose final stored value can actually change.
- Preserve the existing
old __mo_rowid IS NULL OR ...guard for non-conflicting inserts. - Add regression coverage with
ROW_COUNT()for secondary-unique no-op with a different incoming PK, e.g. the SQL above. This PR currently has good unit coverage for helper accounting/flag propagation, but the SQL BVT does not directly assertROW_COUNT()for the issue's affected-rows contract.
Local checks I ran:
go test ./pkg/sql/colexec/multi_update -run 'TestUpsertAffectRowsAccounting|TestAddInsertAffectRows|TestAddDeleteAffectRows'passedgo test ./pkg/sql/plan -run 'TestUpsertAffectRowsPlan'passedgo test ./pkg/sql/compile -run 'TestDupOperatorMultiUpdateCountDeleteAffectRows|TestConvert.*MultiUpdate|Test.*CountDeleteAffectRows'passed for the matching compile test present in this head
Those tests do not cover this secondary-unique no-op case.
…e the dedup-update join The ON DUPLICATE KEY UPDATE no-op guard compared every visible column of the old image against the raw incoming row image. When a conflict is resolved through a secondary UNIQUE key and the incoming PK differs from the existing row's PK, the immutable PK column (never written by the update) compared old-vs-incoming and always mismatched, turning a genuine no-op update into a counted one (affected rows 2 instead of MySQL's 0). Compare only the columns the update actually writes (updateExprs), against their final written value rather than the incoming image. Columns absent from updateExprs keep their old value and are trivially unchanged, so immutable key columns resolved through a secondary-unique conflict are excluded. This exposed a latent pushdown bug: when the update writes a column to a pure function of its old value (e.g. v = v), the no-op predicate references only the old-image (left) side, and the optimizer pushed it below the dedup-update join onto the raw old-table scan. A DEDUP-update join NULL-extends its left side, so on the scan isnull(old rowid) is never true and the matching old row is dropped; the incoming row then looks like a fresh insert and the secondary-unique FAIL dedup raises a spurious "Duplicate entry" error. Keep left-only filters above a DEDUP join that carries a DedupJoinCtx, exactly as OUTER joins already require. Adds plan unit tests (secondary-unique no-op compares only written columns; dedup-update join keeps left filters above the join while plain dedup still pushes them down) and a BVT case asserting a no-op through a secondary-unique conflict with a different incoming PK does not advance an ON UPDATE timestamp. Co-Authored-By: Claude Fable 5 <[email protected]>
|
@XuPeng-SH Thanks for the thorough review. Regarding Instead the new BVT case (
Additionally, I verified protocol-level affected-rows correctness via a
All align with MySQL semantics. |
…t re-run the assignment The previous no-op guard compared the old image against a fresh copy of the update assignment expression. The dedup-update join already evaluates each assignment once to produce the stored value, so re-executing it in the guard double-evaluates non-deterministic assignments (e.g. v = floor(rand()*2)): the guard's value can disagree with what was actually written, either dropping a real update (data loss) or counting a genuine no-op as an update. Compare the old image against the final written value the dedup-update join materialized into the new-image (selectTag) column at colName2Idx instead. That column holds the single evaluation the join stored, so the no-op decision always matches the persisted value. Non-deterministic assignments are now evaluated exactly once. Referencing the new-image column also makes the predicate span both join sides, so it can no longer be pushed below the dedup-update join onto the raw old-table scan. This removes the need for the previous pushdown.go guard, which is reverted. Adds a plan test asserting every no-op <=> compares against a bare new-image column reference (never a re-evaluated function), covering both a trivial (loc = loc) and a computed (loc = substring(loc,1,2)) assignment. Updates the secondary-unique BVT to insert a differing incoming value (v = v with incoming 99 over stored 5) so it asserts the guard uses the materialized old value, not the raw incoming image. Co-Authored-By: Claude Fable 5 <[email protected]>
|
@XuPeng-SH Good catch — fixed in 59b2050. You are right: the previous guard re-evaluated the assignment expression, which double-evaluates a non-deterministic update ( The fix compares the old image against the final written value the dedup-update join already materialized, rather than re-running the assignment. At runtime the join evaluates each A side benefit: referencing the new-image column makes the predicate span both join sides, so it can no longer be pushed below the dedup-update join. That removes the need for the earlier Verification (Go
Also added a plan test asserting every no-op |
XuPeng-SH
left a comment
There was a problem hiding this comment.
LGTM.
The implementation side looks consistent with the MySQL affected-rows contract you are targeting:
- REPLACE and ODKU now opt into counting the DELETE side where appropriate.
- The ODKU no-op guard is placed early enough to compare old-image vs final written value, and it correctly skips implicit ON UPDATE / derived generated-column noise.
- The new flag is threaded through dupOperator and remote pipeline serialization.
The only things I still see are non-blocking coverage gaps: the new SQL/BVT file does not directly assert ROW_COUNT() yet, and the partition/remote propagation is mostly field-copy/round-trip tested rather than query-level affected-row assertions. Given the code path itself and the existing unit/planner/operator coverage, I don't see a substantive correctness blocker here.
XuPeng-SH
left a comment
There was a problem hiding this comment.
LGTM.
The implementation side looks consistent with the MySQL affected-rows contract you are targeting:
- REPLACE and ODKU now opt into counting the DELETE side where appropriate.
- The ODKU no-op guard is placed early enough to compare old-image vs final written value, and it correctly skips implicit ON UPDATE / derived generated-column noise.
- The new flag is threaded through dupOperator and remote pipeline serialization.
I did not find a substantive correctness blocker in the code path itself. The remaining gaps are test-only: the new SQL/BVT file still does not directly assert ROW_COUNT(), and the partition/remote propagation is mostly field-copy/round-trip tested rather than query-level affected-row assertions. Those are worth adding, but they are not enough on their own to block this PR.
Merge Queue Status
This pull request spent 3 hours 27 minutes 8 seconds in the queue, including 1 hour 28 minutes 59 seconds running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
issue #24907
What this PR does / why we need it:
Align affected-rows for REPLACE and INSERT ... ON DUPLICATE KEY UPDATE with MySQL:
Changes
count_delete_affect_rowsfield toUpdateCtxmulti_update/types.go): count DELETE side for upsert when flaggedUpdateCtxfor ODKU and REPLACE; add NOT(<=>) no-op FILTER for ODKU to detect unchanged rows🤖 Generated with Claude Code