Skip to content

Commit 4703f1e

Browse files
authored
Fix(macros): ensure @filter condition is evaluated (#1020)
1 parent 7fbd804 commit 4703f1e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

sqlmesh/core/macros.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,11 @@ def filter_(evaluator: MacroEvaluator, *args: t.Any) -> t.List[t.Any]:
410410
>>> from sqlmesh.core.macros import MacroEvaluator
411411
>>> sql = "@REDUCE(@FILTER([1, 2, 3], x -> x > 1), (x, y) -> x + y)"
412412
>>> MacroEvaluator().transform(parse_one(sql)).sql()
413-
'1 + 2 + 3'
413+
'2 + 3'
414414
415415
>>> sql = "@EVAL(@REDUCE(@FILTER([1, 2, 3], x -> x > 1), (x, y) -> x + y))"
416416
>>> MacroEvaluator().transform(parse_one(sql)).sql()
417-
'6'
417+
'5'
418418
419419
Args:
420420
evaluator: MacroEvaluator that invoked the macro
@@ -425,7 +425,7 @@ def filter_(evaluator: MacroEvaluator, *args: t.Any) -> t.List[t.Any]:
425425
"""
426426
*items, func = args
427427
items, func = _norm_var_arg_lambda(evaluator, func, *items) # type: ignore
428-
return list(filter(func, items))
428+
return list(filter(lambda arg: evaluator.eval_expression(func(arg)), items))
429429

430430

431431
@macro("WITH")

tests/core/test_macros.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ def test_ast_correctness(macro_evaluator):
9595
{},
9696
),
9797
("""@SQL(@REDUCE([100, 200, 300, 400], (x,y) -> x + y))""", "1000", {}),
98+
(
99+
"""@SQL(@REDUCE(@FILTER([100, 200, 300, 400], x -> x > 250), (x,y) -> x + y))""",
100+
"700",
101+
{},
102+
),
98103
(
99104
"""select @EACH([a, b, c], x -> x and @SQL('@y'))""",
100105
"SELECT a AND z, b AND z, c AND z",

0 commit comments

Comments
 (0)