Skip to content

TPC-DS Q6 — Customer State/Item Analysis

TPC-DS benchmark · all TPC-DS queries

Finds items where customers are concentrated by state

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
a.ca_state AS state,
COUNT(*) AS cnt
FROM customer_address AS a, customer AS c, store_sales AS s, date_dim AS d, item AS i
WHERE
a.ca_address_sk = c.c_current_addr_sk
AND c.c_customer_sk = s.ss_customer_sk
AND s.ss_sold_date_sk = d.d_date_sk
AND s.ss_item_sk = i.i_item_sk
AND d.d_month_seq = (
SELECT DISTINCT
(
d_month_seq
)
FROM date_dim
WHERE
d_year = 2000 AND d_moy = 2
)
AND i.i_current_price > 1.2 * (
SELECT
AVG(j.i_current_price)
FROM item AS j
WHERE
j.i_category = i.i_category
)
GROUP BY
a.ca_state
HAVING
COUNT(*) >= 10
ORDER BY
cnt,
a.ca_state
LIMIT 100

Representative DataFrame

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

def q6_expression_impl(ctx: DataFrameContext) -> Any:
params = get_parameters(6)
year = params.get("year", 2001)
month = params.get("month", 1)
customer_address, customer, store_sales, date_dim, item = _tables(
ctx, "customer_address", "customer", "store_sales", "date_dim", "item"
)
col = ctx.col
lit = ctx.lit
category_avg = item.group_by("i_category").agg(col("i_current_price").mean().alias("category_avg_price"))
item_with_avg = item.join(category_avg, on="i_category")
return _sort_null_largest_expression(
ctx,
customer_address.join(customer, left_on="ca_address_sk", right_on="c_current_addr_sk")
.join(store_sales, left_on="c_customer_sk", right_on="ss_customer_sk")
.join(date_dim, left_on="ss_sold_date_sk", right_on="d_date_sk")
.join(item_with_avg, left_on="ss_item_sk", right_on="i_item_sk")
.filter(
(col("d_year") == lit(year))
& (col("d_moy") == lit(month))
& (col("i_current_price") > lit(1.2) * col("category_avg_price"))
)
.group_by("ca_state")
.agg(col("ss_sold_date_sk").count().alias("cnt"))
.filter(col("cnt") >= lit(10))
.select(col("ca_state").alias("state"), col("cnt")),
["cnt", "state"],
).limit(100)

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 TPCDS
bench = TPCDS(scale_factor=1.0) # your scale factor
queries = bench.get_queries(dialect="datafusion") # your platform's dialect; drop dialect= for native SQL
print(queries['6'])

Parameter substitution for this benchmark is seed-driven; pass a seed for a specific stream’s parameters:

print(bench.get_query(6, scale_factor=1.0, seed=42, dialect="datafusion"))

For the DataFrame form:

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

Source

Query text: _sources/tpc-ds/query_templates/query6.tpl