Skip to content

perf(plan): use equi-joins for REPLACE conflicts - #28170

Merged
mergify[bot] merged 1 commit into
matrixorigin:mainfrom
gouhongshen:agent/issue-27141-f3027b95
Sep 5, 2026
Merged

mergify[bot] merged 1 commit into
matrixorigin:mainfrom
gouhongshen:agent/issue-27141-f3027b95

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #27141

What this PR does / why we need it:

Root cause

REPLACE old-row discovery combined the primary-key predicate and every unique-key predicate into one LEFT JOIN ... ON pk_match OR uk_match. The OR predicate prevented the equality hash-join/runtime-filter path, so even the first statement could execute as a loop join against the full base table.

Changes

  • Materialize the normalized REPLACE source once when more than one uniqueness constraint must be checked.
  • Build one equality-only lookup branch per constraint:
    • PK branch: source PK to base-table PK.
    • UK branch: serialized source key to the hidden unique-index key, then hidden-index PK to base-table PK.
  • Merge candidate old rows with an input-row ordinal plus UNION DISTINCT, then restore input order before the existing DEDUP joins. The ordinal prevents equal source rows from being collapsed, while distinct union removes duplicate candidates when several constraints find the same old row.
  • Remove the old OR-join construction and reuse this lookup path for the existing non-merged REPLACE cases.

Issue-to-test proof

  • Planner regression tests assert that PK+UK and PK+composite-UK REPLACE plans contain the ordered branch merge, contain no UNION ALL, and contain no OR join predicate. The PK-only path remains on its existing merged-scan plan.
  • The new BVT uses the issue's exact PK+UK schema and checks same-row dual conflicts, different-row PK/UK conflicts, PK-only conflicts, UK-only conflicts, insert-only rows, mixed batches, affected-row counts, transaction commit results, and keep-last ordering for duplicate input keys.
  • The complete existing REPLACE BVT directory covers auto-increment, multiple unique indexes, composite keys, NULL/NOT NULL handling, cross-key fanout, and workspace behavior.

Validation

  • make -j8 cgo
  • make -j8 build
  • ./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/sql/plan
  • mo-tester -n -g -p test/distributed/cases/dml/replace: 521/521 statements, 100%
  • New BVT repeated twice on one service instance: 24/24 statements each, 100%
  • Latest post-rebase validation on 3f7a346236 over base e7bb057223: stable patch-id 55042ece61ee matches the prior green head; focused planner test passed all 3 subcases; make -j8 build and git diff --check passed.
  • Exact BVT on patch-identical head 957d37db7c: replace_conflict_lookup passed 24/24 twice; resolved blocker case window_hash_partition passed 15/15 twice; both test databases were absent after teardown.
  • Reduced issue workload on commit 0278591bd2 (10,000 preloaded rows; 12 statements x 25,000 VALUES):
    • statement latency: min 1.712s, median 1.818s, max 2.223s
    • transaction execution: 22.010s
    • commit: 8.29ms
    • final validation: 300000,300000,300000
  • EXPLAIN VERBOSE on the final commit contains equality join conditions and runtime-filter probes for both PK and hidden UK paths; it contains no loop join and no OR join condition.

Residual risks

  • Multi-constraint REPLACE now pays source materialization, distinct-union, and ordinal-sort cost proportional to input rows and constraint count. The exact reduced workload above exercises that path; PK-only plans do not take it.
  • The 20-million-row original workload was not run locally. Its reported nonlinear operator is removed by plan construction, and the final plan exposes runtime filters on both lookup paths, but that scale still needs manual environment validation.

@gouhongshen

gouhongshen commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor Author

The unrelated CI blocker #28171 is resolved by merged PR #28172 (main commit 313991b503), which updates the stale window_hash_partition ANALYZE expectation. This branch is rebased onto that commit, and the replacement head started fresh workflow 33937004888. Local normal-comparison validation passed window_hash_partition 15/15 twice.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH 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.

Deep review of exact head 3f7a346236400e25ee0f7c4bad7f456ddac1c341 against merge-base e7bb0572235ec4bac81eb098eb0ad8900f0065ab. I read every changed hunk and traced normalized REPLACE input through materialization/ordinal assignment, per-PK/UK equality lookup branches, UNION DISTINCT, order restoration, downstream DEDUP/MULTI_UPDATE, hidden-index maintenance, transactions, and FK actions. Counterexamples checked include same-row dual matches, PK/UK matches to different rows, matched plus unmatched branches, multiple and composite UKs, fake PK, static NULL UKs, auto-increment, duplicate/batched inputs, and insert-only rows. The existing exact-head green CI plus focused planner and full REPLACE BVT evidence cover these contracts. The added materialization/distinct/sort cost is confined to multi-constraint REPLACE and is a justified bounded tradeoff for eliminating the table-scale OR loop join; PK-only keeps the merged-scan path. No correctness, unhappy-path, resource, compatibility, or performance blocker found.

