Skip to content

fix(validation): compare quoted identifiers as written in metrics - #784

Merged
jbonofre merged 4 commits into
apache:mainfrom
vigneshio:fix/validation-quoted-identifiers-in-metrics
Nov 26, 2026
Merged

jbonofre merged 4 commits into
apache:mainfrom
vigneshio:fix/validation-quoted-identifiers-in-metrics

Conversation

@vigneshio

Copy link
Copy Markdown
Contributor

Metric references, dataset-scoped scoping, AGG, and field shadowing compared SQL names with casefold, so SUM("Amount") matched field amount and AGG(orders."total") matched metric total. The mapping checker already follows expression_language.md: a regular identifier upper-cases, a quoted one matches as written.

This reuses normalized_identifier and declared_name for those SQL lookups. YAML keys and relationship lists stay on dataset_field_names (casefold). The source qualifier is the regular identifier SOURCE; quoted "source" is not

@christianeu-db christianeu-db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vigneshio! The comments on vendor dialects and on the pytest failures should be fixed before merge.

cc @kayemkim if you want to take a look as well

Comment thread validation/validate.py Outdated
return errors


def _sql_ident(part) -> str:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A vendor-dialect entry now gets the OSSIE_SQL_2026 quoting rule, so references that are valid in that dialect fail validation (both passed on main):

POSTGRES  SUM("amount")  -> references undeclared field(s) '"amount"'
BIGQUERY  SUM(`amount`)  -> references undeclared field(s) '"amount"'

expression_language.md gives the double-quote rule for the Ossie dialect and calls quoted-versus-unquoted comparison DB specific. Could _sql_ident apply only to OSSIE_SQL_2026 entries, as the AGG checks do, and keep the old comparison for other dialects?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, _sql_ident and the declared-name maps now follow the quoting table only for OSSIE_SQL_2026. POSTGRES SUM("amount") and BIGQUERY SUM(`amount`) keep the case-insensitive lookup they had on main.

Comment thread validation/validate.py Outdated
continue
name = member.get("name")
if isinstance(name, str) and name:
names.add(declared_name(name))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two existing tests in validation/tests/ now fail, because declared_name runs during validate_sql and its parse_one is outside _quiet_sqlglot:

FAILED test_another_thread_keeps_its_sqlglot_warnings_while_one_probes
FAILED test_a_validation_that_finishes_does_not_unquiet_one_still_running

CI stops at the duplicate-key tests before it reaches them. Could declared_name get @_quiet_sqlglot(), as expression_is_empty has?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declared_name now has @_quiet_sqlglot(), same as expression_is_empty. That parse is the first one on the validate_sql path once field names are keyed that way, so the two thread tests were seeing an unquieted parse_one.

