Skip to content

Fix a mixed JOIN ON condition evaluated over mismatched column types for a dictionary - #113534

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-mixed-join-dict-nullable
Aug 9, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-mixed-join-dict-nullable

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Related: #112831

A mixed join condition is a cross-side non-equi residual in the ON clause, for example ON (t.key = d.key) AND (t.a * 10 < d.a). Only the hash family evaluates one: HashJoin passes TableJoin::getMixedJoinExpression into AddedColumns::additional_filter_expression and applies it while matching, resolving the right columns it needs by name against the stored right blocks.

buildPhysicalJoinImpl drops the join_use_nulls conversion of the right columns from the right-side expression when the right side is a prepared storage, because such a storage delivers its columns already converted, and for a key-value storage it merges the conversion back into the right-side DAG under the original column names. But DirectKeyValueJoin declines a mixed condition, so a join onto a dictionary that carries one runs an ordinary algorithm which reads the dictionary as a stream - and is handed a right side whose a is Nullable(UInt32) under the very name the mixed condition declared as UInt32. buildAdditionalFilter creates the column from the declared type and fills it with insertFrom from the stored one, so a ColumnUInt32 was filled from a ColumnNullable.

CREATE TABLE dsrc (key UInt64, a UInt32) ENGINE = Memory;
INSERT INTO dsrc VALUES (1, 100), (2, 20);
CREATE DICTIONARY dict (key UInt64, a UInt32) PRIMARY KEY key
    SOURCE(CLICKHOUSE(TABLE 'dsrc')) LIFETIME(0) LAYOUT(FLAT());
CREATE TABLE t (key UInt64, a UInt32) ENGINE = Memory;
INSERT INTO t VALUES (1, 1), (2, 2), (3, 3);

SELECT count(), sum(d.a) FROM t LEFT ANY JOIN dict AS d
    ON (t.key = d.key) AND (t.a * 10 < d.a)
SETTINGS allow_experimental_join_condition = 1, join_use_nulls = 1, join_algorithm = 'hash';

Only key 1 satisfies the residual, so the answer is 3, 100. A release build returned 3, 120, having matched key 2 as well; a sanitizer build aborted on the IColumn::insertFrom type assertion.

The fix keeps the conversion in the right-side expression when a key-value storage carries a mixed condition, so the join applies join_use_nulls itself exactly as it does for an ordinary table and the mixed condition keeps the non-Nullable columns it was built on. StorageJoin is unaffected - it rejects a mixed condition outright with INCOMPATIBLE_TYPE_OF_JOIN. HashJoin now also compares the type and not only the name when it resolves those columns, so a future divergence throws where both types are still known instead of reading a column through a mismatched IColumn interface.

The shape has been reachable with an explicit join_algorithm = 'hash' for as long as the mixed condition and the prepared-storage handling have coexisted. #112831 made DirectKeyValueJoin decline a mixed condition, which put it on the default join_algorithm path and into the new 04667_mixed_join_condition_algorithm_selection test, so the assertion started firing in most stress runs from 2026-08-04 - STID 2508-30f6 and 2508-2e0c, which differ only by the concurrent-hash-join wrapper in the stack. No tracking issue was open for either STID.

CI report the fix was written from: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=0f1b923e6247ef3d8409635a69738c7f3f02b84a&name_0=MasterCI&name_1=Stress%20test%20%28azure%2C%20amd_tsan%29

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):

Fix wrong results and, in a build with assertions enabled, an aborted assertion for a JOIN onto a dictionary whose ON clause has a non-equi condition over both tables, such as ON (t.key = d.key) AND (t.a * 10 < d.a), when join_use_nulls is enabled. The condition was evaluated over a column read through a mismatched type, so rows could match arbitrarily.

Version info

  • Backported to: 26.7.4.35

…for a dictionary

A mixed join condition is a cross-side non-equi residual in the `ON` clause, for example
`ON (t.key = d.key) AND (t.a * 10 < d.a)`. Only the hash family evaluates one: `HashJoin`
passes `TableJoin::getMixedJoinExpression` into `AddedColumns::additional_filter_expression`
and applies it while matching, resolving the right columns it needs by name against the
stored right blocks.

`buildPhysicalJoinImpl` drops the `join_use_nulls` conversion of the right columns from the
right-side expression when the right side is a prepared storage, because such a storage
delivers its columns already converted, and for a key-value storage it merges the conversion
back into the right-side DAG under the original column names. But `DirectKeyValueJoin`
declines a mixed condition, so a join onto a dictionary that carries one runs an ordinary
algorithm which reads the dictionary as a stream - and is handed a right side whose `a` is
`Nullable(UInt32)` under the very name the mixed condition declared as `UInt32`.
`buildAdditionalFilter` creates the column from the declared type and fills it with
`insertFrom` from the stored one, so a `ColumnUInt32` was filled from a `ColumnNullable`: an
aborted assertion under sanitizers, and arbitrary matching in a release build.

    CREATE TABLE dsrc (key UInt64, a UInt32) ENGINE = Memory;
    INSERT INTO dsrc VALUES (1, 100), (2, 20);
    CREATE DICTIONARY dict (key UInt64, a UInt32) PRIMARY KEY key
        SOURCE(CLICKHOUSE(TABLE 'dsrc')) LIFETIME(0) LAYOUT(FLAT());
    CREATE TABLE t (key UInt64, a UInt32) ENGINE = Memory;
    INSERT INTO t VALUES (1, 1), (2, 2), (3, 3);

    SELECT count(), sum(d.a) FROM t LEFT ANY JOIN dict AS d
        ON (t.key = d.key) AND (t.a * 10 < d.a)
    SETTINGS allow_experimental_join_condition = 1, join_use_nulls = 1, join_algorithm = 'hash';

Only key 1 satisfies the residual, so the answer is `3, 100`; the release build returned
`3, 120`, having matched key 2 as well.

Keep the conversion in the right-side expression when a key-value storage carries a mixed
condition, so the join applies `join_use_nulls` itself exactly as it does for an ordinary
table and the mixed condition keeps the non-`Nullable` columns it was built on. `StorageJoin`
is unaffected: it rejects a mixed condition outright with `INCOMPATIBLE_TYPE_OF_JOIN`.

Also compare the type and not only the name when `HashJoin` resolves the right columns the
mixed condition needs, so a future divergence throws where both types are still known instead
of reading a column through a mismatched `IColumn` interface.

The shape has been reachable with an explicit `join_algorithm = 'hash'` for as long as the
mixed condition and the prepared-storage handling have coexisted. #112831 made
`DirectKeyValueJoin` decline a mixed condition, which put it on the default `join_algorithm`
path and into the new `04667_mixed_join_condition_algorithm_selection` test, and the assertion
started firing in most stress runs from 2026-08-04 (STID 2508-30f6 and 2508-2e0c, which differ
only by the concurrent-hash-join wrapper in the stack).

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=0f1b923e6247ef3d8409635a69738c7f3f02b84a&name_0=MasterCI&name_1=Stress%20test%20%28azure%2C%20amd_tsan%29
Related: #112831

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d6bfa48]

Summary: ✅


AI Review

Summary

This PR keeps mixed JOIN ... ON predicates over key-value-backed right sides on the hash-family path aligned with the ordinary-table behavior when join_use_nulls is enabled, and adds a HashJoin type check so a future name/type divergence fails closed instead of reading a column through a mismatched interface. The change is consistent with the current join-algorithm fallback logic and the added regression coverage exercises the default/direct-fallback and explicit hash-family paths. I did not find a remaining correctness, metadata, or test-gap issue in the current diff.

Final Verdict

✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.80% +0.00%

Changed lines: Changed C/C++ lines covered: 31/37 (83.78%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 5, 2026
…-nullable

# Conflicts:
#	src/Processors/QueryPlan/JoinStepLogical.cpp
Merged via the queue into master with commit cae1a75 Aug 9, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-mixed-join-dict-nullable branch August 9, 2026 21:02
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 9, 2026
alexey-milovidov added a commit to ArtemovMichael/ClickHouse that referenced this pull request Aug 9, 2026
Pull in the fix from ClickHouse#113534 for the
unrelated `Stress test (amd_tsan)` failure (logical error in
`IColumn::assertTypeEquality` from `buildAdditionalFilter`,
ClickHouse#113931).
alexey-milovidov added a commit that referenced this pull request Aug 10, 2026
Pull in the fix for the `assertTypeEquality` logical error in
`HashJoin::buildAdditionalFilter` (#113534, merged 2026-08-09), which was the
sole remaining CI failure on this branch (Stress test (amd_asan_ubsan),
issue #113931) and is unrelated to this pull request.
valerypetrov added a commit to valerypetrov/ClickHouse that referenced this pull request Aug 10, 2026
The stress test (amd_tsan) failure was an unrelated, already-fixed JOIN bug
(dictionary right side + join_use_nulls, see issue ClickHouse#113931, fixed by ClickHouse#113534
which merged into master after this branch's last push). Retriggering CI to
pick up the fix.
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 12, 2026
robot-clickhouse added a commit that referenced this pull request Aug 13, 2026
Cherry pick #113534 to 26.7: Fix a mixed JOIN ON condition evaluated over mismatched column types for a dictionary
robot-clickhouse added a commit that referenced this pull request Aug 13, 2026
…r mismatched column types for a dictionary
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 13, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 14, 2026
Backport #113534 to 26.7: Fix a mixed JOIN ON condition evaluated over mismatched column types for a dictionary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.7-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants