Skip to content

Commit

Permalink
fix!: snowflake optional merge insert
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed May 2, 2024
1 parent 33bae9b commit 3768514
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ class Insert(DDL, DML):
"hint": False,
"with": False,
"is_function": False,
"this": True,
"this": False,
"expression": False,
"conflict": False,
"returning": False,
Expand Down
12 changes: 7 additions & 5 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3353,9 +3353,10 @@ def when_sql(self, expression: exp.When) -> str:

then_expression = expression.args.get("then")
if isinstance(then_expression, exp.Insert):
then = f"INSERT {self.sql(then_expression, 'this')}"
if "expression" in then_expression.args:
then += f" VALUES {self.sql(then_expression, 'expression')}"
this = self.sql(then_expression, "this")
this = f"INSERT {this}" if this else "INSERT"
then = self.sql(then_expression, "expression")
then = f"{this} VALUES {then}" if then else this
elif isinstance(then_expression, exp.Update):
if isinstance(then_expression.args.get("expressions"), exp.Star):
then = f"UPDATE {self.sql(then_expression, 'expressions')}"
Expand All @@ -3377,10 +3378,11 @@ def merge_sql(self, expression: exp.Merge) -> str:
this = self.sql(table)
using = f"USING {self.sql(expression, 'using')}"
on = f"ON {self.sql(expression, 'on')}"
expressions = self.expressions(expression, sep=" ")
expressions = self.expressions(expression, sep=" ", indent=False)
sep = self.sep()

return self.prepend_ctes(
expression, f"MERGE INTO {this}{table_alias} {using} {on} {expressions}"
expression, f"MERGE INTO {this}{table_alias}{sep}{using}{sep}{on}{sep}{expressions}"
)

def tochar_sql(self, expression: exp.ToChar) -> str:
Expand Down
7 changes: 5 additions & 2 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2467,14 +2467,17 @@ def _parse_partition(self) -> t.Optional[exp.Partition]:
exp.Partition, expressions=self._parse_wrapped_csv(self._parse_conjunction)
)

def _parse_value(self) -> exp.Tuple:
def _parse_value(self) -> t.Optional[exp.Tuple]:
if self._match(TokenType.L_PAREN):
expressions = self._parse_csv(self._parse_expression)
self._match_r_paren()
return self.expression(exp.Tuple, expressions=expressions)

# In some dialects we can have VALUES 1, 2 which results in 1 column & 2 rows.
return self.expression(exp.Tuple, expressions=[self._parse_expression()])
expression = self._parse_expression()
if expression:
return self.expression(exp.Tuple, expressions=[expression])
return None

def _parse_projections(self) -> t.List[exp.Expression]:
return self._parse_expressions()
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class TestSnowflake(Validator):
dialect = "snowflake"

def test_snowflake(self):
self.validate_identity(
"MERGE INTO my_db AS ids USING (SELECT new_id FROM my_model WHERE NOT col IS NULL) AS new_ids ON ids.type = new_ids.type AND ids.source = new_ids.source WHEN NOT MATCHED THEN INSERT VALUES (new_ids.new_id)"
)
self.validate_identity("ALTER TABLE table1 CLUSTER BY (name DESC)")
self.validate_identity(
"INSERT OVERWRITE TABLE t SELECT 1", "INSERT OVERWRITE INTO t SELECT 1"
Expand Down

0 comments on commit 3768514

Please sign in to comment.