Skip to content

Read Primitives — QUALIFY LAG/LEAD

Read Primitives benchmark · all Read Primitives queries

Find orders with increasing price using LAG

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
c_custkey,
c_name,
o_orderkey,
o_orderdate,
o_totalprice,
prev_order_price
FROM (
SELECT
c_custkey,
c_name,
o_orderkey,
o_orderdate,
o_totalprice,
LAG(o_totalprice) OVER (PARTITION BY c_custkey ORDER BY o_orderdate, o_orderkey) AS prev_order_price,
LAG(o_totalprice) OVER (PARTITION BY c_custkey ORDER BY o_orderdate, o_orderkey) AS _w
FROM customer AS c
JOIN orders AS o
ON c.c_custkey = o.o_custkey
WHERE
o_orderdate >= CAST('1995-01-01' AS DATE)
ORDER BY
c_custkey,
o_orderdate,
o_orderkey
) AS _t
WHERE
o_totalprice > _w

Representative DataFrame

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

def qualify_lag_lead_expression_impl(ctx: DataFrameContext) -> Any:
return (
_orders_customer_since_1995_expr(ctx)
.with_columns(
ctx.window_lag(
"o_totalprice",
1,
partition_by=["c_custkey"],
order_by=[("o_orderdate", True), ("o_orderkey", True)],
).alias("prev_order_price")
)
.filter(ctx.col("o_totalprice") > ctx.col("prev_order_price"))
.select("c_custkey", "c_name", "o_orderkey", "o_orderdate", "o_totalprice", "prev_order_price")
.sort(["c_custkey", "o_orderdate", "o_orderkey"])
)

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['qualify_lag_lead'])

For the DataFrame form:

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

Source

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