Skip to content

Commit

Permalink
Fix: join/pivot/lateral order and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed May 13, 2023
1 parent 7d98790 commit 4601831
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 23 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dev": [
"autoflake",
"black",
"duckdb",
"duckdb>=0.6",
"isort",
"mypy>=0.990",
"pandas",
Expand Down
6 changes: 3 additions & 3 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,13 +1160,13 @@ def table_sql(self, expression: exp.Table, sep: str = " AS ") -> str:
alias = f"{sep}{alias}" if alias else ""
hints = self.expressions(expression, key="hints", sep=", ", flat=True)
hints = f" WITH ({hints})" if hints and self.TABLE_HINTS else ""
laterals = self.expressions(expression, key="laterals", sep="")
joins = self.expressions(expression, key="joins", sep="")
pivots = self.expressions(expression, key="pivots", sep="")
joins = self.expressions(expression, key="joins", sep="")
laterals = self.expressions(expression, key="laterals", sep="")
system_time = expression.args.get("system_time")
system_time = f" {self.sql(expression, 'system_time')}" if system_time else ""

return f"{table}{system_time}{alias}{hints}{laterals}{joins}{pivots}"
return f"{table}{system_time}{alias}{hints}{pivots}{joins}{laterals}"

def tablesample_sql(
self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
Expand Down
24 changes: 6 additions & 18 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ class Parser(metaclass=_Parser):
}

QUERY_MODIFIER_PARSERS = {
"laterals": lambda self: list(iter(self._parse_lateral, None)),
"match": lambda self: self._parse_match_recognize(),
"where": lambda self: self._parse_where(),
"group": lambda self: self._parse_group(),
Expand Down Expand Up @@ -1979,25 +1980,12 @@ def _parse_query_modifiers(self, this: t.Optional[exp.Expression]) -> None:
if not isinstance(this, self.MODIFIABLES):
return

table = isinstance(this, exp.Table)

while True:
join = self._parse_join()
if join:
this.append("joins", join)

lateral = None
if not join:
lateral = self._parse_lateral()
if lateral:
this.append("laterals", lateral)

comma = None if table else self._match(TokenType.COMMA)
if comma:
join = self._parse_join()
while join:
this.append("joins", join)
while self._match(TokenType.COMMA):
this.args["from"].append("expressions", self._parse_table())

if not (lateral or join or comma):
break
join = self._parse_join()

for key, parser in self.QUERY_MODIFIER_PARSERS.items():
expression = parser(self)
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ SELECT 1 AS delete, 2 AS alter
SELECT * FROM (x)
SELECT * FROM ((x))
SELECT * FROM ((SELECT 1))
SELECT * FROM (x LATERAL VIEW EXPLODE(y) JOIN foo)
SELECT * FROM (x JOIN foo LATERAL VIEW EXPLODE(y))
SELECT * FROM (SELECT 1) AS x
SELECT * FROM (SELECT 1 UNION SELECT 2) AS x
SELECT * FROM (SELECT 1 UNION ALL SELECT 2) AS x
Expand Down

0 comments on commit 4601831

Please sign in to comment.