Skip to content

Commit

Permalink
Fix: don't generate connector comments when comments=False closes #3439
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed May 9, 2024
1 parent 273731f commit 761ba6f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2843,7 +2843,7 @@ def connector_sql(
stack.append(self.expressions(expression, sep=f" {op} "))
else:
stack.append(expression.right)
if expression.comments:
if expression.comments and self.comments:
for comment in expression.comments:
op += f" /*{self.pad_comment(comment)}*/"
stack.extend((op, expression.left))
Expand Down
15 changes: 9 additions & 6 deletions tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,21 +834,22 @@ def test_comment_alias(self):
b AS B,
c, /*comment*/
d AS D, -- another comment
CAST(x AS INT) -- final comment
CAST(x AS INT), -- yet another comment
y AND /* foo */ w AS E -- final comment
FROM foo
"""
expression = parse_one(sql)
self.assertEqual(
[e.alias_or_name for e in expression.expressions],
["a", "B", "c", "D", "x"],
["a", "B", "c", "D", "x", "E"],
)
self.assertEqual(
expression.sql(),
"SELECT a, b AS B, c /* comment */, d AS D /* another comment */, CAST(x AS INT) /* final comment */ FROM foo",
"SELECT a, b AS B, c /* comment */, d AS D /* another comment */, CAST(x AS INT) /* yet another comment */, y AND /* foo */ w AS E /* final comment */ FROM foo",
)
self.assertEqual(
expression.sql(comments=False),
"SELECT a, b AS B, c, d AS D, CAST(x AS INT) FROM foo",
"SELECT a, b AS B, c, d AS D, CAST(x AS INT), y AND w AS E FROM foo",
)
self.assertEqual(
expression.sql(pretty=True, comments=False),
Expand All @@ -857,7 +858,8 @@ def test_comment_alias(self):
b AS B,
c,
d AS D,
CAST(x AS INT)
CAST(x AS INT),
y AND w AS E
FROM foo""",
)
self.assertEqual(
Expand All @@ -867,7 +869,8 @@ def test_comment_alias(self):
b AS B,
c, /* comment */
d AS D, /* another comment */
CAST(x AS INT) /* final comment */
CAST(x AS INT), /* yet another comment */
y AND /* foo */ w AS E /* final comment */
FROM foo""",
)

Expand Down

0 comments on commit 761ba6f

Please sign in to comment.