Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 86d3398

Browse files
committed
SQL/JSON: Rethink c2d93c3
This essentially reverts c2d93c3 except tests. The problem with c2d93c3 was that it only changed the casting behavior for types with typmod, and had coding issues noted in the post-commit review. This commit changes coerceJsonFuncExpr() to use assignment-level casts instead of explicit casts to coerce the result of JSON constructor functions to the specified or the default RETURNING type. Using assignment-level casts fixes the problem that using explicit casts was leading to the wrong typmod / length coercion behavior -- truncating results longer than the specified length instead of erroring out -- which c2d93c3 aimed to solve. That restricts the set of allowed target types to string types, the same set that's currently allowed. Discussion: https://postgr.es/m/202406291824.reofujy7xdj3@alvherre.pgsql
1 parent ec67869 commit 86d3398

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

src/backend/parser/parse_expr.c

+10-24
Original file line numberDiff line numberDiff line change
@@ -3582,7 +3582,6 @@ coerceJsonFuncExpr(ParseState *pstate, Node *expr,
35823582
Node *res;
35833583
int location;
35843584
Oid exprtype = exprType(expr);
3585-
int32 baseTypmod = returning->typmod;
35863585

35873586
/* if output type is not specified or equals to function type, return */
35883587
if (!OidIsValid(returning->typid) || returning->typid == exprtype)
@@ -3612,19 +3611,18 @@ coerceJsonFuncExpr(ParseState *pstate, Node *expr,
36123611
}
36133612

36143613
/*
3615-
* For domains, consider the base type's typmod to decide whether to setup
3616-
* an implicit or explicit cast.
3614+
* For other cases, try to coerce expression to the output type using
3615+
* assignment-level casts, erroring out if none available. This basically
3616+
* allows coercing the jsonb value to any string type (typcategory = 'S').
3617+
*
3618+
* Requesting assignment-level here means that typmod / length coercion
3619+
* assumes implicit coercion which is the behavior we want; see
3620+
* build_coercion_expression().
36173621
*/
3618-
if (get_typtype(returning->typid) == TYPTYPE_DOMAIN)
3619-
(void) getBaseTypeAndTypmod(returning->typid, &baseTypmod);
3620-
3621-
/* try to coerce expression to the output type */
36223622
res = coerce_to_target_type(pstate, expr, exprtype,
3623-
returning->typid, baseTypmod,
3624-
baseTypmod > 0 ? COERCION_IMPLICIT :
3625-
COERCION_EXPLICIT,
3626-
baseTypmod > 0 ? COERCE_IMPLICIT_CAST :
3627-
COERCE_EXPLICIT_CAST,
3623+
returning->typid, returning->typmod,
3624+
COERCION_ASSIGNMENT,
3625+
COERCE_IMPLICIT_CAST,
36283626
location);
36293627

36303628
if (!res && report_error)
@@ -3649,7 +3647,6 @@ makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type,
36493647
JsonConstructorExpr *jsctor = makeNode(JsonConstructorExpr);
36503648
Node *placeholder;
36513649
Node *coercion;
3652-
int32 baseTypmod = returning->typmod;
36533650

36543651
jsctor->args = args;
36553652
jsctor->func = fexpr;
@@ -3687,17 +3684,6 @@ makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type,
36873684
placeholder = (Node *) cte;
36883685
}
36893686

3690-
/*
3691-
* Convert the source expression to text, because coerceJsonFuncExpr()
3692-
* will create an implicit cast to the RETURNING types with typmod and
3693-
* there are no implicit casts from json(b) to such types. For domains,
3694-
* the base type's typmod will be considered, so do so here too.
3695-
*/
3696-
if (get_typtype(returning->typid) == TYPTYPE_DOMAIN)
3697-
(void) getBaseTypeAndTypmod(returning->typid, &baseTypmod);
3698-
if (baseTypmod > 0)
3699-
placeholder = coerce_to_specific_type(pstate, placeholder, TEXTOID,
3700-
"JSON_CONSTRUCTOR()");
37013687
coercion = coerceJsonFuncExpr(pstate, placeholder, returning, true);
37023688

37033689
if (coercion != placeholder)

0 commit comments

Comments
 (0)