Skip to content

[Bug]: classic fulltext BOOLEAN phrase on the ngram parser looks up the raw CJK string and misses the indexed trigrams #29271

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 whose parser is "", "default", or "ngram", MATCH(col) AGAINST('"..."' IN BOOLEAN MODE) returns no rows for a CJK phrase the column contains. The statement succeeds.

The index stores SimpleTokenizer tokens (pkg/sql/colexec/table_function/fulltext_tokenize.go 138–195, pkg/monlp/tokenizer/simple.go outputCJK 179–206): sliding 3-rune windows plus the shorter tail of the run. "红苹果甜" is stored as 红苹果@0, 苹果甜@3, 果甜@6, 甜@9. "苹果香蕉" is stored as 苹果香@0, 果香蕉@3, 香蕉@6, 蕉@9. The whole phrase is never one word.

A quoted boolean pattern does not use that tokenizer. ParsePatternInBooleanMode (pkg/fulltext/fulltext.go 734–741) calls ParsePhrase. For every parser other than gojieba, ParsePhrase (662–689) only splits on spaces, so "苹果香蕉" becomes one TEXT child 苹果香蕉 and "苹果" becomes one TEXT child 苹果. patternToSql ( pkg/fulltext/sql.go 579–581) then sends a PHRASE node straight to SqlPhrase, which emits:

SELECT doc_id, CAST(0 as int) FROM <index> WHERE word = '苹果香蕉' GROUP BY doc_id
SELECT doc_id, CAST(0 as int) FROM <index> WHERE word = '苹果' GROUP BY doc_id

Neither literal is an indexed word, so the scan is empty. This SQL is what the planner precompiles (pkg/sql/plan/apply_indices_fulltext.go 698–707 → getFullTextIndexScanSql, pkg/sql/plan/fulltext.go 153–161) and what fulltext_index_scan runs again (pkg/sql/colexec/table_function/fulltext.go runWordStats 475–476).

The unquoted boolean term and natural-language mode already expand CJK through GenTextSql (sql.go 168–188) → ParsePatternInNLMode. On the same inputs that produces prefix_eq(word,'苹果') and the positional pair word = '苹果香' at anchor 0 with word = '果香蕉' at anchor+3. Those hit the rows above. parser = "gojieba" is also unaffected: parsePhraseJieba segments the quote. CREATE FULLTEXT2 INDEX does not use this SQL; ngramPhraseSlots (pkg/fulltext2/query.go 473–528) builds the shifted trigrams.

Expected Behavior

A quoted boolean phrase on the default/ngram parser should be decomposed the same way GenTextSql decomposes an unquoted boolean term, then matched with the positions SqlPhrase already applies. "苹果香蕉" should return the row whose body is 苹果香蕉. "苹果" should return the row whose body is 红苹果甜.

Steps to Reproduce

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

SELECT id FROM ft WHERE MATCH(body) AGAINST('"苹果香蕉"' IN BOOLEAN MODE);
-- success, 0 rows. Row 1 contains the phrase.

SELECT id FROM ft WHERE MATCH(body) AGAINST('"苹果"' IN BOOLEAN MODE);
-- success, 0 rows. Row 2 contains 苹果. Indexed words are 红苹果 / 苹果甜 / 果甜 / 甜.

-- same parser, these already hit:
SELECT id FROM ft WHERE MATCH(body) AGAINST('苹果香蕉' IN NATURAL LANGUAGE MODE); -- id 1
SELECT id FROM ft WHERE MATCH(body) AGAINST('+苹果香蕉' IN BOOLEAN MODE);         -- id 1
SELECT id FROM ft WHERE MATCH(body) AGAINST('+苹果' IN BOOLEAN MODE);             -- id 2

The same word = '<whole phrase>' SQL is produced for parser "" and "default". A phrase of 1, 2, or more than 3 CJK characters without spaces is the broken shape. A phrase of exactly 3 CJK characters happens to equal one stored trigram, so that single lookup can hit.

Additional information

TestPatternToSqlGojiebaPhrase (pkg/fulltext/jieba_test.go 136–156) already treats word = '我来到北京' as the bug for gojieba and only pins the whitespace split for an English phrase (TestParsePhraseWhitespaceParserUnchanged). GenTextSql's comment says a Chinese boolean term must be tokenized into ngrams; the quoted-phrase branch never calls it.

