fix(osi): skip empty expressions when picking a dialect expression (#2413)

This commit is contained in:
Bartok
2026-07-02 09:43:48 +08:00
committed by GitHub
parent 06a51a81b0
commit bcf78d19aa
2 changed files with 36 additions and 6 deletions
+12 -6
View File
@@ -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 ""
+24
View File
@@ -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"