Repository navigation
Fix integer rounding functions with |N| >= 20 - #128283
alexey-milovidov merged 1 commit into
Conversation
`Dispatcher` turned the scale argument into `size_t intExp10(|N|)`, which
saturates to 2^64 - 1 for |N| > 19. With a constant N the value reached the
`switch` in `IntegerRoundingImpl::apply`, which has no case for it, so
`round(number, 20)` and `floor(number, -20)` threw BAD_ARGUMENTS for every
integer type (the switch was taken for positive N too, although `compute`
ignores the scale there). With a non-constant N or constant arguments the
value reached the wide-integer `compute` overload, which has no guard, so
`floor(toInt128('100000000000000000005'), -20)` rounded to a multiple of
2^64 - 1 and returned 92233720368547758075.
Pass the exponent instead of the scale, as `DecimalRoundingImpl` already
does. `IntegerRoundingImpl` returns the argument for N >= 0, 0 when 10^|N|
does not fit the type (what the native guard already returned), the exact
`T`-typed scale for 128/256-bit types with 20 <= |N| <= digits10, and keeps
the compile-time divisor switch for |N| <= 19. `FloatRoundingImpl` computes
the same `intExp10` value itself, so float results are unchanged.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold review of the diff.
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261030-194500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261030-182500 |
|
cc @Ergus @rschu1ze, could you review this and add the |
|
Workflow [PR], commit [c833f72] Summary: ✅
AI ReviewSummaryThe change fixes integer rounding with Missing context / blind spots
Final Verdict✅ Approve. No code changes requested; pending CI results remain to be evaluated. |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsRound.cpp.o |
10.63 MiB | 8.79 MiB | -1.84 MiB (-17.30%) |
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
10 translation units recompiled, 59 s compile time in total, 10 of them have a recent master baseline.
Median compile-time ratio to the baselines is ×0.93 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -8.3 s (-12%) in total before that adjustment.
alexey-milovidov
left a comment
There was a problem hiding this comment.
A weird corner case. Thanks for the fix!
3fbcffc
…large-scale Resolve the conflict in src/Functions/FunctionsRound.h with ClickHouse#128283: keep master's IntegerRoundingImpl, keep this branch's FloatRoundingImpl, whose FloatRoundingComputation::prepare takes the exponent itself. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
round,roundBankers,floor,ceilandtruncof integers with|N| >= 20: a constantNthrewBAD_ARGUMENTS, and with a negativeN128/256-bit integers were rounded to a multiple of2^64 - 1(floor(toInt128('100000000000000000005'), -20)returned92233720368547758075instead of100000000000000000000).Description
On 26.10 (and 26.3),
SELECT round(number, 20) FROM numbers(1)throwsBAD_ARGUMENTS, andfloor(toInt128('100000000000000000005'), -20)returns92233720368547758075instead of100000000000000000000.DispatcherturnedNintosize_t scale = intExp10(|N|), which saturates to2^64 - 1for|N| > 19. With a constantNthat value reached theswitchinIntegerRoundingImpl::apply, which only has cases up to10^19and throws otherwise (also for a positiveN, where the result is justx). With a non-constantNor constant arguments, the 128/256-bitcomputeoverload divided by2^64 - 1.size_tcannot hold10^20..10^77, soDispatchernow passes the exponent, as the decimal path already does, andIntegerRoundingImplbuilds the scale:N >= 0: returnsxwithout the switch;10^|N|does not fit the type: 0, the only multiple of10^|N|the type can hold;20 <= |N| <= digits10: the exact scale of the type (up to10^77forUInt256);|N| <= 19: the existing compile-time divisor switch, unchanged.Float results are unchanged (
FloatRoundingImplcomputes the sameintExp10value itself); largeNon floats is a separate problem not changed here.Validation: the new test fails on master and passes 50/50 with randomized settings. An exact sweep of 396k cases (12 integer types,
Nin[-80, -18]and large positiveN, constant and non-constantN, constant arguments) matches a Python oracle, and master and this PR give byte-identical results for integers with|N| <= 19, decimals with|N| <= 40and floats.No related issue found.
Workflow [PR]
Sync PR [sync-upstream/pr/128283]
Version info
26.11.1.1088-master(included in26.11and later)