Skip to content

Commit 7cf970f

Browse files
authored
fix: allow macros to be in windows (#2613)
1 parent 9067e8f commit 7cf970f

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

sqlmesh/core/dialect.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -199,46 +199,54 @@ def _parse_macro(self: Parser, keyword_macro: str = "") -> t.Optional[exp.Expres
199199
index = self._index
200200
field = self._parse_primary() or self._parse_function(functions={}) or self._parse_id_var()
201201

202-
if isinstance(field, exp.Func):
203-
macro_name = field.name.upper()
204-
if macro_name != keyword_macro and macro_name in KEYWORD_MACROS:
205-
self._retreat(index)
206-
return None
207-
208-
if isinstance(field, exp.Anonymous):
209-
if macro_name == "DEF":
210-
return self.expression(
211-
MacroDef,
212-
this=field.expressions[0],
213-
expression=field.expressions[1],
202+
def _build_macro(field: t.Optional[exp.Expression]) -> t.Optional[exp.Expression]:
203+
if isinstance(field, exp.Func):
204+
macro_name = field.name.upper()
205+
if macro_name != keyword_macro and macro_name in KEYWORD_MACROS:
206+
self._retreat(index)
207+
return None
208+
209+
if isinstance(field, exp.Anonymous):
210+
if macro_name == "DEF":
211+
return self.expression(
212+
MacroDef,
213+
this=field.expressions[0],
214+
expression=field.expressions[1],
215+
comments=comments,
216+
)
217+
if macro_name == "SQL":
218+
into = field.expressions[1].this.lower() if len(field.expressions) > 1 else None
219+
return self.expression(
220+
MacroSQL, this=field.expressions[0], into=into, comments=comments
221+
)
222+
else:
223+
field = self.expression(
224+
exp.Anonymous,
225+
this=field.sql_name(),
226+
expressions=list(field.args.values()),
214227
comments=comments,
215228
)
216-
if macro_name == "SQL":
217-
into = field.expressions[1].this.lower() if len(field.expressions) > 1 else None
218-
return self.expression(
219-
MacroSQL, this=field.expressions[0], into=into, comments=comments
220-
)
221-
else:
222-
field = self.expression(
223-
exp.Anonymous,
224-
this=field.sql_name(),
225-
expressions=list(field.args.values()),
226-
comments=comments,
227-
)
228229

229-
return self.expression(MacroFunc, this=field, comments=comments)
230+
return self.expression(MacroFunc, this=field, comments=comments)
230231

231-
if field is None:
232-
return None
232+
if field is None:
233+
return None
233234

234-
if field.is_string or (isinstance(field, exp.Identifier) and field.quoted):
235-
return self.expression(
236-
MacroStrReplace, this=exp.Literal.string(field.this), comments=comments
237-
)
235+
if field.is_string or (isinstance(field, exp.Identifier) and field.quoted):
236+
return self.expression(
237+
MacroStrReplace, this=exp.Literal.string(field.this), comments=comments
238+
)
239+
240+
if "@" in field.this:
241+
return field
242+
return self.expression(MacroVar, this=field.this, comments=comments)
243+
244+
if isinstance(field, exp.Window):
245+
field.set("this", _build_macro(field.this))
246+
else:
247+
field = _build_macro(field)
238248

239-
if "@" in field.this:
240-
return field
241-
return self.expression(MacroVar, this=field.this, comments=comments)
249+
return field
242250

243251

244252
KEYWORD_MACROS = {"WITH", "JOIN", "WHERE", "GROUP_BY", "HAVING", "ORDER_BY", "LIMIT"}

tests/core/test_dialect.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,14 @@ def test_model_normalization_quote_flexibility():
559559
# It doesn't work the other way which is what we currently expect
560560
with pytest.raises(ParseError):
561561
normalize_model_name("`catalog`.`db`.`table`", default_catalog=None, dialect=None)
562+
563+
564+
def test_macro_parse():
565+
q = parse_one(
566+
"""select * from table(@get(x) OVER (PARTITION BY y ORDER BY z)) AS results""",
567+
read="snowflake",
568+
)
569+
assert (
570+
q.sql()
571+
== "SELECT * FROM TABLE(@get(x) OVER (PARTITION BY y ORDER BY z NULLS LAST)) AS results"
572+
)

0 commit comments

Comments
 (0)