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
4 changes: 2 additions & 2 deletions core-spec/expression_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ 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**, identifiers are unqualified and refer to fields declared in that dataset.
* 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.
* Metric expressions cannot refer to columns of the dataset's `source` or to other metrics. If a field and a source column share a name, a metric expression refers to the field.
* 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.

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

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 @@ -252,7 +252,7 @@
"items": {
"$ref": "#/$defs/Metric"
},
"description": "Dataset-scoped metrics: aggregations evaluated at this dataset's row level, for measures that semantically belong to the dataset. The expression references this dataset's declared fields by single-part (unqualified) name and must resolve entirely within this dataset; an expression that must reference another dataset belongs in the model-scoped metrics instead. Referencing a column of the source that is not a declared field is a future extension. Within the dataset, field names and metric names share one namespace and must all be unique across the two."
"description": "Dataset-scoped metrics: aggregations evaluated at this dataset's row level, for measures that semantically belong to the dataset. The expression references this dataset's declared fields by single-part (unqualified) name, and columns of the dataset's source as source.column. It must resolve entirely within this dataset; an expression that must reference another dataset belongs in the model-scoped metrics instead. Within the dataset, field names and metric names share one namespace and must all be unique across the two."
},
"custom_extensions": {
"type": "array",
Expand Down
41 changes: 24 additions & 17 deletions core-spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Logical datasets represent business entities or concepts (fact and dimension tab
| `description` | string | No | Human-readable description |
| `ai_context` | string/object | No | Additional context for AI tools (e.g., synonyms, common terms) |
| `fields` | array | No | Row-level attributes for grouping, filtering, and metric expressions |
| `metrics` | array | No | Dataset-scoped metrics that semantically belong to this dataset; expressions reference its declared fields by single-part name and resolve entirely within it. See [Metric Scoping](#metric-scoping). |
| `metrics` | array | No | Dataset-scoped metrics that semantically belong to this dataset; expressions reference its declared fields by single-part name and its source columns as `source.column`, and resolve entirely within it. See [Metric Scoping](#metric-scoping). |
| `custom_extensions` | array | No | Vendor-specific attributes |

### Primary Key Examples
Expand Down Expand Up @@ -418,10 +418,10 @@ Quantitative measures defined on business data, representing key calculations li

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 resolves entirely within it.
- **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.

Metric expressions refer to dataset fields. They cannot refer to columns of a dataset's `source` or to other metrics. 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`. 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.

### Schema

Expand Down Expand Up @@ -490,17 +490,17 @@ A metric may be defined on an individual dataset or at the model level. Both pla

| | Dataset-scoped (`datasets[].metrics`) | Model-scoped (model-root `metrics`) |
|---|---|---|
| Expression references | This dataset's declared fields, single-part: `field` | Fields of any dataset, multi-part: `dataset.field` |
| Expression references | This dataset's declared fields, single-part: `field`; columns of its `source`: `source.column` | Fields of any dataset, multi-part: `dataset.field` |
| Spans datasets | No | Yes, via relationships |
| Referenced as | `dataset_name.metric_name` | `metric_name` |

**Rules**

1. A dataset-scoped metric's expression MUST reference only the declared fields of the dataset that declares it, written by single-part (unqualified) name: `SUM(net_amount)`. It MUST NOT reference a field of another dataset; a metric that must do so is model-scoped and MUST be declared in the model-root `metrics`.
2. Within a dataset, declared field names and dataset-scoped metric names share one namespace and MUST all be unique across the two.
3. A model-scoped metric's expression references fields by multi-part name, `dataset.field`, and MAY combine fields from more than one dataset.

> Referencing a column of a dataset's `source` that is not a declared field is a future extension. Today a dataset-scoped expression reaches only the dataset's declared fields.
1. A dataset-scoped metric's expression MUST reference only the declared fields of the dataset that declares it, written by single-part (unqualified) name: `SUM(net_amount)`, and the columns of that dataset's `source`, written as `source.column`: `SUM(source.quantity)`. It MUST NOT reference a field of another dataset; a metric that must do so is model-scoped and MUST be declared in the model-root `metrics`.
2. In a dataset-scoped metric, an unqualified name MUST be a declared field of the dataset. A source column MUST be qualified with `source`, which is case insensitive: `source.quantity` and `SOURCE.quantity` name the same column. `source.amount` refers to the source column even when the dataset declares a field named `amount`.
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.

**Example: dataset-scoped metrics**

Expand Down Expand Up @@ -612,17 +612,20 @@ An Ossie model has three levels of names:
fields are its public interface.
3. **Metrics.** Dataset-scoped metrics (`datasets[].metrics`) and model-scoped
metrics (the model-root `metrics`), defined in terms of dataset fields.
Dataset-scoped metrics may also use their own dataset's source columns,
written as `source.column`.

Metrics and relationships do not reach through a dataset to its source
columns, so a dataset that declares no fields exposes nothing to them.
Apart from a dataset's own fields and dataset-scoped metrics, nothing reaches
through a dataset to its source columns. A dataset that declares no fields
exposes nothing to keys, relationships, or model-scoped metrics.

| Where the name appears | What it refers to |
|------------------------|-------------------|
| Field `expression` | A column of the same dataset's `source` |
| `datasets[].primary_key`, `datasets[].unique_keys` | A field of the same dataset |
| `relationships[].from_columns` | A field of the `from` dataset |
| `relationships[].to_columns` | A field of the `to` dataset |
| Dataset-scoped metric `expression` (`datasets[].metrics`) | A field of the same dataset, unqualified |
| 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` |

For example, a dataset can expose the source column `o_totalprice` as the
Expand Down Expand Up @@ -677,17 +680,20 @@ In this model, these references are invalid:
- `SUM(total_usd)` in a model-scoped metric, because model-scoped metric
expressions must qualify field names with the dataset name, even when only
one dataset has a field with that name.
- `SUM(o_totalprice)` in a metric declared on `orders`, because dataset-scoped
metrics reach only the dataset's declared fields, not its source columns.
- `SUM(o_totalprice)` in a metric declared on `orders`, because an unqualified
name in a dataset-scoped metric must be a declared field. Write
`SUM(source.o_totalprice)` to refer to the source column.

If a field and a source column share a name (as in a field `amount` defined as
`amount`), the name refers to the source column inside that dataset's field
expressions and to the field everywhere else.
expressions and to the field everywhere else. A dataset-scoped metric can
still refer to the source column as `source.amount`.

Consumers that generate SQL against the source tables, rather than against a
layer that exposes the fields, must replace each field reference with the
field's expression. For example, `SUM(orders.total_amount)` compiles to
`SUM(o_totalprice)` over `sales.public.orders`.
field's expression, and each `source.column` reference with `column`. For
example, `SUM(orders.total_amount)` compiles to `SUM(o_totalprice)` over
`sales.public.orders`.

This version of the specification does not support:

Expand Down Expand Up @@ -854,6 +860,7 @@ ai_context:
- **0.2.0.dev0** (Unreleased): In-development next minor release. Schema is mutable; do not depend on this version in production.
- Breaking: each standalone document contains one model directly at the root; the `semantic_model` array is removed.
- 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).
- **0.1.1** (2025-12-11): Initial release
- Core semantic model structure
- Support for datasets, relationships, fields, and metrics
Expand Down
15 changes: 9 additions & 6 deletions core-spec/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ datasets:
#
# Rules (see core-spec/spec.md#metric-scoping):
# - The expression MUST reference only this dataset's declared fields,
# written by single-part (unqualified) name: SUM(net_amount).
# written by single-part (unqualified) name: SUM(net_amount), and the
# columns of its source, written as source.column: SUM(source.quantity).
# - The source qualifier is case insensitive. If the dataset declares a
# field named source, the name refers to that field, and the metrics
# cannot refer to source columns.
# - It MUST NOT reference another dataset; a metric that must do so is
# model-scoped and belongs in the model-root metrics.
# - Referencing a column of the source that is not a declared field is a
# future extension.
# - Field names and metric names share one namespace; every name MUST be
# unique across the two.
# - Referenced from outside the dataset as dataset_name.metric_name.
Expand Down Expand Up @@ -278,9 +280,10 @@ metrics:
# Required: Expression definition with dialect support
# Supports multiple SQL dialects for cross-platform compatibility
# Each metric can have expressions in different dialects for portability
# References are dataset fields, not columns of the dataset's source
# Model-scoped metrics qualify them as dataset.field; dataset-scoped
# metrics use unqualified field names
# Model-scoped metrics reference dataset fields, qualified as dataset.field,
# 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
# At least one of the portable `sql` or `dialects` must be present; either may stand alone
expression:
Expand Down
3 changes: 2 additions & 1 deletion python/src/ossie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ class OssieDataset(BaseModel):
description: Optional[str] = None
ai_context: Optional[OssieAIContext] = None
fields: Optional[list[OssieField]] = None
# Dataset-scoped: expressions reference this dataset's declared fields by single-part (unqualified) name.
# Dataset-scoped: expressions reference this dataset's declared fields by single-part
# (unqualified) name and its source columns as source.column.
metrics: Optional[list[OssieMetric]] = None
custom_extensions: Optional[list[OssieCustomExtension]] = None

Expand Down
50 changes: 47 additions & 3 deletions validation/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,49 @@ def test_dataset_scoped_metric_rejects_an_undeclared_field() -> None:
assert any(error.startswith("[Scope]") and "'quantity'" in error for error in errors)


def test_dataset_scoped_metric_accepts_source_columns() -> None:
# The source qualifier is case insensitive, and reaches a column that a field
# of the same name shadows.
doc = _orders_with(
fields=[{"name": "amount", "expression": _ds_expr("amount * 100")}],
metrics=[
{"name": "total_amount", "expression": _ds_expr("SUM(source.amount)")},
{"name": "total_qty", "expression": _ds_expr("SUM(SOURCE.quantity)")},
{"name": "total_cents", "expression": _ds_expr("SUM(amount)")},
],
)

assert _VALIDATE.validate_metric_scoping(doc) == []


def test_dataset_scoped_metric_rejects_a_three_part_source_reference() -> None:
doc = _orders_with(
metrics=[{"name": "total", "expression": _ds_expr("SUM(source.orders.amount)")}],
)

errors = _VALIDATE.validate_metric_scoping(doc)

assert any(error.startswith("[Scope]") and "'source'" in error for error in errors)


def test_dataset_scoped_metric_rejects_source_columns_when_a_field_is_named_source() -> None:
# A field named source takes precedence over the source qualifier.
doc = _orders_with(
fields=[{"name": "source", "expression": _ds_expr("channel")}],
metrics=[
{"name": "sources", "expression": _ds_expr("COUNT(DISTINCT source)")},
{"name": "total_amount", "expression": _ds_expr("SUM(source.amount)")},
],
)

assert _VALIDATE.validate_metric_scoping(doc) == [
"[Scope] Dataset-scoped metric 'orders.total_amount' in model 'm' (OSSIE_SQL_2026) "
"uses source.column, but dataset 'orders' declares a field named 'source', which "
"takes precedence over the source qualifier. This dataset's metrics cannot refer "
"to source columns."
]


def test_dataset_scoped_metric_count_star_references_no_field() -> None:
doc = _orders_with(metrics=[{"name": "n", "expression": _ds_expr("COUNT(*)")}])

Expand Down Expand Up @@ -1781,8 +1824,9 @@ def test_dataset_scoped_metric_portable_sql_rejects_a_cross_dataset_reference()
assert _VALIDATE.validate_metric_scoping(doc) == [
"[Scope] Dataset-scoped metric 'orders.bad' in model 'm' (OSSIE_SQL_2026) uses "
"qualified reference(s) 'customers'. A dataset-scoped metric expression must "
"reference the dataset's fields by single-part (unqualified) name; a metric that "
"must reference another dataset belongs in the model-root metrics."
"reference the dataset's fields by single-part (unqualified) name and its source "
"columns as source.column; a metric that must reference another dataset belongs "
"in the model-root metrics."
]


Expand All @@ -1795,7 +1839,7 @@ def test_dataset_scoped_metric_portable_sql_rejects_an_undeclared_field() -> Non
assert _VALIDATE.validate_metric_scoping(doc) == [
"[Scope] Dataset-scoped metric 'orders.total_qty' in model 'm' (OSSIE_SQL_2026) "
"references undeclared field(s) 'quantity'. A dataset-scoped metric may reference "
"only the dataset's declared fields."
"only the dataset's declared fields, and its source columns as source.column."
]


Expand Down
Loading
Loading