Repository navigation
Fix CAST of an Array(Map) constant sent to a remote server - #115883
Conversation
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
|
Workflow [PR], commit [cd4cff6] Summary: ✅
AI ReviewSummaryThis PR fixes a real Final Verdict✅ No findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 10/10 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
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 units7 translation units recompiled, 51 s compile time in total, 7 of them have a recent master baseline. |
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]>
Related: #115647
A
Mapis physically anArray(Tuple(key, value)), and that is also how aMapconstant is written down when the analyzer serializes a query for a remote server:ConstantNode::toASTImplrenders the field as an array of tuples and wraps it into_CAST(..., 'Array(Map(K, V))'). On the receiving sideCASTcompared the nesting depth ofArray(Array(Tuple(K, V)))(two) withArray(Map(K, V))(one) and rejected the cast before ever reaching the element wrapper, which convertsArray(Tuple(K, V))toMap(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:CASTnow counts aMapas the array of tuples it stands for when comparing nesting depth, so both spellings sit at the same depth. A genuine depth mismatch, such asArray(String)toArray(Array(String)), is still rejected.This is what makes
04510_not_has_function_explainfail in theParallelReplicasjob onmaster: 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 ahas([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 plainDistributedquery.Changelog category (leave one):
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 typeArray(Map(...)), such ashas([map('k', 'v')], m), executed over aDistributedtable or with parallel replicas.Documentation entry for user-facing changes
Workflow [PR]
Sync PR [sync-upstream/pr/115883]
Version info
26.8.1.1912(included in26.8and later)