如何检查配置单元中的第一个非空值

1cosmwyk  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(335)

如何检查配置单元中的第一个非空值
例如。
选择('',5)应得到5
选择(5,'')应得到5
select('',null)的结果应为null
select(“”,“”)的结果应为“”
请帮助,我知道coalesce将适用于查找第一个非空值。

w8rqjzmb

w8rqjzmb1#

变换 NULL 把它们串起来 'NULL' ,空到 NULL 使用 coalesce ,然后转换回。像这样:

create temporary macro empty2null(s string)
case when s='' then null
     when s is null then 'NULL'
     else s
  end;

create temporary macro NULL2empty (s string)
case when s='NULL' then null
     when s is null then ''
     else s
end;

select NULL2empty(coalesce(empty2null(''),empty2null(5)));

唯一不方便的是,您必须将每一列都 Package 到 empty2null() ,除了常量 5 ,只是检查一下
测验:

hive> create temporary macro empty2null(s string)
    > case when s='' then null
    >      when s is null then 'NULL'
    >      else s
    >   end;
OK
Time taken: 0.97 seconds
hive>
    > create temporary macro NULL2empty (s string)
    > case when s='NULL' then null
    >      when s is null then ''
    >      else s
    > end;
OK
Time taken: 0.02 seconds
hive>
    > select NULL2empty(coalesce(empty2null(''),empty2null(5)));
OK
5
Time taken: 7.08 seconds, Fetched: 1 row(s)
hive> select NULL2empty(coalesce(empty2null(''),empty2null('')));
OK

Time taken: 3.96 seconds, Fetched: 1 row(s)
hive> select NULL2empty(coalesce(empty2null(''),empty2null(null)));
OK
NULL
Time taken: 0.952 seconds, Fetched: 1 row(s)
hive> select NULL2empty(coalesce(empty2null(5),empty2null('')));
OK
5
Time taken: 0.067 seconds, Fetched: 1 row(s)

相关问题