Skip to content

Fix a join returning rows whose WHERE equality of Tuple or Variant values is NULL - #124854

Merged
m-selmi merged 4 commits into
ClickHouse:masterfrom
groeneai:where-equality-null-in-tuple-join-key
Oct 15, 2026
Merged

m-selmi merged 4 commits into
ClickHouse:masterfrom
groeneai:where-equality-null-in-tuple-join-key

Conversation

@groeneai

@groeneai groeneai commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #125936
Related: #67493
Related: #116358

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 Tuple values with Nullable elements, or between Variant values, over a comma, CROSS or INNER join: the pair whose equality is NULL, such as tuple(NULL) = tuple(NULL), was returned. With allow_dynamic_type_in_join_keys = 0, a comma or CROSS join with a WHERE equality on a Dynamic value no longer returns no rows or fails with ILLEGAL_COLUMN.

Description

On 26.8.21.12 (and 26.3.33.24, master), SELECT ta.k, tb.k FROM ta, tb WHERE ta.t = tb.t over Tuple(Nullable(UInt64)) columns returns the pair of tuple(NULL) rows although the equality is NULL. The same holds for CROSS JOIN, INNER JOIN ... ON ta.k = tb.k WHERE ta.t = tb.t, LowCardinality(Nullable) or nested-tuple elements, and Variant values. Over 2M rows with 10% tuple(NULL) the comma query counted 40001800000 rows instead of 1800000.

CrossToInnerJoinPass and mergeFilterIntoJoinCondition turn such an equality into a JOIN key and drop it from WHERE. A JOIN key matches a NULL inside a Tuple, or a NULL Variant, as an ordinary value (the intended JOIN ON semantics, #67493), while equals returns NULL. Since #125195 (master only), (l.key, l.x) = (r.key, NULL) also reaches both places as one tuple equality, so this fixes #125936 too.

The change: for these types both places still use the equality as a JOIN key and also keep it as a filter. The filter only sees pairs the key matched (or, pushed down, t = t per side), so it cannot hit a Variant type mismatch (NO_COMMON_TYPE); the plan optimization adds the key once. The cost is one equals per joined row, for these types only; the key and the hash join stay. JOIN ON and other types are unchanged. In 04665 a Dynamic key now keeps its filter too. With allow_dynamic_type_in_join_keys = 0, CrossToInnerJoinPass now leaves a Dynamic equality in WHERE, like mergeFilterIntoJoinCondition. Float keys (-0.0 vs 0, #116358) are a separate case, not changed here.

Validation: the new test fails on master in 17 of 21 lines and passes with the fix, 50/50 with random settings. Each part of the change is proven necessary by a deletion mutation. 2005 existing join/tuple/variant/dynamic tests: only 04665 changed.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.2480-master (included in 26.10 and later)

groeneai and others added 2 commits October 9, 2026 07:06
…lter when it becomes a JOIN key

A JOIN key matches a NULL nested in a Tuple, or a NULL Variant or Dynamic, as an ordinary value,
while `equals` returns NULL for such a pair, so a WHERE equality turned into a JOIN key returned
rows the WHERE drops. `CrossToInnerJoinPass` and `mergeFilterIntoJoinCondition` still use such an
equality as a JOIN key, and now also keep it as a filter. The kept filter only sees pairs the key
matched, so it cannot hit a Variant type mismatch, and the merge adds the key to the join once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the two call-site comments on keyMatchesNullAsValue to one line each; drop the narration of the add-once check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-self PR origin: self-found defect (impact-bar validated) labels Oct 9, 2026
@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
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 (2 nits).

# Sev Finding Verdict Evidence / action
1 💡 An equality where only one side can hold such a NULL (e.g. Int32 = Dynamic in 04665) also keeps the filter, although a key-matched pair has no NULL there. AGREE, not changed Narrowing to "both sides" would be wrong for Nullable(Int32) = Variant/Dynamic (the NULL cast into the supertype matches as a value).
2 💡 No test line for a top-level Dynamic NULL (with allow_dynamic_type_in_join_keys = 1), which the change also fixes. AGREE, not changed Same canContainNull path as Variant; 04665 covers the Dynamic plan.
3 💡 PR description links. AGREE, fixed Description now links #116358 and states the cost.

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-self-slot-58:20261009-071200

@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Default settings: SELECT ta.k, tb.k FROM ta, tb WHERE ta.t = tb.t over Tuple(Nullable(UInt64)) returns the tuple(NULL) pair every run (master, 26.8.21.12, 26.3.33.24).
b Root cause explained? CrossToInnerJoinPass and mergeFilterIntoJoinCondition turn the equality into a JOIN key and drop it from WHERE. The key matches a NULL inside a Tuple, or a NULL Variant, as a value; equals returns NULL for that pair.
c Fix matches root cause? Yes. At both sites the equality stays a JOIN key and is also kept as a filter for exactly these types.
d Test intent preserved / new tests added? New 05340 (19 lines: comma, CROSS, INNER, rewrite = 0/1/2, merge on/off, subquery filter, plan checks). 04665 reference: the Dynamic key now keeps its filter (0 -> 1).
e Both directions demonstrated? Master: 15 of 19 lines print the extra NULL pair. Fix: matches the reference. 50/50 with random settings.
f Fix is general across code paths? Both carriers fixed through one helper; a deletion mutation of each part reddens the test (without "added once": TOO_MANY_QUERY_PLAN_OPTIMIZATIONS). JOIN ON unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? 13 types x 5 forms vs ground truth: Tuple with NULL-capable elements (plain, named, nested, Nullable(Tuple)), Variant fixed; top-level Nullable/LowCardinality(Nullable), Array, Map, NULL-free Tuple unchanged and correct.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; only the wrong NULL pairs disappear. A type-mismatched Variant pair does not throw (the kept filter sees only key-matched pairs).
i Invariants and contracts preserved? The filter DAG is untouched unless a conjunct moved out; the key is added once, so the optimization loop terminates. 2005 join/tuple/variant/dynamic tests A/B vs master: only 04665 changed.

Session id: cron:clickhouse-impl-self-slot-57:20261009-054100

@groeneai
groeneai requested review from nickitat and vdimir October 9, 2026 07:54
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6986b4b]

Summary: ✅


AI Review

Summary

This PR fixes a real correctness gap in the join rewrites: when a WHERE equality over Tuple values with nested NULLs or over Variant/Dynamic values is promoted into a join key, the key can match rows that equals would evaluate to NULL. The current diff fixes both rewrite sites consistently, preserves the filter only for the affected type families, and also closes the previously reported Dynamic-key regression when allow_dynamic_type_in_join_keys = 0. I do not see any blocking or major issues in the current revision.

Missing context / blind spots
  • ⚠️ No local build or test artifacts were available in this checkout, so I did not re-run tests/queries/0_stateless/05340_where_equality_null_in_tuple_join_key.sql or tests/queries/0_stateless/04665_merge_filter_into_join_dynamic_key.sql; this verdict is based on the current code, the added test coverage, and the full PR discussion.
Final Verdict

✅ No new inline findings for the current diff.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Oct 9, 2026
…c keys are disallowed

With `allow_dynamic_type_in_join_keys = 0` (the default) the planner rejects a `Dynamic`
JOIN key with ILLEGAL_COLUMN. `CrossToInnerJoinPass` still turned a comma/CROSS
`WHERE a = d` into such a key, so a valid filtered cross join threw instead of
filtering. `mergeFilterIntoJoinCondition` already leaves such an equality in the filter;
the analyzer pass now does the same.

`FROM t_int, t_dyn WHERE a = d` returns the matching rows (it threw on master and
returned no rows on 26.8.18.2 / 26.3.42.3), the same as with
`cross_to_inner_join_rewrite = 0`.

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

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 6986b4b4ea5ced776433ea08fd4c33a4ece6af12 was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "file", "type": "LowCardinality(String)" …).

