Skip to content

Read Primitives — QUALIFY DENSE_RANK

Read Primitives benchmark · all Read Primitives queries

Find top N parts by price per category using DENSE_RANK

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
p_type,
p_name,
p_retailprice,
price_rank
FROM (
SELECT
p_type,
p_name,
p_retailprice,
DENSE_RANK() OVER (PARTITION BY p_type ORDER BY p_retailprice DESC) AS price_rank,
DENSE_RANK() OVER (PARTITION BY p_type ORDER BY p_retailprice DESC) AS _w
FROM part
ORDER BY
p_type,
price_rank,
p_name
) AS _t
WHERE
_w <= 2

Representative DataFrame

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

def qualify_dense_rank_expression_impl(ctx: DataFrameContext) -> Any:
result = ctx.get_table("part").with_columns(
ctx.window_dense_rank(order_by=[("p_retailprice", False)], partition_by=["p_type"]).alias("price_rank")
)
return (
result.filter(ctx.col("price_rank") <= ctx.lit(2))
.select("p_type", "p_name", "p_retailprice", "price_rank")
.sort(["p_type", "price_rank", "p_name"])
)

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

For the DataFrame form:

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

Source

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