Skip to content

Fix set skip index throwing "It's a bug!" on non-integer WHERE atoms - #105384

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-105355-set-skip-index-float-bitwrapperfunc
May 20, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-105355-set-skip-index-float-bitwrapperfunc

Conversation

@groeneai

@groeneai groeneai commented May 19, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #105355.

MergeTreeIndexConditionSet::traverseDAG wraps every WHERE atom in __bitWrapperFunc to coerce it to a BoolMask. __bitWrapperFunc is defined only for integer arguments and throws BAD_ARGUMENTS ("It's a bug!") on Float, BFloat16, etc. Queries such as WHERE c0 + 0.1 against a TYPE set(N) skip index hit that throw at execution time.

Check the atom result type (after stripping Nullable) before wrapping. If it is not integer, fall back to UNKNOWN_FIELD so the index does not prune granules and the query goes through the regular filter path. This matches the behavior the user gets when the index is absent.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix BAD_ARGUMENTS exception "It's a bug! Only integer types are supported by __bitWrapperFunc" thrown when a SELECT with a TYPE set(N) skip index has a WHERE atom whose result type is Float, BFloat16, or any other non-integer type (for example WHERE c0 + 0.1 or WHERE log(c0)). The skip index now falls back to the regular filter path in that case.

Version info

  • Merged into: 26.5.1.866
  • Backported to: 26.3.13.19

`MergeTreeIndexConditionSet::traverseDAG` wrapped every WHERE atom with
`__bitWrapperFunc` to coerce it to a `BoolMask`. `__bitWrapperFunc` is
defined only for integer arguments and throws `BAD_ARGUMENTS` with the
internal "It's a bug!" message when called with `Float`, `BFloat16`,
or other non-integer types.

For queries like `WHERE c0 + 0.1`, `WHERE log(c0)`, or a direct
`Float` column predicate, this turned a normal full-scan fallback into
a user-visible exception whenever a `TYPE set(N)` skip index was
present on the table.

Check the atom result type (after stripping `Nullable`) before
wrapping. If it is not integer, fall back to `UNKNOWN_FIELD` so the
index does not prune granules and the query goes through the regular
filter path.

Closes ClickHouse#105355
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

a) Deterministic repro: Yes. The 5-line repro from #105355 (CREATE TABLE t (c0 UInt32, INDEX idx c0 TYPE set(100)) ENGINE = MergeTree() ORDER BY c0; INSERT INTO t VALUES (1); SELECT c0 FROM t WHERE c0 + 0.1;) throws BAD_ARGUMENTS on every run before the fix. Same shape for WHERE log(c0), WHERE c0 + toBFloat16(0.1), and WHERE f (direct Float32 column).

b) Root cause: MergeTreeIndexConditionSet::traverseDAG wraps every WHERE atom with __bitWrapperFunc to coerce it to a BoolMask. __bitWrapperFunc::apply is templated on the column native type A and contains if constexpr (!is_integer<A>) throw "It's a bug!". When the atom result type is Float, BFloat16, or any other non-integer, the wrapper is added to the index DAG and throws at execution time, even though the same query without the index works through the regular filter path.

c) Fix matches root cause: Check WhichDataType(removeNullable(atom_node_ptr->result_type)).isInteger() before wrapping with __bitWrapperFunc. For non-integer atom result types, fall back to UNKNOWN_FIELD (the same sentinel the function already uses for atoms it cannot represent). This precisely targets the wrapper site and does not weaken the index for integer atoms.

d) Test intent preserved + new test added: No existing tests modified. New regression test 04258_set_skip_index_float_where_issue_105355.sql covers Float64 atoms from arithmetic (c0 + 0.1), from log and -log, from BFloat16 arithmetic, and from a direct Float32 INPUT, plus sanity checks that integer atoms (c0 = 2, c0 > 0) still use the index. Locally 50/50 passes with full randomization. All 10 pre-existing set-skip-index tests (00907_*, 00979_*, 00997_*, 01450_*, 01917_*, 02131_*, 02499_*, 02670_*) still pass.

e) Both directions demonstrated:

  • Before fix: Code: 36. DB::Exception: It's a bug! Only integer types are supported by __bitWrapperFunc on the 5-line repro and on each of the four variants above.
  • After fix: every variant returns the row that matches the filter (or empty for log(1) == 0), consistent with the no-index baseline.