Comment thread validation/validate.py Outdated
Comment on lines +1292 to +1294
dataset_label = idents[0].name if idents else arg.table
return (
f"AGG({arg.table}.{arg.name}): '{arg.name}' is not a dataset-scoped metric of "
f"dataset '{arg.table}'"
f"AGG({written}): '{idents[-1].name if idents else arg.name}' is not a "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker: these messages drop the quotes from the identifier, so they read as if a declared name, or the source qualifier, were wrong:

AGG(orders."total"): 'total' is not a dataset-scoped metric of dataset 'orders'
'"orders".amount' references unknown dataset 'orders'
uses qualified reference(s) 'source'

Could the metric name and dataset label in agg_argument_problem, the unknown-dataset label in validate_metric_references, and the qualifier in scope_errors print the identifier as written, as the mapping check does ('"orders"')? The two new AGG tests that assert the unquoted text would change with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Labels in agg_argument_problem, the unknown-dataset message in validate_metric_references, and the qualifier in scope_errors now print the identifier as written, quotes included. The two AGG tests assert '"total"' and '"order_revenue"'.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The messages now print identifiers as written, but _ident_written and _column_written render them in sqlglot's default dialect, so a BIGQUERY reference shows double quotes where the model has backticks:

SUM(`orders`.nope)  -> '"orders".nope' is not a declared field of dataset '"orders"'

Could both render in the expression's dialect through SQLGLOT_DIALECTS, as _expression_with_first_token_quoted does with .sql(dialect=dialect)? Fine as a follow-up if you'd prefer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left the dialect of the labels for a follow-up. _ident_written still uses part.sql() in the default dialect, so a BIGQUERY backtick still prints as double quotes.

@vigneshio
vigneshio force-pushed the fix/validation-quoted-identifiers-in-metrics branch from 3bcdd10 to 5db6937 Compare November 23, 2026 08:49

@christianeu-db christianeu-db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vigneshio can you take a look at the failing CI when you have the chance?

@kayemkim

Copy link
Copy Markdown
Member

@vigneshio the two failing tests aren't from your change. sqlglot 30.34.0 came out shortly before this run started, and main fails on the same two with it. Merged onto main your branch passes on 30.33.0. #810 fixes them, and I left a note there on its new quoted-name test, which asserts 'day' is not qualified where your branch prints '"day"'.

@kayemkim kayemkim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

names_cache in _expressions_in() lives for one field or metric, so _expression_field_names() runs again for every member of a dataset, and every name in it goes through declared_name(), which parses with sqlglot. The time grows with the square of the field count. A dataset of 400 fields written with sql validates in 0.3 s on main and in about 8.5 s on this branch, and one of 1,600 fields takes over two minutes. The examples in the repository stop at 21 fields, so nothing there shows it. With functools.lru_cache on declared_name() the 400-field document is back at 0.4 s and the suite passes.

The comparison itself matches the table in expression_language.md for the declared names and reference forms I tried under sql and OSSIE_SQL_2026: regular and mixed-case names, names that need quotes, and the source qualifier. Other dialects resolve as on main.

Metric references, dataset-scoped scoping, AGG, and field shadowing
compared SQL names with casefold, so SUM("Amount") matched field amount
and AGG(orders."total") matched metric total. The mapping checker
already follows expression_language.md: a regular identifier upper-cases,
a quoted one matches as written.

Reuse normalized_identifier and declared_name for those SQL lookups.
YAML keys and relationship lists stay on dataset_field_names (casefold).
The source qualifier is the regular identifier SOURCE; quoted "source"
is not.
Quoted versus unquoted identifier matching is DB specific
(expression_language.md). Vendor dialects keep the case-insensitive
lookup they had; POSTGRES SUM("amount") and BIGQUERY SUM(`amount`)
against field amount stay valid.

Quiet declared_name the same way as other probes, so it is not the
first unquieted parse on the validate_sql path. Print identifiers as
written in AGG and scope labels.
names_cache in _expressions_in is per member, so every YAML name was
parsed once per field. Cache the parse. Vendor dialects still casefold
without it.

Quoted "amount" stays an ANSI pass-through, as on main.
@vigneshio
vigneshio force-pushed the fix/validation-quoted-identifiers-in-metrics branch from 5db6937 to 5150a67 Compare November 25, 2026 11:45
@vigneshio

Copy link
Copy Markdown
Contributor Author

names_cache in _expressions_in() lives for one field or metric, so _expression_field_names() runs again for every member of a dataset, and every name in it goes through declared_name(), which parses with sqlglot. The time grows with the square of the field count. A dataset of 400 fields written with sql validates in 0.3 s on main and in about 8.5 s on this branch, and one of 1,600 fields takes over two minutes. The examples in the repository stop at 21 fields, so nothing there shows it. With functools.lru_cache on declared_name() the 400-field document is back at 0.4 s and the suite passes.

The comparison itself matches the table in expression_language.md for the declared names and reference forms I tried under sql and OSSIE_SQL_2026: regular and mixed-case names, names that need quotes, and the source qualifier. Other dialects resolve as on main.

declared_name now has @lru_cache(maxsize=None) around the existing @_quiet_sqlglot() probe. The per-member names_cache is still there; the parse is what was quadratic.

@vigneshio

Copy link
Copy Markdown
Contributor Author

@vigneshio the two failing tests aren't from your change. sqlglot 30.34.0 came out shortly before this run started, and main fails on the same two with it. Merged onto main your branch passes on 30.33.0. #810 fixes them, and I left a note there on its new quoted-name test, which asserts 'day' is not qualified where your branch prints '"day"'.

Rebased onto main so this picks up #818. The GET / PGDATE_PART failures were sqlglot 30.34.0 on main, same as you mentioned. The quoted-name clash with #810 went away when #818 replaced it.

@christianeu-db christianeu-db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vigneshio, this looks good to me now. Pre-commit fails on the ruff hooks that #818 added: make pre-commit fixes the formatting, and D401/D205 on the new docstrings and B023 on by_name need small edits by hand. Happy to approve once it's green.

@MrBlack1995 MrBlack1995 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converters are the main writers of quoted identifiers, so I ran this branch (5150a67) and main (59080f6) over real documents and compared the diagnostics:

  • the latest Interop matrix run on ossie-converters main: every input and every document a converter returned in the 272 portability cases, 514 documents;
  • the 17 fixtures under compliance/portability and the 11 documents under examples.

Both validators report the same on every one of them, so nothing the converters return today starts to fail or to warn.

One case that may surprise an author, and that I did not find a test for: a field declared with a mixed-case name that needs no quotes, name: Amount, is the regular identifier AMOUNT, so SUM(orders."Amount") now gets 'orders."Amount"' is not a declared field of dataset 'orders'. name: Order Amount with SUM(orders."Order Amount") passes, because that name can only be written quoted. Both follow from the comparison table in expression_language.md. A test with a mixed-case declared name would pin the first case down, and the message could say that the declared name is read as a regular identifier. Fine as a follow-up.

I did not run the test suite or pre-commit on this head.

@jbonofre
jbonofre self-requested a review November 26, 2026 13:47

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @vigneshio!

I pushed a small commit to address the pre-commit Ruff lint and formatting checks.

@jbonofre
jbonofre merged commit 34e4d37 into apache:main Nov 26, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants