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

@ -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)]
@ -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>,
@ -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 {