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 1 commit
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: 26 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,21 @@ pub enum Expr {
/// ```sql
/// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
/// ```
/// `DuckDB` specific `Struct` literal expression [2]
/// Syntax:
/// ```sql
/// syntax: {'field_name': expr1[, ... ]}
/// ```
/// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
/// [2]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
Struct {
/// Struct values.
values: Vec<Expr>,
/// Struct field definitions.
fields: Vec<StructField>,
/// true if uses duckdb array notation syntax(no change in semantics)
/// this field is used for formatting
array_notation: bool,
},
/// `BigQuery` specific: An named expression in a typeless struct [1]
///
Expand Down Expand Up @@ -1131,8 +1140,23 @@ impl fmt::Display for Expr {
Expr::Tuple(exprs) => {
write!(f, "({})", display_comma_separated(exprs))
}
Expr::Struct { values, fields } => {
if !fields.is_empty() {
Expr::Struct {
values,
fields,
array_notation,
} => {
if *array_notation {
let args = values
.iter()
.map(|value| match value {
Expr::Named { expr, name } => {
format!("'{}': {}", name.value, expr)
}
_ => unreachable!(),
})
.collect::<Vec<_>>();
write!(f, "{{{}}}", display_comma_separated(&args))
} else if !fields.is_empty() {
write!(
f,
"STRUCT<{}>({})",
Expand Down
56 changes: 55 additions & 1 deletion 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 @@ -2008,7 +2012,11 @@ impl<'a> Parser<'a> {
.parse_comma_separated(|parser| parser.parse_struct_field_expr(!fields.is_empty()))?;
self.expect_token(&Token::RParen)?;

Ok(Expr::Struct { values, fields })
Ok(Expr::Struct {
values,
fields,
array_notation: false,
})
}

/// Parse an expression value for a bigquery struct [1]
Expand Down Expand Up @@ -2127,6 +2135,52 @@ impl<'a> Parser<'a> {
))
}

/// DuckDB specific: Parse a struct literal [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 values = self.parse_comma_separated(Self::parse_duckdb_struct_field)?;

self.expect_token(&Token::RBrace)?;

Ok(Expr::Struct {
values,
fields: vec![],
array_notation: true,
})
}

/// Parse an expression value for a duckdb struct [1]
/// Syntax
/// ```sql
/// 'name': expr
/// ```
///
/// [1]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
fn parse_duckdb_struct_field(&mut self) -> Result<Expr, ParserError> {
let next_token = self.next_token();

let name = match next_token.token {
Token::SingleQuotedString(name) => name,
_ => return self.expected("single quoted string", next_token),
};

self.expect_token(&Token::Colon)?;

let expr = self.parse_expr()?;

Ok(Expr::Named {
expr: Box::new(expr),
name: Ident::with_quote('\'', name),
})
}

/// 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
75 changes: 50 additions & 25 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,17 @@ fn parse_typeless_struct_syntax() {
Expr::Value(number("2")),
Expr::Value(number("3")),
],
fields: Default::default()
fields: Default::default(),
array_notation: false,
},
expr_from_projection(&select.projection[0])
);

assert_eq!(
&Expr::Struct {
values: vec![Expr::Value(Value::SingleQuotedString("abc".to_string())),],
fields: Default::default()
fields: Default::default(),
array_notation: false,
},
expr_from_projection(&select.projection[1])
);
Expand All @@ -427,7 +429,8 @@ fn parse_typeless_struct_syntax() {
Expr::Value(number("1")),
Expr::CompoundIdentifier(vec![Ident::from("t"), Ident::from("str_col")]),
],
fields: Default::default()
fields: Default::default(),
array_notation: false,
},
expr_from_projection(&select.projection[2])
);
Expand All @@ -443,7 +446,8 @@ fn parse_typeless_struct_syntax() {
name: Ident::from("b")
},
],
fields: Default::default()
fields: Default::default(),
array_notation: false
},
expr_from_projection(&select.projection[3])
);
Expand All @@ -453,7 +457,8 @@ fn parse_typeless_struct_syntax() {
expr: Expr::Identifier(Ident::from("str_col")).into(),
name: Ident::from("abc")
}],
fields: Default::default()
fields: Default::default(),
array_notation: false
},
expr_from_projection(&select.projection[4])
);
Expand All @@ -473,7 +478,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Int64,
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand Down Expand Up @@ -507,7 +513,8 @@ fn parse_typed_struct_syntax() {
}),
field_type: DataType::String(None)
},
]
],
array_notation: false
},
expr_from_projection(&select.projection[1])
);
Expand All @@ -531,7 +538,8 @@ fn parse_typed_struct_syntax() {
field_type: DataType::Bool
}])
},
]
],
array_notation: false
},
expr_from_projection(&select.projection[2])
);
Expand All @@ -556,7 +564,8 @@ fn parse_typed_struct_syntax() {
DataType::Struct(Default::default())
)))
},
]
],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand All @@ -570,7 +579,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Bool
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand All @@ -582,7 +592,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Bytes(Some(42))
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[1])
);
Expand All @@ -598,7 +609,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Date
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand All @@ -611,7 +623,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Datetime(None)
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[1])
);
Expand All @@ -621,7 +634,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Float64
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[2])
);
Expand All @@ -631,7 +645,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Int64
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[3])
);
Expand All @@ -653,7 +668,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Interval
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand All @@ -666,7 +682,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::JSON
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[1])
);
Expand All @@ -680,7 +697,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::String(Some(42))
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand All @@ -693,7 +711,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Timestamp(None, TimezoneInfo::None)
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[1])
);
Expand All @@ -707,7 +726,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Time(None, TimezoneInfo::None)
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[2])
);
Expand All @@ -724,7 +744,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::Numeric(ExactNumberInfo::None)
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand All @@ -737,7 +758,8 @@ fn parse_typed_struct_syntax() {
fields: vec![StructField {
field_name: None,
field_type: DataType::BigNumeric(ExactNumberInfo::None)
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[1])
);
Expand All @@ -754,7 +776,8 @@ fn parse_typed_struct_with_field_name() {
fields: vec![StructField {
field_name: Some(Ident::from("x")),
field_type: DataType::Int64
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand All @@ -764,7 +787,8 @@ fn parse_typed_struct_with_field_name() {
fields: vec![StructField {
field_name: Some(Ident::from("y")),
field_type: DataType::String(None)
}]
}],
array_notation: false
},
expr_from_projection(&select.projection[1])
);
Expand All @@ -784,7 +808,8 @@ fn parse_typed_struct_with_field_name() {
field_name: Some(Ident::from("y")),
field_type: DataType::Int64
}
]
],
array_notation: false
},
expr_from_projection(&select.projection[0])
);
Expand Down
Loading