Repository navigation
Fix Bad cast crash for an explicit JOIN USING key with a Dynamic supertype - #107289
Conversation
…rtype An explicit reference t.key to a JOIN USING key adopts the merged key type (the common supertype of the joined keys) in IdentifierResolver::tryResolveIdentifierFromJoin. When the current column was Nullable from a previous OUTER JOIN but the merged supertype was not Nullable, the code re-derived the type from the current column instead of the supertype, discarding the supertype's value type. For a key whose supertype is Dynamic (e.g. Nullable(String) joined with a Dynamic key under allow_dynamic_type_in_join_keys = 1), this left the query tree typed Nullable(String) while the planner casts the key to Dynamic. An aggregate over the explicit reference (count(t.key) IGNORE NULLS) was then built with the Null combinator from the declared Nullable type, and its assert_cast<const ColumnNullable*> aborted on the runtime ColumnDynamic with "Bad cast from type DB::ColumnDynamic to DB::ColumnNullable". Keep the merged key's value type and re-apply nullability from the current column. Use makeNullableOrLowCardinalityNullableSafe so a supertype that cannot be wrapped in Nullable (Dynamic) is left as-is. This matches the qualified matcher path in QueryAnalyzer::updateMatchedColumnsFromJoinUsing. Found by the AST fuzzer. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-3:20260611-202000 |
|
cc @vdimir @novikd — could you review this? It is the explicit-reference ( |
|
Workflow [PR], commit [f7cdcc1] Summary: ✅
AI ReviewSummaryThis PR fixes the explicit-reference Missing context / blind spots
Final VerdictStatus: ✅ Approve |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 5/5 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
CI summary for HEAD Session: cron:our-pr-ci-monitor:20260612-033000 |
|
@Avogar Would you check this one please? |
|
📊 Cloud Performance Report ✅ AI verdict: This PR is a narrow analyzer correctness fix: it changes how a JOIN USING key's type is resolved when one side is Nullable from a prior OUTER JOIN and the merged supertype is a Dynamic type (using the safe nullability wrapper instead of throwing). ClickBench Q4 is a single-table aggregate with no joins, so this code path is never exercised by it. The flagged -16.17% on Q4 cannot plausibly come from this change and is downgraded to not-sure as run-to-run variance. No genuine performance impact is expected from this fix. clickbenchFlagged queries (1 of 43)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
…using-dynamic-key-aggregate-bad-cast Fix Bad cast crash for an explicit JOIN USING key with a Dynamic supertype
Related: #107129
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
Bad cast from type DB::ColumnDynamic to DB::ColumnNullablelogical error (server crash in debug builds) when an explicit reference to aJOIN ... USINGkey whose common supertype isDynamicis passed to an aggregate function, for examplecount(t.key) IGNORE NULLSwithallow_dynamic_type_in_join_keys = 1.Description
Found by the AST fuzzer (STID 3520-4a13).
An explicit reference
t.keyto aJOIN ... USINGkey adopts the merged key type (the common supertype of the joined keys) inIdentifierResolver::tryResolveIdentifierFromJoin. When the current column wasNullablefrom a previous OUTER JOIN but the merged supertype was notNullable, the code re-derived the type from the current column rather than from the supertype, discarding the supertype's value type.For a key whose supertype is
Dynamic(for exampleNullable(String)joined with aDynamickey underallow_dynamic_type_in_join_keys = 1), this left the query tree typedNullable(String)while the planner casts the key toDynamic. An aggregate over the explicit reference (count(t.key) IGNORE NULLS) was then built with theNullcombinator from the declaredNullabletype, and itsassert_cast<const ColumnNullable*>aborted on the runtimeColumnDynamic.The fix keeps the merged key's value type and re-applies nullability from the current column, using
makeNullableOrLowCardinalityNullableSafeso a supertype that cannot be wrapped inNullable(such asDynamic) is left as-is. This is the explicit-reference counterpart of the qualified-matcher fix in #107129 (QueryAnalyzer::updateMatchedColumnsFromJoinUsing), addressing the same "USING can cast to any supertype, not just nullability" class of bug.Reproducer: