Add support for DuckDB struct literal syntax (#1194)

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
gstvg 2024-03-29 10:39:52 -03:00 committed by GitHub
parent 4472789171
commit e747c9c2af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 158 additions and 0 deletions

View file

@ -1117,6 +1117,10 @@ impl<'a> Parser<'a> {
self.prev_token();
Ok(Expr::Value(self.parse_value()?))
}
Token::LBrace if dialect_of!(self is DuckDbDialect | GenericDialect) => {
self.prev_token();
self.parse_duckdb_struct_literal()
}
_ => self.expected("an expression:", next_token),
}?;
@ -2127,6 +2131,45 @@ impl<'a> Parser<'a> {
))
}
/// DuckDB specific: Parse a duckdb dictionary [1]
///
/// Syntax:
///
/// ```sql
/// {'field_name': expr1[, ... ]}
/// ```
///
/// [1]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
fn parse_duckdb_struct_literal(&mut self) -> Result<Expr, ParserError> {
self.expect_token(&Token::LBrace)?;
let fields = self.parse_comma_separated(Self::parse_duckdb_dictionary_field)?;
self.expect_token(&Token::RBrace)?;
Ok(Expr::Dictionary(fields))
}
/// Parse a field for a duckdb dictionary [1]
/// Syntax
/// ```sql
/// 'name': expr
/// ```
///
/// [1]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
fn parse_duckdb_dictionary_field(&mut self) -> Result<DictionaryField, ParserError> {
let key = self.parse_identifier(false)?;
self.expect_token(&Token::Colon)?;
let expr = self.parse_expr()?;
Ok(DictionaryField {
key,
value: Box::new(expr),
})
}
/// For nested types that use the angle bracket syntax, this matches either
/// `>`, `>>` or nothing depending on which variant is expected (specified by the previously
/// matched `trailing_bracket` argument). It returns whether there is a trailing