Skip to content

[Bug]: DELETE JOIN without WHERE deletes every row from irregular-index targets #28309

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

bcd5ed8

Other Environment Information

- MatrixOne was built from the commit above and started locally with etc/launch/launch.toml.
- MySQL comparison version: 8.0.45 for the FULLTEXT case.
- Local single-CN/TN/LOG launch; three-row target tables only.

Actual Behavior

A single-target DELETE ... JOIN whose target table has any non-regular index
deletes every target row when the join predicate is entirely in ON and there
is no WHERE clause.

DELETE d
FROM docs AS d
JOIN src AS s ON s.grp=d.grp;

With target rows in groups 10, 20, and 30 and source rows only in groups 10 and
30, MatrixOne removes all three target rows. MySQL 8.0.45 removes groups 10 and
30 and preserves group 20.

The zero-match boundary is more severe: an empty source table, a source whose
keys do not match any target, or an explicit ON 1=0 still causes MatrixOne to
delete all target rows. The same shapes on a regular-index target affect zero
rows. Therefore the statement can empty an indexed table even though its join
result contains no row at all.

The same whole-table deletion occurs with each tested MatrixOne index family:

Target index Rows that should be deleted Rows MatrixOne deletes
synchronous FULLTEXT 2 3
asynchronous FULLTEXT 2 3
FULLTEXT2 2 3
MASTER 2 3
HNSW 2 3
IVF-FLAT 2 3
FULLTEXT and HNSW on the same table 2 3

Both DELETE d FROM ... JOIN ... and DELETE FROM d USING ... JOIN ... have
the problem. Using a CTE or a derived table for the source does not change it.
Preparing the same zero-match statement with SQL PREPARE and then executing it
also deletes all target rows.

The relational shape itself is valid: the same statement on a target with only
primary/unique/secondary indexes preserves group 20. Adding an equivalent
WHERE TRUE to an indexed target also preserves group 20. A multi-target
DELETE d,s FROM ... JOIN ... remains on a different planner route and deletes
only the matched rows from both targets.

This is a data-loss issue because an index added to an existing table changes
the rows removed by otherwise identical SQL.

Expected Behavior

The ON predicate must determine the rows visible to the delete target whether
or not the target carries a FULLTEXT or vector index. Only target rows that
participate in the join should be deleted.

Steps to Reproduce

CREATE DATABASE irregular_index_join_delete;
USE irregular_index_join_delete;

SET experimental_fulltext_index=1;

CREATE TABLE docs(
  id BIGINT PRIMARY KEY,
  grp INT,
  body TEXT,
  FULLTEXT ft_body(body)
);
CREATE TABLE src(id INT PRIMARY KEY, grp INT);

INSERT INTO docs VALUES
  (1,10,'alpha one'),
  (2,20,'beta two'),
  (3,30,'gamma three');
-- Deliberately does not match any target row.
INSERT INTO src VALUES (99,99);

DELETE d
FROM docs AS d
JOIN src AS s ON s.grp=d.grp;

SELECT id,grp FROM docs ORDER BY id;
-- Expected/MySQL 8.0.45: (1,10), (2,20), (3,30)
-- MatrixOne: empty result

The same boundary can be seen without changing data permanently:

BEGIN;
DELETE d FROM docs AS d JOIN src AS s ON s.grp=d.grp WHERE TRUE;
SELECT id,grp FROM docs ORDER BY id;
-- MatrixOne correctly preserves all three rows when WHERE is present.
ROLLBACK;

Additional information

This is a remaining irregular-index variant of #25098, which fixed the same
whole-table deletion for ordinary target tables.

The current modern delete binder intentionally avoids the truncate rewrite for
all JOIN deletes at pkg/sql/plan/bind_delete.go:87. Later, however, it returns
ErrUnsupportedDML for every non-regular target index at
pkg/sql/plan/bind_delete.go:223-227. bindAndOptimizeDeleteQuery routes that
error to the legacy delete planner at pkg/sql/plan/build.go:403-415. The legacy
path is therefore still reached by FULLTEXT and vector-index targets and loses
the ON-only join restriction that #25098 removed from the ordinary path.

The statements were executed inside transactions and rolled back after each
observation; rollback restored all base-table rows. The issue is the selected
delete set, not transaction rollback.

