Add support for PostgreSQL JSON function 'RETURNING' clauses

This commit is contained in:
Adam Johnson 2025-08-15 11:20:14 +01:00
parent 60a5c8d42a
commit 61657837af
4 changed files with 142 additions and 7 deletions

View file

@ -3351,7 +3351,31 @@ fn test_json() {
}
#[test]
fn test_fn_arg_with_value_operator() {
fn json_object_colon_syntax() {
match pg().verified_expr("JSON_OBJECT('name' : 'value')") {
Expr::Function(Function {
args: FunctionArguments::List(FunctionArgumentList { args, .. }),
..
}) => {
assert!(
matches!(
&args[..],
&[FunctionArg::ExprNamed {
operator: FunctionArgOperator::Colon,
..
}]
),
"Invalid function argument: {args:?}"
);
}
other => panic!(
"Expected: JSON_OBJECT('name' : 'value') to be parsed as a function, but got {other:?}"
),
}
}
#[test]
fn json_object_value_syntax() {
match pg().verified_expr("JSON_OBJECT('name' VALUE 'value')") {
Expr::Function(Function { args: FunctionArguments::List(FunctionArgumentList { args, .. }), .. }) => {
assert!(matches!(
@ -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:?}"
),
}
}
#[test]
fn parse_json_table_is_not_reserved() {
// JSON_TABLE is not a reserved keyword in PostgreSQL, even though it is in SQL:2023