Skip to content

Commit

Permalink
Feat: include comments in gen (#4535)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Dec 18, 2024
1 parent cd6e00f commit 52c8374
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions sqlglot/optimizer/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,28 +1328,35 @@ def _flat_simplify(expression, simplifier, root=True):
return expression


def gen(expression: t.Any) -> str:
def gen(expression: t.Any, comments: bool = False) -> str:
"""Simple pseudo sql generator for quickly generating sortable and uniq strings.
Sorting and deduping sql is a necessary step for optimization. Calling the actual
generator is expensive so we have a bare minimum sql generator here.
Args:
expression: the expression to convert into a SQL string.
comments: whether to include the expression's comments.
"""
return Gen().gen(expression)
return Gen().gen(expression, comments=comments)


class Gen:
def __init__(self):
self.stack = []
self.sqls = []

def gen(self, expression: exp.Expression) -> str:
def gen(self, expression: exp.Expression, comments: bool = False) -> str:
self.stack = [expression]
self.sqls.clear()

while self.stack:
node = self.stack.pop()

if isinstance(node, exp.Expression):
if comments and node.comments:
self.stack.append(f" /*{','.join(node.comments)}*/")

exp_handler_name = f"{node.key}_sql"

if hasattr(self, exp_handler_name):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,10 @@ def test_simplify(self):
SELECT :with,WITH :expressions,CTE :this,UNION :this,SELECT :expressions,1,:expression,SELECT :expressions,2,:distinct,True,:alias, AS cte,CTE :this,SELECT :expressions,WINDOW :this,ROW(),:partition_by,y,:over,OVER,:from,FROM ((SELECT :expressions,1):limit,LIMIT :expression,10),:alias, AS cte2,:expressions,STAR,a + 1,a DIV 1,FILTER("B",LAMBDA :this,x + y,:expressions,x,y),:from,FROM (z AS z:joins,JOIN :this,z,:kind,CROSS) AS f(a),:joins,JOIN :this,a.b.c.d.e.f.g,:side,LEFT,:using,n,:order,ORDER :expressions,ORDERED :this,1,:nulls_first,True
""".strip(),
)
self.assertEqual(
optimizer.simplify.gen(parse_one("select item_id /* description */"), comments=True),
"SELECT :expressions,item_id /* description */",
)

def test_unnest_subqueries(self):
self.check_file("unnest_subqueries", optimizer.unnest_subqueries.unnest_subqueries)
Expand Down

0 comments on commit 52c8374

Please sign in to comment.