WindowSpec¶
WindowSpec
defines a window specification for Window Functions.
A window (specification) describes a group of records that are in some relation to the current record.
Creating Instance¶
WindowSpec
takes the following to be created:
WindowSpec
is created when:
Window
utility is used to create a default WindowSpecWindowSpec
is requested to partitionBy, orderBy, rowsBetween, rangeBetween
Partition Specification¶
Partition specification are Expressions that define which rows are in the same partition. With no partition defined, all rows belong to a single partition.
Order Specification¶
Order specification are SortOrders that define how rows are ordered in a partition that in turn defines the position of a row in a partition.
The ordering could be ascending (ASC
in SQL or asc
in Scala) or descending (DESC
or desc
).
Frame Specification¶
Frame specification is a WindowFrame
that defines the rows to include in the frame for the current row, based on their relative position to the current row.
Frame specification is defined using rowsBetween and rangeBetween operators.
For example, "the three rows preceding the current row to the current row" describes a frame including the current input row and three rows appearing before the current row.
Window
utility defines special Frame Boundaries.
rowsBetween¶
rowsBetween(
start: Long,
end: Long): WindowSpec
rowsBetween
creates a WindowSpec with a SpecifiedWindowFrame
boundary frame (of RowFrame
type) from the givenstart
and end
(both inclusive).
rangeBetween¶
rangeBetween(
start: Long,
end: Long): WindowSpec
rangeBetween
creates a WindowSpec with a SpecifiedWindowFrame
boundary frame (of RangeFrame type) from the givenstart
and end
(both inclusive).
withAggregate¶
withAggregate(
aggregate: Column): Column
withAggregate
creates a WindowSpecDefinition expression for the partitionSpec, orderSpec and the WindowFrame (of this WindowSpec
).
In the end, withAggregate
creates a WindowExpression expression for the aggregate expression (of the given Column) and the WindowSpecDefinition.
withAggregate
is used when:
- Column.over operator is used