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/10/11 13:00:46 UTC

(ossie) branch main updated: fix(microsoft): preserve measures on calculated tables (#564)

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 a1605a5d fix(microsoft): preserve measures on calculated tables (#564)
a1605a5d is described below

commit a1605a5dd715ab1adcc492057656b493d95b97e0
Author: sean0407 <se...@gmail.com>
AuthorDate: Sun Oct 11 21:00:41 2026 +0800

    fix(microsoft): preserve measures on calculated tables (#564)
    
    * fix(microsoft): preserve measures on calculated tables
    
    * fix(microsoft): import read_stash in semantic_model_to_ossie
    
    ---------
    
    Co-authored-by: Sean <se...@users.noreply.github.com>
    Co-authored-by: Jean-Baptiste Onofré <jb...@apache.org>
---
 .../src/ossie_microsoft/ossie_to_semantic_model.py |  7 ++-
 .../src/ossie_microsoft/semantic_model_to_ossie.py | 57 +++++++++++++++++++++-
 .../tests/test_semantic_model_to_ossie.py          | 55 +++++++++++++++++++++
 3 files changed, 114 insertions(+), 5 deletions(-)

diff --git a/converters/microsoft/src/ossie_microsoft/ossie_to_semantic_model.py b/converters/microsoft/src/ossie_microsoft/ossie_to_semantic_model.py
index 1744d9cc..09e264ba 100644
--- a/converters/microsoft/src/ossie_microsoft/ossie_to_semantic_model.py
+++ b/converters/microsoft/src/ossie_microsoft/ossie_to_semantic_model.py
@@ -189,6 +189,9 @@ def convert_ossie_to_semantic_model(
     warn_unsupported("model", semantic_model, OSSIE_UNSUPPORTED, "Power BI", _DROPPED)
 
     tables, table_columns, generated_partitions = _convert_datasets(datasets)
+    # Restore excluded hosts before placing model-level metrics so a metric whose
+    # home table was a calculated table retains its original table placement.
+    tables.extend(stash.get("excludedTables") or [])
     _apply_measures(tables, semantic_model.get("metrics") or [])
     _restore_excluded_measures(tables, stash.get("excludedMeasures") or [])
 
@@ -197,10 +200,6 @@ def convert_ossie_to_semantic_model(
     )
     relationships.extend(stash.get("excludedRelationships") or [])
 
-    # Tables the import excluded (private, calculation group, auto date table) are
-    # restored verbatim so a round trip reproduces the original model.
-    tables.extend(stash.get("excludedTables") or [])
-
     model = {"tables": tables}
     if generated_partitions:
         expressions, expression_name = _merge_direct_lake_expressions(
diff --git a/converters/microsoft/src/ossie_microsoft/semantic_model_to_ossie.py b/converters/microsoft/src/ossie_microsoft/semantic_model_to_ossie.py
index 587b37b9..e86b0f18 100644
--- a/converters/microsoft/src/ossie_microsoft/semantic_model_to_ossie.py
+++ b/converters/microsoft/src/ossie_microsoft/semantic_model_to_ossie.py
@@ -52,6 +52,7 @@ from ._common import (
     TMSL_UNTYPED,
     is_date_only_format,
     make_expression,
+    read_stash,
     text,
     warn,
     warn_unsupported,
@@ -174,7 +175,9 @@ def build_ossie_document(bim_file):
     if relationships:
         semantic_model["relationships"] = relationships
 
-    metrics, excluded_measures = _convert_metrics(tables)
+    metric_tables = [table for table in all_tables if _is_measure_table(table)]
+    metrics, excluded_measures = _convert_metrics(metric_tables)
+    excluded_tables = _strip_converted_measures(excluded_tables, metrics)
     if metrics:
         semantic_model["metrics"] = metrics
 
@@ -235,6 +238,20 @@ def _is_calculated_table(table):
     )
 
 
+def _is_measure_table(table):
+    """Whether measures on a table can become model-level Apache Ossie metrics."""
+    if _is_exported_table(table):
+        return True
+    # Calculated tables have no portable dataset source, but their measures are still
+    # model-level metrics and can be restored to the original table from the stash.
+    return (
+        _is_calculated_table(table)
+        and not table.get("isPrivate")
+        and not table.get("calculationGroup")
+        and not AUTO_DATE_TABLE_RE.match(table["name"])
+    )
+
+
 def _convert_table(table):
     dataset = {"name": table["name"], "source": _table_source(table)}
     scope = f"table '{table['name']}'"
@@ -525,6 +542,44 @@ def _convert_metrics(tables):
     return metrics, excluded
 
 
+def _strip_converted_measures(excluded_tables, metrics):
+    """Keep converted metrics out of the table stash to avoid restoring them twice."""
+    converted = set()
+    for metric in metrics:
+        stash = read_stash(metric)
+        table_name = stash.get("table")
+        measure_name = stash.get("name", metric.get("name"))
+        if table_name and measure_name:
+            converted.add((table_name.casefold(), measure_name.casefold()))
+
+    if not converted:
+        return excluded_tables
+
+    result = []
+    for table in excluded_tables:
+        measures = table.get("measures")
+        if not isinstance(measures, list):
+            result.append(table)
+            continue
+        remaining = [
+            measure
+            for measure in measures
+            if not isinstance(measure, dict)
+            or not measure.get("name")
+            or (table["name"].casefold(), measure["name"].casefold()) not in converted
+        ]
+        if len(remaining) == len(measures):
+            result.append(table)
+            continue
+        preserved = dict(table)
+        if remaining:
+            preserved["measures"] = remaining
+        else:
+            preserved.pop("measures", None)
+        result.append(preserved)
+    return result
+
+
 # ---------------------------------------------------------------------------
 # Relationships
 # ---------------------------------------------------------------------------
diff --git a/converters/microsoft/tests/test_semantic_model_to_ossie.py b/converters/microsoft/tests/test_semantic_model_to_ossie.py
index 50d179a6..c0bce28b 100644
--- a/converters/microsoft/tests/test_semantic_model_to_ossie.py
+++ b/converters/microsoft/tests/test_semantic_model_to_ossie.py
@@ -774,6 +774,61 @@ def test_model_stash_preserves_excluded_tables_and_relationships(model):
     assert len(stash["excludedRelationships"]) == 4
 
 
+def test_calculated_table_measures_become_metrics_and_round_trip(bim):
+    measure_table = {
+        "name": "Measure Only",
+        "columns": [
+            {
+                "name": "_",
+                "dataType": "string",
+                "sourceColumn": "_",
+                "type": "calculatedTableColumn",
+                "isHidden": True,
+            }
+        ],
+        "partitions": [
+            {
+                "name": "Measure Only",
+                "mode": "import",
+                "source": {
+                    "type": "calculated",
+                    "expression": 'ROW("_", BLANK())',
+                },
+            }
+        ],
+        "measures": [
+            {
+                "name": "Measure Total",
+                "expression": "SUM(Sales[Amount])",
+                "formatString": "#,0.00",
+            }
+        ],
+    }
+    bim["model"]["tables"].append(measure_table)
+
+    with warnings.catch_warnings():
+        warnings.simplefilter("ignore")
+        ossie = build_ossie_document(bim)
+
+    metric = _metric(ossie, "Measure Total")
+    assert _expression(metric, "DAX") == "SUM(Sales[Amount])"
+    assert read_stash(metric)["table"] == "Measure Only"
+    stashed_table = next(
+        table
+        for table in read_stash(ossie)["excludedTables"]
+        if table["name"] == "Measure Only"
+    )
+    assert "measures" not in stashed_table
+
+    round_tripped = convert_ossie_to_semantic_model(ossie)
+    restored_table = next(
+        table
+        for table in round_tripped["model"]["tables"]
+        if table["name"] == "Measure Only"
+    )
+    assert restored_table["measures"] == [measure_table["measures"][0]]
+
+
 def test_dataset_stash_preserves_partitions_and_annotations(model):
     stash = read_stash(_dataset(model, "Sales"))
     assert stash["partitions"][0]["source"]["type"] == "m"