Skip to content

fix(sql): preserve accepted INSERT IGNORE rows across constraint failures - #33528

Merged
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
XuPeng-SH:fix/insert-ignore-constraint-actions
Nov 30, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
XuPeng-SH:fix/insert-ignore-constraint-actions

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • Improvement

Which issue(s) this PR fixes:

Fixes #33521
Fixes #33522
Fixes #33523

Related to #28159 and #28238. Main already accepts the combined syntax through #28482. #28349 fixed the original duplicate-only generated-ID example. This PR also addresses accepted-row ID assignment for plain INSERT IGNORE when an independent CHECK or FK rejects input.

What this PR does / why we need it:

Each ignored action must either publish one valid result or leave the last accepted row, its keys and counters intact. Previously plain insert could omit warning 1452, an orphan could abort valid siblings, and a failed duplicate-update CHECK could still write the rejected value.

  • Evaluate the offered CHECK before arbitration. Evaluate affected constraints on the completed, left-to-right UPDATE assignment before publishing that image. A later proposal sees the last accepted image.
  • Use the existing DEDUP operator for a primary-key-only target. The first accepted input seeds a fresh group. Where several UNIQUE keys compete, the existing ordered arbiter checks the FK only after every key misses and before reserving any key. A duplicate that changes another column does no speculative offered-parent lookup.
  • Both execution paths use the same typed, transaction-visible FK lookup. An exact scalar parent PK uses bounded memoized storage probes. A changed tuple that needs canonical comparison builds an accounted parent set on demand. The final accepted reference still takes its parent witness lock and is revalidated. Read failures and resource errors propagate.
  • Feed independent CHECK/FK admission to the existing ordered AUTO_INCREMENT arbiter for plain IGNORE. Rejected rows do not consume another accepted row's candidate. Provisional-ID-dependent constraints keep their original evaluation order.

Ordinary INSERT IGNORE and LOAD DATA IGNORE share the FK diagnostic implementation. Only the existing LOAD function requires protocol v125. Parent membership uses the hash join iterator’s SQL equality rules for signed zero and NaN.

Validation

  • Four standalone SQL regression suites. 1,024 statements passed, 14 existing skips, zero failures. The 86 statements in the new regression file cover same-target replay, conflicting UNIQUE keys, nullable and composite FKs, prepared execution, transaction visibility, and reservation behavior.
  • MySQL 8.4.8 comparison, repeating a seeded 50-statement workload with a PK-only child and with a second UNIQUE. All 100 post-statement row sets and the per-statement counts of 287 constraint warnings matched. The 17,000-row spill exercise matched across a rejected prefix and batch boundaries. A separate 16,391-input AUTO_INCREMENT case matched all 14,317 accepted ID/payload pairs and the next ID.
  • Verified binary string FK lookup, mixed fresh and existing rows with two FKs, and reset/reuse after failed reads. A concurrent parent delete waited for the child commit and then returned 1451. Client affected-row counts matched with and without CLIENT_FOUND_ROWS.
  • On a standalone CN at protocol v124, plain and auto-increment INSERT IGNORE each returned warning 1452 and committed their valid siblings. LOAD DATA IGNORE returned its required-version error at v124, then warned and committed its valid sibling at v125.
  • Running the new SQL file directly from an empty database and from a database containing an incompatible table produced identical results. Its mo-tester run and the full four-suite run also passed.
  • The repository-wide type-family guard passed in coverage mode. Local race tests passed in all 13 affected packages, including the hashmap, FK lookup, both execution owners, planning and SQL functions. Incremental golangci-lint checked the changed files in full. The dedupjoin package also passed unfiltered lint, including the receiver-consistency check.

Performance

These measurements compare main c22be4264e to the demand-driven implementation at 5d83c369c0. Two local standalone clusters ran the same SQL in alternating order with 500,000 real parent PKs. In the second round the binaries exchanged cluster slots. Each number is the median of 48 timed statements after eight warmups, in milliseconds.

Duplicate action Round 1 main Round 1 PR Round 2 main Round 2 PR
PK hit, change ordinary column 6.53 8.83 6.99 7.65
Secondary UNIQUE hit, change ordinary column 7.21 8.90 7.64 7.78
Changed computed FK equals the offered value 11.85 7.69 11.27 7.33
Changed computed FK differs from the offered value 11.20 8.24 11.67 8.10
No FK control 5.25 5.29 5.05 5.04

