Component Matching¶
ComponentMatching (load("component_matching"), compute_component_matching) evaluates Text2SQL predictions at the SQL clause level by reporting exact-set Precision, Recall, and F1 across 12 SQL components and 4 Spider hardness levels (easy, medium, hard, extra, plus overall all). It is a dialect-general, bug-fixed sqlglot AST adaptation of Yu et al. (EMNLP 2018).
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,process_sql.py).
See Also: Full AST Engine Deep Dive
ComponentMatching and ExactMatching share one sqlglot AST decomposition engine (matching.py). For the 6-pass AST pipeline diagram, why Spider's process_sql.py + nltk and flat-dict lowering fail on modern SQL, two-layer identifier normalization, and foreign_keys union-find equivalence, read Adapting Spider to sqlglot.
Mathematical Definition & Why F1¶
1. Per-Example Exact-Set Rule (0 or 1)¶
For each SQL component \(c\), both the predicted query and golden reference are decomposed into order-independent multisets (or ordered tuples for order) of canonicalized sub-components with sizes \(n^{\text{pred}}_{i,c}\) (pred_total), \(n^{\text{label}}_{i,c}\) (label_total), and matched element count \(n^{\text{overlap}}_{i,c}\) (overlap). On example \(i\), component \(c\) scores binary 1 or 0—there is no partial credit within a single clause:
Per example, if \(m_{i,c} = 1\) (including empty-vs-empty \(n^{\text{pred}}_{i,c} = n^{\text{label}}_{i,c} = 0\)), the per-example ComponentScore sets precision = recall = f1 = 1; otherwise precision = recall = f1 = 0.
2. Corpus Precision, Recall, and F1¶
Across the dataset (and inside each hardness bucket), rows only enter a denominator when that side actually contains component \(c\):
- Rows where neither prediction nor reference contains component \(c\) (
pred_total == 0andlabel_total == 0) are excluded from bothpred_countandlabel_count. - If
pred_count == 0andlabel_count == 0for a component in a bucket,precision,recall, andf1returnNone. - If
pred_count > 0orlabel_count > 0andprecision + recall == 0.0,f1returns0.0(fixing an upstream Spider bug—see below).
3. Why F1 Instead of Plain Accuracy?¶
Most SQL clauses (WHERE, GROUP BY, HAVING, ORDER BY, IUEN, WITH CTEs, and/or) are optional and sparse across a benchmark:
- Plain accuracy is dominated by true negatives: If
GROUP BYappears in 40 of 500 queries, a trivial model that never emitsGROUP BYagrees on all 460 empty cases and would get92.0%accuracy while having0%GROUP BYcapability. - Precision alone rewards under-predicting: Emitting
ORDER BYon 1 trivial query and omitting it everywhere else achievesPrecision = 1.0. - Recall alone hides hallucinated clauses: Counting only
gold > 0ignores every spuriousWHEREorGROUP BYhallucinated on simple queries. - F1 balances omission and hallucination: Excluding empty-vs-empty queries removes true-negative inflation, and the harmonic mean requires both high precision (clauses emitted match gold) and high recall (gold clauses are not dropped).
- (Special case:) Because every valid SQL query has a
SELECTclause, when every prediction parses (unparseable_count == 0),pred_count == label_count == N, so forselectandselect(no AGG)specifically, \(P = R = F_1\) equals exact-set clause accuracy.
The 12 Evaluated Components¶
Under Spider defaults (ignore_values=True, ignore_distinct=True, include_limit_value=False, include_join_conditions=False), components 1–10 align with taoyds/spider/evaluation.py, while 11 (cte) and 12 (join_on, opt-in) extend coverage to modern SQL:
| # | Component Key | What Is Extracted & Compared |
|---|---|---|
| 1 | select |
Multiset of canonicalized SELECT projection expressions (AS output aliases stripped; includes outer aggregates; includes DISTINCT / DISTINCT ON when ignore_distinct=False). |
| 2 | select(no AGG) |
Multiset of SELECT expressions with the outermost aggregate wrapper (MAX, MIN, COUNT, SUM, AVG) unwrapped; non-aggregate projections remain unchanged. |
| 3 | where |
Multiset of flattened top-level WHERE predicate atoms (splitting AND/OR chains while keeping compound NOT (a AND b) intact; respects ignore_values and recursive __subq_<sig>__ hashing). |
| 4 | where(no OP) |
Multiset of target expressions extracted from each WHERE atom (unwrapping outer NOT/parens): non-literal LHS for binary comparisons (=, <>, <, <=, >, >=, LIKE, ILIKE, IS), BETWEEN, and IN; "exists" for EXISTS (...); canonicalized expression otherwise. |
| 5 | group(no Having) |
Multiset of GROUP BY expressions with leading table/schema/catalog qualifiers stripped on bare columns (matching Spider eval_group). |
| 6 | group |
Presence unit (1 if GROUP BY or HAVING is non-empty, else 0); matches (1) iff group_by_full multiset (with normalized table qualifiers & FK collapse) AND having_preds multiset AND having_and_or connector set all match. |
| 7 | order |
Presence unit (1 if ORDER BY or LIMIT/OFFSET/FETCH is present, else 0); matches (1) iff ordered (expr, "asc"\|"desc", "first"\|"last"\|"default") list (defaulting omitted direction to "asc") matches AND has_limit matches (plus canonical LIMIT/OFFSET text when include_limit_value=True). |
| 8 | and/or |
Set of boolean connectors ("and", "or") in WHERE. When both sets are empty (frozenset()), emits pred_total = 0, label_total = 0 with per-example f1 = 1 so corpus denominators are never inflated. |
| 9 | IUEN |
Set-operation branches (union, intersect, except, or *_all when ignore_distinct=False) decomposed recursively via left-associative chain flattening; matched as an order-insensitive multiset via recursive exact match. |
| 10 | keywords |
Set of structural SQL keywords at the current query level only (no recursion into subqueries/CTEs/IUEN): where, group, having, order, asc, desc, limit, intersect, union, except, with, plus or, not (NOT, NOT IN, NOT LIKE, IS NOT), in, like (LIKE/ILIKE) inside current-level JOIN ON/WHERE/HAVING. |
| 11 | cte |
Multiset of (column_aliases, body_nq) pairs from WITH clauses after wave-by-wave topological alpha-renaming (__cte_0__, ...), matched recursively via exact match. |
| 12 | join_on |
When include_join_conditions=True, multiset of canonicalized JOIN ... ON atoms and USING(col) tokens at the current query level. When False (default), inactive (pred_count = label_count = 0). |
Spider Hardness Stratification (by_hardness)¶
Following Yu et al. (2018), every scored example is classified by its reference query into easy, medium, hard, or extra (plus the corpus summary all) using three counts at the current query level:
count_comp1: presence ofWHERE(+1),GROUP BY/HAVING(+1),ORDER BY(+1),LIMIT(+1), extra tables/subqueriesmax(0, len(from_tables) + len(from_subqueries) - 1), plus count of current-levelORandLIKE/ILIKEnodes.count_comp2:len(from_subqueries)+ direct nested query nodes inWHERE/HAVING/SELECT/JOIN ON+len(iuen_branches)+len(ctes).count_others:+1if current-levelexp.AggFunccount > 1,+1iflen(select_items) > 1,+1iflen(where_preds) > 1,+1iflen(group_by_full) > 1.
| Bucket | Exact Threshold Rule |
|---|---|
easy |
count_comp1 <= 1 and count_others == 0 and count_comp2 == 0 |
medium |
(count_others <= 2 and count_comp1 <= 1 and count_comp2 == 0) or (count_comp1 <= 2 and count_others < 2 and count_comp2 == 0) |
hard |
(count_others > 2 and count_comp1 <= 2 and count_comp2 == 0) or (2 < count_comp1 <= 3 and count_others <= 2 and count_comp2 == 0) or (count_comp1 <= 1 and count_others == 0 and count_comp2 <= 1) |
extra |
All remaining queries (e.g., count_comp2 >= 2, or CTE/IUEN + multiple clauses). |
How We Adapted Yu et al. (2018) to sqlglot: Choices & Bug Fixes¶
Instead of cloning taoyds/spider/process_sql.py (which uses nltk.word_tokenize, downloads punkt at runtime, and fails on >90% of bird_mini_dev gold SQL) or lowering sqlglot ASTs into Spider's flat Python dicts (which fails on 376/500 = 75.2% of bird_mini_dev gold SQL), ComponentMatching decomposes sqlglot ASTs directly while fixing five verified defects in taoyds/spider/evaluation.py:
Bug Fixes vs. taoyds/spider/evaluation.py¶
- Zero-match corpus
f1 = 1bug (evaluation.py:604-605):- Upstream bug: When
acc == 0 and rec == 0, Spider unconditionally setsscores[level]['partial'][type_]['f1'] = 1—even whenacc_count > 0orrec_count > 0(meaning predictions or references had the component and matched zero times!). Onbird_mini_dev, this bug would reportIUEN = 1.000andcte = 1.000despite0matches! - Our fix (
finalize_component_bucket): ReturnsNone(precision,recall,f1) only whenpred_count == 0 and label_count == 0; returns0.0whenever either count> 0andprecision + recall == 0.0.
- Upstream bug: When
- Swapped denominators & empty-set
(1, 1, 1)inflation ineval_and_or(evaluation.py:195-203, 419):- Upstream bug: On mismatch,
eval_and_orreturned(len(pred_ao), len(label_ao), 0), buteval_partial_matchunpackedlabel_total, pred_total, cnt = eval_and_or(...)—incrementing Precision's denominator using gold counts and Recall's using prediction counts. It also returned(1, 1, 1)whenpred_ao == label_ao == set(), inflatingand/orF1 with every query lacking boolean connectors. - Our fix (
score_pair): Assignsao_label_total = len(ref.where_and_or)andao_pred_total = len(pred.where_and_or)in the right slots, and returns(pred_total=0, label_total=0, f1=1)when both sets are empty so corpusand/orF1 measures actualAND/ORqueries cleanly without breakingExactMatching.
- Upstream bug: On mismatch,
- In-place mutation of parsed gold dicts (
evaluation.py:123):- Upstream bug:
eval_selmutateslabel['select'][1]via.remove(unit), corrupting gold dicts reused across multi-reference or repeated calls. - Our fix: All decomposed
NormalizedQueryfields are immutable tuples/frozensets and overlap usescollections.Counterintersections.
- Upstream bug:
- Hardness aggregate over-counting (
evaluation.py:333, 338):- Upstream bug:
count_othersrancount_agg(unit[0] != 0)onwhere[::2](where index0is a booleannot_opflag!) and on unslicedhaving(containing'and'/'or'strings, where'and'[0] != 0is truthy), misclassifying queries withNOTor compoundHAVINGinto higher hardness buckets. - Our fix (
_count_current_level_aggs): Counts actualexp.AggFuncAST nodes at the current query level acrossSELECT,WHERE,GROUP BY,ORDER BY, andHAVING.
- Upstream bug:
- Silent clause omissions in
eval_having/eval_order(evaluation.py:166-192):- Upstream gaps: Upstream
eval_havingignoredHAVINGon queries withoutGROUP BYand comparedgroupByandhavingas order-sensitive Python lists;eval_orderignoredLIMITon queries withoutORDER BY. - Our adaptations:
grouptriggers on(group_by_full or having_preds)and comparesCountermultisets forGROUP BYandHAVING(+having_and_or);ordertriggers on(order_by or has_limit)(handlingLIMIT-only andFETCH FIRSTqueries cleanly).
- Upstream gaps: Upstream
Intentional Generalizations & Configuration Choices¶
- Non-literal-LHS canonicalization (
where&where(no OP)): Spider puts the column expression inunit[2]and literal inunit[3]. Insqlglot,WHERE 'S' = s.rtypeputs'S'onthis(LHS). Our predicate canonicalizer swapsEQ/NEQand flips ordered inequalities (10 < age->age > 10) so non-literal expressions stay onthis, makingwhere(no OP)work naturally without colliding unrelated columns (WHERE a = 1vsWHERE b = 2both yielding normalized column targetsaandb). 12component keys (+ cte,+ join_on): Addscte(coveringWITHqueries present in30%ofbird_mini_dev) and opt-injoin_on(include_join_conditions=True) rather than hiding CTEs inside subqueries or losingJOIN ONsignal.- Spider value-blind defaults + auditable strict flags: Preserves
ignore_values=True,ignore_distinct=True,include_limit_value=False, andinclude_join_conditions=Falseby default for Spider continuity, while exposing all flags as explicit keyword toggles and stamping them onto everycompute()output dictionary. - Multi-reference deterministic winner selection: Supports
str | Sequence[str]references per example, picking the reference maximizing(exact_match, matched_active_components, all_components_f1_sum, -ref_index)(both score and hardness come from that winning reference). - Unparseable-prediction penalty (
skip_unparseable_predictions=False): Matches Spider's empty-query penalty (0on all gold components inlabel_countwithout shrinking recall denominators) by default, with an opt-inskip_unparseable_predictions=Truetoggle when evaluating syntax-valid subsets.
API Reference¶
Signatures¶
gbv.load("component_matching", **kwargs) -> ComponentMatching/ComponentMatching(...)add(prediction, reference, schema=None, foreign_keys=None) -> Noneadd_batch(predictions, references, schemas=None, foreign_keys=None) -> Nonecompute_component_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 rewrite COUNT(1) -> COUNT(*). Set False to check exact constants. |
ignore_distinct |
bool |
True |
Strip DISTINCT and collapse UNION/INTERSECT/EXCEPT ALL distinctions. Set False for strict matching. |
include_limit_value |
bool |
False |
Match exact LIMIT/OFFSET expressions instead of only presence/absence (has_limit). |
include_join_conditions |
bool |
False |
Evaluate flattened JOIN ... ON and USING(...) predicates in the join_on component. |
skip_unparseable_predictions |
bool |
False |
Skip unparseable predictions (None in per_example) instead of scoring against an empty query. |
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 component score dicts in compute()["per_example"]. |
compute() Output Schema¶
| Key | Type | Semantics |
|---|---|---|
components |
dict[str, dict[str, float \| int \| None]] |
Overall ("all") breakdown for all 12 keys; each maps to {"precision", "recall", "f1", "pred_count", "label_count"} (None when both counts are 0). |
by_hardness |
dict[str, dict[str, Any]] |
Keys "easy", "medium", "hard", "extra", "all"; each maps to {"count": int, "components": <12-key dict>}. |
scored_count |
int |
Examples contributing to scores. |
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 {"hardness", "prediction_parsed", "schema_applied", "foreign_keys_applied", "components"} (no raw SQL or literals). |
Examples¶
Spider Value-Blind Defaults vs. Opt-In Strict Mode¶
import group_by_vibes as gbv
preds = [
"SELECT u.id, COUNT(1) FROM users u JOIN orders o ON u.id = o.user_id "
"WHERE 'A' = u.status GROUP BY u.id ORDER BY COUNT(1) LIMIT 10"
]
refs = [
"SELECT ord.user_id, COUNT(*) FROM users usr JOIN orders ord ON usr.id = ord.user_id "
"WHERE usr.status = 'Z' GROUP BY ord.user_id ORDER BY COUNT(*) LIMIT 99"
]
schemas = [{"users": ["id", "status"], "orders": ["user_id"]}]
fks = [[("users.id", "orders.user_id")]]
# Default Spider value-blind mode + FK equivalence collapse
default_res = gbv.compute_component_matching(
preds, refs, schemas=schemas, foreign_keys=fks, dialect="postgres"
)
assert default_res["components"]["select"]["f1"] == 1.0
assert default_res["components"]["where"]["f1"] == 1.0
assert default_res["components"]["group"]["f1"] == 1.0
assert default_res["components"]["order"]["f1"] == 1.0
assert default_res["components"]["cte"]["f1"] is None
# Strict mode enforces exact literal values, COUNT(1) != COUNT(*), and LIMIT values
strict_res = gbv.compute_component_matching(
preds,
refs,
schemas=schemas,
foreign_keys=fks,
dialect="postgres",
ignore_values=False,
ignore_distinct=False,
include_limit_value=True,
include_join_conditions=True,
)
assert strict_res["components"]["select"]["f1"] == 0.0
assert strict_res["components"]["where"]["f1"] == 0.0
assert strict_res["components"]["order"]["f1"] == 0.0