diff --git a/sqlglot/generator.py b/sqlglot/generator.py index 5e5cd176f7..4a2d34ad2f 100644 --- a/sqlglot/generator.py +++ b/sqlglot/generator.py @@ -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}" diff --git a/sqlglot/parser.py b/sqlglot/parser.py index e59963ac17..dd94acf521 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -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, ) diff --git a/tests/test_transpile.py b/tests/test_transpile.py index ad8ec723a3..701f8ef4ea 100644 --- a/tests/test_transpile.py +++ b/tests/test_transpile.py @@ -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 @@ -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,