Skip to content

Commit

Permalink
Fix: performance regression due to 6f80cc8
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jul 4, 2023
1 parent 5a84605 commit d1ad7da
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
23 changes: 11 additions & 12 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2277,23 +2277,22 @@ def _parse_join(self, skip_join_token: bool = False) -> t.Optional[exp.Join]:
if hint:
kwargs["hint"] = hint

def parse_join_condition(kwargs: t.Dict) -> None:
if self._match(TokenType.ON):
kwargs["on"] = self._parse_conjunction()
elif self._match(TokenType.USING):
kwargs["using"] = self._parse_wrapped_id_vars()

parse_join_condition(kwargs)

if not (kind and kind.token_type == TokenType.CROSS):
if self._match(TokenType.ON):
kwargs["on"] = self._parse_conjunction()
elif self._match(TokenType.USING):
kwargs["using"] = self._parse_wrapped_id_vars()
elif not (kind and kind.token_type == TokenType.CROSS):
index = self._index
joins = self._parse_joins()

if joins and self._match_set({TokenType.ON, TokenType.USING}, advance=False):
parse_join_condition(kwargs)
kwargs["this"].set("joins", joins)
if joins and self._match(TokenType.ON):
kwargs["on"] = self._parse_conjunction()
elif joins and self._match(TokenType.USING):
kwargs["using"] = self._parse_wrapped_id_vars()
else:
joins = None
self._retreat(index)
kwargs["this"].set("joins", joins)

return self.expression(exp.Join, **kwargs)

Expand Down
50 changes: 50 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import unittest
from unittest.mock import patch

Expand Down Expand Up @@ -523,6 +524,55 @@ def test_pivot_columns(self):
columns = expr.args["from"].this.args["pivots"][0].args["columns"]
self.assertEqual(expected_columns, [col.sql(dialect=dialect) for col in columns])

def test_parse_nested(self):
now = time.time()
query = parse_one(
"""
select *
FROM a
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
LEFT JOIN b ON a.id = b.id
"""
)
self.assertIsNotNone(query)
self.assertLessEqual(time.time() - now, 0.1)

def test_parse_properties(self):
self.assertEqual(
parse_one("create materialized table x").sql(), "CREATE MATERIALIZED TABLE x"
Expand Down

0 comments on commit d1ad7da

Please sign in to comment.