原始数据:
1001 2022-05-01
1001 2022-05-02
1001 2022-05-04
1001 2022-05-05
1001 2022-05-08
1001 2022-05-10
1002 2022-05-01
1002 2022-05-03
1002 2022-05-05
1002 2022-05-07
1002 2022-05-09
1002 2022-05-13
1002 2022-05-14
建表语句:
create table test2(
id string,
dt string)
row format delimited fields terminated by '\t';
插入数据:
load data local inpath '/opt/module/hive/datas/test2.txt' into table test2;
查询语句:
select
id,
dt,
sum(if(days>2,1,0)) over(partition by id order by dt) g_id
from
(select
id,dt,datediff(dt,lag(dt,1,"1970-01-01")over(partition by id order by dt)) days
from test2)t1
报错信息:
解决办法:关闭hive的CBO优化
set hive.cbo.enable=false;
再次执行:顺利得到结果
猜测原因:
hive4.0.0之前版本的CBO优化(4.0.0版本解决了一些bug)
不支持
select
xxx
from(
select
yyy
from tmp
)t1
格式的查询语句,测试集群用的是3.1.2版本,所以需要把默认开启的CBO优化关闭。