Skip to content

Commit 47f544c

Browse files
chuenchen309claude
andcommitted
Fix(macros): @filter with a single non-array argument
@filter's docstring says "The first argument can be an Array or var args can be used", the same contract @each and @reduce state. All three route their arguments through _norm_var_arg_lambda, whose single-item branch returns a bare expression rather than a list: expressions = ( item.expressions if isinstance(item, (exp.Array, exp.Tuple)) else [item.this] if isinstance(item, exp.Paren) else item # <- bare expression ) @each and @reduce absorb that with ensure_collection(); @filter iterates it directly, so a lone argument raises: @each(a, x -> x + 1) -> SELECT a + 1 @reduce(a, (x, y) -> x+y) -> SELECT a @filter(a, x -> x > 1) -> MacroEvalError: 'Column' object is not iterable Wrap the items in ensure_collection() so @filter matches its siblings. An explicit array was already working and is unaffected. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: chuenchen309 <48723787+chuenchen309@users.noreply.github.com>
1 parent 97d60cb commit 47f544c

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

sqlmesh/core/macros.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ def filter_(evaluator: MacroEvaluator, *args: t.Any) -> t.List[t.Any]:
789789
"""
790790
*items, func = args
791791
items, func = _norm_var_arg_lambda(evaluator, func, *items) # type: ignore
792-
return list(filter(lambda arg: evaluator.eval_expression(func(arg)), items))
792+
return list(filter(lambda arg: evaluator.eval_expression(func(arg)), ensure_collection(items)))
793793

794794

795795
def _optional_expression(

tests/core/test_macros.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ def test_ast_correctness(macro_evaluator):
358358
"700",
359359
{},
360360
),
361+
# @FILTER documents "The first argument can be an Array or var args can be
362+
# used", like @EACH and @REDUCE, so a lone item must work too.
363+
(
364+
"""@SQL(@REDUCE(@FILTER(300, x -> x > 250), (x,y) -> x + y))""",
365+
"300",
366+
{},
367+
),
368+
(
369+
"""select @FILTER(a, x -> x > 1)""",
370+
"SELECT a",
371+
{},
372+
),
361373
(
362374
"""select @EACH([a, b, c], x -> x and @SQL('@y'))""",
363375
"SELECT a AND z, b AND z, c AND z",

0 commit comments

Comments
 (0)