Windowing

A window groups the rows of an unbounded stream into buckets of finite size, so that you can compute results such as counts and sums for each period of time. In SQL, you define windows with windowing table-valued functions.

Windowing table-valued functions split a table into windows and add the columns window_start, window_end, and window_time to each row. You then group by these columns to aggregate each window.

Table 1. Windowing functions
Function Description
TUMBLE Fixed-size windows that do not overlap. Each row belongs to exactly one window.
HOP Fixed-size windows that slide by a smaller interval. Windows overlap, and a row can belong to more than one window.
CUMULATE Windows that start at the same time and grow by a step until a maximum size.
SESSION Windows that end after a period of inactivity. The size is not fixed. Available only for streaming queries.

The following example counts the orders in each window of 10 minutes:

SELECT window_start, window_end, COUNT(*) AS order_count
FROM TABLE(
  TUMBLE(TABLE orders, DESCRIPTOR(order_time), INTERVAL '10' MINUTES))
GROUP BY window_start, window_end;

To close a window correctly, the window column must be an event time column with a watermark. For more information, see Event time and watermarks.

For the full syntax, see Windowing table-valued functions in the Apache Flink documentation.