@@ -188,14 +188,18 @@ def evaluate(self, node: MacroFunc) -> exp.Expression | t.List[exp.Expression] |
188188 return [exp .convert (item ) for item in result if item is not None ]
189189 return exp .convert (result )
190190
191- def eval_expression (self , node : exp . Expression ) -> t .Any :
191+ def eval_expression (self , node : t . Any ) -> t .Any :
192192 """Converts a SQLGlot expression into executable Python code and evals it.
193193
194+ If the node is not an expression, it will simply be returned.
195+
194196 Args:
195197 node: expression
196198 Returns:
197199 The return value of the evaled Python Code.
198200 """
201+ if not isinstance (node , exp .Expression ):
202+ return node
199203 code = node .sql ()
200204 try :
201205 code = self .generator .generate (node )
@@ -333,6 +337,31 @@ def each(
333337 return [item for item in map (func , ensure_collection (items )) if item is not None ]
334338
335339
340+ @macro ("IF" )
341+ def if_ (
342+ evaluator : MacroEvaluator ,
343+ condition : t .Any ,
344+ true : t .Any ,
345+ false : t .Any = None ,
346+ ) -> t .Any :
347+ """Evaluates a given condition and returns the second argument if true or else the third argument.
348+
349+ If false is not passed in, the default return value will be None.
350+
351+ Example:
352+ >>> from sqlglot import parse_one
353+ >>> from sqlmesh.core.macros import MacroEvaluator
354+ >>> MacroEvaluator().transform(parse_one("@IF('a' = 1, a, b)")).sql()
355+ 'b'
356+
357+ >>> MacroEvaluator().transform(parse_one("@IF('a' = 1, a)"))
358+ """
359+
360+ if evaluator .eval_expression (condition ):
361+ return true
362+ return false
363+
364+
336365@macro ("REDUCE" )
337366def reduce_ (evaluator : MacroEvaluator , * args : t .Any ) -> t .Any :
338367 """Iterates through items applying provided function that takes two arguments
0 commit comments