-
Notifications
You must be signed in to change notification settings - Fork 605
Support parsing empty map literal syntax for DuckDB and Genric #1361
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
Pull Request Test Coverage Report for Build 10235175469Details
💛 - Coveralls |
src/parser/mod.rs
Outdated
|
||
Ok(Expr::Map(Map { entries: fields })) | ||
if self.peek_token().token == Token::RBrace { | ||
let _ = self.next_token(); // consume } |
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.
Nit: ambiguous comments
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 followed the same style as parsing the array literal. Should I enhance them?
https://github.com/sqlparser-rs/sqlparser-rs/blob/d49acc67b13e1d68f2e6a25546161a68e165da4f/src/parser/mod.rs#L1853
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.
My bad, i interpreted wrongly. This look good.
src/parser/mod.rs
Outdated
self.expect_token(&Token::RBrace)?; | ||
|
||
Ok(Expr::Map(Map { entries: fields })) | ||
if self.peek_token().token == Token::RBrace { |
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.
is it possible to switch to let fields = self.parse_comma_separated0(Self::parse_duckdb_map_field)?;
instead? if so that would let us skip the if/else here
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 don't think we can do it. It seems that parse_comma_separated0
is for parentheses, but MAP uses braces.
https://github.com/sqlparser-rs/sqlparser-rs/blob/d49acc67b13e1d68f2e6a25546161a68e165da4f/src/parser/mod.rs#L3487
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.
Ah i see, I'm thinking we could still reuse the logic (thinking its currently a bit odd that parse_comma_separated0
is hardcoded to parenthesis while parse_comma_separated
is token agnostic as it should be). Seems there's ony a couple of usages of parse_comma_separated0
, we could change it to something like parse_comma_separated0(|| {}, Token::RParen)
?
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.
src/parser/mod.rs
Outdated
pub fn parse_comma_separated0<T, F>( | ||
&mut self, | ||
f: F, | ||
trailing_commas: bool, |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
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.
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
)
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.
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.
src/parser/mod.rs
Outdated
self.expect_token(&Token::RBracket)?; | ||
Ok(Expr::Array(Array { elem: exprs, named })) | ||
} | ||
let exprs = self.parse_comma_separated0(Parser::parse_expr, false, Token::RBracket)?; |
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?
src/parser/mod.rs
Outdated
pub fn parse_comma_separated0<T, F>( | ||
&mut self, | ||
f: F, | ||
trailing_commas: bool, |
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.
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
)
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.
LGTM! cc @alamb
pub fn parse_comma_separated0<T, F>( | ||
&mut self, | ||
f: F, | ||
end_token: Token, |
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.
❤️ that is a nice generalization
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.
Thanks @goldmedal and @iffyio
Description
DuckDB supports creating an empty map like