Repository navigation
fix: correct checkConflict to return row index instead of unique key ordinal - #23953
Conversation
…ordinal (matrixorigin#23947) When a table has multiple unique keys, checkConflict() previously returned the unique key ordinal (loop variable i) instead of the actual conflicting row index in checkConflictBatch. This caused callers to: - Read the wrong rowId from insertBatch - Copy data from/to the wrong row position Changes: - checkConflict(): return rowIdx (the actual conflicting row index) instead of i (the unique key ordinal) - Remove onDuplicateNeedAgg and the AGG/any_value() plan node injection in the legacy planner path, which is no longer needed since the execution layer now correctly distinguishes same-row vs cross-row conflicts - Rename conflict index variables for clarity (oldConflictIdx -> oldConflictRowIdx, conflictIdx -> conflictRowIdx) - Add unit test TestCheckConflictReturnsBatchRowIndex - Add BVT cases for multi-unique-key scenarios: same-row update, cross-row error, composite keys, and NULL semantics Approved by: @aspect-build Co-authored-by: Copilot <[email protected]>
|
|
There was a problem hiding this comment.
Pull request overview
Fixes incorrect behavior (and potential silent corruption) in the legacy INSERT ... ON DUPLICATE KEY UPDATE path when multiple UNIQUE keys exist, by making conflict detection return the conflicting row index (not the unique-key ordinal) and aligning planner behavior/tests accordingly.
Changes:
- Fix
checkConflict()to return the conflicting row index fromcheckConflictBatchand rename related variables for clarity. - Remove legacy planner “dedup via AGG/any_value()” injection for multi-unique-key ODKU.
- Add unit + distributed SQL test coverage for multi-UK same-row update vs cross-row conflict (including NULL cases), and update expected results.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
pkg/sql/colexec/onduplicatekey/on_duplicate_key.go |
Core fix: return conflicting row index from checkConflict(); rename conflict index variables. |
pkg/sql/colexec/onduplicatekey/on_duplicate_key_test.go |
Adds unit test to assert checkConflict() returns row index (0) when conflict is detected by the 2nd unique-key check. |
pkg/sql/plan/build_insert.go |
Removes legacy AGG/any_value() node insertion before ON_DUPLICATE_KEY. |
pkg/sql/plan/build_constraint_util.go |
Removes onDuplicateNeedAgg plumbing from legacy insert rewrite info. |
test/distributed/cases/dml/insert/insert_duplicate.sql |
Adds distributed regression scenarios for multi-UK ODKU (same-row update, cross-row error, composite UKs, NULL semantics). |
test/distributed/cases/dml/insert/insert_duplicate.result |
Adds/updates expected output for the distributed test suite after new scenarios. |
Comments suppressed due to low confidence (1)
pkg/sql/colexec/onduplicatekey/on_duplicate_key.go:153
- In the conflict branch, when
insertArg.IsIgnoreis true the codecontinues without cleaningnewBatch. This skips thenewBatch.Clean(...)at the end of the loop and can leak memory (especially for large INSERT IGNORE batches with many conflicts). CleannewBatchbefore continuing (or restructure to ensure cleanup on all paths).
if insertArg.IsIgnore {
continue
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Carry forward the upstream cleanup test into the PR branch so the conflicted test file merges cleanly without bringing in unrelated merge content. Co-authored-by: Copilot <[email protected]>
Reorder the conflicted test additions so upstream/main can merge the file cleanly while keeping the PR's row-index regression coverage. Co-authored-by: Copilot <[email protected]>
Place the PR-specific row-index regression tests and helpers away from the upstream insertion point so the conflicted test file merges cleanly while preserving coverage. Co-authored-by: Copilot <[email protected]>
Merge Queue Status
This pull request spent 8 seconds in the queue, including 1 second running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) does this PR fix?
Fixes #23947
What this PR does / why we need it:
When a table has multiple unique keys (e.g.,
UNIQUE KEY uk_b(b), UNIQUE KEY uk_c(c)),INSERT ... ON DUPLICATE KEY UPDATEcould produce incorrect results or silently corrupt data.Root Cause
checkConflict()in the legacy ODKU execution path returned the unique key ordinal (i, the outer loop variable) instead of the conflicting row index incheckConflictBatch. When the second unique key (i=1) detected a conflict at row 0, the caller misinterpreted1as a row index, leading to:rowIdfrominsertBatchFix
Core fix:
checkConflict()now returnsrowIdx(the actual conflicting row index) instead ofi(the unique key ordinal).Remove dead AGG logic: Removed
onDuplicateNeedAggand the AGG/any_value()plan node injection in the legacy planner path. This was previously used to deduplicate LEFT JOIN output when multiple unique keys existed, but is no longer needed—the execution layer now correctly handles same-row vs cross-row conflicts:rowIdcomparison and produce a constraint violation error.Variable renames:
oldConflictIdx→oldConflictRowIdx,conflictIdx→conflictRowIdxfor clarity.What testing was done
Unit Tests
TestCheckConflictReturnsBatchRowIndex: directly verifies that when the second unique key (i=1) detects a conflict, the returned value is0(row index) not1(key ordinal).BVT Tests (insert_duplicate.sql)
Added 6 new scenarios:
uk_banduk_chit same row → update)mo-tester Validation
Manual Runtime Probes
value = value + VALUES(value)) applies exactly once after AGG removal (result 101, not 102)