Activity

  1. added
    kind/bug
    on Sep 7, 2026
  2. added theissue type on Sep 7, 2026
  3. added
    severity/s0
    on Sep 7, 2026
  4. self-assigned this
    on Sep 7, 2026
  5. Ariznawlll commented on Sep 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充一个会扩大影响面的复现维度:问题不只存在于文本 SQL;真实 MySQL 二进制协议(COM_STMT_PREPARE / COM_STMT_EXECUTE)在跨连接 DDL 后也会进入相同的错误删除路径。

    环境:main / bcd5ed833b5400fe95f322e4028adb2bde3de0a4,本地单 CN。连接 A 先 prepare 下列无 WHERE 的单目标 DELETE JOIN;连接 B 随后给目标表创建索引;再由连接 A 复用同一个 statement 执行。每个目标表均有三行,源表只有 grp=10,因此正确结果应只删除 id=1。

    DELETE d
    FROM docs d JOIN selector s ON s.grp = d.grp;

    连续 3 次得到完全一致的结果:

    prepare 后新增的索引 实际受影响行数 实际剩余行
    普通二级索引 1 id=2,3
    FULLTEXT 3 空表
    HNSW 3 空表
    IVF-FLAT 3 空表

    对照组表明这不是 binary protocol 参数绑定或 statement 复用本身的问题:同样在 prepare 后新增 FULLTEXT、同一个 statement 只要加 WHERE ? 并绑定 TRUE,就只删除匹配的 id=1,id=2,3 保留。FULLTEXT 查询和 HNSW/IVF-FLAT Top-K 查询也与被错误清空后的基表一致,没有出现“基表未删、索引未同步”的第二个问题。

    因此,跨连接 DDL 已使该 prepared statement 看到新索引元数据,但非规则索引在这个路径上仍将无 WHERE 的 DELETE JOIN 降级为丢失 JOIN 条件的删除。这是本 issue 既有问题在 prepared plan cache / 运行期加索引场景下的复现,不单独创建重复 issue。

  6. XuPeng-SH commented on Sep 7, 2026

    @XuPeng-SH
    Contributor

    Implemented and pushed as PR #28310.

    Root cause was the legacy irregular-index DELETE planner treating any single-target statement without WHERE/LIMIT as an unrestricted delete, while ignoring JOIN/USING table references. The fix centralizes the unrestricted-target predicate and applies it to base-table and all index-maintenance consumers. It includes planner UT, race validation, and distributed regression coverage.

    55 Linux/amd64 validation also covered FULLTEXT, MASTER, HNSW, IVF-FLAT, FULLTEXT2, mixed indexes, empty/zero/duplicate matches, USING, prepared reuse, rollback, and index consistency.

    Adjacent independent findings from the same counterexample audit are tracked in #28311 and #28312.

  7. added a commit that references this issue on Sep 7, 2026
    3d67664
  8. Ariznawlll commented on Sep 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:bcd5ed833b5400fe95f322e4028adb2bde3de0a4
    • 部署方式:本地单 CN/TN/LogService,使用 etc/launch/launch.toml
    • 测试日期:2026-09-07

    原问题验证

    创建带 FULLTEXT 非规则索引的 docs 表和仅匹配部分分组的 src 表,执行原始语句 DELETE d FROM docs AS d JOIN src AS s ON s.grp=d.grp。3 轮均应只删除 2 行,实际删除 3 行并清空目标表。将 src 置为空或使用完全不匹配的键时,3 轮仍删除全部目标行。普通二级索引对照组只删除匹配行。

    Happy Path 覆盖

    1. 普通二级索引目标表执行相同 JOIN DELETE,3/3 轮只删除匹配行,未匹配行保留。
    2. FULLTEXT 目标表执行匹配部分分组的 JOIN DELETE,3/3 轮均出现全表删除。

    Unhappy Path 覆盖

    1. src 为空:应影响 0 行,实际全表删除。
    2. src 键全部不匹配:应影响 0 行,实际全表删除。
    3. JOIN 条件完全位于 ON 且无 WHERE:复现同一错误删除行为。

    白盒验证

    不适用。本轮通过公开 SQL 接口复现,未使用内部故障注入。

    回归测试

    修复 PR #28310 已合并;本轮在其合并前的目标 SHA 上复现,未将失败行为加入 MOTR 基线。

    测试证据

    测试结论

    测试不通过。

  9. Ariznawlll commented on Sep 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    更正此前测试结论:上一条评论使用的目标版本是 bcd5ed833b5400fe95f322e4028adb2bde3de0a4,该版本早于修复 PR #28310 合并,确实复现了问题,但不能代表当前 main 的结果。

    由于 PR #28310 已合并到 main,当前 issue 应重新在最新 main 上完整复测后再作结论。本条撤回“测试不通过并转回研发”的处理,issue 保持开放,暂不作关闭或研发转派结论。

  10. Ariznawlll commented on Sep 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:e7cadcf03150e9ca7a9bb3eeb789d0735295329a
    • 部署方式:单机 mo-service(LOG/TN/CN,独立数据目录,监听 6001)
    • 修复提交:PR fix(dml): preserve join scope for indexed deletes #28310,merge 3d67664696e48cec0efc84308fb7de1cfda2f7db
    • 测试日期:2026-09-07

    原问题验证

    1. 开启 experimental_fulltext_index,创建 docs(id, grp, body) 及 FULLTEXT ft_body(body),插入分组 10、20、30 三行。
    2. 创建 src(grp),插入 10、30,执行 DELETE d FROM docs d JOIN src s ON s.grp=d.grp。
    3. 查询结果从 3 (10,20,30) 变为 1 (20),仅删除 JOIN 命中的两行。
    4. 清空 src 后插入不存在的分组 99,再执行同一 DELETE,结果保持 1 (20),未误删剩余行。
    5. 完整场景连续执行 3 次,3/3 结果一致。

    Happy Path 覆盖

    1. 部分匹配 JOIN DELETE 只删除匹配行,剩余分组为 20。
    2. 零匹配 JOIN DELETE 为 no-op,剩余行和数据内容不变,3/3 通过。

    Unhappy Path 覆盖

    零匹配源表是该问题的边界场景;执行后仍保留 1 行 (grp=20),未出现全表删除。

    白盒验证

    未修改源码,未新增 UT;通过单机服务级 SQL 回归验证修复行为。

    回归测试

    • MOTR PR:#147
    • 新增文件:suites/14_issue_regression/t/issue_28309_fulltext_join_delete.test
    • MOTR --repeat 3:3/3 通过,48/48 statements 通过。

    测试证据

    • main commit:e7cadcf03150e9ca7a9bb3eeb789d0735295329a
    • 修复 PR:#28310
    • 单机 3 轮结果:after_partial=1,20;after_zero=1,20

    测试结论

    测试通过。

  11. added 3 commits that reference this issue on Sep 17, 2026
    3814492
    6191c1c
    b8e4b64
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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions