Skip to content

Commit 6d27f4e

Browse files
committed
Fix ?? coalesce at nested depth and inline join metadata
P1: _transform_null_coalesce now tracks parenthesis depth and only splits on ?? at the top level. concat(a ?? b, c) and (a ?? b) + c are no longer corrupted. P2: Inline join model creation now includes _timezone and _model_tags in metadata, matching the behavior of top-level source creation.
1 parent e500074 commit 6d27f4e

1 file changed

Lines changed: 37 additions & 4 deletions

File tree

sidemantic/adapters/malloy.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,41 @@ def _transform_malloy_expr(self, expr: str) -> str:
373373
return expr
374374

375375
def _transform_null_coalesce(self, expr: str) -> str:
376-
"""Transform Malloy ?? null coalescing to SQL COALESCE."""
376+
"""Transform Malloy ?? null coalescing to SQL COALESCE.
377+
378+
Only splits on ?? at the top expression depth (not inside parens/brackets).
379+
"""
377380
if "??" not in expr:
378381
return expr
379-
parts = re.split(r"\s*\?\?\s*", expr)
380-
if len(parts) > 1:
381-
return f"COALESCE({', '.join(p.strip() for p in parts)})"
382+
383+
# Split on ?? only at depth 0
384+
parts = []
385+
current = []
386+
depth = 0
387+
i = 0
388+
while i < len(expr):
389+
ch = expr[i]
390+
if ch in ("(", "[", "{"):
391+
depth += 1
392+
current.append(ch)
393+
elif ch in (")", "]", "}"):
394+
depth -= 1
395+
current.append(ch)
396+
elif depth == 0 and expr[i : i + 2] == "??":
397+
parts.append("".join(current).strip())
398+
current = []
399+
i += 2
400+
# Skip whitespace after ??
401+
while i < len(expr) and expr[i] == " ":
402+
i += 1
403+
continue
404+
else:
405+
current.append(ch)
406+
i += 1
407+
408+
if parts:
409+
parts.append("".join(current).strip())
410+
return f"COALESCE({', '.join(parts)})"
382411
return expr
383412

384413
def _transform_and_tree(self, expr: str) -> str:
@@ -1160,6 +1189,10 @@ def _extract_inline_join_source(self, join_name: str, sq_expr):
11601189
metadata = {}
11611190
if self.current_connection:
11621191
metadata["connection"] = self.current_connection
1192+
if self._timezone:
1193+
metadata["timezone"] = self._timezone
1194+
if self._model_tags:
1195+
metadata["tags"] = self._model_tags
11631196
inline_model = Model(
11641197
name=join_name,
11651198
table=self.current_table,

0 commit comments

Comments
 (0)