Skip to content

Do not treat a NULL inside a tuple key as a NULL key - #119081

Merged
nihalzp merged 1 commit into
masterfrom
nullable-tuple-join-key-null-element-118816
Sep 10, 2026
Merged

nihalzp merged 1 commit into
masterfrom
nullable-tuple-join-key-null-element-118816

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

For a Nullable(Tuple(Nullable(T))) join key that is never NULL at the top level, the hash-family algorithms treated any row whose tuple contains a NULL element as a NULL key and dropped it, while the merge algorithms compare the nested tuples and kept it. The same query on the same data returned a different row count depending only on join_algorithm (hash / parallel_hash / grace_hash = 1, full_sorting_merge / partial_merge = 2), and wrapping a key in toNullable - a value-wise no-op there - was enough to change the result.

extractNestedColumnsAndNullMap folded the null maps of the tuple's elements into the key's null map. Only the top level says whether the key of a row is NULL; the NULLs of the elements are part of the key and are compared as any other value, exactly as they are for a Tuple(Nullable(T)) that is not wrapped in Nullable. A row whose key really is NULL still joins nothing, and it is not the same key as a tuple of NULLs.

The same extraction is used by IN with a subquery, which had the mirror defect: such a row matched nothing there either.

Closes: #118816

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 to CHANGELOG.md):

Fixed the hash-family join algorithms treating a non-NULL Nullable(Tuple(...)) key that contains a NULL element as a NULL key, which made the result of a join depend on the join_algorithm setting. IN with a subquery over such a key was affected in the same way.


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

Version info

  • Merged into: 26.9.1.1101 (included in 26.9 and later)

`extractNestedColumnsAndNullMap` folded the null maps of the elements of
a `Nullable(Tuple(Nullable(T), ...))` key into the key's null map, so a
row whose tuple merely contains a NULL was a NULL key. The hash-family
joins dropped such a row, while the merge algorithms compare the nested
tuples and kept it, so the result of a join depended on
`join_algorithm`; wrapping a key that is never NULL in `toNullable` was
enough to change it.

Only the top level says whether the key of a row is NULL. The NULLs of
the elements are part of the key and are compared as any other value,
exactly as they are for a `Tuple(Nullable(T), ...)` that is not wrapped
in `Nullable`.

Closes: #118816
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ac653e2]

Summary: ✅


AI Review

Summary

This PR fixes the hash-family join handling of Nullable(Tuple(Nullable(T))) keys by stopping extractNestedColumnsAndNullMap from folding inner tuple-element null maps into the top-level null-key mask. That join fix looks correct, but the same helper is also the null-key filter for subquery-backed IN, and there the change breaks the established transform_null_in = 0 contract instead of just removing a join inconsistency.

Findings

❌ Blockers

  • src/DataTypes/NullableUtils.cpp:98: Set still defines transform_null_in = 0 as “ignore any tuples with at least one NULL element” (src/Interpreters/Set.h:119), and the existing IN coverage keeps that behavior for both Tuple(Nullable(...)) and Nullable(Tuple(Nullable(...))) (tests/queries/0_stateless/04234_non_constant_rhs_in.reference:84-90). After this helper change, only subquery/table-backed IN over the wrapped tuple shape starts returning 1, while literal and row-wise IN still return 0, so the PR introduces a new semantic divergence. The join fix needs to be scoped away from Set, or the Set contract needs a consistent cross-implementation change.
Final Verdict

❌ Changes requested.

LLVM Coverage Report

Measured on commit ac653e2.

Metric Baseline Current Δ
Lines 88.90% 88.90% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 2/2 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 9, 2026
* The NULLs of the elements are part of the key and are compared as any other value,
* exactly as they are for a `Tuple(Nullable(T), ...)` that is not wrapped in `Nullable`.
*/
column = &column_nullable->getNestedColumn();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

extractNestedColumnsAndNullMap is also the null-key filter for subquery-backed IN, not just joins. Set still documents the transform_null_in = 0 contract as “ignore any tuples with at least one NULL element” (src/Interpreters/Set.h:119), and the existing non-subquery coverage keeps that behavior for both Tuple(Nullable(...)) and Nullable(Tuple(Nullable(...))) (tests/queries/0_stateless/04234_non_constant_rhs_in.reference:84-90).

With this change, only the subquery/table-backed IN path flips to 1 for the wrapped tuple shape, while literal and row-wise IN still return 0, so we trade one inconsistency for another. I think the relaxed null-map handling needs to stay join-only, or Set semantics need to be changed consistently across all IN implementations in a broader compatibility change.

@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 Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ac653e276 with master b75d29e7d (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline.

Job report

@nihalzp nihalzp self-assigned this Sep 10, 2026
@nihalzp
nihalzp added this pull request to the merge queue Sep 10, 2026
Merged via the queue into master with commit 490ec0b Sep 10, 2026
174 checks passed
@nihalzp
nihalzp deleted the nullable-tuple-join-key-null-element-118816 branch September 10, 2026 05:33
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-bugfix Pull request with bugfix, not backported by default 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.

Hash-family joins treat a non-NULL Nullable(Tuple) key with a NULL element as a NULL key: row count depends on join_algorithm

3 participants