@@ -3351,7 +3351,31 @@ fn test_json() {
33513351}
33523352
33533353#[ test]
3354- fn test_fn_arg_with_value_operator ( ) {
3354+ fn json_object_colon_syntax ( ) {
3355+ match pg ( ) . verified_expr ( "JSON_OBJECT('name' : 'value')" ) {
3356+ Expr :: Function ( Function {
3357+ args : FunctionArguments :: List ( FunctionArgumentList { args, .. } ) ,
3358+ ..
3359+ } ) => {
3360+ assert ! (
3361+ matches!(
3362+ & args[ ..] ,
3363+ & [ FunctionArg :: ExprNamed {
3364+ operator: FunctionArgOperator :: Colon ,
3365+ ..
3366+ } ]
3367+ ) ,
3368+ "Invalid function argument: {args:?}"
3369+ ) ;
3370+ }
3371+ other => panic ! (
3372+ "Expected: JSON_OBJECT('name' : 'value') to be parsed as a function, but got {other:?}"
3373+ ) ,
3374+ }
3375+ }
3376+
3377+ #[ test]
3378+ fn json_object_value_syntax ( ) {
33553379 match pg ( ) . verified_expr ( "JSON_OBJECT('name' VALUE 'value')" ) {
33563380 Expr :: Function ( Function { args : FunctionArguments :: List ( FunctionArgumentList { args, .. } ) , .. } ) => {
33573381 assert ! ( matches!(
@@ -3363,6 +3387,63 @@ fn test_fn_arg_with_value_operator() {
33633387 }
33643388}
33653389
3390+ #[ test]
3391+ fn parse_json_object ( ) {
3392+ let sql = "JSON_OBJECT('name' VALUE 'value' NULL ON NULL)" ;
3393+ let expr = pg ( ) . verified_expr ( sql) ;
3394+ assert ! (
3395+ matches!(
3396+ expr. clone( ) ,
3397+ Expr :: Function ( Function {
3398+ name: ObjectName ( parts) ,
3399+ args: FunctionArguments :: List ( FunctionArgumentList { args, clauses, .. } ) ,
3400+ ..
3401+ } ) if parts == vec![ ObjectNamePart :: Identifier ( Ident :: new( "JSON_OBJECT" ) ) ]
3402+ && matches!(
3403+ & args[ ..] ,
3404+ & [ FunctionArg :: ExprNamed { operator: FunctionArgOperator :: Value , .. } ]
3405+ )
3406+ && clauses == vec![ FunctionArgumentClause :: JsonNullClause ( JsonNullClause :: NullOnNull ) ]
3407+ ) ,
3408+ "Failed to parse JSON_OBJECT with expected structure, got: {expr:?}"
3409+ ) ;
3410+
3411+ let sql = "JSON_OBJECT('name' VALUE 'value' RETURNING JSONB)" ;
3412+ let expr = pg ( ) . verified_expr ( sql) ;
3413+ assert ! (
3414+ matches!(
3415+ expr. clone( ) ,
3416+ Expr :: Function ( Function {
3417+ name: ObjectName ( parts) ,
3418+ args: FunctionArguments :: List ( FunctionArgumentList { args, clauses, .. } ) ,
3419+ ..
3420+ } ) if parts == vec![ ObjectNamePart :: Identifier ( Ident :: new( "JSON_OBJECT" ) ) ]
3421+ && matches!(
3422+ & args[ ..] ,
3423+ & [ FunctionArg :: ExprNamed { operator: FunctionArgOperator :: Value , .. } ]
3424+ )
3425+ && clauses == vec![ FunctionArgumentClause :: JsonReturningClause ( JsonReturningClause { data_type: DataType :: JSONB } ) ]
3426+ ) ,
3427+ "Failed to parse JSON_OBJECT with expected structure, got: {expr:?}"
3428+ ) ;
3429+
3430+ let sql = "JSON_OBJECT(RETURNING JSONB)" ;
3431+ let expr = pg ( ) . verified_expr ( sql) ;
3432+ assert ! (
3433+ matches!(
3434+ expr. clone( ) ,
3435+ Expr :: Function ( Function {
3436+ name: ObjectName ( parts) ,
3437+ args: FunctionArguments :: List ( FunctionArgumentList { args, clauses, .. } ) ,
3438+ ..
3439+ } ) if parts == vec![ ObjectNamePart :: Identifier ( Ident :: new( "JSON_OBJECT" ) ) ]
3440+ && args. is_empty( )
3441+ && clauses == vec![ FunctionArgumentClause :: JsonReturningClause ( JsonReturningClause { data_type: DataType :: JSONB } ) ]
3442+ ) ,
3443+ "Failed to parse JSON_OBJECT with expected structure, got: {expr:?}"
3444+ ) ;
3445+ }
3446+
33663447#[ test]
33673448fn parse_json_table_is_not_reserved ( ) {
33683449 // JSON_TABLE is not a reserved keyword in PostgreSQL, even though it is in SQL:2023
0 commit comments