iffyio commented on code in PR #2001: URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2001#discussion_r2285648859
########## tests/sqlparser_postgres.rs: ########## @@ -3363,6 +3387,68 @@ fn test_fn_arg_with_value_operator() { } } +#[test] +fn json_object_with_null_clause() { + match pg().verified_expr("JSON_OBJECT('name' VALUE 'value' NULL ON NULL)") { + Expr::Function(Function { args: FunctionArguments::List(FunctionArgumentList { args, clauses, .. }), .. }) => { + assert!(matches!( + &args[..], + &[FunctionArg::ExprNamed { operator: FunctionArgOperator::Value, .. }] + ), "Invalid function argument: {args:?}"); + assert_eq!( + clauses, + vec![FunctionArgumentClause::JsonNullClause(JsonNullClause::NullOnNull)], + "Expected: NULL ON NULL to be parsed as a null treatment clause, but got {clauses:?}" + ); + } + other => panic!("Expected: JSON_OBJECT('name' VALUE 'value' NULL ON NULL) to be parsed as a function, but got {other:?}"), + } +} + +#[test] +fn json_object_with_returning_clause() { + match pg().verified_expr("JSON_OBJECT('name' VALUE 'value' RETURNING JSONB)") { + Expr::Function(Function { args: FunctionArguments::List(FunctionArgumentList { args, clauses, .. }), .. }) => { + assert!(matches!( + &args[..], + &[FunctionArg::ExprNamed { operator: FunctionArgOperator::Value, .. }] + ), "Invalid function argument: {args:?}"); + assert_eq!( + clauses, + vec![FunctionArgumentClause::JsonReturningClause(JsonReturningClause { + data_type: DataType::JSONB + })], + "Expected: RETURNING JSONB to be parsed as a returning clause, but got {clauses:?}" + ); + } + other => panic!("Expected: JSON_OBJECT('name' VALUE 'value' RETURNING jsonb) to be parsed as a function, but got {other:?}"), + } +} + +#[test] +fn json_object_only_returning_clause() { + match pg().verified_expr("JSON_OBJECT(RETURNING JSONB)") { + Expr::Function(Function { + args: FunctionArguments::List(FunctionArgumentList { args, clauses, .. }), + .. + }) => { + assert!(args.is_empty(), "Expected no arguments, got: {args:?}"); + assert_eq!( + clauses, + vec![FunctionArgumentClause::JsonReturningClause( + JsonReturningClause { + data_type: DataType::JSONB + } + )], + "Expected: RETURNING JSONB to be parsed as a returning clause, but got {clauses:?}" + ); + } + other => panic!( + "Expected: JSON_OBJECT(RETURNING jsonb) to be parsed as a function, but got {other:?}" + ), + } +} Review Comment: since they're covering the same feature, can we merge these test cases into one `parse_json_object()` test function? ########## src/parser/mod.rs: ########## @@ -15397,6 +15409,15 @@ impl<'a> Parser<'a> { } } + fn parse_json_returning_clause(&mut self) -> Result<Option<JsonReturningClause>, ParserError> { Review Comment: ```suggestion fn maybe_parse_json_returning_clause(&mut self) -> Result<Option<JsonReturningClause>, ParserError> { ``` ########## src/ast/mod.rs: ########## @@ -7802,11 +7802,16 @@ pub enum FunctionArgumentClause { /// /// [`GROUP_CONCAT`]: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat Separator(Value), - /// The json-null-clause to the [`JSON_ARRAY`]/[`JSON_OBJECT`] function in MSSQL. + /// The `ON NULL` clause for some JSON functions in MSSQL and PostgreSQL Review Comment: ```suggestion /// The `ON NULL` clause for some JSON functions. ``` -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org