Skip to content

[Bug]: fulltext MATCH score remains unrewritten inside aggregate expressions #28681

Closed

Description

@Ariznawlll

问题

全文索引能够为 WHERE MATCH(...) AGAINST(...) 建立 fulltext_index_scan,但同一个 MATCH 分数一旦直接出现在聚合表达式中,优化器没有把它替换成索引扫描产生的 score 列。以下查询因此在执行期报错 20105:

select max(match(body) against('hello'))
from docs where match(body) against('hello');

select group_concat(match(body) against('hello') order by id separator '|')
from docs where match(body) against('hello');

select group_concat(id order by match(body) against('hello') desc, id separator '|')
from docs where match(body) against('hello');

错误为:

ERROR 20105 (HY000): MATCH() AGAINST() function cannot be replaced by FULLTEXT INDEX and full table scan with fulltext search is not supported yet

这不是索引不可用:相同过滤条件下直接投影 score 正常,GROUP_CONCAT(id ...) 正常,EXPLAIN 也显示 fulltext_index_scan 已建立。将 score 先放入 CTE,再由外层聚合,同样可以得到正确结果。

环境

  • MatrixOne: official main 065a67528608c475d0219e0be6b7d454a58f7b1a
  • 拓扑:本地 2 CN / 1 TN / 1 LogService
  • experimental_fulltext_index=1
  • 复现次数:上述三种聚合形态分别 3/3

最小复现

set experimental_fulltext_index = 1;
drop database if exists ft_agg_repro;
create database ft_agg_repro;
use ft_agg_repro;

create table docs(id int primary key, body text);
insert into docs values
  (1, 'hello world'),
  (2, 'foo bar'),
  (3, 'hello hello again'),
  (4, 'world only'),
  (5, 'hello matrix one'),
  (6, null);
create fulltext index ft_body on docs(body);

select max(match(body) against('hello'))
from docs where match(body) against('hello');

对照

select group_concat(id order by id separator '|')
from docs where match(body) against('hello');
-- 1|3|5

with hits as (
  select id, match(body) against('hello') score
  from docs where match(body) against('hello')
)
select max(score), group_concat(id order by score desc, id separator '|')
from hits;
-- 0.09843373    3|1|5

MySQL 8.3 对三种直接聚合写法均可执行;按 score 排序的结果同为 3|1|5。

代码定位

applyIndicesForAggUsingFullTextIndex 调用 applyJoinFullTextIndices 后只把聚合节点的 child 改为新 join,未使用返回的 served 映射遍历和改写 aggNode.AggList。因此计划中仍然保留:

Aggregate Functions: max(fulltext_match('hello', 0, docs.body))

现有 TestFullTextGroupedAggregateWithOrderByUsesIndex 只验证 WHERE MATCH 上方的普通 COUNT(*),没有覆盖 MATCH 本身作为聚合参数或聚合内部排序键。

Activity

  1. added this to the MO-202609 milestone on Sep 11, 2026
  2. added
    kind/bug
    on Sep 11, 2026
  3. added theissue type on Sep 11, 2026
  4. Ariznawlll commented on Sep 11, 2026

    @Ariznawlll
    CollaboratorAuthor

    在更新后的 official main ee0c2bf563bf27ff2c80891bb57a1de5886bf90b(本地 2 CN / 1 TN / 1 LogService)复核:

    • MAX(MATCH(...)):3/3 返回 20105;
    • GROUP_CONCAT(MATCH(...) ORDER BY id):3/3 返回 20105;
    • GROUP_CONCAT(id ORDER BY MATCH(...) DESC):3/3 返回 20105;
    • 同一个 score 先在 CTE 中投影再由外层聚合,可以返回 0.09843373 和 3|1|5;
    • 两个 CN 上的全文索引过滤与投影 control 均可执行。

    EXPLAIN 仍把 fulltext_match(...) 留在 Aggregate Functions 内,而索引扫描已经出现在聚合子树中,说明问题仍是聚合表达式列表未使用 score 映射,不是索引未就绪。

  5. added
    severity/s0
    and removed on Sep 11, 2026
  6. cpegeric commented on Sep 14, 2026

    @cpegeric
    Contributor

    please verify

  7. Ariznawlll commented on Sep 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • MatrixOne main:0c3a04f390adaf6281fd592a49778ea5b1155e67
    • 本地 standalone(1 CN / 1 TN / 1 LogService),2026-09-15。

    原问题验证

    验证 MATCH 位于聚合参数和 ORDER BY 时均被正确改写为 score,不再残留未改写表达式。

    Happy Path 覆盖

    覆盖 FULLTEXT 表、聚合内 MATCH、分组与按 score 排序。

    Unhappy Path 覆盖

    覆盖聚合表达式嵌套与多文档结果,确认不会报 planner/执行错误。

    白盒验证

    TestFullTextAggMatchRewrittenToScore 在 pkg/sql/plan 连续 3 次通过。

    回归测试

    已加入 MOTR:issue_28681_fulltext_aggregate_match,见 motr#176。

    测试证据

    定向 plan UT 3/3 通过;新增 MOTR 用例连续 3 轮通过。

    测试结论

    测试通过。

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