Skip to content

[Bug]: fulltext index: MATCH() AGAINST() function cannot be replaced by FULLTEXT INDEX and full table scan with fulltext search is not supported yet. #20687

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

9c6ab9e

Other Environment Information

- Hardware parameters:
- OS type:
- Others:

Actual Behavior

场景1:
image

场景2:
image

Expected Behavior

No response

Steps to Reproduce

场景一:
drop table if exists articles;
drop table if exists authors;
create table articles (
    id int auto_increment primary key,
    title varchar(255),
    content text,
    author_id int,
    fulltext(content)
);
create table authors (
    id int auto_increment primary key,
    name varchar(100)
);
insert into authors (name) values ('John Doe'), ('Jane Smith'), ('Alice Johnson');
insert into articles (title, content, author_id) values
('MO全文索引入门', 'MO全文索引是一种强大的工具,可以帮助你快速检索数据库中的文本数据。', 1),
('深入理解全文索引', '全文索引不仅可以提高搜索效率,还可以通过JOIN操作与其他表结合使用。', 2),
('MO性能优化', '本文将探讨如何优化MO数据库的性能,包括索引优化和查询优化。', 3),
('全文索引与JOIN操作', '全文索引可以与JOIN操作结合使用,以实现跨表的全文搜索。', 1);
select * from articles;
select * from authors;
select a.title, a.content, au.name
from articles a
join authors au on a.author_id = au.id
where match(a.content) against ('MO' IN NATURAL LANGUAGE MODE);

场景2:
drop table if exists posts;
drop table if exists comments;
create table posts (
    post_id int auto_increment primary key,
    title varchar(255),
    content text
);
create table comments (
    comment_id int auto_increment primary key,
    post_id int,
    comment_text text,
    foreign key (post_id) references posts(post_id)
);
alter table posts add fulltext(content);
insert into posts (title, content) values
('MO全文索引入门', 'MO全文索引是一种强大的工具,可以帮助你快速检索数据库中的文本数据。'),
('深入理解全文索引', '全文索引不仅可以提高搜索效率,还可以通过JOIN操作与其他表结合使用。');
insert into comments (post_id, comment_text) values
(1, '这篇文章很有用,谢谢分享!'),
(1, '我也在学习全文索引,很有帮助。'),
(2, '全文索引真的很强大,学习了。');
select count(posts.title), count(comments.comment_id) as comment_count
from posts
left join comments on posts.post_id = comments.post_id
where match(posts.content) against ('全文索引' IN NATURAL LANGUAGE MODE)
group by posts.post_id;

Additional information

No response