The original extra full-parent pass is gone. Fixed-target ordinary updates still measured slower in these two rounds. In a separate varied-target test the median paired differences were −0.32 ms for the PK and −0.08 ms for the secondary UNIQUE. For a fixed PK, 20 paired physical profiles measured DEDUP at 0.030 / 0.032 ms on main / PR. These data do not establish that the remaining end-to-end difference is required for correctness.

On 500,000 parent keys, EXPLAIN PHYPLAN ANALYZE for a duplicate changing only an ordinary column shows one parent hash build in main and in this revision. The first PR revision did two. The remaining build belongs to final FK validation. For a changed scalar FK, both forms of the computed assignment use one final parent build and a keyed membership probe.

For dense demand, the lookup switches from key probes to one accounted parent set after 64 distinct scalar keys. Composite or coerced keys use the canonical set at first demand. Bulk tests used 1, 64, 65, 256 and 8,192 rows against 1,000 and 500,000 parents. For 8,192 fresh rows against 500,000 parents, the final FK witness loop already present in main dominated the physical profiles (6.59 s on main and 7.56 s here). The new DEDUP stage took 27 ms in that PR profile. A five-sample autocommit run measured medians of 8.31 / 10.38 s. In a separate paired run within transactions, with rollback outside the measured insert, the medians were 11.29 / 8.67 s. The measurements do not establish a general bulk speedup.

Self-referencing FKs continue through the existing post-write validation. Explicit NULL coercion in ODKU assignments to NOT NULL columns, ODKU provisional IDs, and the allocator high watermark after an explicit positive ID retain their existing behavior.

@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 →

@mergify

mergify Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: 6541715e5d87c0895aa9e61fe7ad7a0b881d43e1

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: 6541715e5d87c0895aa9e61fe7ad7a0b881d43e1

Assessment: approve

No actionable regression was established. The change reuses existing foreign-key joins and witness locks, while adding an accounted parent-key set for computed updates that must be evaluated against earlier accepted actions.

Model: gpt-6.1-terra / max

Session: 01a22aff-bb68-7e31-ae71-ee4ab48ad9d0 · slot 0 · generation 45

Validation:

  • Private /tmp and loopback preflight passed.

  • Focused deduplication, ordered-insert and planner tests passed: checks/1796048354689926552-3.json and focused-tests.log.

  • A single-CN SQL probe passed for ordered CHECK and FK rejection, warning counts, generated IDs and an uncommitted parent: checks/1796048491162046955-3.json and sql-probe.log.

  • A concurrent parent-delete probe passed, including a final rejected update after an accepted FK change: checks/1796048799960717380-5.json and lock-rejected-last.log.

  • New plan and pipeline fields passed a wire round-trip probe: checks/1796048753903985538-3.json and wire-probe.log.

  • A two-CN SQL probe passed with multi-CN scheduling enabled: checks/1796049471888169015-4.json and multicn-standard.log.

  • git diff --check passed; the assigned checkout remained clean.

Limitations:

  • A coordinator-draining two-CN variant timed out on both the candidate and a pinned-base source overlay. The captured stacks show runtime-filter waits, but the root cause was not established; the successful normal two-CN run does not validate that forced variant.

  • The full BVT suites, MySQL differential workload, benchmarks and race tests reported in the PR were not independently repeated; local_validation contained no retained author runs.

@XuPeng-SH

XuPeng-SH commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: 5d83c369c027057c9476c3f6a8d364642c2e0c6f

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: 5d83c369c027057c9476c3f6a8d364642c2e0c6f

Assessment: request_changes

The ordered-action design reuses existing arbiters and parent witness locks, while the bounded membership lookup supports action-specific foreign-key checks. The reviewed paths passed focused validation, but the change extends a protocol gate contrary to the pinned upgrade policy, and the new distributed test expects an error on a clean run. The earlier approval applied to a prior head and does not cover the current planner and membership changes.

Model: gpt-6.1-terra / max

