Skip to content

Fix CAST of an Array(Map) constant sent to a remote server - #115883

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-cast-array-of-map-constant
Aug 22, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-cast-array-of-map-constant

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 22, 2026 •

Copy link
Copy Markdown
Member

Related: #115647

A Map is physically an Array(Tuple(key, value)), and that is also how a Map constant is written down when the analyzer serializes a query for a remote server: ConstantNode::toASTImpl renders the field as an array of tuples and wraps it into _CAST(..., 'Array(Map(K, V))'). On the receiving side CAST compared the nesting depth of Array(Array(Tuple(K, V))) (two) with Array(Map(K, V)) (one) and rejected the cast before ever reaching the element wrapper, which converts Array(Tuple(K, V)) to Map(K, V) without trouble.

So any query whose constant folds to an Array(Map(...)) failed once it was shipped to a shard or to a parallel replica:

CREATE TABLE t (m Map(String, String)) ENGINE = MergeTree ORDER BY m;
INSERT INTO t SELECT map('k', toString(number)) FROM numbers(4);

SELECT count() FROM remote('127.0.0.{1,2}', currentDatabase(), t) WHERE has([map('k', '3')], m);
Code: 53. DB::Exception: Received from 127.0.0.2:9000. DB::Exception:
CAST AS Array can only be performed between same-dimensional array types:
In scope SELECT count() AS `count()` FROM default.t AS __table1
WHERE has(_CAST([[('k', '3')]], 'Array(Map(String, String))'), __table1.m). (TYPE_MISMATCH)

CAST now counts a Map as the array of tuples it stands for when comparing nesting depth, so both spellings sit at the same depth. A genuine depth mismatch, such as Array(String) to Array(Array(String)), is still rejected.

This is what makes 04510_not_has_function_explain fail in the ParallelReplicas job on master: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=31e78bbbb66e018db116f4956eb25cbbc27960e6&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29 - that test got a has([map('k', NULL)], m) query in #115647, and the query hits this cast whenever the filter is actually shipped to a replica. The bug itself is much older and reproduces on any version with a plain Distributed query.

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 TYPE_MISMATCH ("CAST AS Array can only be performed between same-dimensional array types") for a query with a constant of type Array(Map(...)), such as has([map('k', 'v')], m), executed over a Distributed table or with parallel replicas.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

Version info

  • Merged into: 26.8.1.1912 (included in 26.8 and later)

A `Map` is physically an `Array(Tuple(key, value))`, and that is also how a
`Map` constant is written down when the analyzer serializes a query for a
remote server: `ConstantNode::toASTImpl` renders the field as an array of
tuples and wraps it into `_CAST(..., 'Array(Map(K, V))')`. On the receiving
side `CAST` compared the nesting depth of `Array(Array(Tuple(K, V)))` (two)
with `Array(Map(K, V))` (one) and rejected the cast with `TYPE_MISMATCH`
before reaching the element wrapper, which converts `Array(Tuple(K, V))` to
`Map(K, V)` without trouble.

So any query whose constant folds to an `Array(Map(...))` failed once it was
shipped to a shard or to a parallel replica, for example
`has([map('k', 'v')], m)` over a `Distributed` table.

`CAST` now counts a `Map` as the array of tuples it stands for when comparing
nesting depth. A genuine depth mismatch, such as `Array(String)` to
`Array(Array(String))`, is still rejected.

This is what makes `04510_not_has_function_explain` fail in the
`ParallelReplicas` job:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=31e78bbbb66e018db116f4956eb25cbbc27960e6&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29
@clickhouse-gh

clickhouse-gh Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cd4cff6]

Summary: ✅


AI Review

Summary

This PR fixes a real CAST contract gap for remotely serialized Array(Map(...)) constants by making the array-dimension check treat Map as the single Array(Tuple(...)) layer it physically represents. The implementation change in FunctionCast::createArrayWrapper matches the existing nested conversion wrappers, the new stateless test covers both direct casts and the distributed query path that was failing, and I did not find a remaining correctness, compatibility, or evidence gap in the current patch.

Final Verdict

✅ No findings.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.40% +0.10%

Changed lines: Changed C/C++ lines covered: 10/10 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing cd4cff64d with master d3d7dc9bb (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
Binary Master PR Δ
programs/clickhouse-stripped 705.16 MiB 702.14 MiB -3.02 MiB (-0.43%)

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

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

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool.

@alexey-milovidov alexey-milovidov self-assigned this Aug 22, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 22, 2026
@alexey-milovidov alexey-milovidov added the blocker This issue / pr blocks a new release label Aug 22, 2026
Merged via the queue into master with commit 9f19372 Aug 22, 2026
183 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-cast-array-of-map-constant branch August 22, 2026 19:04
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 22, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 22, 2026
@groeneai groeneai mentioned this pull request Aug 25, 2026
1 task done
pull Bot pushed a commit to SINHASantos/ClickHouse that referenced this pull request Sep 20, 2026
A `Map(K, V)` is physically an `Array(Tuple(K, V))` and `CAST` converts between the two spellings,
so the nesting check of `CAST AS Array` has to accept both. ClickHouse#115883 made it count a `Map` as one
dimension unconditionally, which is right when the other type spells the `Map` out as an array, but
wrong when the other type keeps the `Map` whole - behind a `Variant`, a `Dynamic`, a `JSON` or a
`String`, none of which add a dimension of their own:

    SELECT CAST([map('a', 'x')], 'Array(Variant(String, Map(String, String)))');
    Code: 53. DB::Exception: CAST AS Array can only be performed between same-dimensional array
    types. (TYPE_MISMATCH)

The nesting depth of a type that contains a `Map` is not a single number, so it is computed as a
range now: a `Map` is zero dimensions when it is matched as a `Map` and one dimension when it is
matched as the `Array(Tuple(K, V))` it stands for. The cast passes the check when the two ranges
intersect. For types without a `Map` both ends of the range are equal and the check is exactly the
old equality, so nothing else changes. A genuine depth mismatch, such as `Array(String)` to
`Array(Array(String))`, is still rejected, and `Array(Array(Tuple(K, V)))` to `Array(Map(K, V))` -
the constant shipped to a remote server that ClickHouse#115883 fixed - still works.

Closes: ClickHouse#119835
Related: ClickHouse#115883

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants