From cff0d946dd65b81c020f2a06a6b029c8aeb8100e Mon Sep 17 00:00:00 2001 From: jian he Date: Thu, 25 Jan 2024 22:35:45 +0800 Subject: [PATCH v1 1/1] minor refactor coerceJsonFuncExprOutput cast function do the coercion for the following types cannot handle error softly, so we use coercion. so the following queries instead of generate an error, null will be produced. SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING int2); SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING int4); SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING int8); SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING bool); SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING numeric); SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING real); SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING float8); --- src/backend/parser/parse_expr.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 7c3870ac..2785744e 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -4503,6 +4503,33 @@ coerceJsonFuncExprOutput(ParseState *pstate, JsonExpr *jsexpr) default_typid = JsonFuncExprDefaultReturnType(jsexpr); default_typmod = -1; + + /* + * cast function do the coercion for the following types cannot handle error softly, + * so we use JsonCoercion + */ + if(jsexpr->op == JSON_QUERY_OP) + { + JsonCoercion *coercion = NULL; + switch (returning->typid) + { + case BOOLOID: + case NUMERICOID: + case INT2OID: + case INT4OID: + case INT8OID: + case FLOAT4OID: + case FLOAT8OID: + coercion = makeJsonCoercion(returning); + coercion->omit_quotes = jsexpr->omit_quotes; + break; + default: + break; + } + if(coercion) + return (Node *) coercion; + } + if (returning->typid != default_typid || returning->typmod != default_typmod) { -- 2.34.1