You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature Request]: INSERT ON DUPLICATE KEY UPDATE - Unify Paths and Fix Semantic Issues #23947
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
Activity
fix: correct checkConflict to return row index instead of unique key …
Ariznawlll commentedon Apr 3, 2026
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';
verified
test: add BVT cases for issues matrixorigin#24020, matrixorigin#23947, …
test: add BVT cases for issues #24020, #23947, #23861 (#24110)
test: add BVT cases for issues matrixorigin#24020, matrixorigin#23947, …
test: add BVT cases for issues matrixorigin#24020, matrixorigin#23947, …
test: add BVT cases for issues matrixorigin#24020, matrixorigin#23947, …
test: add BVT cases for issues matrixorigin#24020, matrixorigin#23947, …