Skip to content

Commit 0363fef

Browse files
authored
Fix: attach comments to Commands (#3758)
1 parent 15ca924 commit 0363fef

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

sqlglot/generator.py

+1
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ class Generator(metaclass=_Generator):
512512

513513
# Expressions whose comments are separated from them for better formatting
514514
WITH_SEPARATED_COMMENTS: t.Tuple[t.Type[exp.Expression], ...] = (
515+
exp.Command,
515516
exp.Create,
516517
exp.Delete,
517518
exp.Drop,

sqlglot/parser.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,10 @@ def _warn_unsupported(self) -> None:
14781478
def _parse_command(self) -> exp.Command:
14791479
self._warn_unsupported()
14801480
return self.expression(
1481-
exp.Command, this=self._prev.text.upper(), expression=self._parse_string()
1481+
exp.Command,
1482+
comments=self._prev_comments,
1483+
this=self._prev.text.upper(),
1484+
expression=self._parse_string(),
14821485
)
14831486

14841487
def _try_parse(self, parse_method: t.Callable[[], T], retreat: bool = False) -> t.Optional[T]:

tests/dialects/test_bigquery.py

+6
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,12 @@ def test_errors(self):
14271427
transpile("DATE_ADD(x, day)", read="bigquery")
14281428

14291429
def test_warnings(self):
1430+
with self.assertLogs(parser_logger) as cm:
1431+
self.validate_identity(
1432+
"/* some comment */ DECLARE foo DATE DEFAULT DATE_SUB(current_date, INTERVAL 2 day)"
1433+
)
1434+
self.assertIn("contains unsupported syntax", cm.output[0])
1435+
14301436
with self.assertLogs(helper_logger) as cm:
14311437
self.validate_identity(
14321438
"WITH cte(c) AS (SELECT * FROM t) SELECT * FROM cte",

0 commit comments

Comments
 (0)