diff --git a/sqlmesh/core/macros.py b/sqlmesh/core/macros.py index ad8b922a07..8706897120 100644 --- a/sqlmesh/core/macros.py +++ b/sqlmesh/core/macros.py @@ -789,7 +789,7 @@ def filter_(evaluator: MacroEvaluator, *args: t.Any) -> t.List[t.Any]: """ *items, func = args items, func = _norm_var_arg_lambda(evaluator, func, *items) # type: ignore - return list(filter(lambda arg: evaluator.eval_expression(func(arg)), items)) + return list(filter(lambda arg: evaluator.eval_expression(func(arg)), ensure_collection(items))) def _optional_expression( diff --git a/tests/core/test_macros.py b/tests/core/test_macros.py index 0135cd8ca5..17b699d11f 100644 --- a/tests/core/test_macros.py +++ b/tests/core/test_macros.py @@ -358,6 +358,18 @@ def test_ast_correctness(macro_evaluator): "700", {}, ), + # @FILTER documents "The first argument can be an Array or var args can be + # used", like @EACH and @REDUCE, so a lone item must work too. + ( + """@SQL(@REDUCE(@FILTER(300, x -> x > 250), (x,y) -> x + y))""", + "300", + {}, + ), + ( + """select @FILTER(a, x -> x > 1)""", + "SELECT a", + {}, + ), ( """select @EACH([a, b, c], x -> x and @SQL('@y'))""", "SELECT a AND z, b AND z, c AND z",