Jump to content

Window functions

From Open Lakehouse Wiki
Window functions
Also known as Analytic functions, OVER clause
Field SQL query processing
Key parts Partitioning, ordering, window frame (ROWS or RANGE), ranking, offset and aggregate functions
Open source implementations Apache Spark, PostgreSQL, Trino, DuckDB, Snowflake
Related Shuffle, Partition, Lateral column alias
Last verified 23 November 2026 against the v4.3.0 tag of apache/spark (docs/sql-ref-syntax-qry-select-window.md, docs/sql-ref-syntax-qry-select-qualify.md, docs/sql-ref-syntax-qry-select.md, SqlBaseParser.g4, python/pyspark/sql/window.py, WindowExecBase.scala, sql-tests/inputs/window.sql), the Spark 4.2.0 release notes, PostgreSQL 19 documentation, Trino 484 documentation, DuckDB 2.0 documentation and Snowflake documentation.

A window function is a SQL function that computes a value for each row from a group of related rows, called the window, without collapsing those rows into one as GROUP BY does; in Apache Spark it is written as a function followed by an OVER clause that names the partitioning, the ordering and an optional frame.[1] Window functions are used for rankings, running totals, moving averages and comparisons with the previous or next row. SQL engines that support them differ in frame types, null handling and the filtering clause QUALIFY, which the comparison table below sets out.

How does a window function work?

The OVER clause has three parts. PARTITION BY splits rows into independent groups; ORDER BY orders rows inside each group; and the frame clause picks which rows around the current row the function sees. In Spark SQL the frame is written { RANGE | ROWS } { frame_start | BETWEEN frame_start AND frame_end }, with bounds UNBOUNDED PRECEDING, offset PRECEDING, CURRENT ROW, offset FOLLOWING or UNBOUNDED FOLLOWING; an omitted frame_end means CURRENT ROW.[1]

A ROWS frame counts physical rows. A RANGE frame uses value distance on the ordering column, so rows with equal ordering values (peers) are in or out of the frame together. Spark accepts numeric, date and interval offsets in RANGE frames, for example RANGE BETWEEN CURRENT ROW AND INTERVAL 23 DAYS 4 HOURS FOLLOWING.[2]

What is the default frame?

In Spark, when a window has no ordering, the frame is the whole partition (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING). When ordering is defined, the default is a growing frame, RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.[3] This is why last_value(x) OVER (ORDER BY t) returns the current row's value (or that of its last peer) unless the frame is extended to UNBOUNDED FOLLOWING.

Which kinds of window functions are there?

Spark groups window functions into three kinds.[1]

  • Ranking functions: ROW_NUMBER, RANK, DENSE_RANK, PERCENT_RANK, NTILE.
  • Analytic (offset and value) functions: LAG, LEAD, NTH_VALUE, FIRST_VALUE, LAST_VALUE, CUME_DIST.
  • Aggregate functions used with OVER, such as SUM, AVG, MIN, MAX and COUNT.

LAG, LEAD, NTH_VALUE, FIRST_VALUE and LAST_VALUE accept IGNORE NULLS in Spark; the default is RESPECT NULLS.[1] RANK leaves gaps after ties and DENSE_RANK does not.[1]

How do you filter on a window function?

Spark SQL has a QUALIFY clause that filters rows after window functions are evaluated, either by referring to a window function's alias in the SELECT list or by writing the window function in the condition itself; it was added in Spark 4.2.0 (SPARK-31561).[4][5] Without QUALIFY, the same result needs a subquery or common table expression with a WHERE on the computed column. A named window can be declared once with the WINDOW clause and reused.[6]

Example

PySpark, with a rank, the previous salary and a three-row moving average per department:[3]

from pyspark.sql import Window, functions as F

w = Window.partitionBy("dept").orderBy("salary")

df.select(
    "name", "dept", "salary",
    F.rank().over(w).alias("rank"),
    F.lag("salary").over(w).alias("prev_salary"),
    F.avg("salary").over(w.rowsBetween(-1, 1)).alias("moving_avg"),
).show()

Spark SQL, keeping the highest-paid employee of each department with QUALIFY:[4]

SELECT name, dept, salary
FROM employees
QUALIFY ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) = 1;

How do window functions run in Spark?

Spark's physical window operator requires its input to be clustered by the PARTITION BY columns and sorted by the partition and ORDER BY columns, so the planner usually adds a shuffle and a sort before it. A window with no PARTITION BY requires all rows in one partition, and Spark logs a warning that this "can cause serious performance degradation".[7]

Engine differences

Feature Apache Spark 4.3 PostgreSQL 19 Trino 484 DuckDB 2.0 Snowflake
Frame types ROWS, RANGE[1] ROWS, RANGE, GROUPS[8] ROWS, RANGE, GROUPS[9] ROWS, RANGE, GROUPS[10] ROWS, RANGE[11]
Frame exclusion (EXCLUDE) Not in the frame grammar[12] Yes[8] Yes[13] Yes[10] Not documented[11]
Default frame with ORDER BY RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW[3] Same[8] Same[13] Same[10] Same for aggregates; ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING for FIRST_VALUE, LAST_VALUE, NTH_VALUE[11]
IGNORE NULLS on offset and value functions Yes[1] Yes[14] Yes[15] Yes[10] Yes (for example LAG)[16]
QUALIFY Yes, since 4.2.0[5] Not documented[17] Not documented[13] Yes[10] Yes[18]
Named WINDOW clause Yes[6] Yes[17] Yes[15] Yes[10] Not documented[11]

"Not documented" means the cited documentation page for that engine does not describe the feature.

See also

References

  1. ↑ 1.0 1.1 1.2 1.3 1.4 1.5 1.6 Window Functions (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ sql-tests/inputs/window.sql (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 3.2 window.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ 4.0 4.1 QUALIFY Clause (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ 5.0 5.1 Spark Release 4.2.0. Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ 6.0 6.1 SELECT (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ WindowExecBase.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  8. ↑ 8.0 8.1 8.2 Value Expressions: Window Function Calls (PostgreSQL 19). PostgreSQL Global Development Group.Retrieved 2026-11-23.
  9. ↑ Row pattern recognition in window structures (Trino 484). Trino Software Foundation.Retrieved 2026-11-23.
  10. ↑ 10.0 10.1 10.2 10.3 10.4 10.5 Window Functions (DuckDB 2.0). DuckDB Foundation.Retrieved 2026-11-23.
  11. ↑ 11.0 11.1 11.2 11.3 Window function syntax and usage. Snowflake.Retrieved 2026-11-23.
  12. ↑ SqlBaseParser.g4 (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  13. ↑ 13.0 13.1 13.2 SELECT (Trino 484). Trino Software Foundation.Retrieved 2026-11-23.
  14. ↑ Window Functions (PostgreSQL 19). PostgreSQL Global Development Group.Retrieved 2026-11-23.
  15. ↑ 15.0 15.1 Window functions (Trino 484). Trino Software Foundation.Retrieved 2026-11-23.
  16. ↑ LAG. Snowflake.Retrieved 2026-11-23.
  17. ↑ 17.0 17.1 SELECT (PostgreSQL 19). PostgreSQL Global Development Group.Retrieved 2026-11-23.
  18. ↑ QUALIFY. Snowflake.Retrieved 2026-11-23.