Skip to content

Commit 2448057

Browse files
MubinUmarovclaude
andcommitted
fix: prioritize @each lambda params over column name conflicts
When a lambda parameter name matched an existing column name, the substitution was silently skipped due to the `not isinstance(node.parent, exp.Column)` guard wrapping both `args` and `evaluator.locals` lookups. Lambda arguments now take priority regardless of Column context, while `evaluator.locals` substitution remains restricted to non-Column nodes. Fixes #5582 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 0f192c9 commit 2448057

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
@@ -647,10 +647,10 @@ def substitute(
647647
node: exp.Expr, args: t.Dict[str, exp.Expr]
648648
) -> exp.Expr | t.List[exp.Expr] | None:
649649
if isinstance(node, (exp.Identifier, exp.Var)):
650+
name = node.name.lower()
651+
if name in args:
652+
return args[name].copy()
650653
if not isinstance(node.parent, exp.Column):
651-
name = node.name.lower()
652-
if name in args:
653-
return args[name].copy()
654654
if name in evaluator.locals:
655655
return exp.convert(evaluator.locals[name])
656656
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)