Skip to content

Commit

Permalink
Fix: preserve comments in exp.Drop
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Jul 13, 2023
1 parent 4f088d9 commit cc33749
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ class Generator:

# Expressions whose comments are separated from them for better formatting
WITH_SEPARATED_COMMENTS: t.Tuple[t.Type[exp.Expression], ...] = (
exp.Select,
exp.Drop,
exp.From,
exp.Select,
exp.Where,
exp.With,
)
Expand Down
1 change: 1 addition & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@ def _parse_drop(self) -> exp.Drop | exp.Command:

return self.expression(
exp.Drop,
comments=start.comments,
exists=self._parse_exists(),
this=self._parse_table(schema=True),
kind=kind,
Expand Down
41 changes: 41 additions & 0 deletions tests/test_transpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,47 @@ def test_comments(self):
"SELECT 1 /* hi this is a comment */",
read="snowflake",
)
self.validate(
"-- comment\nDROP TABLE IF EXISTS foo",
"/* comment */ DROP TABLE IF EXISTS foo",
)
self.validate(
"""
-- comment1
-- comment2
-- comment3
DROP TABLE IF EXISTS db.tba
""",
"""/* comment1 */
/* comment2 */
/* comment3 */
DROP TABLE IF EXISTS db.tba""",
pretty=True,
)
self.validate(
"""
CREATE TABLE db.tba AS
SELECT a, b, c
FROM tb_01
WHERE
-- comment5
a = 1 AND b = 2 --comment6
-- and c = 1
-- comment7
""",
"""CREATE TABLE db.tba AS
SELECT
a,
b,
c
FROM tb_01
WHERE
a /* comment5 */ = 1 AND b = 2 /* comment6 */
/* and c = 1 */
/* comment7 */""",
pretty=True,
)

def test_types(self):
self.validate("INT 1", "CAST(1 AS INT)")
Expand Down

0 comments on commit cc33749

Please sign in to comment.