Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sqlmesh/core/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,10 @@ def substitute(
node: exp.Expr, args: t.Dict[str, exp.Expr]
) -> exp.Expr | t.List[exp.Expr] | None:
if isinstance(node, (exp.Identifier, exp.Var)):
name = node.name.lower()
if name in args:
return args[name].copy()
if not isinstance(node.parent, exp.Column):
name = node.name.lower()
if name in args:
return args[name].copy()
if name in evaluator.locals:
return exp.convert(evaluator.locals[name])
if SQLMESH_MACRO_PREFIX in node.name:
Expand Down
14 changes: 14 additions & 0 deletions tests/core/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,20 @@ def test_ast_correctness(macro_evaluator):
"SELECT * FROM (VALUES ((1, 2), (2, 3), (3, 4))) AS v",
{},
),
# Lambda parameter name matches the column identifier inside a Column expression
# (e.g. schema.product where 'product' is both the lambda arg and the column name).
# Lambda args must take precedence over the Column-context guard so that the param
# is substituted correctly (regression test for GitHub issue #5582).
(
"SELECT @EACH([a, b, c], product -> schema.product)",
"SELECT schema.a, schema.b, schema.c",
{},
),
(
"SELECT @EACH([x, y], col -> tbl.col)",
"SELECT tbl.x, tbl.y",
{},
),
],
)
def test_macro_functions(macro_evaluator: MacroEvaluator, assert_exp_eq, sql, expected, args):
Expand Down
Loading