首页 > 解决方案 > Hive 中 rand() 函数的精度?

问题描述

如果我的表有很多行,比如数百亿行,是否会select rand() from table为两行给出两个相同的结果?

标签: hive

解决方案


rand()hive 中函数的返回类型是double. double 的精度大约在 -10^308 到 10^308 的范围内。所以 rand() 返回重复结果的机会几乎为零。

在具有 150 亿行的 hive 上测试了以下查询。

 select r, count(*)
 from (
   select rand() as r from <table name>
 ) as a
  group by r 
  having count(*)>1;

结果是No Rows Found。这表示rand()没有返回重复的行。

检查来自 Hive wiki 的关于浮点精度的链接。


推荐阅读