@mergify mergify Bot added the queued label Sep 5, 2026
@mergify

mergify Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-05 05:50 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-09-05 05:50 UTC · at 512f726384ac2aa4088a78cfe380f4e6d7be2ba8 · squash

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

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • 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 CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

@mergify
mergify Bot merged commit 512f726 into matrixorigin:main Sep 5, 2026
43 of 49 checks passed
@mergify mergify Bot removed the queued label Sep 5, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
### Root cause

`REPLACE` old-row discovery combined the primary-key predicate and every unique-key predicate into one `LEFT JOIN ... ON pk_match OR uk_match`. The OR predicate prevented the equality hash-join/runtime-filter path, so even the first statement could execute as a loop join against the full base table.

### Changes

- Materialize the normalized REPLACE source once when more than one uniqueness constraint must be checked.
- Build one equality-only lookup branch per constraint:
- PK branch: source PK to base-table PK.
- UK branch: serialized source key to the hidden unique-index key, then hidden-index PK to base-table PK.
- Merge candidate old rows with an input-row ordinal plus `UNION DISTINCT`, then restore input order before the existing DEDUP joins. The ordinal prevents equal source rows from being collapsed, while distinct union removes duplicate candidates when several constraints find the same old row.
- Remove the old OR-join construction and reuse this lookup path for the existing non-merged REPLACE cases.

### Issue-to-test proof

- Planner regression tests assert that PK+UK and PK+composite-UK REPLACE plans contain the ordered branch merge, contain no `UNION ALL`, and contain no OR join predicate. The PK-only path remains on its existing merged-scan plan.
- The new BVT uses the issue's exact PK+UK schema and checks same-row dual conflicts, different-row PK/UK conflicts, PK-only conflicts, UK-only conflicts, insert-only rows, mixed batches, affected-row counts, transaction commit results, and keep-last ordering for duplicate input keys.
- The complete existing REPLACE BVT directory covers auto-increment, multiple unique indexes, composite keys, NULL/NOT NULL handling, cross-key fanout, and workspace behavior.

### Validation

- `make -j8 cgo`
- `make -j8 build`
- `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/sql/plan`
- `mo-tester -n -g -p test/distributed/cases/dml/replace`: 521/521 statements, 100%
- New BVT repeated twice on one service instance: 24/24 statements each, 100%
- Latest post-rebase validation on `3f7a346236` over base `e7bb057223`: stable patch-id `55042ece61ee` matches the prior green head; focused planner test passed all 3 subcases; `make -j8 build` and `git diff --check` passed.
- Exact BVT on patch-identical head `957d37db7c`: `replace_conflict_lookup` passed 24/24 twice; resolved blocker case `window_hash_partition` passed 15/15 twice; both test databases were absent after teardown.
- Reduced issue workload on commit `0278591bd2` (10,000 preloaded rows; 12 statements x 25,000 VALUES):
- statement latency: min 1.712s, median 1.818s, max 2.223s
- transaction execution: 22.010s
- commit: 8.29ms
- final validation: `300000,300000,300000`
- `EXPLAIN VERBOSE` on the final commit contains equality join conditions and runtime-filter probes for both PK and hidden UK paths; it contains no loop join and no OR join condition.

### Residual risks

- Multi-constraint REPLACE now pays source materialization, distinct-union, and ordinal-sort cost proportional to input rows and constraint count. The exact reduced workload above exercises that path; PK-only plans do not take it.
- The 20-million-row original workload was not run locally. Its reported nonlinear operator is removed by plan construction, and the final plan exposes runtime filters on both lookup paths, but that scale still needs manual environment validation.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
### Root cause

`REPLACE` old-row discovery combined the primary-key predicate and every unique-key predicate into one `LEFT JOIN ... ON pk_match OR uk_match`. The OR predicate prevented the equality hash-join/runtime-filter path, so even the first statement could execute as a loop join against the full base table.

### Changes

- Materialize the normalized REPLACE source once when more than one uniqueness constraint must be checked.
- Build one equality-only lookup branch per constraint:
- PK branch: source PK to base-table PK.
- UK branch: serialized source key to the hidden unique-index key, then hidden-index PK to base-table PK.
- Merge candidate old rows with an input-row ordinal plus `UNION DISTINCT`, then restore input order before the existing DEDUP joins. The ordinal prevents equal source rows from being collapsed, while distinct union removes duplicate candidates when several constraints find the same old row.
- Remove the old OR-join construction and reuse this lookup path for the existing non-merged REPLACE cases.

### Issue-to-test proof

- Planner regression tests assert that PK+UK and PK+composite-UK REPLACE plans contain the ordered branch merge, contain no `UNION ALL`, and contain no OR join predicate. The PK-only path remains on its existing merged-scan plan.
- The new BVT uses the issue's exact PK+UK schema and checks same-row dual conflicts, different-row PK/UK conflicts, PK-only conflicts, UK-only conflicts, insert-only rows, mixed batches, affected-row counts, transaction commit results, and keep-last ordering for duplicate input keys.
- The complete existing REPLACE BVT directory covers auto-increment, multiple unique indexes, composite keys, NULL/NOT NULL handling, cross-key fanout, and workspace behavior.

