Skip to content

Added support for parsing nested object in query for Snowflake#2359

Open
kfirSatori wants to merge 2 commits into
apache:mainfrom
SatoriCyber:snowflake-nested-object-support
Open

Added support for parsing nested object in query for Snowflake#2359
kfirSatori wants to merge 2 commits into
apache:mainfrom
SatoriCyber:snowflake-nested-object-support

Conversation

@kfirSatori

Copy link
Copy Markdown

Queries parsing like:
SELECT TRY_CAST(PARSE_JSON('{"obj_field":{"field":"value",}}') AS OBJECT(obj_field OBJECT( field VARCHAR)));

in Snowflake are failing since nested object are not supported.

I have added support for it.

@kfirSatori

Copy link
Copy Markdown
Author

@iffyio Can you please help resolve the PR?

Comment thread src/parser/mod.rs
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?

Comment thread src/parser/mod.rs
Comment on lines +13041 to +13060
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)?;

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?

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

Comment thread src/ast/data_type.rs
/// 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants