select
map('k1','v1', 'k2','v2'), --构建map
map('k1','v1', 'k2','v2')['k1'], --获取map中的元素
map('k1','v1', 'k2','v2')['k3'], --不存在的key返回NULL
size(map('k1','v1', 'k2','v2')) --获取map的长度
;
+------------------------+------+-------+------+
| _c0 | _c1 | _c2 | _c3 |
+------------------------+------+-------+------+
| {"k1":"v1","k2":"v2"} | v1 | NULL | 2 |
+------------------------+------+-------+------+
select
struct(123, 'abc', true), --构建struct
named_struct('c1',123, 'c2','abc', 'c3',true), --构建named_struct
named_struct('c1',123, 'c2','abc', 'c3',true).c1 --获取named_struct中的属性
;
+----------------------------------------+----------------------------------+------+
| _c0 | _c1 | c1 |
+----------------------------------------+----------------------------------+------+
| {"col1":123,"col2":"abc","col3":true} | {"c1":123,"c2":"abc","c3":true} | 123 |
+----------------------------------------+----------------------------------+------+
使用不存在的属性会报错
select named_struct('c1',123, 'c2','abc').c3;
Error: Error while compiling statement: FAILED: RuntimeException cannot find field c3(lowercase form: c3) in [c1, c2] (state=42000,code=40000)
使用union all时,named_struct的属性名称必须一致
select named_struct('c1',123, 'c2','abc', 'c3',true)
union all
select named_struct('c1',123, 'c2','abc', 'c4',true);
Error: Error while compiling statement: FAILED: SemanticException Schema of both sides of union should match: Column _c0 is of type struct on first table and type struct on second table. Cannot tell the position of null AST. (state=42000,code=40000)
select
array(123, 'abc'), --构建array(类型不一致会统一转换成兼容的类型)
array('abc', 123), --元素的位置不影响强转的类型
array('abc', 123)[0], --获取指定位置的元素
array('abc', 123)[2], --数组越界返回NULL
size(array('abc', 123)) --获取array的长度
;
+----------------+----------------+------+-------+------+
| _c0 | _c1 | _c2 | _c3 | _c4 |
+----------------+----------------+------+-------+------+
| ["123","abc"] | ["abc","123"] | abc | NULL | 2 |
+----------------+----------------+------+-------+------+
元素字段类型不一致,且类型不支持强转就会报错
select array(123, 'abc', true);
Error: Error while compiling statement: FAILED: SemanticException [Error 10016]: Line 1:25 Argument type mismatch 'true': Argument type "boolean" is different from preceding arguments. Previous type was "string" (state=42000,code=10016)
使用union all时,array的元素类型必须一致
select array(1,2,3)
union all
select array('a','b','c');
Error: Error while compiling statement: FAILED: SemanticException Schema of both sides of union should match: Column _c0 is of type array on first table and type array on second table. Cannot tell the position of null AST. (state=42000,code=40000)
select
create_union(0, 123, 'abc', true), --uniontype
create_union(1, 123, 'abc', true)
;
+----------+------------+
| _c0 | _c1 |
+----------+------------+
| {0:123} | {1:"abc"} |
+----------+------------+
SELECT
id, create_union(if(cnt>10,1,0), cnt, false)
FROM
(
select 1 as id, 5 as cnt union all
select 2 as id, 15 as cnt
) t;
+-----+------------+
| id | _c1 |
+-----+------------+
| 1 | {0:5} |
| 2 | {1:false} |
+-----+------------+
会出现数组越界问题
select create_union(3, 123, 'abc', true);
Error: Error while compiling statement: FAILED: ArrayIndexOutOfBoundsException 4 (state=42000,code=40000)