f) Fix is general, not a narrow patch: The check is at the single wrap site in traverseDAG, where __bitWrapperFunc is added for INPUT/FUNCTION/COLUMN atoms. The same bug pattern does not exist in operatorFromDAG (it only wires __bitBoolMaskAnd/__bitBoolMaskOr/__bitSwapLastTwo over already-wrapped children that are guaranteed UInt8). No other call site adds __bitWrapperFunc to a user-supplied predicate.

Session: cron:clickhouse-ci-task-worker:20260519-213000.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rschu1ze @alexey-milovidov for review (comp-skip-index, MergeTreeIndexSet wrapping logic). cc @PedroTadim — closes #105355 you tagged me on.

Test-only change to verify: just runs the regression test added in this PR. C++ change is a single guarded fallback in MergeTreeIndexConditionSet::traverseDAG (~6 effective lines).

@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fdbffb4]

Summary: ✅


AI Review

Summary

This PR fixes a real correctness regression in set skip index evaluation: non-integer WHERE atoms were being wrapped by __bitWrapperFunc, which raised a user-visible BAD_ARGUMENTS exception. The new type guard in MergeTreeIndexConditionSet::traverseDAG falls back to UNKNOWN_FIELD for non-integer atom result types, preserving correctness by disabling unsafe pruning and letting regular filtering evaluate the predicate. The added regression test covers multiple non-integer atom shapes and keeps integer-atom sanity checks. I found no unresolved correctness, safety, or compatibility issues in the current PR state.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (10/10) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this May 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 20, 2026
Merged via the queue into ClickHouse:master with commit 4290d68 May 20, 2026
165 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 20, 2026
@Avogar Avogar added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label May 27, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label May 27, 2026
robot-ch-test-poll3 added a commit that referenced this pull request May 27, 2026
Cherry pick #105384 to 25.8: Fix set skip index throwing "It's a bug!" on non-integer WHERE atoms
robot-clickhouse added a commit that referenced this pull request May 27, 2026
robot-ch-test-poll3 added a commit that referenced this pull request May 27, 2026
Cherry pick #105384 to 26.3: Fix set skip index throwing "It's a bug!" on non-integer WHERE atoms
robot-clickhouse added a commit that referenced this pull request May 27, 2026
robot-ch-test-poll3 added a commit that referenced this pull request May 27, 2026
Cherry pick #105384 to 26.4: Fix set skip index throwing "It's a bug!" on non-integer WHERE atoms
robot-clickhouse added a commit that referenced this pull request May 27, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 27, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request May 29, 2026
The first `SELECT count()` is the canonical bug trigger from ClickHouse#105938 /
ClickHouse#105384. As written, the stateless runner randomizes
`use_skip_indexes_on_data_read` and `use_skip_indexes_for_disjunctions`
across runs. When either disables the partial-disjunction skip-index
path in `MergeTreeDataSelectExecutor` / `ReadFromMergeTree`, the buggy
`MergeTreeIndexConditionSet::traverseDAG` wrapping is never exercised
and a pre-fix binary returns the same `100` as a post-fix binary, so
the regression would silently slip through.

Pin the four settings that gate the buggy path on that query directly
via per-query `SETTINGS`, so the test always exercises the fix:

  use_skip_indexes = 1
  use_skip_indexes_on_data_read = 0
  use_skip_indexes_for_disjunctions = 1
  use_query_condition_cache = 0

The other two queries are intentionally left alone: the second forces
`use_skip_indexes = 0` as the no-index control, and the third uses
`assumeNotNull(n)` so the atom result type is no longer
`LowCardinality(Nullable(UInt8))` and the bug cannot trigger.

Addresses bot review on PR ClickHouse#105990.
alexey-milovidov added a commit that referenced this pull request May 31, 2026
Backport #105384 to 26.3: Fix set skip index throwing "It's a bug!" on non-integer WHERE atoms
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…5-set-skip-index-float-bitwrapperfunc

Fix set skip index throwing "It's a bug!" on non-integer WHERE atoms
alexbakharew pushed a commit to groeneai/ClickHouse that referenced this pull request Jun 2, 2026
…issue ClickHouse#105938)

Locks down the fix from ClickHouse#105384 against issue ClickHouse#105938, where a `WHERE` clause
ORing a `set(N)` skip index predicate on a `LowCardinality(Nullable(String))`
column with an `ngrambf_v1` predicate on a different column silently returned
zero rows when the `LowCardinality(Nullable)` column contained only NULL
values.

`MergeTreeIndexConditionSet::traverseDAG` used to wrap every WHERE atom with
`__bitWrapperFunc`. For `LowCardinality(Nullable(String)) = String` the atom
result type is `LowCardinality(Nullable(UInt8))`, which the pre-fix wrapper
coerced to `0` for NULL values, so the set index incorrectly concluded the
granule could not match and the ORed `ngrambf_v1` side could not override the
decision. PR ClickHouse#105384 detects that the atom result type is not integer after
stripping `Nullable` and falls back to `UNKNOWN_FIELD`, keeping the granule.

The new stateless test reproduces the smaller form reported by @den-crane on
issue ClickHouse#105938 and asserts that the result with `use_skip_indexes = 1` matches
the result with `use_skip_indexes = 0` (and the `assumeNotNull` workaround
referenced in the issue). Verified locally that the test fails on a binary
without ClickHouse#105384 (first SELECT returns `0`) and passes on master HEAD.
alexbakharew pushed a commit to groeneai/ClickHouse that referenced this pull request Jun 2, 2026
The first `SELECT count()` is the canonical bug trigger from ClickHouse#105938 /
ClickHouse#105384. As written, the stateless runner randomizes
`use_skip_indexes_on_data_read` and `use_skip_indexes_for_disjunctions`
across runs. When either disables the partial-disjunction skip-index
path in `MergeTreeDataSelectExecutor` / `ReadFromMergeTree`, the buggy
`MergeTreeIndexConditionSet::traverseDAG` wrapping is never exercised
and a pre-fix binary returns the same `100` as a post-fix binary, so
the regression would silently slip through.

Pin the four settings that gate the buggy path on that query directly
via per-query `SETTINGS`, so the test always exercises the fix:

  use_skip_indexes = 1
  use_skip_indexes_on_data_read = 0
  use_skip_indexes_for_disjunctions = 1
  use_query_condition_cache = 0

The other two queries are intentionally left alone: the second forces
`use_skip_indexes = 0` as the no-index control, and the third uses
`assumeNotNull(n)` so the atom result type is no longer
`LowCardinality(Nullable(UInt8))` and the bug cannot trigger.

Addresses bot review on PR ClickHouse#105990.
alexey-milovidov added a commit that referenced this pull request Jun 3, 2026
Backport #105384 to 26.4: Fix set skip index throwing "It's a bug!" on non-integer WHERE atoms
thevar1able added a commit that referenced this pull request Jun 18, 2026
`MergeTreeIndexConditionSet::traverseDAG` wraps each WHERE atom with
`__bitWrapperFunc` to coerce it to a `BoolMask`. `__bitWrapperFunc` is
defined only for integer arguments, so #105384 started wrapping an atom
only when `WhichDataType(removeNullable(atom->result_type)).isInteger()`
and otherwise fell back to `UNKNOWN_FIELD` (the index keeps every
granule and the query goes through the regular filter path). That fix
correctly handles `Float` atoms (#105355) and `LowCardinality(Nullable)`
atoms (#105938), where wrapping would throw "It's a bug!" or coerce a
NULL granule value to `0` and wrongly prune.

But it strips only `Nullable`, not `LowCardinality`. Over a non-nullable
`LowCardinality(String)` column, function predicates such as `hasToken`,
`LIKE` or `match` produce `LowCardinality(UInt8)`, which is not "integer",
so every atom became `UNKNOWN_FIELD` and the `set` index stopped pruning
entirely -- turning an index lookup into a full scan with large read
amplification. A plain `String` column yields `UInt8` and was unaffected,
which is why the regression was specific to `LowCardinality`.

Look through `LowCardinality` before the integer check. The wrapper is
stripped only for `LowCardinality` types and, unlike the plain case,
`Nullable` is not stripped afterwards, so `LowCardinality(UInt8)` is
wrapped and prunes again while `LowCardinality(Nullable(...))` keeps
falling back to `UNKNOWN_FIELD` and the #105938 fix is preserved.
`__bitWrapperFunc` accepts `LowCardinality(UInt8)`, which is how these
predicates pruned before #105384.

Caused by: #105384
Closes: #107881

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…5384

Backport ClickHouse#105384 to 26.4: Fix set skip index throwing "It's a bug!" on non-integer WHERE atoms
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
STOR: set(N) skip index with a non-integer WHERE atom (Float etc.) -> exc BAD_ARGUMENTS `__bitWrapperFunc`.
Effect in the production build a394e92: wrong/failing (the upstream test fails on it and passes on official 26.8.8.8; evidence/fix_ci_20260925/upstream_repro/residual/105384.PROD263.txt).
Upstream patch unchanged, with its test(s): 04258_set_skip_index_float_where_issue_105355.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

__bitWrapperFunc internal assertion ("It's a bug!") on Float WHERE predicate against a TYPE set(N) skip index

6 participants