Repository navigation
fix(frontend): apply Data Branch updates in place - #27996
Conversation
90c4219 to
baef7a2
Compare
5f30138 to
50db9a8
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-reviewed exact head ad143902644719e65fa703df6397a011dc7d7dfa against base 17c161a31b47a22e71ca222839143f66717ac365, including MERGE/PICK diff provenance, ACCEPT conflict mapping, generated/composite identity keys, staged SQL ordering, schema evolution, special types, FK semantics, performance, cleanup, tests, and the full #27992 issue discussion.
The central fix is sound for the original non-key case: a real-PK source update is now recognized as one logical update, the destination key is carried in a dedicated stage alias, and one staged UPDATE JOIN preserves the referenced row instead of replaying DELETE/INSERT. Generated PK values are used only for matching and are not assigned. Existing and focused exact-head tests for ordinary/composite/generated-key, SET, geometry, and PICK mixed update+insert pass.
Two blocking gaps remain:
-
[P1 correctness] The PR does not solve #27992's documented primary-key update /
ON UPDATE CASCADEcase. The issue discussion explicitly adds the case where the branch changes a referenced primary key and ordinary UPDATE cascades the child FK. This implementation skips every primary-key assignment in bothstagedUpdateSQL(data_branch_output.go:331-342) anddataBranchDirectUpdateSQL(data_branch_output.go:2154-2157). More fundamentally, once the PK changes, the old and new rows no longer have the same hash identity, so they are not emitted as afromUpdatepair and still take the old DELETE/INSERT path.I reproduced this on the exact head with: parent row
id=2, child FKREFERENCES parent(id) ON UPDATE CASCADE, branchUPDATE ... SET id=3 WHERE id=2, thenDATA BRANCH MERGE ... WHEN CONFLICT ACCEPT. It still fails withCannot delete or update a parent row: a foreign key constraint fails; the ordinary UPDATE control cascades successfully. Thus the PR body's “#27992 issue-to-test proof” overstates closure. Preserve old/new identity and apply a native PK UPDATE so FK update actions execute, with deterministic MERGE and PICK regressions. If PK-changing apply is intentionally a separate feature, split it into an explicit tracked issue and make this PR/body unambiguously a partial non-key fix rather than proof that #27992 is resolved. -
[P1 performance] The special-type fallback is table-wide, not assignment-specific, and can turn a large otherwise ordinary update into one SQL statement per row.
dataBranchUpdateNeedsDirectSQLscans all writable columns in the table and returns true when any SET/ENUM or spatial column exists (data_branch_output.go:2049-2070), regardless of whether that column changed.dataBranchStagesUpdatethen disables batching for the entire update batch, andappendOrExecuteDataBranchApplyRowinvokesrunSqlonce per row. For example, updating onlypayload VARCHARon one million rows of(id PK, payload VARCHAR, status ENUM)produces one million parse/plan/execute cycles. This contradicts the PR body's claim that only SET/ENUM/spatial assignments use the exact-row path and undermines the stated large-PICK scalability goal. Classify the columns/rows actually changed (or otherwise omit unchanged planner-incompatible columns from the staged SET list), retain bounded batching for ordinary changes in mixed schemas, and add a mixed-schema large-row regression/benchmark that asserts bounded SQL execution count.
Unhappy-path note: apply tables are regular user-schema tables, but deferred dropApplyTables reuses the operation context. Cancellation can therefore leave __mo_diff_del_*, __mo_diff_ins_*, and the new __mo_diff_upd_* tables behind. This predates the PR for the first two tables, so I am not treating it as a separate blocker here, but the new table expands the leak and should be cleaned with a bounded lifecycle-owned context and covered by cancellation testing.
Validation at exact head:
- substantive GitHub CI: green
git diff --check: pass- focused frontend staged/direct update tests under
-race -count=10: pass TestDataBranchDiffAsFile/update_apply_special_columns: passTestDataBranchPick/accept_mixed_update_and_insert: pass- review counterexample
TestReviewDataBranchPrimaryKeyUpdateCascade: fails as described with the FK constraint error
|
@XuPeng-SH Thank you for the exact-head review. I verified both P1 findings.
The cancellation observation remains unchanged and pre-existing; I left it out of this focused fix. Validation on new head
Could you please re-review the updated head? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
[P1] Preserve UPDATE semantics for indexed special columns
stagedSpecialUpsertSQL routes every ENUM/SET/spatial assignment through INSERT ... ON DUPLICATE KEY UPDATE, but MatrixOne explicitly rejects an ODKU assignment when the assigned column participates in a primary/unique key. ENUM columns are valid unique-index columns, so this remains inside the PR's stated stable-primary-key/non-key-update scope and makes a valid Data Branch update fail.
I reproduced this at exact head 22fbede3d5635c6c9d364820a7c78fcf5b82cc1c with a review-only embedded test:
create table update_enum_unique_base (
id int primary key,
status enum('new','paid','shipped'),
unique key uk_status(status)
);
insert into update_enum_unique_base values (1, 'new'), (2, 'paid');
data branch create table update_enum_unique_branch from update_enum_unique_base;
update update_enum_unique_branch set status = 'shipped' where id = 1;
data branch merge update_enum_unique_branch into update_enum_unique_base when conflict accept;The target value shipped is unused, so the corresponding ordinary UPDATE is valid. The merge instead fails with:
Error 20313 (HY000): unsupported DML: update unique key on duplicate
The rejection comes from pkg/sql/plan/build_constraint_util.go, which disallows updating a PK/unique-key column in ODKU. Because the special-column partition is schema-wide, the generated ODKU can also be emitted when another column is the actual changed column.
Please route special columns that participate in unique keys through a semantics-preserving update path, and add MERGE/PICK regression coverage for both a non-conflicting indexed ENUM update and a duplicate-value failure with unchanged target data.
|
@XuPeng-SH Addressed the indexed-special-column P1 in cae24dc. Changed SET/ENUM/spatial columns that participate in a unique secondary index are now marked during hash diff and applied with the existing exact-key native UPDATE. They are omitted from staged ODKU assignments; non-indexed special changes remain bounded staged ODKU, and an ordinary change in the same indexed-ENUM schema remains a staged UPDATE JOIN. The regression uses a valid branch update and creates the duplicate only in the destination after branching, so it verifies the actual apply-time conflict and transactional target preservation. It covers MERGE and PICK success, MERGE and PICK duplicate failures, and a payload-only update with a unique ENUM. Validation: full pkg/frontend test package and TestDataBranchDiffAsFile/update_apply_special_columns pass; git diff --check passes. The PR description is updated. Could you please re-review this head? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Requesting changes for one correctness blocker on cae24dc9caa172452e40beb7855cd0220a3bfc1e.
[P1] Preserve the complete source row for WHEN CONFLICT ACCEPT.
The new native/staged decision is made from the source-side update relative to the LCA. That is insufficient once the destination changed an indexed special column independently. If the source changes only an ordinary column, requiresNativeUpdate stays false; the staged path then deliberately omits every indexed special column, so ACCEPT keeps the destination value instead of replacing it with the source value. The result is a source/destination hybrid row, contradicting the documented “source value wins” contract.
I reproduced this on the exact head with both MERGE and PICK:
create table t (
id int primary key,
payload varchar(32),
status enum('new','paid','shipped'),
unique key uk_status(status)
);
insert into t values (1, 'base-one', 'new'), (2, 'two', 'paid');
-- create source branch from t
update source_t set payload = 'source-one' where id = 1;
update t set status = 'shipped' where id = 1;
-- MERGE/PICK source_t into t WHEN CONFLICT ACCEPTExpected row 1: (1, 'source-one', 'new') (the complete source row).
Actual row 1: (1, 'source-one', 'shipped').
Both focused integration tests fail deterministically with that mismatch. This is within the PR's supported scope: stable primary key, non-key updates, and an indexed ENUM column.
Please make accepted replacement rows use a semantics-preserving full-row apply whenever an omitted indexed-special value can differ in the destination (or use another mechanism that applies every writable source value), and add this cross-column conflict regression for both MERGE and PICK.
|
@XuPeng-SH Verified the cross-column A destination update that changed an indexed SET/ENUM/spatial value is recorded by primary key. If the matching source update changes only ordinary data, that one row is split out of its staged batch and applied with the existing full-row exact-key native UPDATE; all other rows remain staged. This restores the complete source row rather than retaining a destination special value.
|
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep-reviewed exact head c9e33a5cddcb1d662c880976d845b488ea1cc97e against base 17c161a31b47a22e71ca222839143f66717ac365.
The previous source-wins blocker is resolved. For WHEN CONFLICT ACCEPT, a destination-side update of an indexed SET/ENUM/spatial column is now remembered by exact primary key; only a matching ordinary source-update row is split from the staged batch and routed through the full-row native UPDATE. This restores every writable source value instead of retaining an omitted destination indexed-special value. Unmatched rows remain on the bounded staged path.
I checked the change from correctness, conflict semantics, batching/performance, composite-key identity, unique-index failure atomicity, cancellation/ownership, and MERGE/PICK perspectives. I found no blocking issue.
Counterexample validation on this exact head:
- the prior MERGE and PICK cross-column repro now returns the complete source row;
- a mixed update batch correctly routes only the conflicting row through native UPDATE while retaining the other row;
- a composite primary key containing a comma plus a composite unique ENUM index matches the correct row;
- when the accepted source row would violate another destination unique key, MERGE fails and leaves the full destination state unchanged;
- focused batch-split and staged/native output tests pass under
-race -count=3; git diff --checkand all substantive GitHub CI checks pass.
Performance remains proportional to the affected data. The extra primary-key maps and scan are linear; the per-row native path is restricted to changed indexed-special rows and accepted cross-column conflicts, while normal and non-indexed-special updates retain bounded staging. Primary-key-changing updates remain explicitly outside this PR and are tracked by #28026.
Merge Queue Status
This pull request spent 39 minutes 22 seconds in the queue, with no time running CI. Waiting for
All conditions
ReasonPull request #27996 has been dequeued Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.
Failing checks:
HintYou should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it. Tick the box to put this pull request back in the merge queue (same as
|
What type of PR is this?
Which issue(s) this PR fixes:
Related to #27992
What this PR does / why we need it:
Scope
This PR covers stable-primary-key, non-key updates. A source primary-key move with ON UPDATE CASCADE needs durable old/new row identity in the Data Branch change stream; that separate behavior is tracked in #28026 and is not claimed here.
Root cause
Data Branch represents a source-side non-key update as a DELETE/INSERT diff pair. Replaying the delete first transiently removes a referenced destination row, so foreign-key validation rejects an otherwise valid update.
The staged UPDATE JOIN remedy must carry generated identity values without assigning them. SET/ENUM and spatial values cannot be assigned through that join. Non-indexed values can use a bounded staged ON DUPLICATE KEY UPDATE path, but MatrixOne rejects an ODKU assignment to a primary or unique-key column.
Changes
Issue-to-test proof
Tests
Residual risk
Primary-key-changing Data Branch updates, including ON UPDATE CASCADE, remain outside this PR and are tracked in #28026.
ACCEPT replacement refinement
WHEN CONFLICT ACCEPTreplaces every writable destination value with the source row.TestDataBranchDiffAsFile/update_apply_special_columns/indexed_enum_conflict_acceptproves MERGE and PICK source-wins behavior;TestSplitDataBranchBatchForNativeUpdateproves only matched rows leave the staged batch.