-
Notifications
You must be signed in to change notification settings - Fork 664
Add support for MSSQL's OPENJSON WITH
clause
#1498
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
Changes from 1 commit
36fea1c
b07bfc5
e5702ce
3425957
609cb09
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 |
---|---|---|
|
@@ -535,6 +535,7 @@ define_keywords!( | |
ONE, | ||
ONLY, | ||
OPEN, | ||
OPENJSON, | ||
OPERATOR, | ||
OPTIMIZE, | ||
OPTIMIZER_COSTS, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10000,7 +10000,7 @@ impl<'a> Parser<'a> { | |
table_with_joins: Box::new(table_and_joins), | ||
alias, | ||
}) | ||
} else if dialect_of!(self is SnowflakeDialect | GenericDialect) { | ||
} else if dialect_of!(self is SnowflakeDialect | GenericDialect | MsSqlDialect) { | ||
|
||
// Dialect-specific behavior: Snowflake diverges from the | ||
// standard and from most of the other implementations by | ||
// allowing extra parentheses not only around a join (B), but | ||
|
@@ -10020,6 +10020,7 @@ impl<'a> Parser<'a> { | |
| TableFactor::Function { alias, .. } | ||
| TableFactor::UNNEST { alias, .. } | ||
| TableFactor::JsonTable { alias, .. } | ||
| TableFactor::OpenJsonTable { alias, .. } | ||
| TableFactor::TableFunction { alias, .. } | ||
| TableFactor::Pivot { alias, .. } | ||
| TableFactor::Unpivot { alias, .. } | ||
|
@@ -10133,6 +10134,30 @@ impl<'a> Parser<'a> { | |
columns, | ||
alias, | ||
}) | ||
} else if self.parse_keyword_with_tokens(Keyword::OPENJSON, &[Token::LParen]) { | ||
let json_expr = self.parse_expr()?; | ||
let json_path = if self.consume_token(&Token::Comma) { | ||
Some(self.parse_value()?) | ||
} else { | ||
None | ||
}; | ||
self.expect_token(&Token::RParen)?; | ||
let columns = if self.parse_keyword(Keyword::WITH) { | ||
self.expect_token(&Token::LParen)?; | ||
let columns = | ||
self.parse_comma_separated(Parser::parse_openjson_table_column_def)?; | ||
self.expect_token(&Token::RParen)?; | ||
columns | ||
} else { | ||
Vec::new() | ||
}; | ||
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?; | ||
Ok(TableFactor::OpenJsonTable { | ||
json_expr, | ||
json_path, | ||
columns, | ||
alias, | ||
}) | ||
gaoqiangz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} else { | ||
let name = self.parse_object_name(true)?; | ||
|
||
|
@@ -10468,6 +10493,34 @@ impl<'a> Parser<'a> { | |
}) | ||
} | ||
|
||
/// Parses MSSQL's `OPENJSON WITH` column definition. | ||
/// | ||
/// ```sql | ||
/// colName type [ column_path ] [ AS JSON ] | ||
/// ``` | ||
/// | ||
/// Reference: <https://learn.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16#syntax> | ||
pub fn parse_openjson_table_column_def(&mut self) -> Result<OpenJsonTableColumn, ParserError> { | ||
let name = self.parse_identifier(false)?; | ||
let r#type = self.parse_data_type()?; | ||
let path = if let Token::SingleQuotedString(path) = self.peek_token().token { | ||
self.next_token(); | ||
Some(Value::SingleQuotedString(path)) | ||
} else { | ||
None | ||
}; | ||
let as_json = self.parse_keyword(Keyword::AS); | ||
if as_json { | ||
self.expect_keyword(Keyword::JSON)?; | ||
} | ||
Ok(OpenJsonTableColumn { | ||
name, | ||
r#type, | ||
path, | ||
as_json, | ||
}) | ||
} | ||
|
||
fn parse_json_table_column_error_handling( | ||
&mut self, | ||
) -> Result<Option<JsonTableColumnErrorHandling>, ParserError> { | ||
|
Uh oh!
There was an error while loading. Please reload this page.