当前位置: 移动技术网 > IT编程>数据库>其他数据库 > HIVE中IN的坑

HIVE中IN的坑

2018年11月07日  | 移动技术网IT编程  | 我要评论

问题:为什么hive中用了 not in,结果集没了?

  注:这个是原创,转载请注明,谢谢!
直接进实验室>>

> select * from a;
ok
1 a1
2 a2
3 a3
time taken: 0.063 seconds, fetched: 3 row(s)

hive> select * from b;
ok
1 b1
2 b2
null b3
time taken: 0.063 seconds, fetched: 3 row(s)

# 两表通过id匹配,求 a-b ,用 left join实现
hive> select t1.id,t1.name,t2.name from a t1
> left join b t2 on t1.id = t2.id
> where t2.name is null
ok
3 a3 null
time taken: 34.123 seconds, fetched: 1 row(s)

# 两表通过id匹配,求 a-b ,用 not in 实现
select * from a where id not in ( select id from b );
ok
time taken: 34.123 seconds, fetched: 0 row(s)

这里有诡异了,为什么结果集没了呢? 不能啊??


原因:

在rmdb中, t1.id in (select t2.id from b t2 ) 等价于 : t1 join b t2 on t1.id = t2.id and t1.id is not null
在hive中,虽然我们的版本已经高达2.0.0,但是对于in的处理还是就比较简陋,没有对null值进行屏蔽,导致凡是子查询中有null值, 条件就会变成: id in ( null) , 当然, id in ( null) 这个条件是永远不会有结果的。


正确的用法:

# 两表通过id匹配,求 a-b ,用 not in 实现
select * from a where id not in ( select id from b where id is not null );
ok
3 a3 null
time taken: 34.123 seconds, fetched: 1 row(s)

各位不妨可以做个试验:
--没结果
hive> select * from a where id not in (null);
ok
time taken: 3.603 seconds

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网