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
11 changes: 11 additions & 0 deletions src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ 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 {
/// `None` for bare `OBJECT`, `Some` when parentheses are present (possibly empty).
fields: Option<Vec<StructField>>,
},
/// 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 @@ -778,6 +785,10 @@ impl fmt::Display for DataType {
DataType::Union(fields) => {
write!(f, "UNION({})", display_comma_separated(fields))
}
DataType::Object { fields } => match fields {
None => write!(f, "OBJECT"),
Some(fields) => write!(f, "OBJECT({})", display_comma_separated(fields)),
},
// ClickHouse
DataType::Nullable(data_type) => {
write!(f, "Nullable({data_type})")
Expand Down
30 changes: 30 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3657,6 +3657,32 @@ impl<'a> Parser<'a> {
/// ```
///
/// [1]: https://duckdb.org/docs/sql/data_types/union.html
fn parse_object_data_type(&mut self) -> Result<DataType, ParserError> {
self.expect_keyword_is(Keyword::OBJECT)?;
// Object type may have no fields: OBJECT or OBJECT()
if !self.peek_token_ref().token.eq(&Token::LParen) {
return Ok(DataType::Object { fields: None });
}
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)?;
Ok(DataType::Object {
fields: Some(fields),
})
}

fn parse_union_type_def(&mut self) -> Result<Vec<UnionField>, ParserError> {
self.expect_keyword_is(Keyword::UNION)?;

Expand Down Expand Up @@ -13115,6 +13141,10 @@ 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @iffyio, i dont understand what do you mean.
i did it the same way done as UNION and NULLABLE

self.prev_token();
self.parse_object_data_type()
}
Keyword::NULLABLE if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
Ok(self.parse_sub_type(DataType::Nullable)?)
}
Expand Down
18 changes: 18 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4912,3 +4912,21 @@ 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() {
// nested OBJECT with a single field
snowflake().verified_stmt("SELECT TRY_CAST(PARSE_JSON('{\"obj_field\":{\"field\":\"value\",}}') AS OBJECT(obj_field OBJECT(field VARCHAR)))");

// OBJECT with multiple fields
snowflake().verified_stmt("SELECT CAST(v AS OBJECT(a VARCHAR, b INT, c BOOLEAN))");

// nested OBJECT with multiple fields at both levels
snowflake().verified_stmt("SELECT CAST(v AS OBJECT(x OBJECT(a INT, b VARCHAR), y NUMBER))");

// OBJECT with zero fields (empty parentheses)
snowflake().verified_stmt("SELECT CAST(v AS OBJECT())");

// bare OBJECT without parentheses round-trips as OBJECT
snowflake().verified_stmt("SELECT CAST(v AS OBJECT)");
}
Loading