diff --git a/core/wren/src/wren/osi.py b/core/wren/src/wren/osi.py index 284d062bb..96ac6f53a 100644 --- a/core/wren/src/wren/osi.py +++ b/core/wren/src/wren/osi.py @@ -288,14 +288,20 @@ def _pick_expression(expr_field: Any, dialect_preference: str) -> str: val = d.get("expression", "") if isinstance(key, str): by_dialect[key] = val if isinstance(val, str) else "" - if dialect_preference in by_dialect: + # Resolve in priority order, but skip empty expressions so a present-but- + # blank preferred (or ANSI_SQL) entry does not shadow a real expression + # further down the list. An empty string carries no SQL, so treating it as + # "found" would emit an empty/identity column for a genuinely calculated + # field. + if by_dialect.get(dialect_preference): return by_dialect[dialect_preference] - if "ANSI_SQL" in by_dialect: + if by_dialect.get("ANSI_SQL"): return by_dialect["ANSI_SQL"] - first = dialects[0] - if isinstance(first, dict): - val = first.get("expression", "") - return val if isinstance(val, str) else "" + for d in dialects: + if isinstance(d, dict): + val = d.get("expression", "") + if isinstance(val, str) and val: + return val return "" diff --git a/core/wren/tests/unit/test_osi.py b/core/wren/tests/unit/test_osi.py index aa86ef36f..bb0c71bc1 100644 --- a/core/wren/tests/unit/test_osi.py +++ b/core/wren/tests/unit/test_osi.py @@ -241,6 +241,30 @@ def test_pick_expression_falls_back_to_first(): assert _pick_expression(expr, "SNOWFLAKE") == "[x]" +def test_pick_expression_skips_empty_preferred_and_uses_ansi(): + # An empty preferred-dialect expression must not shadow a real ANSI_SQL one. + expr = { + "dialects": [ + {"dialect": "SNOWFLAKE", "expression": ""}, + {"dialect": "ANSI_SQL", "expression": "SUM(x)"}, + ] + } + assert _pick_expression(expr, "SNOWFLAKE") == "SUM(x)" + + +def test_pick_expression_skips_empty_ansi_and_uses_first_nonempty(): + # Empty preferred AND empty ANSI_SQL must fall through to the next + # non-empty expression rather than returning "". + expr = { + "dialects": [ + {"dialect": "SNOWFLAKE", "expression": ""}, + {"dialect": "ANSI_SQL", "expression": ""}, + {"dialect": "MDX", "expression": "[x]"}, + ] + } + assert _pick_expression(expr, "SNOWFLAKE") == "[x]" + + def test_pick_expression_shorthand_string(): assert _pick_expression("x", "ANSI_SQL") == "x"