Skip to content

Commit 06a51dc

Browse files
authored
Chore: do not warn if non-literal is used in then/else_value of PIVOT macro (#4423)
1 parent 5d7f21d commit 06a51dc

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

sqlmesh/core/macros.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,17 +1087,17 @@ def haversine_distance(
10871087
@macro()
10881088
def pivot(
10891089
evaluator: MacroEvaluator,
1090-
column: exp.Column,
1091-
values: t.Union[exp.Array, exp.Tuple],
1092-
alias: exp.Boolean = exp.true(),
1093-
agg: exp.Literal = exp.Literal.string("SUM"),
1094-
cmp: exp.Literal = exp.Literal.string("="),
1095-
prefix: exp.Literal = exp.Literal.string(""),
1096-
suffix: exp.Literal = exp.Literal.string(""),
1097-
then_value: exp.Literal = exp.Literal.number(1),
1098-
else_value: exp.Literal = exp.Literal.number(0),
1099-
quote: exp.Boolean = exp.true(),
1100-
distinct: exp.Boolean = exp.false(),
1090+
column: SQL,
1091+
values: t.List[SQL],
1092+
alias: bool = True,
1093+
agg: SQL = SQL("SUM"),
1094+
cmp: SQL = SQL("="),
1095+
prefix: SQL = SQL(""),
1096+
suffix: SQL = SQL(""),
1097+
then_value: SQL = SQL("1"),
1098+
else_value: SQL = SQL("0"),
1099+
quote: bool = True,
1100+
distinct: bool = False,
11011101
) -> t.List[exp.Expression]:
11021102
"""Returns a list of projections as a result of pivoting the given column on the given values.
11031103
@@ -1109,15 +1109,19 @@ def pivot(
11091109
'SELECT date_day, SUM(CASE WHEN status = \\'cancelled\\' THEN 1 ELSE 0 END) AS "\\'cancelled\\'", SUM(CASE WHEN status = \\'completed\\' THEN 1 ELSE 0 END) AS "\\'completed\\'" FROM rides GROUP BY 1'
11101110
"""
11111111
aggregates: t.List[exp.Expression] = []
1112-
for value in values.expressions:
1113-
proj = f"{agg.this}("
1114-
if distinct.this:
1112+
for value in values:
1113+
proj = f"{agg}("
1114+
if distinct:
11151115
proj += "DISTINCT "
1116-
proj += f"CASE WHEN {column} {cmp.this} {value} THEN {then_value} ELSE {else_value} END) "
1116+
1117+
proj += f"CASE WHEN {column} {cmp} {value} THEN {then_value} ELSE {else_value} END) "
11171118
node = evaluator.parse_one(proj)
1118-
if alias.this:
1119-
node = node.as_(f"{prefix.this}{value}{suffix.this}", quoted=quote.this, copy=False)
1119+
1120+
if alias:
1121+
node = node.as_(f"{prefix}{value}{suffix}", quoted=quote, copy=False)
1122+
11201123
aggregates.append(node)
1124+
11211125
return aggregates
11221126

11231127

0 commit comments

Comments
 (0)