Skip to content

Read Primitives — QUALIFY ROW_NUMBER

Read Primitives benchmark · all Read Primitives queries

Find top N orders per customer using ROW_NUMBER

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
c_custkey,
c_name,
o_orderkey,
o_orderdate,
o_totalprice,
order_rank
FROM (
SELECT
c_custkey,
c_name,
o_orderkey,
o_orderdate,
o_totalprice,
ROW_NUMBER() OVER (PARTITION BY c_custkey ORDER BY o_totalprice DESC) AS order_rank,
ROW_NUMBER() OVER (PARTITION BY c_custkey ORDER BY o_totalprice DESC) 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,
order_rank
) AS _t
WHERE
_w <= 3

Representative DataFrame

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

def qualify_row_number_expression_impl(ctx: DataFrameContext) -> Any:
result = _orders_customer_since_1995_expr(ctx).with_columns(
ctx.window_row_number(order_by=[("o_totalprice", False)], partition_by=["c_custkey"]).alias("order_rank")
)
return (
result.filter(ctx.col("order_rank") <= ctx.lit(3))
.select("c_custkey", "c_name", "o_orderkey", "o_orderdate", "o_totalprice", "order_rank")
.sort("c_custkey", "order_rank")
)

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_row_number'])

For the DataFrame form:

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

Source

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