Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86d3398

Browse files
committed
SQL/JSON: Rethinkc2d93c3
This essentially revertsc2d93c3 except tests. The problem withc2d93c3 was that it only changed the casting behavior for typeswith typmod, and had coding issues noted in the post-commit review.This commit changes coerceJsonFuncExpr() to use assignment-level castsinstead of explicit casts to coerce the result of JSON constructorfunctions to the specified or the default RETURNING type. Usingassignment-level casts fixes the problem that using explicit casts wasleading to the wrong typmod / length coercion behavior -- truncatingresults longer than the specified length instead of erroring out --whichc2d93c3 aimed to solve.That restricts the set of allowed target types to string types, thesame set that's currently allowed.Discussion:https://postgr.es/m/202406291824.reofujy7xdj3@alvherre.pgsql
1 parentec67869 commit86d3398

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

‎src/backend/parser/parse_expr.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,7 +3582,6 @@ coerceJsonFuncExpr(ParseState *pstate, Node *expr,
35823582
Node*res;
35833583
intlocation;
35843584
Oidexprtype=exprType(expr);
3585-
int32baseTypmod=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-
int32baseTypmod=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)

[8]ページ先頭

©2009-2025 Movatter.jp