Activity

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

    @Ariznawlll
    CollaboratorAuthor

    Same path also skips SimpleTokenizer for a Latin word longer than 23 bytes. A CJK-only ngram split of the quoted text would still miss this.

    outputLatin (pkg/monlp/tokenizer/simple.go 144–164) stores at most MAX_TOKEN_SIZE (23) bytes. A 26-byte run of b is indexed as 23 bs. ParsePhrase (662–689) only splits on spaces for every parser other than gojieba, and patternToSql (579–581) sends that PHRASE to SqlPhrase as raw word =.

    ParsePattern + PatternToSql on this tree:

    • NL, 26 bs, parser ngram: WHERE word = 'bbbbbbbbbbbbbbbbbbbbbbb' (23). Matches the index.
    • Unquoted boolean, 26 bs, parser ngram, and + the same word, parser default: the same 23-byte word =.
    • Quoted boolean "bbbbbbbbbbbbbbbbbbbbbbbbbb", parser ngram: WHERE word = 'bbbbbbbbbbbbbbbbbbbbbbbbbb' GROUP BY doc_id (26). Empty scan.
    • Quoted "hello bbbbbbbbbbbbbbbbbbbbbbbbbb", parser "": second anchor is word = 'bbbbbbbbbbbbbbbbbbbbbbbbbb' at pos - 6. Empty.
    • NL hello + the 26-byte word, parser ngram: second word is the 23-byte token at pos - 6. Matches.

    parsePhraseJieba already tokenizes the quote, and jieba truncates to 23 bytes, so gojieba is not this case. The quoted text on "" / default / ngram needs SimpleTokenizer: CJK trigram split and the 23-byte Latin cut.

    This is still classic SqlPhrase, not the fulltext2 ngramPhraseSlots miss (filed separately).

  5. added
    severity/s0
    and removed on Sep 23, 2026
  6. Ariznawlll commented on Sep 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    Same ParsePhrase path also misses hyphen and apostrophe, which SimpleTokenizer splits at index time.

    ParsePhrase (pkg/fulltext/fulltext.go 662–689) splits a whole-string quoted pattern only on spaces for every parser other than gojieba. SqlPhrase then looks the pieces up with raw word =. Index build runs SimpleTokenizer, and isBreakerRune treats both - and ' as breakers:

    • Document full-text search is stored as full @0, text @5, search @10. AGAINST('"full-text search"' IN BOOLEAN MODE) looks up word = 'full-text' and word = 'search'. No row matches.
    • Document Ma'trix Origin is stored as ma @0, trix @3, origin @8. AGAINST('"Ma'\''trix Origin"' IN BOOLEAN MODE) looks up word = 'ma'\''trix'. No row matches.

    Unquoted natural-language Ma'trix Origin does go through SimpleTokenizer and is not this miss. This is the same raw-lookup hole as the CJK trigram case and the >23-byte Latin case above, so it stays on this issue.

    A quote that does not wrap the entire boolean string never becomes a PHRASE node at all. That false positive is #29289, not this one.

  7. Ariznawlll commented on Oct 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    MatrixOne 最新 main 1a0088790c8823bd3fe824ad9ff2ef1ce130eb31;本地独立数据目录、单 CN/TN 服务;MOTR PR #218 已推送,Python 3.10/3.12/3.13 与 Go 离线检查均通过。

    原问题验证

    classic FULLTEXT ngram 的引号 BOOLEAN 短语现在命中实际索引词,不再将中文整串当作未入索引的原始词查找。

    正常场景覆盖

    "苹果香蕉" 命中 id=1;短词 "苹果" 命中 1/2/6;自然语言与未引号 BOOLEAN 对照保持正确。

    边界与异常场景覆盖

    连字符 full-text、撇号 Ma'trix 与超过 23 字节的 Latin 词均命中各自目标,不误命中旁支文档。

    白盒验证

    现有 MatrixOne BVT test/distributed/cases/fulltext/fulltext_boolean_phrase_ngram.sql 覆盖更完整的混合文字、大小写折叠、容量边界;此前按 golden 3/3 通过。

    回归测试

    新增 MOTR 14_issue_regression.issue_29271_fulltext_boolean_phrase_ngram,3/3 通过(16 条语句/轮)。

    测试证据

    MOTR PR #218 的 SQL/golden,6 行文档及命中 id 可复核。

    测试结论

    测试通过。

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