Skip to content

Find both signed zeros in a key subcolumn of a bucketed Map - #125713

Merged
harikrishnan94 merged 9 commits into
ClickHouse:masterfrom
aschoube:fix-map-bucket-signed-zero-key
Oct 20, 2026
Merged

harikrishnan94 merged 9 commits into
ClickHouse:masterfrom
aschoube:fix-map-bucket-signed-zero-key

Conversation

@aschoube

@aschoube aschoube commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Related: #123834
Related: #125652
Related: #115970

A bucketed Map (map_serialization_version = 'with_buckets') puts each key into the bucket of the SipHash of its bits, and m[k] reads only the bucket of k. -0.0 = 0.0, but the bits of the two zeros differ, so m[0.0] does not find a -0.0 key in another bucket, and the other way around:

CREATE TABLE t (id UInt64, m Map(Float64, String)) ENGINE = MergeTree ORDER BY id
SETTINGS map_serialization_version = 'with_buckets', map_serialization_version_for_zero_level_parts = 'with_buckets',
    map_buckets_strategy = 'constant', map_buckets_min_avg_size = 0, max_buckets_in_map = 4;
INSERT INTO t VALUES (1, map(-0.0, 'a')), (2, map(0.0, 'b'));

SELECT id, m[0.0], m[-0.0] FROM t ORDER BY id;
-- 1		a
-- 2	b

With map_serialization_version = 'basic' both columns are a and b. It reproduces on 26.9.17.8: https://fiddle.clickhouse.com/6747c5fc-cdf8-4b4d-aa3c-f546426fee3b

The same happens for Float32 keys and for a zero inside a Tuple, an Array or a path of a JSON key. This is why 05325_bloom_filter_signed_zero from #123834 failed with the randomized map_serialization_version_for_zero_level_parts = 'with_buckets': m[0.0] = 'x' returned [3] with and without the index.

Change

DataTypeMap::getDynamicSubcolumnInfo checks whether the key holds a float zero (also inside a Tuple, an Array, a Map or a path of a JSON). For such a key in a bucketed Map, SerializationMapKeyValue reads the whole Map through SerializationMap, which restores the order of the keys from the bucket index, and then extracts the key as usual. So a map with both zeros returns the value of the first one, as with basic. Nothing changes in what is written, so existing parts work as they are. Other keys still read only their bucket.

A key with a float zero now reads all buckets and the bucket index. On 200k rows with 16 keys per map and 8 buckets in a wide part, m[3.0] opens 9 files and reads 1.7 MB, and m[0.0] opens 53 files and reads 12.5 MB. A basic table reads 2.8 MB in 6 files for either key.

Hashing -0.0 as 0.0 on write would not be enough, because existing parts already have -0.0 in its own bucket.

Test

05395_map_buckets_signed_zero_key checks m[0.0], m[-0.0], a non-zero key, Float32, Tuple, Array and JSON keys, a map with both zeros, and a filter on m[0.0]. The reference is the output with map_serialization_version = 'basic'. On master it returns:

1		a			a		a		a
2			b
3	c			c		c		c
4	e	d		d	e	d	e	e	d
m[0.0] != \'\'	[3,4]

05407_map_buckets_zero_key_read_volume checks that m[0.0] reads more than twice the compressed bytes of m[3.0], and that both return the right sums.

Locally, the new test passed 60 runs with randomized settings. 05325_bloom_filter_signed_zero with the culprit settings from the master failure (map_serialization_version_for_zero_level_parts = 'with_buckets', map_buckets_strategy = 'constant', map_buckets_min_avg_size = 1) fails without this change and passes with it, and it passed 60 randomized runs. The other Map subcolumn, Map bucket and Map bloom filter tests pass. The only failures were caused by the local hostname.

The failing master run: 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

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 bucketed Map (map_serialization_version = 'with_buckets') where m[0.0] did not find the key -0.0, and m[-0.0] did not find the key 0.0. The same applied to a zero inside a Tuple, Array or JSON key.


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

Version info

  • Merged into: 26.11.1.328-master (included in 26.11 and later)

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

Copy link
Copy Markdown
Contributor Author

@Algunenano, this is the cause of the 05325_bloom_filter_signed_zero failure for which #123834 was reverted (#123834 (comment)). The failing query returns the wrong result also without the index.

@alexey-milovidov, #115970 includes the bloom_filter part of #123834 and keeps the bucket of a key by its raw bits, because it is a part of the format of a data part, so it pins 05325_bloom_filter_signed_zero to map_serialization_version = 'basic'. This PR fixes the read of a bucketed Map for that case. It does not change the bucket of a key, so it works with both versions of the hash, and the basic pin can be removed after it is merged.

@clickhouse-gh clickhouse-gh Bot added comp-map-datatype Map datatype implementation and semantics. external Author is not a member of the ClickHouse organization labels Oct 14, 2026
@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label Oct 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [21964f2]

Summary: ✅


AI Review

Summary

This PR fixes signed-zero lookups in bucketed Map columns by reading all buckets when the requested key contains a floating-point zero, preserving first-match ordering without changing stored data. No unresolved findings at head 21964f2d4988fa8e4c67646c1fa3c88e60f273e6. The earlier JSON key finding is addressed. Both added regressions pass in Fast test; 200 completed local comparisons also matched full-map extraction across shared streams, block boundaries, and complex values.

Missing context / blind spots
  • ⚠️ Broader CI, including sanitizer builds and randomized tests, is still pending or running. Final reports are needed to confirm those configurations.
Final Verdict

Status: ✅ Approve. No review changes requested; remaining CI must pass.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 14, 2026
Comment thread src/DataTypes/DataTypeMap.cpp Resolved
The check for a float zero in a key of `m[k]` did not enter an `Object`, so a
`JSON` key with a zero in a path read only one bucket. Use `anyFieldSatisfies`,
which enters `Tuple`, `Array`, `Map` and `Object`.

Related: ClickHouse#125713 (comment)
@harikrishnan94 harikrishnan94 self-assigned this Oct 15, 2026
@aschoube

Copy link
Copy Markdown
Contributor Author

@harikrishnan94 could I get a review on this PR

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
harikrishnan94 and others added 5 commits October 19, 2026 09:09
The local `isFloatZero` duplicated `isFloatZeroField` from `Field.h`, which
came with master after this branch was started.

The comments of `getDynamicSubcolumnInfo` now say that a key with a float zero
reads the whole `Map`, and split the long sentence about the bucket of a key.

Co-authored-by: harikrishnan.prabakaran <[email protected]>
The only caller, `DataTypeMap::getDynamicSubcolumnInfo`, passes
`read_whole_map` explicitly.

Co-authored-by: harikrishnan.prabakaran <[email protected]>
…le `Map`

With `read_whole_map`, `map_nested_serialization` and `nested_type` hold the
whole `Map` serialization and the `Map` type, and all buckets are read. The
comments still described `Array(Tuple(K, V))` and a read of one bucket.

Co-authored-by: harikrishnan.prabakaran <[email protected]>
The test compares `ReadCompressedBytes` of `m[3.0]`, which reads one bucket,
and `m[0.0]`, which reads all buckets. Both lookups also print their sums, so
a wrong result fails the test too. The tests of the results are in
`05395_map_buckets_signed_zero_key`.

Co-authored-by: harikrishnan.prabakaran <[email protected]>

@harikrishnan94 harikrishnan94 left a comment •

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.

@aschoube Thanks for the PR! LGTM

@aschoube

Copy link
Copy Markdown
Contributor Author

@harikrishnan94 I have rebased the branch with master, staleness tests were failing, can you please reapprove

…o key of a bucketed `Map`

CI picks `serialization_info_version` at random. With `basic`, a `Map` is written without buckets whatever `map_serialization_version` says. Then `m[3.0]` and `m[0.0]` read the same bytes, and `05407_map_buckets_zero_key_read_volume` failed in about half of the runs. `05395_map_buckets_signed_zero_key` did not test a bucketed `Map` in those runs either.

Set `serialization_info_version = 'with_types'` in the tables of both tests, as the other tests of a bucketed `Map` do.

Co-authored-by: Cursor <[email protected]>

@harikrishnan94 harikrishnan94 left a comment •

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.

Thanks @aschoube. No action item is needed from your side. Once CI run is complete and it becomes green the PR will be automatically merged

@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 21964f2d4988fa8e4c67646c1fa3c88e60f273e6 was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "file", "type": "LowCardinality(String)" …).

See the job log for details.

@aschoube

Copy link
Copy Markdown
Contributor Author

@harikrishnan94 Are you looking into the flakiness or do you want me to check this ?

With parallel replicas, the replicas split the reading, so `ReadCompressedBytes` of a query varies. In 2 of 150 runs in `Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel)`, `m[0.0]` read only 4068 and 1839 bytes and the check that it reads more than twice the bytes of `m[3.0]` failed.

Set `enable_parallel_replicas = 0` in the first `SET` of the test.

Co-authored-by: Cursor <[email protected]>
@harikrishnan94
harikrishnan94 added this pull request to the merge queue Oct 20, 2026
Merged via the queue into ClickHouse:master with commit f39fc58 Oct 20, 2026
179 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 20, 2026
alexey-milovidov added a commit that referenced this pull request Oct 22, 2026
…e lookup of a zero key in all buckets

The previous commit was wrong: the `CH Inc sync` failure
(ClickHouse/clickhouse-private#70376) is not specific to `SharedMergeTree`.
It reproduces locally after merging `master`, which now reads all buckets of a bucketed `Map`
for a key with a floating point zero (#125713),
so `m[0.]` finds the key `-0.`. Remove the tag and update the comment and the reference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov pushed a commit to lazerg/ClickHouse that referenced this pull request Nov 17, 2026
`count(DISTINCT)`, `uniq*`, `SELECT DISTINCT`, `arrayDistinct` and hash
`JOIN` identify a row by the SipHash of `IColumn::updateHashWithValue`
alone. `ColumnObject::updateHashWithValue` hashed every path without its
size and every object without its number of paths, and
`ColumnDynamic::updateHashWithValue` hashed the type name without its
size. Different values could therefore give the same byte stream:
the paths of adjacent objects of an `Array(JSON)` ran together
(`[{a},{b,c}]` vs `[{a,b},{c}]`, ClickHouse#129458), a path ran into the type name
after it (`x` + `UInt64` vs `xU` + `Int64`), and in `Dynamic` a type
name ran into the value (`Date` + `32` vs `Date32`). The framing dates
from ClickHouse#69203; `GROUP BY` was correct because it keys on the arena
serialization, which writes the count and every size.

Now the object hash starts with the number of paths, as
`serializeDynamicPathsAndSharedDataIntoArena` computes it, every path is
preceded by its size, and a non-NULL `Dynamic` value hashes a zero tag,
the size of the type name and the name. The tag differs from the
`NULL_DISCRIMINATOR` hashed for NULL. One helper,
`ColumnDynamic::updateHashWithTypeName`, is used for dynamic paths and
for shared data, so the hash stays independent of where a path is
stored.

Bucketed `Map` parts place a key in bucket `SipHash(key) % N`, and a
`m[key]` lookup reads only that bucket, so the new hash would miss keys
with `JSON` or `Dynamic` written by an older version, and an older
version would miss keys written by this one. Such a `Map` is now read
whole (the guard ClickHouse#125713 added for float zeros) and written in one
bucket, which every version reads whole.

Hash values of `JSON` and `Dynamic` rows change, so `uniq*` states of
such arguments stored by older versions do not deduplicate against new
ones. `sipHash64` of `JSON` uses `serializeForHashCalculation` and is not
changed.

Closes ClickHouse#129458

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 comp-map-datatype Map datatype implementation and semantics. 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.

5 participants