Skip to content

Commit aa46e93

Browse files
authored
Support UNNEST as a table factor (#493)
* support unnest * add test * fix ast * Update condition for BigQueryDialect or GenericDialect I updated condition. This changes conditionalize parsing for only BigQueryDialect or GenericDialect. * Add some tests * fix test
1 parent d19c6c3 commit aa46e93

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

src/ast/query.rs

+27
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,19 @@ pub enum TableFactor {
355355
expr: Expr,
356356
alias: Option<TableAlias>,
357357
},
358+
/// SELECT * FROM UNNEST ([10,20,30]) as numbers WITH OFFSET;
359+
/// +---------+--------+
360+
/// | numbers | offset |
361+
/// +---------+--------+
362+
/// | 10 | 0 |
363+
/// | 20 | 1 |
364+
/// | 30 | 2 |
365+
/// +---------+--------+
366+
UNNEST {
367+
alias: Option<TableAlias>,
368+
array_expr: Box<Expr>,
369+
with_offset: bool,
370+
},
358371
/// Represents a parenthesized table factor. The SQL spec only allows a
359372
/// join expression (`(foo <JOIN> bar [ <JOIN> baz ... ])`) to be nested,
360373
/// possibly several times.
@@ -406,6 +419,20 @@ impl fmt::Display for TableFactor {
406419
}
407420
Ok(())
408421
}
422+
TableFactor::UNNEST {
423+
alias,
424+
array_expr,
425+
with_offset,
426+
} => {
427+
write!(f, "UNNEST({})", array_expr)?;
428+
if let Some(alias) = alias {
429+
write!(f, " AS {}", alias)?;
430+
}
431+
if *with_offset {
432+
write!(f, " WITH OFFSET")?;
433+
}
434+
Ok(())
435+
}
409436
TableFactor::NestedJoin(table_reference) => write!(f, "({})", table_reference),
410437
}
411438
}

src/parser.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -3242,6 +3242,7 @@ impl<'a> Parser<'a> {
32423242
} else {
32433243
vec![]
32443244
};
3245+
32453246
let mut lateral_views = vec![];
32463247
loop {
32473248
if self.parse_keywords(&[Keyword::LATERAL, Keyword::VIEW]) {
@@ -3490,7 +3491,6 @@ impl<'a> Parser<'a> {
34903491

34913492
pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
34923493
let relation = self.parse_table_factor()?;
3493-
34943494
// Note that for keywords to be properly handled here, they need to be
34953495
// added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
34963496
// a table alias.
@@ -3635,6 +3635,7 @@ impl<'a> Parser<'a> {
36353635
match &mut table_and_joins.relation {
36363636
TableFactor::Derived { alias, .. }
36373637
| TableFactor::Table { alias, .. }
3638+
| TableFactor::UNNEST { alias, .. }
36383639
| TableFactor::TableFunction { alias, .. } => {
36393640
// but not `FROM (mytable AS alias1) AS alias2`.
36403641
if let Some(inner_alias) = alias {
@@ -3658,6 +3659,29 @@ impl<'a> Parser<'a> {
36583659
// appearing alone in parentheses (e.g. `FROM (mytable)`)
36593660
self.expected("joined table", self.peek_token())
36603661
}
3662+
} else if dialect_of!(self is BigQueryDialect | GenericDialect)
3663+
&& self.parse_keyword(Keyword::UNNEST)
3664+
{
3665+
self.expect_token(&Token::LParen)?;
3666+
let expr = self.parse_expr()?;
3667+
self.expect_token(&Token::RParen)?;
3668+
3669+
let alias = match self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS) {
3670+
Ok(Some(alias)) => Some(alias),
3671+
Ok(None) => None,
3672+
Err(e) => return Err(e),
3673+
};
3674+
3675+
let with_offset = match self.expect_keywords(&[Keyword::WITH, Keyword::OFFSET]) {
3676+
Ok(()) => true,
3677+
Err(_) => false,
3678+
};
3679+
3680+
Ok(TableFactor::UNNEST {
3681+
alias,
3682+
array_expr: Box::new(expr),
3683+
with_offset,
3684+
})
36613685
} else {
36623686
let name = self.parse_object_name()?;
36633687
// Postgres, MSSQL: table-valued functions:

tests/sqlparser_common.rs

+78
Original file line numberDiff line numberDiff line change
@@ -2786,6 +2786,84 @@ fn parse_table_function() {
27862786
);
27872787
}
27882788

2789+
#[test]
2790+
fn parse_unnest() {
2791+
fn chk(alias: bool, with_offset: bool, dialects: &TestedDialects, want: Vec<TableWithJoins>) {
2792+
let sql = &format!(
2793+
"SELECT * FROM UNNEST(expr){}{}",
2794+
if alias { " AS numbers" } else { "" },
2795+
if with_offset { " WITH OFFSET" } else { "" },
2796+
);
2797+
let select = dialects.verified_only_select(sql);
2798+
assert_eq!(select.from, want);
2799+
}
2800+
let dialects = TestedDialects {
2801+
dialects: vec![Box::new(BigQueryDialect {}), Box::new(GenericDialect {})],
2802+
};
2803+
// 1. both Alias and WITH OFFSET clauses.
2804+
chk(
2805+
true,
2806+
true,
2807+
&dialects,
2808+
vec![TableWithJoins {
2809+
relation: TableFactor::UNNEST {
2810+
alias: Some(TableAlias {
2811+
name: Ident::new("numbers"),
2812+
columns: vec![],
2813+
}),
2814+
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
2815+
with_offset: true,
2816+
},
2817+
joins: vec![],
2818+
}],
2819+
);
2820+
// 2. neither Alias nor WITH OFFSET clause.
2821+
chk(
2822+
false,
2823+
false,
2824+
&dialects,
2825+
vec![TableWithJoins {
2826+
relation: TableFactor::UNNEST {
2827+
alias: None,
2828+
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
2829+
with_offset: false,
2830+
},
2831+
joins: vec![],
2832+
}],
2833+
);
2834+
// 3. Alias but no WITH OFFSET clause.
2835+
chk(
2836+
false,
2837+
true,
2838+
&dialects,
2839+
vec![TableWithJoins {
2840+
relation: TableFactor::UNNEST {
2841+
alias: None,
2842+
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
2843+
with_offset: true,
2844+
},
2845+
joins: vec![],
2846+
}],
2847+
);
2848+
// 4. WITH OFFSET but no Alias.
2849+
chk(
2850+
true,
2851+
false,
2852+
&dialects,
2853+
vec![TableWithJoins {
2854+
relation: TableFactor::UNNEST {
2855+
alias: Some(TableAlias {
2856+
name: Ident::new("numbers"),
2857+
columns: vec![],
2858+
}),
2859+
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
2860+
with_offset: false,
2861+
},
2862+
joins: vec![],
2863+
}],
2864+
);
2865+
}
2866+
27892867
#[test]
27902868
fn parse_delimited_identifiers() {
27912869
// check that quoted identifiers in any position remain quoted after serialization

0 commit comments

Comments
 (0)