蜂巢中 collect_set 的条件

Where condition on collect_set in hive

有没有一种方法可以过滤掉 hive 中 collect_set 输出中的非连续数字? 请参见下面的示例,突出显示的行没有连续的数字,我只想要这样的输出

这个数字可能是 [5,6,9][4,5,7,8,] 我想从一个配置单元查询中过滤所有这样的行,其中数组中的值是不连续的。

分解数组,将每个元素与下一个元素进行比较以查找不连续的元素,计算此类出现次数并进行过滤。

演示:

set hive.on.master=true;
with your_data as (
    select  array (5,6,9) as myarray
    union all
    select  array (1,2,3,4,5,6) as myarray
    union all
    select  array (0) as myarray
    union all
    select  array (4,5,7,8) as myarray
    union all
    select  array (0,1) as myarray
    union all
    select  array (0,1,3) as myarray
)

select myarray--, sum(non_consecutive)
from
(
select myarray,
       case when lead(value) over(partition by myarray order by pos )-value != 1 then 1 else 0 end non_consecutive
  from your_data d
       lateral view posexplode(myarray) e as pos, value
)s
group by myarray    
having sum(non_consecutive)>0
;

结果:

myarray
[0,1,3]
[4,5,7,8]
[5,6,9]

如果你一开始就爆了数据,那么就用collect_set之前的逻辑:计算先导值(你应该知道按什么排序),与当前值比较,然后聚合集合,然后在同时聚合 non_consecutive 和过滤。