Skip to content

Feat: correctly handle the generation of VALUES expressions using macros#4975

Merged
georgesittas merged 1 commit intomainfrom
jo/fix_value_macro_rendering
Jul 16, 2025
Merged

Feat: correctly handle the generation of VALUES expressions using macros#4975
georgesittas merged 1 commit intomainfrom
jo/fix_value_macro_rendering

Conversation

@georgesittas
Copy link
Contributor

This PR aims to address a feature gap. This is the behavior in main today:

>>> from sqlglot import parse_one
>>> from sqlmesh.core.macros import MacroEvaluator
>>>
>>> MacroEvaluator().transform(parse_one("SELECT * FROM (VALUES @EACH([1, 2, 3], v -> (v)) ) as v")).sql()
'SELECT * FROM (VALUES ((1), (2), (3))) AS v'
>>> MacroEvaluator().transform(parse_one("SELECT * FROM (VALUES (@EACH([1, 2, 3], v -> (v))) ) as v")).sql()
'SELECT * FROM (VALUES ((1), (2), (3))) AS v'

Notice how both inputs result in the same output, which produces a single row with three columns. There is no obvious way to produce three columns, using @EACH, due to how the VALUES parsing works today.

@georgesittas georgesittas requested a review from a team July 16, 2025 14:48
# intention was to construct a row or a column with the VALUES expression. To avoid this, we
# amend the AST such that the Tuple is replaced by the macro function call itself.
expr = self.__parse_value() # type: ignore
if expr and not wrapped and isinstance(seq_get(expr.expressions, 0), MacroFunc):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. I imagine this preserves any other functionality and in this first position it should be each always this macro function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have to be @EACH– other macro functions could also return lists. In that case, we should interpret the items in the list as either columns or rows depending on the presence of the parentheses after VALUES.

@georgesittas georgesittas merged commit 4fa9992 into main Jul 16, 2025
27 checks passed
@georgesittas georgesittas deleted the jo/fix_value_macro_rendering branch July 16, 2025 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants