Skip to content

Commit 9a70c42

Browse files
authored
PostgreSQL: Add support for LATERAL ... WITH ORDINALITY (#2304)
1 parent 53dcc82 commit 9a70c42

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

src/ast/query.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,8 @@ pub enum TableFactor {
15221522
name: ObjectName,
15231523
/// Arguments passed to the function.
15241524
args: Vec<FunctionArg>,
1525+
/// Whether `WITH ORDINALITY` was specified to include ordinality.
1526+
with_ordinality: bool,
15251527
/// Optional alias for the result of the function.
15261528
alias: Option<TableAlias>,
15271529
},
@@ -2277,13 +2279,17 @@ impl fmt::Display for TableFactor {
22772279
lateral,
22782280
name,
22792281
args,
2282+
with_ordinality,
22802283
alias,
22812284
} => {
22822285
if *lateral {
22832286
write!(f, "LATERAL ")?;
22842287
}
22852288
write!(f, "{name}")?;
22862289
write!(f, "({})", display_comma_separated(args))?;
2290+
if *with_ordinality {
2291+
write!(f, " WITH ORDINALITY")?;
2292+
}
22872293
if let Some(alias) = alias {
22882294
write!(f, " {alias}")?;
22892295
}

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,7 @@ impl Spanned for TableFactor {
19901990
lateral: _,
19911991
name,
19921992
args,
1993+
with_ordinality: _,
19931994
alias,
19941995
} => union_spans(
19951996
name.0

src/parser/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15898,11 +15898,13 @@ impl<'a> Parser<'a> {
1589815898
let name = self.parse_object_name(false)?;
1589915899
self.expect_token(&Token::LParen)?;
1590015900
let args = self.parse_optional_args()?;
15901+
let with_ordinality = self.parse_keywords(&[Keyword::WITH, Keyword::ORDINALITY]);
1590115902
let alias = self.maybe_parse_table_alias()?;
1590215903
Ok(TableFactor::Function {
1590315904
lateral: true,
1590415905
name,
1590515906
args,
15907+
with_ordinality,
1590615908
alias,
1590715909
})
1590815910
}

tests/sqlparser_common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9028,6 +9028,7 @@ fn lateral_function() {
90289028
vec![Ident::new("customer"), Ident::new("id")],
90299029
))),
90309030
],
9031+
with_ordinality: false,
90319032
alias: None,
90329033
},
90339034
global: false,

tests/sqlparser_postgres.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6136,6 +6136,34 @@ fn test_table_function_with_ordinality() {
61366136
}
61376137
}
61386138

6139+
#[test]
6140+
fn test_lateral_function_with_ordinality_and_column_aliases() {
6141+
let from = pg()
6142+
.verified_only_select(
6143+
"SELECT * FROM tbl, \
6144+
LATERAL json_array_elements(c1::JSON) \
6145+
WITH ORDINALITY AS t (c1, index)",
6146+
)
6147+
.from;
6148+
assert_eq!(2, from.len());
6149+
match &from[1].relation {
6150+
TableFactor::Function {
6151+
lateral: true,
6152+
name,
6153+
with_ordinality: true,
6154+
alias: Some(alias),
6155+
..
6156+
} => {
6157+
assert_eq!("json_array_elements", name.to_string().as_str());
6158+
assert_eq!("t", alias.name.value.as_str());
6159+
assert_eq!(2, alias.columns.len());
6160+
assert_eq!("c1", alias.columns[0].name.value.as_str());
6161+
assert_eq!("index", alias.columns[1].name.value.as_str());
6162+
}
6163+
_ => panic!("Expecting TableFactor::Function with ordinality and alias columns"),
6164+
}
6165+
}
6166+
61396167
#[test]
61406168
fn test_table_unnest_with_ordinality() {
61416169
let from = pg_and_generic()

0 commit comments

Comments
 (0)