Skip to content

Read Primitives — Window Lead/Lag

Read Primitives benchmark · all Read Primitives queries

Offset window functions over the same frame

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
o_orderkey,
o_orderdate,
o_totalprice,
LAG(o_totalprice, 1) OVER (PARTITION BY o_custkey ORDER BY o_orderdate, o_orderkey) AS prev_order_price,
LEAD(o_totalprice, 1) OVER (PARTITION BY o_custkey ORDER BY o_orderdate, o_orderkey) AS next_order_price
FROM orders
WHERE
o_orderdate >= CAST('1995-01-01' AS DATE)
AND o_orderdate < CAST('1996-01-01' AS DATE)

Representative DataFrame

Expression family (Polars, PySpark, DataFusion), default parameters.

def window_lead_lag_expression_impl(ctx: DataFrameContext) -> Any:
order_by = [("o_orderdate", True), ("o_orderkey", True)]
return (
ctx.get_table("orders")
.filter(
(ctx.col("o_orderdate") >= ctx.lit(date(1995, 1, 1))) & (ctx.col("o_orderdate") < ctx.lit(date(1996, 1, 1)))
)
.with_columns(
ctx.window_lag("o_totalprice", 1, partition_by=["o_custkey"], order_by=order_by).alias("prev_order_price"),
ctx.window_lead("o_totalprice", 1, partition_by=["o_custkey"], order_by=order_by).alias("next_order_price"),
)
.sort(["o_custkey", "o_orderdate", "o_orderkey"])
.select("o_orderkey", "o_orderdate", "o_totalprice", "prev_order_price", "next_order_price")
)

Get the exact query for your run

The blocks above are representative. To get the precise statement a specific platform runs at a specific scale, substitute your own values:

from benchbox import ReadPrimitives
bench = ReadPrimitives(scale_factor=0.01) # your scale factor
queries = bench.get_queries(dialect="datafusion") # your platform's dialect; drop dialect= for native SQL
print(queries['window_lead_lag_same_frame'])

For the DataFrame form:

from benchbox.core.query_catalog import get_dataframe_render
print(get_dataframe_render('read_primitives', 'window_lead_lag_same_frame', family="expression").source) # or family="pandas"

Source

Query text: benchbox/core/read_primitives/queries.py