Repository navigation
Do not use text index for equality with an empty needle - #108340
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [42970c7] Summary: ✅
AI ReviewSummaryThis PR prevents plain Findings
Tests
Final VerdictStatus: Minimum required actions: reject empty-token |
| INSERT INTO tab VALUES (1, 'v1.0 release notes'), (2, 'beta version'), (3, ''); | ||
|
|
||
| SELECT '-- Must not use the index with and without the optimization'; | ||
| SELECT * FROM tab WHERE col = '' ORDER BY id SETTINGS optimize_empty_string_comparisons = 0, force_data_skipping_indices='idx'; -- { serverError INDEX_NOT_USED } |
There was a problem hiding this comment.
This regression still misses the exact direct-read path guarded here. With TYPE text(tokenizer = 'array'), equals uses TextIndexDirectReadMode::Exact; before the new tokens.empty() return, replaceToVirtualColumn could replace s = '' with a text-index virtual column, and MergeTreeReaderTextIndex::classifyVirtualColumns marks empty-token queries as always true. That can return non-matching rows when query_plan_direct_read_from_text_index = 1. Please add a tiny case with an array tokenizer and direct read enabled, e.g. rows ('', 'x'), asserting that WHERE s = '' returns only the empty row.
| /// is disabled, i.e. `col = ''` is _not_ rewritten into `empty(col)`. The latter doesn't | ||
| /// use the index (because `empty` is unsupported). For consistency make sure `equals('')` | ||
| /// behaves the same. | ||
| if (tokens.empty()) |
There was a problem hiding this comment.
This guard needs to cover every FUNCTION_EQUALS text query, not only plain equals. The array/map branches still create exact direct-read queries from an empty token set: for example, has(arr, ''), mapContainsKey(m, ''), or mapContainsValue(m, '') with TYPE text(tokenizer = 'array') can reach the unguarded stringToTokens calls below/above. Empty strings produce no postings, but replaceToVirtualColumn still accepts the Exact query and MergeTreeReaderTextIndex::classifyVirtualColumns marks that virtual column always true, so the original predicate can be removed and non-matching rows returned when query_plan_direct_read_from_text_index = 1.
Please reject empty token sets in a shared helper before constructing FUNCTION_EQUALS/Exact queries, or make replaceToVirtualColumn reject empty tokens for Exact too. A focused regression should cover at least one exact direct-read case such as has(arr, '') or mapContainsKey(m, '').
…empty-string-equals
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 6/6 (100.00%) | Lost baseline coverage: none · Uncovered code |
… empty needle The text index never indexes empty values, and a needle that tokenizes to an empty token list produces an All-mode text search over zero tokens, which is vacuously true. Because the text index condition can replace the real predicate evaluation (exact/direct read), every row matched: has(a, ''), mapContainsKey(m, '') and mapContainsValue(m, '') returned all rows instead of 0. Extend the empty-needle guard added in ClickHouse#108340 (equals branch) to the has handler and the mapKeys/mapValues (mapContainsKey/mapContainsValue and their *Like variants) branch: when the tokenized needle is empty, bail out so the predicate falls back to a full scan with real evaluation. Closes: ClickHouse#110092 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…mpty-string-equals Do not use text index for equality with an empty needle
… empty needle The text index never indexes empty values, and a needle that tokenizes to an empty token list produces an All-mode text search over zero tokens, which is vacuously true. Because the text index condition can replace the real predicate evaluation (exact/direct read), every row matched: has(a, ''), mapContainsKey(m, '') and mapContainsValue(m, '') returned all rows instead of 0. Extend the empty-needle guard added in ClickHouse#108340 (equals branch) to the has handler and the mapKeys/mapValues (mapContainsKey/mapContainsValue and their *Like variants) branch: when the tokenized needle is empty, bail out so the predicate falls back to a full scan with real evaluation. Closes: ClickHouse#110092 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Fixes a special case with optimization
optimize_empty_string_comparisonsthat that popped up in #107385.@alexbakharew ^
Changelog category (leave one):
Version info
26.7.1.40