Skip to content

Commit 991a327

Browse files
MubinUmarovclaude
andauthored
fix: prioritize @each lambda params over column name conflicts (#5769)
Signed-off-by: Mubin Umarov <mubin.umarov@gmail.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 431725a commit 991a327

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

sqlmesh/core/macros.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,10 @@ def substitute(
648648
node: exp.Expr, args: t.Dict[str, exp.Expr]
649649
) -> exp.Expr | t.List[exp.Expr] | None:
650650
if isinstance(node, (exp.Identifier, exp.Var)):
651+
name = node.name.lower()
652+
if name in args:
653+
return args[name].copy()
651654
if not isinstance(node.parent, exp.Column):
652-
name = node.name.lower()
653-
if name in args:
654-
return args[name].copy()
655655
if name in evaluator.locals:
656656
return exp.convert(evaluator.locals[name])
657657
if SQLMESH_MACRO_PREFIX in node.name:

tests/core/test_macros.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,20 @@ def test_ast_correctness(macro_evaluator):
618618
"SELECT * FROM (VALUES ((1, 2), (2, 3), (3, 4))) AS v",
619619
{},
620620
),
621+
# Lambda parameter name matches the column identifier inside a Column expression
622+
# (e.g. schema.product where 'product' is both the lambda arg and the column name).
623+
# Lambda args must take precedence over the Column-context guard so that the param
624+
# is substituted correctly (regression test for GitHub issue #5582).
625+
(
626+
"SELECT @EACH([a, b, c], product -> schema.product)",
627+
"SELECT schema.a, schema.b, schema.c",
628+
{},
629+
),
630+
(
631+
"SELECT @EACH([x, y], col -> tbl.col)",
632+
"SELECT tbl.x, tbl.y",
633+
{},
634+
),
621635
],
622636
)
623637
def test_macro_functions(macro_evaluator: MacroEvaluator, assert_exp_eq, sql, expected, args):

0 commit comments

Comments
 (0)