Skip to content

Commit 8f8c96f

Browse files
authored
Support parsing empty map literal syntax for DuckDB and Genric (#1361)
1 parent d49acc6 commit 8f8c96f

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/parser/mod.rs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,17 +1849,9 @@ impl<'a> Parser<'a> {
18491849
/// Parses an array expression `[ex1, ex2, ..]`
18501850
/// if `named` is `true`, came from an expression like `ARRAY[ex1, ex2]`
18511851
pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
1852-
if self.peek_token().token == Token::RBracket {
1853-
let _ = self.next_token(); // consume ]
1854-
Ok(Expr::Array(Array {
1855-
elem: vec![],
1856-
named,
1857-
}))
1858-
} else {
1859-
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
1860-
self.expect_token(&Token::RBracket)?;
1861-
Ok(Expr::Array(Array { elem: exprs, named }))
1862-
}
1852+
let exprs = self.parse_comma_separated0(Parser::parse_expr, Token::RBracket)?;
1853+
self.expect_token(&Token::RBracket)?;
1854+
Ok(Expr::Array(Array { elem: exprs, named }))
18631855
}
18641856

18651857
pub fn parse_listagg_on_overflow(&mut self) -> Result<Option<ListAggOnOverflow>, ParserError> {
@@ -2352,11 +2344,8 @@ impl<'a> Parser<'a> {
23522344
/// [map]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
23532345
fn parse_duckdb_map_literal(&mut self) -> Result<Expr, ParserError> {
23542346
self.expect_token(&Token::LBrace)?;
2355-
2356-
let fields = self.parse_comma_separated(Self::parse_duckdb_map_field)?;
2357-
2347+
let fields = self.parse_comma_separated0(Self::parse_duckdb_map_field, Token::RBrace)?;
23582348
self.expect_token(&Token::RBrace)?;
2359-
23602349
Ok(Expr::Map(Map { entries: fields }))
23612350
}
23622351

@@ -2937,7 +2926,7 @@ impl<'a> Parser<'a> {
29372926
Expr::InList {
29382927
expr: Box::new(expr),
29392928
list: if self.dialect.supports_in_empty_list() {
2940-
self.parse_comma_separated0(Parser::parse_expr)?
2929+
self.parse_comma_separated0(Parser::parse_expr, Token::RParen)?
29412930
} else {
29422931
self.parse_comma_separated(Parser::parse_expr)?
29432932
},
@@ -3479,18 +3468,20 @@ impl<'a> Parser<'a> {
34793468
}
34803469

34813470
/// Parse a comma-separated list of 0+ items accepted by `F`
3482-
pub fn parse_comma_separated0<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError>
3471+
/// * `end_token` - expected end token for the closure (e.g. [Token::RParen], [Token::RBrace] ...)
3472+
pub fn parse_comma_separated0<T, F>(
3473+
&mut self,
3474+
f: F,
3475+
end_token: Token,
3476+
) -> Result<Vec<T>, ParserError>
34833477
where
34843478
F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
34853479
{
3486-
// ()
3487-
if matches!(self.peek_token().token, Token::RParen) {
3480+
if self.peek_token().token == end_token {
34883481
return Ok(vec![]);
34893482
}
3490-
// (,)
3491-
if self.options.trailing_commas
3492-
&& matches!(self.peek_tokens(), [Token::Comma, Token::RParen])
3493-
{
3483+
3484+
if self.options.trailing_commas && self.peek_tokens() == [Token::Comma, end_token] {
34943485
let _ = self.consume_token(&Token::Comma);
34953486
return Ok(vec![]);
34963487
}
@@ -4059,7 +4050,7 @@ impl<'a> Parser<'a> {
40594050
})
40604051
};
40614052
self.expect_token(&Token::LParen)?;
4062-
let args = self.parse_comma_separated0(parse_function_param)?;
4053+
let args = self.parse_comma_separated0(parse_function_param, Token::RParen)?;
40634054
self.expect_token(&Token::RParen)?;
40644055

40654056
let return_type = if self.parse_keyword(Keyword::RETURNS) {
@@ -10713,7 +10704,8 @@ impl<'a> Parser<'a> {
1071310704
}
1071410705

1071510706
if self.consume_token(&Token::LParen) {
10716-
let interpolations = self.parse_comma_separated0(|p| p.parse_interpolation())?;
10707+
let interpolations =
10708+
self.parse_comma_separated0(|p| p.parse_interpolation(), Token::RParen)?;
1071710709
self.expect_token(&Token::RParen)?;
1071810710
// INTERPOLATE () and INTERPOLATE ( ... ) variants
1071910711
return Ok(Some(Interpolate {

tests/sqlparser_common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10282,6 +10282,8 @@ fn test_map_syntax() {
1028210282
}),
1028310283
},
1028410284
);
10285+
10286+
check("MAP {}", Expr::Map(Map { entries: vec![] }));
1028510287
}
1028610288

1028710289
#[test]

0 commit comments

Comments
 (0)