Skip to content

Commit 7c7412c

Browse files
committed
Code review for commit b6e1157
b6e1157 made some changes to enforce that JsonValueExpr.formatted_expr is always set and is the expression that gives a JsonValueExpr its runtime value, but that's not really apparent from the comments about and the code manipulating formatted_expr. This commit fixes that. Per suggestion from Álvaro Herrera. Discussion: https://postgr.es/m/20230718155313.3wqg6encgt32adqb%40alvherre.pgsql
1 parent 97ff8dd commit 7c7412c

File tree

6 files changed

+24
-20
lines changed

6 files changed

+24
-20
lines changed

src/backend/nodes/makefuncs.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -848,16 +848,13 @@ makeJsonFormat(JsonFormatType type, JsonEncoding encoding, int location)
848848
* creates a JsonValueExpr node
849849
*/
850850
JsonValueExpr *
851-
makeJsonValueExpr(Expr *expr, JsonFormat *format)
851+
makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr,
852+
JsonFormat *format)
852853
{
853854
JsonValueExpr *jve = makeNode(JsonValueExpr);
854855

855-
jve->raw_expr = expr;
856-
857-
/*
858-
* Set after checking the format, if needed, in transformJsonValueExpr().
859-
*/
860-
jve->formatted_expr = NULL;
856+
jve->raw_expr = raw_expr;
857+
jve->formatted_expr = formatted_expr;
861858
jve->format = format;
862859

863860
return jve;

src/backend/nodes/nodeFuncs.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ exprType(const Node *expr)
225225
{
226226
const JsonValueExpr *jve = (const JsonValueExpr *) expr;
227227

228-
type = exprType((Node *)
229-
(jve->formatted_expr ? jve->formatted_expr :
230-
jve->raw_expr));
228+
type = exprType((Node *) jve->formatted_expr);
231229
}
232230
break;
233231
case T_JsonConstructorExpr:

src/backend/parser/gram.y

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16362,7 +16362,9 @@ opt_asymmetric: ASYMMETRIC
1636216362
json_value_expr:
1636316363
a_expr json_format_clause_opt
1636416364
{
16365-
$$ = (Node *) makeJsonValueExpr((Expr *) $1, castNode(JsonFormat, $2));
16365+
/* formatted_expr will be set during parse-analysis. */
16366+
$$ = (Node *) makeJsonValueExpr((Expr *) $1, NULL,
16367+
castNode(JsonFormat, $2));
1636616368
}
1636716369
;
1636816370

src/backend/parser/parse_expr.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,10 @@ makeJsonByteaToTextConversion(Node *expr, JsonFormat *format, int location)
32053205
/*
32063206
* Transform JSON value expression using specified input JSON format or
32073207
* default format otherwise.
3208+
*
3209+
* Returned expression is either ve->raw_expr coerced to text (if needed) or
3210+
* a JsonValueExpr with formatted_expr set to the coerced copy of raw_expr
3211+
* if the specified format requires it.
32083212
*/
32093213
static Node *
32103214
transformJsonValueExpr(ParseState *pstate, const char *constructName,
@@ -3304,6 +3308,10 @@ transformJsonValueExpr(ParseState *pstate, const char *constructName,
33043308
}
33053309
}
33063310

3311+
/* If returning a JsonValueExpr, formatted_expr must have been set. */
3312+
Assert(!IsA(expr, JsonValueExpr) ||
3313+
((JsonValueExpr *) expr)->formatted_expr != NULL);
3314+
33073315
return expr;
33083316
}
33093317

@@ -3631,13 +3639,12 @@ transformJsonArrayQueryConstructor(ParseState *pstate,
36313639
makeString(pstrdup("a")));
36323640
colref->location = ctor->location;
36333641

3634-
agg->arg = makeJsonValueExpr((Expr *) colref, ctor->format);
3635-
36363642
/*
36373643
* No formatting necessary, so set formatted_expr to be the same as
36383644
* raw_expr.
36393645
*/
3640-
agg->arg->formatted_expr = agg->arg->raw_expr;
3646+
agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3647+
ctor->format);
36413648
agg->absent_on_null = ctor->absent_on_null;
36423649
agg->constructor = makeNode(JsonAggConstructor);
36433650
agg->constructor->agg_order = NIL;
@@ -3906,9 +3913,7 @@ transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format,
39063913
expr = makeJsonByteaToTextConversion(expr, format, exprLocation(expr));
39073914
*exprtype = TEXTOID;
39083915

3909-
jve = makeJsonValueExpr((Expr *) raw_expr, format);
3910-
3911-
jve->formatted_expr = (Expr *) expr;
3916+
jve = makeJsonValueExpr((Expr *) raw_expr, (Expr *) expr, format);
39123917
expr = (Node *) jve;
39133918
}
39143919
else

src/include/nodes/makefuncs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ extern VacuumRelation *makeVacuumRelation(RangeVar *relation, Oid oid, List *va_
110110

111111
extern JsonFormat *makeJsonFormat(JsonFormatType type, JsonEncoding encoding,
112112
int location);
113-
extern JsonValueExpr *makeJsonValueExpr(Expr *expr, JsonFormat *format);
113+
extern JsonValueExpr *makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr,
114+
JsonFormat *format);
114115
extern Node *makeJsonKeyValue(Node *key, Node *value);
115116
extern Node *makeJsonIsPredicate(Node *expr, JsonFormat *format,
116117
JsonValueType item_type, bool unique_keys,

src/include/nodes/primnodes.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,8 +1596,9 @@ typedef struct JsonReturning
15961596
* JsonValueExpr -
15971597
* representation of JSON value expression (expr [FORMAT JsonFormat])
15981598
*
1599-
* Note that raw_expr is only there for displaying and is not evaluated by
1600-
* ExecInterpExpr() and eval_const_exprs_mutator().
1599+
* The actual value is obtained by evaluating formatted_expr. raw_expr is
1600+
* only there for displaying the original user-written expression and is not
1601+
* evaluated by ExecInterpExpr() and eval_const_exprs_mutator().
16011602
*/
16021603
typedef struct JsonValueExpr
16031604
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy