Skip to content

feat(core-spec): Let dataset-scoped metrics evaluate source measures with AGG() - #726

Merged
christianeu-db merged 5 commits into
apache:mainfrom
christianeu-db:dataset-scoped-agg-source-measures
Nov 20, 2026
Merged

christianeu-db merged 5 commits into
apache:mainfrom
christianeu-db:dataset-scoped-agg-source-measures

Conversation

@christianeu-db

@christianeu-db christianeu-db commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

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:

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

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

  • 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: https://lists.apache.org/thread.html/48mt12114jopqpbhq4gl39qy84pvbmpp
  • Breaking changes to the spec are clearly called out in the summary - n/A
  • For a material spec change, docs/spec_changes.md records rationale, compatibility and migration impact, or the PR explains why no entry is needed

Validation

  • Validation rules in validation/ are updated if the spec changed
  • New validation cases are covered by tests

Tests

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

Compliance

  • ASF license headers are present on all new source files
  • New third-party dependencies follow Third-Party Dependencies
  • If third-party source code is vendored/copied (not just declared as a dependency), NOTICE and/or LICENSE have been updated per ASF policy

AI disclosure

Per the ASF Generative Tooling Guidance, this contribution was prepared with AI assistance. I have reviewed and verified every change.

@christianeu-db
christianeu-db force-pushed the dataset-scoped-agg-source-measures branch 2 times, most recently from 24e36ac to 4927166 Compare November 13, 2026 20:39
@christianeu-db
christianeu-db force-pushed the dataset-scoped-agg-source-measures branch from 4927166 to 4550972 Compare November 13, 2026 23:28
@christianeu-db
christianeu-db marked this pull request as ready for review November 13, 2026 23:46
@christianeu-db
christianeu-db force-pushed the dataset-scoped-agg-source-measures branch 3 times, most recently from a560c1a to f3ba6b0 Compare November 14, 2026 00:31
…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>
@christianeu-db
christianeu-db force-pushed the dataset-scoped-agg-source-measures branch from f3ba6b0 to 8dcdcea Compare November 14, 2026 00:35
@jbonofre
jbonofre self-requested a review November 16, 2026 15:58
jbonofre
jbonofre previously approved these changes Nov 16, 2026

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

👍

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.name would also reject an empty quoted identifier like AGG(source."").
  • validation/validate.py (validate_metric_scoping): the comment # validate_agg_placement reports an OSSIE_SQL_2026 AGG call here... could be updated since valid AGG(source.measure) calls are now allowed there too.
  • core-spec/expression_language.md: the new changelog row uses 0.3.0.dev instead of 0.3.0.dev0.
  • docs/spec_changes.md: please update the 2026-11-13 date on the #726 row to the actual merge date before/at merge time.

christianeu-db added a commit to christianeu-db/ossie that referenced this pull request Nov 16, 2026
- 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>
@christianeu-db
christianeu-db force-pushed the dataset-scoped-agg-source-measures branch from df60508 to 5bfc2b3 Compare November 16, 2026 21:17
@christianeu-db

Copy link
Copy Markdown
Contributor Author

👍

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.name would also reject an empty quoted identifier like AGG(source."").
  • validation/validate.py (validate_metric_scoping): the comment # validate_agg_placement reports an OSSIE_SQL_2026 AGG call here... could be updated since valid AGG(source.measure) calls are now allowed there too.
  • core-spec/expression_language.md: the new changelog row uses 0.3.0.dev instead of 0.3.0.dev0.
  • docs/spec_changes.md: please update the 2026-11-13 date on the #726 row to the actual merge date before/at merge time.

@jbonofre thanks for the feedback - I've addressed your comments when you have a chance to re-stamp

@justint-db justint-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 @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) and FILTER on AGG are errors, a Snowflake AGG(...) is ignored, and a model-scoped AGG(orders.m) over a dataset-scoped metric that uses AGG(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?

Comment thread core-spec/spec.md Outdated
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.

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.

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 source MUST reject a metric or field that refers to a source measure outside AGG, or that calls AGG on a column that is not a measure of the source, including when the source has 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 |

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.

Small one: this row uses 0.3.0.dev0, and the rows above use 0.2.0.dev. The #703 row just above should also have been 0.3.0.dev, since #703 merged after #691, but I missed it. Could you relabel that row in this PR and use the same form for both? My mistake, sorry.

Comment thread core-spec/spec.md Outdated

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

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.

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

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>
kayemkim
kayemkim previously approved these changes Nov 18, 2026

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

376ffc0 covers the two docs/index.md rows. On this head merged with main the new example validates and the two AGG forms the text calls invalid are rejected, so this looks good to me.

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>
@christianeu-db

Copy link
Copy Markdown
Contributor Author

@kayemkim I merged main to resolve a conflict with #705 in spec.md and docs/spec_changes.md, which dismissed your approval. The PR's own changes are unchanged; both sides of each conflict are kept. Could you re-approve when you have a moment?

@christianeu-db
christianeu-db merged commit 1b1ebb6 into apache:main Nov 20, 2026
14 checks passed
gyli added a commit to gyli/ossie-converters that referenced this pull request Nov 20, 2026
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.
christianeu-db added a commit to christianeu-db/ossie that referenced this pull request Nov 21, 2026
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>
ekut added a commit to Dactopus/dactopus-ossie-clickhouse that referenced this pull request Nov 23, 2026
…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>
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.

4 participants