Window Aggregation

    Window aggregations are defined in the clause contains “window_start” and “window_end” columns of the relation applied Windowing TVF. Just like queries with regular GROUP BY clauses, queries with a group by window aggregation will compute a single result row per group.

    Unlike other aggregations on continuous tables, window aggregation do not emit intermediate results but only a final result, the total aggregation at the end of the window. Moreover, window aggregations purge all intermediate state when no longer needed.

    Flink supports TUMBLE, HOP and CUMULATE types of window aggregations. In streaming mode, the time attribute field of a window table-valued function must be on either . See Windowing TVF for more windowing functions information. In batch mode, the time attribute field of a window table-valued function must be an attribute of type TIMESTAMP or TIMESTAMP_LTZ.

    Here are some examples for TUMBLE, HOP and CUMULATE window aggregations.

    1. -- tables must have time attribute, e.g. `bidtime` in this table
    2. Flink SQL> desc Bid;
    3. +-------------+------------------------+------+-----+--------+---------------------------------+
    4. | name | type | null | key | extras | watermark |
    5. +-------------+------------------------+------+-----+--------+---------------------------------+
    6. | bidtime | TIMESTAMP(3) *ROWTIME* | true | | | `bidtime` - INTERVAL '1' SECOND |
    7. | price | DECIMAL(10, 2) | true | | | |
    8. | item | STRING | true | | | |
    9. | supplier_id | STRING | true | | | |
    10. +-------------+------------------------+------+-----+--------+---------------------------------+
    11. Flink SQL> SELECT * FROM Bid;
    12. +------------------+-------+------+-------------+
    13. | bidtime | price | item | supplier_id |
    14. +------------------+-------+------+-------------+
    15. | 2020-04-15 08:05 | 4.00 | C | supplier1 |
    16. | 2020-04-15 08:07 | 2.00 | A | supplier1 |
    17. | 2020-04-15 08:09 | 5.00 | D | supplier2 |
    18. | 2020-04-15 08:11 | 3.00 | B | supplier2 |
    19. | 2020-04-15 08:13 | 1.00 | E | supplier1 |
    20. | 2020-04-15 08:17 | 6.00 | F | supplier2 |
    21. -- tumbling window aggregation
    22. Flink SQL> SELECT window_start, window_end, SUM(price)
    23. FROM TABLE(
    24. TUMBLE(TABLE Bid, DESCRIPTOR(bidtime), INTERVAL '10' MINUTES))
    25. GROUP BY window_start, window_end;
    26. +------------------+------------------+-------+
    27. | window_start | window_end | price |
    28. +------------------+------------------+-------+
    29. | 2020-04-15 08:00 | 2020-04-15 08:10 | 11.00 |
    30. | 2020-04-15 08:10 | 2020-04-15 08:20 | 10.00 |
    31. +------------------+------------------+-------+
    32. -- hopping window aggregation
    33. Flink SQL> SELECT window_start, window_end, SUM(price)
    34. FROM TABLE(
    35. HOP(TABLE Bid, DESCRIPTOR(bidtime), INTERVAL '5' MINUTES, INTERVAL '10' MINUTES))
    36. GROUP BY window_start, window_end;
    37. +------------------+------------------+-------+
    38. +------------------+------------------+-------+
    39. | 2020-04-15 08:00 | 2020-04-15 08:10 | 11.00 |
    40. | 2020-04-15 08:05 | 2020-04-15 08:15 | 15.00 |
    41. | 2020-04-15 08:10 | 2020-04-15 08:20 | 10.00 |
    42. | 2020-04-15 08:15 | 2020-04-15 08:25 | 6.00 |
    43. +------------------+------------------+-------+
    44. -- cumulative window aggregation
    45. Flink SQL> SELECT window_start, window_end, SUM(price)
    46. FROM TABLE(
    47. CUMULATE(TABLE Bid, DESCRIPTOR(bidtime), INTERVAL '2' MINUTES, INTERVAL '10' MINUTES))
    48. GROUP BY window_start, window_end;
    49. | window_start | window_end | price |
    50. +------------------+------------------+-------+
    51. | 2020-04-15 08:00 | 2020-04-15 08:06 | 4.00 |
    52. | 2020-04-15 08:00 | 2020-04-15 08:08 | 6.00 |
    53. | 2020-04-15 08:00 | 2020-04-15 08:10 | 11.00 |
    54. | 2020-04-15 08:10 | 2020-04-15 08:12 | 3.00 |
    55. | 2020-04-15 08:10 | 2020-04-15 08:14 | 4.00 |
    56. | 2020-04-15 08:10 | 2020-04-15 08:16 | 4.00 |
    57. | 2020-04-15 08:10 | 2020-04-15 08:18 | 10.00 |
    58. | 2020-04-15 08:10 | 2020-04-15 08:20 | 10.00 |
    59. +------------------+------------------+-------+

    Note: in order to better understand the behavior of windowing, we simplify the displaying of timestamp values to not show the trailing zeros, e.g. 2020-04-15 08:05 should be displayed as 2020-04-15 08:05:00.000 in Flink SQL Client if the type is TIMESTAMP(3).

    GROUPING SETS

    Window aggregations also support GROUPING SETS syntax. Grouping sets allow for more complex grouping operations than those describable by a standard GROUP BY. Rows are grouped separately by each specified grouping set and aggregates are computed for each group just as for simple GROUP BY clauses.

    Window aggregations with GROUPING SETS require both the window_start and window_end columns have to be in the GROUP BY clause, but not in the GROUPING SETS clause.

    Each sublist of GROUPING SETS may specify zero or more columns or expressions and is interpreted the same way as though used directly in the GROUP BY clause. An empty grouping set means that all rows are aggregated down to a single group, which is output even if no input rows were present.

    References to the grouping columns or expressions are replaced by null values in result rows for grouping sets in which those columns do not appear.

    ROLLUP

    ROLLUP is a shorthand notation for specifying a common type of grouping set. It represents the given list of expressions and all prefixes of the list, including the empty list.

    For example, the following query is equivalent to the one above.

    1. SELECT window_start, window_end, supplier_id, SUM(price) as price
    2. FROM TABLE(
    3. TUMBLE(TABLE Bid, DESCRIPTOR(bidtime), INTERVAL '10' MINUTES))
    4. GROUP BY window_start, window_end, ROLLUP (supplier_id);

    CUBE

    CUBE is a shorthand notation for specifying a common type of grouping set. It represents the given list and all of its possible subsets - the power set.

    Window aggregations with CUBE requires both the window_start and columns have to be in the GROUP BY clause, but not in the CUBE clause.

    For example, the following two queries are equivalent.

    The start and end timestamps of group windows can be selected with the grouped window_start and window_end columns.

    Cascading Window Aggregation

    The window_start and window_end columns are regular timestamp columns, not time attributes. Thus they can’t be used as time attributes in subsequent time-based operations. In order to propagate time attributes, you need to additionally add window_time column into GROUP BY clause. The window_time is the third column produced by which is a time attribute of the assigned window. Adding window_time into GROUP BY clause makes window_time also to be group key that can be selected. Then following queries can use this column for subsequent time-based operations, such as cascading window aggregations and Window TopN.

    The following shows a cascading window aggregation where the first window aggregation propagates the time attribute for the second window aggregation.

    1. -- tumbling 5 minutes for each supplier_id
    2. CREATE VIEW window1 AS
    3. -- Note: The window start and window end fields of inner Window TVF are optional in the select clause. However, if they appear in the clause, they need to be aliased to prevent name conflicting with the window start and window end of the outer Window TVF.
    4. SELECT window_start as window_5mintumble_start, window_end as window_5mintumble_end, window_time as rowtime, SUM(price) as partial_price
    5. FROM TABLE(
    6. TUMBLE(TABLE Bid, DESCRIPTOR(bidtime), INTERVAL '5' MINUTES))
    7. GROUP BY supplier_id, window_start, window_end, window_time;
    8. -- tumbling 10 minutes on the first window
    9. SELECT window_start, window_end, SUM(partial_price) as total_price
    10. FROM TABLE(
    11. TUMBLE(TABLE window1, DESCRIPTOR(rowtime), INTERVAL '10' MINUTES))
    12. GROUP BY window_start, window_end;

    Group Window Aggregation

    Batch Streaming

    Time Attributes

    In streaming mode, the time_attr argument of the group window function must refer to a valid time attribute that specifies the processing time or event time of rows. See the to learn how to define time attributes.

    In batch mode, the time_attr argument of the group window function must be an attribute of type TIMESTAMP.

    The start and end timestamps of group windows as well as time attributes can be selected with the following auxiliary functions:

    Auxiliary FunctionDescription
    TUMBLE_START(time_attr, interval)
    HOP_START(time_attr, interval, interval)
    SESSION_START(time_attr, interval)

    Returns the timestamp of the inclusive lower bound of the corresponding tumbling, hopping, or session window.

    TUMBLE_END(time_attr, interval)
    HOP_END(time_attr, interval, interval)
    SESSION_END(time_attr, interval)

    Returns the timestamp of the exclusive upper bound of the corresponding tumbling, hopping, or session window.

    Note: The exclusive upper bound timestamp cannot be used as a rowtime attribute in subsequent time-based operations, such as and group window or .

    TUMBLE_ROWTIME(time_attr, interval)
    HOP_ROWTIME(time_attr, interval, interval)
    SESSION_ROWTIME(time_attr, interval)

    Returns the timestamp of the inclusive upper bound of the corresponding tumbling, hopping, or session window.

    The resulting attribute is a rowtime attribute that can be used in subsequent time-based operations such as and group window or .

    TUMBLE_PROCTIME(time_attr, interval)
    HOP_PROCTIME(time_attr, interval, interval)

    Returns a proctime attribute that can be used in subsequent time-based operations such as and group window or .

    Note: Auxiliary functions must be called with exactly same arguments as the group window function in the GROUP BY clause.