OVER, PARTITION BY, and WINDOW

Window functions were introduced in the SQL:2003 standard and expanded in the SQL:2011 standard. They let you run calculations on a set of table rows that are related to the current row in some way.

Unlike aggregate functions, window functions don’t group rows into one output row: the number of rows in the resulting table is always the same as in the source table.

If a query contains both aggregate and window functions, grouping is performed and aggregate function values are calculated first. The calculated values of aggregate functions can be used as window function arguments (but not the other way around).

Syntax

General syntax for calling a window function is as follows

  1. function_name([expression [, expression ...]]) OVER (window_definition)
  2. or
  3. function_name([expression [, expression ...]]) OVER window_name

WINDOW - 图1

Here, window_name (window name) is an arbitrary ID that is unique within the query and expression is an arbitrary expression that contains no window function calls.

In the query, each window name must be mapped to the window definition (window_definition):

  1. SELECT
  2. F0(...) OVER (window_definition_0),
  3. F1(...) OVER w1,
  4. F2(...) OVER w2,
  5. ...
  6. FROM my_table
  7. WINDOW
  8. w1 AS (window_definition_1),
  9. ...
  10. w2 AS (window_definition_2)
  11. ;

WINDOW - 图2

Here, the window_definition is written as

  1. [ PARTITION BY (expression AS column_identifier | column_identifier) [, ...] ]
  2. [ ORDER BY expression [ASC | DESC] ]
  3. [ frame_definition ]

WINDOW - 图3

You can set an optional frame definition (frame_definition) one of two ways:

  • ROWS frame_begin
  • ROWS BETWEEN frame_begin AND frame_end

The frame start (frame_begin) and frame end (frame_end) are set one of the following ways:

  • UNBOUNDED PRECEDING
  • offset PRECEDING
  • CURRENT ROW
  • offset FOLLOWING
  • UNBOUNDED FOLLOWING

Here, the frame offset is a non-negative numeric literal. If the frame end isn’t set, the CURRENT ROW is assumed.

There should be no window function calls in any of the expressions inside the window definition.

Calculation algorithm

Partitioning

If PARTITION BY is set, the source table rows are grouped into partitions, which are then handled independently of each other.
If PARTITION BY isn’t set, all rows in the source table are put in the same partition. If ORDER BY is set, it determines the order of rows in a partition.
Both in PARTITION BY and GROUP BY you can use aliases and SessionWindow.

If ORDER BY is omitted, the order of rows in the partition is undefined.

Frame

The frame_definition specifies a set of partition rows that fall into the window frame associated with the current row.

In ROWS mode (the only one that YQL currently supports), the window frame contains rows with the specified offsets relative to the current row in the partition.

  • For example, if ROWS BETWEEN 3 PRECEDING AND 5 FOLLOWING is used, the window frame contains 3 rows preceding the current one, the current row, and 5 rows following it.

The set of rows in the window frame may change depending on which row is the current one.

  • For example, for the first row in the partition, the ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING window frame will have no rows.

Setting UNBOUNDED PRECEDING as the frame start means “from the first partition row” and UNBOUNDED FOLLOWING as the frame end — “up to the last partition row”. Setting CURRENT ROW means “from/to the current row”.

If no frame_definition is specified, a set of rows to be included in the window frame depends on whether there is ORDER BY in the window_definition.
Namely, if there is ORDER BY, then ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW is implicitly assumed. If none, then ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

Further, depending on the specific window function, it’s calculated either based on the set of rows in the partition or the set of rows in the window frame.

List of available window functions

Examples:

  1. SELECT
  2. COUNT(*) OVER w AS rows_count_in_window,
  3. some_other_value -- access the current row
  4. FROM `my_table`
  5. WINDOW w AS (
  6. PARTITION BY partition_key_column
  7. ORDER BY int_column
  8. );

WINDOW - 图4

  1. SELECT
  2. LAG(my_column, 2) OVER w AS row_before_previous_one
  3. FROM `my_table`
  4. WINDOW w AS (
  5. PARTITION BY partition_key_column
  6. );

WINDOW - 图5

  1. SELECT
  2. -- AVG (like all aggregate functions used as window functions)
  3. -- is calculated on the window frame
  4. AVG(some_value) OVER w AS avg_of_prev_current_next,
  5. some_other_value -- access the current row
  6. FROM my_table
  7. WINDOW w AS (
  8. PARTITION BY partition_key_column
  9. ORDER BY int_column
  10. ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
  11. );

WINDOW - 图6

  1. SELECT
  2. -- LAG doesn't depend on the window frame position
  3. LAG(my_column, 2) OVER w AS row_before_previous_one
  4. FROM my_table
  5. WINDOW w AS (
  6. PARTITION BY partition_key_column
  7. ORDER BY my_column
  8. );

WINDOW - 图7

Implementation specifics

  • Functions calculated on the ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING or ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW window frame are implemented efficiently (do not require additional memory and their computation runs on a partition in O(partition size) time).

  • For the ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING window frame, you can choose the execution strategy in RAM by specifying the COMPACT hint after the PARTITION keyword.

    For example, PARTITION COMPACT BY key or PARTITION COMPACT BY () (if PARTITION BY was missing initially).

    If the COMPACT hint is specified, this requires additional memory equal to O(partition size), but then no extra JOIN operation is made.

  • If the window frame doesn’t start with UNBOUNDED PRECEDING, calculating window functions on this window requires additional memory equal to O(the maximum number of rows from the window boundaries to the current row), while the computation time is equal to O(number_of_partition_rows * window_size).

  • For the window frame starting with UNBOUNDED PRECEDING and ending with N, where N is neither CURRENT ROW nor UNBOUNDED FOLLOWING, additional memory equal to O(N) is required and the computation time is equal to O(N * number_of_partition_rows).

  • The LEAD(expr, N) and LAG(expr, N) functions always require O(N) of RAM.

Given the above, a query with ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING should, if possible, be changed to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW by reversing the ORDER BY sorting order.