Skip to content

CoffeeShop — Daily Revenue by Region

CoffeeShop benchmark · all CoffeeShop queries

Daily revenue and order volume by region with NULLIF avg order value

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
ol.order_date,
dl.region,
COUNT(DISTINCT ol.order_id) AS order_count,
SUM(ol.total_price) AS gross_revenue,
SUM(ol.total_price) / NULLIF(COUNT(DISTINCT ol.order_id), 0) AS avg_order_value
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('2023-01-31' AS DATE)
GROUP BY
ol.order_date,
dl.region
ORDER BY
ol.order_date,
dl.region

Representative DataFrame

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

def sa1_expression_impl(ctx: DataFrameContext) -> Any:
params = get_parameters("SA1")
start_date = params.get("start_date", "2023-01-01")
end_date = params.get("end_date", "2023-01-31")
ol = ctx.get_table("order_lines")
dl = ctx.get_table("dim_locations")
col = ctx.col
lit = ctx.lit
return (
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("order_date", "region")
.agg(
col("order_id").n_unique().alias("order_count"),
col("total_price").sum().alias("gross_revenue"),
)
.with_columns(
ctx.when(col("order_count") != lit(0))
.then(col("gross_revenue") / col("order_count"))
.otherwise(lit(None))
.alias("avg_order_value")
)
.sort(["order_date", "region"])
)

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

For the DataFrame form:

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

Source

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