Skip to content

Text index condition does not look through positive boolean wrappers — hasToken(s, 'x') = true scans all granules while bare hasToken(s, 'x') prunes #110012

Description

@PedroTadim

Describe the bug

The text skip-index condition recognizes hasToken(col, const) as a prunable atom when it appears as a bare conjunct, but not when the same atom is wrapped in a semantically-equivalent positive boolean comparison. hasToken returns a non-Nullable UInt8, so all of the following select exactly the same rows:

WHERE hasToken(s, 'rare')            -- uses the text index
WHERE hasToken(s, 'rare') = true     -- full scan
WHERE hasToken(s, 'rare') != false   -- full scan
WHERE hasToken(s, 'rare') IN (true)  -- full scan

The wrapped forms build no index condition (EXPLAIN indexes = 1 doesn't even list the index) and read every granule. Results are correct in all cases; only pruning is lost.

Explicit = true wrappers are common in generated SQL (BI layers and ORMs that normalize boolean expressions), so a text-search predicate can silently degrade to a full scan through an equivalence that's invisible in review.

Expected performance

The wrapped forms should build the same index condition as the bare atom and read the same 8/128 granules. Generally: when the text-index condition builder encounters equals(atom, true), notEquals(atom, false), or IN-with-only-truthy-constants around one of its supported atoms (hasToken, hasAnyTokens, equals, LIKE, ...), it should recurse into the atom; the symmetric negative forms (= false, != true) should map to the negated atom where the index supports negation. The IS TRUE / IS NOT DISTINCT FROM true spellings desugar to isNotDistinctFrom(atom, true) and deserve the same treatment.

How to reproduce

Run Fiddle: https://fiddle.clickhouse.com/33bc89bf-a740-450a-a1c8-26e7c3c59ba5

Error message and/or stacktrace

No response

Activity

  1. added
    fuzz
    potential bug
    on Jul 10, 2026
  2. PedroTadim commented on Jul 10, 2026

    @PedroTadim
    MemberAuthor
  3. groeneai commented on Jul 10, 2026

    @groeneai
    Collaborator

    Fix opened: #110024. The text-index condition builder now looks through positive boolean wrappers (= true, != false, IN (true), IS TRUE/isNotDistinctFrom(atom, true), both operand orders) by recursing into the wrapped atom, so all of them prune the same granules as the bare atom (verified 1/128 vs the previous full 128/128). Negative wrappers (= false, != true) and non-boolean RHS (= 5) correctly decline. Regression test 04365_text_index_positive_boolean_wrapper asserts pruning is preserved via EXPLAIN indexes = 1.

  4. added
    comp-text-index
    and removed
    potential bug
    on Jul 10, 2026
  5. rschu1ze commented on Jul 13, 2026

    @rschu1ze
    Member

    Before we let the bot slop more code into the implementation, can we please approach this a little bit more systematically? I ran almost the same test but for bloom filter indexes and with function equals: https://fiddle.clickhouse.com/33e70734-3e2f-4b34-81f0-59738ec37f7b Looks like at least one case (IN) isn't supported there as well.

    The bigger question is: Should boolean wrappers be supported at all? They look like a special case (constant right side) of an equality comparison, as such we'd need shortcuts in many different places in the code. Also, it will be interesting: Which index types (besides text and bloom) and which functions (besides hasToken) lack such support today?

  6. PedroTadim commented on Jul 13, 2026

    @PedroTadim
    MemberAuthor

    Before we let the bot slop more code into the implementation, can we please approach this a little bit more systematically? I ran almost the same test but for bloom filter indexes and with function equals: https://fiddle.clickhouse.com/33e70734-3e2f-4b34-81f0-59738ec37f7b Looks like at least one case (IN) isn't supported there as well.

    The bigger question is: Should boolean wrappers be supported at all? They look like a special case (constant right side) of an equality comparison, as such we'd need shortcuts in many different places in the code. Also, it will be interesting: Which index types (besides text and bloom) and which functions (besides hasToken) lack such support today?

    The main reason is some ORM tools may generate these predicates and it would be nice to support them. I was not sure how many lines were needed to add to support this. As the PR is not huge, is it ok to get merged?

  7. rschu1ze commented on Jul 13, 2026

    @rschu1ze
    Member

    I'm not against merging in the current state (haven't reviewed the code in detail yet). I just thought a more systematic gap analysis will be helpful instead of the patch 1, patch 2, patch 3, ... patch N route.

  8. groeneai commented on Jul 13, 2026

    @groeneai
    Collaborator

    @rschu1ze Systematic survey, all measured on this branch's debug build via EXPLAIN indexes=1 (Granules pruned vs total, index_granularity=4, 64 rows / 16 granules, matching value in 1 granule). Reproducers are the two Fiddles plus the table below.

    Which index types / functions look through positive boolean wrappers today

    predicate PK / minmax / partition bloom_filter ngrambf_v1 set text (#110024)
    bare atom 1/16 1/16 1/16 1/16 1/16
    atom = true 1/16 1/16 1/16 1/16 1/16
    atom != false 16/16 16/16 16/16 1/16 1/16
    atom IN (true) 16/16 16/16 16/16 1/16 1/16
    atom IS TRUE 16/16* 16/16 16/16 1/16 1/16

    16/16 = pruning LOST (full scan). Confirms your bloom-filter IN finding, and it is not just IN: != false and IS TRUE are lost too, on bloom, ngrambf, PK, minmax and partition. Results are correct in every cell; only pruning differs. * PK/minmax/partition IS TRUE is fixed by our open #110006.

    Function coverage is not the axis that matters: on the text index, the wrapped forms fail identically for hasToken, equals, LIKE, etc. It is not per-function; the builder simply never recurses through the wrapper node regardless of the inner atom. Same story for KeyCondition.

    Why = true is the only form that works everywhere

    = true prunes in every family for one reason: the analyzer folds X = true -> X before the expression ever reaches KeyCondition or any skip-index builder (EXPLAIN actions=1 shows the filter is bare equals(id, 7)). The other three forms are not folded, so every pruning consumer has to peel them individually. Today only two do: set (evaluates the stored per-granule value-set against the real expression, so it is wrapper-agnostic by construction) and text (via #110024).

    Recommendation: normalize centrally, not per-index

    I agree these are a special case of equality-with-constant and should not be scattered. The cleanest fix is to extend the existing analyzer fold that already collapses X = true -> X, to also collapse, for boolean-result X:

    • X != false -> X
    • X IS TRUE / X IS NOT DISTINCT FROM true -> X
    • X IN (<only-truthy-const-set>) -> X (decline if any element is false/0, or the set is a subquery)

    Done once in the analyzer, this fixes PK, minmax, partition, bloom, ngrambf, set and text simultaneously, with no per-index code, and it also normalizes the negative forms (= false, != true, IN (false) -> NOT X) for free where a consumer supports negation. It supersedes both the per-index #110024 (text) and the KeyCondition-layer #110006 approach: those become unnecessary if the fold lands upstream.

    Trade-off to flag: the analyzer fold must be gated to genuinely boolean-result expressions. X != 0 is only equivalent to X when X in {0,1}; for a non-boolean X, X != false means X != 0 and must NOT collapse to X. set sidesteps this by evaluating the real expression, which is why it is already correct.

    So: it belongs in one central normalization pass (the analyzer's constant-comparison folding), not in each index. Happy to prototype that fold if you agree on the direction; I'll hold #110024 and #110006 pending your call.

  9. groeneai commented on Jul 16, 2026

    @groeneai
    Collaborator

    Hi @rschu1ze, gentle ping — any preference on the fix scope for the lost positive-boolean-wrapper text-index pruning (my wrapper survey above)? Happy to proceed once you confirm the direction. Thanks.

  10. rschu1ze commented on Jul 20, 2026

    @rschu1ze
    Member

    Later

  11. self-assigned this
    on Sep 8, 2026
  12. groeneai commented on Sep 23, 2026

    @groeneai
    Collaborator

    @rschu1ze The direction question can stay deferred. This is only about disposing of #110024, which has now been frozen for 72 days, and two facts from July that I should have flagged at the time make that freeze look accidental rather than intended:

    1. The hold you put on Text index looks through positive boolean wrappers #110024 at 09:02 on 13 July predates your 09:35 comment here, "I'm not against merging in the current state (haven't reviewed the code in detail yet). I just thought a more systematic gap analysis will be helpful". I posted that gap analysis five minutes later, and then volunteered to freeze the PR pending a central-fold decision. The freeze since 13 July has been mine, not yours.
    2. The sibling patch for the same defect class one layer down, Support index pruning for IS NOT DISTINCT FROM and IS TRUE #110006 (KeyCondition, IS TRUE and IS NOT DISTINCT FROM), was approved and merged by @ alexey-milovidov on 20 July and backported to 26.6. So the incremental route already landed for the primary key, minmax and partition paths, while the text index half of the same gap sat here.

    One question, and either answer works for me:

    (a) Lift hold, and I refresh #110024 against current master (it has picked up three compile errors from upstream changes since July) and land the narrow per-index fix. The central X != false / X IN (only-truthy) fold stays a separate proposal for whenever you want to pick the direction up.

    (b) You would rather not have per-index code here at all, in which case say so and I will close #110024 and leave this issue as the tracker for the central fold.

    Separately, I have corrected the changelog category on #110024 from Bug Fix to Improvement, matching #110006: results were always correct and only pruning was lost, and @ PedroTadim relabelled this issue performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

comp-text-indexfuzzperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions