Skip to content

TPC-H Skew Q22 — Global Sales Opportunity

TPC-H Skew benchmark · all TPC-H Skew queries

Identify customers likely to make purchases

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
cntrycode,
COUNT(*) AS numcust,
SUM(c_acctbal) AS totacctbal
FROM (
SELECT
SUBSTRING(c_phone FROM 1 FOR 2) AS cntrycode,
c_acctbal
FROM customer
WHERE
SUBSTRING(c_phone FROM 1 FOR 2) IN ('13', '31', '23', '29', '30', '18', '17')
AND c_acctbal > (
SELECT
AVG(c_acctbal)
FROM customer
WHERE
c_acctbal > 0.00
AND SUBSTRING(c_phone FROM 1 FOR 2) IN ('13', '31', '23', '29', '30', '18', '17')
)
AND NOT EXISTS(
SELECT
*
FROM orders
WHERE
o_custkey = c_custkey
)
) AS custsale
GROUP BY
cntrycode
ORDER BY
cntrycode

Representative DataFrame

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

def q22_expression_impl(ctx: DataFrameContext) -> Any:
customer = ctx.get_table("customer")
orders = ctx.get_table("orders")
col = ctx.col
lit = ctx.lit
params = get_tpch_parameters(22)
country_codes = params["country_codes"]
customer_with_code = customer.with_columns(col("c_phone").str.slice(0, 2).alias("cntrycode"))
avg_balance = ctx.scalar(
customer_with_code.filter((col("c_acctbal") > lit(0)) & col("cntrycode").is_in(country_codes)).select(
col("c_acctbal").mean().alias("avg_bal")
)
)
customers_with_orders = orders.select("o_custkey").unique()
return (
customer_with_code.filter(col("cntrycode").is_in(country_codes) & (col("c_acctbal") > lit(avg_balance)))
.join(customers_with_orders, left_on="c_custkey", right_on="o_custkey", how="anti")
.group_by("cntrycode")
.agg(col("c_custkey").count().alias("numcust"), col("c_acctbal").sum().alias("totacctbal"))
.sort("cntrycode")
)

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

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

print(bench.get_query(22, scale_factor=0.01, seed=42, dialect="datafusion"))

For the DataFrame form:

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

Source

Query text: benchbox/_binaries/tpc-h/templates/queries/22.sql