自建ClickHouse可正常运行的查询在云版本中报错求助
ClickHouse云版本嵌套arrayMap查询报错解决
问题描述
相同的嵌套arrayMap查询在自建ClickHouse中正常执行,但在ClickHouse云版本(24.6.3.95官方构建)中报错,提示找不到hit.product和product.id标识符。
原查询语句
SELECT arrayMap(hit -> arrayMap(product -> product.id, hit.product), hits ) AS `hits.product.id` FROM table
源列hits数据类型
hits(Array( Tuple(hitId String, product Array(Tuple(id String, isImpression String, impressionList String, productBrand String ) ) ) ) )
报错信息(翻译后)
SQL Error [47] [07000]: Code: 47. DB::Exception: 缺失列: 'hit.product' 'product.id',处理查询时出错: 'SELECT arrayMap(hit -> arrayMap(product -> product.id, hit.product), hits) AS `hits.product.id` FROM marketing_raw_data.owoxbi_sessions_', 需要的列: 'product.id' 'hits' 'hit.product',你可能想输入的是: 'hits'。(UNKNOWN_IDENTIFIER) (版本 24.6.3.95 (official build))
问题原因
ClickHouse云版本默认启用了更严格的语法解析规则,或者默认关闭了Tuple命名字段访问的实验特性,导致lambda表达式中无法通过.直接访问Tuple的命名字段。
解决方案
提供两种兼容方案,任选其一即可:
方案1:使用Tuple位置索引访问(兼容性最优)
直接通过Tuple的位置索引来获取对应字段,不受配置或版本影响:
SELECT arrayMap(hit -> arrayMap(p -> p.1, hit.2), hits) AS `hits.product.id` FROM table
- 说明:
hit.2对应Tuple的第二个字段product数组,p.1对应product Tuple的第一个字段id。
方案2:开启Tuple命名字段访问特性
如果需要保留原有的命名字段访问语法,可显式开启实验特性:
SELECT arrayMap(hit -> arrayMap(product -> product.id, hit.product), hits) AS `hits.product.id` FROM table SETTINGS allow_experimental_tuple_fields = 1
- 说明:
allow_experimental_tuple_fields参数用于启用Tuple命名字段的点语法访问,部分新版本默认关闭该特性。
验证
修正后的查询将返回预期的Array(Array(String))类型结果,与自建ClickHouse的执行结果一致。
内容的提问来源于stack exchange,提问作者R V
相关产品推荐
相关产品推荐

