您可以使用collect_list 或colect_set 来收集从连接中获取的结构数组,如果连接条件为假,那么 collect_list 将生成一个空的结构数组。
此查询返回大小为 0 的数组:
select a.id, size(collect_list(b.str))=0 array_size_zero
from
(select 2 id ) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on a.id=b.id
group by a.id
结果:
a.id array_size_zero
2 true
如果您将第一个子查询中的 id 更改为与 b 连接,它将返回包含 1 个元素的数组。并且这些结果属于同一类型,你可以使用 union all 轻松查看。
检查结果属于同一类型:
select a.id, collect_list(b.str) my_array
from
(select 1 id ) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on a.id=b.id
group by a.id
union all
select a.id, collect_list(b.str) my_array
from
(select 2 id ) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on a.id=b.id
group by a.id
结果:
id my_array
1 [{"name":null,"value":null}]
2 []
如果我尝试 UNION ALL 不同类型的空结构数组会发生什么,例如 array():
select 1 id, array() my_array
union all
select a.id, collect_list(b.str) my_array
from
(select 2 id ) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on a.id=b.id
group by a.id
例外:
编译语句时出错:FAILED: SemanticException Schema of
联合的两边应该匹配:列 my_array 是类型
第一个表上的数组和类型
array 在第二个表上。不能告诉
空 AST 的位置。
这表明第一个查询确实返回了空的结构数组。
您可以轻松地在查询中进行类似的联接。
如何在有条件的查询中使用它?
演示:
select a.id, case when true --Put your condition here instead of dummy <true>
then collect_list(a.str) --not empty
else collect_list(b.str) --this one is empty array of the same type
end as my_array
from
(select 2 id, named_struct('name',null,'value',null) str) a
left join (select 1 as id, named_struct('name',null,'value',null) as str) b
on FALSE
group by a.id
CASE 表达式非常满意,不会引发不兼容类型的异常