Skip to content

Add support for DuckDB struct literal syntax #1194

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 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,23 @@ impl fmt::Display for StructField {
}
}

/// A dictionary field within a dictionary.
///
/// [duckdb]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct DictionaryField {
pub key: Ident,
pub value: Box<Expr>,
}

impl fmt::Display for DictionaryField {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.key, self.value)
}
}

/// Options for `CAST` / `TRY_CAST`
/// BigQuery: <https://cloud.google.com/bigquery/docs/reference/standard-sql/format-elements#formatting_syntax>
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -687,6 +704,14 @@ pub enum Expr {
expr: Box<Expr>,
name: Ident,
},
/// `DuckDB` specific `Struct` literal expression [1]
///
/// Syntax:
/// ```sql
/// syntax: {'field_name': expr1[, ... ]}
/// ```
/// [1]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
Dictionary(Vec<DictionaryField>),
/// An array index expression e.g. `(ARRAY[1, 2])[1]` or `(current_schemas(FALSE))[1]`
ArrayIndex {
obj: Box<Expr>,
Expand Down Expand Up @@ -1146,6 +1171,9 @@ impl fmt::Display for Expr {
Expr::Named { expr, name } => {
write!(f, "{} AS {}", expr, name)
}
Expr::Dictionary(fields) => {
write!(f, "{{{}}}", display_comma_separated(fields))
}
Expr::ArrayIndex { obj, indexes } => {
write!(f, "{obj}")?;
for i in indexes {
Expand Down
43 changes: 43 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}?;

Expand Down Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,90 @@ fn test_duckdb_load_extension() {
stmt
);
}

#[test]
fn test_duckdb_struct_literal() {
//struct literal syntax https://duckdb.org/docs/sql/data_types/struct#creating-structs
//syntax: {'field_name': expr1[, ... ]}
let sql = "SELECT {'a': 1, 'b': 2, 'c': 3}, [{'a': 'abc'}], {'a': 1, 'b': [t.str_col]}, {'a': 1, 'b': 'abc'}, {'abc': str_col}, {'a': {'aa': 1}}";
let select = duckdb_and_generic().verified_only_select(sql);
assert_eq!(6, select.projection.len());
assert_eq!(
&Expr::Dictionary(vec![
DictionaryField {
key: Ident::with_quote('\'', "a"),
value: Box::new(Expr::Value(number("1"))),
},
DictionaryField {
key: Ident::with_quote('\'', "b"),
value: Box::new(Expr::Value(number("2"))),
},
DictionaryField {
key: Ident::with_quote('\'', "c"),
value: Box::new(Expr::Value(number("3"))),
},
],),
expr_from_projection(&select.projection[0])
);

assert_eq!(
&Expr::Array(Array {
elem: vec![Expr::Dictionary(vec![DictionaryField {
key: Ident::with_quote('\'', "a"),
value: Box::new(Expr::Value(Value::SingleQuotedString("abc".to_string()))),
},],)],
named: false
}),
expr_from_projection(&select.projection[1])
);
assert_eq!(
&Expr::Dictionary(vec![
DictionaryField {
key: Ident::with_quote('\'', "a"),
value: Box::new(Expr::Value(number("1"))),
},
DictionaryField {
key: Ident::with_quote('\'', "b"),
value: Box::new(Expr::Array(Array {
elem: vec![Expr::CompoundIdentifier(vec![
Ident::from("t"),
Ident::from("str_col")
])],
named: false
})),
},
],),
expr_from_projection(&select.projection[2])
);
assert_eq!(
&Expr::Dictionary(vec![
DictionaryField {
key: Ident::with_quote('\'', "a"),
value: Expr::Value(number("1")).into(),
},
DictionaryField {
key: Ident::with_quote('\'', "b"),
value: Expr::Value(Value::SingleQuotedString("abc".to_string())).into(),
},
],),
expr_from_projection(&select.projection[3])
);
assert_eq!(
&Expr::Dictionary(vec![DictionaryField {
key: Ident::with_quote('\'', "abc"),
value: Expr::Identifier(Ident::from("str_col")).into(),
}],),
expr_from_projection(&select.projection[4])
);
assert_eq!(
&Expr::Dictionary(vec![DictionaryField {
key: Ident::with_quote('\'', "a"),
value: Expr::Dictionary(vec![DictionaryField {
key: Ident::with_quote('\'', "aa"),
value: Expr::Value(number("1")).into(),
}],)
.into(),
}],),
expr_from_projection(&select.projection[5])
);
}