Repository navigation
perf(plan): use equi-joins for REPLACE conflicts - #28170
Conversation
|
The unrelated CI blocker #28171 is resolved by merged PR #28172 (main commit |
0278591 to
957d37d
Compare
957d37d to
3f7a346
Compare
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? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 15 seconds in the queue, including 1 second running CI. Required conditions to merge
|
### 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
### 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
### 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
What type of PR is this?
Which issue(s) this PR fixes:
Related to #27141
What this PR does / why we need it:
Root cause
REPLACEold-row discovery combined the primary-key predicate and every unique-key predicate into oneLEFT 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
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.Issue-to-test proof
UNION ALL, and contain no OR join predicate. The PK-only path remains on its existing merged-scan plan.Validation
make -j8 cgomake -j8 build./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/sql/planmo-tester -n -g -p test/distributed/cases/dml/replace: 521/521 statements, 100%3f7a346236over basee7bb057223: stable patch-id55042ece61eematches the prior green head; focused planner test passed all 3 subcases;make -j8 buildandgit diff --checkpassed.957d37db7c:replace_conflict_lookuppassed 24/24 twice; resolved blocker casewindow_hash_partitionpassed 15/15 twice; both test databases were absent after teardown.0278591bd2(10,000 preloaded rows; 12 statements x 25,000 VALUES):300000,300000,300000EXPLAIN VERBOSEon 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