Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions core-spec/expression_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ will be able to be multi-part and the parts will be separated by the '.' charact
What an identifier refers to depends on where the expression appears:

* In a **field expression**, identifiers refer to columns of the dataset's `source`. A field expression cannot refer to other fields.
* In a **dataset-scoped metric expression**, unqualified identifiers refer to fields declared in that dataset. Identifiers qualified with `source`, as in `source.column`, refer to columns of the dataset's `source`. The `source` qualifier is case insensitive. If the dataset declares a field named `source`, the name refers to that field, and the dataset's metrics cannot refer to source columns.
* In a **model-scoped metric expression**, identifiers must be qualified as `dataset.field` and refer to a field declared in that dataset.
* Model-scoped metric expressions cannot refer to source columns, and no metric expression can refer to another metric. If a field and a source column share a name, an unqualified name in a metric expression refers to the field.
* In a **dataset-scoped metric expression**, unqualified identifiers refer to fields declared in that dataset, even when a source column has the same name. Identifiers qualified with `source`, as in `source.column`, refer to columns of the dataset's `source`. The `source` qualifier is case insensitive. If the dataset declares a field named `source`, the name refers to that field, and the dataset's metrics cannot refer to source columns.
* In a **model-scoped metric expression**, identifiers must be qualified. An identifier written `dataset.field` refers to a field declared in that dataset, not to a source column. Inside `AGG`, an identifier written `dataset.metric` refers to a dataset-scoped metric of that dataset; this `AGG(dataset.metric)` form is the only way to reference a metric.

