Skip to content

Commit 32b6609

Browse files
goldmedalayman-sigma
authored andcommitted
Support parsing empty map literal syntax for DuckDB and Genric (apache#1361)
1 parent c3eb076 commit 32b6609

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/parser/mod.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -1853,17 +1853,9 @@ impl<'a> Parser<'a> {
18531853
/// Parses an array expression `[ex1, ex2, ..]`
18541854
/// if `named` is `true`, came from an expression like `ARRAY[ex1, ex2]`
18551855
pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
1856-
if self.peek_token().token == Token::RBracket {
1857-
let _ = self.next_token(); // consume ]
1858-
Ok(Expr::Array(Array {
1859-
elem: vec![],
1860-
named,
1861-
}))
1862-
} else {
1863-
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
1864-
self.expect_token(&Token::RBracket)?;
1865-
Ok(Expr::Array(Array { elem: exprs, named }))
1866-
}
1856+
let exprs = self.parse_comma_separated0(Parser::parse_expr, Token::RBracket)?;
1857+
self.expect_token(&Token::RBracket)?;
1858+
Ok(Expr::Array(Array { elem: exprs, named }))
18671859
}
18681860

18691861
pub fn parse_listagg_on_overflow(&mut self) -> Result<Option<ListAggOnOverflow>, ParserError> {
@@ -2356,11 +2348,8 @@ impl<'a> Parser<'a> {
23562348
/// [map]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
23572349
fn parse_duckdb_map_literal(&mut self) -> Result<Expr, ParserError> {
23582350
self.expect_token(&Token::LBrace)?;
2359-
2360-
let fields = self.parse_comma_separated(Self::parse_duckdb_map_field)?;
2361-
2351+
let fields = self.parse_comma_separated0(Self::parse_duckdb_map_field, Token::RBrace)?;
23622352
self.expect_token(&Token::RBrace)?;
2363-
23642353
Ok(Expr::Map(Map { entries: fields }))
23652354
}
23662355

@@ -2942,7 +2931,7 @@ impl<'a> Parser<'a> {
29422931
Expr::InList {
29432932
expr: Box::new(expr),
29442933
list: if self.dialect.supports_in_empty_list() {
2945-
self.parse_comma_separated0(Parser::parse_expr)?
2934+
self.parse_comma_separated0(Parser::parse_expr, Token::RParen)?
29462935
} else {
29472936
self.parse_comma_separated(Parser::parse_expr)?
29482937
},
@@ -3493,18 +3482,20 @@ impl<'a> Parser<'a> {
34933482
}
34943483

34953484
/// Parse a comma-separated list of 0+ items accepted by `F`
3496-
pub fn parse_comma_separated0<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError>
3485+
/// * `end_token` - expected end token for the closure (e.g. [Token::RParen], [Token::RBrace] ...)
3486+
pub fn parse_comma_separated0<T, F>(
3487+
&mut self,
3488+
f: F,
3489+
end_token: Token,
3490+
) -> Result<Vec<T>, ParserError>
34973491
where
34983492
F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
34993493
{
3500-
// ()
3501-
if matches!(self.peek_token().token, Token::RParen) {
3494+
if self.peek_token().token == end_token {
35023495
return Ok(vec![]);
35033496
}
3504-
// (,)
3505-
if self.options.trailing_commas
3506-
&& matches!(self.peek_tokens(), [Token::Comma, Token::RParen])
3507-
{
3497+
3498+
if self.options.trailing_commas && self.peek_tokens() == [Token::Comma, end_token] {
35083499
let _ = self.consume_token(&Token::Comma);
35093500
return Ok(vec![]);
35103501
}
@@ -4073,7 +4064,7 @@ impl<'a> Parser<'a> {
40734064
})
40744065
};
40754066
self.expect_token(&Token::LParen)?;
4076-
let args = self.parse_comma_separated0(parse_function_param)?;
4067+
let args = self.parse_comma_separated0(parse_function_param, Token::RParen)?;
40774068
self.expect_token(&Token::RParen)?;
40784069

40794070
let return_type = if self.parse_keyword(Keyword::RETURNS) {
@@ -10742,7 +10733,8 @@ impl<'a> Parser<'a> {
1074210733
}
1074310734

1074410735
if self.consume_token(&Token::LParen) {
10745-
let interpolations = self.parse_comma_separated0(|p| p.parse_interpolation())?;
10736+
let interpolations =
10737+
self.parse_comma_separated0(|p| p.parse_interpolation(), Token::RParen)?;
1074610738
self.expect_token(&Token::RParen)?;
1074710739
// INTERPOLATE () and INTERPOLATE ( ... ) variants
1074810740
return Ok(Some(Interpolate {

tests/sqlparser_common.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10284,6 +10284,8 @@ fn test_map_syntax() {
1028410284
}),
1028510285
},
1028610286
);
10287+
10288+
check("MAP {}", Expr::Map(Map { entries: vec![] }));
1028710289
}
1028810290

1028910291
#[test]

0 commit comments

Comments
 (0)