Skip to content

CoffeeShop — Revenue Share by Region

CoffeeShop benchmark · all CoffeeShop queries

Revenue share by region using SUM OVER() window function

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
dl.region,
COUNT(DISTINCT ol.order_id) AS orders,
SUM(ol.total_price) AS revenue,
SUM(ol.total_price) / SUM(SUM(ol.total_price)) OVER () AS revenue_share
FROM order_lines AS ol
JOIN dim_locations AS dl
ON ol.location_record_id = dl.record_id
WHERE
ol.order_date BETWEEN CAST('2023-01-01' AS DATE) AND CAST('2024-12-31' AS DATE)
GROUP BY
dl.region
ORDER BY
revenue DESC

Representative DataFrame

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

def sa4_expression_impl(ctx: DataFrameContext) -> Any:
params = get_parameters("SA4")
start_date = params.get("start_date", "2023-01-01")
end_date = params.get("end_date", "2024-12-31")
ol = ctx.get_table("order_lines")
dl = ctx.get_table("dim_locations")
col = ctx.col
lit = ctx.lit
grouped = (
ol.filter(
(col("order_date") >= lit(_parse_date(start_date))) & (col("order_date") <= lit(_parse_date(end_date)))
)
.join(dl, left_on="location_record_id", right_on="record_id")
.group_by("region")
.agg(
col("order_id").n_unique().alias("orders"),
col("total_price").sum().alias("revenue"),
)
)
total_val = grouped.select(col("revenue").sum().alias("total")).scalar()
return grouped.with_columns(
ctx.when(lit(total_val) != lit(0))
.then(col("revenue") / lit(total_val))
.otherwise(lit(0.0))
.alias("revenue_share")
).sort("revenue", descending=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 CoffeeShop
bench = CoffeeShop(scale_factor=0.001) # your scale factor
queries = bench.get_queries(dialect="datafusion") # your platform's dialect; drop dialect= for native SQL
print(queries['SA4'])

For the DataFrame form:

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

Source

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