Repository navigation
Fix Bad cast crash for qualified asterisk over a JOIN USING key - #107129
alexey-milovidov merged 1 commit into
Conversation
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-1:20260611-010300 |
|
cc @vdimir @novikd — could you review this? It fixes an analyzer type/data nullability mismatch: a qualified matcher ( |
|
Workflow [PR], commit [815cd53] Summary: ✅
AI ReviewSummaryThis PR fixes analyzer type inference for qualified matchers such as Final VerdictStatus: ✅ Approve |
| /// For a qualified matcher keep the supertype but not the merged key's | ||
| /// nullability: take the matched column's own nullability instead, so we | ||
| /// don't declare Nullable a column the planner keeps plain (join_use_nulls=0, | ||
| /// non-nullable side of an outer JOIN). | ||
| if (is_qualified_matcher | ||
| && isNullableOrLowCardinalityNullable(using_column_type) | ||
| && !isNullableOrLowCardinalityNullable(matched_column_node_typed.getColumnType())) | ||
| using_column_type = removeNullableOrLowCardinalityNullable(using_column_type); | ||
|
|
There was a problem hiding this comment.
Using can alter not only nullability, but cast to any supertype. So, we'd probably better off using original type from somewhere
There was a problem hiding this comment.
Good point, addressed. The matcher now keeps the merged USING key's value type but takes its nullability from the matched column's own type, instead of only stripping nullability. This is exactly how an explicit t.key is typed in IdentifierResolver::tryResolveIdentifierFromJoin (take the supertype, keep the referenced column's own nullability), so t.* and t.key agree.
On the non-nullability axes you mentioned: I checked width and decimal-scale supertypes (UInt8 vs Int64, Decimal(9,2) vs Decimal(18,4), String vs FixedString) across INNER/LEFT/RIGHT/FULL and both join_use_nulls values. There the qualified matcher already had to take the supertype, because the planner casts the value to that supertype regardless, so keeping the value type from the USING key is correct and toTypeName(t.*) = toTypeName(t.key) holds. The only axis where the matcher must diverge from the merged key is nullability under join_use_nulls=0 (the crash). Added regression coverage for the width and decimal cases plus an aggregate over a widened+nullable key.
There was a problem hiding this comment.
Generalized further. Instead of patching nullability onto the merged key, the qualified matcher now resolves the explicit identifier t.col via tryResolveIdentifier and adopts its result type. That is the authoritative type an explicit t.col reference gets (IdentifierResolver::tryResolveIdentifierFromJoin), correct along every axis the merged key can differ on (value-type widening and nullability), and it follows only the joins t participates in rather than the outer USING key.
The earlier nullability-from-own-type version was still wrong for a nested JOIN where a sibling USING table is Nullable: there t.col is genuinely Nullable but its own table type is not, so it under-declared the type. The AST fuzzer hit exactly that on the new test (anyHeavyOrNull(sipHash64(t2.*)) over (t1 Nullable LEFT JOIN t2) GLOBAL RIGHT JOIN t3 Nullable USING(id), the opposite-direction bad cast). Added that case to 04329; it now crashes without this change and passes with it.
22cf076 to
123f29e
Compare
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-5:20260611-103500 |
CI failures on 123f29e — neither is caused by this PRAST fuzzer (amd_tsan) — Proof:
CIDB shows 1 hit in 180 days across all of master and every PR (the base rate for a unique fuzzer-mutated query). I will track and fix this trunk JIT bug separately; it is out of scope for this analyzer change. Integration tests (amd_tsan, 5/6) — cron:clickhouse-worker-slot-11:20260611-163700 |
CI summary, covered HEAD
|
| Check | Failure | Classification | Disposition |
|---|---|---|---|
| AST fuzzer (amd_tsan) | Logical error: BitNotImpl expected an integral type (STID 2649-32ef) |
Pre-existing trunk JIT bug (bitNot/bitCount compile under group_by_use_nulls + compile_expressions) | NOT PR-caused. This PR touches only QueryAnalyzer JOIN USING matcher typing, unrelated to JIT bit-function codegen. Tracked + fixed by our separate PR #107263. The fuzzer surfaced it on this run by chance. |
| Integration tests (amd_tsan, 5/6) | test_backup_restore_on_cluster/test_cancel_backup.py::test_cancel_restore + ::test_short_disconnection_doesnt_stop_restore |
Chronic flaky (test_cancel_restore 7 hits/3 PRs; test_short_disconnection 5 hits/5 PRs in 30d; latter is a cluster start/shutdown fixture error) | NOT PR-caused (backup/restore-on-cluster is unrelated to an analyzer JOIN USING change). Tracked. |
No PR-caused failures. The fix (Bad cast crash for a qualified asterisk over a JOIN USING key; regression test 04329) is unaffected.
123f29e to
bd37298
Compare
A qualified matcher (t.*) that matched a JOIN USING key adopted the merged key's type in updateMatchedColumnsFromJoinUsing. That is wrong: the merged key is the common type of all joined keys at the join where the USING clause sits, while t.col only follows the joins t actually participates in. In a nested JOIN the two differ. For SELECT anyHeavyOrNull(sipHash64(t2.*)) FROM t1 LEFT JOIN t2 USING (id) GLOBAL RIGHT JOIN t3 USING (id) SETTINGS join_use_nulls = 0 with t1 and t3 keys Nullable and t2 key non-Nullable, the matched t2.id was typed from the outer USING key. Depending on which side was Nullable the declared type disagreed with the column the planner produces, and an aggregate built from it aborted with a Bad cast in either direction (declared Nullable vs runtime non-Nullable, or the reverse). The first direction was the original report; the second was found by the AST fuzzer on the new test. Resolve the explicit identifier t.col through tryResolveIdentifier and adopt its result type, which is exactly what an explicit t.col reference resolves to (IdentifierResolver::tryResolveIdentifierFromJoin). This is correct along every axis the merged key can differ on (value-type widening and nullability), not just nullability, and follows only the joins t participates in. The unqualified matcher (*) still keeps the merged key type as-is, since there the column is the merged key. Co-Authored-By: Claude Opus 4.8 <[email protected]>
bd37298 to
815cd53
Compare
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-2:20260620-085000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 29/29 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 4 line(s) · Uncovered code |
CI summary, covered HEAD
|
alexey-milovidov
left a comment
There was a problem hiding this comment.
The reasoning behind this is good.
|
Hi — this looks like a backport candidate for |
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::IColumn const* to DB::ColumnNullable const*logical error / crash when a qualified asterisk (t.*) over aJOIN ... USINGkey is passed to an aggregate function and the other side of an outer JOIN has aNullablekey (withjoin_use_nulls = 0).Description
Found by the AST fuzzer:
AST fuzzer (amd_debug), STID3524-3f17, reporthttps://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104625&sha=79737e37a9f5cc4c525fbadef72b35ce8349c871&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
Reproducer:
Crash path:
AggregateFunctionNullUnary<true, true>::addBatchSinglePlace(
AggregateFunctionNull.h:442) <-Aggregator::executeWithoutKeyImpl(
Aggregator.cpp) <-AggregatingTransform::consume.Root cause is in the analyzer. When a qualified matcher (
t2.*) matches aJOIN USINGkey,updateMatchedColumnsFromJoinUsingoverrode the matchedcolumn's type with the merged USING-key type. That type is the common
supertype of the joined keys and, for an outer JOIN, can be
Nullablebecause the other side's key is natively
Nullable. Withjoin_use_nulls = 0the data the planner produces for
t2.idstays plain (non-Nullable), so thequery tree typed
sipHash64(t2.*)asNullable(UInt64)while the actualcolumn is plain
UInt64. The aggregate functionanyHeavywas therefore builtwith the
Nullcombinator (expecting aColumnNullableargument) but receivedthe plain column at runtime, and
assert_cast<const ColumnNullable *>aborted.The existing code carried a
TODOnoting this was "not completely correct forqualified matchers".
Fix: for a qualified matcher keep the USING-key supertype but preserve the
matched column's own nullability (do not declare a column
Nullablethat theplanner keeps plain). Unqualified matcher (
*) behaviour, where the matchedcolumn genuinely is the merged key, is unchanged. Added a stateless regression
test that crashes on master and passes with the fix, and that also checks the
supertype-coalescing and
join_use_nulls = 1cases still resolve as before.Version info
26.6.1.1074