Skip to content

fix(fulltext2): keep the whole CJK stem for a BOOLEAN trailing * (#29274) - #29346

Merged
XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
cpegeric:bug_29274
Sep 25, 2026
Merged

XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
cpegeric:bug_29274

Conversation

@cpegeric

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 #29274

What this PR does / why we need it:

A fulltext2 boolean-mode CJK operand with a trailing * kept only the first token and prefix-matched it, dropping the rest of the stem and over-matching. Same class of bug in two tokenizers:

  • ngram / default / "" tile 苹果香蕉 into overlapping trigrams 苹果香/果香蕉/香蕉/蕉 and kept only 苹果香, so 苹果香蕉* returned 苹果香瓜 (shares 苹果香, diverges at rune 4).
  • gojieba segments it into words 苹果/香蕉 and kept only 苹果, so 苹果香蕉* returned every 苹果* document (1,2,3,4).

Root cause: rawToClauseParser's rc.star branch returned clausePrefix over terms[:1].

Fix: when the operand tokenizes into more than one token, decompose it the same way the unstarred/quoted operand already does, via phraseSlots. The positional phrase pins the whole stem (ngram: 苹果香@0 + 果香蕉@3; gojieba: 苹果@0 + 香蕉@…), and a phrase already allows trailing document content — exactly the * semantics — so 苹果香蕉* matches 苹果香蕉 / 苹果香蕉西瓜 but not 苹果香瓜.

A single-token stem keeps its genuine prefix: gojieba 苹果* stays a word prefix (still matches 红苹果甜's 苹果甜); for ngram the gate is always taken and phraseSlots itself emits a presence-only prefix slot for a run shorter than a trigram (slotPostings expands it via prefixTerms), so a short ngram stem such as 苹果* also keeps prefix behavior. Non-CJK (Latin) stays a plain first-token prefix. This also fixes StreamQuery, which shares buildBooleanQuery.

Verification

Live (ngram and gojieba): 苹果香蕉* / +苹果香蕉* → {1,3}, 苹果香* → {1,2,3}, 苹果* → {1,2,3,4}, NL 苹果香蕉 → {1,3}.

  • Unit tests cover "" / default / ngram and gojieba, proven to fail on the pre-fix code.
  • BVT fulltext2_cjk_boolean_prefix (ngram + gojieba sections) locks the SQL behavior.
  • Full pkg/fulltext2 UT and the fulltext2 BVT suite (930) pass; gofmt clean.

@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 →

…rixorigin#29274)

A boolean-mode CJK operand with a trailing `*` kept only the FIRST token and prefix-matched
it, dropping the rest of the stem and over-matching:
  - ngram / default / "" tile 苹果香蕉 into 苹果香/果香蕉/香蕉/蕉 and kept only 苹果香, so
    `苹果香蕉*` returned 苹果香瓜 (shares 苹果香, diverges at rune 4).
  - gojieba segments it into words 苹果/香蕉 and kept only 苹果, so `苹果香蕉*` returned every
    苹果* document (1,2,3,4).

rawToClauseParser's rc.star branch returned clausePrefix over terms[:1]. When the operand
tokenizes into more than one token, decompose it the same way the unstarred/quoted operand
does, via phraseSlots: the positional phrase pins the whole stem (ngram: 苹果香@0+果香蕉@3;
gojieba: 苹果@0+香蕉@…), and a phrase already allows trailing document content -- exactly the
`*` semantics -- so `苹果香蕉*` matches 苹果香蕉 / 苹果香蕉西瓜 but not 苹果香瓜.

A SINGLE-token stem keeps its genuine prefix: gojieba `苹果*` stays a word prefix (still
matches 红苹果甜's 苹果甜); for ngram the gate is always taken and phraseSlots itself emits a
presence-only prefix slot for a run shorter than a trigram (slotPostings expands it via
prefixTerms), so a short ngram stem such as `苹果*` also keeps prefix behavior. Non-CJK
(Latin) stays a plain first-token prefix. Also fixes StreamQuery (shares buildBooleanQuery).

The four operand branches (bare-CJK, quoted, starred-CJK, default) built the same
"phraseSlots -> err -> empty -> clausePhrase" block; factor it into phraseClause to remove
the duplication and its repeated guards.

Verified live for ngram AND gojieba: `苹果香蕉*` / `+苹果香蕉*` -> {1,3}, `苹果香*` -> {1,2,3},
`苹果*` -> {1,2,3,4}, NL 苹果香蕉 -> {1,3}. Tests: TestBooleanCJKTrailingStarKeepsWholeStem
("" / default / ngram) and TestBooleanGojiebaTrailingStarKeepsWholeStem, both proven to fail
on the pre-fix code; TestRawToClauseParserEdgeCases / TestPhraseClause cover the operand
classification and empty/edge inputs directly (rawToClauseParser 72.7% -> 91.4%). BVT
fulltext2_cjk_boolean_prefix (ngram + gojieba) locks the SQL behavior. Full pkg/fulltext2 UT
(87.5%) and the fulltext2 BVT suite (930) pass.

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

Two regressions from the starred-stem phrase decomposition:

1. gojieba final word: `苹果香*` segments to 苹果@0 + 香@6, but a document 苹果香蕉
   stored 苹果@0 + 香蕉@6. Leaving the last slot exact dropped the row. The final
   gojieba word is a PARTIAL the * cut, so prefix-match it even when CJK (the head
   words stay positionally exact, so a divergent head is still excluded).

2. single-slot ngram: a complete-trigram stem `苹果香*` (or a sub-trigram run 中*)
   decomposes to ONE slot. Converting it to a one-slot positional phrase forced
   matchPhrase to decode every position of a hot prefix (measured ~14x time, ~20x
   allocs). There is no second slot to over-match, so keep the cheap prefix
   evaluation (doc-id/tf) as before.

Multi-slot ngram stems keep the positional phrase (the original over-match fix) and
a Latin tail still prefix-matches (the earlier mixed CJK/Latin fix). Adds gojieba
partial-CJK-word and single-slot-stays-prefix regressions (unit + BVT).

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.

Re-reviewed exact head 7ce85dbb07f43dbe78ec7117277fc23161cb76c1 against merge-base 426cf10b000b6a828e8c69f1af26082f26036ed3. All four changed files reviewed. The two blockers from my previous review are resolved; no new blocking findings.

  • gojieba now prefix-matches its final Chinese word while retaining preceding positional constraints. The original whole-stem and mixed CJK/Latin cases remain correct.
  • Single-slot ngram stems return to the position-free prefix evaluator. On the previous loaded-index benchmark (6,000 documents, 2,000 hits, 250 occurrences/hit, BM25 top 10), current runs are 0.119–0.134 ms/query and ~200,032 B / 93 allocations, versus 0.120–0.122 ms and the same allocations for the prior-prefix control. Row/score sets match; the former ~1.9ms / 4.32MB regression is gone.

Verification:

  • Full pkg/fulltext2 with -vet=all -count=1: PASS, 526 test/subtest pass events, no skips; gojieba executed.
  • Reviewer-only loaded-index probes: SearchQuery and StreamQuery both pass repeated partial-CJK/mixed-Latin cases, divergent-head/tail and wrong-position controls, absent prefixes, MUST-NOT interaction, and consumer-error followed by successful reuse.
  • Exact changed mo-tester case ran twice in normal comparison mode on the same fresh single-CN instance: 32/32 each (0.406s / 0.227s). Database and relation residue counts were zero after each run, checked before subsequent runner cleanup. Test-owned server exited cleanly.
  • Exact-head CI run 36149032476 is green; its pessimistic artifact independently confirms this case ran 32/32.

The small shared phrase helper and narrow parser-specific tail rule are proportionate; no additional framework is warranted. No production code was changed during review.

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

mergify Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-25 18:21 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-25 18:22 UTC · at 357550b119b62da022ea75ef02dd1c4e23098f7c

This pull request spent 37 seconds in the queue, with no time running CI.

Reason

Pull request #29346 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

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.

4 participants