-
Notifications
You must be signed in to change notification settings - Fork 632
Add support for PostgreSQL JSON function 'RETURNING' clauses #2001
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?
Conversation
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:?}" | ||
), | ||
} | ||
} |
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.
since they're covering the same feature, can we merge these test cases into one parse_json_object()
test function?
@@ -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 |
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.
/// The `ON NULL` clause for some JSON functions in MSSQL and PostgreSQL | |
/// The `ON NULL` clause for some JSON functions. |
@@ -15397,6 +15409,15 @@ impl<'a> Parser<'a> { | |||
} | |||
} | |||
|
|||
fn parse_json_returning_clause(&mut self) -> Result<Option<JsonReturningClause>, ParserError> { |
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.
fn parse_json_returning_clause(&mut self) -> Result<Option<JsonReturningClause>, ParserError> { | |
fn maybe_parse_json_returning_clause(&mut self) -> Result<Option<JsonReturningClause>, ParserError> { |
Fixes #2000.