Skip to content

[Compatibility]: REPLACE scalar subqueries are rejected in SET and VALUES forms #28299

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

74668fc

Other Environment Information

- MatrixOne was built from the commit above and started locally with etc/launch/launch.toml.
- MySQL comparison version: 8.0.45.
- Every query was repeated three times with the same result.

Actual Behavior

MatrixOne rejects scalar subqueries in the value expressions of both
REPLACE ... SET and REPLACE ... VALUES:

REPLACE ... SET:    subquery in replace set value is not yet implemented
REPLACE ... VALUES: subquery in JOIN condition is not yet implemented

Preparing REPLACE ... SET with a scalar subquery is rejected by the same
replace set value binder guard before the prepared statement is created.

This restriction is specific to the value-binding paths. The following nearby
forms already work in MatrixOne and produce the same rows as MySQL:

  • REPLACE ... SET with literal expressions;
  • REPLACE ... SELECT;
  • REPLACE ... SELECT whose SELECT list contains a scalar subquery.

Expected Behavior

MatrixOne should accept scalar subqueries in REPLACE ... SET and
REPLACE ... VALUES, as MySQL 8.0.45 does. With the sample data below,
MAX(source_t.v) is 404, so both statements should replace row id=1 with
v=404. The prepared statement with @p=2 should use v=202.

Steps to Reproduce

CREATE DATABASE codex_replace_subquery_0907;
USE codex_replace_subquery_0907;

CREATE TABLE target_t (
  id INT PRIMARY KEY,
  v INT,
  note VARCHAR(40)
);
CREATE TABLE source_t (
  id INT PRIMARY KEY,
  v INT
);

INSERT INTO target_t VALUES
  (1,10,'one'),(2,20,'two'),(3,30,'three');
INSERT INTO source_t VALUES
  (1,101),(2,202),(4,404);

-- Control: supported.
REPLACE INTO target_t SET id=1, v=99, note='literal';

-- Scalar subquery in SET value.
REPLACE INTO target_t
SET id=1,
    v=(SELECT MAX(v) FROM source_t),
    note='set-subquery';

-- Scalar subquery in VALUES value.
REPLACE INTO target_t(id,v,note)
VALUES(1,(SELECT MAX(v) FROM source_t),'values-subquery');

-- Control: the SELECT form, including a nested scalar subquery, is supported.
REPLACE INTO target_t(id,v,note)
SELECT id,(SELECT MAX(v) FROM source_t),'nested-select'
FROM source_t
WHERE id=2;

-- The SET form is also rejected during PREPARE.
PREPARE ps_replace FROM
  'REPLACE INTO target_t
   SET id=1,
       v=(SELECT MAX(v) FROM source_t WHERE id<=?),
       note="prepared"';
SET @p=2;
EXECUTE ps_replace USING @p;

Additional information

The REPLACE ... SET rejection is explicit in
pkg/sql/plan/replace_value_binder.go. The VALUES form reaches a different
binder and reports an unrelated subquery in JOIN condition NYI message.

Activity

  1. added this to the MO-202609 milestone on Sep 7, 2026
  2. added
    kind/bug
    on Sep 7, 2026
  3. added theissue type on Sep 7, 2026
  4. added
    severity/s0
    and removed on Sep 7, 2026
  5. added a commit that references this issue on Sep 9, 2026
    a9d3b8b
  6. Ariznawlll commented on Sep 9, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:e8ae1e51f2b0f04b6d69d37ff6ccf8166736c9e6
    • 部署方式:本地单机 CN/TN/Log;MOTR 通过 MySQL 协议连接 127.0.0.1:6001
    • 测试日期:2026-09-09

    原问题验证

    在目标表和两行源表上执行 REPLACE INTO ... VALUES (...,(SELECT MAX(v) ...))、REPLACE ... SET 以及带参数的 PREPARE/EXECUTE。三轮均不再报“unsupported subquery”,结果分别写入 (1,20)、(2,10)、(3,20)。

    Happy Path 覆盖

    1. VALUES 中的标量子查询可执行并返回正确聚合值。
    2. SET 形式的标量子查询可执行。
    3. 预编译 REPLACE 复用参数与标量子查询可执行。
    4. 源码 issue 回归还覆盖了重复主键/唯一键的执行顺序和 1000 行混合 VALUES 路径,三轮通过。

    Unhappy Path 覆盖

    1. 多行子查询用于标量位置被正确拒绝,返回 ERROR 1242: Subquery returns more than 1 row。
    2. 拒绝后重新读取目标表仍只有前三条成功记录,行数为 3,确认无部分写入且连接可继续使用。
    3. 源码 issue 回归覆盖取消两个阻塞子查询后的清理,确认没有残留分支和部分写入。

    白盒验证

    go test ./pkg/tests/issues -run '^TestIssue28299ReplaceValuesOrder$' 连续 3 轮通过;其中使用独立故障点验证后到达 VALUES 分支先完成时仍保持 SQL 文本顺序。

    回归测试

    测试证据

    • MOTR PR:matrixorigin/motr#152,提交 156c477b1d8757b1de4f5784532b5ddd75d8744e。
    • 黑盒与源码 issue 回归均在上述 main commit 完成三轮验证。

    测试结论

    测试通过。

  7. added 3 commits that reference this issue on Sep 17, 2026
    df2edc1
    cb19b10
    2c0a8d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions