Skip to content

Read Primitives — QUALIFY NTILE

Read Primitives benchmark · all Read Primitives queries

Find orders in top quartile per segment using NTILE

Representative SQL

Rendered for DataFusion with default parameters.

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

Representative DataFrame

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

def qualify_ntile_expression_impl(ctx: DataFrameContext) -> Any:
n = 4
return (
_orders_customer_since_1995_expr(ctx)
.with_columns(
ctx.window_ntile(
n,
order_by=[("o_totalprice", True), ("o_orderkey", True)],
partition_by=["c_mktsegment"],
).alias("quartile")
)
.filter(ctx.col("quartile") == n)
.select("c_mktsegment", "o_orderkey", "o_totalprice", "quartile")
.sort(["c_mktsegment", "o_totalprice", "o_orderkey"], descending=[False, True, True])
)

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

For the DataFrame form:

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

Source

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