Skip to content

Docs: how each JOIN algorithm evaluates an equality in the ON clause - #129361

Merged
vdimir merged 4 commits into
ClickHouse:masterfrom
groeneai:docs-join-key-comparison-per-algorithm
Nov 5, 2026
Merged

vdimir merged 4 commits into
ClickHouse:masterfrom
groeneai:docs-join-key-comparison-per-algorithm

Conversation

@groeneai

@groeneai groeneai commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #129219
Related: #112409

Changelog category (leave one):

  • Documentation (changelog entry is not required)

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

...

Description

@ vdimir asked on #129219 to document, per JOIN algorithm, where an ON equality used as a JOIN key differs from equals, since the same query can return different rows depending on join_algorithm.

Adds a "Comparison of JOIN keys" section to the JOIN algorithms page and points the ie_join and block nested loop sections at it. As a key, two NaN values match (the hash family only for the same bit pattern), as do two (1, NULL) tuples and two NULL Variant values. ie_join and the block nested loop fallback use equals and match none of them. The section also notes two cases where the NaN cell depends on more than the algorithm: a direct JOIN into a dictionary that reads the keys from a CLICKHOUSE source matches only the bit pattern of nan, and with a non-zero max_rows_in_set_to_optimize_join, whether full_sorting_merge matches two NaN values with different bit patterns can vary from run to run.

-0.0 and String/FixedString padding are left out: #115970 and #118561 remove those differences. The JOIN statement page is untouched because #129276 rewrites it.

Validation: I measured every cell on master (26.11.1.1), one row per side, for each join_algorithm value, the block nested loop fallback, direct (dictionary, EmbeddedRocksDB, MergeTree), the Join engine and ASOF.

🤖 Generated with Claude Code


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

Version info

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

groeneai and others added 4 commits November 4, 2026 16:13
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]>
…erge JOINs

The set filter that `max_rows_in_set_to_optimize_join` adds before the sorts of a `full_sorting_merge` JOIN compares keys by bits and applies only to blocks that arrive after the other side's set is complete, so `NaN` keys with different bit patterns match in some runs and not in others.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…N keys in direct JOINs

A dictionary that fetches the requested keys from its source writes them into a text query, where every `NaN` becomes `nan`, so a `direct` JOIN into it matches only the bit pattern of `nan`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s to the source as text

The `complex_key_ssd_cache` layout fetches missing keys through the same `CacheDictionary` path as `complex_key_cache`, so a `direct` JOIN into it also matches only the bit pattern of `nan`. Also split the paragraph below the key comparison table into three.

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

mintlify Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit e987a52.

Approve preview

@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)
Finding Verdict
⚠️ The merge-family NaN cell does not hold with a non-zero max_rows_in_set_to_optimize_join: the pre-sort set filter compares keys by bits AGREE, fixed in this PR: measured, different-bit NaN keys then match in some runs only (16 to 29 of 50); one sentence added
⚠️ The direct NaN cell does not hold for a dictionary that fetches the requested keys from a CLICKHOUSE source: the keys go into a text query where every NaN is written as nan AGREE, fixed in this PR: measured, complex_key_direct, complex_key_cache and complex_key_ssd_cache match only the bit pattern of nan; one sentence added
⚠️ A NULL Variant key would miss in direct lookups into EmbeddedRocksDB / KeeperMap / Redis DISAGREE: none of these engines accepts a Variant key (DATA_TYPE_CANNOT_BE_USED_IN_KEY, KeyDescription.cpp)
💡 direct lookups into EmbeddedRocksDB / KeeperMap / Redis widen a Float32 key to Float64 and back, which quiets a signaling NaN Noted, not blocking: only a signaling NaN changes, which no computation or text input produces, and the result (no match) agrees with equals
💡 The first sentence of the new section could exclude an equality under OR with a keyless disjunct Noted, not blocking: covered by the block nested loop paragraph

@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

cc @Blargian, could you review this and add the can be tested label so CI runs? It adds a "Comparison of JOIN keys" section to the JOIN algorithms page showing, per algorithm, where an ON equality used as a JOIN key matches values that equals does not (NaN, (1, NULL) tuples, NULL Variant), as @ vdimir asked on #129219.

@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e987a52]

Summary: ✅


AI Review

Summary

This documentation change explains how JOIN key comparisons differ from scalar equals across algorithms for NaN, tuples containing NULL, and Variant nulls. It also documents the dictionary text-conversion and merge-input set-filter caveats. The current implementations support the added descriptions; no actionable findings remain.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-documentation Documentation PRs for the specific code PR can be tested Allows running workflows for external contributors comp-documentation Documentation (docs, examples, READMEs). labels Nov 4, 2026
@vdimir vdimir self-assigned this Nov 5, 2026
@vdimir
vdimir added this pull request to the merge queue Nov 5, 2026
Merged via the queue into ClickHouse:master with commit d7a894a Nov 5, 2026
548 of 550 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 5, 2026
zoomxi added a commit to zoomxi/ClickHouse that referenced this pull request Nov 19, 2026
ClickHouse#129219 concluded that `ie_join` should not yield to another algorithm when a residual `equals` differs from a join key: the remaining differences (`NaN`, `NULL` inside tuples) are documented per algorithm in ClickHouse#129361, and ClickHouse#118561 makes `String`/`FixedString` keys, including `ie_join` range keys, compare like the scalar functions.

The `hash` requirement for merging ranges now states its actual reason: a merged range is a non-equi `ON` condition. Add the `String`/`FixedString` regressions from the review of ClickHouse#128140, and drop the `parallel_hash` case: `parallel_hash` is now the same algorithm as `hash`.

Related: ClickHouse#129219
Related: ClickHouse#128140
zoomxi added a commit to zoomxi/ClickHouse that referenced this pull request Nov 19, 2026
Drop the `String`/`FixedString` cases (fixed in master by ClickHouse#118561), the `NaN`/`Tuple(Nullable)` key cases (documented per algorithm in ClickHouse#129361), and the reorder cases that repeat one query under other join or reorder algorithms. Shorten the comment of the performance test.
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-documentation Documentation (docs, examples, READMEs). pr-documentation Documentation PRs for the specific code PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants