在hivewindow中,如果CURRENT ROW的值小于UNBOUNDED PRECEDING的值会怎样

In hive window, what would happen if the value of CURRENT ROW is smaller than that of UNBOUNDED PRECEDING

当我在配置单元中使用 RANGE 指定 window 时,我得到了一些混乱的结果。

有一个测试table。

select
  id,
  val,
  sum(val) over(order by val rows between unbounded preceding and 
   current row) rows_sum,
  sum(val) over(order by val range between unbounded preceding and 
   current row) range_sum
from test

这是上述查询的结果。这也是我期待的结果

id val rows_sum range_sum
1 1 1 2
2 1 2 2
3 3 5 5
4 6 11 23
5 6 17 23
6 6 23 23

但对于 range_sum 字段,如果我将顺序规则从 asc 更改为 desc。说

      sum(val) over(order by val desc range between unbounded preceding and 
   current row) range_sum

这是结果

val range_sum
6 18
6 18
6 18
3 21
1 23
1 23

但我对 range_sum 的预期结果是

val range_sum
6 18
6 18
6 18
3 NULL
1 NULL
1 NULL

在 Hive 中有两种定义帧的方法,ROWS AND RANGE.for 示例,SUM(val) RANGE BETWEEN 100 PRECEDING AND 200 FOLLOWING 根据距当前行值的距离选择行。假设当前 val 为 200,此帧将包含 val 值范围为 100 到 400

的行

所以在我的示例中 above:range 在无限制的前一行和当前行之间

当val为6时,frame包含3行val = 6,所以和为18。

但是当我们考虑第 4 行时,val 是 3。因为排序规则是 desc,UNBOUNDED PRECEDING 行的 val 是 6,而 CURRENT ROW IS 3.The frame 应该包括 val 的行介于 6 和 3 之间,没有行满足此条件。但查询结果是 21 。我的意思是它喜欢 3 到 6 之间,而不是 6 到 3 之间。

它按照设计和标准工作,在其他数据库中也有同样的行为。

与标准文档相比,更容易找到 Hive 和其他数据库(如 Oracle)的规范 (for free). For example see "Windowing Specifications in HQL" and Oracle "Window Function Frame Specification"

首先对分区进行排序,然后计算边界并使用边界之间的框架。 Frame是根据ORDER BY取的,不一定都是>=bound1<=bound2.

按 DESC Bound1>=Row.value>=Bound2 订购。帧包括从分区开始到当前行的行,包括当前行的所有对等行(根据 ORDER BY 子句,等于当前行的行)。

按 ASC 排序 Bound1<=Row.value<=Bound2

无限前导:

The bound (bound1) is the first partition row (according to the order).

当前行:

For ROWS, the bound (bound2) is the current row. For RANGE, the bound is the peers of the current row (rows with the same value as current row).

另请阅读此文excellent explanation from Sybase

The sort order of the ORDER BY values is a critical part of the test for qualifying rows in a value-based frame; the numeric values alone do not determine exclusion or inclusion