Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ pub enum DataType {
///
/// [DuckDB]: https://duckdb.org/docs/sql/data_types/union.html
Union(Vec<UnionField>),
/// Object type, see [Snowflake].
///
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/data-types-semistructured#object
Object(Vec<StructField>),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we turn this into a Object { xxx: Option<Vec<StructField>> }? the named field syntax makes it possible to extend later on (like attaching spans) and the option iiuc is needed if we're to properly support OBJECT syntax (unclear since the behavior suggests it but the tests don't cover it)

/// Nullable - special marker NULL represents in ClickHouse as a data type.
///
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nullable
Expand Down Expand Up @@ -776,6 +780,9 @@ impl fmt::Display for DataType {
DataType::Union(fields) => {
write!(f, "UNION({})", display_comma_separated(fields))
}
DataType::Object(fields) => {
write!(f, "OBJECT({})", display_comma_separated(fields))
}
// ClickHouse
DataType::Nullable(data_type) => {
write!(f, "Nullable({data_type})")
Expand Down
25 changes: 25 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13036,6 +13036,31 @@ impl<'a> Parser<'a> {
let fields = self.parse_union_type_def()?;
Ok(DataType::Union(fields))
}
Keyword::OBJECT if dialect_is!(dialect is SnowflakeDialect | GenericDialect) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change this to use a dialect method?

self.prev_token();
self.expect_keyword_is(Keyword::OBJECT)?;
// Object type may have no fields: OBJECT or OBJECT()
if !self.peek_token_ref().token.eq(&Token::LParen) {
Ok(DataType::Object(vec![]))
} else {
self.expect_token(&Token::LParen)?;
let fields = if self.peek_token_ref().token == Token::RParen {
vec![]
} else {
self.parse_comma_separated(|parser| {
let field_name = parser.parse_identifier()?;
let field_type = parser.parse_data_type()?;
Ok(StructField {
field_name: Some(field_name),
field_type,
options: None,
})
})?
};
self.expect_token(&Token::RParen)?;
Comment on lines +13041 to +13060

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move this to its own parse_object_data_type() or similar helper method?

Ok(DataType::Object(fields))
}
}
Keyword::NULLABLE if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
Ok(self.parse_sub_type(DataType::Nullable)?)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4914,3 +4914,12 @@ fn test_select_dollar_column_from_stage() {
// With table function args, without alias
snowflake().verified_stmt("SELECT $1, $2 FROM @mystage1(file_format => 'myformat')");
}

#[test]
fn parse_nested_object() {
let sql = r#"SELECT TRY_CAST(PARSE_JSON('{"obj_field":{"field":"value",}}') AS OBJECT(obj_field OBJECT(
field VARCHAR
)));"#;

snowflake().parse_sql_statements(sql).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use verified_stmt? also if we can include test cases for mulltiple struct fields, zero struct fields, OBJECT() and OBJECT

}