Skip to content

TPC-DS-OBT Q9 — obt_sales_by_channel_return_flag

TPC-DS-OBT benchmark · all TPC-DS-OBT queries

Sales counts by channel and return flag

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
CASE
WHEN (
SELECT
COUNT(*)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 1 AND 20 AND channel = 'store'
) > 1
THEN (
SELECT
AVG(ext_discount_amt)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 1 AND 20 AND channel = 'store'
)
ELSE (
SELECT
AVG(net_paid)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 1 AND 20 AND channel = 'store'
)
END AS bucket1,
CASE
WHEN (
SELECT
COUNT(*)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 21 AND 40 AND channel = 'store'
) > 1
THEN (
SELECT
AVG(ext_discount_amt)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 21 AND 40 AND channel = 'store'
)
ELSE (
SELECT
AVG(net_paid)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 21 AND 40 AND channel = 'store'
)
END AS bucket2,
CASE
WHEN (
SELECT
COUNT(*)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 41 AND 60 AND channel = 'store'
) > 1
THEN (
SELECT
AVG(ext_discount_amt)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 41 AND 60 AND channel = 'store'
)
ELSE (
SELECT
AVG(net_paid)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 41 AND 60 AND channel = 'store'
)
END AS bucket3,
CASE
WHEN (
SELECT
COUNT(*)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 61 AND 80 AND channel = 'store'
) > 1
THEN (
SELECT
AVG(ext_discount_amt)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 61 AND 80 AND channel = 'store'
)
ELSE (
SELECT
AVG(net_paid)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 61 AND 80 AND channel = 'store'
)
END AS bucket4,
CASE
WHEN (
SELECT
COUNT(*)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 81 AND 100 AND channel = 'store'
) > 1
THEN (
SELECT
AVG(ext_discount_amt)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 81 AND 100 AND channel = 'store'
)
ELSE (
SELECT
AVG(net_paid)
FROM tpcds_sales_returns_obt AS obt
WHERE
obt.quantity BETWEEN 81 AND 100 AND channel = 'store'
)
END AS bucket5
FROM tpcds_sales_returns_obt AS obt
WHERE
reason_r_reason_sk = 1 AND channel = 'store'

Representative DataFrame

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

def impl(ctx: DataFrameContext) -> Any:
table = ctx.get_table(OBT_TABLE)
for column, op, value in spec.filters:
table = table.filter(_FILTER_OPS[op](ctx.col(column), ctx.lit(value)))
aggregates = [getattr(ctx.col(column), func)().alias(alias) for alias, column, func in spec.aggregations]
if spec.group_keys:
result = table.group_by(*spec.group_keys).agg(*aggregates)
else:
result = table.select(*aggregates)
if spec.sort_keys:
descending = list(spec.descending) if spec.descending else [False] * len(spec.sort_keys)
result = result.sort(list(spec.sort_keys), descending=descending)
if spec.limit:
result = result.limit(spec.limit)
return 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 TPCDSOBT
bench = TPCDSOBT(scale_factor=1.0) # your scale factor
queries = bench.get_queries(dialect="datafusion") # your platform's dialect; drop dialect= for native SQL
print(queries['9'])

For the DataFrame form:

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

Source

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