Skip to content

fix(validation): compare qualified columns and handle huge literals in the drift check - #701

Merged
christianeu-db merged 1 commit into
apache:mainfrom
jbonofre:metric-drift-followups
Nov 18, 2026
Merged

christianeu-db merged 1 commit into
apache:mainfrom
jbonofre:metric-drift-followups

Conversation

@jbonofre

@jbonofre jbonofre commented Nov 6, 2026

Copy link
Copy Markdown
Member

Summary

Follow-ups to #634 (issue #279):

  • _number_text normalized literals under the default Decimal context, so a literal past Emax such as 1e1000000 raised decimal.Overflow out of validation, even for a single-dialect metric, and literals over 28 digits rounded to the same token. It now normalizes under a context that keeps every digit and exponent.
  • Columns are read with iter_columns(), so the date unit in DATEDIFF(day, ...) is no longer counted as a column under ANSI_SQL and BigQuery, which gave identical SQL a [Drift] warning.
  • Columns keep their qualifier, both in the signature and in predicates. In a metric, orders.amount and returns.amount are different fields, as are source.amount and amount in a dataset metric (spec.md, Name Resolution). The test that treated SUM(o.amount) and SUM(r.amount) as equivalent now expects drift.
  • Metrics are read one at a time with read_expressions() instead of itertools.groupby over every expression, so two metrics that share a name (already a [Unique] error) are no longer compared as dialects of one. The signature is only computed when a metric has two parsed dialects.

@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.

With a field amount declared as sql: amount, a dataset-scoped metric written SUM(amount) in one dialect and SUM(source.amount) in another now gets a [Drift] on referenced columns, although by the Name Resolution paragraph on shared names both are the source column amount; the test only covers the derived field, where they differ. Folding source.c into c when the field c is the bare column c would keep the warning for that case alone, but it is a warning, so leaving it is fine too.

The drift tests also pass at the header floor sqlglot 30.12.0, the example and compliance documents validate as on main, and the 1e1000000 overflow and the duplicate-name [Drift] both reproduce on main and are gone here. The branch conflicts with #698 in validate.py.

christianeu-db
christianeu-db previously approved these changes Nov 10, 2026

@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.

LGTM, thanks! Two notes inline: one matters for whichever of this and #696 lands second, and the other can be a follow-up.

Comment thread validation/validate.py Outdated
columns = frozenset(
column.name.lower() for column in tree.find_all(exp.Column) if column.name
)
columns = frozenset(_column_key(column) for column in iter_columns(tree))

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.

#696 makes dialect a required argument of iter_columns, and this call passes only the tree. The two branches merge without a conflict, but with both applied a metric with SUM(orders.amount) in ANSI_SQL and SNOWFLAKE fails:

Error: TypeError while validating m.yaml: iter_columns() missing 1 required positional argument: 'dialect'
Could whichever of the two lands second pass the expression's dialect through _expression_signature?

Comment thread validation/validate.py
return constant
if isinstance(node, exp.Column):
return node.name.lower()
return _column_key(node)

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.

Identical SQL in ANSI_SQL and SNOWFLAKE still drifts when a filter uses DATEDIFF, because the default parser reads day as a date argument and the Snowflake parser reads it as the unit:

COUNT(CASE WHEN DATEDIFF(day, orders.opened_at, orders.closed_at) <= 3 THEN 1 END)

[Drift] ... filter predicates differ across dialects
(ANSI_SQL=['LTE(datediff(day, orders.opened_at, orders.closed_at),3)'],
SNOWFLAKE=['LTE(datediff(orders.closed_at, orders.opened_at, day),3)'])

This is on main too. Could _comparison_operand read the DATEDIFF unit the way iter_columns does?

…n the drift check

Follow-ups to apache#634:

- Normalise numeric literals under a context that keeps every digit and
  exponent, so a literal such as 1e1000000 no longer raises Overflow out
  of validation and literals over 28 digits no longer round together.
- Read columns with iter_columns(), so the date unit in DATEDIFF(day, ...)
  is not a column under parsers that do not know the function.
- Keep column qualifiers: orders.amount and returns.amount are different
  fields, as are source.amount and amount in a dataset metric.
- Compare each metric's own dialects via read_expressions() instead of
  grouping adjacent expressions by name, and compute the signature only
  when a metric has two parsed dialects.
@christianeu-db
christianeu-db merged commit b50b3a7 into apache:main Nov 18, 2026
8 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.

3 participants