Skip to content

Fix mixed JOIN ON condition over a dictionary with join_use_nulls - #113933

Closed
alexey-milovidov wants to merge 3 commits into
masterfrom
fix/mixed-join-condition-dict-join-use-nulls
Closed

alexey-milovidov wants to merge 3 commits into
masterfrom
fix/mixed-join-condition-dict-join-use-nulls

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

With join_use_nulls = 1, a mixed (cross-side non-equi) ON condition over a dictionary right side returned wrong results in release builds and failed a type-equality chassert in debug builds — the dominant stress-test failure STID: 2508-2e0c, hitting master and dozens of pull requests daily since 2026-08-04.

Since #112831, such a join correctly declines the direct-join lookup and falls back to a hash join. But for a prepared join storage the plan converts the right columns of the outer join to Nullable in place, without keeping the raw input column the mixed condition was built against (a plain table keeps both toNullable(__table2.a) and the raw __table2.a). buildAdditionalFilter binds the filter's required columns to the stored right block by name, creates the column with the filter's plain type, and insertFrom then read a Nullable column through a non-Nullable one: garbage filter input in release (SELECT count(), sum(d.a) FROM t3 LEFT ANY JOIN dict AS d ON (t3.key = d.key) AND (t3.a * 10 < d.a) SETTINGS join_use_nulls = 1 returned 4, 80 instead of 4, 60), a failed chassert in IColumn::assertTypeEquality in debug.

The fix unwraps the Nullable when filling the filter's right-side columns. The stored rows are real right-side rows, never NULL-extended, so the unwrap is exact; a genuine NULL now throws a LOGICAL_ERROR exception instead of being misread.

Added 04821_mixed_join_condition_dictionary_join_use_nulls covering LEFT ANY/LEFT/INNER/SEMI/ANTI with hash and parallel_hash, and verifying unmatched rows are NULL-extended. The existing 04667_mixed_join_condition_algorithm_selection still passes.

Closes: #113931
Related: #112831

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 a logical-error exception in debug builds) for a mixed JOIN ON condition (equality plus a cross-side inequality) over a dictionary when join_use_nulls is enabled.

When a prepared join storage (a dictionary lookup) with `join_use_nulls`
falls back from a direct join to a hash join because of a mixed
(cross-side non-equi) `ON` condition, the plan converts the right
columns of the outer join to `Nullable` in place, without keeping the
raw input column the mixed condition was built against. The stored
right block then holds `Nullable(T)` under the name for which
`buildAdditionalFilter` creates a plain `T` column, and `insertFrom`
read the column through the wrong type: wrong results in release builds
(the residual matched arbitrary rows) and a failed type-equality
`chassert` in debug stress runs (STID 2508-2e0c).

Unwrap the `Nullable` when filling the filter's right-side columns; the
stored rows are real right-side rows, never NULL-extended, so the
unwrap is exact, and a genuine NULL now throws a `LOGICAL_ERROR`
exception instead of being misread.

Closes: #113931

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fceb4cb]

Summary: ❌

job_name test_name status info comment
Stress test (amd_msan) FAIL
S3_ERROR No such key thrown (see clickhouse-server.log or no_such_key_errors.txt) FAIL cidb

AI Review

Summary

This PR fixes wrong results for mixed JOIN ON predicates over dictionary-backed joins with join_use_nulls = 1 by unwrapping stored right-side Nullable columns before evaluating the additional hash-join filter. I traced the current fceb4cbc diff, including the previously raised LowCardinality(Nullable(...)) carrier, and I did not find any remaining correctness, compatibility, or test-contract issues that warrant a new inline finding.

Missing context / blind spots
  • ⚠️ The workspace checkout is GitHub's synthetic pull/113933/merge commit rather than the raw PR head object, so I cross-checked the changed files directly from GitHub at fceb4cbc3851020de570d1df8cf7c2677f4a8e1f before concluding.
  • ⚠️ In this environment I only had the PR's Build profile diff Praktika report and no local build directory, so I could not execute 04821_mixed_join_condition_dictionary_join_use_nulls myself.
Final Verdict

✅ No new blockers or majors. The current head fixes the earlier LowCardinality(Nullable(...)) review point as well, so I resolved that outdated bot thread and did not post a new review batch.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.60% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.90% +0.10%

Changed lines: Changed C/C++ lines covered: 16/21 (76.19%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fceb4cbc3 with master 9b6a2d734 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e2194b4a8; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 690.65 MiB 687.63 MiB -3.01 MiB (-0.44%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

…nal join filter like plain `Nullable`

`JoinUtils::tryConvertColumnToNullable` wraps a right-side `LowCardinality(T)`
column as `LowCardinality(Nullable(T))`, which reports `isNullable() == false`
and exposes the nullability only through `nestedIsNullable`. The previous
branch handled only plain `ColumnNullable`, so such a column fell through to
`insertFrom`, where `ColumnLowCardinality::insertFrom` converts a genuine NULL
dictionary entry into the nested default value instead of rejecting it. Detect
the nested-nullable source, throw a logical error on a genuine NULL, and insert
the non-NULL value through a `Field` round-trip on this cold path.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Superseded by #113534, which merged into master as cae1a75dcefc.

Both pull requests fix the same defect (issue #113931) at two different layers, and they are semantically incompatible: #113534 fixes it at the plan level (JoinStepLogical) and adds a type check in HashJoin, which makes the Nullable unwrap in buildAdditionalFilter proposed here unreachable — and its tolerance of a mismatched type would contradict the invariant #113534 establishes. Per the plan agreed in #107567 (comment), this pull request was kept open only as a fallback in case #113534 did not land.

Test coverage is not lost: the merged 04811_mixed_join_condition_dictionary_join_use_nulls is a superset of 04821_mixed_join_condition_dictionary_join_use_nulls proposed here — it covers the same join strictnesses (LEFT ANY/ALL/SEMI/ANTI/INNER) and algorithms, plus grace_hash, the join_use_nulls = 0 case, and a check that a plain equi join still uses the direct join.

Thanks @groeneai for the measured analysis that established the two pull requests were the same defect at two layers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

1 participant