You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ossie.apache.org by jb...@apache.org on 2026/09/25 12:34:51 UTC

(ossie) branch main updated: fix(dbt): read OSSIE_SQL_2026 expressions (#464)

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ossie.git


The following commit(s) were added to refs/heads/main by this push:
     new ac54fbf1 fix(dbt): read OSSIE_SQL_2026 expressions (#464)
ac54fbf1 is described below

commit ac54fbf19fd56f85afce3eb064c9d452659cf34c
Author: RyutoYoda❄️ <98...@users.noreply.github.com>
AuthorDate: Fri Sep 25 21:34:44 2026 +0900

    fix(dbt): read OSSIE_SQL_2026 expressions (#464)
    
    * fix(dbt): read OSSIE_SQL_2026 expressions instead of picking by position
    
    OSSIE_SQL_2026 (#439, #440) was not recognised anywhere in converters/dbt.
    _get_expression() matched only self._dialect -- always ANSI_SQL, since the
    constructor argument is never passed -- and otherwise returned dialects[0].
    Expression.dialects carries no ordering constraint, so an expression written
    in OSSIE_SQL_2026 alongside a vendor dialect resolved by array position: with
    the vendor entry first a Snowflake-only column reference was written into the
    MetricFlow output as portable SQL, with no warning and no ConverterIssue.
    expression_language.md asks implementations to always support the Ossie
    dialect and to choose deterministically between dialects; this did neither.
    
    Add OSSIE_SQL_2026 to the chain as an ANSI_SQL equivalent (self._dialect >
    OSSIE_SQL_2026 > dialects[0]), mirroring #443/#446/#447 for orionbelt,
    databricks and snowflake. The change is additive: expressions carrying
    ANSI_SQL keep their current behaviour.
    
    Add regression tests for the metric and field paths in either dialect order,
    for ANSI_SQL keeping precedence over OSSIE_SQL_2026, and for the positional
    fallback when no portable dialect is present. tests/helpers.py only builds
    single-dialect expressions, so this is the first multi-dialect coverage in
    the Ossie -> dbt direction.
    
    Partial fix for #461 (_get_expression only); the positional read at
    ossie_to_msi.py:401 is left for a follow-up.
    
    * fix(dbt): apply the dialect preference in _find_dataset_for_col too
    
    Review follow-ups on #464.
    
    _find_dataset_for_col still read field.expression.dialects[0] positionally,
    so it and _get_expression could resolve the same field to different text and
    a metric could be attributed to the wrong dataset. It now goes through
    _get_expression, which makes it an instance method; its only call site
    already went through self.
    
    A dialect may also be listed more than once, since Expression.dialects has
    minItems but no uniqueItems. The first OSSIE_SQL_2026 entry now wins, as the
    converter's own dialect already does. The loop is not cut short on that match
    because the converter's dialect outranks OSSIE_SQL_2026 and may appear later
    in the list.
---
 converters/dbt/src/ossie_dbt/ossie_to_msi.py |  25 ++++-
 converters/dbt/tests/test_ossie_to_msi.py    | 147 ++++++++++++++++++++++++++-
 2 files changed, 168 insertions(+), 4 deletions(-)

diff --git a/converters/dbt/src/ossie_dbt/ossie_to_msi.py b/converters/dbt/src/ossie_dbt/ossie_to_msi.py
index 4374d61e..ca13eab5 100644
--- a/converters/dbt/src/ossie_dbt/ossie_to_msi.py
+++ b/converters/dbt/src/ossie_dbt/ossie_to_msi.py
@@ -374,8 +374,8 @@ class OssieToMSIConverter:
     # Helpers
     # ------------------------------------------------------------------
 
-    @staticmethod
     def _find_dataset_for_col(
+        self,
         raw_expr_str: str,
         bare_col: str,
         datasets: List[OssieDataset],
@@ -398,17 +398,36 @@ class OssieToMSIConverter:
             for field in dataset.fields or []:
                 if field.name == bare_col:
                     return dataset.name
-                field_expr = field.expression.dialects[0].expression if field.expression.dialects else ""
+                field_expr = self._get_expression(field.expression)
                 if _strip_qualifier(field_expr) == bare_col:
                     return dataset.name
 
         return datasets[0].name if datasets else ""
 
     def _get_expression(self, ossie_expr: OssieExpression) -> str:
-        """Return the expression string for the preferred dialect (fallback: first available)."""
+        """Return the expression string for the preferred dialect.
+
+        Preference order: the converter's dialect, then OSSIE_SQL_2026, then the
+        first entry available. OSSIE_SQL_2026 is Ossie's portable expression
+        language, based on ANSI SQL:2003 Core, so it is treated as an ANSI_SQL
+        equivalent rather than left to the positional fallback.
+
+        `dialects` has no `uniqueItems` constraint, so one dialect may appear
+        more than once. The first entry wins in that case, as it already does
+        for the converter's own dialect. The scan is not cut short on an
+        OSSIE_SQL_2026 match because the converter's dialect outranks it and
+        may still appear further down the list.
+        """
+        ossie_sql_expr: Optional[str] = None
+
         for dialect_expr in ossie_expr.dialects:
             if dialect_expr.dialect is self._dialect:
                 return dialect_expr.expression
+            if dialect_expr.dialect is OssieDialect.OSSIE_SQL_2026 and ossie_sql_expr is None:
+                ossie_sql_expr = dialect_expr.expression
+
+        if ossie_sql_expr is not None:
+            return ossie_sql_expr
         return ossie_expr.dialects[0].expression if ossie_expr.dialects else ""
 
     @staticmethod
diff --git a/converters/dbt/tests/test_ossie_to_msi.py b/converters/dbt/tests/test_ossie_to_msi.py
index c68e547d..bcd4769a 100644
--- a/converters/dbt/tests/test_ossie_to_msi.py
+++ b/converters/dbt/tests/test_ossie_to_msi.py
@@ -20,7 +20,16 @@
 import pytest
 from syrupy.assertion import SnapshotAssertion
 
-from ossie import OssieDataType, OssieDimension
+from ossie import (
+    OssieDataType,
+    OssieDialect,
+    OssieDialectExpression,
+    OssieDimension,
+    OssieDocument,
+    OssieExpression,
+    OssieField,
+    OssieMetric,
+)
 from ossie_dbt.msi_to_ossie import MSIToOssieConverter
 from ossie_dbt.ossie_to_msi import OssieToMSIConverter
 from metricflow_semantic_interfaces.implementations.elements.measure import (
@@ -456,6 +465,142 @@ class TestOssieToMSIMetricConversion:
         assert m.type_params.expr == "amount"
 
 
+def _multi_dialect_expr(*pairs: tuple[OssieDialect, str]) -> OssieExpression:
+    """Build an expression carrying more than one dialect, in the given order."""
+    return OssieExpression(
+        dialects=[OssieDialectExpression(dialect=dialect, expression=expr) for dialect, expr in pairs]
+    )
+
+
+_SNOWFLAKE_METRIC = (OssieDialect.SNOWFLAKE, "SUM(orders.amt_snowflake_only)")
+_OSSIE_SQL_METRIC = (OssieDialect.OSSIE_SQL_2026, "SUM(orders.amount)")
+_ANSI_METRIC = (OssieDialect.ANSI_SQL, "SUM(orders.amount_ansi)")
+
+
+def _doc_with_metric_expression(expression: OssieExpression) -> OssieDocument:
+    return _ossie_doc(
+        datasets=[_ossie_dataset("orders", fields=[_ossie_field("amount")])],
+        metrics=[OssieMetric(name="revenue", expression=expression)],
+    )
+
+
+class TestOssieToMSIDialectSelection:
+    @pytest.mark.parametrize(
+        "order",
+        [
+            (_OSSIE_SQL_METRIC, _SNOWFLAKE_METRIC),
+            (_SNOWFLAKE_METRIC, _OSSIE_SQL_METRIC),
+        ],
+        ids=["ossie_sql_first", "vendor_first"],
+    )
+    def test_ossie_sql_2026_wins_over_a_vendor_dialect_in_either_order(
+        self, order: tuple[tuple[OssieDialect, str], ...]
+    ) -> None:
+        doc = _doc_with_metric_expression(_multi_dialect_expr(*order))
+
+        result = OssieToMSIConverter().convert(doc).output
+
+        assert result.metrics[0].type_params.expr == "amount"
+
+    @pytest.mark.parametrize(
+        "order",
+        [
+            (_ANSI_METRIC, _OSSIE_SQL_METRIC),
+            (_OSSIE_SQL_METRIC, _ANSI_METRIC),
+        ],
+        ids=["ansi_first", "ossie_sql_first"],
+    )
+    def test_ansi_sql_still_takes_precedence_over_ossie_sql_2026(
+        self, order: tuple[tuple[OssieDialect, str], ...]
+    ) -> None:
+        doc = _doc_with_metric_expression(_multi_dialect_expr(*order))
+
+        result = OssieToMSIConverter().convert(doc).output
+
+        assert result.metrics[0].type_params.expr == "amount_ansi"
+
+    def test_field_expression_prefers_ossie_sql_2026_over_a_vendor_dialect(self) -> None:
+        doc = _ossie_doc(
+            datasets=[
+                _ossie_dataset(
+                    "orders",
+                    fields=[
+                        OssieField(
+                            name="region",
+                            expression=_multi_dialect_expr(
+                                (OssieDialect.SNOWFLAKE, "region_snowflake_only"),
+                                (OssieDialect.OSSIE_SQL_2026, "region_portable"),
+                            ),
+                        )
+                    ],
+                )
+            ]
+        )
+
+        sm = OssieToMSIConverter().convert(doc).output.semantic_models[0]
+
+        assert [(d.name, d.expr) for d in sm.dimensions] == [("region", "region_portable")]
+
+    def test_first_entry_is_still_used_when_no_portable_dialect_is_present(self) -> None:
+        doc = _doc_with_metric_expression(
+            _multi_dialect_expr(
+                (OssieDialect.SNOWFLAKE, "SUM(orders.amt_snowflake_only)"),
+                (OssieDialect.DAX, "SUM(orders.amt_dax_only)"),
+            )
+        )
+
+        result = OssieToMSIConverter().convert(doc).output
+
+        assert result.metrics[0].type_params.expr == "amt_snowflake_only"
+
+    def test_the_first_ossie_sql_2026_entry_wins_when_the_dialect_is_repeated(self) -> None:
+        # `dialects` has no `uniqueItems` constraint, so the same dialect may be listed twice.
+        doc = _doc_with_metric_expression(
+            _multi_dialect_expr(
+                (OssieDialect.OSSIE_SQL_2026, "SUM(orders.amount)"),
+                (OssieDialect.OSSIE_SQL_2026, "SUM(orders.amount_duplicate)"),
+            )
+        )
+
+        result = OssieToMSIConverter().convert(doc).output
+
+        assert result.metrics[0].type_params.expr == "amount"
+
+    def test_the_dataset_of_a_column_is_resolved_with_the_same_dialect_preference(self) -> None:
+        # `unrelated` is declared first, so resolving the field expression positionally
+        # would miss `orders` and fall back to it.
+        doc = _ossie_doc(
+            datasets=[
+                _ossie_dataset("unrelated", fields=[_ossie_field("other_column")]),
+                _ossie_dataset(
+                    "orders",
+                    fields=[
+                        OssieField(
+                            name="amount",
+                            expression=_multi_dialect_expr(
+                                (OssieDialect.SNOWFLAKE, "amt_snowflake_only"),
+                                (OssieDialect.OSSIE_SQL_2026, "amount_portable"),
+                            ),
+                        )
+                    ],
+                ),
+            ],
+            metrics=[
+                OssieMetric(
+                    name="revenue",
+                    expression=_multi_dialect_expr(
+                        (OssieDialect.OSSIE_SQL_2026, "SUM(amount_portable)"),
+                    ),
+                )
+            ],
+        )
+
+        result = OssieToMSIConverter().convert(doc).output
+
+        agg_params = result.metrics[0].type_params.metric_aggregation_params
+        assert agg_params.semantic_model == "orders"
+
+
 class TestOssieToMSIRoundTrip:
     def test_ossie_to_msi_to_ossie_preserves_structure(self, snapshot: SnapshotAssertion) -> None:
         """Ossie → MSI → Ossie preserves dataset names, fields, and metric expressions."""