Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

adamchainz
Copy link
Contributor

Fixes #2000.

Comment on lines +3391 to +3450
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:?}"
),
}
}
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn parse_json_returning_clause(&mut self) -> Result<Option<JsonReturningClause>, ParserError> {
fn maybe_parse_json_returning_clause(&mut self) -> Result<Option<JsonReturningClause>, ParserError> {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Postgres' JSON_OBJECT function RETURNING clause
2 participants