"""Compute the answer key from the Parquet files and write questions.jsonl, answer_key.csv and answer_key.md. python -m answer_key [--parquet data/parquet] """ from __future__ import annotations import argparse import csv import json import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) from answer_key.questions import DEFINITIONS, QUESTIONS, render # noqa: E402 from verify.engines import DuckDB, stated, value_of # noqa: E402 def definition(measures: list[str], question: str) -> str: parts = [f"{m}: {DEFINITIONS[m]}" for m in measures if m in DEFINITIONS] if "Guest orders have no customer" in question: parts.append(f"Geography slices: {DEFINITIONS['Geography slices']}") return " ".join(parts) def build(parquet: Path) -> list[dict]: db = DuckDB(parquet) out = [] for q in QUESTIONS: sql_ch, sql_db = render(q.sql, "clickhouse"), render(q.sql, "duckdb") expected = stated(value_of(db.row(sql_db), q.value_type), q.value_type) out.append({"id": q.id, "question": q.question, "measures": q.measures, "definition": definition(q.measures, q.question), "slice": q.slice, "sql_clickhouse": sql_ch, "sql_duckdb": sql_db, "expected": expected, "value_type": q.value_type, "tolerance": q.tolerance, "difficulty": q.difficulty, "traps": q.traps, "pattern": q.pattern}) return out def _shown(e) -> str: return f"{e['member']}; {e['value']}" if isinstance(e, dict) else str(e) def write(records: list[dict], out: Path) -> None: with (out / "questions.jsonl").open("w", encoding="utf-8") as fh: for r in records: fh.write(json.dumps(r, ensure_ascii=False) + "\n") with (out / "answer_key.csv").open("w", newline="", encoding="utf-8") as fh: w = csv.writer(fh) w.writerow(["id", "question", "expected", "value_type", "tolerance", "measures", "definition", "difficulty", "pattern"]) for r in records: w.writerow([r["id"], r["question"], _shown(r["expected"]), r["value_type"], "" if r["tolerance"] is None else r["tolerance"], "; ".join(r["measures"]), r["definition"], r["difficulty"], r["pattern"] or ""]) lines = ["# Answer key", "", "Each question has one expected value, computed from the data and checked on ClickHouse 24.3 and DuckDB. Money is exact to the " "cent. Averages of money (AOV) are given to the cent and graded within ±0.01. Ratios are given to six decimals and graded " "within ±0.0001 absolute. Members are exact names. The SQL for every question is in `questions.jsonl`.", "", "All data is as of 2026-06-30. Amounts are in USD.", "", "## Measure definitions", "", "| Measure | Definition |", "|---|---|"] lines += [f"| {k} | {v} |" for k, v in DEFINITIONS.items()] lines += ["", "## Questions", "", "| ID | Question | Expected | Type | Measures |", "|---|---|---|---|---|"] for r in records: q = r["question"].replace("|", "\\|") lines.append(f"| {r['id']} | {q} | `{_shown(r['expected'])}` | {r['value_type']} | {', '.join(r['measures'])} |") (out / "answer_key.md").write_text("\n".join(lines) + "\n", encoding="utf-8") def main() -> None: ap = argparse.ArgumentParser(prog="python -m answer_key") ap.add_argument("--parquet", type=Path, default=ROOT / "data" / "parquet") ap.add_argument("--out", type=Path, default=ROOT / "answer_key") a = ap.parse_args() records = build(a.parquet) write(records, a.out) for r in records: print(f"{r['id']} {_shown(r['expected'])}") if __name__ == "__main__": main()