Skip to content

Support parsing empty map literal syntax for DuckDB and Genric #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 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, false, Token::RBracket)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we know if this change is going to be correct for all dialects? Otherwise we can probably keep the behavior to match the parser configuration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but all the tests are passed. The logic is the same as what I did for the map literal (actually, I followed it to implement the empty map). Should I roll back it?

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,9 @@ 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, false, Token::RBrace)?;
self.expect_token(&Token::RBrace)?;

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

Expand Down Expand Up @@ -2937,7 +2927,11 @@ 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,
self.options.trailing_commas,
Token::RParen,
)?
} else {
self.parse_comma_separated(Parser::parse_expr)?
},
Expand Down Expand Up @@ -3479,18 +3473,22 @@ 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>
/// * `trailing_commas` - support trailing_commas or not
/// * `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,
trailing_commas: bool,

This comment was marked as outdated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I'm not too sure if pulling out the trailing_comma option is worth it/desirable - the current behavior isn't specified on the dialect level, but rather on the parser itself, to be overridden by the user, and I wonder if it'll get confusing to have to figure out where each dialect supports or doesnt support trailing commas. Would it be okay to keep the behavior as it was before? (i.e. always guarding internally on self.options.trailing_comma)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I'm not too sure if pulling out the trailing_comma option is worth it/desirable - the current behavior isn't specified on the dialect level, but rather on the parser itself, to be overridden by the user, and I wonder if it'll get confusing to have to figure out where each dialect supports or doesnt support trailing commas. Would it be okay to keep the behavior as it was before? (i.e. always guarding internally on self.options.trailing_comma)

I see. I tried more cases for DuckDB, I found the trailing_comma behaviors are shared.

D select * from t where a in (1,2,);
┌────────┐
│   a    │
│ int32  │
├────────┤
│ 0 rows │
└────────┘
D select {'a':1,};
┌──────────────────────────┐
│ main.struct_pack(a := 1) │
│    struct(a integer)     │
├──────────────────────────┤
│ {'a': 1}                 │
└──────────────────────────┘
D select [1,2,];
┌───────────────────────┐
│ main.list_value(1, 2) │
│        int32[]        │
├───────────────────────┤
│ [1, 2]                │
└───────────────────────┘

I think we can use self.options.trailing_comma internally.

end_token: Token,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ that is a nice generalization

) -> 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 trailing_commas && self.peek_tokens() == [Token::Comma, end_token] {
let _ = self.consume_token(&Token::Comma);
return Ok(vec![]);
}
Expand Down Expand Up @@ -4059,7 +4057,11 @@ 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,
self.options.trailing_commas,
Token::RParen,
)?;
self.expect_token(&Token::RParen)?;

let return_type = if self.parse_keyword(Keyword::RETURNS) {
Expand Down Expand Up @@ -10713,7 +10715,11 @@ 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(),
self.options.trailing_commas,
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
Loading