Skip to content

fix(compliance): check the dataset in an AGG(dataset.metric) argument against the model - #815

Merged
jbonofre merged 2 commits into
apache:mainfrom
MrBlack1995:fix/grader-agg-dataset-check
Nov 26, 2026
Merged

jbonofre merged 2 commits into
apache:mainfrom
MrBlack1995:fix/grader-agg-dataset-check

Conversation

@MrBlack1995

@MrBlack1995 MrBlack1995 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to the review of #758.

The grader resolves a dataset qualifier against the authored model before it compares expression trees, so that customers.amount returned for sales.amount needs review. It did that for fields only: _field_owners reads datasets[].fields. The argument of AGG(dataset.metric) names a dataset-scoped metric, which no dataset declares as a field, so its qualifier was dropped before the comparison:

authored  AGG(sales.total_amount) / NULLIF(AGG(sales.order_count), 0)
returned  AGG(orders.total_amount) / NULLIF(AGG(orders.order_count), 0)
before    REWRITTEN cosmetic (normalized trees equal)
after     REWRITTEN review: normalized trees differ

What changed:

  • _metric_owners maps each dataset-scoped metric name to the datasets that declare it, the counterpart of _field_owners.
  • In a model-scoped metric, a column that is the argument of AGG is resolved against the metric owners, every other column against the field owners as before. The rules are the same as for fields: a qualifier that names another dataset needs review; a respelled or dropped qualifier is cosmetic when one dataset declares the metric; when several datasets declare it, the qualifier is meaning and dropping it needs review.
  • A dataset renamed after its source table (the existing _dataset_aliases rule) is still read as the authored dataset inside AGG.
  • The grader README describes the rule.

Out of scope: no fixture or manifest changes. AGG(source.measure) in a dataset-scoped metric is unchanged; in that scope a written qualifier is already kept as written.

Related Issues

Follow-up to #758 (review note on m5_agg_ref.ossie.yaml).

Validation

  • compliance/portability/grader: 64 passed (58 on main plus 6 new), with sqlglot 30.32.0, with the declared minimum 30.29.0, and with 25.0.0. The last one matters because older sqlglot parses AGG as an anonymous function and newer versions as an Agg node; both paths are covered.
  • On main's grade.py four of the six new tests fail (the wrong dataset, the metric declared by two datasets, metric against field, and _metric_owners itself). The two that pass there are the cosmetic cases, which must not change.
  • Real converter output: I graded the 255 portability cases of the latest Interop matrix run on apache/ossie-converters main (run 45326958915, 246f687) with main's grader and with this one. The findings are identical in all 255. That matrix does not contain M5.agg_ref yet (its spec pin is older than test(compliance): add an M5 variant that composes through AGG(dataset.metric) #758), so no real return with an AGG call was graded.
  • pre-commit passes on the three files.

Checklist

Specification

  • Spec changes are included in core-spec/ and follow the existing structure
  • Spec changes have a [DISCUSS] thread on dev@ with a stated end date, and reach lazy consensus (or pass a [VOTE]) before merge
  • Breaking changes to the spec are clearly called out in the summary
  • For a material spec change, docs/spec_changes.md records rationale, compatibility and migration impact, or the PR explains why no entry is needed

No spec change: this is a grader fix under compliance/portability/grader/.

Tests

  • All existing tests pass (pytest / CI green)
  • New functionality is covered by tests

Documentation

  • docs/ is updated to reflect any user-facing changes (the grader README)

Compliance

  • ASF license headers are present on all new source files (no new files)

… against the model

The grader resolves a dataset qualifier against the authored model before it
compares expression trees, but only for fields: _field_owners reads
datasets[].fields. The argument of AGG(dataset.metric) names a dataset-scoped
metric, which no dataset declares as a field, so its qualifier was dropped
before the comparison. A return of AGG(orders.total_amount) for the authored
AGG(sales.total_amount) graded as a cosmetic rewrite, where SUM(orders.amount)
for SUM(sales.amount) needs review.

AGG arguments are now resolved against the datasets that declare the metric
(_metric_owners), with the same rules as fields: a qualifier that names another
dataset needs review, a respelled or dropped qualifier is cosmetic when one
dataset declares the metric, and it stays meaning when several do.

Follow-up to the review of apache#758. Grader only, no fixture changes.

Co-authored-by: Isaac <no-reply@databricks.com>
@MrBlack1995
MrBlack1995 force-pushed the fix/grader-agg-dataset-check branch from 20dbc9c to 63620b1 Compare November 24, 2026 12:49
@jbonofre
jbonofre self-requested a review November 25, 2026 17:06
Comment thread compliance/portability/grader/grade.py Outdated
Comment on lines +259 to +260
declarations = (metric_owners or {}) if agg_argument(column) else owners
declared_by = declarations.get(column.name.lower(), frozenset())

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.

When the name isn't a declared metric, or a caller doesn't pass metric_owners, the qualifier is dropped. Main still checked it against the field owners in that case: AGG(sales.amount) -> AGG(customers.amount) was review on main and is cosmetic here. No current caller hits this, but falling back keeps the grader from getting more lenient than main:

Suggested change
declarations = (metric_owners or {}) if agg_argument(column) else owners
declared_by = declarations.get(column.name.lower(), frozenset())
name = column.name.lower()
declared_by = (metric_owners or {}).get(name, frozenset()) if agg_argument(column) else frozenset()
declared_by = declared_by or owners.get(name, frozenset())

# on M0 needs review.
finding = _m5_agg_finding("AGG(orders.total_amount) / NULLIF(AGG(orders.order_count), 0)")
assert finding.outcome == grade.REWRITTEN
assert finding.detail.startswith("review"), finding.detail

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.

startswith("review") also matches "review: could not compare trees". If a parse failure happened, these tests would still pass without checking the qualifier resolution.

Could they assert finding.detail == "review: normalized trees differ"?

…at is not a metric

An AGG argument no dataset declares as a metric, or one graded without
metric_owners, lost its qualifier check. It is now checked against the
fields, as on main. The review assertions of the AGG tests name the
"normalized trees differ" detail, so a parse failure cannot pass them.

Co-authored-by: Isaac <no-reply@databricks.com>
@MrBlack1995

Copy link
Copy Markdown
Contributor Author

Thanks, both applied in b30dc20. The fallback is your suggestion as written, plus two tests for it (a name that is not a metric, and a caller without metric_owners). For the assertions I used startswith("review: normalized trees differ") rather than ==, because the detail continues with ; returned '...'; that still rules out could not compare trees.

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

Thanks @MrBlack1995!

LGTM!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants