窗口函数的默认窗口框架是什么 [英] What's the default window frame for window functions

查看:20
本文介绍了窗口函数的默认窗口框架是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码:

val sales = Seq(
  (0, 0, 0, 5),
  (1, 0, 1, 3),
  (2, 0, 2, 1),
  (3, 1, 0, 2),
  (4, 2, 0, 8),
  (5, 2, 2, 8))
  .toDF("id", "orderID", "prodID", "orderQty")

val orderedByID = Window.orderBy('id)

val totalQty = sum('orderQty).over(orderedByID).as('running_total)
val salesTotalQty = sales.select('*, totalQty).orderBy('id)
salesTotalQty.show

结果是:

+---+-------+------+--------+-------------+
| id|orderID|prodID|orderQty|running_total|
+---+-------+------+--------+-------------+
|  0|      0|     0|       5|            5|
|  1|      0|     1|       3|            8|
|  2|      0|     2|       1|            9|
|  3|      1|     0|       2|           11|
|  4|      2|     0|       8|           19|
|  5|      2|     2|       8|           27|
+---+-------+------+--------+-------------+

上面代码中没有定义窗口框架,看起来默认的窗口框架是rowsBetween(Window.unboundedPreceding, Window.currentRow)

There is no window frame defined in the above code, it looks the default window frame is rowsBetween(Window.unboundedPreceding, Window.currentRow)

不确定我对默认窗口框架的理解是否正确

Not sure my understanding about default window frame is correct

推荐答案

来自 Spark 陷阱

默认框架规范取决于给定窗口定义的其他方面:

Default frame specification depends on other aspects of a given window defintion:

  • 如果指定了 ORDER BY 子句并且函数接受框架规范,则框架规范由 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 定义,
  • 否则,框架规范由 ROWS BETWEEN UNBOUNDED PRECEDING 和 UNBOUNDED FOLLOWING 定义.

这篇关于窗口函数的默认窗口框架是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