Skip to content

Commit 2b928e2

Browse files
authored
Feat: improve pretty-printing of MERGE statement (#5102)
1 parent f5f4ca1 commit 2b928e2

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

sqlglot/generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3841,7 +3841,7 @@ def when_sql(self, expression: exp.When) -> str:
38413841
if isinstance(then_expression.args.get("expressions"), exp.Star):
38423842
then = f"UPDATE {self.sql(then_expression, 'expressions')}"
38433843
else:
3844-
then = f"UPDATE SET {self.expressions(then_expression, flat=True)}"
3844+
then = f"UPDATE SET{self.sep()}{self.expressions(then_expression)}"
38453845
else:
38463846
then = self.sql(then_expression)
38473847
return f"WHEN {matched}{source}{condition} THEN {then}"

tests/dialects/test_tsql.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,8 @@ def test_ddl(self):
962962
ProductID
963963
) AS src(ProductID, OrderQty)
964964
ON pi.ProductID = src.ProductID
965-
WHEN MATCHED AND pi.Quantity - src.OrderQty >= 0 THEN UPDATE SET pi.Quantity = pi.Quantity - src.OrderQty
965+
WHEN MATCHED AND pi.Quantity - src.OrderQty >= 0 THEN UPDATE SET
966+
pi.Quantity = pi.Quantity - src.OrderQty
966967
WHEN MATCHED AND pi.Quantity - src.OrderQty <= 0 THEN DELETE
967968
OUTPUT $action, Inserted.ProductID, Inserted.LocationID, Inserted.Quantity AS NewQty, Deleted.Quantity AS PreviousQty
968969
) AS Changes(Action, ProductID, LocationID, NewQty, PreviousQty)

tests/fixtures/pretty.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,11 @@ SELECT
449449
FROM foo
450450
WHERE
451451
1 = 1 AND /* first comment */ foo.a /* second comment */ = 1;
452+
453+
MERGE INTO t USING s ON t.id = s.id WHEN MATCHED THEN UPDATE SET status = s.status, amount = s.amount;
454+
MERGE INTO t
455+
USING s
456+
ON t.id = s.id
457+
WHEN MATCHED THEN UPDATE SET
458+
status = s.status,
459+
amount = s.amount;

0 commit comments

Comments
 (0)