-
Notifications
You must be signed in to change notification settings - Fork 744
Added support for parsing nested object in query for Snowflake #2359
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)?) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
| } | ||
There was a problem hiding this comment.
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 supportOBJECTsyntax (unclear since the behavior suggests it but the tests don't cover it)