Skip to content

Commit

Permalink
Fix: bubble up comments in qualified column refs fixes #4353
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Nov 6, 2024
1 parent 87ab8fe commit eb8e2fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5138,6 +5138,9 @@ def _parse_column_ops(self, this: t.Optional[exp.Expression]) -> t.Optional[exp.
else:
this = self.expression(exp.Dot, this=this, expression=field)

if field and field.comments:
t.cast(exp.Expression, this).add_comments(field.pop_comments())

this = self._parse_bracket(this)

return self._parse_colon_as_variant_extract(this) if self.COLON_IS_VARIANT_EXTRACT else this
Expand Down
18 changes: 16 additions & 2 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def test_comments_select(self):
e, --
f -- space
FROM foo
"""
"""
)

self.assertEqual(expression.comments, ["comment1.1", "comment1.2", "comment1.3"])
Expand All @@ -340,6 +340,20 @@ def test_comments_select(self):
self.assertEqual(expression.expressions[4].comments, [""])
self.assertEqual(expression.expressions[5].comments, [" space"])

expression = parse_one(
"""
SELECT a.column_name --# Comment 1
,b.column_name2, --# Comment 2
b.column_name3 AS NAME3 --# Comment 3
FROM table_name a
JOIN table_name2 b ON a.column_name = b.column_name
"""
)

self.assertEqual(expression.expressions[0].comments, ["# Comment 1"])
self.assertEqual(expression.expressions[1].comments, ["# Comment 2"])
self.assertEqual(expression.expressions[2].comments, ["# Comment 3"])

def test_comments_select_cte(self):
expression = parse_one(
"""
Expand All @@ -350,7 +364,7 @@ def test_comments_select_cte(self):
a.*
FROM /*comment3*/
a
"""
"""
)

self.assertEqual(expression.comments, ["comment2"])
Expand Down

0 comments on commit eb8e2fe

Please sign in to comment.