iffyio commented on code in PR #1507:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/1507#discussion_r1835440731
##########
src/ast/mod.rs:
##########
@@ -931,6 +931,54 @@ pub enum Expr {
///
/// See
<https://docs.databricks.com/en/sql/language-manual/sql-ref-lambda-functions.html>.
Lambda(LambdaFunction),
+ /// MSSQL's `JSON_ARRAY` function for construct JSON-ARRAY object
+ ///
+ /// Syntax:
+ ///
+ /// ```plaintext
+ /// JSON_ARRAY ( [ <json_array_value> [,...n] ] [ <json_null_clause> ] )
+ ///
+ /// <json_array_value> ::= value_expression
+ ///
+ /// <json_null_clause> ::=
+ /// NULL ON NULL
+ /// | ABSENT ON NULL
+ /// ```
+ ///
+ /// Example:
+ ///
+ /// ```sql
+ /// SELECT JSON_ARRAY('a', 1, 'b', 2) --["a",1,"b",2]
+ /// SELECT JSON_ARRAY('a', 1, NULL, 2 NULL ON NULL) --["a",1,null,2]
+ /// SELECT JSON_ARRAY('a', JSON_OBJECT('name':'value', 'type':1),
JSON_ARRAY(1, null, 2 NULL ON NULL))
--["a",{"name":"value","type":1},[1,null,2]]
+ /// ```
+ ///
+ /// Reference:
<https://learn.microsoft.com/en-us/sql/t-sql/functions/json-array-transact-sql?view=sql-server-ver16>
+ JsonArray(JsonArray),
+ /// MSSQL's `JSON_OBJECT` function for construct JSON-OBJECT object
+ ///
+ /// Syntax:
+ ///
+ /// ```plaintext
+ /// JSON_OBJECT ( [ <json_key_value> [,...n] ] [ json_null_clause ] )
+ ///
+ /// <json_key_value> ::= json_key_name : value_expression
+ ///
+ /// <json_null_clause> ::=
+ /// NULL ON NULL
+ /// | ABSENT ON NULL
+ /// ```
+ ///
+ /// Example:
+ ///
+ /// ```sql
+ /// SELECT JSON_OBJECT('name':'value', 'type':1)
--{"name":"value","type":1}
+ /// SELECT JSON_OBJECT('name':'value', 'type':NULL ABSENT ON NULL)
--{"name":"value"}
+ /// SELECT JSON_OBJECT('name':'value', 'type':JSON_ARRAY(1, 2))
--{"name":"value","type":[1,2]}
Review Comment:
```suggestion
/// SELECT JSON_OBJECT('name':'value', 'type':1)
/// SELECT JSON_OBJECT('name':'value', 'type':NULL ABSENT ON NULL)
/// SELECT JSON_OBJECT('name':'value', 'type':JSON_ARRAY(1, 2))
```
thinking for the examples we can skip the comments giving they don't add
value in this context (I got confused for a second thinking it was part of the
syntax :sweat_smile: )
##########
src/ast/mod.rs:
##########
@@ -931,6 +931,54 @@ pub enum Expr {
///
/// See
<https://docs.databricks.com/en/sql/language-manual/sql-ref-lambda-functions.html>.
Lambda(LambdaFunction),
+ /// MSSQL's `JSON_ARRAY` function for construct JSON-ARRAY object
+ ///
+ /// Syntax:
+ ///
+ /// ```plaintext
+ /// JSON_ARRAY ( [ <json_array_value> [,...n] ] [ <json_null_clause> ] )
+ ///
+ /// <json_array_value> ::= value_expression
+ ///
+ /// <json_null_clause> ::=
+ /// NULL ON NULL
+ /// | ABSENT ON NULL
+ /// ```
+ ///
+ /// Example:
+ ///
+ /// ```sql
+ /// SELECT JSON_ARRAY('a', 1, 'b', 2) --["a",1,"b",2]
+ /// SELECT JSON_ARRAY('a', 1, NULL, 2 NULL ON NULL) --["a",1,null,2]
+ /// SELECT JSON_ARRAY('a', JSON_OBJECT('name':'value', 'type':1),
JSON_ARRAY(1, null, 2 NULL ON NULL))
--["a",{"name":"value","type":1},[1,null,2]]
+ /// ```
+ ///
+ /// Reference:
<https://learn.microsoft.com/en-us/sql/t-sql/functions/json-array-transact-sql?view=sql-server-ver16>
+ JsonArray(JsonArray),
+ /// MSSQL's `JSON_OBJECT` function for construct JSON-OBJECT object
Review Comment:
```suggestion
/// MSSQL's `JSON_OBJECT` function to construct a `JSON-OBJECT` object
```
##########
src/ast/mod.rs:
##########
@@ -931,6 +931,54 @@ pub enum Expr {
///
/// See
<https://docs.databricks.com/en/sql/language-manual/sql-ref-lambda-functions.html>.
Lambda(LambdaFunction),
+ /// MSSQL's `JSON_ARRAY` function for construct JSON-ARRAY object
+ ///
+ /// Syntax:
+ ///
+ /// ```plaintext
+ /// JSON_ARRAY ( [ <json_array_value> [,...n] ] [ <json_null_clause> ] )
+ ///
+ /// <json_array_value> ::= value_expression
+ ///
+ /// <json_null_clause> ::=
+ /// NULL ON NULL
+ /// | ABSENT ON NULL
+ /// ```
+ ///
+ /// Example:
+ ///
+ /// ```sql
+ /// SELECT JSON_ARRAY('a', 1, 'b', 2) --["a",1,"b",2]
+ /// SELECT JSON_ARRAY('a', 1, NULL, 2 NULL ON NULL) --["a",1,null,2]
+ /// SELECT JSON_ARRAY('a', JSON_OBJECT('name':'value', 'type':1),
JSON_ARRAY(1, null, 2 NULL ON NULL))
--["a",{"name":"value","type":1},[1,null,2]]
+ /// ```
+ ///
+ /// Reference:
<https://learn.microsoft.com/en-us/sql/t-sql/functions/json-array-transact-sql?view=sql-server-ver16>
+ JsonArray(JsonArray),
Review Comment:
Ah jsonarray looks like a special function call, I think we try to represent
those as function calls to avoid too many special case expressions. Could you
take a look at the
[parse_function_argument_list](https://github.com/apache/datafusion-sqlparser-rs/blob/a4c73f3c6b221d4db51c8126330e177c5d9bd820/src/parser/mod.rs#L11212)
to see if we can represent this as a
[FunctionArgumentList](https://github.com/apache/datafusion-sqlparser-rs/blob/main/src/ast/mod.rs#L5526-L5537)?
(I'm guessing that would be a matter of adding a `FunctionArgumentClause`
variant to represent the `NULL ON NULL`)
##########
src/ast/mod.rs:
##########
@@ -7317,6 +7367,87 @@ impl Display for UtilityOption {
}
}
+/// MSSQL's `JSON_ARRAY` constructor function
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct JsonArray {
+ pub args: Vec<Expr>,
+ pub null_clause: Option<JsonNullClause>,
+}
+
+impl Display for JsonArray {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "JSON_ARRAY({}", display_comma_separated(&self.args))?;
+ if let Some(null_clause) = &self.null_clause {
+ if !self.args.is_empty() {
+ write!(f, " ")?;
+ }
+ write!(f, "{null_clause}")?;
+ }
+ write!(f, ")")
+ }
+}
+
+/// MSSQL's `JSON_OBJECT` constructor function
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Review Comment:
Ah I think piggy-backing on my comment on the JsonArray, we would probably
support this syntax automatically by extending the function_arguments_list
representation? Did you see the existing [json
functionality](https://github.com/apache/datafusion-sqlparser-rs/blob/a4c73f3c6b221d4db51c8126330e177c5d9bd820/src/parser/mod.rs#L2951),
I would expect the parser currently supports `foo:bar` as an expression today
iirc that's snowflake json syntax, which is identical to this and feels like
should be supported already?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]