Skip to content

[Bug]: fulltext2 NL, quoted boolean, and BM25 search of a word longer than 23 bytes looks up the raw string and misses the truncated index token #29276

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", a Latin word longer than 23 bytes is stored truncated and the natural-language, quoted-boolean, and IN BM25 MODE lookups use the untruncated string. The statement succeeds and returns no rows.

SimpleTokenizer.outputLatin (pkg/monlp/tokenizer/simple.go 144–164) keeps at most MAX_TOKEN_SIZE (23) bytes. An ASCII run longer than that is cut at byte 23. A 2-byte rune (isLatin: rune < 0x7FF, lines 77–79) backs up to the previous leading byte, so 12 Cyrillic я (24 bytes) are stored as 11 letters (22 bytes). tokenWord (pkg/fulltext2/build.go 29–34) documents that the tokenizer already truncates. A 26-byte run of b is indexed as 23 bs.

ngramPhraseSlots (pkg/fulltext2/query.go 502–508) does not apply that cap. A Latin run becomes strings.ToLower of the entire run. phraseSlots (443–459) uses ngramPhraseSlots for "" / "default" / "ngram".

Three production lookups call phraseSlots:

  • Natural language. Index.SearchQuery with boolean=false (387–393). The TVF sets Boolean only for FULLTEXT_BOOLEAN (pkg/sql/colexec/table_function/fulltext2_search.go 747) and Fulltext2Search.runTopK calls SearchQuery (pkg/fulltext2/search_cache.go 342).
  • IN BM25 MODE. SearchBagOfWords → buildBagOfWordsQuery (402–418) builds one SHOULD term per phraseSlots term. The TVF sets BagOfWords for FULLTEXT_BM25 (748) and runTopK calls SearchBagOfWords (339–340).
  • Quoted boolean. rawToClauseParser (666–676) sends rc.quoted through phraseSlots. Unquoted boolean does not: it calls tokenizeToTerms (654–680), which uses SimpleTokenizer and therefore hits.

Probed with buildSeg (SimpleTokenizer) and NewIndex. Documents: 1 = 26 bs, 2 = 23 as, 3 = hello + 26 bs, 4 = short. Indexed tokens of the 26-b word: ["bbbbbbbbbbbbbbbbbbbbbbb"] (23). Parsers default, ngram, and "" all printed the same rows:

mode query slots doc ids
NL 26 bs the full 26-byte word (empty). Docs 1 and 3 contain it
NL 23 as the 23-byte word 2
boolean, bare 26 bs lookup is the truncated token, not the printed slot 1, 3
boolean, + 26 bs same 1, 3
boolean, quoted "…" around 26 bs the full 26-byte word (empty)
NL hello + 26 bs hello @0, full 26-byte word @6 (empty). Doc 3 contains it
boolean +hello + 26 bs each operand tokenized 3
BM25 26 bs SearchBagOfWords (empty)

Same root for a 2-byte rune. 12 я (24 bytes) are indexed as 11 я. phraseSlots keeps all 12. NL SearchQuery returns nothing. Unquoted boolean returns doc 9.

parser = "gojieba" is not this path. phraseSlots (452–457) tokenizes, and JiebaTokenizer truncates with truncateUTF8(..., MAX_TOKEN_SIZE) (pkg/monlp/tokenizer/jieba.go 174).

Expected Behavior

Natural-language MATCH, quoted boolean, and IN BM25 MODE should look up the same token SimpleTokenizer.outputLatin stored. A document whose body is 26 bs should be returned for that word in all three modes, the way unquoted boolean already returns it. The 23-byte query that already hits doc 2 should stay a hit. A 12-letter Cyrillic word should be looked up as the 11-letter token the index stored.

Steps to Reproduce

SET experimental_fulltext2_index = 1;

CREATE TABLE ft(id INT PRIMARY KEY, body VARCHAR(200));
INSERT INTO ft VALUES
  (1, 'bbbbbbbbbbbbbbbbbbbbbbbbbb'),
  (2, 'aaaaaaaaaaaaaaaaaaaaaaa'),
  (3, 'hello bbbbbbbbbbbbbbbbbbbbbbbbbb');
CREATE FULLTEXT2 INDEX fi ON ft(body) WITH PARSER ngram;

-- 26 b's are indexed as the first 23. These succeed and return 0 rows.
SELECT id FROM ft WHERE MATCH(body) AGAINST('bbbbbbbbbbbbbbbbbbbbbbbbbb');
SELECT id FROM ft WHERE MATCH(body) AGAINST('bbbbbbbbbbbbbbbbbbbbbbbbbb' IN BM25 MODE);
SELECT id FROM ft WHERE MATCH(body) AGAINST('"bbbbbbbbbbbbbbbbbbbbbbbbbb"' IN BOOLEAN MODE);
SELECT id FROM ft WHERE MATCH(body) AGAINST('hello bbbbbbbbbbbbbbbbbbbbbbbbbb');

-- these hit:
SELECT id FROM ft WHERE MATCH(body) AGAINST('bbbbbbbbbbbbbbbbbbbbbbbbbb' IN BOOLEAN MODE);
SELECT id FROM ft WHERE MATCH(body) AGAINST('+bbbbbbbbbbbbbbbbbbbbbbbbbb' IN BOOLEAN MODE);
SELECT id FROM ft WHERE MATCH(body) AGAINST('aaaaaaaaaaaaaaaaaaaaaaa');

The executed evidence is SearchQuery / SearchBagOfWords on one segment built with SimpleTokenizer, which is what runTopK calls after the index exists. An empty result from querying before the async build finishes is not this bug. "" and "default" take the same ngramPhraseSlots path as "ngram".

Additional information

Unquoted boolean is the control, not a second bug: tokenizeToTerms already truncates. A query that is exactly the stored 23-byte token already hits; the longer query should use that same token. #29274 is the opposite wrong result (a trailing * returns extra rows). Fixing the star path does not truncate this term.

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 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:5be6cd90b501da225be591ffffe6000bf01f515e
    • 部署方式:本地单机(1 CN / 1 TN / 1 LogService)
    • 测试日期:2026-09-27

    原问题验证

    按 issue 原始 SQL 在最新 main 上独立执行 3 次,结果 3/3 符合修复预期;确认修复提交 10d3a3d5f5f34e6727b14827a15879407227111a 已包含在当前 main。

    Happy Path 覆盖

    1. FULLTEXT2 对 26 字节长 token 的自然语言、BM25、quoted boolean 与普通 boolean 查询均返回 id 1、3。

    Unhappy Path 覆盖

    1. 覆盖 23 字节边界 token 和多词查询;查询 token 与索引 token 的截断语义一致。

    白盒验证

    核对修复提交 10d3a3d5f5f34e6727b14827a15879407227111a、对应 BVT/UT 以及当前 main 的祖先关系;本次同时完成公开 SQL 协议验收。

    回归测试

    已加入 MOTR PR #204 的 issue_29079_29276_fulltext_query_contracts.test,MOTR 提交 c7aeb11c;本地基线比对连续 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