See the job log for details.

@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 5d3339e

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, s3 storage, DBReplicated, parallel, 1/2) / 05316_secret_arguments_declared_by_engines test regression from merged #123364, not from this PR: under DatabaseReplicated the final query_log count returns 0 2 instead of 0 1 (the password stays hidden; the CREATE DICTIONARY row is counted twice). 22/22 reruns pass; the same diff is on 33 PRs in this lane today a fix task is created (investigating at full effort, fixing-PR link to follow here)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 05233_string_filters_fixed_string_constant flaky, not from this PR: the last query_log ProfileEvents check reads 0 instead of 1; under parallel replicas the scan events can land only in the remote replica's row, which the current_database = currentDatabase() filter drops. 123/123 reruns pass #124911 (mine, open)
Mergeable Check, PR praktika rollups of the two rows above, no independent failure #124911; a fix task is created (05316)

Session id: cron:our-pr-ci-monitor:20261009-140011

@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for 05316_secret_arguments_declared_by_engines (DatabaseReplicated 0 1 -> 0 2) from the CI finish ledger above: #124928 (external, open).

@m-selmi m-selmi self-assigned this Oct 12, 2026
@m-selmi

m-selmi commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

@groeneai merge master to pickup fix for 05316_secret_arguments_declared_by_engines.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Done: merged master at 4f53afc, which includes the 05316_secret_arguments_declared_by_engines fix from #124928. New head: 6986b4b.

@m-selmi
m-selmi added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ClickHouse:master with commit 8493c80 Oct 15, 2026
177 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 15, 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... groeneai-origin-self PR origin: self-found defect (impact-bar validated) 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.

Tuple = with a NULL element in WHERE over a JOIN returns extra rows

3 participants