AMPLab Q4 — Country/Language Analytics
AMPLab benchmark · all AMPLab queries
Country and language analytics with HAVING and COUNT DISTINCT
Representative SQL
Rendered for DataFusion with default parameters.
SELECT countryCode, languageCode, COUNT(*) AS visit_count, SUM(adRevenue) AS total_revenue, AVG(duration) AS avg_duration, COUNT(DISTINCT sourceIP) AS unique_visitorsFROM uservisitsWHERE visitDate >= '2000-01-01' AND adRevenue > 1.0GROUP BY countryCode, languageCodeHAVING COUNT(*) > 10ORDER BY total_revenue DESCLIMIT 100Representative DataFrame
Expression family (Polars, PySpark, DataFusion), default parameters.
def q4_expression_impl(ctx: DataFrameContext) -> Any: params = get_parameters("Q4") start_date = params.get("start_date", date(2000, 1, 1)) min_revenue = params.get("min_revenue", 1.0) min_visits = params.get("min_visits", 10) limit_rows = params.get("limit_rows", 100)
uservisits = ctx.get_table("uservisits") col = ctx.col lit = ctx.lit
return ( uservisits.filter((col("visitDate").cast_date() >= lit(start_date)) & (col("adRevenue") > lit(min_revenue))) .group_by("countryCode", "languageCode") .agg( col("countryCode").count().alias("visit_count"), col("adRevenue").sum().alias("total_revenue"), col("duration").mean().alias("avg_duration"), col("sourceIP").n_unique().alias("unique_visitors"), ) .filter(col("visit_count") > lit(min_visits)) .sort("total_revenue", descending=True) .limit(limit_rows) )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 AMPLab
bench = AMPLab(scale_factor=0.01) # your scale factorqueries = bench.get_queries(dialect="datafusion") # your platform's dialect; drop dialect= for native SQLprint(queries['4'])For the DataFrame form:
from benchbox.core.query_catalog import get_dataframe_render
print(get_dataframe_render('amplab', '4', family="expression").source) # or family="pandas"Source
Query text: benchbox/core/amplab/queries.py