See [Name Resolution](spec.md#name-resolution) in the core specification.

Expand Down Expand Up @@ -153,7 +152,7 @@ Ossie expressions support the following SQL constructs within any expression:
| `UNION` / `INTERSECT` / `EXCEPT` | Not applicable to expressions |
| DDL statements | Out of scope |
| DML statements | Out of scope |
| `AGG(...)` | Reserved for a future version. See [Measure Evaluation: `AGG`](#measure-evaluation-agg-reserved). |
| `AGG(...)` outside a model-scoped metric | Reserved for a future version. See [Measure Evaluation: `AGG`](#measure-evaluation-agg). |

### Operator Precedence

Expand Down Expand Up @@ -239,7 +238,7 @@ APPROX_PERCENTILE(response_time, 0.95)
### Conditional Aggregations (REQUIRED)

SUM / COUNT aggregation functions support `DISTINCT.`
All aggregations must support filtered aggregation:
All aggregations except `AGG` must support filtered aggregation:

```sql
-- DISTINCT modifier
Expand All @@ -257,12 +256,14 @@ COUNT(*) FILTER (WHERE status = 'completed')

#### `FILTER (WHERE ...)` (REQUIRED)

Every aggregate function must support a postfix `FILTER (WHERE <predicate>)` modifier:
Every aggregate function except `AGG` must support a postfix `FILTER (WHERE <predicate>)` modifier:

```
<aggregate_function>(<args>) FILTER (WHERE <predicate>)
```

In this version, `AGG` takes no `FILTER` clause (see [Measure Evaluation: `AGG`](#measure-evaluation-agg)).

The clause has the semantics of the SQL:2003 `<filter clause>` (optional feature T612): the aggregate considers only the rows for which `<predicate>` succeeds. `FILTER` is applied to the aggregate's input, not as a query `WHERE`, so it never removes output groups. A converter or engine MUST NOT rewrite an intrinsic `FILTER` predicate into a query-level `WHERE` clause when doing so would change group membership.

`FILTER (WHERE ...)` is a modifier on an aggregate expression. It is not the standalone `WHERE` clause listed under [Not Supported in Expressions](#not-supported-in-expressions). The `<predicate>` must reference only fields of the same dataset as the aggregate's arguments.
Expand All @@ -279,11 +280,11 @@ COUNT(DISTINCT customer_id) FILTER (WHERE status = 'completed') --> COUNT(DISTI

A non-matching row contributes `NULL`, which these functions ignore. An `ELSE 0` would count as a real row and would corrupt `AVG`, `MIN`, `MAX`, and `COUNT`. The identity does not hold for an aggregate function that does not ignore `NULL` inputs, such as `ARRAY_AGG`.

### Measure Evaluation: `AGG` (RESERVED)
### Measure Evaluation: `AGG`

Ossie SQL reserves the function name `AGG` for evaluating a measure: a column that holds an aggregate calculation instead of a value for each row. The [Relational Query Interface](relational_semantics.md) defines measures (Section 4) and `AGG` (Section 6), including its valid arguments. In `OSSIE_SQL_2026` expressions, an implementation MUST NOT give `AGG` any other meaning. Expressions in other dialects are not affected. Because function names should be case insensitive, the reservation covers every capitalization, such as `agg` and `Agg`.

This version reserves the name only. `OSSIE_SQL_2026` expressions MUST NOT call `AGG` until a future version defines where model declarations may use it.
A model-scoped metric may use `AGG` to evaluate a dataset-scoped metric, as in `AGG(orders.total_revenue)`. The argument is a single `dataset.metric` reference, and the call takes no `FILTER (WHERE ...)` clause (see [Metric Scoping](spec.md#metric-scoping)). `OSSIE_SQL_2026` expressions MUST NOT call `AGG` anywhere else until a future version defines it.
Comment thread
justint-db marked this conversation as resolved.
Resolved

**Compatibility.** Earlier versions of this specification did not reserve `AGG`, so an older `OSSIE_SQL_2026` expression might call a different function by that name. To update it, rename that function or write the expression in a vendor dialect. Reserving `AGG` as a function name does not prevent a field, metric, or column from being named `agg`.

Expand Down Expand Up @@ -929,6 +930,7 @@ tooling aid; where it and this document differ, this document is authoritative.
|:----------|:-----------| :---- |
| 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)` |

---

Expand Down
2 changes: 1 addition & 1 deletion core-spec/ossie-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@
"items": {
"$ref": "#/$defs/Metric"
},
"description": "Model-scoped metrics: aggregations that span datasets or need to be aware of cross-dataset concepts. Expressions reference fields by multi-part name (dataset.field) and may combine fields from more than one dataset. Names must be unique across the model. See core-spec/spec.md#metric-scoping."
"description": "Model-scoped metrics: aggregations that span datasets or need to be aware of cross-dataset concepts. Expressions reference fields by multi-part name (dataset.field) and may combine fields from more than one dataset. They may evaluate a dataset-scoped metric with AGG(dataset.metric). Names must be unique across the model. See core-spec/spec.md#metric-scoping."
},
"nonportable_extensions": {
"type": "array",
Expand Down
9 changes: 6 additions & 3 deletions core-spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ There are two types of metrics:
- **Dataset-scoped** (`datasets[].metrics`): aggregations that belong to a single dataset. The expression references that dataset's declared fields by single-part name and its source columns as `source.column`, and resolves entirely within it.
- **Model-scoped** (the model-root `metrics`): aggregations that span datasets across the model. The expression references fields by multi-part `dataset.field` name and may combine fields across datasets.

Model-scoped metric expressions refer to dataset fields. Dataset-scoped metric expressions refer to the dataset's fields and may also refer to columns of its `source`, written as `source.column`. No metric expression can refer to another metric. See [Metric Scoping](#metric-scoping) for the rules governing each and [Name Resolution](#name-resolution) for how names resolve.
Model-scoped metric expressions refer to dataset fields. Dataset-scoped metric expressions refer to the dataset's fields and may also refer to columns of its `source`, written as `source.column`. A model-scoped metric can refer to a dataset-scoped metric only through `AGG(dataset.metric)`. No other metric references are supported. See [Metric Scoping](#metric-scoping) for the rules governing each and [Name Resolution](#name-resolution) for how names resolve.

### Schema

Expand Down Expand Up @@ -492,6 +492,7 @@ A metric may be defined on an individual dataset or at the model level. Both pla
3. `source` is not reserved: a field or dataset may be named `source`. If a dataset declares a field named `source`, the name `source` refers to that field, so the dataset's metrics cannot refer to source columns.
4. Within a dataset, declared field names and dataset-scoped metric names share one namespace and MUST all be unique across the two.
5. A model-scoped metric's expression references fields by multi-part name, `dataset.field`, and MAY combine fields from more than one dataset. It cannot reference source columns.
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.

**Example: dataset-scoped metrics**

Expand Down Expand Up @@ -659,7 +660,7 @@ exposes nothing to keys, relationships, or model-scoped metrics.
| `relationships[].from_columns` | A field of the `from` dataset |
| `relationships[].to_columns` | A field of the `to` dataset |
| Dataset-scoped metric `expression` (`datasets[].metrics`) | Unqualified: a field of the same dataset. `source.column`: a column of its `source` |
| Model-scoped metric `expression` (model-root `metrics`) | A field of the named dataset, qualified as `dataset.field` |
| Model-scoped metric `expression` (model-root `metrics`) | A field of the named dataset, qualified as `dataset.field`. Inside `AGG`: a dataset-scoped metric of the named dataset, qualified as `dataset.metric` |

For example, a dataset can expose the source column `o_totalprice` as the
field `total_amount`, and derive `total_usd` from `total_cents`:
Expand Down Expand Up @@ -722,7 +723,8 @@ This version of the specification does not support:

- a field expression that refers to another field, in the same or another
dataset;
- a metric expression that refers to another metric;
- a metric expression that refers to another metric, except a model-scoped
metric that evaluates a dataset-scoped metric with `AGG(dataset.metric)`;
- a dataset that uses another dataset as its `source`.

Future versions may add these.
Expand Down Expand Up @@ -874,6 +876,7 @@ including rationale and migration impact for consumers, see the
- Breaking: in `OSSIE_SQL_2026`, `ROUND(x, d)` breaks ties half away from zero and `/` is true division (`5 / 2` is `2.5`). Earlier versions did not specify either, and engines differ on both; an engine with another default must evaluate an equivalent expression. See [Numeric Semantics](expression_language.md#numeric-semantics).
- Breaking: metric expressions, `primary_key`, `unique_keys`, and relationship `from_columns`/`to_columns` refer to dataset fields, not columns of the dataset's `source`. Model-scoped metric expressions must use qualified `dataset.field` references; dataset-scoped metric expressions use unqualified field names. Earlier versions did not specify this, and some converters read these names as source columns; those converters must now resolve them as fields. See [Name Resolution](#name-resolution).
- Dataset-scoped metric expressions may reference columns of the dataset's `source`, written as `source.column`. See [Metric Scoping](#metric-scoping).
- Model-scoped metric expressions may evaluate a dataset-scoped metric with `AGG(dataset.metric)`. See [Metric Scoping](#metric-scoping).
- **0.1.1** (2025-12-11): Initial release
- Core semantic model structure
- Support for datasets, relationships, fields, and metrics
Expand Down
3 changes: 2 additions & 1 deletion core-spec/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ metrics:
# not columns of a dataset's source
# Dataset-scoped metrics reference unqualified field names, and may also
# reference source columns as source.column
# Metrics cannot reference other metrics
# A model-scoped metric can refer to a dataset-scoped metric only through
# AGG(dataset.metric); no other metric references are supported
# At least one of the portable `sql` or `dialects` must be present; either may stand alone
expression:
sql: string # Primary, portable OSSIE_SQL_2026 form, e.g., "SUM(orders.sales)", "AVG(orders.amount)"
Expand Down
2 changes: 1 addition & 1 deletion core-spec/wide_table_semantics.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ The process is: split metrics into components (Section 5.2), check reachability
A **component** is an aggregate expression over fields of one **home dataset**. A row of a home dataset is a **home row**. The lowering defines each component as an internally named measure of its home dataset, and evaluates it with one `AGG()` call (Layer 2 Sections 3.4 and 5.1).

- A dataset-scoped metric `D.m` is one component with home dataset `D`, whatever its expression. The expression may combine several aggregate calls, such as `SUM(revenue) / NULLIF(COUNT(*), 0)`, and may use `COUNT(*)` with or without a `FILTER (WHERE ...)` clause. It is still one measure and one `AGG()` call.
- In a model-scoped metric, each aggregate call is a separate component. Its home dataset is the one dataset that its arguments read. Its `FILTER (WHERE ...)` clause MUST read only that dataset (Layer 1, `FILTER (WHERE ...)`), and it does not determine the home dataset. The metric's scalar expression is evaluated from the component values afterward (Section 5.5), as in Layer 1 and Layer 2 (Layer 2 Section 8.1).
- In a model-scoped metric, each aggregate call is a separate component. Its home dataset is the one dataset that its arguments read. Its `FILTER (WHERE ...)` clause MUST read only that dataset (Layer 1, `FILTER (WHERE ...)`), and it does not determine the home dataset. The metric's scalar expression is evaluated from the component values afterward (Section 5.5), as in Layer 1 and Layer 2 (Layer 2 Section 8.1). An `AGG(D.m)` call is one component with home dataset `D`, evaluated as the dataset-scoped metric `D.m`.

The **empty-input value** of a component is its value over no rows. For example, `SUM(x)` gives NULL, `COUNT(*)` gives 0, `COALESCE(SUM(x), 0)` gives 0, and `SUM(revenue) / NULLIF(COUNT(*), 0)` gives NULL. It equals the value of `AGG()` for an empty input set (Layer 2 Section 8.1). Section 5.5 requires it.

Expand Down
2 changes: 1 addition & 1 deletion validation/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# dependencies = [
# "jsonschema>=4.26.0",
# "pyyaml>=6.0.3",
# "sqlglot>=30.12.0",
# "sqlglot>=30.19.0",
# ]
# ///

Expand Down
Loading
Loading