<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Session_window</id>
	<title>Session window - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Session_window"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Session_window&amp;action=history"/>
	<updated>2026-12-06T00:13:25Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Session_window&amp;diff=177&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Session_window&amp;diff=177&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:50Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Session window&lt;br /&gt;
| also_known_as   = Gap-based window, sessionization&lt;br /&gt;
| field           = [[Stream processing]]&lt;br /&gt;
| introduced      = Spark 3.2.0 (&amp;lt;code&amp;gt;session_window&amp;lt;/code&amp;gt;)&lt;br /&gt;
| key_parts       = Grouping key, event-time column, gap duration (static or per record), [[watermark]]&lt;br /&gt;
| implementations = [[Structured Streaming]] (&amp;lt;code&amp;gt;session_window&amp;lt;/code&amp;gt;), [[Apache Flink]] (session window assigners, &amp;lt;code&amp;gt;SESSION&amp;lt;/code&amp;gt; TVF), Kafka Streams (&amp;lt;code&amp;gt;SessionWindows&amp;lt;/code&amp;gt;)&lt;br /&gt;
| related         = [[Windowing (stream processing)]], [[Stateful stream processing]], [[transformWithState]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the Spark 4.3.0 Structured Streaming programming guide and Real-time Mode guide, &amp;lt;code&amp;gt;python/pyspark/sql/functions/builtin.py&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;SQLConf.scala&amp;lt;/code&amp;gt; at tag v4.3.0 of apache/spark, SPARK-10816, the Apache Flink 2.3 DataStream &amp;quot;Windows&amp;quot; and SQL &amp;quot;Windowing TVF&amp;quot; pages, and the Kafka Streams 4.4 DSL developer guide}}&lt;br /&gt;
&lt;br /&gt;
In [[stream processing]], a &amp;#039;&amp;#039;&amp;#039;session window&amp;#039;&amp;#039;&amp;#039; is a window whose length is set by the data: it opens with a record for a key, extends each time another record for that key arrives within a &amp;#039;&amp;#039;&amp;#039;gap duration&amp;#039;&amp;#039;&amp;#039;, and closes when no record arrives for the length of the gap. Session windows group bursts of activity, such as one user&amp;#039;s clicks during a visit, without fixing the window size in advance. In [[Apache Spark]] [[Structured Streaming]], session windows are built with the &amp;lt;code&amp;gt;session_window&amp;lt;/code&amp;gt; function, added in Spark 3.2.0.&amp;lt;ref name=&amp;quot;builtin&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/functions/builtin.py |title=python/pyspark/sql/functions/builtin.py (Apache Spark v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;jira&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-10816 |title=SPARK-10816: EventTime based sessionization (session window) |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How is a session window different from a tumbling or sliding window? ==&lt;br /&gt;
Tumbling and sliding windows have a fixed size and fixed boundaries, so the engine can compute a record&amp;#039;s window from its timestamp alone. A session window has neither: its start is the first record of the session and its end moves later with every new record. Spark&amp;#039;s guide describes it as having a dynamic size that depends on the inputs.&amp;lt;ref name=&amp;quot;ss-types&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/apis-on-dataframes-and-datasets.html#types-of-time-windows |title=Structured Streaming: Types of time windows (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; See [[Windowing (stream processing)]] for the other window types.&lt;br /&gt;
&lt;br /&gt;
Because a later record can join two earlier sessions into one, engines implement session windows by merging. [[Apache Flink]]&amp;#039;s documentation states that its session window operator creates a new window for every record and merges windows that are closer to each other than the gap.&amp;lt;ref name=&amp;quot;flink-win&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-release-2.3/docs/dev/datastream/operators/windows/#session-windows |title=Windows: Session Windows (Apache Flink 2.3) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does session_window work in Spark? ==&lt;br /&gt;
&amp;lt;code&amp;gt;session_window(timeColumn, gapDuration)&amp;lt;/code&amp;gt; returns a struct column named &amp;lt;code&amp;gt;session_window&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;start&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt; fields. The end of a session is the timestamp of its latest record plus the gap, so it grows as records join the session.&amp;lt;ref name=&amp;quot;builtin&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gap can be a static string such as &amp;lt;code&amp;gt;&amp;quot;5 minutes&amp;quot;&amp;lt;/code&amp;gt; or a column expression evaluated per row, for example a different gap per user type. With a dynamic gap, a session&amp;#039;s range is the union of the ranges of its events (each event&amp;#039;s time plus its own gap), and rows whose gap evaluates to zero or a negative value are filtered out of the aggregation.&amp;lt;ref name=&amp;quot;ss-types&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;builtin&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A streaming session window query has these restrictions in Spark 4.3.0:&amp;lt;ref name=&amp;quot;ss-types&amp;quot; /&amp;gt;&lt;br /&gt;
* Update [[output mode]] is not supported; use append (sessions are emitted when the [[watermark]] passes their end) or complete.&lt;br /&gt;
* The grouping key must contain at least one column besides &amp;lt;code&amp;gt;session_window&amp;lt;/code&amp;gt;. A global session window, with &amp;lt;code&amp;gt;session_window&amp;lt;/code&amp;gt; as the only key, is allowed only in batch queries.&lt;br /&gt;
&lt;br /&gt;
By default Spark does not pre-aggregate sessions before the shuffle, because doing so needs an extra local sort. Setting &amp;lt;code&amp;gt;spark.sql.streaming.sessionWindow.merge.sessions.in.local.partition&amp;lt;/code&amp;gt; (default &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;, since 3.2.0) to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; sorts and merges sessions within each partition first, which helps when many rows of a batch belong to the same session.&amp;lt;ref name=&amp;quot;ss-types&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (Apache Spark v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Session-window aggregation is not supported in Real-time Mode as of Spark 4.3.0.&amp;lt;ref name=&amp;quot;rtm&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/real-time-mode.html |title=Real-time Mode (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Sessions with custom rules, such as closing a session on a logout event, need arbitrary stateful processing with [[transformWithState]].&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
This PySpark query counts page views per user session, where a session ends after 30 minutes without activity, and uses a shorter gap for one class of users:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql import functions as F&lt;br /&gt;
&lt;br /&gt;
views = spark.readStream.table(&amp;quot;main.web.page_views&amp;quot;)   # columns: ts, user_id, user_type&lt;br /&gt;
&lt;br /&gt;
gap = F.when(F.col(&amp;quot;user_type&amp;quot;) == &amp;quot;bot&amp;quot;, &amp;quot;1 minute&amp;quot;).otherwise(&amp;quot;30 minutes&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
sessions = (views&lt;br /&gt;
    .withWatermark(&amp;quot;ts&amp;quot;, &amp;quot;1 hour&amp;quot;)&lt;br /&gt;
    .groupBy(F.session_window(&amp;quot;ts&amp;quot;, gap), &amp;quot;user_id&amp;quot;)&lt;br /&gt;
    .agg(F.count(&amp;quot;*&amp;quot;).alias(&amp;quot;views&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
(sessions.writeStream&lt;br /&gt;
    .outputMode(&amp;quot;append&amp;quot;)&lt;br /&gt;
    .option(&amp;quot;checkpointLocation&amp;quot;, &amp;quot;/checkpoints/sessions&amp;quot;)&lt;br /&gt;
    .toTable(&amp;quot;main.web.user_sessions&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Spark SQL the same grouping is &amp;lt;code&amp;gt;GROUP BY session_window(ts, &amp;#039;30 minutes&amp;#039;), user_id&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Spark session_window compared with Flink ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Spark &amp;lt;code&amp;gt;session_window&amp;lt;/code&amp;gt; !! Flink DataStream session assigners !! Flink SQL &amp;lt;code&amp;gt;SESSION&amp;lt;/code&amp;gt; TVF&lt;br /&gt;
|-&lt;br /&gt;
! Gap&lt;br /&gt;
| Static string or per-row column expression&amp;lt;ref name=&amp;quot;builtin&amp;quot; /&amp;gt;&lt;br /&gt;
| Static gap, or a &amp;lt;code&amp;gt;SessionWindowTimeGapExtractor&amp;lt;/code&amp;gt; for dynamic gaps&amp;lt;ref name=&amp;quot;flink-win&amp;quot; /&amp;gt;&lt;br /&gt;
| Static gap only&amp;lt;ref name=&amp;quot;flink-tvf&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-release-2.3/docs/sql/reference/queries/window-tvf/ |title=Windowing table-valued functions (Apache Flink 2.3) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Key&lt;br /&gt;
| At least one grouping column besides the window in streaming&amp;lt;ref name=&amp;quot;ss-types&amp;quot; /&amp;gt;&lt;br /&gt;
| Keyed (&amp;lt;code&amp;gt;keyBy&amp;lt;/code&amp;gt;) or non-keyed streams&amp;lt;ref name=&amp;quot;flink-win&amp;quot; /&amp;gt;&lt;br /&gt;
| Optional &amp;lt;code&amp;gt;PARTITION BY&amp;lt;/code&amp;gt; key columns&amp;lt;ref name=&amp;quot;flink-tvf&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Time&lt;br /&gt;
| Event time; processing time by windowing on &amp;lt;code&amp;gt;current_timestamp&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;builtin&amp;quot; /&amp;gt;&lt;br /&gt;
| Event-time and processing-time variants&amp;lt;ref name=&amp;quot;flink-win&amp;quot; /&amp;gt;&lt;br /&gt;
| Event or processing time attribute&amp;lt;ref name=&amp;quot;flink-tvf&amp;quot; /&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The two engines differ in batch support and in late data. Spark runs &amp;lt;code&amp;gt;session_window&amp;lt;/code&amp;gt; in batch queries too, including a global session window, while Flink&amp;#039;s &amp;lt;code&amp;gt;SESSION&amp;lt;/code&amp;gt; TVF is not supported in batch mode.&amp;lt;ref name=&amp;quot;ss-types&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;flink-tvf&amp;quot; /&amp;gt; In Spark, a record that arrives after the watermark has passed its session&amp;#039;s end is not added to the session; Flink&amp;#039;s DataStream session windows drop such records by default, and &amp;lt;code&amp;gt;allowedLateness&amp;lt;/code&amp;gt; keeps windows open longer.&amp;lt;ref name=&amp;quot;ss-types&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;flink-win&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Kafka Streams also has session windows: sessions are periods of activity separated by an inactivity gap, and the process is called sessionization.&amp;lt;ref name=&amp;quot;kstreams&amp;quot;&amp;gt;{{Cite web |url=https://kafka.apache.org/44/streams/developer-guide/dsl-api/ |title=Kafka Streams DSL (Apache Kafka 4.4) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Windowing (stream processing)]]&lt;br /&gt;
* [[Watermark]]&lt;br /&gt;
* [[Output mode]]&lt;br /&gt;
* [[transformWithState]]&lt;br /&gt;
* [[Structured Streaming]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Pipelines and orchestration]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>