Repository navigation
Conversation
The DateTime64 branch of UTCTimestampTransform::executeImpl regressed about 1.65x when ClickHouse#109738 fixed a signed overflow and a wrong-offset-at-a-boundary bug there. That fix promoted the per-row arithmetic to Int128 and clamped in Int128. Int128 in ClickHouse is wide::integer<128, signed>, a software big-integer: its operator* sign-normalizes through branches before reaching a native multiply and then reassembles a two-UInt64 struct. Every row gained that call plus two 128-bit compares and selects in place of one imul and one add, which is not vectorizable. The wide arithmetic was redundant. The floored split guarantees seconds * scale_multiplier + micros == value exactly, so shifting the seconds by the timezone offset and reassembling equals shifting the underlying value by offset * scale_multiplier. The Int128 multiply only re-derived its own input. Shift the underlying value directly instead, with a saturating Int64 add/sub via common::addOverflow / common::subOverflow, which compile to one add/sub plus a flag test. offset * scale_multiplier cannot overflow Int64: scanning timeZoneOffset over all 597 shipped time zones and 4.18 million samples, including out-of-LUT extremes, gives a maximum absolute offset of 130800, and scale_multiplier is at most 10^9 because DataTypeDateTime64 rejects scale > 9. The product stays below 1.4e14 against an Int64 limit of 9.2e18. Both semantics from ClickHouse#109738 are preserved: the seconds split still floors, so timezoneOffset() reads the floor second, and the result is still saturated into Int64 rather than overflowing. Measured on the queries in tests/performance/utc_timestamp_transform.xml (10 million rows, max_threads=1, optimized build, arms differing by exactly this hunk): 0.102s to 0.062s, against 0.061s before ClickHouse#109738. The DateTime queries of the same test are the control and stay at 0.033s. Results are bit-identical to master, verified over 12.9 million (value, scale, timezone, direction) cases plus 112 thousand cases built on the 1056 real offset-transition seconds, and 03891 passes byte-identical while still reddening if the saturation is dropped, its direction flipped, or the floor correction removed. Closes ClickHouse#115802
Internal second-model review (click to expand)An independent cold review of this diff plus a second-model gate pass, adjudicated against the
Independently of the recorded evidence, I re-derived the algebraic identity the fix rests on and Gate: 0 findings, full coverage of the changed file, its callers, the overflow and Session id: cron:clickhouse-review-slot-8:20260821-172900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20260821-162100 |
|
Workflow [PR], commit [a90db33] Summary: ✅
AI ReviewSummaryThis PR rewrites the Final Verdict✅ No actionable review findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 13/13 (100.00%) · Uncovered code |
|
cc @george-larionov @nihalzp, could you review this? The DateTime64 branch of toUTCTimestamp/fromUTCTimestamp did its per-row arithmetic in Int128, which is a software big-integer here, but the floored seconds split makes that wide math algebraically redundant, so this shifts the underlying value directly with a saturating Int64 add/sub and results stay bit-identical. |
The arm_tidy build runs clang-tidy with -warnings-as-errors, and cppcoreguidelines-init-variables is one of only two cppcoreguidelines checks enabled in .clang-tidy. It flagged the declaration of `res`, failing the build: src/Functions/UTCTimestampTransform.cpp:157:27: error: variable 'res' is not initialized [cppcoreguidelines-init-variables,-warnings-as-errors] `res` is a pure out-parameter of common::subOverflow/addOverflow, which wrap __builtin_ssubl_overflow/__builtin_saddl_overflow and always write it, so this was a lint-policy failure rather than a real uninitialized read. Initializing at the declaration matches how the rest of the codebase declares these out-parameters (DecimalFunctions.h, HashTable.h, CompressionCodecMultiple.cpp). The initializer is dead: rebuilding with an arbitrary initial value instead of 0 produces a byte-identical object file, so codegen and the measured performance of the DateTime64 path are unchanged. Co-Authored-By: Claude Fable 5 <[email protected]>
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 units417 translation units recompiled, 3830 s compile time in total, 417 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.09 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up
toUTCTimestamp/to_utc_timestampandfromUTCTimestamp/from_utc_timestamponDateTime64arguments, restoring the throughput they had before the overflow fix in #109738. Results are unchanged. Closes #115802.Description
Closes: #115802
My #109738 fixed a signed-overflow and a wrong-offset-at-a-boundary bug on the
DateTime64path of these functions, but it did the arithmetic and the clamp inInt128, once per row.Int128here iswide::integer<128, signed>, a software big-integer, sooperator*sign-normalizes through branches before a native multiply and reassembles a two-UInt64struct. Each row gained that call plus two 128-bit compares and selects where it previously had oneimuland oneadd. TheDateTimebranch was untouched and did not regress.The wide arithmetic is redundant. The floored split guarantees
seconds * scale_multiplier + micros == valueexactly, so shifting the seconds by the offset and reassembling equals shifting the underlying value byoffset * scale_multiplier; theInt128multiply only re-derived its own input. This PR shifts the underlying value directly with a saturatingInt64add/sub (common::addOverflow/subOverflow: oneadd/subplus a flag test).offset * scale_multipliercannot overflowInt64: scanningtimeZoneOffsetover all 597 shipped time zones (4.18M samples, including out-of-LUT extremes) givesmax|offset| = 130800, andscale_multiplier <= 10^9, so the product stays 70000x below theInt64limit. Both semantics from #109738 are kept: the split still floors, and the result is still saturated.Measured on the queries from
tests/performance/utc_timestamp_transform.xml(10M rows,max_threads=1, optimized build, arms differing by exactly this hunk):0.102s -> 0.062s, against0.061sbefore #109738. TheDateTimequeries are the control and stay at0.033sthroughout. That is x86_64, so the ratio is smaller than the ARM 2.7x in the issue, but the fix returns to the pre-#109738 cost rather than narrowing the gap.Results are bit-identical to master over 12.9M
(value, scale, timezone, direction)cases plus 112K cases built on the 1,056 real offset-transition seconds.03891_utc_timestamp_datetime64_overflowpasses byte-identical, and still reddens if the saturation is dropped, its direction flipped, or the floor correction removed.Workflow [PR]
Sync PR [sync-upstream/pr/115838]
Version info
26.8.1.1944(included in26.8and later)