合并配置单元中的整数数组

avwztpqn  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(448)

foo_ids 是类型的数组 bigint ,但整个数组可以为空。如果数组为null,则我需要一个空数组。
如果我这样做: COALESCE(foo_ids, ARRAY()) 我得到: FAILED: SemanticException [Error 10016]: Line 13:45 Argument type mismatch 'ARRAY': The expressions after COALESCE should all have the same type: "array<bigint>" is expected but "array<string>" is found 如果我这样做: COALESCE(foo_ids, ARRAY<BIGINT>()) 我得到一个语法错误: FAILED: ParseException line 13:59 cannot recognize input near ')' ')' 'AS' in expression specification 这里的正确语法是什么?

doinxwow

doinxwow1#

使用这个:

合并(foo\u id,数组(cast(null为bigint)))

之前,配置单元将空数组[]视为[]。但是在hadoop2中,hive现在将空数组[]显示为null(参见下面的参考)。对bigint类型的空数组使用array(cast(null为bigint))。奇怪的是,空数组的大小是-1(而不是0)。希望这有帮助。谢谢。

Sample data:
foo_ids 
[112345677899098765,1123456778990987633]        
[null,null]     
NULL    

select foo_ids, size(foo_ids) as sz from tbl;
Result:
foo_ids                                        sz
[112345677899098765,1123456778990987633]        2
[null,null]                                     2
NULL                                           -1

select foo_ids, coalesce(foo_ids, array(cast(null as bigint))) as newfoo from tbl;
Result:
foo_ids                                         newfoo
[112345677899098765,1123456778990987633]        [112345677899098765,1123456778990987633]
[null,null]                                     [null,null] 
NULL                                            NULL

参考文献:https://docs.treasuredata.com/articles/hive-change-201602

相关问题