Skip to content

[Bug]: classic fulltext BOOLEAN trailing * on a 4+ rune CJK stem does prefix_eq of the whole stem and misses the indexed trigrams #29273

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 classic FULLTEXT index, boolean mode with a trailing * on a CJK stem of 4 or more runes returns no rows for a document that contains that stem. The statement succeeds.

CreatePattern (pkg/fulltext/fulltext.go 637–642) turns a token whose last rune is * into STAR and keeps the whole token, including the *, as Text. SqlBoolean only dispatches to GenSql (pkg/fulltext/sql.go 379). The non-TEXT branch (sql.go 327–332, and the JOIN form at 359–366) strips one trailing * and emits prefix_eq of whatever remains. It does not call GenTextSql / ParsePatternInNLMode. The same strip-and-prefix_eq is in GenJoinPlusSql (216–223) and SqlPhrase (477–489, 517–522).

Probed with NewSearchAccum + PatternToSql for parsers "", "ngram", "default", and "gojieba":

苹果香蕉*   -> prefix_eq(word,'苹果香蕉')
+苹果香蕉*  -> prefix_eq(word,'苹果香蕉')
苹果香蕉西瓜* -> prefix_eq(word,'苹果香蕉西瓜')

SimpleTokenizer.outputCJK (pkg/monlp/tokenizer/simple.go 179–206) stores a sliding 3-rune window plus the shorter tail. "苹果香蕉" is stored as 苹果香, 果香蕉, 香蕉, 蕉. None of those words has the prefix 苹果香蕉, so the scan is empty. "苹果香蕉西瓜" is stored as 苹果香, 果香蕉, 香蕉西, 蕉西瓜, 西瓜, 瓜, which also does not match prefix_eq(word,'苹果香蕉西瓜').

The same operand without * already hits. +苹果香蕉 goes through GenTextSql (sql.go 168–188) and, for the ngram parser, becomes the positional pair word = '苹果香' at anchor 0 with word = '果香蕉' at anchor+3. For gojieba it becomes word = '苹果' with word = '香蕉'. Adding * drops that expansion and looks up a string longer than any stored token.

A stem of at most 3 runes can still hit, because the prefix is itself a stored token or a prefix of one: 苹果* is prefix_eq(word,'苹果') and 苹果香* is prefix_eq(word,'苹果香'). TestSqlBoolean (pkg/fulltext/sql_test.go 132–154) already locks the English whole-token form (Origin* → prefix_eq(word,'origin')) and the unstarred 4-rune CJK form (+读写汉字 → the two trigrams). It does not cover a 4-rune CJK star.

This SQL is what the planner precompiles (pkg/sql/plan/fulltext.go getFullTextIndexScanSql 153–161) and what fulltext_index_scan runs (pkg/sql/colexec/table_function/fulltext.go runWordStats 475–476).

Expected Behavior

A trailing * should not make a stem that already matches disappear. 苹果香蕉* / +苹果香蕉* should return the row whose body is 苹果香蕉 or 苹果香蕉西瓜, the same rows +苹果香蕉 returns, and should also return a longer body that continues past that stem. The ngram/default path should expand the stem the way GenTextSql does, and only the part that is actually a prefix of a stored token should be prefix_eq. gojieba should segment first and then apply * to the last segment, instead of prefix_eq of the raw 4-rune string.

Steps to Reproduce

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

SELECT id FROM ft WHERE MATCH(body) AGAINST('苹果香蕉*' IN BOOLEAN MODE);
-- success, 0 rows. Rows 1 and 2 contain the stem.

SELECT id FROM ft WHERE MATCH(body) AGAINST('+苹果香蕉*' IN BOOLEAN MODE);
-- success, 0 rows.

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

SELECT id FROM ft WHERE MATCH(body) AGAINST('苹果*' IN BOOLEAN MODE);
-- id 1, 2, and 3. A 2-rune prefix still hits the stored trigram 苹果香.

The same prefix_eq(word,'苹果香蕉') SQL is produced for parser "", "default", and "gojieba". On gojieba, +苹果香蕉 without * is word = '苹果' AND word = '香蕉'; with * it is still prefix_eq(word,'苹果香蕉').

Additional information

MySQL's ngram parser documents that boolean * is not a word-prefix operator. This is not a request to copy that. MatrixOne already implements * as prefix_eq against stored tokens (+提效 in TestSqlBoolean, English Origin*). For a stem longer than the stored token, that predicate cannot hit the document that contains the stem, while the same operand without * can. #29271 is the quoted form (word = '苹果香蕉'); fixing the phrase branch does not fix STAR.

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 Sep 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    Same STAR path also drops a Latin word longer than 23 bytes.

    SimpleTokenizer.outputLatin (pkg/monlp/tokenizer/simple.go 144–177) stores one token, the first 23 bytes, lowercased. A 26-byte run of b is indexed as bbbbbbbbbbbbbbbbbbbbbbb (23). gojieba truncates to the same MAX_TOKEN_SIZE (pkg/monlp/tokenizer/jieba.go 174).

    NewSearchAccum + PatternToSql on the four classic parsers ("", ngram, default, gojieba):

    +bbbbbbbbbbbbbbbbbbbbbbbbbb
      -> word = 'bbbbbbbbbbbbbbbbbbbbbbb'          -- 23 bytes, hits the indexed token
    
    +bbbbbbbbbbbbbbbbbbbbbbbbbb*
      -> prefix_eq(word,'bbbbbbbbbbbbbbbbbbbbbbbbbb')  -- 26 bytes
    
    bbbbbbbbbbbbbbbbbbbbbbbbbb*
      -> prefix_eq(word,'bbbbbbbbbbbbbbbbbbbbbbbbbb')
    
    +cccccccccccccccccccccccc    -- 24 c's
      -> word = 'ccccccccccccccccccccccc'          -- 23 bytes
    
    +cccccccccccccccccccccccc*
      -> prefix_eq(word,'cccccccccccccccccccccccc')    -- 24 bytes
    

    prefix_eq is bytes.HasPrefix (pkg/sql/plan/function/func_prefix.go). A stored 23-byte token cannot have a 24- or 26-byte prefix, so the scan succeeds and returns no rows for a document that contains the word. A stem of exactly 23 bytes still hits: + + 23 b + * is prefix_eq(word,'bbbbbbbbbbbbbbbbbbbbbbb').

    TestSqlBoolean locks Origin* (shorter than 23). It does not lock this cut.

    A fix that only ngram-expands a 4+ rune CJK stem still leaves this Latin case on the raw-stem prefix_eq. The expected behavior already says only the part that is actually a prefix of a stored token should be prefix_eq. That has to include the 23-byte Latin cut, on all four parsers, not only the CJK trigram split.

  6. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

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

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。classic BOOLEAN 的 4+ rune CJK stem 加 * 返回 ids 6,7,使用索引后的 trigram/prefix 语义正确。

    Happy Path 覆盖

    覆盖 CJK 长 stem、前缀匹配和 direct control。

    Unhappy Path 覆盖

    覆盖不存在前缀、超长 Latin token、空 *;不匹配全表。

    白盒验证

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

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:classic BOOLEAN 的 4+ rune CJK stem 加 * 返回 ids 6,7,使用索引后的 trigram/prefix 语义正确。
    • 回归代码与 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