sql - 窗口功能的默认窗口框架是什么

标签 sql apache-spark apache-spark-sql window-functions

运行以下代码:

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)
不确定我对默认窗口框架的理解是否正确

最佳答案

Spark Gotchas

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

  • if the ORDER BY clause is specified and the function accepts the frame specification, then the frame specification is defined by RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,
  • otherwise the frame specification is defined by ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

关于sql - 窗口功能的默认窗口框架是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47130030/

相关文章:

Scala Dataframe 列的空间保存为 databricks 表

sql server 从日期获取完整的月份名称

php - MySQL数据库表命名约定

database - Apache Spark 的主键

java - 在使用 Mockito 定义 mock 的行为时测试 RDD 的 equals

Scala Dataframe空检查列

mysql - 如果查询有 1 行,则返回一个值,否则返回另一个值

mysql - 如何获取来自同一属性的一些表值的平均值

python-3.x - SparkSession 在本地独立集群上花费的时间太长

caching - apache Spark sql 中的缓存表