Repository navigation
Text index: support '<string>%' and '%<string>' patterns for LIKE/ILIKE optimization - #114728
Conversation
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
efe044e to
029ce21
Compare
|
Workflow [PR], commit [c41a767] Summary: ✅
AI ReviewSummaryThis PR extends text-index dictionary-scan optimization to prefix/suffix Findings❌ Blockers
Final Verdict❌ Not ready to merge until nullable infix direct read preserves LLVM Coverage ReportMeasured on commit c41a767.
Changed lines: Changed C/C++ lines covered: 152/154 (98.70%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units12 translation units recompiled, 63 s compile time in total, 12 of them have a recent master baseline. |
029ce21 to
f196f2c
Compare
| const auto & like_pattern = value_field.safeGet<String>(); | ||
| auto patterns = stringLikeToPatterns(value_field, /*case_insensitive=*/ false); | ||
| const bool is_infix = isInfixPattern(like_pattern); | ||
| if (patterns.size() == 1 && (is_infix || affix_patterns_allowed)) |
There was a problem hiding this comment.
The new nullable guard only covers affix hints. Infix patterns still bypass it here, so Nullable(String) can take the exact virtual-column path for LIKE '%foo%' and ILIKE '%foo%'.
That breaks three-valued logic under NOT: a NULL row has no postings, so the reader fills the UInt8 virtual column with 0, and optimizeDirectReadFromTextIndex then replaces the original predicate with that column alone in exact mode (src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp:791-813). NOT 0 becomes 1, while NOT (NULL LIKE '%foo%') / NOT (NULL ILIKE '%foo%') should stay NULL.
Please block exact pattern direct-read on nullable columns as well, or keep the original predicate/fallback in the plan so NULL can survive as NULL.
|
📊 Cloud Performance Report
1 query inconclusive (baseline era shift, segment too thin). K_source=6 K_base=30 flagged=0/65 clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
…like-prefix-suffix # Conflicts: # docs/reference/engines/table-engines/mergetree-family/textindexes.mdx
Fix ClickHouse spelling in the affix test fixture
…like-prefix-suffix
|
@ahmadov @rschu1ze — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
…ng>' patterns for LIKE/ILIKE optimization
…-index-like-affix Backport #114728 to 26.8: Text index: support '<string>%' and '%<string>' patterns for LIKE/ILIKE optimization
…nd '%<string>' patterns for LIKE/ILIKE optimization
…nd '%<string>' patterns for LIKE/ILIKE optimization
Issue ClickHouse#118197 (duplicate ClickHouse#118037): on a `text(tokenizer = array)` index, `startsWith`/`endsWith` are answered by a dictionary scan with the LIKE pattern `needle%` / `%needle` in Exact mode, so the original predicate is dropped. Before ClickHouse#115649 the needle went into the pattern unescaped, and a `%` in it acted as a wildcard: `startsWith(tag, 'foobar%')` returned every row starting with `foobar`. ClickHouse#115649 escapes `%`, `_` and `\` on master, but no test pinned it: the only such case, `startsWith(name, '100%')` in 02346_text_index_function_like_array_patterns, returns the same rows with or without the escaping. The new test covers `%` next to the added wildcard and at the far edge, `LIKE 'foobar\%%'` (rewritten to `startsWith` by `optimize_rewrite_like_perfect_affix`), `_` and a trailing backslash, in both directions. Each indexed query is compared with `use_skip_indexes = 0`, and EXPLAIN checks that the text index answers it alone. Validated: matches the reference on master; with `escapeForLikePattern` removed from the affix branch all nine indexed results change (the last one throws CANNOT_PARSE_ESCAPE_SEQUENCE); on 26.8.21.12, which has the ClickHouse#114728 backport without ClickHouse#115649, the five `%` cases return extra rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>

Currently, text index is leveraged only for a LIKE pattern
%<alpha-numerical>%(dictionary scan), other similar patterns (<alpha-numerical>%and%<alpha-numerical>) fall back to the full-table scan.With this PR, those two patterns (a.k.a.
startsWithandendsWith) are supported now. Compare to the pattern%<alpha-numerical>%, the text index is used inHINTmode - it means rows will be filtered out using the text index (dictionary scan) then the original filter condition will be run again to match. So, the query would be re-written internallyfrom:
to:
The virtual column is filled from the posting lists of the tokens the dictionary scan matched.
Fixes #112597.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Utilize text index for the supported LIKE/ILIKE patterns.
Workflow [PR]
Sync PR [sync-upstream/pr/114728]