Repository navigation
Conversation
When `coalesce` filtered down to a single argument whose type was narrower than the expected result type (e.g., UInt16 vs UInt32 from a multi-table JOIN), it returned the column without casting, causing "Unexpected return type" exception. Add a cast when types differ. Fixes ClickHouse#89407
Renumber `04072_coalesce_join_type_mismatch` to `04077` to avoid collision with `04072_jit_decimal_expressions`, and update table name suffixes from `_04076` to `_04077` for consistency.
… table suffixes to avoid number collision with upstream master
| return multi_if_args.front().column; | ||
| { | ||
| const auto & src = multi_if_args.front(); | ||
| if (!src.type->equals(*result_type)) |
There was a problem hiding this comment.
Why was it wrong in the first place? Shouldn't we find the root cause?
There was a problem hiding this comment.
I dug into this and the root cause is upstream of coalesce, not inside it.
In the original report (issue #89407, master ~2025-11), the legacy JoinStep was producing a result block where the runtime column types didn't match what the analyzer had declared for the function call. The action graph for coalesce(t2.x, t1.x) carried result_type = UInt32 (the least common type across the three USING columns), but the post-join block produced __table2.x as UInt16 and __table1.x as UInt32 — the type-aligning CAST on the t2 side was missing. coalesce then forwarded a UInt16 column out and the executor caught the mismatch (Unexpected return type from coalesce. Expected UInt32. Got UInt16.).
On current master that branch is gone. The plan now emits an explicit _CAST(__table1.x, 'UInt32') / _CAST(__table2.x, 'UInt32') in Post Join Actions before the coalesce, so both inputs arrive at coalesce already typed as UInt32:
Expression (Post Join Actions)
Actions: INPUT : 0 -> _CAST(__table1.x, 'UInt32'_String) UInt32 : 0
INPUT : 1 -> _CAST(__table2.x, 'UInt32'_String) UInt32 : 1
...
FUNCTION coalesce(__table2.x :: 1, __table1.x :: 0) -> coalesce(__table2.x, __table1.x) UInt32
This was fixed upstream by the new logical JOIN step refactor — query_plan_use_new_logical_join_step is now an obsolete alias (d4b469e03a9, 2026-05-04) and the new step always inserts the type-aligning CASTs. I confirmed by reverting coalesce.cpp to its master version (no fix) and rerunning the issue's exact query — it no longer raises LOGICAL_ERROR. That's also why Bugfix validation fails on this PR: the test passes on master without the patch, because the bug is no longer reproducible by that query.
So the situation is:
- The reported bug's root cause was a missing type-aligning CAST in the legacy
JoinStep, not incoalesce. That has been fixed independently. - The
multi_if_args.size() == 1branch incoalescewas theoretically lying about its return type (returning the surviving column verbatim with no check againstresult_type), but I couldn't construct any current query that reaches that branch with a type mismatch —getReturnTypeImplandexecuteImplapply the sameonlyNullfilter, and the analyzer no longer produces planner-level constant-NULL substitutions that would diverge between the two.
Given that, my recommendation is to close this PR:
- Adding a silent cast here would be a fallback path that hides any future JOIN-side regression — it would silently downgrade a regressed
LOGICAL_ERRORinto a slow CAST, exactly what the codebase tries to avoid. - The bug it claimed to fix is already fixed elsewhere; there is no remaining reproducer.
- The test no longer validates anything — Bugfix validation correctly flags this.
If we ever want belt-and-suspenders in coalesce, the right shape is a chassert(src.type->equals(*result_type)) so the invariant breaks loudly in debug builds rather than a runtime cast that papers over upstream bugs.
@Onyx2406 thanks for chasing this — sorry it converged on "no longer needed".
|
Workflow [PR], commit [c0c37ad] Summary: ❌
AI ReviewSummaryThis PR fixes Missing context
ClickHouse Rules
Final Verdict
|
|
The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix. |
|
The failures of "Flaky check" in "functions_bad_arguments" will be fixed by #101994. |
|
The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158 |
The old analyzer does not support chaining multiple `FULL OUTER JOIN ... USING` clauses, so the repro query throws `Multiple USING statements are not supported` before `coalesce` is even evaluated. That caused the Stateless (old analyzer) shard to fail on this PR. Add `SETTINGS enable_analyzer = 1` to the repro so the test exercises the fix rather than hitting a parser limitation.
Alexey asked why `coalesce` was returning the wrong type in the first place. Add an inline comment that spells it out so future readers (and reviewers) don't have to reconstruct the reasoning: `getReturnTypeImpl` computes `result_type` from the full, analyzer-time argument list. The planner can later replace some of those arguments with const-null columns whose type is `Nullable(Nothing)` — this happens routinely in `FULL OUTER JOIN` over columns of different widths, where the non-matching side is materialised as a pure NULL. The filtering loop at the top of `executeImpl` drops every such `onlyNull` argument, so a single surviving argument may have a narrower type than `result_type`. Returning its column unchanged violates the contract that the output matches the declared return type, which triggers the `Unexpected return type from coalesce` exception. The cast on mismatch is the correct fix at this layer: the deeper issue is that the planner changes the effective argument list after analysis, and coalesce's contract is to honour the declared return type regardless.
|
@alexey-milovidov Root cause explanation (also added as inline docs in
In the planner, the non-matching side of a Returning that column verbatim violates the contract that the output matches the declared Note this only manifests with |
LLVM Coverage Report
Changed lines: 83.33% (5/6) · Uncovered code |
|
Closing per the root-cause analysis above: the original bug (issue #89407) was a missing type-aligning CAST in the legacy Thanks again @Onyx2406. |
The test was accidentally added to this branch by an earlier renumber commit. It belonged to PR ClickHouse#101788 which was closed because the underlying `coalesce` bug got fixed upstream by the new logical JOIN step (`d4b469e03a9`). The test is no longer applicable and would conflict with `04089_decimal_datetime64_minmax_if.sql` on master.
What this PR does
When
coalescefiltered down to a single argument whose type was narrower than the expected result type (e.g., UInt16 vs UInt32 from a multi-table JOIN), it returned the column without casting. Added a cast when types differ.Fixes #89407
Changelog category (leave one):
Changelog entry:
Fixed logical error "Unexpected return type from coalesce" when using FULL OUTER JOIN with columns of different numeric types.