Repository navigation
feat(core-spec): Let dataset-scoped metrics evaluate source measures with AGG() - #726
Conversation
24e36ac to
4927166
Compare
4927166 to
4550972
Compare
a560c1a to
f3ba6b0
Compare
…with AGG() A dataset-scoped metric may now evaluate a measure of its dataset's source with AGG(source.measure), as in AGG(source.total_revenue) / COUNT(DISTINCT order_id). The source must be a measure relation (Relational Query Interface, Section 3.1). A new section, Measure Relation as a source, applies that definition to a source, which is an object that an engine holds. The source is a measure relation if and only if it has at least one measure column and four conditions hold. Every query reads one multiset of rows, which does not depend on the columns that the query selects, so a query returns the same groups with no measure, one measure, or several measures. A query can group and filter every measure by every column that is not a measure. Each measure aggregates rows that are fixed where the measure is defined, with a row identity that a query does not change. A query can join the source to another relation, and each measure still aggregates each of its rows at most once in each group. The conditions apply to each source and to the results that its queries return. A source whose queries return rows or groups that depend on the selected columns is not a measure relation, even if a query can name it in FROM and evaluate its metrics with AGG; the Layer 3 wide table over an Ossie semantic model is the example. A dataset still cannot use another dataset as its source. The argument must be a single source.measure reference, and the call takes no FILTER clause. A source measure cannot be referenced in any other way, and AGG cannot be called in any other way: SUM of a source measure, AGG of a field, and AGG of a metric are invalid. A field expression cannot refer to a measure column. Such a metric is a derived measure (Relational Query Interface, Section 5.2). Each measure of the source keeps the rows that it aggregates and their row identity, also when the source has measures with different row identities, so the metric keeps each one's no-duplication guarantee, including through AGG(dataset.metric). Other aggregates in the expression aggregate the dataset's rows. Section 3.4 of the Relational Query Interface now distinguishes base and derived dataset-scoped measures, and a dataset-scoped metric is described as defined on the dataset's relation rather than at its row level. The document does not declare which source columns are measures, so document validation checks only the form of each AGG call. A consumer that generates SQL against the dataset's source must not read a measure as an ordinary column or replace AGG with an ordinary aggregate, and may reject a source that it does not support. The validator accepts AGG(source.measure) in dataset-scoped metrics and reports any other AGG call there as an error. These checks apply only to OSSIE_SQL_2026. This change also adds the docs/spec_changes.md row for apache#703. Co-authored-by: Isaac <no-reply@databricks.com>
f3ba6b0 to
8dcdcea
Compare
There was a problem hiding this comment.
👍
The [DISCUSS] thread is here: https://lists.apache.org/thread/48mt12114jopqpbhq4gl39qy84pvbmpp
It started on Nov 14, target Friday, Nov 20.
I have a few minor nits:
validation/validate.py(source_agg_problem):or not arg.namewould also reject an empty quoted identifier likeAGG(source."").validation/validate.py(validate_metric_scoping): the comment# validate_agg_placement reports an OSSIE_SQL_2026 AGG call here...could be updated since validAGG(source.measure)calls are now allowed there too.core-spec/expression_language.md: the new changelog row uses0.3.0.devinstead of0.3.0.dev0.docs/spec_changes.md: please update the2026-11-13date on the#726row to the actual merge date before/at merge time.
- Reject an empty quoted identifier, as in AGG(source.""), and test it. - Update the validate_metric_scoping comment: validate_agg_placement now accepts AGG(source.measure) and reports any other form. - Use 0.3.0.dev0 in the expression language change log row. - Date the apache#726 row in docs/spec_changes.md 2026-11-20, the merge date. Co-authored-by: Isaac <no-reply@databricks.com>
- Reject an empty quoted identifier, as in AGG(source.""), and test it. - Update the validate_metric_scoping comment: validate_agg_placement now accepts AGG(source.measure) and reports any other form. - Use 0.3.0.dev0 in the expression language change log row. - Date the expression language change log row and the apache#726 row in docs/spec_changes.md 2026-11-20, the merge date. Co-authored-by: Isaac <no-reply@databricks.com>
df60508 to
5bfc2b3
Compare
@jbonofre thanks for the feedback - I've addressed your comments when you have a chance to re-stamp |
justint-db
left a comment
There was a problem hiding this comment.
Thanks @christianeu-db, this reads well and fits #633 and #703. A few things I checked:
- It merges cleanly into the current
main, and the validation suite passes. - The validator behaves as described:
AGG(region)andFILTERonAGGare errors, a SnowflakeAGG(...)is ignored, and a model-scopedAGG(orders.m)over a dataset-scoped metric that usesAGG(source.…)passes. - Thanks for adding the #703 row to
docs/spec_changes.md. I'd missed that.
One request (rule 9) and two small notes inline.
One question, not a blocker: #703 added a sentence to wide_table_semantics.md §5.2 saying an AGG(D.m) call is one component with home dataset D. With this PR, D.m can wrap a source measure whose row identity starts upstream of D. I think Layer 3 still holds, since it's defined through Layer 2, but would a sentence in §5.2 help, or is that already implied?
| 6. A model-scoped metric MAY evaluate a dataset-scoped metric with `AGG(dataset.metric)`: `AGG(orders.total_revenue) / COUNT(DISTINCT customers.customer_id)`. The argument MUST be a single reference to a dataset-scoped metric, written as `dataset.metric`, and the call MUST NOT have a `FILTER (WHERE ...)` clause. A model-scoped metric MUST NOT refer to a metric in any other way: a bare `orders.total_revenue`, `AGG` of a field, and `AGG` of a model-scoped metric are invalid. | ||
| 7. A dataset-scoped metric MAY evaluate a measure of the dataset's `source` with `AGG(source.measure)` if the `source` is a measure relation (see [Measure Relation as a `source`](#measure-relation-as-a-source)): `AGG(source.total_revenue) / COUNT(DISTINCT order_id)`. The argument MUST be a single reference to a measure of the `source`, written as `source.measure`, and the call MUST NOT have a `FILTER (WHERE ...)` clause. A dataset-scoped metric MUST NOT refer to a source measure in any other way, and MUST NOT call `AGG` in any other way: `SUM(source.total_revenue)`, `AGG` of a field, and `AGG` of a metric are invalid. If the `source` is not a measure relation, a dataset-scoped metric MUST NOT call `AGG`. | ||
| 8. A dataset-scoped metric that calls `AGG` is a derived measure ([Relational Query Interface](relational_semantics.md), Section 5.2), and its dataset remains a relation with the metric as a measure column. The `AGG(source.measure)` call composes the source measure into the metric and does not evaluate it where the metric is defined: the source measure is evaluated when a consuming query evaluates the metric, in that query's context. Each measure of the `source` keeps the rows that it aggregates and their row identity (Sections 3.2 and 3.3 of that document), also when the `source` has measures with different row identities. So the metric keeps the no-duplication guarantee of each source measure (Section 8.3 of that document), also when a model-scoped metric evaluates it with `AGG(dataset.metric)`. Its other aggregates, such as `COUNT(DISTINCT order_id)`, aggregate the dataset's rows. | ||
| 9. The Ossie document does not declare which source columns are measures, so document validation checks only the form of each `AGG` call. A consumer that generates SQL against the dataset's `source` MUST NOT read a measure as an ordinary column or replace `AGG` with an ordinary aggregate, and MAY reject a `source` that it does not support. |
There was a problem hiding this comment.
This leaves open who reports an invalid AGG argument. Rule 7 makes AGG(source.x) invalid when x isn't a measure, and SUM(source.total_revenue) invalid when it is one. But the validator can't tell which columns are measures, so SUM(source.total_revenue) passes document validation, and "MAY reject a source that it does not support" doesn't require anyone to catch it.
On #633, JB suggested that the engine MUST reject the metric when it binds it to its source. Could rule 9 say that? For example:
A consumer that binds the dataset to its
sourceMUST reject a metric or field that refers to a source measure outsideAGG, or that callsAGGon a column that is not a measure of thesource, including when thesourcehas no measures.
| | 0.2.0.dev | 2026-07-15 | Initial draft | | ||
| | 0.2.0.dev | 2026-10-29 | State that function names are case insensitive. Reserve `AGG` for measure evaluation; expressions that call another function named `AGG` must rename it or move to a vendor dialect | | ||
| | 0.2.0.dev | 2026-11-06 | Allow model-scoped metrics to evaluate a dataset-scoped metric with `AGG(dataset.metric)` | | ||
| | 0.3.0.dev0 | 2026-11-20 | Allow dataset-scoped metrics to evaluate a source measure with `AGG(source.measure)` when the `source` is a measure relation | |
There was a problem hiding this comment.
|
|
||
| An engine that conforms to the Relational Query Interface (Section 12 of that document) exposes measure relations that meet these conditions. | ||
|
|
||
| The conditions apply to each `source`, and to the results that its queries return. A `source` whose queries return rows or groups that depend on the selected columns does not meet condition 1, even if a query can name the `source` in `FROM` and evaluate its metrics with `AGG`. For example, an Ossie semantic model has datasets and metrics, and Layer 3 exposes the model as one wide table ([Wide Table Query Interface](wide_table_semantics.md)). The wide table is not a measure relation, and it would not become one if its queries evaluated metrics with `AGG`: each metric supplies the groups of its home dataset, so the groups that a query returns depend on the metrics that the query evaluates (Section 6.1 of that document). |
There was a problem hiding this comment.
Optional: the four conditions are useful, since a source may come from an engine that doesn't claim Layer 2 conformance. This paragraph, though, explains at some length why the Layer 3 wide table doesn't qualify. It could be shortened to one sentence, for example: "The Layer 3 wide table is not a measure relation: the groups a query returns depend on the metrics it evaluates (Wide Table Query Interface, Section 6.1)." Fine to keep as it is.
…eferences Address review feedback on AGG(source.measure): - Rule 9: a consumer that generates SQL against the source MUST reject a source measure outside AGG, and AGG on a column that is not a measure. Update the docs/spec_changes.md row to match. - Shorten the paragraph on why the Layer 3 wide table is not a measure relation. - Wide Table Query Interface, Section 5.2: state that a dataset-scoped metric that calls AGG(source.measure) is still one component. - Label the apache#703 row of the expression language change log 0.3.0.dev0. Co-authored-by: Isaac <no-reply@databricks.com>
kayemkim
left a comment
There was a problem hiding this comment.
docs/index.md still describes a dataset-scoped metric the old way: the Metrics row under "Specification at a Glance" says it "aggregates one dataset's own fields at its row level", and the Glossary says it is "evaluated at the dataset's row level". The PR replaces that wording in spec.md, spec.yaml and the schema description, so these two rows could take the same "defined on the dataset's relation" phrasing.
Address review feedback on AGG(source.measure): the Metrics row under "Specification at a Glance" and the Metric row in the Glossary now say that a dataset-scoped metric is defined on the dataset's relation, as spec.md, spec.yaml and the schema description do. Co-authored-by: Isaac <no-reply@databricks.com>
Resolve conflicts with apache#705 in core-spec/spec.md and docs/spec_changes.md by keeping both sides. The apache#726 entries stay last in each change log. Co-authored-by: Isaac <no-reply@databricks.com>
apache/ossie#726 lets a dataset-scoped metric evaluate a measure of its dataset's source with AGG(source.measure), when the source is a measure relation. A MetricFlow semantic model reads a plain table, and the spec says a consumer must not replace AGG with an ordinary aggregate, so the converter drops such a metric with UNSUPPORTED_METRIC_EXPRESSION. It already did, because AGG is not an aggregate the converter knows. Pin that with a test, alone, in a ratio and in a scalar wrapper, and say so in the README. Also test that a dataset ratio carries the generated_ratio_helpers provenance from apache#121, so the reverse pass folds its helpers away.
Resolve conflicts with apache#726 (AGG(source.measure) in dataset-scoped metrics): - expression_language.md: keep upstream's AGG row and add the METRIC row to "Not Supported in Expressions"; keep upstream's version rows and add the METRIC row after them. - relational_semantics.md: take upstream's Section 3.4 measure row and add that the metric's expression is the operand of METRIC(). - docs/spec_changes.md: keep the apache#726 entry before the apache#774 entry. Co-authored-by: Isaac <no-reply@databricks.com>
…ource.x) apache/ossie#726 lets a dataset's metric call AGG(source.measure) when its source is a measure relation, and says a consumer must refuse a call on a column that is no measure of the source, including a source with none, rather than run it as a plain aggregate (spec.md, "Metric Scoping" rules 7 and 9). A ClickHouse table declares no measures, so the call stays refused, with that reason in place of the one that AGG is reserved for a model's metric. Any other call of AGG in a dataset's metric or a field keeps the general reason. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
A dataset-scoped metric may now evaluate a measure of its dataset's source with AGG(source.measure), as in AGG(source.total_revenue) / COUNT(DISTINCT order_id). The source must be a measure relation (Relational Query Interface, Section 3.1).
A new section, Measure Relation as a
source, gives the conditions:An object that may change its internal join paths for each query does not meet these conditions, even if a query can name the object in FROM and evaluate its metrics with AGG. The Layer 3 wide table over an Ossie semantic model is the example: each metric supplies the groups of its home dataset. A dataset still cannot use another dataset as its source.
The argument must be a single source.measure reference, and the call takes no FILTER clause. A source measure cannot be referenced in any other way, and AGG cannot be called in any other way: SUM of a source measure, AGG of a field, and AGG of a metric are invalid. A field expression cannot refer to a measure column.
Such a metric is a derived measure (Relational Query Interface, Section 5.2). Each source measure keeps its own source rows and row identity, also when the source has measures with different row identities, so the metric keeps each one's no-duplication guarantee, including through AGG(dataset.metric). Other aggregates in the expression aggregate the dataset's rows. Section 3.4 of the Relational Query Interface now distinguishes base and derived dataset-scoped measures, and a dataset-scoped metric is described as defined on the dataset's relation rather than at its row level.
The document does not declare which source columns are measures, so document validation checks only the form of each AGG call. A consumer that generates SQL against the dataset's source must not read a measure as an ordinary column or replace AGG with an ordinary aggregate, and may reject a source that it does not support.
The validator accepts AGG(source.measure) in dataset-scoped metrics and reports any other AGG call there as an error. These checks apply only to OSSIE_SQL_2026.
Related Issues
Related to #703, #633, #662
Checklist
Specification
core-spec/and follow the existing structure[DISCUSS]thread ondev@with a stated end date, and reach lazy consensus (or pass a[VOTE]) before merge: https://lists.apache.org/thread.html/48mt12114jopqpbhq4gl39qy84pvbmppdocs/spec_changes.mdrecords rationale, compatibility and migration impact, or the PR explains why no entry is neededValidation
validation/are updated if the spec changedTests
pytest/ CI green)Compliance
NOTICEand/orLICENSEhave been updated per ASF policyAI disclosure
Per the ASF Generative Tooling Guidance, this contribution was prepared with AI assistance. I have reviewed and verified every change.