Skip to content

Do not use text index for equality with an empty needle - #108340

Merged
rschu1ze merged 3 commits into
masterfrom
fix-text-index-empty-string-equals
Jun 24, 2026
Merged

rschu1ze merged 3 commits into
masterfrom
fix-text-index-empty-string-equals

Conversation

@rschu1ze

@rschu1ze rschu1ze commented Jun 23, 2026 •

Copy link
Copy Markdown
Member

Fixes a special case with optimization optimize_empty_string_comparisons that that popped up in #107385.

@alexbakharew ^

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Version info

  • Merged into: 26.7.1.40

@clickhouse-gh

clickhouse-gh Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [42970c7]

Summary: ✅


AI Review

Summary

This PR prevents plain col = '' predicates from being planned through a text index and adds stateless coverage for optimize_empty_string_comparisons both off and on. The narrow equals guard fixes only part of the contract: other equality-like text-index predicates can still build empty-token Exact direct-read queries and remove the original predicate, so the PR needs changes before merge.

Findings

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreeIndexConditionText.cpp:783], [src/Storages/MergeTree/MergeTreeIndexConditionText.cpp:792], [src/Storages/MergeTree/MergeTreeIndexConditionText.cpp:1147] Empty needles must not become text-index equality queries, because empty token sets are not indexed and the direct reader treats an empty virtual-column query as always true. The new guard only covers plain equals; mapContainsKey, mapContainsValue, and has still call stringToTokens and construct FUNCTION_EQUALS without rejecting an empty result. With TYPE text(tokenizer = 'array') and query_plan_direct_read_from_text_index = 1, these predicates use TextIndexDirectReadMode::Exact, so replaceFunctionsToVirtualColumns can replace the original predicate with a virtual column and MergeTreeReaderTextIndex::classifyVirtualColumns fills that virtual column as true. This can return rows that do not match has(arr, ''), mapContainsKey(m, ''), or mapContainsValue(m, '').
    Suggested fix: centralize construction of FUNCTION_EQUALS / Exact text queries behind a helper that returns false when tokenization produces an empty vector, or make replaceToVirtualColumn reject empty token/pattern queries for exact search predicates as well.
Tests
  • ⚠️ [tests/queries/0_stateless/02346_text_index_empty_string_equals.sql:18] The regression test only uses splitByNonAlpha plus force_data_skipping_indices, so it does not prove the exact direct-read path. Add a small TYPE text(tokenizer = 'array') case with query_plan_direct_read_from_text_index = 1, for example rows ('', 'x'), asserting WHERE s = '' returns only the empty row. After fixing the shared empty-token guard, also cover at least one equality-like exact predicate such as has(arr, '') or mapContainsKey(m, '').
Final Verdict

Status: ⚠️ Request changes

Minimum required actions: reject empty-token Exact text-index queries across all equality-like predicates, and add focused direct-read regression coverage for the array tokenizer path.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jun 23, 2026

@Ergus Ergus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ohh good catch

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 }

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.

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())

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.

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, '').

@clickhouse-gh

clickhouse-gh Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 6/6 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@rschu1ze
rschu1ze enabled auto-merge June 24, 2026 18:12
@rschu1ze
rschu1ze added this pull request to the merge queue Jun 24, 2026
Merged via the queue into master with commit fa9c21d Jun 24, 2026
167 checks passed
@rschu1ze
rschu1ze deleted the fix-text-index-empty-string-equals branch June 24, 2026 18:47
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 24, 2026
pull Bot pushed a commit to Haofei/ClickHouse that referenced this pull request Jul 23, 2026
… 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]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…mpty-string-equals

Do not use text index for equality with an empty needle
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
… 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants