Skip to content

Commit bda17b1

Browse files
authored
fix: macros resolution inside lambdas (#1849)
1 parent d518afe commit bda17b1

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

sqlmesh/core/macros.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,12 @@ def substitute(
387387
node: exp.Expression, args: t.Dict[str, exp.Expression]
388388
) -> exp.Expression | t.List[exp.Expression] | None:
389389
if isinstance(node, (exp.Identifier, exp.Var)):
390-
if node.name in args and not isinstance(node.parent, exp.Column):
391-
return args[node.name].copy()
390+
if not isinstance(node.parent, exp.Column):
391+
name = node.name
392+
if name in args:
393+
return args[name].copy()
394+
if name in evaluator.locals:
395+
return exp.convert(evaluator.locals[name])
392396
if SQLMESH_MACRO_PREFIX in node.name:
393397
return node.__class__(
394398
this=evaluator.template(node.name, {k: v.name for k, v in args.items()})

tests/core/test_macros.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ def test_ast_correctness(macro_evaluator):
119119
@pytest.mark.parametrize(
120120
"sql, expected, args",
121121
[
122+
(
123+
"""select @each(['a', 'b'], x -> @x + @{x}_z + @y + @{y}_@{x})""",
124+
"SELECT 'a' + a_z + 'c' + c_a, 'b' + b_z + 'c' + c_b",
125+
{"y": "c"},
126+
),
122127
(
123128
'"is_@{x}"',
124129
'"is_b"',

0 commit comments

Comments
 (0)