Skip to content

Commit

Permalink
Fix: fix perf issues with nested left joins
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Oct 1, 2023
1 parent a794bfe commit 5fb7174
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
10 changes: 5 additions & 5 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,17 +2461,17 @@ def _parse_join(
kwargs["using"] = self._parse_wrapped_id_vars()
elif not (kind and kind.token_type == TokenType.CROSS):
index = self._index
joins = self._parse_joins()
join = self._parse_join()

if joins and self._match(TokenType.ON):
if join and self._match(TokenType.ON):
kwargs["on"] = self._parse_conjunction()
elif joins and self._match(TokenType.USING):
elif join and self._match(TokenType.USING):
kwargs["using"] = self._parse_wrapped_id_vars()
else:
joins = None
join = None
self._retreat(index)

kwargs["this"].set("joins", joins)
kwargs["this"].set("joins", [join] if join else None)

comments = [c for token in (method, side, kind) if token for c in token.comments]
return self.expression(exp.Join, comments=comments, **kwargs)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,31 @@ def test_parse_nested(self):
LEFT JOIN b ON a.id = b.id
"""
)

self.assertIsNotNone(query)

query = parse_one(
"""
SELECT *
FROM a
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
LEFT JOIN UNNEST(ARRAY[])
"""
)

self.assertIsNotNone(query)
self.assertLessEqual(time.time() - now, 0.2)

Expand Down

0 comments on commit 5fb7174

Please sign in to comment.