Skip to content

fix(fulltext): expand BOOLEAN trailing-star CJK stem to a positional phrase (#29273) - #29366

Merged
mergify[bot] merged 10 commits into
matrixorigin:mainfrom
cpegeric:bug_29273
Sep 29, 2026
Merged

mergify[bot] merged 10 commits into
matrixorigin:mainfrom
cpegeric:bug_29273

Conversation

@cpegeric

@cpegeric cpegeric commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #29273

What this PR does / why we need it:

In classic fulltext (MATCH ... AGAINST in BOOLEAN mode), a trailing * on a CJK stem returned no rows for a document that clearly contains the stem, e.g. 苹果香蕉* missed a row storing 苹果香蕉.

Root cause

BOOLEAN-mode * was implemented as prefix_eq(word, stem) over the whole stem, but the ngram tokenizer never stores the whole stem:

  • CJK is stored as sliding trigrams — 苹果香蕉 → 苹果香, 果香蕉, 香蕉, 蕉 — so prefix_eq(word, '苹果香蕉') matches nothing.
  • Latin tokens are capped at MAX_TOKEN_SIZE (23 bytes), so an over-long Latin stem prefix-matched the untruncated string and missed the stored (truncated) token.

Fix (pkg/fulltext only — no SQL/plan-layer changes)

  • genStarPhraseSql expands a multi-trigram CJK stem (or a multi-word gojieba stem) into a positional phrase at SQL-gen time: the leading exact piece(s) + the last piece as prefix_eq, joined on a shared positional anchor with HAVING COUNT(*) = N. So 苹果香蕉* becomes word = '苹果香' + prefix_eq(word,'蕉') — matching 苹果香蕉 / 苹果香蕉西瓜 but not the sibling 苹果香瓜.
  • The parse tree stays a plain (* stem*) STAR leaf, so the +苹果香蕉* form and the Index/leaf-extraction paths are untouched.
  • truncateStarPrefix caps the star prefix at the 23-byte stored-token boundary (rune-aware) at all prefix sites, fixing the Latin facet.
  • A stem that is ≤ one trigram (e.g. 苹果*, 苹果香*) stays a plain prefix_eq — existing behavior.

Test

  • New unit tests pkg/fulltext/star_phrase_29273_test.go (CJK positional-phrase expansion, Latin 23-byte truncation, gojieba last-segment star).
  • New BVT test/distributed/cases/fulltext/fulltext_star_phrase.{sql,result} covering ngram CJK (incl. + form and non-star controls), Latin truncation, and gojieba.
  • Full fulltext BVT suite 1643/1643 and fulltext2 909/909 — no regressions; gofmt/go vet clean.

🤖 Generated with Claude Code

…phrase (matrixorigin#29273)

In classic fulltext BOOLEAN mode a trailing `*` did a prefix_eq of the whole
stem. The ngram tokenizer stores a CJK stem as sliding trigrams (苹果香蕉 ->
苹果香/果香蕉/香蕉/蕉) and caps Latin tokens at 23 bytes, so prefix_eq'ing the raw
stem matched a token that is never stored -> 0 rows.

Expand a multi-trigram CJK (or multi-word gojieba) star stem into a positional
phrase at SQL-gen time: leading exact piece(s) + last piece as prefix_eq +
HAVING COUNT(*)=N. The parse tree stays a plain STAR leaf so the `+` form and
the Index/leaf paths are untouched. Also cap the star prefix at the 23-byte
stored-token boundary so an over-long Latin stem matches the truncated token.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify mergify Bot added the kind/bug Something isn't working label Sep 24, 2026
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Sep 24, 2026
@cpegeric

Copy link
Copy Markdown
Contributor Author

fixed

…alue + linear CJK offsets (matrixorigin#29273)

P1 (correctness): json_value stores each JSON value as ONE verbatim token (capped at
127 bytes by bytejson.fillToken), not trigrams. genStarPhraseSql routed json_value
into cjkStarPhraseChildren, so `苹果香蕉*` required never-stored 苹果香/蕉 tokens and
returned nothing; and truncateStarPrefix cut a json_value Latin query to the
SimpleTokenizer 23-byte cap, so a 26-char prefix wrongly matched sibling values.
Make both parser-aware: json_value returns ok=false (whole-value prefix_eq) and caps
at 127 bytes. Other parsers are unchanged.

P2 (perf): cjkStarPhraseChildren computed each child's byte Position via
string(runes[:i]), re-encoding the whole preceding prefix every trigram -> O(n^2) on a
long CJK stem. Track the byte offset in one pass so construction is linear.

Add json_value SQL counterexamples (CJK whole-value star; 26-char Latin prefix) as
unit + BVT, plus truncateStarPrefix/genStarPhraseSql json_value unit cases.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ion boundary (matrixorigin#29273)

truncateStarPrefix's non-json_value path used a clean UTF-8 backoff (inspecting the
byte AT the 23-byte cut), but SimpleTokenizer.outputLatin keys off the last KEPT byte
and drops a trailing multi-byte char that straddles the cap: `a`+12×`я` (25B) is
stored as `a`+10×`я` (21B), not a clean 23B. The query therefore built a prefix one
char longer than the stored token, so prefix_eq was false and a BOOLEAN
`AGAINST('<multibyte-Latin >23B>*')` on an ngram index missed the row. Reuse
tokenizer.TruncateLatinToken, which reproduces outputLatin's boundary exactly (the
classic-fulltext analogue of the fulltext2 fix in matrixorigin#29276). ASCII, CJK trigram tails,
and short gojieba words are unaffected.

Assert the exact stored bytes (é×12->é×11, a+12я->a+10я, and !=clean-23B) plus an
end-to-end SimpleTokenizer round-trip, and add a live Cyrillic BVT case.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep re-review of 0919d491e7 against 9322133017. All five changed files and the index-writer/tokenizer consumers were reviewed; no subagents.

Decision: approve this reviewed revision.

Previous findings are substantively closed:

  • #discussion_r4106602574: all four STAR SQL-generation sites are parser-aware. json_value retains whole-value matching and its 127-byte cap; it does not enter CJK/jieba phrase expansion.
  • #discussion_r4106602583: CJK byte positions are accumulated once, removing repeated prefix re-encoding.

Design / QA audit:

  • R2 query/index contract: checked ngram CJK positional tokens, short/exact/no-star controls, Latin/Cyrillic token boundaries, gojieba segmentation and json_value adjacent-prefix nonmatches. The unchanged index writer is the independent representation oracle.
  • R2 scale: reviewer mixed 3-/4-byte Unicode and 24K-rune offset probes pass. A 5-iteration benchmark measured 6K/12K/24K runes at approximately 0.227/0.471/0.782 ms and 201/402/804 KB per operation. This supports linear construction, not a general query-execution speedup claim.
  • Ownership / unhappy paths: generated patterns and SQL remain request-owned; jieba initialization errors propagate; no new worker, cache, wire/catalog format or index rebuild requirement. The shared native tokenizer retains its existing ownership contract.
  • Engineering: reuses positional phrase SQL and tokenizer boundaries, without a second indexing implementation. No blocking overengineering finding.

Verification:

  • Full ./pkg/fulltext suite passed on this head, including gojieba (no skips).
  • The committed fulltext_star_phrase.sql/.result ran in normal mo-tester comparison mode twice on the same exact-head embedded instance: 35/35 each, zero failed/ignored/abnormal. An independent catalog query confirmed ft_star_phrase absent after each run, before the next runner could clean it.
  • Exact-head CI run 36399151966 also actually selected this case: 35/35, not merely a green aggregate status.

The branch is behind its current target; this approval is tied to the SHA above and does not certify a future rebase or merge result.

@cpegeric
cpegeric requested a review from aptend September 28, 2026 13:39

@aptend aptend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 0919d49. Rechecked both prior findings against the incremental and complete diff, historical inline threads/replies, token storage semantics for ngram/gojieba/json_value, all four STAR SQL-generation paths, and the added SQL/Go regression cases. The CJK byte-position construction is linear and the parser-specific fallback matches the indexed token boundaries. No remaining blocking issue found. Head CI (UT, BVT, SCA) is green. Local focused rerun was unavailable because this isolated worktree has no matching CGo artifacts; I did not alter the primary checkout.

@mergify mergify Bot added the queued label Sep 29, 2026
@mergify

mergify Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-29 02:53 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-09-29 04:04 UTC · at c69187e0e35e04c9cf3791b2a5318944d5bd2446 · squash

This pull request spent 1 hour 10 minutes 35 seconds in the queue, including 1 hour 10 minutes 18 seconds running CI.

Required conditions to merge

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants