Read Primitives — MAX_BY Complex
Read Primitives benchmark · all Read Primitives queries
Find the most expensive order for each customer segment
Representative SQL
Rendered for DataFusion with default parameters.
WITH ranked AS ( SELECT c.c_mktsegment, o.o_orderkey, o.o_orderdate, o.o_totalprice, ROW_NUMBER() OVER (PARTITION BY c.c_mktsegment ORDER BY o.o_totalprice DESC, o.o_orderkey) AS rn FROM orders AS o JOIN customer AS c ON o.o_custkey = c.c_custkey)SELECT c_mktsegment, o_orderkey AS most_expensive_order_id, o_orderdate AS most_expensive_order_date, o_totalprice AS max_order_valueFROM rankedWHERE rn = 1ORDER BY max_order_value DESC, c_mktsegmentRepresentative DataFrame
Expression family (Polars, PySpark, DataFusion), default parameters.
def expression_impl(ctx: DataFrameContext) -> Any: table = _extreme_source(ctx, base, joins) extrema = table.group_by(*groups).agg(getattr(ctx.col(value), agg)().alias(alias)) result = table.join(extrema, on=group_key).filter(ctx.col(value) == ctx.col(alias)).select(*expr_select) result = result.unique(subset=expr_unique[0] if len(expr_unique) == 1 else list(expr_unique)).sort( alias, descending=desc ) return result.limit(limit) if limit is not None else resultGet 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 factorqueries = bench.get_queries(dialect="datafusion") # your platform's dialect; drop dialect= for native SQLprint(queries['max_by_complex'])For the DataFrame form:
from benchbox.core.query_catalog import get_dataframe_render
print(get_dataframe_render('read_primitives', 'max_by_complex', family="expression").source) # or family="pandas"Source
Query text: benchbox/core/read_primitives/queries.py