Repository navigation
Document that a float NaN JOIN key can match NaN - #112409
Conversation
Internal review: 7 rounds, 32 findings adjudicated, 25 agreed and fixed before publicationBefore opening this PR I ran, for each of seven rounds, an independent cold review of the diff plus a The mechanism the PR documents held up in every round, verified independently each time: Round 1 (4 gate + 4 own):
Round 2 (5 gate + 4 own):
Round 3 (3 gate + 3 own):
Round 4 (4 findings): the
Round 5 (2 findings) and round 6 (3 findings): the null-safe-predicate restriction, twice raised.
Round 7 (2 findings): both on sentences this PR authored, both fixed.
Three findings not listed above were on my own The |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20260729-034400 |
|
cc @PedroTadim @nickitat — could you review this? It is the docs PR requested on #106540: the |
|
Workflow [PR], commit [70defdf] Summary: ✅ AI ReviewSummaryThis PR adds a short Findings
Final VerdictStatus: Minimum required actions:
|
| Hash tables in ClickHouse compare keys byte-wise, so different `NaN` bit patterns hash to | ||
| different buckets and are treated as distinct values by set-semantics operations including | ||
| `DISTINCT`, `GROUP BY`, `uniqExact`, `countDistinct`, and equi-`JOIN` on a `Float` key: | ||
| ClickHouse decides whether two `Float` values are the same key in one of two ways, and the two |
There was a problem hiding this comment.
Removed in 0e84018 (via f6001d4); the file is back to its master content, and the PR is now join.mdx only.
One consequence I want to flag rather than leave silent. That embedded documentation is what generates the Float page, and on master it states the byte-wise key-identity rule as a blanket fact, naming equi-JOIN among the operations that treat different NaN bit patterns as distinct. That holds for the hash joins only. Measured on this branch's merge base with l = {nan, 1.0} joined on equality:
right-side NaN |
hash / parallel_hash / grace_hash | full_sorting_merge / partial_merge |
|---|---|---|
same bit pattern (7FF8000000000000 both sides) |
matches | matches |
different bit pattern (nan vs 0./0.) |
no match | matches |
The paragraph in join.mdx linked to that page "for the details", which would have sent readers to text that contradicts it for the merge joins, so I dropped the pointer and the paragraph now stands on its own.
Which would you prefer: I open a separate PR correcting the Float page, I leave it as it is, or you would rather it said something different from what I had written? I did not want to open a follow-up you had not asked for.
| -- which explicitly leaves partial_merge's bitwise behavior unchanged. | ||
| -- with `compareAt`, under which any two NaNs are equal regardless of their bit | ||
| -- patterns, so NaN==NaN on the first key; the second key is what excludes the | ||
| -- pair. Note this test uses the same `nan` literal on both sides, so it |
There was a problem hiding this comment.
Removed in the same commit; the file is back to its master content and no longer part of this PR.
Just so it is on the record: those changes were comment-only, so nothing about the test's coverage changed either way. The comment on master says partial_merge compares float keys bitwise via nullableCompareAt, and that helper delegates to compareAt, under which any two NaN values are equal regardless of bit pattern. I will leave that as it is unless you want it corrected separately.
CI finish ledger — 0e84018Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
CI is fully finished on this head with no failing checks. Session id: cron:our-pr-ci-monitor:20260729-103000 |
The NULL section of the JOIN docs does not cover NaN. A scalar NaN = NaN is 0, but an equality JOIN key is compared by key identity, so NaN keys can match each other depending on their bit patterns and on the join implementation, and isNotDistinctFrom does not reliably separate them. Document the two workarounds and scope each one. On a key that is not Nullable, if(isNaN(A.id), NULL, A.id) = B.id excludes NaN. On a Nullable(Float) key that form also stops NULL keys from matching each other, because isNaN(NULL) is NULL, so a reader arriving from the isNotDistinctFrom section would silently lose those matches; the null-preserving form is documented alongside it, together with the join algorithm restriction its extra condition carries. Related: ClickHouse#106540 Related: ClickHouse#106531
0e84018 to
b39a22a
Compare
The Float page's embedded documentation states the byte-wise key-identity rule as a blanket fact and names equi-JOIN among the operations that treat different NaN bit patterns as distinct. That holds for the hash joins only: full_sorting_merge and partial_merge match NaN keys across differing bit patterns. It also presents if(isNaN(x), NULL, x) as working for JOIN keys in general, which breaks on a Nullable(Float) key. The correction to that page was removed from this PR on review, so the link sent readers from this paragraph to text contradicting it. The paragraph is self-contained, and Nullable(Float) two sentences below was already an unlinked type name, so drop the link rather than send readers to the contradiction.
…ntity The paragraph claimed an equality JOIN key is "compared by key identity". That describes only the byte-comparing algorithms: hash and direct compare the key bytes (bitEquals in HashTable.h), while full_sorting_merge and partial_merge go through the column comparator (MergeJoinTransform.cpp compareCursors and MergeJoin.cpp nullableCompareAt, both reaching ColumnVector::compareAt and FloatCompareHelper::compare), which returns 0 for any two NaN values regardless of payload. Calling both key identity contradicted this PR's own measured matrix. Naming the algorithm as the thing that decides it is not right either, because other layers decide it too. A LowCardinality(Float) key can arrive with its NaN payloads already collapsed into one dictionary entry, so two originally distinct payloads join as one key (measured: plain Float64 tables give 0 matches, the same two values in LowCardinality(Float64) tables give 1). And with the default enable_join_runtime_filters, a build side holding a single distinct key makes the runtime filter fall back to scalar equality, which rejects every NaN and drops the probe row before the join runs; that one is a separate defect and is not documented here as intended behaviour. So state what actually holds: whether two NaN keys match is unspecified and varies with the join implementation, the key type and the settings. The two comparators remain as illustrations, and the practical advice is unchanged, since it is what a reader should follow either way. Also drop "rather than by key identity" from the ASOF sentence, since ordering is the point there.
Co-Authored-By: Claude Fable 5 <[email protected]>
Proofreading only, no change to the described behavior or to the recommended predicate: - "in a float JOIN keys" -> "in float JOIN keys" (number agreement) - "If they actually do is" -> "Whether they actually do is" - "Do do not rely" -> "Do not rely"
.claude/CLAUDE.md requires literal names from the ClickHouse SQL language to be wrapped in inline code. The rest of this file already follows that (21 backticked occurrences of JOIN, none bare), so these two were inconsistent. No wording change.
Head branch was pushed to by a user without write access
| └─────────┴───────┘ | ||
| ``` | ||
|
|
||
| `NaN` values in float `JOIN` keys do not follow the `NULL` rule above. |
There was a problem hiding this comment.
docs/reference/ is the generated docs tree, not the hand-edited source. The repo instructions explicitly say to make documentation changes in docs/en/ and let the sync/generation pipeline update docs/reference/ afterwards.
Right now the corresponding source page, docs/en/sql-reference/statements/select/join.md, still has the old NULL values in JOIN keys section with no NaN paragraph at all. So this PR changes only the derived copy: the canonical docs stay unchanged, and the next sync can overwrite this edit.
Please move the heading/paragraph update to docs/en/sql-reference/statements/select/join.md instead of editing docs/reference/statements/select/join.mdx directly.
There was a problem hiding this comment.
I checked the premise before acting. On current master (07e66b53312d) the direction is the other way round.
- No tooling generates
docs/reference/fromdocs/en/.docs/_migration/is one-shot tooling that migrated pages in from the separateclickhouse-docsDocusaurus repo, is referenced by no CI job, and its own README says the folder can be deleted once the migration completes.autogenerate_docs.pygenerates from a binary's introspection tables intoAUTOGENERATEDregions, and this page has none. - The check that rule names, "No direct edits to generated or read-only docs", takes its protected list from
ci/jobs/scripts/docs/readonly_copies.json, which holds only the twoclickhouse-connectpaths.docs/reference/is not in it, andDocs check (Mintlify)is green on70defdfd. docs/README.mddescribesen/as legacy content from the previous Docusaurus site with "do not add new content here",docs/.mintignoreexcludes/en/from the published build, and this page is listed indocs/reference/navigation.json. There is nonavigation.jsonunderdocs/en/.- 118 of the 134 non-merge commits touching
docs/reference/in the last 90 days do not touchdocs/en/, so the trees are not kept in step.
.claude/CLAUDE.md:13 does say what you quote, but its two factual claims do not hold for this path: there is no sync to overwrite the edit, and the check it names does not cover docs/reference/. It reads stale against the 2026-07-14 Mintlify cutover recorded in docs/README.md.
Moving the paragraph to docs/en/ would place it on a page excluded from the published site, so I am leaving the change where it is.
CI finish ledger — 70defdfEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
All 174 other checks completed without failure ( Session id: cron:our-pr-ci-monitor:20260803-123000 |
Issue ClickHouse#113169 reports that indexOf matches NULL and inf but not nan, while Postgres array_position matches NaN. Both behaviours already exist in the tree and which one you get depends only on the column layout. Float32 and Float64 are in INTEGRAL_PACK (src/Functions/array/arrayIndex.h:703), which routes them to the typed compare overloads at :92-138 - raw left[i] == right[j], so IEEE-754 semantics and NaN never matches. Every other layout falls through to compareAt at :148, which for floats reaches FloatCompareHelper::compare (src/Core/CompareHelper.h:67-76); that returns 0 for two NaN values, so a LowCardinality, Tuple or nested array element does match. That is the same comparator ORDER BY uses, which is why the paragraph describes those layouts as compared by ordering. The behaviour is not changed here. The decision owner put NaN, -0.0, +0.0, -Inf and +Inf in the expected-behavior bucket on ClickHouse#112514 and ClickHouse#112502 and again on this issue, pointing at the docs pattern from ClickHouse#112409, so this documents the outcome as unspecified rather than promising either result. Scoped to indexOf. has, countEqual and indexOfAssumeSorted share the same FunctionArrayIndex implementation and mapContainsKey/mapContainsValue reuse it, and all measure 0; hasAll and hasAny are a separate implementation (FunctionArrayHasAllAny via sliceEqualElements) with the same observable result, so text under indexOf would not speak for them. Every sentence was measured against the built binary with negative controls: plain Array(Float32)/Array(Float64) return 0 while an ordinary needle returns 1; LowCardinality, Tuple and nested-array elements return 2 while the same layouts without a NaN element return 0; indexOf([1.1, NULL], NULL) returns 2, which is why the paragraph says NaN does not follow the NULL rule stated one line above; arrayFirstIndex(x -> isNaN(x), arr) returns 2 on the reported example, 1 with the NaN first, and 0 for a NaN-free and for an empty array. No tests: nothing observable changed, so a test could only restate system.functions. 02415_all_new_functions_must_be_documented still passes. The rendered pages under docs/reference/ come from the nightly regeneration, so only the C++ documentation string changes here. Closes: ClickHouse#113169
The paragraph said that "other layouts, such as a LowCardinality, Tuple or nested array element, are compared by ordering, in which NaN ties with itself". That is right for Tuple and nested-array elements, which reach compareAt and therefore FloatCompareHelper::compare, but wrong for LowCardinality: executeArrayLowCardinality runs first and matches by dictionary index (ColumnUnique::getOrFindValueIndex, then a bytewise getDataAt comparison), so it is not an ordering comparison at all, and the result flips on whether the needle is constant - measured 2 for a constant NaN needle and 0 for a non-constant one, in both the expression form and on a real Array(LowCardinality(Float64)) column, each with a non-NaN control. The surrounding sentences already say the outcome is an implementation detail not to be relied on, so a corrected LowCardinality rule would just hand the reader a second thing to rely on. Drop the mechanism clause instead of refining it, matching ClickHouse#112409, which states no mechanism either. The remaining Tuple and nested-array claim was re-measured in both needle constness shapes and on real columns, with non-NaN controls: 2 for the NaN row, 0 for the control, in all eight arms. Also split the 193-character sentence above it in two. Co-Authored-By: Claude Fable 5 <[email protected]>
Add a "Comparison of JOIN keys" section to the JOIN algorithms page and point the `ie_join` and block nested loop sections at it. A JOIN key is compared by the algorithm, not by `equals`. The hash family compares `Float` keys by their bits, so a `NaN` matches only a `NaN` with the same bit pattern; the sort-merge family compares with `IColumn::compareAt`, where any two `NaN` are equal; both match a `Tuple` with a `NULL` element and a `NULL` `Variant`, for which `equals` returns `NULL`. `ie_join` evaluates a remaining equality as a residual filter or post-join filter with `equals`, and the block nested loop fallback evaluates the whole `ON` condition, so neither matches those pairs. Measured per algorithm on master (26.11.1.1) with one row on each side. `-0.0` vs `0.0` and a `String` with trailing zero bytes vs `FixedString` are left out on purpose: ClickHouse#115970 and ClickHouse#118561 remove those differences. The JOIN statement page is not edited because ClickHouse#129276 rewrites it. Related: ClickHouse#129219 Related: ClickHouse#112409 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):
...
Description
Related: #106540
Closes: #106531
@ PedroTadim asked for a docs PR after #106540 was closed. This adds one paragraph to the
NULL values in JOIN keyssection ofjoin.mdx, which a reader would read as coveringNaN.An equality
JOINkey is not compared with the scalar=semantics, soNaN = NaNis0while aFloatJOINkey holdingNaNcan still match on the other side. The paragraph states that theoutcome is unspecified and varies with the join implementation, the key type and the settings, and
gives the two comparators as illustrations: the merge algorithms use
FloatCompareHelper::compare,which reports any two
NaNvalues equal, while the hash algorithms compare key bytes viabitEquals.It promises no per-algorithm outcome, because further layers change it: a
LowCardinality(Float)keycan arrive with its
NaNpayloads already collapsed to one dictionary entry, and the default-enabledjoin runtime filter drops matching
NaNprobe rows when the build side has one distinct key (aseparate defect).
isNotDistinctFromdoes not separateNaNeither; theparagraph gives a form that does, and scopes itself away from
ASOF.Per review this is docs-only: the
Float-page correction and a stale test comment were removed at@ PedroTadim's request. The paragraph no longer points at the
Floatpage, because that page statesthe byte-wise rule as a blanket fact. Measured on master with runtime filters off:
NaN7FF8000000000000both sides)nanvs0./0.)I have asked in the review thread how you would like that page handled rather than opening a follow-up
you did not ask for. The
docs/encopies are untouched.Version info
26.8.1.676(included in26.8and later)