Repository navigation
fix(validation): compare qualified columns and handle huge literals in the drift check - #701
Conversation
kayemkim
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
LGTM, thanks! Two notes inline: one matters for whichever of this and #696 lands second, and the other can be a follow-up.
| 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)) |
There was a problem hiding this comment.
#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?
| return constant | ||
| if isinstance(node, exp.Column): | ||
| return node.name.lower() | ||
| return _column_key(node) |
There was a problem hiding this comment.
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.
be9e639 to
f8e810e
Compare
Summary
Follow-ups to #634 (issue #279):
_number_textnormalized literals under the defaultDecimalcontext, so a literal past Emax such as1e1000000raiseddecimal.Overflowout 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.iter_columns(), so the date unit inDATEDIFF(day, ...)is no longer counted as a column under ANSI_SQL and BigQuery, which gave identical SQL a[Drift]warning.orders.amountandreturns.amountare different fields, as aresource.amountandamountin a dataset metric (spec.md, Name Resolution). The test that treatedSUM(o.amount)andSUM(r.amount)as equivalent now expects drift.read_expressions()instead ofitertools.groupbyover 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.