Repository navigation
Conversation
GCD pre-processing was using signed values directly, so negative inputs could degrade divisor discovery and perform worse than expected on signed integer-backed data. The fix computes the divisor on absolute values for signed types and threads signedness from the column type into codec instantiation, while keeping unsigned behavior unchanged. Also document the type-agnostic default path: unlike `T64`, `registerCodecGCD` intentionally does not throw when type information is missing and falls back to unsigned behavior. This preserves correctness but may reduce compression efficiency for signed data, and can surface via type-agnostic `default_compression_codec` / `CODEC(Default)` resolution. The stateless test covers signed/unsigned integers and decimals, and is stabilized for randomized settings by forcing wide parts and comparing ratios where equality is valid. Closes ClickHouse#78190
|
Workflow [PR], commit [344251a] Summary: ✅
AI ReviewSummaryThis PR fixes the signed-value arithmetic inside Findings
Final VerdictChanges requested. The signed-magnitude arithmetic itself looks correct, but the fix is still incomplete on the mixed-width Compact-stream hash path, and the explicitly-dismissed LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 73/80 (91.25%) · Uncovered code |
|
|
||
| private: | ||
| const UInt8 gcd_bytes_size; | ||
| const bool is_signed_type; |
There was a problem hiding this comment.
This new is_signed_type state needs to participate in updateHash. MergeTreeDataPartWriterCompact shares one CompressedStream per codec hash, and CompressionCodecMultiple propagates the same GCD hash into CODEC(GCD, ZSTD) chains. After this patch, Int64 CODEC(GCD) and UInt64 CODEC(GCD) of the same width are no longer interchangeable, but they still hash identically because updateHash only covers the AST. In a compact part, the second stream can therefore reuse the first stream's codec object and take the wrong signed/unsigned path, which silently brings back poor compression for one of the columns. Please include at least is_signed_type in the hash here (and ideally gcd_bytes_size as well, for the same stream-sharing reason).
There was a problem hiding this comment.
src/Compression/CompressionCodecGCD.cpp:80-83 still only hashes the AST plus is_signed_type; gcd_bytes_size is no longer part of getHash(). src/Storages/MergeTree/MergeTreeDataPartWriterCompact.cpp:103-123 still keys streams_by_codec only by that hash, so UInt32 CODEC(GCD) and UInt64 CODEC(GCD) in the same Compact part can still reuse one CompressedStream and compress one column with the other width. The broader fix mentioned in the top-level discussion is not in this base yet either: #114189 is still open, so the width-collision half of the original issue is still present in the current code.
There was a problem hiding this comment.
Re-checked the current revision: src/Compression/CompressionCodecGCD.cpp:80-83 still hashes only the AST plus is_signed_type; gcd_bytes_size is still missing from getHash(). Because src/Storages/MergeTree/MergeTreeDataPartWriterCompact.cpp:103-123 still reuses one CompressedStream per codec hash, mixed-width CODEC(GCD) columns such as UInt32 + UInt64 in the same Compact part can still collide and compress one column with the other's width-specific codec state.
1. Remove redundant `static` on `toMagnitude` in anonymous namespace. Clang-tidy raises readability-static-definition-in-anonymous-namespace as a warning-as-error. The function is replaced by a local lambda inside `compressDataForType`, which removes the warning and keeps the logic co-located with its only use. 2. Halve template instantiations of `compressDataForType`. The previous commit introduced six signed specializations (Int8/16/32/64/128/256) alongside the six unsigned ones, doubling compile-time and binary size. Because two's-complement arithmetic makes unsigned modular operations equivalent to signed ones, we can work entirely in the unsigned domain: magnitude extraction and sign restoration are controlled by a runtime `bool is_signed` parameter, keeping the instantiation count at six. 3. Fix `CompressionCodecGCD::updateHash` to include `gcd_bytes_size` and `is_signed_type`. `MergeTreeDataPartWriterCompact` keys `CompressedStream` reuse on `codec->getHash()`. Without hashing these fields, an `Int64 CODEC(GCD)` column and a `UInt64 CODEC(GCD)` column in the same compact part could share one stream and silently use the wrong signed/unsigned path, undoing the fix from the parent commit.
Covers the third fix in f789550: CompressionCodecGCD::updateHash must include gcd_bytes_size and is_signed_type, because MergeTreeDataPartWriterCompact keys CompressedStream reuse on codec->getHash(). Without those fields, an Int64 CODEC(GCD) column and a UInt64 CODEC(GCD) column written into the same Compact part hash-collide and share one CompressedStream, so one column silently adopts the other's sign convention. The test writes both column types into one Compact part and compares the total compressed size against the sum of each column compressed alone in separate tables: with the hash fix, this is (near) additive; a stream collision inflates the combined size well above the sum. Per-column stats (system.columns) aren't populated for Compact parts, so the test asserts on whole-part sizes instead. Verified the test actually detects the regression: reverting the hash.update(gcd_bytes_size)/hash.update(is_signed_type) calls and rebuilding flips the assertion from 1 to 0; restoring the fix flips it back to 1, reproducibly across runs. Note: a signedness hash collision alone does not corrupt data - quotient * gcd == val (mod 2^N) holds regardless of sign convention, verified exhaustively for 8-bit width - it only degrades the compression ratio, which is what this test measures.
|
@rienath The bug in updateHash #113798 (comment) found by @clickhouse-gh was a real thing, but interestingly there was a pre-existing part of it (not hashing gcd_bytes_size). Now Both gcd_bytes_size and the new is_signed_type are used in updateHash. That made me wonder if other codecs have the same class of gap, and I probably found one more — logged a separate "potential bug" ticket #113940 for it. UPD: The other Al's complaint about the |
04813_gcd_codec_hash_signedness only mixed Int64/UInt64 (same gcd_bytes_size, different is_signed_type), so it stayed green even if hash.update(gcd_bytes_size) were dropped from CompressionCodecGCD::updateHash again. Add a second scenario mixing UInt32/UInt64 (same is_signed_type, different gcd_bytes_size) to isolate that field. Verified by temporarily reverting the hash fix and rebuilding: the width-mismatch scenario's combined part size was ~11.3% larger than the sum of its parts (broken) vs. ~0.02% (fixed), well outside the existing 5% margin. 4062_gcd_codec_on_negative_integers Case 2 only compared min/max across the inserted rows, so a regression mangling the middle row would pass unnoticed since the extrema are unaffected. Compare the full ordered row set instead.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units8 translation units recompiled, 18 s compile time in total, 8 of them have a recent master baseline. |
rienath
left a comment
There was a problem hiding this comment.
Thank you for creating the issue! Quite nice we also have a fix so fast. Good progress on this PR, I've left more comments
Polish comments explaining the codec's intent (why GCD is computed over magnitudes for signed types, why `compressDataForType`'s template parameter `T` is always unsigned) and trim the two regression tests to focus on the negative-value/signedness bug this branch actually fixes, dropping coverage that duplicated other tests or was out of scope. Also revert the `hash.update(gcd_bytes_size);` line added to `CompressionCodecGCD::updateHash` in f789550. That line worked around a larger-scope issue: `updateHash` for several codecs does not account for type-resolved parameters, so structurally identical `CODEC(...)` columns of different types can share a `CompressedStream` and silently borrow each other's decoding parameters. This class of bug is tracked and fixed for all affected codecs by ClickHouse#113940 / PR ClickHouse#114189, which will merge before this branch, so the one-off fix here is removed to avoid scope creep and duplicated logic. The corresponding `04813_gcd_codec_hash_signedness` width-collision scenario is dropped for the same reason. Separately, a codec instance can be cached and reused keyed off its textual `CODEC(...)` description, which can serve a suboptimal cached codec for a superficially identical declaration on a different column type. This is a distinct, larger-scope issue tracked as ClickHouse#114202 and is not addressed here. Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
@rienath not sure whether you get notifications for “request review”, so I’m duplicating it as a comment. Will you please take a look? |
rienath
left a comment
There was a problem hiding this comment.
A few more comments. Btw, I recommend switching to Opus or higher. Sonnet is too weak for ClickHouse
- Explain why T must be unsigned in compressDataForType even for signed columns: the arithmetic needs modulo-2^N semantics, and the magnitude of the most negative value does not fit in the signed type. - Replace rand64 with intHash64(number) in both tests so the data is deterministic and any failure is reproducible; compute the modulo on the unsigned hash so the value range is symmetric in [-1000, 1000], matching the comment. - Explain why wide parts are forced in the test: Compact parts track only the whole-part size, so system.columns would report zero per-column bytes. - Unify variable naming between the two division branches in compressDataForType (mag -> magnitude). Co-Authored-By: Claude Fable 5 <[email protected]>
|
Hello @rienath, thank you for your comments. I believe they’ve been addressed now. UPD: I've also made sure that modified tests STILL count as regression markers. I reverted the fix, and ran the tests - they failed, as expected. |
The testcase used ordinary negative values for Int128/Int256, so the separate non-libdivide division branch in compressDataForType was never exercised at the signed-min overflow edge. Use the literal (-2^127 and -2^255) so every signed width hits the exact boundary that motivated the unsigned-magnitude rewrite.
…odec_on_negative_integers
Resolve the CompressionCodecGCD::updateHash conflict as a union of both sides. Master's ClickHouse#113798 added is_signed_type to the hash; this branch added gcd_bytes_size. GCD takes no AST arguments (setCodecDescription("GCD", {})), so both values are derived from the column type and neither reaches updateTreeHash. Keeping only one still lets two CODEC(GCD) columns that differ in the other value share a CompressedStream in Compact parts.
Master's ClickHouse#113798 added <Common/SipHash.h> to this file, so the copy this branch added is now redundant. The ALP and FPC siblings carry one each.
|
@kshcherbatov @rienath — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
|
@clickgapai A separate pool request #114189 for #113940 should have resolved this issue by now; it has already been merged, just next to #113798. |
|
@kshcherbatov #114189 is still open (
Adding |
Closes: #78190
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed GCD codec handling of signed fixed-width values with negative numbers. Previously it computed the divisor from signed values directly, which could miss the common divisor and significantly reduce compression efficiency for signed integer and decimal columns. It now computes the divisor from magnitudes for signed types while preserving existing behavior for unsigned types and decompression.
Description
GCD pre-processing was using signed values directly when discovering the common divisor. For signed fixed-width storage types, negative values could therefore spoil divisor discovery and make CODEC(GCD) compress much worse than expected, even though the data still had a large common divisor in magnitude.
The fix makes CompressionCodecGCD compute the divisor on unsigned magnitudes for signed types, and threads signedness from the column type into codec instantiation so Int* and signed Decimal* columns use the correct path. This also handles the signed-minimum edge case safely by avoiding signed negation overflow. Unsigned behavior is unchanged.
The type-agnostic fallback is kept deliberately unchanged: unlike T64, GCD still does not throw when type information is unavailable and falls back to the 1-byte unsigned path. That preserves correctness for cases such as default_compression_codec or CODEC(Default) resolution without a concrete column type, but signed data on that path may still compress less efficiently because signedness is unknown.
Workflow [PR]
Sync PR [sync-upstream/pr/113798]
Version info
26.8.1.1915(included in26.8and later)