Skip to content

Commit

Permalink
Fix: missing from comment and spacing closes tobymao#1631
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao authored and adrianisk committed Jun 21, 2023
1 parent f1a128b commit 3c5ac15
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,11 @@ def maybe_comment(
return sql

if isinstance(expression, self.WITH_SEPARATED_COMMENTS):
return f"{comments_sql}{self.sep()}{sql}"
return (
f"{self.sep()}{comments_sql}{sql}"
if sql[0].isspace()
else f"{comments_sql}{self.sep()}{sql}"
)

return f"{sql} {comments_sql}"

Expand Down
3 changes: 2 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2028,11 +2028,12 @@ def _parse_from(self, modifiers: bool = False) -> t.Optional[exp.Expression]:
if not self._match(TokenType.FROM):
return None

comments = self._prev_comments
this = self._parse_table()

return self.expression(
exp.From,
comments=self._prev_comments,
comments=comments,
this=self._parse_query_modifiers(this) if modifiers else this,
)

Expand Down
7 changes: 6 additions & 1 deletion tests/test_transpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ def test_comments(self):
)
self.validate(
"""
select a from b
select a
-- from
from b
-- where
where foo
-- comment 1
and bar
Expand All @@ -233,7 +236,9 @@ def test_comments(self):
""",
"""SELECT
a
/* from */
FROM b
/* where */
WHERE
foo /* comment 1 */ AND bar AND bla /* comment 2 */""",
pretty=True,
Expand Down

0 comments on commit 3c5ac15

Please sign in to comment.