Skip to content
Merged
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
1 change: 1 addition & 0 deletions sqlmesh/core/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def _parse_id_var(

while (
identifier
and not identifier.args.get("quoted")
and self._is_connected()
and (
self._match_texts(("{", SQLMESH_MACRO_PREFIX))
Expand Down
10 changes: 5 additions & 5 deletions sqlmesh/core/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ def haversine_distance(
def pivot(
evaluator: MacroEvaluator,
column: SQL,
values: t.List[SQL],
values: t.List[exp.Expression],
alias: bool = True,
agg: exp.Expression = exp.Literal.string("SUM"),
cmp: exp.Expression = exp.Literal.string("="),
Expand All @@ -1146,23 +1146,23 @@ def pivot(
>>> from sqlmesh.core.macros import MacroEvaluator
>>> sql = "SELECT date_day, @PIVOT(status, ['cancelled', 'completed']) FROM rides GROUP BY 1"
>>> MacroEvaluator().transform(parse_one(sql)).sql()
'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'
'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'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what led to this decision, i.e. including the SQL-representation of the value as-is in the alias.

>>> sql = "SELECT @PIVOT(a, ['v'], then_value := tv, suffix := '_sfx', quote := FALSE)"
>>> MacroEvaluator(dialect="bigquery").transform(parse_one(sql)).sql("bigquery")
"SELECT SUM(CASE WHEN a = 'v' THEN tv ELSE 0 END) AS `v_sfx`"
"SELECT SUM(CASE WHEN a = 'v' THEN tv ELSE 0 END) AS v_sfx"
"""
aggregates: t.List[exp.Expression] = []
for value in values:
proj = f"{agg.name}("
if distinct:
proj += "DISTINCT "

proj += f"CASE WHEN {column} {cmp.name} {value} THEN {then_value} ELSE {else_value} END) "
proj += f"CASE WHEN {column} {cmp.name} {value.sql(evaluator.dialect)} THEN {then_value} ELSE {else_value} END) "
node = evaluator.parse_one(proj)

if alias:
node = node.as_(
f"{prefix.name}{value}{suffix.name}",
f"{prefix.name}{value.name}{suffix.name}",
quoted=quote,
copy=False,
dialect=evaluator.dialect,
Expand Down
5 changes: 4 additions & 1 deletion sqlmesh/core/snapshot/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,10 @@ def _migrate_target_table(
) -> None:
adapter = self.get_adapter(snapshot.model.gateway)

tmp_table_name = f"{target_table_name}_schema_tmp"
target_table = exp.to_table(target_table_name)
target_table.this.set("this", f"{target_table.name}_schema_tmp")

tmp_table_name = target_table.sql()
if snapshot.is_materialized:
self._execute_create(
snapshot=snapshot,
Expand Down
4 changes: 2 additions & 2 deletions tests/core/engine_adapter/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def test_scd_type_2_by_time(
"test_valid_from",
"test_valid_to",
TRUE AS "_exists"
FROM ""__temp_target_efgh""
FROM "__temp_target_efgh"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was incorrect, the quotes here are off.

WHERE
NOT "test_valid_to" IS NULL
), "latest" AS (
Expand All @@ -652,7 +652,7 @@ def test_scd_type_2_by_time(
"test_valid_from",
"test_valid_to",
TRUE AS "_exists"
FROM ""__temp_target_efgh""
FROM "__temp_target_efgh"
WHERE
"test_valid_to" IS NULL
), "deleted" AS (
Expand Down
5 changes: 5 additions & 0 deletions tests/core/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,8 @@ def test_sqlglot_extended_correctly(dialect: str) -> None:
assert isinstance(value, exp.Table)
assert value.sql() == "foo"
assert ast.sql(dialect=dialect) == "MODEL (\nname foo\n)"


def test_connected_identifier():
ast = d.parse_one("""SELECT ("x"at time zone 'utc')::timestamp as x""", "redshift")
assert ast.sql("redshift") == """SELECT CAST(("x" AT TIME ZONE 'utc') AS TIMESTAMP) AS x"""
2 changes: 1 addition & 1 deletion tests/core/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_format_files(tmp_path: pathlib.Path, mocker: MockerFixture):
f3 = create_temp_file(
tmp_path,
pathlib.Path(audits_dir, "audit_1.sql"),
"AUDIT(name assert_positive_id, dialect 'duckdb'); SELECT * FROM @this_model WHERE \"CaseSensitive\"_item_id < 0;",
"AUDIT(name assert_positive_id, dialect 'duckdb'); SELECT * FROM @this_model WHERE \"CaseSensitive_item_id\" < 0;",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was also incorrect, it's like trying to do SELECT * FROM t WHERE "foo"_bar < 0.

)
f4 = create_temp_file(
tmp_path,
Expand Down