### Validation

- `make -j8 cgo`
- `make -j8 build`
- `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/sql/plan`
- `mo-tester -n -g -p test/distributed/cases/dml/replace`: 521/521 statements, 100%
- New BVT repeated twice on one service instance: 24/24 statements each, 100%
- Latest post-rebase validation on `3f7a346236` over base `e7bb057223`: stable patch-id `55042ece61ee` matches the prior green head; focused planner test passed all 3 subcases; `make -j8 build` and `git diff --check` passed.
- Exact BVT on patch-identical head `957d37db7c`: `replace_conflict_lookup` passed 24/24 twice; resolved blocker case `window_hash_partition` passed 15/15 twice; both test databases were absent after teardown.
- Reduced issue workload on commit `0278591bd2` (10,000 preloaded rows; 12 statements x 25,000 VALUES):
- statement latency: min 1.712s, median 1.818s, max 2.223s
- transaction execution: 22.010s
- commit: 8.29ms
- final validation: `300000,300000,300000`
- `EXPLAIN VERBOSE` on the final commit contains equality join conditions and runtime-filter probes for both PK and hidden UK paths; it contains no loop join and no OR join condition.

### Residual risks

- Multi-constraint REPLACE now pays source materialization, distinct-union, and ordinal-sort cost proportional to input rows and constraint count. The exact reduced workload above exercises that path; PK-only plans do not take it.
- The 20-million-row original workload was not run locally. Its reported nonlinear operator is removed by plan construction, and the final plan exposes runtime filters on both lookup paths, but that scale still needs manual environment validation.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
### Root cause

`REPLACE` old-row discovery combined the primary-key predicate and every unique-key predicate into one `LEFT JOIN ... ON pk_match OR uk_match`. The OR predicate prevented the equality hash-join/runtime-filter path, so even the first statement could execute as a loop join against the full base table.

### Changes

- Materialize the normalized REPLACE source once when more than one uniqueness constraint must be checked.
- Build one equality-only lookup branch per constraint:
- PK branch: source PK to base-table PK.
- UK branch: serialized source key to the hidden unique-index key, then hidden-index PK to base-table PK.
- Merge candidate old rows with an input-row ordinal plus `UNION DISTINCT`, then restore input order before the existing DEDUP joins. The ordinal prevents equal source rows from being collapsed, while distinct union removes duplicate candidates when several constraints find the same old row.
- Remove the old OR-join construction and reuse this lookup path for the existing non-merged REPLACE cases.

### Issue-to-test proof

- Planner regression tests assert that PK+UK and PK+composite-UK REPLACE plans contain the ordered branch merge, contain no `UNION ALL`, and contain no OR join predicate. The PK-only path remains on its existing merged-scan plan.
- The new BVT uses the issue's exact PK+UK schema and checks same-row dual conflicts, different-row PK/UK conflicts, PK-only conflicts, UK-only conflicts, insert-only rows, mixed batches, affected-row counts, transaction commit results, and keep-last ordering for duplicate input keys.
- The complete existing REPLACE BVT directory covers auto-increment, multiple unique indexes, composite keys, NULL/NOT NULL handling, cross-key fanout, and workspace behavior.

### Validation

- `make -j8 cgo`
- `make -j8 build`
- `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/sql/plan`
- `mo-tester -n -g -p test/distributed/cases/dml/replace`: 521/521 statements, 100%
- New BVT repeated twice on one service instance: 24/24 statements each, 100%
- Latest post-rebase validation on `3f7a346236` over base `e7bb057223`: stable patch-id `55042ece61ee` matches the prior green head; focused planner test passed all 3 subcases; `make -j8 build` and `git diff --check` passed.
- Exact BVT on patch-identical head `957d37db7c`: `replace_conflict_lookup` passed 24/24 twice; resolved blocker case `window_hash_partition` passed 15/15 twice; both test databases were absent after teardown.
- Reduced issue workload on commit `0278591bd2` (10,000 preloaded rows; 12 statements x 25,000 VALUES):
- statement latency: min 1.712s, median 1.818s, max 2.223s
- transaction execution: 22.010s
- commit: 8.29ms
- final validation: `300000,300000,300000`
- `EXPLAIN VERBOSE` on the final commit contains equality join conditions and runtime-filter probes for both PK and hidden UK paths; it contains no loop join and no OR join condition.

### Residual risks

- Multi-constraint REPLACE now pays source materialization, distinct-union, and ordinal-sort cost proportional to input rows and constraint count. The exact reduced workload above exercises that path; PK-only plans do not take it.
- The 20-million-row original workload was not run locally. Its reported nonlinear operator is removed by plan construction, and the final plan exposes runtime filters on both lookup paths, but that scale still needs manual environment validation.

Approved by: @XuPeng-SH

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/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants