Skip to content

Look up both signed zeros in a bloom_filter index - #123834

Merged
Ergus merged 2 commits into
ClickHouse:masterfrom
aschoube:fix-bloom-filter-signed-zero
Oct 7, 2026
Merged

Ergus merged 2 commits into
ClickHouse:masterfrom
aschoube:fix-bloom-filter-signed-zero

Conversation

@aschoube

@aschoube aschoube commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #123744
Related: #117722

A bloom_filter index on a float column skipped the granules holding the other signed zero:

CREATE TABLE t (id UInt64, f Float64, INDEX i f TYPE bloom_filter GRANULARITY 1)
ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 1;
INSERT INTO t VALUES (1, -0.0), (2, 0.5), (3, 0.0);

SELECT arraySort(groupArray(id)) FROM t WHERE f = 0;                                -- [3]
SELECT arraySort(groupArray(id)) FROM t WHERE f = 0 SETTINGS use_skip_indexes = 0;  -- [1,3]

It also happens on 26.4.5.143, and for Float32, has in both directions, hasAny, hasAll, mapContainsKey, mapContainsValue, m[k] = v and m[k] IN (...).

Cause

-0.0 = 0.0, but the index stores BloomFilterHash of each value's bits, and the bits of the two zeros differ. MergeTreeIndexConditionBloomFilter::traverseTreeEquals hashed the converted constant the same way, so a zero constant looked up only one of the two zeros. bloomFilterHashDomainMatches already excludes floats for this reason, but only on the arrayJoin paths.

Change

For a float zero constant, the lookup now also includes the hash of the other zero:

  • equals, has(array_column, x), mapContainsKey, mapContainsValue and the other has over a Map, m[k] = v and m[k] IN (...) use hashWithFieldAndOtherZero.
  • has(constant_array, column) and hasAny/hasAll use hashWithColumnAndOtherZeros, which adds the other zero for each zero element.

These predicates already match a granule that may hold any of their hashes (maybeTrueOnBloomFilter with match_all = false), so a zero now matches a granule with either zero. Nothing changes in what is written, so existing parts work as they are. Normalizing -0.0 when hashing would not be enough, because existing parts already store hash(-0.0).

hasAll needs every hash in the granule, but for a zero element one of the two is enough, so hasAll with a zero element does not use the index. IN compares bits (f IN (0.0) does not match -0.0, with or without the index), so it is not changed.

The index still skips most granules for a zero, since it looks up two hashes instead of one. On 100 rows with index_granularity = 1, the index selects 10 of 100 granules for f = 0 with this PR (4 before, which missed -0.0), and 8 of 100 for f = 42 both before and after.

Test

05325_bloom_filter_signed_zero compares every function above with and without the index, on Float64, Float32, Array(Float64) and Map(Float64, String). It also covers mapContainsValue over a mapValues index, m[0.0] IN (...) over a mapKeys index, IN and a non-zero constant, and checks with force_data_skipping_indices and max_rows_to_read that the index still prunes for 0 and 42. On an unfixed build the eleven zero queries miss rows. The queries with m[0.0] pin optimize_functions_to_subcolumns = 1: with 0, m[0.0] on a Map(Float64, String) throws ILLEGAL_TYPE_OF_ARGUMENT even without an index, which is unrelated to this change. With the fix all pass, also with --test-runs 5 under randomized settings, apart from runs where the randomizer enabled parallel replicas: my local server does not define the parallel_replicas cluster (CLUSTER_DOESNT_EXIST), which CI has.

I ran the 62 existing stateless tests matching bloom on servers started from both builds. No failure is caused by the change: the tests that failed only with the fix passed when rerun, or failed on the same missing cluster.

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

Fixed a bloom_filter index on a float column skipping rows with the other signed zero, for example -0.0 for WHERE f = 0. It affected equals, has, hasAny, hasAll, mapContainsKey, mapContainsValue and map element comparisons, including m[k] IN (...).


Workflow [PR]
Sync PR [sync-upstream/pr/123834]

Version info

  • Merged into: 26.10.1.1757-master (included in 26.10 and later)

`-0.0 = 0.0`, but a `bloom_filter` index holds the hash of each value's
bits, which differ for the two zeros, so `f = 0` skipped the granules
holding `-0.0`. The same happened for `has` in both directions, `hasAny`,
`hasAll`, `mapContainsKey` and `m[k] = v`.

For a float zero, the lookup now also includes the hash of the other zero.
These predicates match a granule that may hold any of their hashes, so
the index format does not change and existing parts work. `hasAll` needs
every hash, so it does not use the index when the array has a zero. `IN`
compares bits and is unchanged.

Closes: ClickHouse#123744
@clickhouse-gh clickhouse-gh Bot added comp-skip-index Skipping indexes (minmax, set, bloom) and their evaluation. external Author is not a member of the ClickHouse organization labels Oct 4, 2026
@Ergus Ergus added the can be tested Allows running workflows for external contributors label Oct 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [728c0ca]

Summary: ✅


AI Review

Summary

This PR fixes the bloom_filter false-negative on signed floating-point zero by probing both +0.0 and -0.0 for equality-style lookups, and it adds focused stateless coverage for the affected scalar, array, and Map cases. I do not see a remaining correctness issue in the current diff, the previously raised m[0.0] IN (...) coverage gap is now closed, and the Praktika report for 2026-10-05 is all green.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 728c0ca.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 59/62 (95.16%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 5, 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.

On a master build with mv Map(String, Float64), a mapValues(mv) index and the row map('a', -0.0) mapContainsValue(mv, 0.0) returns [] with the index and [1] without.

This branch now uses hashWithFieldAndOtherZero, so the PR fixes it.

I will approve, but please add the m[k] IN and mapContainsValue queries and mention mapContainsValue in the changelog entry.

ColumnPtr hashes = hashWithColumnAndOtherZeros(actual_type, column, has_float_zero);

/// `hasAll` needs one of the two zeros in the granule, while `FUNCTION_HAS_ALL` needs every hash.
if (function_name == "hasAll" && has_float_zero)

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.

I have a performance suggestion, because hasAll with a zero element could keep pruning instead of dropping the index.

  • mayBeTrueOnGranuleANDs the entries inout.predicate, and each entry is checked with any-of under FUNCTION_HAS_ANY. That is exactly the semantics needed by hasAll`.
  • Emit FUNCTION_HAS_ANY with one predicate entry per element:
    • a non-zero element gets its single hash;
    • a zero element gets both zero hashes.
  • hasAll(a, [0.0, 42.0]) would then still skip granules without 42. Today it reads every granule.

Fine as a follow-up ;)

@aschoube aschoube Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in the draft #124045: one FUNCTION_HAS_ANY entry per element, with both hashes for a zero. hasAll(a, [0.0, 42.0]) now selects 2 of 103 granules, where before it read all of them. It stays a draft until this PR is merged.

return false;

out.predicate.emplace_back(std::make_pair(position, BloomFilterHash::hashWithField(actual_type.get(), converted_field)));
out.predicate.emplace_back(std::make_pair(position, hashWithFieldAndOtherZero(actual_type, converted_field)));

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.

IIUC this fix also covers mapContainsValue, but the description, changelog and test leave it out.

@aschoube aschoube Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 728c0ca: mapContainsValue(mv, 0.0) over a mapValues index, and m[0.0] IN ('x') over a mapKeys index, are now in 05325_bloom_filter_signed_zero. On master both return [3] with the index and [1,3] without it. I also mentioned mapContainsValue in the description and the changelog. The hasAll pruning and the float arrayJoin paths are in the draft follow-up #124045.

@Ergus

Ergus commented Oct 5, 2026

Copy link
Copy Markdown
Member

The arrayJoin(col) = c and arrayJoin(col) IN paths still turn the index off for floats, and the comment gives -0.0 as the only reason. With the new helpers, the equals arm could use the index for floats. Constant-to-element conversion precision needs checking first.

With this change there is a remedy for the float exclusion in bloomFilterHashDomainMatches.

What if we do it as a folloup PR?

@Ergus Ergus self-assigned this Oct 5, 2026
…ter`

Address review: add the `mapContainsValue` query over a `mapValues` index
and `m[0.0] IN (...)` over a `mapKeys` index. Both missed the row with
`-0.0` on master with the index and are fixed by this PR. `mv['a'] IN
(0.0)` stays bit-exact.

The queries that use `m[0.0]` pin `optimize_functions_to_subcolumns = 1`:
with 0, `m[0.0]` on a `Map(Float64, String)` throws
`ILLEGAL_TYPE_OF_ARGUMENT` even without an index, which the randomized
settings hit.
@aschoube

aschoube commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

The arrayJoin(col) = c and arrayJoin(col) IN paths still turn the index off for floats, and the comment gives -0.0 as the only reason. With the new helpers, the equals arm could use the index for floats. Constant-to-element conversion precision needs checking first.

With this change there is a remedy for the float exclusion in bloomFilterHashDomainMatches.

What if we do it as a folloup PR?

Raised a follow-up PR covering the remaining aspects: #124045

@aschoube

aschoube commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@Ergus I addressed the review in 728c0ca. I added the mapContainsValue and m[k] IN (...) tests and mentioned mapContainsValue in the changelog. The m[0.0] IN ('x') case also covers the traverseTreeIn map-key branch the bot pointed out. The hasAll pruning and the float arrayJoin paths are in the draft follow-up #124045. Could you take another look?

@aschoube
aschoube requested a review from Ergus October 5, 2026 15:38
@clickhouse-gh

clickhouse-gh Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 728c0ca69 with master b5d7e18bb (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 15 s compile time in total, 7 of them have a recent master baseline.

Job report

@aschoube

aschoube commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@Ergus Gentle nudge on the PR : )

@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.

LGTM.

Just made two final totally optional comments. Please confirm if you like to address them or keep this as is and I will merge then.

if (!isFloat(removeLowCardinalityAndNullable(type)))
return hashes;

MutableColumnPtr result;

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.

I think that this is ok to merge.
Just a suggestion:

The loop appends the other-zero hash once per zero element.

hasAny(a, [0.0, 0.0, 0.0]) appends three identical hashes, and every granule check iterates all of them in maybeTrueOnBloomFilter.

If the array already holds both 0.0 and -0.0, no extra hash is needed, but the loop still appends two. Track the signs seen instead and append at most one hash per missing sign after the loop:

WHat about something like (untested)

bool has_positive = false;
bool has_negative = false;
for (size_t i = 0; i < column->size(); ++i)
{
    Field value = (*column)[i];
    if (!isFloatZero(type, value))
        continue;
    (std::signbit(value.safeGet<Float64>()) ? has_negative : has_positive) = true;
}
has_float_zero = has_positive || has_negative;
if (!has_float_zero || (has_positive && has_negative))
    return hashes;

auto result = IColumn::mutate(std::move(hashes));
assert_cast<ColumnUInt64 &>(*result).insertValue(
    BloomFilterHash::hashWithField(type.get(), has_positive ? Field(-0.0) : Field(0.0))->getUInt(0));
return result;


static Field otherFloatZero(const Field & zero)
{
return std::signbit(zero.safeGet<Float64>()) ? Field(0.0) : Field(-0.0);

@Ergus Ergus Oct 7, 2026 •

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.

Why not something simpler: Field(-zero.safeGet<Float64>()) ?

/// hashes matches a granule that may hold any of them, except for `FUNCTION_HAS_ALL`.
static ColumnPtr hashWithFieldAndOtherZero(const DataTypePtr & type, const Field & field)
{
ColumnPtr hash = BloomFilterHash::hashWithField(type.get(), field);

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.

Just a cosmetic observation:

I see that hashWithField allocates a ColumnConst over a one-row ColumnUInt64, and the helper does this twice only to read getUInt(0) from each.

An UInt64-returning variant in BloomFilterHash would avoid both allocations.

I would not add API surface for it unless the follow-up PR for hasAll also needs the raw hash (which it likely does) since it emits one predicate entry per element.

@aschoube

aschoube commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

🕵 @Ergus Thanks for the review! I'd like to keep this PR as-is so the fix can land and be backported. I'll pick up all three suggestions in #124045, which will likely need the raw UInt64 hash anyway. Feel free to merge this one.

@Ergus
Ergus added this pull request to the merge queue Oct 7, 2026
aschoube added a commit to aschoube/ClickHouse that referenced this pull request Oct 7, 2026
Suggestions from the review of ClickHouse#123834:

- `otherFloatZero` negates the zero instead of branching on its sign.
- `BloomFilterHash::getFieldHash` returns the hash of a `Field` as `UInt64`,
  and `hashWithField` wraps it in a `ColumnConst`. The signed-zero helpers use
  it instead of building a `ColumnConst` per hash and reading it back with
  `getUInt`.
- `hashWithColumnAndOtherZeros` adds the hash of a zero only when the constant
  array has the other zero but not this one, at most once, instead of once per
  zero element. `has_float_zero` stays set when both zeros are present, so
  `hasAll` still takes the per-element path.
- `hasAll` with a zero element adds the predicate entry for zeros once, not
  once per zero element.

`05327_bloom_filter_signed_zero_has_all_array_join` now also covers both zeros
and repeated zeros in the constant array of `hasAny` and `hasAll`.

Related: ClickHouse#123834

Co-authored-by: Copilot <[email protected]>
Merged via the queue into ClickHouse:master with commit b7ce5f8 Oct 7, 2026
294 of 295 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 7, 2026
alexey-milovidov added a commit that referenced this pull request Oct 7, 2026
Conflict in `MergeTreeIndexBloomFilter.cpp`: master (#123834) added
`hashWithFieldAndOtherZero` / `hashWithColumnAndOtherZeros` to probe the
hash of the other signed zero. With this PR both zeros hash the same, and
`hashWithField` / `addLegacyNegativeZeroProbes` already add the raw-bit
hash of `-0.` for parts written by older versions, so master's helpers are
replaced by this PR's mechanism (they would have dropped the legacy probe).
`hasAll` with a zero drops the zero probes instead of disabling the index.

Conflict in `ColumnsHashing.h`: keep both the negative zero
canonicalization and the new `LowCardinality` cache initialization.
alexey-milovidov added a commit that referenced this pull request Oct 7, 2026
The test came from master (#123834) and pinned that `IN` compares the bits
of floating point values. With this PR `-0. IN (0.)` is true, like `=`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Algunenano

Copy link
Copy Markdown
Member

alexey-milovidov added a commit that referenced this pull request Oct 13, 2026
…ash-keys

Master reverted #123834 (`Look up both signed zeros in a bloom_filter index`) in #125652.
This branch had already replaced its lookup of both zeros with the canonicalized hashes and
the legacy probes of `BloomFilterHash`, so the conflicts in `MergeTreeIndexBloomFilter.cpp`
are resolved in favor of this branch, and the test `05325_bloom_filter_signed_zero`, which
covers this behavior, is kept.
alexey-milovidov added a commit that referenced this pull request Oct 13, 2026
With the randomized `map_serialization_version_for_zero_level_parts = 'with_buckets'`,
`m[0.0]` on a `Map(Float64, String)` read only the bucket of the positive zero and missed
the row with the key `-0.`, with and without the index. The bucket of a key is chosen by
the raw bits of the value, which is a part of the format of a data part, so this is not
about the `bloom_filter` index. This is why the test failed on master and #123834 was
reverted: #123834 (comment)

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=40fc4717e5517a27aa1a01fb9c78ca91a2bb8c40&name_0=MasterCI&name_1=Stateless+tests+%28arm_binary%2C+ParallelReplicas%2C+s3+storage%2C+parallel%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
clickhouse-gh Bot pushed a commit that referenced this pull request Oct 14, 2026
Edited by the nightly changelog CI job following .claude/skills/edit-changelog/SKILL.md.

Changelog-revert: 125458 125455 Revert "Revert "Iceberg variant type""
Changelog-revert: 125463 110114 Revert "Push filters below WindowStep on PARTITION BY columns"
Changelog-revert: 125569 116724 Revert "Verify the server certificate hostname on outbound TLS connections"
Changelog-revert: 125652 123834 Revert "Look up both signed zeros in a bloom_filter index"
Changelog-deleted-entry: 116724 [Backward Incompatible Change] [section] * Outbound TLS connections now verify that the server certificate names the host that was connected to. This affects `clickhouse-client --secure`, `remoteSecure`, `Distributed` over the secure native protocol, secure `Keeper` connections, and HTTPS clients such as `s3`, `url`, HTTP dictionaries, and JWT token endpoints. Connecting to an endpoint whose certificate does not name it, for example an IP address with no matching SAN, an S3-compatible gateway fronted by a different name, or an internal CA with missing SANs, now fails with `Unacceptable certificate`. Set `openSSL.client.extendedVerification` to `false` to restore the previous behavior, or pass `--tls-sni-override` to name the host the certificate was issued for. Loopback addresses stay exempt unless `verificationMode` is `strict`, and the default for `openSSL.server.extendedVerification` is unchanged. [#116724](#116724) ([Groene AI](https://github.com/groeneai)).
Changelog-deleted-entry: 123834 [Bug Fix (user-visible misbehavior in an official stable release)] [section] * Fixed a `bloom_filter` index on a float column skipping rows with the other signed zero, for example `-0.0` for `WHERE f = 0`. It affected `equals`, `has`, `hasAny`, `hasAll`, `mapContainsKey`, `mapContainsValue`, and map element comparisons, including `m[k] IN (...)`. [#123834](#123834) ([Ashish Kumar Choubey](https://github.com/aschoube)).
aschoube added a commit to aschoube/ClickHouse that referenced this pull request Oct 18, 2026
This reverts commit cba98e5, which reverted ClickHouse#123834.
The helpers keep the versions of this branch. `05325_bloom_filter_signed_zero` pins `basic` `Map`
serialization: a bucketed `Map` does not find a `-0.0` key by `m[0.0]`, which is what failed with
randomized `map_serialization_version_for_zero_level_parts = 'with_buckets'`, see
ClickHouse#125713.

Related: ClickHouse#125652
Dasmat13 pushed a commit to Dasmat13/ClickHouse that referenced this pull request Oct 20, 2026
A bucketed `Map` puts a key into the bucket of the hash of its bits, and `m[k]` read only the
bucket of `k`, so `m[0.0]` did not find a `-0.0` key in another bucket. A key with a float zero,
also inside a `Tuple` or an `Array`, now reads the whole `Map`.

This made `05325_bloom_filter_signed_zero` fail with `map_serialization_version_for_zero_level_parts = 'with_buckets'`:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=40fc4717e5517a27aa1a01fb9c78ca91a2bb8c40&name_0=MasterCI&name_1=Stateless+tests+%28arm_binary%2C+ParallelReplicas%2C+s3+storage%2C+parallel%29

Related: ClickHouse#123834
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 comp-skip-index Skipping indexes (minmax, set, bloom) and their evaluation. external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default 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.

bloom_filter index on float column skips granules holding -0.0 vs 0.0

4 participants