Skip to content

Commit

Permalink
some ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jsibbison-square committed Sep 5, 2024
1 parent 71457e8 commit 6905ac3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions fakesnow/transforms_merge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlglot
from sqlglot import exp


# Implements snowflake's MERGE INTO functionality in duckdb (https://docs.snowflake.com/en/sql-reference/sql/merge).
# An example merge statement that we transform into multiple duckdb statements. t1 is the target, t2 is the source:
# MERGE INTO t1 USING t2 ON t1.t1Key = t2.t2Key
Expand Down Expand Up @@ -64,7 +65,7 @@ def _generate_final_expression_set(self) -> list[exp.Expression]:
# Operate in a transaction to freeze rowids https://duckdb.org/docs/sql/statements/select#row-ids
begin_transaction_exp = sqlglot.parse_one("BEGIN TRANSACTION")
end_transaction_exp = sqlglot.parse_one("COMMIT")

# Add modifications results outcome query
results_exp = sqlglot.parse_one(f"""
WITH merge_update_deletes AS (
Expand Down Expand Up @@ -107,7 +108,7 @@ def transform(self) -> list[exp.Expression]:
# Breaking down how we implement MERGE:
# Insert into a temp table the source rows (rowid is stable in a transaction: https://duckdb.org/docs/sql/statements/select.html#row-ids)
# and which modification to apply to those source rows (insert, update, delete).
#
#
# Then apply the modifications to the target table based on the rowids in the temp tables.

# TODO: Error if attempting to update the same source row multiple times (based on a config in the doco).
Expand Down Expand Up @@ -167,7 +168,7 @@ def transform(self) -> list[exp.Expression]:
# Insert into the temp table the rowids of the rows that match the WHEN condition
self._insert_temp_merge_operation("U", w_idx, subquery)

# Build the UPDATE statement to apply to the target table for this specific WHEN clause sourcing
# Build the UPDATE statement to apply to the target table for this specific WHEN clause sourcing
# its target rows from the temp table that we just inserted into.
then.set("this", self._target_table())
then.set(
Expand All @@ -180,19 +181,19 @@ def transform(self) -> list[exp.Expression]:
exp.Where(this=exp.And(this=subquery_on_expression, expression=rowid_in_temp_table_expr)),
)
self._output_expressions.append(then)

# Handling WHEN MATCHED AND <Condition> THEN DELETE
elif then.args.get("this") == "DELETE":
# Insert into the temp table the rowids of the rows that match the WHEN condition
self._insert_temp_merge_operation("D", w_idx, subquery)

# Build the DELETE statement to apply to the target table for this specific WHEN clause sourcing
# Build the DELETE statement to apply to the target table for this specific WHEN clause sourcing
# its target rows from the temp table that we just inserted into.
delete_from_temp = exp.delete(table=self._target_table(), where=rowid_in_temp_table_expr)
self._output_expressions.append(delete_from_temp)
else:
assert isinstance(then, (exp.Update, exp.Delete)), f"Expected 'Update' or 'Delete', got {then}"

# Handling WHEN NOT MATCHED THEN INSERT (<Columns>) VALUES (<Values>)
else:
assert isinstance(then, exp.Insert), f"Expected 'Insert', got {then}"
Expand Down Expand Up @@ -239,7 +240,7 @@ def transform(self) -> list[exp.Expression]:
)

columns = [self._remove_table_alias(e) for e in then.args.get("this").expressions]
# The INSERT statement to apply to the target table for targetted rowids
# The INSERT statement to apply to the target table for targeted rowids
statement = exp.insert(
into=self._target_table(),
columns=[c.this for c in columns],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fakes_merge.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import snowflake.connector
import pytest


# TODO: Also consider nondeterministic config for throwing errors when multiple source criteria match a target row
# https://docs.snowflake.com/en/sql-reference/sql/merge#nondeterministic-results-for-update-and-delete
Expand Down Expand Up @@ -93,4 +93,4 @@ def test_merge_not_matched_condition(conn: snowflake.connector.SnowflakeConnecti
assert res == [
{"T1KEY": 1, "VAL": "Old Value 1", "STATUS": "Old Status 1"},
{"T1KEY": 2, "VAL": "New Value 2", "STATUS": "New Status 2"},
]
]

0 comments on commit 6905ac3

Please sign in to comment.