@@ -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
244252KEYWORD_MACROS = {"WITH" , "JOIN" , "WHERE" , "GROUP_BY" , "HAVING" , "ORDER_BY" , "LIMIT" }
0 commit comments