Skip to content

fix: correct checkConflict to return row index instead of unique key ordinal - #23953

Merged
mergify[bot] merged 12 commits into
matrixorigin:mainfrom
robll-v1:issue/23947
Mar 30, 2026
Merged

mergify[bot] merged 12 commits into
matrixorigin:mainfrom
robll-v1:issue/23947

Conversation

@robll-v1

@robll-v1 robll-v1 commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • Bug fix

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 UPDATE could 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 in checkConflictBatch. When the second unique key (i=1) detected a conflict at row 0, the caller misinterpreted 1 as a row index, leading to:

  • Reading the wrong rowId from insertBatch
  • Copying data from/to the wrong row position
  • Failing to detect cross-row conflicts (different unique keys matching different existing rows)

Fix

  1. Core fix: checkConflict() now returns rowIdx (the actual conflicting row index) instead of i (the unique key ordinal).

  2. Remove dead AGG logic: Removed onDuplicateNeedAgg and 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:

    • Same-row: LEFT JOIN with OR conditions produces exactly one output row per matching (left, right) pair; no duplication occurs.
    • Cross-row: Multiple output rows are correctly detected via rowId comparison and produce a constraint violation error.
  3. Variable renames: oldConflictIdx → oldConflictRowIdx, conflictIdx → conflictRowIdx for clarity.

What testing was done

Unit Tests

  • Added TestCheckConflictReturnsBatchRowIndex: directly verifies that when the second unique key (i=1) detects a conflict, the returned value is 0 (row index) not 1 (key ordinal).

BVT Tests (insert_duplicate.sql)

Added 6 new scenarios:

  • 3.4: Multi-UK same-row update (both uk_b and uk_c hit same row → update)
  • 3.5: Multi-UK cross-row error (different UKs hit different rows → error)
  • 3.6: Composite multi-UK same-row update
  • 3.7: Composite multi-UK cross-row error
  • 3.8: Multi-UK with NULL values (NULL should not falsely conflict)
  • 3.9: Partial NULL where non-NULL UK still drives conflict

mo-tester Validation

cd mo-tester && ./run.sh -p matrixone/test/distributed/cases/dml/insert -g
[SUMMARY] COST : 50s, TOTAL :1261, SUCCESS :1239, FAILED :0, IGNORED :22, ABNORAML :0, SUCCESS RATE : 100%

Manual Runtime Probes

  • Verified same-row multi-UK update produces correct result
  • Verified cross-row multi-UK conflict correctly errors and leaves data unchanged
  • Verified additive update (value = value + VALUES(value)) applies exactly once after AGG removal (result 101, not 102)
  • Verified NULL semantics across multiple unique keys

…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]>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Mar 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 from checkConflictBatch and 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.IsIgnore is true the code continues without cleaning newBatch. This skips the newBatch.Clean(...) at the end of the loop and can leak memory (especially for large INSERT IGNORE batches with many conflicts). Clean newBatch before 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.

robll-v1 and others added 5 commits March 30, 2026 12:53
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]>
@mergify

mergify Bot commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-03-30 08:56 UTC · Rule: main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-03-30 08:57 UTC · at cb7e4bd6963fffde989cd5cfd1967f265ab26cfc

This pull request spent 8 seconds in the queue, including 1 second running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • branch-protection-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-skipped = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: INSERT ON DUPLICATE KEY UPDATE - Unify Paths and Fix Semantic Issues

6 participants