Skip to content

[Bug]: fulltext2 BOOLEAN trailing * keeps only the first CJK trigram and matches documents that do not contain the stem #29274

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

66da387

Other Environment Information

Actual Behavior

On a FULLTEXT2 index with parser "", "default", or "ngram", boolean mode 苹果香蕉* returns a document that does not contain 苹果香蕉. The statement succeeds.

Index.SearchQuery (pkg/fulltext2/query.go 379–384) and Index.StreamQuery (pkg/fulltext2/stream.go 245–249) both call buildBooleanQuery when boolean is true. The search TVF sets that flag from FULLTEXT_BOOLEAN (pkg/sql/colexec/table_function/fulltext2_search.go 747) and Fulltext2Search.runTopK calls SearchQuery (pkg/fulltext2/search_cache.go 342).

rawToClauseParser (pkg/fulltext2/query.go 637–678) sends a bare, non-quoted, non-star CJK operand through phraseSlots, so +苹果香蕉 is the positional pair 苹果香@0 + 果香蕉@3. The comment there says the old OR-of-ngrams path made +中文學習 match 中文學校. A trailing * is excluded by !rc.star and falls through to tokenizeToTerms + clausePrefix of terms[:1] only (677–678). tokenizeToTerms on 苹果香蕉 yields 苹果香, 果香蕉, 香蕉, 蕉. Only 苹果香 is kept.

clausePrefix (pkg/fulltext2/boolean.go 464–488) ORs every term from prefixTerms (pkg/fulltext2/termdict.go 114–133). The trigram 苹果香 is a prefix of itself, so every document that contains that trigram matches, including 苹果香瓜.

Probed with BuildSegmentFromDocs (SimpleTokenizer) and SearchQuery(..., boolean=true, BM25):

query clause doc ids returned
苹果香蕉* prefix 苹果香 1 苹果香蕉, 2 苹果香瓜, 3 苹果香蕉西瓜
+苹果香蕉* must prefix 苹果香 1, 2, 3
+苹果香蕉 phrase 苹果香@0, 果香蕉@3 1, 3
苹果* prefix 苹果 1, 2, 3, 4 红苹果甜
苹果香* prefix 苹果香 1, 2, 3

苹果* and 苹果香* match stored tokens whose prefix is the whole user stem. 苹果香蕉* shortens a 4-rune stem to its first 3-rune token, so doc 2 matches even though rune 4 is 瓜, not 蕉. The same three parsers all produce terms:["苹果香"]. Natural-language 苹果香蕉 (boolean=false) returns only docs 1 and 3.

SearchQuery's comment (368–371) says a word* prefix behaves as its operator. The operator is not the problem. Keeping only the first trigram drops the rest of the user's prefix.

Expected Behavior

苹果香蕉* / +苹果香蕉* should return docs 1 and 3 and should not return doc 2. The ngrams fully contained in the user prefix (苹果香 at byte 0 and 果香蕉 at byte 3) should still be required, the same slots phraseSlots already builds for the unstarred operand. Only a tail shorter than one stored token should be a prefix. A document that shares the first trigram and then diverges (苹果香瓜) should not match.

Steps to Reproduce

CREATE TABLE ft(id INT PRIMARY KEY, body VARCHAR(200));
INSERT INTO ft VALUES (1, '苹果香蕉'), (2, '苹果香瓜'), (3, '苹果香蕉西瓜'), (4, '红苹果甜');
CREATE FULLTEXT2 INDEX fi ON ft(body) WITH PARSER ngram;

SELECT id FROM ft WHERE MATCH(body) AGAINST('苹果香蕉*' IN BOOLEAN MODE);
-- returns 1, 2, and 3. Doc 2 does not contain 苹果香蕉.

SELECT id FROM ft WHERE MATCH(body) AGAINST('+苹果香蕉' IN BOOLEAN MODE);
-- returns 1 and 3.

The unit probe above is Index.SearchQuery on one segment built with SimpleTokenizer, which is the tokenizer normalizeParser selects for "" / "default" / "ngram". parser = "gojieba" was not part of this probe.

Additional information

Classic fulltext has the opposite failure for the same pattern: prefix_eq(word,'苹果香蕉') matches nothing, because no stored token is 4 runes long. That is a separate issue. Fixing one side does not fix the other. SearchBooleanText / ParseBoolean is not this path; production boolean search uses buildBooleanQuery.

Activity

  1. added this to the MO-202609 milestone on Sep 23, 2026
  2. added
    kind/bug
    on Sep 23, 2026
  3. added theissue type on Sep 23, 2026
  4. added
    severity/s0
    and removed on Sep 23, 2026
  5. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:本地全新单 CN mo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE
    • 测试日期:2026-10-02

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。FULLTEXT2 CJK trailing-star 查询返回 ids 1,3,并排除仅部分相似的文档。

    Happy Path 覆盖

    覆盖 CJK prefix、多个 parser 文档及排序。

    Unhappy Path 覆盖

    覆盖不存在前缀、空 term 和 NULL;无过宽命中。

    白盒验证

    关联修复 #29346 已进入当前 main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:FULLTEXT2 CJK trailing-star 查询返回 ids 1,3,并排除仅部分相似的文档。
    • 回归代码与 golden result 已提交到 MOTR #209;本地全量 14_issue_regression 中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。

    测试结论

    测试通过。

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