Skip to content

Commit 3ab9a63

Browse files
committed
Rename JsonIsPredicate.value_type, fix JSON backend/nodes/ infrastructure.
I started out with the intention to rename value_type to item_type to avoid a collision with a typedef name that appears on some platforms. Along the way, I noticed that the adjacent field "format" was not being correctly handled by the backend/nodes/ infrastructure functions: copyfuncs.c erroneously treated it as a scalar, while equalfuncs, outfuncs, and readfuncs omitted handling it at all. This looks like it might be cosmetic at the moment because the field is always NULL after parse analysis; but that's likely a bug in itself, and the code's certainly not very future-proof. Let's fix it while we can still do so without forcing an initdb on beta testers. Further study found a few other inconsistencies in the backend/nodes/ infrastructure for the recently-added JSON node types, so fix those too. catversion bumped because of potential change in stored rules. Discussion: https://postgr.es/m/526703.1652385613@sss.pgh.pa.us
1 parent 2454cb0 commit 3ab9a63

File tree

12 files changed

+52
-42
lines changed

12 files changed

+52
-42
lines changed

src/backend/executor/execExprInterp.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -3952,24 +3952,24 @@ ExecEvalJsonIsPredicate(ExprState *state, ExprEvalStep *op)
39523952
{
39533953
text *json = DatumGetTextP(js);
39543954

3955-
if (pred->value_type == JS_TYPE_ANY)
3955+
if (pred->item_type == JS_TYPE_ANY)
39563956
res = true;
39573957
else
39583958
{
39593959
switch (json_get_first_token(json, false))
39603960
{
39613961
case JSON_TOKEN_OBJECT_START:
3962-
res = pred->value_type == JS_TYPE_OBJECT;
3962+
res = pred->item_type == JS_TYPE_OBJECT;
39633963
break;
39643964
case JSON_TOKEN_ARRAY_START:
3965-
res = pred->value_type == JS_TYPE_ARRAY;
3965+
res = pred->item_type == JS_TYPE_ARRAY;
39663966
break;
39673967
case JSON_TOKEN_STRING:
39683968
case JSON_TOKEN_NUMBER:
39693969
case JSON_TOKEN_TRUE:
39703970
case JSON_TOKEN_FALSE:
39713971
case JSON_TOKEN_NULL:
3972-
res = pred->value_type == JS_TYPE_SCALAR;
3972+
res = pred->item_type == JS_TYPE_SCALAR;
39733973
break;
39743974
default:
39753975
res = false;
@@ -3986,13 +3986,13 @@ ExecEvalJsonIsPredicate(ExprState *state, ExprEvalStep *op)
39863986
}
39873987
else if (exprtype == JSONBOID)
39883988
{
3989-
if (pred->value_type == JS_TYPE_ANY)
3989+
if (pred->item_type == JS_TYPE_ANY)
39903990
res = true;
39913991
else
39923992
{
39933993
Jsonb *jb = DatumGetJsonbP(js);
39943994

3995-
switch (pred->value_type)
3995+
switch (pred->item_type)
39963996
{
39973997
case JS_TYPE_OBJECT:
39983998
res = JB_ROOT_IS_OBJECT(jb);

src/backend/nodes/copyfuncs.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -2557,11 +2557,11 @@ _copyJsonExpr(const JsonExpr *from)
25572557
COPY_NODE_FIELD(result_coercion);
25582558
COPY_NODE_FIELD(format);
25592559
COPY_NODE_FIELD(path_spec);
2560-
COPY_NODE_FIELD(passing_values);
25612560
COPY_NODE_FIELD(passing_names);
2561+
COPY_NODE_FIELD(passing_values);
25622562
COPY_NODE_FIELD(returning);
2563-
COPY_NODE_FIELD(on_error);
25642563
COPY_NODE_FIELD(on_empty);
2564+
COPY_NODE_FIELD(on_error);
25652565
COPY_NODE_FIELD(coercions);
25662566
COPY_SCALAR_FIELD(wrapper);
25672567
COPY_SCALAR_FIELD(omit_quotes);
@@ -2637,8 +2637,8 @@ _copyJsonIsPredicate(const JsonIsPredicate *from)
26372637
JsonIsPredicate *newnode = makeNode(JsonIsPredicate);
26382638

26392639
COPY_NODE_FIELD(expr);
2640-
COPY_SCALAR_FIELD(format);
2641-
COPY_SCALAR_FIELD(value_type);
2640+
COPY_NODE_FIELD(format);
2641+
COPY_SCALAR_FIELD(item_type);
26422642
COPY_SCALAR_FIELD(unique_keys);
26432643
COPY_LOCATION_FIELD(location);
26442644

@@ -2764,6 +2764,7 @@ _copyJsonTableParent(const JsonTableParent *from)
27642764
COPY_SCALAR_FIELD(outerJoin);
27652765
COPY_SCALAR_FIELD(colMin);
27662766
COPY_SCALAR_FIELD(colMax);
2767+
COPY_SCALAR_FIELD(errorOnError);
27672768

27682769
return newnode;
27692770
}

src/backend/nodes/equalfuncs.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ _equalJsonTableParent(const JsonTableParent *a, const JsonTableParent *b)
186186
COMPARE_SCALAR_FIELD(outerJoin);
187187
COMPARE_SCALAR_FIELD(colMin);
188188
COMPARE_SCALAR_FIELD(colMax);
189+
COMPARE_SCALAR_FIELD(errorOnError);
189190

190191
return true;
191192
}
@@ -1104,7 +1105,8 @@ _equalJsonIsPredicate(const JsonIsPredicate *a,
11041105
const JsonIsPredicate *b)
11051106
{
11061107
COMPARE_NODE_FIELD(expr);
1107-
COMPARE_SCALAR_FIELD(value_type);
1108+
COMPARE_NODE_FIELD(format);
1109+
COMPARE_SCALAR_FIELD(item_type);
11081110
COMPARE_SCALAR_FIELD(unique_keys);
11091111
COMPARE_LOCATION_FIELD(location);
11101112

@@ -1134,11 +1136,11 @@ _equalJsonExpr(const JsonExpr *a, const JsonExpr *b)
11341136
COMPARE_NODE_FIELD(result_coercion);
11351137
COMPARE_NODE_FIELD(format);
11361138
COMPARE_NODE_FIELD(path_spec);
1137-
COMPARE_NODE_FIELD(passing_values);
11381139
COMPARE_NODE_FIELD(passing_names);
1140+
COMPARE_NODE_FIELD(passing_values);
11391141
COMPARE_NODE_FIELD(returning);
1140-
COMPARE_NODE_FIELD(on_error);
11411142
COMPARE_NODE_FIELD(on_empty);
1143+
COMPARE_NODE_FIELD(on_error);
11421144
COMPARE_NODE_FIELD(coercions);
11431145
COMPARE_SCALAR_FIELD(wrapper);
11441146
COMPARE_SCALAR_FIELD(omit_quotes);

src/backend/nodes/makefuncs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -927,14 +927,14 @@ makeJsonKeyValue(Node *key, Node *value)
927927
* creates a JsonIsPredicate node
928928
*/
929929
Node *
930-
makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType value_type,
930+
makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type,
931931
bool unique_keys, int location)
932932
{
933933
JsonIsPredicate *n = makeNode(JsonIsPredicate);
934934

935935
n->expr = expr;
936936
n->format = format;
937-
n->value_type = value_type;
937+
n->item_type = item_type;
938938
n->unique_keys = unique_keys;
939939
n->location = location;
940940

src/backend/nodes/outfuncs.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -1801,13 +1801,13 @@ _outJsonConstructorExpr(StringInfo str, const JsonConstructorExpr *node)
18011801
{
18021802
WRITE_NODE_TYPE("JSONCONSTRUCTOREXPR");
18031803

1804+
WRITE_ENUM_FIELD(type, JsonConstructorType);
18041805
WRITE_NODE_FIELD(args);
18051806
WRITE_NODE_FIELD(func);
18061807
WRITE_NODE_FIELD(coercion);
1807-
WRITE_ENUM_FIELD(type, JsonConstructorType);
18081808
WRITE_NODE_FIELD(returning);
1809-
WRITE_BOOL_FIELD(unique);
18101809
WRITE_BOOL_FIELD(absent_on_null);
1810+
WRITE_BOOL_FIELD(unique);
18111811
WRITE_LOCATION_FIELD(location);
18121812
}
18131813

@@ -1817,7 +1817,8 @@ _outJsonIsPredicate(StringInfo str, const JsonIsPredicate *node)
18171817
WRITE_NODE_TYPE("JSONISPREDICATE");
18181818

18191819
WRITE_NODE_FIELD(expr);
1820-
WRITE_ENUM_FIELD(value_type, JsonValueType);
1820+
WRITE_NODE_FIELD(format);
1821+
WRITE_ENUM_FIELD(item_type, JsonValueType);
18211822
WRITE_BOOL_FIELD(unique_keys);
18221823
WRITE_LOCATION_FIELD(location);
18231824
}
@@ -1841,11 +1842,11 @@ _outJsonExpr(StringInfo str, const JsonExpr *node)
18411842
WRITE_NODE_FIELD(result_coercion);
18421843
WRITE_NODE_FIELD(format);
18431844
WRITE_NODE_FIELD(path_spec);
1844-
WRITE_NODE_FIELD(passing_values);
18451845
WRITE_NODE_FIELD(passing_names);
1846+
WRITE_NODE_FIELD(passing_values);
18461847
WRITE_NODE_FIELD(returning);
1847-
WRITE_NODE_FIELD(on_error);
18481848
WRITE_NODE_FIELD(on_empty);
1849+
WRITE_NODE_FIELD(on_error);
18491850
WRITE_NODE_FIELD(coercions);
18501851
WRITE_ENUM_FIELD(wrapper, JsonWrapper);
18511852
WRITE_BOOL_FIELD(omit_quotes);
@@ -1883,20 +1884,21 @@ _outJsonItemCoercions(StringInfo str, const JsonItemCoercions *node)
18831884
static void
18841885
_outJsonTableParent(StringInfo str, const JsonTableParent *node)
18851886
{
1886-
WRITE_NODE_TYPE("JSONTABPNODE");
1887+
WRITE_NODE_TYPE("JSONTABLEPARENT");
18871888

18881889
WRITE_NODE_FIELD(path);
18891890
WRITE_STRING_FIELD(name);
18901891
WRITE_NODE_FIELD(child);
18911892
WRITE_BOOL_FIELD(outerJoin);
18921893
WRITE_INT_FIELD(colMin);
18931894
WRITE_INT_FIELD(colMax);
1895+
WRITE_BOOL_FIELD(errorOnError);
18941896
}
18951897

18961898
static void
18971899
_outJsonTableSibling(StringInfo str, const JsonTableSibling *node)
18981900
{
1899-
WRITE_NODE_TYPE("JSONTABSNODE");
1901+
WRITE_NODE_TYPE("JSONTABLESIBLING");
19001902

19011903
WRITE_NODE_FIELD(larg);
19021904
WRITE_NODE_FIELD(rarg);

src/backend/nodes/readfuncs.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -1484,13 +1484,13 @@ _readJsonConstructorExpr(void)
14841484
{
14851485
READ_LOCALS(JsonConstructorExpr);
14861486

1487+
READ_ENUM_FIELD(type, JsonConstructorType);
14871488
READ_NODE_FIELD(args);
14881489
READ_NODE_FIELD(func);
14891490
READ_NODE_FIELD(coercion);
1490-
READ_INT_FIELD(type);
14911491
READ_NODE_FIELD(returning);
1492-
READ_BOOL_FIELD(unique);
14931492
READ_BOOL_FIELD(absent_on_null);
1493+
READ_BOOL_FIELD(unique);
14941494
READ_LOCATION_FIELD(location);
14951495

14961496
READ_DONE();
@@ -1523,11 +1523,11 @@ _readJsonExpr(void)
15231523
READ_NODE_FIELD(result_coercion);
15241524
READ_NODE_FIELD(format);
15251525
READ_NODE_FIELD(path_spec);
1526-
READ_NODE_FIELD(passing_values);
15271526
READ_NODE_FIELD(passing_names);
1527+
READ_NODE_FIELD(passing_values);
15281528
READ_NODE_FIELD(returning);
1529-
READ_NODE_FIELD(on_error);
15301529
READ_NODE_FIELD(on_empty);
1530+
READ_NODE_FIELD(on_error);
15311531
READ_NODE_FIELD(coercions);
15321532
READ_ENUM_FIELD(wrapper, JsonWrapper);
15331533
READ_BOOL_FIELD(omit_quotes);
@@ -1547,6 +1547,7 @@ _readJsonTableParent(void)
15471547
READ_BOOL_FIELD(outerJoin);
15481548
READ_INT_FIELD(colMin);
15491549
READ_INT_FIELD(colMax);
1550+
READ_BOOL_FIELD(errorOnError);
15501551

15511552
READ_DONE();
15521553
}
@@ -1610,7 +1611,8 @@ _readJsonIsPredicate()
16101611
READ_LOCALS(JsonIsPredicate);
16111612

16121613
READ_NODE_FIELD(expr);
1613-
READ_ENUM_FIELD(value_type, JsonValueType);
1614+
READ_NODE_FIELD(format);
1615+
READ_ENUM_FIELD(item_type, JsonValueType);
16141616
READ_BOOL_FIELD(unique_keys);
16151617
READ_LOCATION_FIELD(location);
16161618

@@ -3229,9 +3231,9 @@ parseNodeString(void)
32293231
return_value = _readJsonCoercion();
32303232
else if (MATCH("JSONITEMCOERCIONS", 17))
32313233
return_value = _readJsonItemCoercions();
3232-
else if (MATCH("JSONTABPNODE", 12))
3234+
else if (MATCH("JSONTABLEPARENT", 15))
32333235
return_value = _readJsonTableParent();
3234-
else if (MATCH("JSONTABSNODE", 12))
3236+
else if (MATCH("JSONTABLESIBLING", 16))
32353237
return_value = _readJsonTableSibling();
32363238
else
32373239
{

src/backend/parser/parse_expr.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -4018,8 +4018,7 @@ transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format,
40184018
}
40194019

40204020
/*
4021-
* Transform IS JSON predicate into
4022-
* json[b]_is_valid(json, value_type [, check_key_uniqueness]) call.
4021+
* Transform IS JSON predicate.
40234022
*/
40244023
static Node *
40254024
transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
@@ -4035,7 +4034,8 @@ transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
40354034
errmsg("cannot use type %s in IS JSON predicate",
40364035
format_type_be(exprtype))));
40374036

4038-
return makeJsonIsPredicate(expr, NULL, pred->value_type,
4037+
/* This intentionally(?) drops the format clause. */
4038+
return makeJsonIsPredicate(expr, NULL, pred->item_type,
40394039
pred->unique_keys, pred->location);
40404040
}
40414041

src/backend/utils/adt/ruleutils.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -9730,7 +9730,9 @@ get_rule_expr(Node *node, deparse_context *context,
97309730

97319731
appendStringInfoString(context->buf, " IS JSON");
97329732

9733-
switch (pred->value_type)
9733+
/* TODO: handle FORMAT clause */
9734+
9735+
switch (pred->item_type)
97349736
{
97359737
case JS_TYPE_SCALAR:
97369738
appendStringInfoString(context->buf, " SCALAR");

src/backend/utils/misc/queryjumble.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ JumbleExpr(JumbleState *jstate, Node *node)
741741
{
742742
JsonFormat *format = (JsonFormat *) node;
743743

744-
APP_JUMB(format->type);
744+
APP_JUMB(format->format_type);
745745
APP_JUMB(format->encoding);
746746
}
747747
break;
@@ -767,12 +767,13 @@ JumbleExpr(JumbleState *jstate, Node *node)
767767
{
768768
JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
769769

770+
APP_JUMB(ctor->type);
771+
JumbleExpr(jstate, (Node *) ctor->args);
770772
JumbleExpr(jstate, (Node *) ctor->func);
771773
JumbleExpr(jstate, (Node *) ctor->coercion);
772774
JumbleExpr(jstate, (Node *) ctor->returning);
773-
APP_JUMB(ctor->type);
774-
APP_JUMB(ctor->unique);
775775
APP_JUMB(ctor->absent_on_null);
776+
APP_JUMB(ctor->unique);
776777
}
777778
break;
778779
case T_JsonIsPredicate:
@@ -781,8 +782,8 @@ JumbleExpr(JumbleState *jstate, Node *node)
781782

782783
JumbleExpr(jstate, (Node *) pred->expr);
783784
JumbleExpr(jstate, (Node *) pred->format);
785+
APP_JUMB(pred->item_type);
784786
APP_JUMB(pred->unique_keys);
785-
APP_JUMB(pred->value_type);
786787
}
787788
break;
788789
case T_JsonExpr:

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 202205121
56+
#define CATALOG_VERSION_NO 202205131
5757

5858
#endif

src/include/nodes/makefuncs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ extern Node *makeJsonTableJoinedPlan(JsonTablePlanJoinType type,
114114
Node *plan1, Node *plan2, int location);
115115
extern Node *makeJsonKeyValue(Node *key, Node *value);
116116
extern Node *makeJsonIsPredicate(Node *expr, JsonFormat *format,
117-
JsonValueType vtype, bool unique_keys,
117+
JsonValueType item_type, bool unique_keys,
118118
int location);
119119
extern JsonEncoding makeJsonEncoding(char *name);
120120

src/include/nodes/primnodes.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1387,14 +1387,14 @@ typedef enum JsonValueType
13871387

13881388
/*
13891389
* JsonIsPredicate -
1390-
* untransformed representation of IS JSON predicate
1390+
* representation of IS JSON predicate
13911391
*/
13921392
typedef struct JsonIsPredicate
13931393
{
13941394
NodeTag type;
1395-
Node *expr; /* untransformed expression */
1395+
Node *expr; /* subject expression */
13961396
JsonFormat *format; /* FORMAT clause, if specified */
1397-
JsonValueType value_type; /* JSON item type */
1397+
JsonValueType item_type; /* JSON item type */
13981398
bool unique_keys; /* check key uniqueness? */
13991399
int location; /* token location, or -1 if unknown */
14001400
} JsonIsPredicate;

0 commit comments

Comments
 (0)