Skip to content

Fix NO_COMMON_TYPE for a mixed-type array literal with use_variant_as_common_type - #127452

Open
JayPokale wants to merge 6 commits into
ClickHouse:masterfrom
JayPokale:planner-variant-array-literal
Open

JayPokale wants to merge 6 commits into
ClickHouse:masterfrom
JayPokale:planner-variant-array-literal

Conversation

@JayPokale

@JayPokale JayPokale commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

With use_variant_as_common_type = 1, an array whose elements have no common type is an Array(Variant(...)). This works with the array function, but the same array written as a literal failed:

SELECT [1, 'ClickHouse', ['Another', 'Array']] AS array, toTypeName(array)
SETTINGS use_variant_as_common_type = 1;
-- Code: 386. DB::Exception: There is no supertype for types UInt8, String, Array(String) because some of them are Array and some of them are not. (NO_COMMON_TYPE)

That is the example from the documentation of the setting (docs/guides/clickhouse/working-with-arrays.mdx), which fails on the current master build (26.11.1.515).

QueryTreeBuilder gives the literal the Array(Variant(...)) type, but calculateActionNodeNameWithCastIfNeeded in the planner computes the type of the constant's value again with FieldToDataType (to decide whether the name needs a _CAST), and that throws for elements without a common type. ConstantNode::toASTImpl makes the same decision and already treats "can't determine the type of the value" as "needs a cast". This PR moves that decision into ConstantNode::valueRequiresCastCall and uses it in both places, so the planner no longer throws and the action name keeps matching the AST that is sent to remote servers.

05451_variant_common_type_array_literal has the two examples from the documentation and the same literal through remote. The reference is what the current master build returns for the same arrays built with the array function (checked on a local server, also through remote). I could not build ClickHouse locally, so CI is the first run of the fix itself.

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 NO_COMMON_TYPE for an array literal with elements of different types (for example [1, 'a']) when use_variant_as_common_type is enabled; it is now an Array(Variant(...)), as the same array built with the array function.


Workflow [PR]
Sync PR [sync-upstream/pr/127452]

…_common_type

The literal gets the type `Array(Variant(...))`, but when the planner named the constant it computed the type of
its value again with `FieldToDataType`, which throws for elements without a common type. `ConstantNode::toASTImpl`
already treats that case as needing a `_CAST`; move that decision into `ConstantNode::valueRequiresCastCall` and
use it in the planner too, so the name also matches the AST sent to remote servers.
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 22, 2026
Comment thread src/Analyzer/ConstantNode.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Oct 23, 2026
…xception

Instead of catching the exception of `FieldToDataType` for elements without a common type, use the
`LeastSupertypeOnError::Variant` policy: such elements get a `Variant`, other values the same type as before.
An array always needs the cast (`requiresCastCall`), so mixed-type arrays still get it.
@nickitat nickitat added the can be tested Allows running workflows for external contributors label Oct 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [50f915b]

Summary: ✅


AI Review

Summary

The PR fixes NO_COMMON_TYPE for mixed-type array literals with use_variant_as_common_type by sharing the cast decision between ConstantNode serialization and planner action naming. Reviewed the complete diff and prior discussion at commit 50f915be4d0138ca04809ba022ac886d2d1b10b6; no actionable findings remain. The focused 05451_variant_common_type_array_literal regression test, including nested arrays, variant subcolumns, and remote, passed in Fast test. No local build or tests were run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 50f915be4 with master ed426a200 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.79 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 7.3 s 15.6 s +9.8 s (+170%)
Compile time of recompiled translation units

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

Job report

This branch has not been deployed

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

Labels

can be tested Allows running workflows for external contributors comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants