Exact Matching¶
ExactMatching (load("exact_matching"), compute_exact_matching) measures order-insensitive structural exact-match accuracy across complete SQL queries—overall and stratified across Yu et al. (2018)'s four Spider hardness buckets (easy, medium, hard, extra, plus all). It is built on the same dialect-general, bug-fixed sqlglot AST engine as ComponentMatching.
Reference: Tao Yu, Rui Zhang, Kai Yang, Michihiro Yasunaga, Dongxu Wang, Zifan Li, James Ma, Irene Li, Qingning Yao, Shanelle Roman, Zilin Zhang, and Dragomir R. Radev (2018). Spider: A Large-Scale Human-Labeled Dataset for Complex and Cross-Domain Semantic Parsing and Text-to-SQL Task. EMNLP 2018, pages 3911–3921. Reference implementation:
taoyds/spider(evaluation.py).
See Also: Shared sqlglot AST Engine Deep Dive
See Adapting Spider to sqlglot for the end-to-end 6-pass canonicalization pipeline, measured failures of taoyds/spider/process_sql.py and flat-dict lowering on bird_mini_dev, two-layer identifier normalization, and transitive union-find foreign_keys (kmap).
Exact Match Criterion¶
In score_pair (source), a prediction scores exact_match = 1 against a reference if and only if all of the following hold:
- Empty consistency:
pred.is_empty == ref.is_empty(unparseable empty fallback queries never falsely match real queries). - All 12 components match (
cs.f1 == 1): Every key inselect,select(no AGG),where,where(no OP),group(no Having),group,order,and/or,IUEN,keywords,cte, andjoin_onhas per-examplef1 == 1(inactive/both-empty components emitf1 = 1per example). FROMphysical & CTE tables match as a multiset:Counter(pred.from_tables) == Counter(ref.from_tables)(preserving duplicate counts on self-joins likeFROM emp e1 JOIN emp e2->("emp", "emp"), and alpha-renamed CTE placeholders like"__cte_0__").- Derived
FROMsubqueries match recursively:len(pred.from_subqueries) == len(ref.from_subqueries)and the multisets match order-insensitively under recursiveeval_exact_match == 1.
Dataset accuracy is then exact_match = exact_match_count / scored_count (or None when scored_count == 0).
How We Adapted Yu et al. (2018) to sqlglot: Choices & Bug Fixes¶
Exact-Match Bug Fixes vs. taoyds/spider/evaluation.py¶
- Python 3 crash (
TypeError) on>= 2FROMsubqueries (evaluation.py:387-388):- Upstream bug:
eval_exact_matchexecutedsorted(label['from']['table_units']). When a query joins two or more derived subqueries,table_unitscontains('sql', dict)tuples; Python 3 raisesTypeError: '<' not supported between instances of 'dict' and 'dict'. - Our fix (
score_pair): Normalizes derived tables deterministically viacanonical_signature(__subq_0__, ...) and comparesfrom_subqueriesusing order-insensitive recursive multiset matching (_multiset_match_count), never invoking<over Python dicts.
- Upstream bug:
- Skipped
FROMverification when gold has emptytable_units(evaluation.py:386):- Upstream bug:
if len(label['from']['table_units']) > 0:bypassed checkingpred['from']['table_units']whenever the reference query had noFROMclause (e.g.,SELECT 1), allowingSELECT 1 FROM usersto score1. - Our fix: Always checks
Counter(pred.from_tables) == Counter(ref.from_tables)andfrom_subqueriesequality unconditionally.
- Upstream bug:
- Hardness aggregate inflation (
evaluation.py:333, 338):- Upstream bug:
count_othersmiscountedNOTflags inWHEREand'and'/'or'strings inHAVINGas aggregate functions, shifting queries intomedium/hard/extraincorrectly. - Our fix: Counts genuine current-level
exp.AggFuncAST nodes before computing Spider's 4-level hardness bucket, keepingby_hardnessbreakdowns accurate.
- Upstream bug:
- In-place mutation corrupting repeated reference checks (
evaluation.py:123):- Upstream bug:
eval_seldeleted elements in place fromlabel['select'][1]. - Our fix: Immutable
NormalizedQuerydecompositions allow safe reuse across nested subqueries, CTEs,IUENbranches, and multi-reference groups.
- Upstream bug:
Architectural & Generalization Choices¶
- Why standalone
ExactMatchingalongsideComponentMatching? We rejected exposing a singleSpiderMatchevaluator or aninclude_components=Trueoverload onExactMatchingsogroup-by-vibesmaintains strict Hugging Faceevaluatesymmetry (one evaluator concept perload()name, non-overlapping output schemas). The deliberate trade-off is parsing twice if a caller runsComponentMatchingandExactMatchingin separate calls. - Wave-by-wave CTE alpha-renaming (
WITH a AS ...vsWITH b AS ...): Instead of comparing CTE aliases verbatim or inlining CTEs (which alters query structure and hardness),matching.pypartitions CTEs into Kahn topological waves, binds recursive self-references to__self__, preserves explicit column aliases (WITH c(x, y)), assigns canonical__cte_0__,__cte_1__, ... IDs sorted by(body.canonical_signature, col_aliases), and verifies both CTE definitions (cte) and main-query references. - Nested subquery canonicalization without string dependence: Nested
exp.Queryexpressions inWHERE,HAVING,SELECT, orJOIN ONare recursively decomposed and replaced by deterministic hashes__subq_<32-char sha256>__, makingWHERE a IN (SELECT x, y FROM t) AND b = 1matchWHERE b = 2 AND a IN (SELECT y, x FROM t)structurally. - Spider value-blind defaults vs. strict LLM evaluation: By default (
ignore_values=True,ignore_distinct=True,include_limit_value=False,include_join_conditions=False),ExactMatchingasks "did the model build the right structural SQL skeleton?". For production regression suites where constants ('active'vs'deleted'),DISTINCT,LIMIT 10, orJOIN ... ONkeys matter, enable the four strict flags—all five flags are echoed into everycompute()output dictionary so runs cannot be silently compared across mismatched configurations. - Unparseable predictions (
skip_unparseable_predictions=Falsevs.True): Defaulting toFalsefollows Spider by scoring unparseable predictions as0inscored_count(avoiding survivorship bias where a model parsing1/100queries scores100%exact match). SettingTruelets you isolate structural equivalence strictly on syntactically valid queries while still reportingparse_success_rate. - Multi-reference hardness-anchoring trade-off: When a row supplies multiple references (
[ref1, ref2]), the winning reference ((exact_match, matched_active_components, all_components_f1_sum, -ref_index)) supplies both the score and the row's hardness bucket. On heterogeneous multi-reference datasets where two acceptable references have different hardness levels,by_hardness[level]["count"]can vary slightly across models; single-reference benchmarks (Spider,bird_mini_dev) are unaffected. - Known static limitation: Like Spider itself,
ExactMatchingchecks canonical AST structure rather than algebraic/runtime equivalence (1.0 * a / bvs.CAST(a AS REAL) / b,IIF(c, x, y)vs.CASE WHEN c THEN x ELSE y END, orJOINvs.IN (SELECT ...)). PairExactMatchingwith Execution Accuracy (EX) when semantically rewritten plans are expected.
API Reference¶
Signatures¶
gbv.load("exact_matching", **kwargs) -> ExactMatching/ExactMatching(...)add(prediction, reference, schema=None, foreign_keys=None) -> Noneadd_batch(predictions, references, schemas=None, foreign_keys=None) -> Nonecompute_exact_matching(predictions, references, schemas=None, foreign_keys=None, *, ...)
| Parameter | Type | Default | Description |
|---|---|---|---|
dialect |
str |
"postgres" |
Target sqlglot dialect ("postgres", "sqlite", "mysql", "snowflake", "bigquery", etc.). |
ignore_values |
bool |
True |
Mask non-exempt literals to __val__ and COUNT(1) -> COUNT(*). Set False for strict literal matching. |
ignore_distinct |
bool |
True |
Strip DISTINCT and collapse UNION/INTERSECT/EXCEPT ALL distinctions. Set False for strict matching. |
include_limit_value |
bool |
False |
Require matching canonical LIMIT / OFFSET expressions rather than only presence. |
include_join_conditions |
bool |
False |
Require matching JOIN ... ON and USING(...) predicates across joins. |
skip_unparseable_predictions |
bool |
False |
Skip unparseable predictions (None in per_example) instead of counting them as 0. |
extractor |
ExtractorLike |
None |
Optional extractor run on predictions only (extract_fenced_or_last_sql, regex, callable). |
max_query_chars |
int \| None |
65_536 |
Character limit per SQL query before rejecting parse. |
max_input_chars |
int \| None |
131_072 |
Character limit on raw prediction input passed to extractor. |
include_per_example |
bool |
False |
Include sanitized per-example dicts in compute()["per_example"]. |
compute() Output Schema¶
| Key | Type | Semantics |
|---|---|---|
exact_match |
float \| None |
Overall accuracy exact_match_count / scored_count, or None when scored_count == 0. |
exact_match_count |
int |
Count of scored examples with exact_match == 1. |
by_hardness |
dict[str, dict[str, Any]] |
Keys "easy", "medium", "hard", "extra", "all"; each maps to {"count": int, "exact_match": float \| None, "exact_match_count": int}. |
scored_count |
int |
Examples contributing to exact_match. |
parsed_count |
int |
Predictions passing extraction, char limits, and query-AST parsing. |
unparseable_count |
int |
Predictions failing extraction, char limits, parsing, or query-AST gate. |
parse_success_rate |
float |
parsed_count / num_examples. |
reference_parse_success_rate |
float |
Parsed references divided by total supplied reference strings. |
schema_applied_count |
int |
Scored examples evaluated with a non-None schema. |
foreign_keys_applied_count |
int |
Scored examples evaluated with non-empty foreign_keys. |
num_examples |
int |
Total rows processed. |
dialect |
str |
Normalized sqlglot dialect name. |
ignore_values |
bool |
Recorded mode flag value. |
ignore_distinct |
bool |
Recorded mode flag value. |
include_limit_value |
bool |
Recorded mode flag value. |
include_join_conditions |
bool |
Recorded mode flag value. |
skip_unparseable_predictions |
bool |
Recorded mode flag value. |
per_example |
list[dict[str, Any] \| None] |
(Only when include_per_example=True) None if skipped; else {"exact_match": int, "hardness": str, "prediction_parsed": bool, "schema_applied": bool, "foreign_keys_applied": bool} (raw SQL and literals omitted). |
Examples¶
Reordered CTEs, Multiple FROM Subqueries, and Hardness Buckets¶
import group_by_vibes as gbv
pred_cte = (
"WITH active_users(uid, name) AS (SELECT id, name FROM users WHERE active = 1) "
"SELECT name, uid FROM active_users"
)
ref_cte = (
"WITH renamed(uid, name) AS (SELECT u.id, u.name FROM users u WHERE 99 = u.active) "
"SELECT uid, name FROM renamed"
)
pred_subq = (
"SELECT a.x, b.y FROM (SELECT id AS x FROM t1) a "
"JOIN (SELECT id AS y FROM t2) b ON a.x = b.y"
)
ref_subq = (
"SELECT b.y, a.x FROM (SELECT id AS y FROM t2) b "
"JOIN (SELECT id AS x FROM t1) a ON b.y = a.x"
)
res = gbv.compute_exact_matching(
[pred_cte, pred_subq],
[ref_cte, ref_subq],
schemas=[{"users": ["id", "name", "active"]}, {"t1": ["id"], "t2": ["id"]}],
dialect="postgres",
include_per_example=True,
)
assert res["exact_match"] == 1.0
assert res["exact_match_count"] == 2
assert res["by_hardness"]["extra"]["count"] == 2