Skip to content

Read Primitives — MAX_BY Simple

Read Primitives benchmark · all Read Primitives queries

Find the customer with the highest account balance in each nation

Representative SQL

Rendered for DataFusion with default parameters.

WITH ranked AS (
SELECT
n.n_name,
c.c_name,
c.c_acctbal,
ROW_NUMBER() OVER (PARTITION BY n.n_name ORDER BY c.c_acctbal DESC, c.c_name) AS rn
FROM customer AS c
JOIN nation AS n
ON c.c_nationkey = n.n_nationkey
)
SELECT
n_name,
c_name AS richest_customer,
c_acctbal AS max_balance
FROM ranked
WHERE
rn = 1
ORDER BY
max_balance DESC,
n_name

Representative 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 result

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

For the DataFrame form:

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

Source

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