Repository navigation
Fix least/greatest with a NaN argument returning different results with and without JIT - #126022
Conversation
Two-argument `least`/`greatest` over floats returned the NaN for a NaN on the right in the interpreted `apply` (`a < b ? a : b`), but the number in the compiled body (`llvm.minnum` / `llvm.maxnum`). The result of an unchanged query therefore flipped once the expression was JIT-compiled (from its 4th execution at default settings). The two bodies also disagreed for equal operands of different sign. Both bodies now compute `(x < y || isNaN(y)) ? x : y` (`>` for `greatest`): a NaN loses against any number, as in the variadic `least`/`greatest` and the `min`/`max` aggregates, and a tie still returns the right operand. Closes ClickHouse#120728 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 (3 nits, no blocker or major).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261015-122700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261015-120100 |
|
cc @Ergus @george-larionov, could you review this and add the |
|
Workflow [PR], commit [35098dd] Summary: ❌
AI ReviewSummaryThe PR makes two-argument Missing context / blind spots
Final Verdict✅ Approve. No changes requested. |
CI finish ledger - b23673aEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261015-170015 |
Algunenano
left a comment
There was a problem hiding this comment.
🤖 The fix looks right, and the branchless (x < y || isNaN(y)) ? x : y is a good choice. I checked CompareHelper<T>::less(x, y, 1) as an alternative: with clang at -O3 -march=x86-64-v3 its vectorized loop is about twice as many instructions, because it has to keep less(nan, nan) == false for sorting. So no need to switch to it. Three requests:
-
Reduce the duplication. The "NaN loses" rule is now written three times in C++:
LeastBaseImpl::apply,GreatestBaseImpl::apply, andtakesPrecedenceinLeastGreatestGeneric.h. Bothleast.cppandgreatest.cppalready include that header, so one small helper there could serve all three. For example,candidate < best || isNaN(best)(with>forgreatest) as "the candidate replaces the current best". The variadic loop's extra!isNaN(incoming)check only changes which NaN is kept when both are NaN, so it can be dropped. -
Fix the claim about the variadic form. The description says the binary form now behaves "as in the variadic form". That holds for NaN, but not for ties:
SELECT 1/least(materialize(-0.), materialize(0.)) -- inf: binary returns the right operand SELECT 1/least(materialize(-0.), materialize(0.), materialize(0.)) -- -inf: variadic keeps the earlier argumentThe new test locks in "the right operand wins" for the binary form. Either make the binary form keep the left operand on ties, like the variadic one (the shared helper from point 1 gives you that for free), or reword the description and the code comments to say only NaN handling matches.
-
Drop the
query_log/CompiledFunctionExecuteblock in the test. It needsSYSTEM FLUSH LOGSplus aquery_logscan, which is expensive for a stateless test. And it doesn't add much: before the fix, the JIT-on and JIT-off sections already give different output, so the reference file catches the regression by itself.
| const auto x = static_cast<Result>(a); | ||
| const auto y = static_cast<Result>(b); | ||
| /// NaN loses against any number, as in the variadic `least`. | ||
| return (x < y || isNaN(y)) ? x : y; |
There was a problem hiding this comment.
🤖 This is the same rule as takesPrecedence in LeastGreatestGeneric.h, apart from tie handling. Could both use one helper? See the review body.
There was a problem hiding this comment.
Done in 4e195dc: one helper, candidateReplacesBest, now used by both apply and the variadic fold.
| greatest(materialize(toNullable(1.)), materialize(toNullable(nan))) | ||
| SETTINGS least_greatest_legacy_null_behavior = 1, compile_expressions = 0; | ||
|
|
||
| -- The carriers are compiled whenever a compilable control is |
There was a problem hiding this comment.
🤖 Suggest removing this block. The SYSTEM FLUSH LOGS and query_log lookup are costly, and the JIT on/off result comparison above already fails without the fix.
`candidateReplacesBest` in `LeastGreatestGeneric.h` is now the one place that says a NaN loses against any number. The two-argument `apply` of `least` and `greatest` and the variadic numeric fold both use it. The binary form passes its right operand as `best`, so its results are unchanged, including ties: a tie still returns the right operand, and `least(0., -0.)` stays `-0` as `01246_least_greatest_generic` expects. The variadic fold no longer special-cases a NaN candidate, which only changes which NaN is returned when every argument is NaN. Drop the `query_log` check from the test: without the fix the JIT-on and JIT-off sections already give different results. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, all three are addressed in 4e195dc.
|
CI finish ledger - 4e195dcEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The build failure dropped 89 dependent jobs, so no functional test has run on this head yet. #126269 merged at 14:32Z, after this run was built. Session id: cron:our-pr-ci-monitor:20261016-143004 |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes6 object files changed (+280.97 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units
|
|
@groeneai Please verify the crash in the upgrade check |
CI finish ledger - 35098ddI checked the Upgrade check crash, and this PR did not cause it. The server that crashed is the previous release
Session id: cron:clickhouse-premium-slot-25:20261021-110700 |
|
Perfect. If upgrade check is on the old release and the fix is already backported everything is fine |
9c39d7d
Backport #126022 to 26.10: Fix `least`/`greatest` with a NaN argument returning different results with and without JIT
… returning different results with and without JIT
Closes: #120728
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
leastandgreatestwith two floating-point arguments returning a different result for aNaNargument depending on the argument order and on whether the expression was JIT-compiled, so the same query could change its result from its 4th execution at default settings. ANaNargument now loses against any number, as in the variadic form. Closes #120728.Description
On 26.8.18.2 (also 26.3.42.3 and master), two-argument
least/greatestoverFloat32/Float64returns a different value for a NaN argument interpreted and JIT-compiled, so at default settings an unchanged query changes its answer from its 4th run:SELECT least(-x, nan) FROM values('x Float64', -1.0)run five times returnsnan nan nan 1 1.The interpreted
applyisa < b ? a : b: a NaN on the right wins and a NaN on the left loses (least(1, nan) = nan,least(nan, 1) = 1). The compiled body isllvm.minnum, where a NaN always loses.greatestmirrors both with>andllvm.maxnum. The two paths also disagree for equal operands of different sign (-0.and0.).Both
applyand the compiled body now compute(x < y || isNaN(y)) ? x : y(>forgreatest) throughcandidateReplacesBestinLeastGreatestGeneric.h, which the variadic numeric fold also uses. A NaN loses against any number and the result is NaN only when both arguments are, as in the variadic andNullableforms (tests01246,04509) and themin/maxaggregates. Ties still differ: the binary form returns the right operand (least(0., -0.)stays-0, pinned by01246), the variadic form the earlier argument.min2/max2are separate, never compiled functions and are not changed.Price, in a standalone AVX2 loop (
-O3 -march=x86-64-v3, 1% NaN): onevminpdbecomes a compare, an unordered compare, an or and a blend, 0.246 -> 0.263 ns/row forFloat64(+7%) and 0.089 -> 0.099 ns/row forFloat32(+12%) interpreted; the compiled loop changes by about 1%.The new test runs both argument orders for
Float32/Float64,Int32promoted toFloat64and signed zeros with and without compilation, plus constant folding,BFloat16andleast_greatest_legacy_null_behavior. It fails on master, fails with onlyapplyfixed, and passes 50/50 randomized runs.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126022]
Version info
26.11.1.412-master(included in26.11and later)