Skip to content

Commit 0b11419

Browse files
authored
Feat: allow macro lambdas with expansion on tuples (#1298)
1 parent c7c6f15 commit 0b11419

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

sqlmesh/core/macros.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,14 @@ def substitute(
314314
expressions = items
315315

316316
if not callable(func):
317-
return expressions, lambda *args: func.this.transform(
317+
return expressions, lambda args: func.this.transform(
318318
substitute,
319-
{expression.name: arg for expression, arg in zip(func.expressions, args)},
319+
{
320+
expression.name: arg
321+
for expression, arg in zip(
322+
func.expressions, args.expressions if isinstance(args, exp.Tuple) else [args]
323+
)
324+
},
320325
)
321326

322327
return expressions, func
@@ -399,7 +404,7 @@ def reduce_(evaluator: MacroEvaluator, *args: t.Any) -> t.Any:
399404
"""
400405
*items, func = args
401406
items, func = _norm_var_arg_lambda(evaluator, func, *items) # type: ignore
402-
return reduce(func, ensure_collection(items))
407+
return reduce(lambda a, b: func(exp.Tuple(expressions=[a, b])), ensure_collection(items))
403408

404409

405410
@macro("FILTER")

tests/core/test_macros.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def test_ast_correctness(macro_evaluator):
6969
@pytest.mark.parametrize(
7070
"sql, expected, args",
7171
[
72+
(
73+
"select @each([(a, b), (c, d)], (x, y) -> x as y)",
74+
"SELECT a AS b, c AS d",
75+
{},
76+
),
7277
(
7378
"select @each(['a', 'b'], x -> x = y as is_@{x})",
7479
"SELECT 'a' = y as is_a, 'b' = y as is_b",

0 commit comments

Comments
 (0)