经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » SQL语言 » 查看文章
HIVE中IN的坑
来源:cnblogs  作者:荒漠依米摩天轮  时间:2018/11/7 3:26:19  对本文有异议

问题:为什么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

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号