Skip to content

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

Description

@XuPeng-SH

Is there an existing issue for the same feature request?

  • I have checked the existing issues.

Summary

INSERT ON DUPLICATE KEY UPDATE (ODKU) in MatrixOne has multiple execution paths with semantic inconsistencies and performance concerns.

Current Architecture

Modern Path

  • Entry: pkg/sql/plan/build.go:74-79 → bindInsert() in pkg/sql/plan/bind_insert.go
  • Features: PK/UK dedup, LOCK_OP, MULTI_UPDATE

Legacy Path

  • Entry: buildInsert() → pkg/sql/plan/build_constraint_util.go + pkg/sql/colexec/onduplicatekey/on_duplicate_key.go

Rewrite-to-Replace Path

  • pkg/sql/plan/build_insert.go:702-745 rewrites some ODKU to REPLACE
  • pkg/sql/compile/scope.go:888-915 executes as DELETE + INSERT (non-atomic)

Critical Concerns

1. Semantic Fragmentation

Different statement patterns may fall into different execution paths, leading to inconsistent behavior and performance.

2. Non-Atomic Rewrite

ODKU rewritten to REPLACE is not a single operator but two SQL statements, introducing:

  • Extra execution overhead
  • Additional write window vulnerability
  • No transactional atomicity

3. Legacy Path Risks

build_constraint_util.go uses a large LEFT JOIN with multiple unique key conditions OR'd together. For tables with multiple unique keys, it adds AGG + any_value, which is prone to semantic drift.

Missing Coverage

  • NULL value handling in ODKU
  • Multi-unique-key scenarios
  • Rewrite-to-REPLACE edge cases
  • Concurrent modification during rewrite window

Proposed Improvements

Short-term

  • Audit and document all ODKU execution paths
  • Add test coverage for edge cases (NULL, multi-UK, partitions)

Medium-term

  • Unify legacy and modern paths under operator-based execution
  • Eliminate or properly handle rewrite-to-REPLACE path

Long-term

  • Consider pushing conflict detection to storage/txn layer
  • Ensure atomic semantics for all ODKU variants

Related Code Locations

  • Modern path: pkg/sql/plan/bind_insert.go
  • Legacy path: pkg/sql/plan/build_constraint_util.go, pkg/sql/colexec/onduplicatekey/on_duplicate_key.go
  • Rewrite logic: pkg/sql/plan/build_insert.go:702-745
  • Execution: pkg/sql/compile/scope.go:888-915

What type of PR is this?

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

Activity

  1. added this to the MO-202603 milestone on Mar 24, 2026
  2. added
    severity/s0
    on Mar 24, 2026
  3. added theissue type on Mar 24, 2026
  4. Ariznawlll commented on Apr 3, 2026

    @Ariznawlll
    Collaborator

    main commit:ce192d954

    -- 建表:有主键 + 唯一键
    CREATE TABLE t1 (
    id INT PRIMARY KEY,
    uk_col INT UNIQUE,
    val VARCHAR(100)
    );

    INSERT INTO t1 VALUES (1, 100, 'original');

    -- 这条 ODKU 可能被重写为 REPLACE(即 DELETE + INSERT)
    -- 在并发场景下可能出现中间状态可见
    INSERT INTO t1 VALUES (1, 100, 'updated') ON DUPLICATE KEY UPDATE val = 'updated';

    CREATE TABLE t2 (
    id INT PRIMARY KEY,
    uk1 INT UNIQUE,
    uk2 INT UNIQUE,
    val VARCHAR(100)
    );

    INSERT INTO t2 VALUES (1, 10, 20, 'row1');
    INSERT INTO t2 VALUES (2, 30, 40, 'row2');

    -- 插入的行同时命中两个不同行的唯一键
    -- 不同路径可能产生不同结果
    INSERT INTO t2 VALUES (3, 10, 40, 'conflict_both')
    ON DUPLICATE KEY UPDATE val = VALUES(val);

    CREATE TABLE t3 (
    id INT PRIMARY KEY,
    uk_col INT UNIQUE,
    val VARCHAR(100)
    );

    INSERT INTO t3 VALUES (1, NULL, 'row1');

    -- NULL 在唯一键中不算重复(SQL 标准),验证 MO 是否正确处理
    INSERT INTO t3 VALUES (2, NULL, 'row2') ON DUPLICATE KEY UPDATE val = 'should_not_update';

    Image Image Image

    verified

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

Metadata

Metadata

Assignees

Labels

kind/featureseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions