Repository navigation
Fix undefined float-to-int cast on NaN in the SZ3 codec quantizer - #110762
Conversation
`SZ3::LinearQuantizer::quantize_and_overwrite` cast `fabs(data - pred) * error_bound_reciprocal` straight to `int64_t`. For NaN or infinite input (or a finite value out of int64 range) that cast is undefined behavior, reported by UBSan in stress tests as `LinearQuantizer.hpp:45:49: runtime error: nan is outside the range of representable values of type 'long'`. Such values can never be quantized - the error-bound recheck sends them to the unpredictable (lossless) path anyway - so the fix routes them there before the cast, in the `ClickHouse/SZ3` fork (commit dda0caee243719d1dad10a9f05bc18eeedc2ab6b on `ClickHouse/v3.3.2`). Behavior for in-range values is unchanged. The new test `04603_sz3_codec_non_finite` checks that NaN and infinities round-trip exactly through `SZ3` (they are stored losslessly), both on insert and after a merge, and would trigger the UBSan report without the fix. Seen in: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110430&sha=2f139250fec65930580e67de7ebd07b87392fd16&name_0=PR&name_1=Stress%20test%20%28amd_asan_ubsan%29 On PR: #110430 Caused by: #108788 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [f347caf] Summary: ✅
AI ReviewSummaryThis PR fixes two experimental Final Verdict
|
Advance the `contrib/sz3` submodule from `dda0caee` to `ff760cc`, the current tip of the `ClickHouse/v3.3.2` branch. In addition to the quantizer NaN cast fix already pinned, this brings in the fix for `remaining_length` accounting after Huffman `decode` in `RegressionPredictor`/`ComposedPredictor` (ClickHouse/SZ3#2), which otherwise made a column compressed with `CODEC(SZ3('ALGO_LORENZO_REG', ...))` fail to decompress after being written. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Cover the `remaining_length` accounting fix pulled in by the submodule bump: a
`Float64` column written with `CODEC(SZ3('ALGO_LORENZO_REG', ...))` is inserted
and read back within the error bound, before and after a merge. Before the fix
the read failed with `CORRUPTED_DATA` ("SZ3 Huffman: encoded length exceeds
compressed buffer") for all but the smallest inputs, so the data was effectively
lost. Verified against the `contrib/sz3` library that the read throws at the
pre-fix pin `dda0caee` and round-trips at the pinned `ff760cc`.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: This PR only updates the experimental SZ3 compression codec (contrib submodule bump plus regression tests for the ALGO_LORENZO_REG decode path and non-finite handling) — a code path that ClickBench and TPC-H queries never execute. None of the flagged deltas can be attributed to this change. Q4 (-14.0%) and Q15 (-20.2%) were already correctly held at no_change within master's variance band. Q23's ×2.3 faster reading is downgraded to not_sure: the query is off-path for this diff and its source run was noisy, so the delta is run-to-run variance rather than a real improvement. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
The test asserted that after OPTIMIZE TABLE ... FINAL the data still deviates from the original by at most the codec's ABS error bound. That does not hold: a merge decompresses and recompresses the column through the lossy SZ3 codec, so every pass adds up to the bound (0.01) on top of the previous one, and the observed error after the merge reaches ~0.0197. The check failed as `1 0` in every stateless CI job. Allow up to three lossy passes (the initial write, a possible concurrent background merge of the two parts, and the OPTIMIZE FINAL rewrite), i.e. 0.031. CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110762&sha=85d8815db3e8ae735bc733f161af214e000d0228&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29 PR: #110762 Co-Authored-By: Claude Fable 5 <[email protected]>
The review noted that the SQL round-trip assertions would also pass if SZ3
silently downgraded a block to the bit-exact `ALGO_LOSSLESS` fallback, or if no
block selected the regression predictor, so a later tuning change could stop
exercising the fixed code while the tests stay green.
- Add the gtest `SZ3Test.LorenzoRegSerializesAndDecodesRegressionCoefficients`:
it compresses the same data shape as test 04604 with
`SZ3('ALGO_LORENZO_REG', 'ABS', 0.01)`, asserts that the stored `cmprAlgo`
stays `ALGO_LORENZO_REG`, that a non-empty regression-coefficient stream is
serialized (the count is the leading field of the inner buffer), and that
decompression - which decodes those coefficients through the fixed
`RegressionPredictor::load` - succeeds, is lossy, and stays within the bound.
- Tests 04603 and 04604: assert that some finite values changed, a positive
signal that the columns stayed on the lossy path (the lossless fallback
would reproduce the input bit-exactly). The default-configured columns in
test 04603 are documented as intentionally lossless: their REL error bound
is relative to the value range, which is infinite for this data.
PR: #110762
Co-Authored-By: Claude Fable 5 <[email protected]>
… merges The first `max(abs(orig - val)) <= 0.011` assertion assumed the two inserted parts were still unmerged (one lossy pass each), but background merges were not stopped: a merge could fire between the second `INSERT` and the `SELECT`, send `val` through a second lossy pass, and legitimately exceed the one-pass bound, making the test flaky. Stop merges before the inserts so the pre-merge check sees exactly one lossy pass, and start them again right before `OPTIMIZE TABLE ... FINAL` so the explicit merge still runs. The post-merge check is then deterministically two passes (initial write plus the `OPTIMIZE FINAL` merge). The merge control is table-scoped, so the test stays parallel-safe. Verified against a build with `contrib/sz3` at `ff760cc`: the test output matches the reference. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed both required actions from the AI review:
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 32/32 (100.00%) · Uncovered code |
The test asserted that after OPTIMIZE TABLE ... FINAL the data still deviates from the original by at most the codec's ABS error bound. That does not hold: a merge decompresses and recompresses the column through the lossy SZ3 codec, so every pass adds up to the bound (0.01) on top of the previous one, and the observed error after the merge reaches ~0.0197. The check failed as `1 0` in every stateless CI job. Allow up to three lossy passes (the initial write, a possible concurrent background merge of the two parts, and the OPTIMIZE FINAL rewrite), i.e. 0.031. CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110762&sha=85d8815db3e8ae735bc733f161af214e000d0228&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29 PR: ClickHouse#110762 Co-Authored-By: Claude Fable 5 <[email protected]>
The review noted that the SQL round-trip assertions would also pass if SZ3
silently downgraded a block to the bit-exact `ALGO_LOSSLESS` fallback, or if no
block selected the regression predictor, so a later tuning change could stop
exercising the fixed code while the tests stay green.
- Add the gtest `SZ3Test.LorenzoRegSerializesAndDecodesRegressionCoefficients`:
it compresses the same data shape as test 04604 with
`SZ3('ALGO_LORENZO_REG', 'ABS', 0.01)`, asserts that the stored `cmprAlgo`
stays `ALGO_LORENZO_REG`, that a non-empty regression-coefficient stream is
serialized (the count is the leading field of the inner buffer), and that
decompression - which decodes those coefficients through the fixed
`RegressionPredictor::load` - succeeds, is lossy, and stays within the bound.
- Tests 04603 and 04604: assert that some finite values changed, a positive
signal that the columns stayed on the lossy path (the lossless fallback
would reproduce the input bit-exactly). The default-configured columns in
test 04603 are documented as intentionally lossless: their REL error bound
is relative to the value range, which is infinite for this data.
PR: ClickHouse#110762
Co-Authored-By: Claude Fable 5 <[email protected]>
…-ubsan Fix undefined float-to-int cast on NaN in the `SZ3` codec quantizer
Related: #110430
Caused by: #108788
Stress tests with UBSan sporadically fail with:
for example on https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110430&sha=2f139250fec65930580e67de7ebd07b87392fd16&name_0=PR&name_1=Stress%20test%20%28amd_asan_ubsan%29 (also seen on master and several unrelated PRs since #108788 revived the
SZ3codec).SZ3::LinearQuantizer::quantize_and_overwritecastsfabs(data - pred) * error_bound_reciprocalstraight toint64_t; for NaN or infinite input the cast is undefined behavior. Such values can never be quantized — the error-bound recheck below the cast sends them to the unpredictable (lossless) storage anyway — so the fix in theClickHouse/SZ3fork (commitdda0caee243719d1dad10a9f05bc18eeedc2ab6bon theClickHouse/v3.3.2branch) routes them there before the cast. Behavior for finite in-range values is unchanged:scaled_diff >= radius * 2implies the oldquant_indexcheck would have sent them to the unpredictable path as well.Verified with a standalone reproducer compiled with
-fsanitize=undefined,float-cast-overflow -fno-sanitize-recover=all: before the fix it fails exactly like the stress report; after the fix, NaN and infinities round-trip losslessly through all three supported algorithms (ALGO_INTERP,ALGO_INTERP_LORENZO,ALGO_LORENZO_REG) for bothFloat32andFloat64, including the codec's defaultRELerror-bound mode. The new test04603_sz3_codec_non_finitecovers the same scenario end-to-end and would trigger the UBSan report without the fix.The submodule bump (to
ff760ccon the same branch, ClickHouse/SZ3#2) additionally fixes theremaining_lengthaccounting after a HuffmandecodeinRegressionPredictor::loadandComposedPredictor::load: the code subtracted the uncompressed index count instead of the bytes the decode consumed, which understated the bound and made a laterencoder.loadreject valid data. As a result, a column written withCODEC(SZ3('ALGO_LORENZO_REG', ...))could be inserted but failed every later read withCORRUPTED_DATA("SZ3 Huffman: encoded length exceeds compressed buffer"). The new test04604_sz3_codec_lorenzo_regcovers the round trip end-to-end (including after a merge recompresses the data, where the lossy error of each pass compounds), and the new gtestSZ3Test.LorenzoRegSerializesAndDecodesRegressionCoefficientsasserts that this data shape really stays on theALGO_LORENZO_REGpath and serializes a non-empty regression-coefficient stream, so the fixed loaders are positively exercised.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed reading data compressed with the experimental
SZ3('ALGO_LORENZO_REG', ...)codec, which previously failed withCORRUPTED_DATA, and avoided undefined behavior when compressing non-finite floating-point values (NaNand infinities) with the experimentalSZ3codec.Documentation entry for user-facing changes
Version info
26.7.1.1154(included in26.7and later)