-
Notifications
You must be signed in to change notification settings - Fork 615
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
Changes from 4 commits
21cdef3
0763f52
5d81654
4479ecd
239ef53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)?; | ||
self.expect_token(&Token::RBracket)?; | ||
Ok(Expr::Array(Array { elem: exprs, named })) | ||
} | ||
|
||
pub fn parse_listagg_on_overflow(&mut self) -> Result<Option<ListAggOnOverflow>, ParserError> { | ||
|
@@ -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 })) | ||
} | ||
|
||
|
@@ -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)? | ||
}, | ||
|
@@ -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.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 |
||
end_token: Token, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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![]); | ||
} | ||
|
@@ -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) { | ||
|
@@ -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 { | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?