Skip to content

fix(fulltext): lowercase a <3-rune NL pattern before the prefix search (#29296) - #29354

Merged
XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
cpegeric:bug_29296
Sep 28, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
cpegeric:bug_29296

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

What this PR does / why we need it:

A classic-fulltext natural-language / default MATCH ... AGAINST pattern shorter than 3 runes was turned into a STAR prefix of the raw pattern by ParsePatternInNLMode, with no case folding. Indexed Latin tokens are stored lowercased (SimpleTokenizer.outputLatin) and prefix_eq is case-sensitive, so AGAINST('Hi') / 'HI' / 'Ab') prefix-searched the capitalized string and returned 0 rows even though the documents contain high / hi / Abyss.

Boolean mode already lowercases its whole pattern, and a ≥3-rune NL pattern folds case through the tokenizer — only this short branch missed it.

Fix: lowercase the pattern before appending *:

return []*Pattern{{Text: strings.ToLower(pattern) + "*", Operator: STAR}}, nil

The other short-pattern STAR sites in the same function build from tokenizer output (already lowercased), so this is the only site. The <3-rune prefix rule and the ≥3-rune exact-token rule are unchanged, and the -5 breaker-prefix case is out of scope. fulltext2 already lowercases its own query path.

Verification

Live: Hi / HI → {1,2}, Ab / AB → {5} (all were empty); lowercase controls, hig (3-rune exact → no match), and HIGH (≥3 tokenized → {1}) unchanged.

  • Unit TestParsePatternInNLModeShortLowercases over "" / ngram / default (plus the hig exact-token control), proven to fail on the pre-fix code.
  • BVT fulltext_short_pattern_case locks the SQL behavior.
  • Full pkg/fulltext UT and the classic fulltext (1636) and fulltext2 (909) BVT suites pass; gofmt clean.

matrixorigin#29296)

A classic-fulltext natural-language / default pattern shorter than 3 runes was turned into a
STAR prefix of the RAW pattern by ParsePatternInNLMode, with no case folding. Indexed Latin
tokens are stored lowercased (SimpleTokenizer.outputLatin) and prefix_eq is case-sensitive, so
`AGAINST('Hi')` / `'HI'` / `'Ab')` prefix-searched the capitalized string and returned no rows
even though the documents contain `high` / `hi` / `Abyss`. Boolean mode already lowercases its
whole pattern, and a >=3-rune NL pattern folds case through the tokenizer -- only this short
branch missed it.

Lowercase the pattern before appending the `*`. The other short-pattern STAR sites in the same
function build from tokenizer output (already lowercased), so this is the only site. The
<3-rune prefix rule and the >=3-rune exact-token rule are unchanged, and the `-5`
breaker-prefix case is out of scope. fulltext2 already lowercases its own query path.

Verified live: `Hi`/`HI` -> {1,2}, `Ab`/`AB` -> {5} (all were empty), lowercase controls and
`hig` (3-rune exact, no match) / `HIGH` (>=3 tokenized) unchanged. Test
TestParsePatternInNLModeShortLowercases over "" / ngram / default, proven to fail on the
pre-fix code; BVT fulltext_short_pattern_case locks the SQL behavior. Full pkg/fulltext UT and
the classic fulltext (1636) and fulltext2 (909) BVT suites pass.

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 →

@cpegeric

Copy link
Copy Markdown
Contributor Author

fixed

# Conflicts:
#	pkg/fulltext/jieba_test.go

@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 review at exact head c788ea94700fb7202981caa549c2db83cfcbf29b: APPROVE. The original short-pattern defect and the follow-up counterexamples are addressed: normalization matches SimpleTokenizer’s Latin-only folding for the relevant parsers and preserves non-Latin runes and json_value case, instead of applying blanket strings.ToLower. That keeps index/query token semantics aligned while retaining the existing <3-rune prefix rule and >=3-rune exact-token path. The real-SQL regression and parser tests cover the previously failing case plus case-preserving controls; required exact-head UT, SCA, coverage, PROXY and pessimistic BVT pass. I found no need for extra state or design machinery and no confirmed new unhappy-path or performance regression.

@mergify

mergify Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@XuPeng-SH
XuPeng-SH merged commit d99187d into matrixorigin:main Sep 28, 2026
19 of 20 checks passed
XuPeng-SH added a commit that referenced this pull request Oct 22, 2026
…efix search (#30484) (#30612)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #30484

## What this PR does / why we need it:

Backport of #29296 / PR #29354 (`d99187d7b3`) to **4.2-dev**.

On the 4.2 line, a classic `FULLTEXT` natural-language query term
**shorter than
3 runes that contains uppercase** silently matched nothing — `DB`, `Db`,
`Xy`,
`XY` → empty, while `db`/`xy` and any length-≥3 term (`ABC`,
`MatrixOne`) work.
This hits the very common 2-letter uppercase acronyms (`AI`, `DB`, `ID`,
`UI`,
`IP`, `VM`, …) as a silent under-recall.

### Root cause

`ParsePatternInNLMode` handles a term shorter than `ngram_size` (3) with
a
prefix (`STAR`) shortcut that built the pattern from the **raw** query
string,
bypassing the case-folding the `SimpleTokenizer` applies to longer terms
(and
that the document side applies at index time). So `DB` became the prefix
`DB*`
and looked up the lowercased stored token `db`, matching nothing.
(Boolean mode
was unaffected — it lowercases the whole pattern up front.)

### Fix (cherry-picked from main)

Normalize the short pattern the **same way the index stores tokens**:
`normalizeShortPattern` folds only Latin-class runes (`<0x7FF`, matching
`outputLatin`) and preserves wider runes verbatim (`outputCJK`). A
blanket
`strings.ToLower` would be wrong — it would fold full-width/CJK capitals
like
`A`/`ẞ` that the ngram index stores cased; `json_value` is preserved
verbatim.

Cherry-pick is clean in `pkg/fulltext/fulltext.go`; the only conflict
was an
adjacent test block in `jieba_test.go` (other backlog tests not part of
this
fix), resolved to keep exactly #29296's three regression tests.

### Validation (on 4.2-dev)

- `pkg/fulltext` unit tests pass, including the three ported #29296
regressions
(`TestParsePatternInNLModeShortLowercases`,
`...ShortMatchesTokenizerFolding`,
  `...ShortJSONValuePreservesCase`).
- End-to-end on a built 4.2-dev `mo-service`: `DB`/`Db`/`Xy`/`XY` now
return the
  expected rows, while `db`/`ABC` remain correct.
- BVT `fulltext/fulltext_short_pattern_case` passes 25/25 (`-g -n`).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: XuPeng-SH <[email protected]>

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/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants