Jump to content

Windowing (stream processing)

From Open Lakehouse Wiki
Revision as of 00:53, 25 November 2026 by Open Lakehouse Wiki sync (talk | contribs) (Sync from open-lakehouse/wiki@7c9abaa)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Windowing (stream processing)
Also known as Time windows, windowed aggregation
Field Stream processing
Key parts Window type (tumbling, sliding or hopping, session), window size, slide, gap, time column, watermark
Open source implementations Structured Streaming (window, session_window), Apache Flink (window assigners, windowing TVFs), Kafka Streams (TimeWindows, SlidingWindows, SessionWindows)
Related Session window, Event time and processing time, Watermark
Last verified 23 November 2026 against the Spark 4.3.0 Structured Streaming programming guide and python/pyspark/sql/functions/builtin.py at tag v4.3.0 of apache/spark, the Apache Flink 2.3 DataStream "Windows" and SQL "Windowing TVF" pages, and the Kafka Streams 4.4 DSL developer guide.

In stream processing, windowing is dividing an unbounded stream into finite groups of records, usually by time, so that aggregations such as counts and averages can be computed and emitted for each group. The three common window types are tumbling (fixed size, no overlap), sliding or hopping (fixed size, overlapping) and session windows (variable size, closed by a gap of inactivity). In Apache Spark Structured Streaming, a window is a grouping key built with the window or session_window function over an event-time column, and a watermark decides when a window is complete.[1]

What window types exist?

Structured Streaming supports three types of time windows: tumbling, sliding and session.[2]

  • A tumbling window is one of a series of fixed-size, non-overlapping, contiguous time intervals. Each record belongs to exactly one window.[2]
  • A sliding window also has a fixed size, but a new window starts every slide interval. When the slide is shorter than the size, windows overlap and one record belongs to several windows.[2]
  • A session window has no fixed size. It starts with a record and grows while further records arrive within a gap duration; it closes when no record arrives within the gap. See Session window.[2]

Spark's guide uses the example of counting words in 10-minute windows that slide every 5 minutes: a word received at 12:07 is counted in both the 12:00 to 12:10 window and the 12:05 to 12:15 window.[1]

How does windowing work in Structured Streaming?

The window(timeColumn, windowDuration, slideDuration=None, startTime=None) function, available since Spark 2.0.0, assigns each row to one or more windows. Without slideDuration, the windows are tumbling; the slide must be less than or equal to the window size.[3]

Window starts are inclusive and ends are exclusive, so 12:05 falls in [12:05, 12:10) and not in [12:00, 12:05). Windows are aligned to 1970-01-01 00:00:00 UTC; startTime shifts them, for example to hourly windows that start 15 minutes past the hour. Durations are fixed lengths of time, not calendar units, and windows measured in months are not supported. The result is a struct column named window with start and end fields.[3]

Because a window is just a grouping key, a windowed aggregation is written as groupBy(window(...), ...), and the same query runs on a batch DataFrame and on a stream.[1] The window_time function, added in Spark 3.4.0, returns a timestamp for a window column, which lets one windowed aggregation feed another, for example 5-minute windows rolled up into 1-hour windows.[3][1]

On a stream, a windowed aggregation keeps state for every open window. A watermark on the same event-time column lets Spark drop the state of windows that the watermark has passed; in append output mode, each window is emitted once, after that point.[4]

Example

from pyspark.sql import functions as F

events = spark.readStream.table("main.web.page_views")   # columns: ts, page

tumbling = (events.withWatermark("ts", "10 minutes")
            .groupBy(F.window("ts", "5 minutes"), "page")
            .count())

sliding = (events.withWatermark("ts", "10 minutes")
           .groupBy(F.window("ts", "10 minutes", "5 minutes"), "page")
           .count())

sessions = (events.withWatermark("ts", "10 minutes")
            .groupBy(F.session_window("ts", "30 minutes"), "page")
            .count())

The same queries in Spark SQL use window(ts, '5 minutes') and session_window(ts, '30 minutes') in the GROUP BY clause.

Windowing in other engines

Apache Flink's DataStream API defines windows with a window assigner. It ships assigners for tumbling, sliding, session and global windows; a global window puts all records of a key into one window and computes nothing unless a custom trigger is set. Tumbling and sliding assigners take an optional offset, similar to Spark's startTime.[5]

Flink SQL uses windowing table-valued functions (TVFs): TUMBLE, HOP, CUMULATE and SESSION. Each adds window_start, window_end and window_time columns, where window_time equals window_end minus 1 millisecond. The session TVF is supported only in streaming mode. Windowing TVFs replace the older grouped window functions.[6]

Kafka Streams names its window types differently. It has hopping, tumbling, sliding and session windows; hopping and tumbling windows are aligned to the epoch, with an inclusive lower bound and an exclusive upper bound. A Kafka Streams sliding window is not the same as a Spark sliding window: it is aligned to record timestamps, both bounds are inclusive, and it is used for joins (JoinWindows) and for SlidingWindows aggregations. The Kafka documentation notes that what other tools call sliding windows, Kafka Streams calls hopping windows.[7]

Comparison

Window type Spark Structured Streaming Apache Flink DataStream Apache Flink SQL Kafka Streams
Fixed, non-overlapping window(ts, size)[3] TumblingEventTimeWindows, TumblingProcessingTimeWindows[5] TUMBLE[6] Tumbling time window[7]
Fixed, overlapping window(ts, size, slide) (sliding)[3] Sliding window assigners[5] HOP[6] Hopping time window[7]
Gap-based session_window(ts, gap), static or per-row gap[3] Session window assigners, static gap or SessionWindowTimeGapExtractor[5] SESSION, streaming only, static gap[6] Session window[7]
Other types None built in Global windows with a custom trigger[5] CUMULATE[6] Sliding windows based on record time differences[7]
Window bounds Start inclusive, end exclusive, aligned to the Unix epoch with an optional startTime[3] Aligned to the epoch, optional offset[5] window_start, window_end, window_time columns[6] Hopping and tumbling aligned to the epoch; sliding aligned to records[7]

See also

References

  1. ↑ 1.0 1.1 1.2 1.3 Structured Streaming: Window Operations on Event Time (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 2.2 2.3 Structured Streaming: Types of time windows (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 3.2 3.3 3.4 3.5 3.6 python/pyspark/sql/functions/builtin.py (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ Structured Streaming: Handling Late Data and Watermarking (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ 7.0 7.1 7.2 7.3 7.4 7.5 Kafka Streams DSL (Apache Kafka 4.4). Apache Software Foundation.Retrieved 2026-11-23.