Skip to content

Fix a join returning a pair of NaN rows for a WHERE float equality - #126071

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:where-float-equality-nan-join-key
Oct 19, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:where-float-equality-nan-join-key

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #116358
Related: #124854
Related: #115970

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed wrong results of a WHERE equality between float values over a comma, CROSS or INNER join, or in a correlated EXISTS: a pair of NaN values was returned, although nan = nan is false.

Description

On 26.8.18.2 (and 26.9.16.11, master), SELECT count() FROM a INNER JOIN b ON a.k = b.k WHERE a.f = b.g returns 1 when both rows hold nan, and 0 with query_plan_merge_filter_into_join_condition = 0. The same holds for comma and CROSS joins, a correlated EXISTS (SELECT 1 FROM b WHERE b.g = a.f), full_sorting_merge, and Float32, BFloat16, Nullable, LowCardinality or Tuple element floats. 26.3 and 26.7 return 0 for the INNER and comma joins.

mergeFilterIntoJoinCondition and CrossToInnerJoinPass turn such an equality into a JOIN key and drop it from the filter. A JOIN key treats a NaN as equal to a NaN (bitwise in a hash join, by compareAt in a sorting merge join; the documented JOIN ON behaviour), while equals follows IEEE.

The change extends the check #124854 added for NULLs nested in a Tuple (keyMatchesNullAsValue, renamed to keyMatchesValuesEqualsRejects) to float types: the equality stays a JOIN key and is also kept as a filter, which is pushed down as f = f per side. The hash join stays. JOIN ON, non-float types, and Array / Map (whose equals already treats NaN as equal) are unchanged. On 2M rows per side (debug build), the comma join took 0.071 s before and 0.075 s after, INNER JOIN ... WHERE a.f = b.g 0.155 s and 0.168 s.

The -0.0 direction of #116358 (the key does not match -0.0 to 0) is the hash table key semantics, handled by #115970.

Validation: the new test fails on master in all 14 data lines and both float EXPLAIN lines and passes with the fix, 50/50 with random settings. Each part of the change is proven necessary by a mutation. 1414 existing join tests: no new failures.


Workflow [PR]
Sync PR [sync-upstream/pr/126071]

Version info

  • Merged into: 26.11.1.174-master (included in 26.11 and later)

A JOIN key treats a NaN as equal to a NaN (bitwise in a hash join, by
`compareAt` in a sorting merge join), while `nan = nan` is false. When
`query_plan_merge_filter_into_join_condition` or `CrossToInnerJoinPass` turns a
`WHERE a.f = b.g` conjunct into a JOIN key and drops it from the filter, a pair
of NaNs is returned that the query rejects.

Extend the predicate that keeps such a conjunct in the filter (added for NULLs
nested in a Tuple) to floats: Float32, Float64 and BFloat16, at the top level,
under Nullable/LowCardinality and as Tuple elements. The equality is still a
JOIN key, so the hash join is kept and only NaN pairs are dropped by the
filter. Array and Map are not affected: their `equals` treats NaN as equal,
like the key.

The `-0.0` direction (the key misses `-0.0 = 0.0`) is the hash table key
semantics and is not changed here.

Related: ClickHouse#116358

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings in round 1, 0 in the delta recheck).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test did not cover LowCardinality floats or the deliberate Array / Map exclusion AGREE, fixed @ e172e01 LowCardinality(Float64) and LowCardinality(Nullable(Float64)) lines, plus an EXPLAIN line showing Array / Map equalities keep the key-only plan; each is proven live by a mutation
2 💡 The comment said a JOIN key compares floats bitwise, while a sorting merge join uses compareAt AGREE, fixed @ e172e01 comments reworded

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261015-163400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05401_where_float_equality_nan_join_key.sql through clickhouse local: master returns the NaN pair on all 14 data lines, every run. Released 26.8.18.2 and 26.9.16.11 also return it (26.3 and 26.7 do not).
b Root cause explained? mergeFilterIntoJoinCondition and CrossToInnerJoinPass move a WHERE a.f = b.g conjunct into the JOIN key and drop it from the filter. The key treats NaN as equal to NaN (bitwise in a hash join, by compareAt in a sorting merge join), while nan = nan is false, and nothing rejects the pair any more.
c Fix matches root cause? Yes. For float types the conjunct stays in the filter as well, at both places that create such keys, with the mechanism #124854 added for NULL in a Tuple. The key and the hash join stay.
d Test intent preserved / new tests added? New SQL test: comma, CROSS, INNER + WHERE, full_sorting_merge, correlated EXISTS, three tables, LowCardinality floats, plus EXPLAIN lines checking that the equality is both a JOIN key and a filter (an integer key and an Array / Map equality are the controls).
e Both directions demonstrated? Yes, on Build-ID verified binaries: master fails 14/14 data lines and both EXPLAIN lines, the fix matches the reference. 50/50 randomized runs green. Six mutations (no float check, no Tuple element check, each call site reverted, Array / Map flagged, LowCardinality not unwrapped) each turn lines red.
f Fix is general across code paths? Both key-creating places are fixed. Explicit JOIN ON keeps its documented NaN matching. Decorrelation never substitutes float columns. Join reordering and runtime filters keep the filter (three-table line, 1414 join tests unchanged).
g Fix generalizes across inputs (params/datatypes/wrappers)? Float32, Float64, BFloat16, Nullable, LowCardinality, LowCardinality(Nullable), nested Tuple elements, Float32 = Float64. Array / Map are not flagged on purpose: their equals treats NaN as equal, like the key. Decimal and integer keys are unchanged.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, no format change; only rows that the WHERE rejects are removed. Plans of float WHERE equalities over joins gain a per-side f = f filter.
i Invariants and contracts preserved? The key is still added once (the existing join_condition_names guard), the kept conjunct follows the normal non-merged path, and only INNER / CROSS / comma joins are affected, as before.

Session id: cron:clickhouse-impl-slot-40:20261015-161600

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e172e01]

Summary: ✅


AI Review

Summary

Preserves floating-point WHERE equalities as filters when they become join keys, preventing NaN pairs from passing through either rewrite. Reviewed commit e172e018d39bfa75c9e1a40584dbf7d31493f44a; no actionable findings. The regression test passed locally with filter pushdown enabled and disabled, and all 18 additional probes passed across four join algorithms, nested wrappers, mixed float widths, correlated EXISTS, and the Array / Map exclusions.

Missing context / blind spots
  • ⚠️ Broader CI remains incomplete: the workflow validation report reports bugfix-validation jobs dropped after an earlier configuration failure. Re-running the dropped jobs would close this gap. The fast-test report confirms the new regression test passed.
Final Verdict

Status: ✅ Approve.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @m-selmi @antaljanosbenjamin, could you review this? It extends the #124854 guard to floats: a WHERE a.f = b.g that becomes a JOIN key is also kept as a filter, because the key matches NaN to NaN while nan = nan is false.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Oct 15, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I don't like the fact that someone wants to fix this, the fix itself is good and well contained.

@alexey-milovidov alexey-milovidov self-assigned this Oct 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 18, 2026
Merged via the queue into ClickHouse:master with commit fac8f9e Oct 19, 2026
524 of 528 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 19, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 3, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #126071 to 26.10: Fix a join returning a pair of `NaN` rows for a `WHERE` float equality
leshikus pushed a commit that referenced this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants