Skip to content

CoffeeShop — Day-Part Cadence

CoffeeShop benchmark · all CoffeeShop queries

Order cadence by day-part (Morning/Midday/Afternoon/Evening) for a region

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
CASE
WHEN EXTRACT(HOUR FROM ol.order_time) BETWEEN 5 AND 10
THEN 'Morning'
WHEN EXTRACT(HOUR FROM ol.order_time) BETWEEN 11 AND 14
THEN 'Midday'
WHEN EXTRACT(HOUR FROM ol.order_time) BETWEEN 15 AND 17
THEN 'Afternoon'
ELSE 'Evening'
END AS day_part,
COUNT(DISTINCT ol.order_id) AS orders,
SUM(ol.total_price) AS revenue,
AVG(ol.total_price) AS avg_line_value
FROM order_lines AS ol
JOIN dim_locations AS dl
ON ol.location_record_id = dl.record_id
WHERE
dl.region = 'South'
AND ol.order_date BETWEEN CAST('2023-01-01' AS DATE) AND CAST('2024-12-31' AS DATE)
GROUP BY
day_part
ORDER BY
day_part

Representative DataFrame

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

def tm1_expression_impl(ctx: DataFrameContext) -> Any:
params = get_parameters("TM1")
region = params.get("region", "South")
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
joined = (
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")
.filter(col("region") == lit(region))
)
if _is_temporal_expression_column(joined, "order_time"):
hour_expr = col("order_time").dt.hour()
else:
hour_expr = col("order_time").str.slice(0, 2).cast_int()
with_hour = joined.with_columns(hour_expr.alias("hour"))
with_part = with_hour.with_columns(
ctx.when((col("hour") >= lit(5)) & (col("hour") <= lit(10)))
.then(lit("Morning"))
.otherwise(
ctx.when((col("hour") >= lit(11)) & (col("hour") <= lit(14)))
.then(lit("Midday"))
.otherwise(
ctx.when((col("hour") >= lit(15)) & (col("hour") <= lit(17)))
.then(lit("Afternoon"))
.otherwise(lit("Evening"))
)
)
.alias("day_part")
)
return (
with_part.group_by("day_part")
.agg(
col("order_id").n_unique().alias("orders"),
col("total_price").sum().alias("revenue"),
col("total_price").mean().alias("avg_line_value"),
)
.sort("day_part")
)

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

For the DataFrame form:

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

Source

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