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_valueFROM order_lines AS olJOIN dim_locations AS dl ON ol.location_record_id = dl.record_idWHERE ol.order_date BETWEEN CAST('2023-01-01' AS DATE) AND CAST('2023-01-31' AS DATE)GROUP BY ol.order_date, dl.regionORDER BY ol.order_date, dl.regionRepresentative 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 factorqueries = bench.get_queries(dialect="datafusion") # your platform's dialect; drop dialect= for native SQLprint(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