Repository navigation
Description
Describe what's wrong
floor,ceilandroundon Float64 values with |N| >= 20 produce wrong output with no error.- Triggered by calls like
floor(-1234.5678, -20)orfloor(1.5e-20, 20), with constant or non-constant N. - Expected
-100000000000000000000but got-18446744073709552000; small values with large positive N collapse to0instead of the correct result.
Root cause: FloatRoundingImpl::apply and applyOne in FunctionsRound.h build the scale factor from intExp10, which saturates to UInt64 max for exponents above 19 (src/Common/intExp10.h), so the float is scaled by 1.8447e19 instead of the actual power of ten.
Analysis details (evidence, affected locations, impact)
Why we believe this is a bug: FunctionRounding::executeImpl -> Dispatcher::apply -> FloatRoundingImpl::apply (FunctionsRound.h:295) / applyOne (FunctionsRound.h:306) -> Op::prepare(intExp10(digits)); intExp10 returns std::numeric_limits<uint64_t>::max() for exponents above 19 (src/Common/intExp10.h:10-11).
Affected locations:
src/Functions/FunctionsRound.h:295—FloatRoundingImpl::apply: scale from saturatingintExp10src/Functions/FunctionsRound.h:306—FloatRoundingImpl::applyOne(non-constant N): samesrc/Common/intExp10.h:11—intExp10saturates to UInt64 max for x > 19
Impact: Silent wrong results when rounding floats to 20+ decimal places (small values collapse to 0) or to 10^20 and coarser (values snap to multiples of 1.8447e19). No error is raised.
Does it reproduce on most recent release?
Confirmed on master; not checked on a release.
How to reproduce
Run SELECT floor(-1234.5678, -20), ceil(1234.5678, -20), round(1.5e20, -20), floor(1.5e-20, 20) and compare with materialized N to confirm both constant and non-constant paths are affected.
-- Rounding a Float64 to |N| >= 20 digits must use the exact power of ten.
SELECT floor(-1234.5678, -20), ceil(1234.5678, -20), round(1.5e20, -20), floor(1.5e-20, 20);
SELECT floor(materialize(-1234.5678), materialize(-20)), floor(materialize(1.5e-20), materialize(20));Verified: this block reproduces on current master (3634df08ebb7) when pasted as is.
Expected behavior
Rounding should scale by the exact power of ten (10^|N|) even when |N| >= 20.
Expected output of the reproducer above:
-100000000000000000000 100000000000000000000 200000000000000000000 1e-20
-100000000000000000000 1e-20
Error message and/or stacktrace
The scale factor saturates at 2^64-1, so results snap to multiples of that value or collapse to zero instead of the correct power-of-ten scaling.
Actual output of the reproducer above:
-18446744073709552000 18446744073709552000 147573952589676410000 0
-18446744073709552000 0
Suggested fix
Build the float scale from a floating-point power of ten (a double table up to 1e308, or exp10) when |N| > 19, keeping intExp10 for |N| <= 19 so the common path and its constant-divisor codegen stay unchanged. Return x when positive N exceeds the type's precision (avoids x * 10^N overflowing to inf). Trade-off: 10^N above 1e22 is not exact in double, so huge |N| is correct to double precision only.
Additional context
Same pattern as #132276 (found by: lexical, vector; vector: cosine distance 0.17 (agrees with the lexical leg)).
Open risks:
Float32andBFloat16shareFloatRoundingImpl; onlyFloat64was run.
Found during automated review of PR #132299; whether that PR introduced it could not be established, so nobody is tagged. Severity P2 · Finding h_pr132299_002
Version info
- Resolved by: Fix
round,floor,ceilandtruncof floats returninginfon overflow or a wrong value for|N| >= 20#128321 - Merged into:
26.11.1.2771-master(included in26.11and later)
Activity
PedroTadim commentedon Nov 18, 2026
@groeneai fix and I review
groeneai commentedon Nov 18, 2026
This is fixed by #128321 (open, CI finished). On its head the reproducer above returns exactly the expected output, for constant and column
N.Float32takes the same path:floor(toFloat32(1.5e-20), 20)=1e-20,floor(toFloat32(-1234.5678), -20)=-1e20. The PR now hasCloses:lines for this issue and #132573, which it fixes too.