Activity

  1. added
    kind/bug
    severity/s0
    on Dec 10, 2024
  2. added this to the 2.1.0 milestone on Dec 10, 2024
  3. added theissue type on Apr 27, 2025
  4. modified the milestones: 2.1.0, 2506 on Apr 27, 2025
  5. modified the milestones: MO-2506, MO-2507 on Jul 17, 2025
  6. modified the milestones: MO-2507, MO-2508 on Aug 11, 2025
  7. modified the milestones: MO-2508, MO-202602 on Feb 3, 2026
  8. 1 remaining item

  9. Ariznawlll commented on Sep 18, 2026

    @Ariznawlll
    CollaboratorAuthor

    在固定 main commit 01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90 上补充多 MATCH 布尔组合范围。

    同一张表分别在 title、body 建立两个全文索引后:

    -- 可执行,返回两个命中集合的交集
    WHERE MATCH(title) AGAINST('+alpha' IN BOOLEAN MODE)
      AND MATCH(body) AGAINST('+beta' IN BOOLEAN MODE)
    
    -- 20105
    WHERE MATCH(title) AGAINST('+alpha' IN BOOLEAN MODE)
       OR MATCH(body) AGAINST('+beta' IN BOOLEAN MODE)
    
    -- 20105
    WHERE MATCH(title) AGAINST('+alpha' IN BOOLEAN MODE)
      AND NOT MATCH(body) AGAINST('+beta' IN BOOLEAN MODE)

    NOT (MATCH(...) OR MATCH(...)) 以及 ((MATCH(...) AND id<... ) OR (MATCH(...) AND id>...)) 也返回相同 20105。classic FULLTEXT 与 FULLTEXT2、两个 CN 的行为一致;而 AND 形态会生成两个全文检索流并返回正确交集。

    在同表同时存在 IVFFLAT、查询按向量距离分页时,AND 形态可同时生成两个 fulltext2_search 和一个 Vector Index Scan,结果与精确路径一致。OR/NOT 的 20105 在移除向量排序后仍存在,因此根因是全文谓词布尔改写不覆盖析取/否定,不是向量候选合并。

  10. Ariznawlll commented on Sep 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    @cpegeric 当前验证仍存在未修复路径,转回研发继续处理。

    测试版本

    • 分支:main
    • commit:3727c5c5436ca136d7b0312f75bc223a11692a85
    • 部署方式:本地单机,1 Log / 1 TN / 1 CN
    • 测试日期:2026-09-22

    原问题验证

    关联修复 #29069 合入后,OUTER JOIN 与窗口算子上的 MATCH 路径已经改善;但原 issue 下补充的多 MATCH 布尔组合仍未闭环。两个全文索引分别建在 title、body 后,MATCH(title) AND MATCH(body) 可执行,而 OR/NOT 组合仍稳定返回 ERROR 20105。

    Happy Path 覆盖

    1. 单个 MATCH ... AGAINST 可正确使用全文索引并返回命中行。
    2. 两个 MATCH 使用 AND 连接时,可生成两个全文检索流并返回正确交集。
    3. 等价 INNER JOIN、相关 EXISTS、CTE、聚合及 prepared statement 路径可正常执行。

    Unhappy Path 覆盖

    1. MATCH(title) ... OR MATCH(body) ... 返回 ERROR 20105。
    2. MATCH(title) ... AND NOT MATCH(body) ... 返回 ERROR 20105。
    3. NOT (MATCH(...) OR MATCH(...)) 返回 ERROR 20105。
    4. MATCH 与普通谓词分别位于 OR 两侧时返回 ERROR 20105。
    5. classic FULLTEXT 与 FULLTEXT2 行为一致;移除向量排序后仍复现,已排除向量候选合并影响。

    白盒验证

    本轮未增加内部注入。失败由公开 SQL 稳定触发,范围集中在全文谓词对析取和否定表达式的 planner 改写,使用黑盒计划与结果即可直接判定。

    回归测试

    当前产品行为仍失败,未把错误结果固化为 MOTR baseline。修复后回归需要覆盖 OR、AND NOT、外层 NOT,以及 classic FULLTEXT/FULLTEXT2 两条路径。

    测试证据

    测试结论

    测试不通过。

  11. changed the issue type fromtoon Sep 23, 2026
  12. cpegeric commented on Sep 24, 2026

    @cpegeric
    Contributor

    Verdict: the 20105 rejection is correct and intentional (WAI), not an unfixed bug

    The behavior is a documented, deliberate design decision, not a path someone forgot to close. Two spots in
    pkg/sql/plan/apply_indices_fulltext.go state it explicitly:

    collectDrivingFullTextMatches (line 2217) only descends through and and membership-implying comparisons (>c, >=c). It returns
    unchanged for or/not, so a MATCH nested in OR/NOT is never harvested → the raw fulltext_match survives to execution → the
    fulltext.go:1189 guard fires 20105. The code comments give the reason:

    • line 2290: "AND-reachable only. Under an OR or a NOT the predicate does not have to hold for a returned row, so the engine
      must not drop anything on its account."
    • line 2484: "The stream a driver builds is INNER JOINed to the base scan, which ANDs 'this document is in the index result'
      onto the query; harvesting a MATCH out of an OR or a NOT would impose a restriction the query never asked for."

    Why it's architecturally correct, not a gap

    1. MO has no full-table-scan fulltext fallback — by design. The 20105 message itself says so. Every MATCH must be driven by the
      index.
    2. The driver is an INNER JOIN of the index-result stream onto the base scan → inherently conjunctive. That's exactly why MATCH
      AND MATCH works (two inner joins), and why the tester confirms AND is fine.
    3. OR would need a UNION-of-streams rewrite; NOT / AND-NOT would need anti-join semantics against the entire base (the index
      enumerates matches, not non-matches). Neither exists. Forcing a MATCH from inside OR/NOT into the join model would silently
      change semantics (return wrong rows). Returning 20105 is the safe choice over returning wrong results.

    Recommendation

    Reject the OR/NOT complaint as WAI — the original issue (single MATCH over JOIN, scenario 1/2) was genuinely fixed by #29069;
    what remains is the intentional refusal. Distinguish two sub-cases when replying to the tester:

    • NOT / NOT(OR) / top-level negation: architecturally un-driveable (index yields matches, not non-matches, and there's no scan
      fallback). This is a permanent limitation, not a bug.
    • OR-of-MATCHes (and MATCH OR predicate): semantically well-defined and MySQL-supports it, but it needs a new union-based
      driver rewrite MO doesn't have, plus resolves undefined score semantics on the non-match branch. That's a feature request, not
      a defect in the current path — track it separately rather than reopening this bug.

    Net: the tester's "测试不通过" observation is factually right that OR/NOT return 20105, but that 20105 is the designed, safe
    behavior. I'd close the bug as WAI and, if OR support is wanted, file it as a scoped feature (union-driver) rather than keep it
    as an S0 bug on the existing rewrite.

  13. Ariznawlll commented on Oct 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:529ee099e32ed8c38d2806043bf1516ab0345e45(复测后确认仍是远端 main HEAD)
    • 部署方式:本地单 CN,以及 1 Log / 1 TN / 2 CN 拓扑
    • 测试日期:2026-10-03

    原问题验证

    按 issue 中的建表和数据,分别复测两个原始场景,各连续执行 3 轮:

    1. articles 与 authors 的 INNER JOIN,在 WHERE 中用 MATCH(a.content) AGAINST('MO' IN NATURAL LANGUAGE MODE) 过滤:3/3 返回两条预期文章(MO全文索引入门 / John Doe、MO性能优化 / Alice Johnson),EXPLAIN 包含 fulltext_index_scan。
    2. posts 与 comments 的 LEFT JOIN,以 MATCH(posts.content) AGAINST('全文索引' IN NATURAL LANGUAGE MODE) 过滤并按 post 分组:3/3 返回 (post_id, comment_count)=(1,2),(2,1);另加一条匹配全文条件、但没有评论的 post,得到 (post_id, comment_count)=(新增ID,0),确认保留侧和 NULL 扩展语义。计划同样包含 fulltext_index_scan。

    上述结果在单 CN 和双 CN 环境一致。原始的单 MATCH / JOIN 故障已由 #29069 修复,本轮未再复现。

    已支持路径覆盖

    • 裸 MATCH ... AGAINST 过滤与 JOIN/聚合组合返回预期行集;LEFT JOIN 的无匹配评论行未丢失。

    设计限制路径覆盖

    • WHERE MATCH(content) AGAINST('MO' IN NATURAL LANGUAGE MODE) OR author_id=2 返回 ERROR 20105。
    • WHERE NOT MATCH(content) AGAINST('MO' IN NATURAL LANGUAGE MODE) 返回 ERROR 20105。
    • 这与研发说明一致:当前全文索引结果以 INNER JOIN 驱动,不能安全地把 OR/NOT 中的 MATCH 强制作为合取过滤;目前没有 OR 的并集驱动、NOT 的反连接或全文全表扫描回退。20105 是有意保留的安全拒绝,不是原 JOIN 缺陷未修复。此前对 classic FULLTEXT / FULLTEXT2 的多 MATCH OR/NOT 在双 CN 上也记录了相同结果。

    白盒验证

    本轮未新增内部注入或 -race 测试;原始问题和当前限制均可由公开 SQL、实际行集及执行计划直接判断。研发已在上述评论中指出限制对应的 planner 收集与执行 guard 代码路径。

    回归测试

    原 JOIN 路径的产品修复见 #29069。OR/NOT 是现有设计限制,不将 ERROR 20105 写成新的 MOTR 正确性预期;如需支持 OR,应另立 Feature 定义并集驱动和非命中分支分数语义。

    测试证据

    测试结论

    测试通过。

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