Skip to content

Commit

Permalink
Support parsing empty map literal syntax for DuckDB and Genric (#1361)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldmedal authored Aug 4, 2024
1 parent d49acc6 commit 8f8c96f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
42 changes: 17 additions & 25 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1849,17 +1849,9 @@ impl<'a> Parser<'a> {
/// Parses an array expression `[ex1, ex2, ..]`
/// if `named` is `true`, came from an expression like `ARRAY[ex1, ex2]`
pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
if self.peek_token().token == Token::RBracket {
let _ = self.next_token(); // consume ]
Ok(Expr::Array(Array {
elem: vec![],
named,
}))
} else {
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
self.expect_token(&Token::RBracket)?;
Ok(Expr::Array(Array { elem: exprs, named }))
}
let exprs = self.parse_comma_separated0(Parser::parse_expr, Token::RBracket)?;
self.expect_token(&Token::RBracket)?;
Ok(Expr::Array(Array { elem: exprs, named }))
}

pub fn parse_listagg_on_overflow(&mut self) -> Result<Option<ListAggOnOverflow>, ParserError> {
Expand Down Expand Up @@ -2352,11 +2344,8 @@ impl<'a> Parser<'a> {
/// [map]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
fn parse_duckdb_map_literal(&mut self) -> Result<Expr, ParserError> {
self.expect_token(&Token::LBrace)?;

let fields = self.parse_comma_separated(Self::parse_duckdb_map_field)?;

let fields = self.parse_comma_separated0(Self::parse_duckdb_map_field, Token::RBrace)?;
self.expect_token(&Token::RBrace)?;

Ok(Expr::Map(Map { entries: fields }))
}

Expand Down Expand Up @@ -2937,7 +2926,7 @@ impl<'a> Parser<'a> {
Expr::InList {
expr: Box::new(expr),
list: if self.dialect.supports_in_empty_list() {
self.parse_comma_separated0(Parser::parse_expr)?
self.parse_comma_separated0(Parser::parse_expr, Token::RParen)?
} else {
self.parse_comma_separated(Parser::parse_expr)?
},
Expand Down Expand Up @@ -3479,18 +3468,20 @@ impl<'a> Parser<'a> {
}

/// Parse a comma-separated list of 0+ items accepted by `F`
pub fn parse_comma_separated0<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError>
/// * `end_token` - expected end token for the closure (e.g. [Token::RParen], [Token::RBrace] ...)
pub fn parse_comma_separated0<T, F>(
&mut self,
f: F,
end_token: Token,
) -> Result<Vec<T>, ParserError>
where
F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
{
// ()
if matches!(self.peek_token().token, Token::RParen) {
if self.peek_token().token == end_token {
return Ok(vec![]);
}
// (,)
if self.options.trailing_commas
&& matches!(self.peek_tokens(), [Token::Comma, Token::RParen])
{

if self.options.trailing_commas && self.peek_tokens() == [Token::Comma, end_token] {
let _ = self.consume_token(&Token::Comma);
return Ok(vec![]);
}
Expand Down Expand Up @@ -4059,7 +4050,7 @@ impl<'a> Parser<'a> {
})
};
self.expect_token(&Token::LParen)?;
let args = self.parse_comma_separated0(parse_function_param)?;
let args = self.parse_comma_separated0(parse_function_param, Token::RParen)?;
self.expect_token(&Token::RParen)?;

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

if self.consume_token(&Token::LParen) {
let interpolations = self.parse_comma_separated0(|p| p.parse_interpolation())?;
let interpolations =
self.parse_comma_separated0(|p| p.parse_interpolation(), Token::RParen)?;
self.expect_token(&Token::RParen)?;
// INTERPOLATE () and INTERPOLATE ( ... ) variants
return Ok(Some(Interpolate {
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10282,6 +10282,8 @@ fn test_map_syntax() {
}),
},
);

check("MAP {}", Expr::Map(Map { entries: vec![] }));
}

#[test]
Expand Down

0 comments on commit 8f8c96f

Please sign in to comment.