Skip to content
Closed
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
16 changes: 16 additions & 0 deletions converters/dbt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ manifest_json = result.output.model_dump_json(by_alias=True, exclude_none=True,
- Anything else → SIMPLE metric with the raw expression stored verbatim
- Time dimensions always receive `TimeGranularity.DAY` (Ossie carries no granularity field)

Non-empty dbt `config.meta` is carried on the matching dataset, field, or metric as
a `DBT` custom extension. For example, `config.meta: {unit: USD}` becomes:

```yaml
custom_extensions:
- vendor_name: DBT
data: '{"unit": "USD"}'
```

The reverse converter restores this JSON object to `config.meta`; empty metadata is
omitted.

## Development

```bash
Expand All @@ -146,6 +158,10 @@ uv sync
uv run pytest
```

The test dependency and lockfile pin dbt Core and the DuckDB adapter so
`uv run pytest` always executes the real-dbt `config.meta` E2E; the test fails if
the environment resolves to the dbt Cloud CLI instead of dbt Core.

Generate new Syrupy snapshots:
```bash
uv run pytest --snapshot-update
Expand Down
1 change: 1 addition & 0 deletions converters/dbt/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ build-backend = "hatchling.build"

[dependency-groups]
dev = [
"dbt-duckdb==1.11.0",
"pytest>=8.0",
"syrupy>=4.0",
]
Expand Down
58 changes: 58 additions & 0 deletions converters/dbt/src/ossie_dbt/meta_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import json
from typing import Optional, Sequence

from ossie import OssieCustomExtension
from metricflow_semantic_interfaces.implementations.element_config import (
PydanticSemanticLayerElementConfig,
)


def _meta_to_custom_extensions(
config: Optional[PydanticSemanticLayerElementConfig],
) -> Optional[list[OssieCustomExtension]]:
"""Serialize non-empty dbt ``config.meta`` into Ossie's DBT extension."""
meta = config.meta if config is not None else None
if not meta:
return None
return [
OssieCustomExtension(
vendor_name="DBT",
data=json.dumps(meta, ensure_ascii=False, sort_keys=True),
)
]


def _meta_config_from_custom_extensions(
extensions: Optional[Sequence[OssieCustomExtension]],
) -> Optional[PydanticSemanticLayerElementConfig]:
"""Restore dbt ``config.meta`` from an Ossie DBT custom extension."""
dbt_extensions = [extension for extension in extensions or [] if extension.vendor_name == "DBT"]
if not dbt_extensions:
return None
if len(dbt_extensions) > 1:
raise ValueError("Expected at most one DBT custom extension for config.meta")

try:
meta = json.loads(dbt_extensions[0].data)
except json.JSONDecodeError as exc:
raise ValueError("DBT custom extension data must contain a JSON object for config.meta") from exc
if not isinstance(meta, dict):
raise ValueError("DBT custom extension data must contain a JSON object for config.meta")
return PydanticSemanticLayerElementConfig(meta=meta)
6 changes: 6 additions & 0 deletions converters/dbt/src/ossie_dbt/msi_to_ossie.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from ossie_dbt.converter_issues import ConverterIssue, ConverterIssueType, ConverterResult
from ossie_dbt.expression_utils import _is_constant_expr
from ossie_dbt.filter_utils import _collect_filter_sql, _merge_filter_sqls
from ossie_dbt.meta_utils import _meta_to_custom_extensions

from metricflow_semantic_interfaces.enum_extension import assert_values_exhausted
from metricflow_semantic_interfaces.implementations.semantic_manifest import PydanticSemanticManifest
Expand Down Expand Up @@ -143,6 +144,7 @@ def convert(
name=metric.name,
expression=self._make_expression(expr),
description=metric.description,
custom_extensions=_meta_to_custom_extensions(metric.config),
)
)
if len(manifest.semantic_models) > 1 and self._aggregates_a_constant(metric):
Expand Down Expand Up @@ -185,6 +187,7 @@ def _convert_semantic_model(self, sm: SemanticModel) -> OssieDataset:
unique_keys=unique_keys if unique_keys else None,
description=sm.description,
fields=fields if fields else None,
custom_extensions=_meta_to_custom_extensions(sm.config),
)

def _convert_dimension(self, dim: Dimension) -> OssieField:
Expand All @@ -197,6 +200,7 @@ def _convert_dimension(self, dim: Dimension) -> OssieField:
dimension=OssieDimension(is_time=is_time),
label=dim.label,
description=dim.description,
custom_extensions=_meta_to_custom_extensions(dim.config),
)

def _convert_entity(self, entity: Entity) -> OssieField:
Expand All @@ -207,6 +211,7 @@ def _convert_entity(self, entity: Entity) -> OssieField:
expression=self._make_expression(expr),
label=entity.label,
description=entity.description,
custom_extensions=_meta_to_custom_extensions(entity.config),
)

def _convert_measure(self, measure: Measure) -> OssieField:
Expand All @@ -217,6 +222,7 @@ def _convert_measure(self, measure: Measure) -> OssieField:
expression=self._make_expression(expr),
label=measure.label,
description=measure.description,
custom_extensions=_meta_to_custom_extensions(measure.config),
)

@staticmethod
Expand Down
33 changes: 24 additions & 9 deletions converters/dbt/src/ossie_dbt/ossie_to_msi.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
_strip_qualifier,
_try_parse_ratio,
)
from ossie_dbt.meta_utils import _meta_config_from_custom_extensions

from metricflow_semantic_interfaces.implementations.elements.dimension import (
PydanticDimension,
Expand All @@ -45,6 +46,9 @@
from metricflow_semantic_interfaces.implementations.elements.measure import (
PydanticMeasureAggregationParameters,
)
from metricflow_semantic_interfaces.implementations.element_config import (
PydanticSemanticLayerElementConfig,
)
from metricflow_semantic_interfaces.implementations.metric import (
PydanticMetric,
PydanticMetricAggregationParams,
Expand Down Expand Up @@ -159,6 +163,7 @@ def _convert_dataset(
entities=entities,
dimensions=dimensions,
measures=[],
config=_meta_config_from_custom_extensions(dataset.custom_extensions),
)

@staticmethod
Expand Down Expand Up @@ -209,6 +214,7 @@ def _classify_field(
Aggregation info lives on metrics (`metric_aggregation_params`), not on
semantic model measures, so there is no measure classification step.
"""
config = _meta_config_from_custom_extensions(field.custom_extensions)
if field.name in primary_key_cols:
entities.append(
PydanticEntity(
Expand All @@ -218,7 +224,7 @@ def _classify_field(
description=field.description,
label=field.label,
role=None,
config=None,
config=config,
)
)
return
Expand All @@ -231,7 +237,7 @@ def _classify_field(
description=field.description,
label=field.label,
role=None,
config=None,
config=config,
)
)
return
Expand All @@ -244,7 +250,7 @@ def _classify_field(
description=field.description,
label=field.label,
role=None,
config=None,
config=config,
)
)
return
Expand All @@ -258,7 +264,7 @@ def _classify_field(
expr=expr_or_none,
description=field.description,
label=field.label,
config=None,
config=config,
)
)
return
Expand All @@ -270,7 +276,7 @@ def _classify_field(
expr=expr_or_none,
description=field.description,
label=field.label,
config=None,
config=config,
)
)

Expand All @@ -284,7 +290,15 @@ def _convert_metrics(self, ossie_sm: OssieSemanticModel) -> Tuple[List[PydanticM
for metric in ossie_sm.metrics or []:
expr_str = self._get_expression(metric.expression)
try:
metrics.extend(self._convert_metric(metric.name, expr_str, metric.description, ossie_sm.datasets))
metrics.extend(
self._convert_metric(
metric.name,
expr_str,
metric.description,
ossie_sm.datasets,
config=_meta_config_from_custom_extensions(metric.custom_extensions),
)
)
except _UnresolvedRowCountDataset:
# Also drops a ratio and its sub-metrics as a whole, so nothing references a missing metric.
issues.append(
Expand All @@ -298,6 +312,7 @@ def _convert_metric(
expr_str: str,
description: Optional[str],
datasets: List[OssieDataset],
config: Optional[PydanticSemanticLayerElementConfig] = None,
) -> List[PydanticMetric]:
"""Return one or more PydanticMetric objects for the given Ossie expression.

Expand Down Expand Up @@ -342,7 +357,7 @@ def _convert_metric(
),
filter=None,
metadata=None,
config=None,
config=config,
)
]

Expand All @@ -364,7 +379,7 @@ def _convert_metric(
),
filter=None,
metadata=None,
config=None,
config=config,
)
return [*num_metrics, *den_metrics, ratio_metric]

Expand Down Expand Up @@ -396,7 +411,7 @@ def _convert_metric(
),
filter=None,
metadata=None,
config=None,
config=config,
)
]

Expand Down
Loading
Loading