LucaCappelletti94 commented on code in PR #2524:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2524#discussion_r4098432376
##########
src/parser/mod.rs:
##########
@@ -16813,6 +16795,67 @@ impl<'a> Parser<'a> {
}
}
+ fn parse_rows_from_function(&mut self) -> Result<TableFactor, ParserError>
{
+ if self.parse_keyword(Keyword::UNNEST) {
+ return self.parse_unnest_table_factor();
+ }
+
+ let name = self.parse_object_name(true)?;
+ self.expect_token(&Token::LParen)?;
+ let args = Some(self.parse_table_function_args()?);
+ let with_ordinality = self.parse_keywords(&[Keyword::WITH,
Keyword::ORDINALITY]);
+ let alias = self.maybe_parse_table_alias()?;
+
+ Ok(TableFactor::Table {
+ name,
+ alias,
+ args,
+ with_hints: vec![],
+ version: None,
+ partitions: vec![],
+ with_ordinality,
+ json_path: None,
+ sample: None,
+ index_hints: vec![],
+ })
+ }
Review Comment:
You should parse each `ROWS FROM` item as a bare function call. I will
provide some example red tests shortly.
```suggestion
fn parse_rows_from_function(&mut self) -> Result<TableFactor,
ParserError> {
if self.parse_keyword(Keyword::UNNEST) {
self.expect_token(&Token::LParen)?;
let array_exprs =
self.parse_comma_separated(Parser::parse_expr)?;
self.expect_token(&Token::RParen)?;
return Ok(TableFactor::UNNEST {
alias: None,
array_exprs,
with_offset: false,
with_offset_alias: None,
with_ordinality: false,
});
}
let name = self.parse_object_name(true)?;
self.expect_token(&Token::LParen)?;
let args = Some(self.parse_table_function_args()?);
Ok(TableFactor::Table {
name,
alias: None,
args,
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
})
}
```
##########
tests/sqlparser_postgres.rs:
##########
@@ -10037,3 +10037,23 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_rows_from_with_ordinality() {
+ pg_and_generic().verified_stmt(
+ "SELECT * FROM ROWS FROM(UNNEST(ARRAY[1, 2]) WITH ORDINALITY) AS r
(val, ord)",
+ );
Review Comment:
You may want to turn this case into a negative one, since PostgreSQL rejects
it with `syntax error at or near "WITH"`. The loop below fails on the current
parser and passes with the change above.
```suggestion
for sql in [
"SELECT * FROM ROWS FROM(UNNEST(ARRAY[1, 2]) WITH ORDINALITY) AS r",
"SELECT * FROM ROWS FROM(UNNEST(ARRAY[1]) WITH OFFSET) AS r",
"SELECT * FROM ROWS FROM(generate_series(1, 2) WITH ORDINALITY) AS
r",
"SELECT * FROM ROWS FROM(generate_series(1, 2) AS g) AS r",
"SELECT * FROM ROWS FROM(generate_series(1, 2) g) AS r",
] {
assert!(pg_and_generic().parse_sql_statements(sql).is_err());
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]