Session: 01a22af4-cf2a-7f70-88e4-931bbf57d739 · slot 2 · generation 14

  • P2 pkg/sql/plan/bind_insert.go:2486: An ordinary INSERT IGNORE checks a non-self-referencing foreign key, and the local MOProtocolVersion is missing or below 125.
    Impact: The statement now fails during binding, introducing a version-dependent failure branch and maintenance dependency on a path that previously did not use this gate. This is a policy and design issue; it is not evidence of a failure in a supported shutdown-only upgrade.
    Evidence: At the pinned base, the version check and _load_fk_ignore_check were used only when loadWarnings was true. The shared binder now uses both for ordinary INSERT IGNORE, and pkg/sql/colexec/evalExpression.go:522 applies the gate again when constructing the executor. The pinned .agents/skills/mo-dev/references/upgrade-compatibility.md:94-109 explicitly says existing gates must not be extended to new features. Preserve the existing LOAD behavior while allowing ordinary INSERT diagnostics without extending that gate.

  • P2 test/distributed/cases/dml/insert/insert_ignore_constraint_actions.result:2: The new distributed test runs when insert_ignore_constraint_actions does not already exist, including after its own final DROP DATABASE.
    Impact: CREATE DATABASE succeeds, but the checked-in result expects an already-exists error, so a clean result comparison cannot pass. The test also depends on leftover database state despite the repository's self-contained test requirement.
    Evidence: The SQL file starts with CREATE DATABASE and drops that database at line 103. The retained one-CN SQL probe confirmed that a clean CREATE succeeds and a duplicate CREATE errors (public-probe.log; checks/1796051782323603509-3.json). Initialize the fixture from a clean state, then regenerate its expected result.

Validation:

  • Isolation preflight passed (preflight.log; checks/1796051379849653097-3.json).

  • Focused non-race tests passed for the affected lookup, deduplication, unique-insert, hash-build and planner packages (focused.log; checks/1796051697671546362-3.json).

  • One-CN public SQL probes, including the 65-key membership transition, passed (public-probe.log and warning-probe.log; checks/1796051782323603509-3.json and checks/1796051935886880564-3.json).

  • The two-CN computed-FK probe and one-CN parent witness-lock probe passed (multicn.log and lock-probe.log; checks/1796052001256259650-3.json and checks/1796052067433587491-3.json).

  • Wire round-trip and selected compiler tests passed; selected lookup and arbitration tests also passed under the race detector with count=10 (wire-compile.log and focused-race.log; checks/1796052021415944379-3.json and checks/1796052210669868907-3.json).

  • git diff --check passed for the assigned base and head (diff-check.log; checks/1796052521392001020-3.json).

Limitations:

  • The complete distributed BVT suite and an external MySQL differential run were not performed.

  • The earlier review reported that a forced coordinator-drain two-CN control timed out on both its candidate and baseline. The current normal two-CN probe passed, but it did not exercise that variant.

  • Mixed CHECK and foreign-key warnings were observed in stage order. Existing repository fixtures do not establish a universal input-row warning order, so that observation is not reported as a defect.

@XuPeng-SH

XuPeng-SH commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: b5946447f5156e24407ba9ca54b09b9f4c2e8bc7

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: b5946447f5156e24407ba9ca54b09b9f4c2e8bc7

Assessment: approve

No actionable defects remain. The change extends the existing ordered insert arbiters and parent-witness locking to handle rejected CHECK and foreign-key actions. The bounded, demand-driven parent lookup supports validation of successive update images without introducing a separate locking mechanism. The prior findings concerning the LOAD protocol gate and BVT fixture isolation are resolved.

Model: gpt-6.1-terra / max

Session: 01a22b07-00b6-7e32-b0a1-3099d0b867c7 · slot 1 · generation 39

Validation:

  • Isolation preflight and the native-input provenance audit passed against the frozen head.

  • Focused tests passed across seven packages, covering admission, foreign-key lookup, hash equality, planning and diagnostics. Selected race tests passed across four affected packages.

  • Public SQL tests passed for CHECK and foreign-key rejection, witness locking, Unicode comparison, automatic IDs, protocol v124 and two-CN execution. Plan, compiler and protobuf checks also passed.

  • git diff --check passed. Command receipts and retained logs are in the diagnostic directory.

Limitations:

  • The author reports successful mo-tester suites and benchmark runs, but their raw logs were not supplied; the exact mo-tester case was not independently repeated.

  • An earlier forced coordinator-drain comparison timed out on both the baseline and candidate, so it did not establish a regression. The normal two-CN test passed.

  • An initial public-test command failed before compilation because it used the wrong shell syntax and Go toolchain. The corrected isolated run passed.

This branch had an error being deployed

1 failed 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 kind/enhancement size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

2 participants