Skip to content

Commit 7bc5a21

Browse files
authored
Feat(postgres): support laterals with ordinality fixes #4965 (#4966)
1 parent 72cf4a4 commit 7bc5a21

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

sqlglot/expressions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,7 @@ class Lateral(UDTF):
25842584
"outer": False,
25852585
"alias": False,
25862586
"cross_apply": False, # True -> CROSS APPLY, False -> OUTER APPLY
2587+
"ordinality": False,
25872588
}
25882589

25892590

sqlglot/generator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,13 @@ def lateral_sql(self, expression: exp.Lateral) -> str:
23072307

23082308
alias = self.sql(expression, "alias")
23092309
alias = f" AS {alias}" if alias else ""
2310-
return f"{self.lateral_op(expression)} {this}{alias}"
2310+
2311+
ordinality = expression.args.get("ordinality") or ""
2312+
if ordinality:
2313+
ordinality = f" WITH ORDINALITY{alias}"
2314+
alias = ""
2315+
2316+
return f"{self.lateral_op(expression)} {this}{alias}{ordinality}"
23112317

23122318
def limit_sql(self, expression: exp.Limit, top: bool = False) -> str:
23132319
this = self.sql(expression, "this")

sqlglot/parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,6 +3576,8 @@ def _parse_lateral(self) -> t.Optional[exp.Lateral]:
35763576
expression=self._parse_function() or self._parse_id_var(any_token=False),
35773577
)
35783578

3579+
ordinality: t.Optional[bool] = None
3580+
35793581
if view:
35803582
table = self._parse_id_var(any_token=False)
35813583
columns = self._parse_csv(self._parse_id_var) if self._match(TokenType.ALIAS) else []
@@ -3586,6 +3588,7 @@ def _parse_lateral(self) -> t.Optional[exp.Lateral]:
35863588
# We move the alias from the lateral's child node to the lateral itself
35873589
table_alias = this.args["alias"].pop()
35883590
else:
3591+
ordinality = self._match_pair(TokenType.WITH, TokenType.ORDINALITY)
35893592
table_alias = self._parse_table_alias()
35903593

35913594
return self.expression(
@@ -3595,6 +3598,7 @@ def _parse_lateral(self) -> t.Optional[exp.Lateral]:
35953598
outer=outer,
35963599
alias=table_alias,
35973600
cross_apply=cross_apply,
3601+
ordinality=ordinality,
35983602
)
35993603

36003604
def _parse_join_parts(

tests/dialects/test_postgres.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def test_postgres(self):
7676
self.validate_identity("SELECT CURRENT_SCHEMA")
7777
self.validate_identity("SELECT CURRENT_USER")
7878
self.validate_identity("SELECT * FROM ONLY t1")
79+
self.validate_identity(
80+
"SELECT * FROM test_data, LATERAL JSONB_ARRAY_ELEMENTS(data) WITH ORDINALITY AS elem(value, ordinality)"
81+
)
7982
self.validate_identity(
8083
"SELECT id, name FROM xml_data AS t, XMLTABLE('/root/user' PASSING t.xml COLUMNS id INT PATH '@id', name TEXT PATH 'name/text()') AS x"
8184
)

0 commit comments

Comments
 (0)