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

Commit a5c58b5

Browse files
author
Nikita Glukhov
committed
Add JSON_OBJECT() transformation
1 parent 95b3ce0 commit a5c58b5

File tree

19 files changed

+1468
-18
lines changed

19 files changed

+1468
-18
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,27 @@ JumbleExpr(pgssJumbleState *jstate, Node *node)
28112811
JumbleExpr(jstate, (Node *) conf->exclRelTlist);
28122812
}
28132813
break;
2814+
case T_JsonValueExpr:
2815+
{
2816+
JsonValueExpr *expr = (JsonValueExpr *) node;
2817+
2818+
JumbleExpr(jstate, expr->expr);
2819+
APP_JUMB(expr->format.type);
2820+
APP_JUMB(expr->format.encoding);
2821+
}
2822+
break;
2823+
case T_JsonCtorOpts:
2824+
{
2825+
JsonCtorOpts *opts = (JsonCtorOpts *) node;
2826+
2827+
APP_JUMB(opts->returning.format.type);
2828+
APP_JUMB(opts->returning.format.encoding);
2829+
APP_JUMB(opts->returning.typid);
2830+
APP_JUMB(opts->returning.typmod);
2831+
APP_JUMB(opts->unique);
2832+
APP_JUMB(opts->absent_on_null);
2833+
}
2834+
break;
28142835
case T_List:
28152836
foreach(temp, (List *) node)
28162837
{

src/backend/executor/execExpr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,11 @@ ExecInitExprRec(Expr *node, PlanState *parent, ExprState *state,
20332033
break;
20342034
}
20352035

2036+
case T_JsonValueExpr:
2037+
ExecInitExprRec(((JsonValueExpr *) node)->expr,
2038+
parent, state, resv, resnull);
2039+
break;
2040+
20362041
default:
20372042
elog(ERROR, "unrecognized node type: %d",
20382043
(int) nodeTag(node));

src/backend/nodes/copyfuncs.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,70 @@ _copyOnConflictExpr(const OnConflictExpr *from)
21282128
return newnode;
21292129
}
21302130

2131+
/*
2132+
* _copyJsonValueExpr
2133+
*/
2134+
static JsonValueExpr *
2135+
_copyJsonValueExpr(const JsonValueExpr *from)
2136+
{
2137+
JsonValueExpr *newnode = makeNode(JsonValueExpr);
2138+
2139+
COPY_NODE_FIELD(expr);
2140+
COPY_SCALAR_FIELD(format);
2141+
2142+
return newnode;
2143+
}
2144+
2145+
/*
2146+
* _copyJsonKeyValue
2147+
*/
2148+
static JsonKeyValue *
2149+
_copyJsonKeyValue(const JsonKeyValue *from)
2150+
{
2151+
JsonKeyValue *newnode = makeNode(JsonKeyValue);
2152+
2153+
COPY_NODE_FIELD(key);
2154+
COPY_NODE_FIELD(value);
2155+
2156+
return newnode;
2157+
}
2158+
2159+
/*
2160+
* _copyJsonObjectCtor
2161+
*/
2162+
static JsonObjectCtor *
2163+
_copyJsonObjectCtor(const JsonObjectCtor *from)
2164+
{
2165+
JsonObjectCtor *newnode = makeNode(JsonObjectCtor);
2166+
2167+
COPY_NODE_FIELD(exprs);
2168+
COPY_NODE_FIELD(output);
2169+
COPY_SCALAR_FIELD(absent_on_null);
2170+
COPY_SCALAR_FIELD(unique);
2171+
COPY_LOCATION_FIELD(location);
2172+
2173+
return newnode;
2174+
}
2175+
2176+
/*
2177+
* _copyJsonCtorOpts
2178+
*/
2179+
static JsonCtorOpts *
2180+
_copyJsonCtorOpts(const JsonCtorOpts *from)
2181+
{
2182+
JsonCtorOpts *newnode = makeNode(JsonCtorOpts);
2183+
2184+
COPY_SCALAR_FIELD(returning.format.type);
2185+
COPY_SCALAR_FIELD(returning.format.encoding);
2186+
COPY_LOCATION_FIELD(returning.format.location);
2187+
COPY_SCALAR_FIELD(returning.typid);
2188+
COPY_SCALAR_FIELD(returning.typmod);
2189+
COPY_SCALAR_FIELD(unique);
2190+
COPY_SCALAR_FIELD(absent_on_null);
2191+
2192+
return newnode;
2193+
}
2194+
21312195
/* ****************************************************************
21322196
* relation.h copy functions
21332197
*
@@ -4997,6 +5061,18 @@ copyObjectImpl(const void *from)
49975061
case T_OnConflictExpr:
49985062
retval = _copyOnConflictExpr(from);
49995063
break;
5064+
case T_JsonValueExpr:
5065+
retval = _copyJsonValueExpr(from);
5066+
break;
5067+
case T_JsonKeyValue:
5068+
retval = _copyJsonKeyValue(from);
5069+
break;
5070+
case T_JsonCtorOpts:
5071+
retval = _copyJsonCtorOpts(from);
5072+
break;
5073+
case T_JsonObjectCtor:
5074+
retval = _copyJsonObjectCtor(from);
5075+
break;
50005076

50015077
/*
50025078
* RELATION NODES

src/backend/nodes/equalfuncs.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,31 @@ _equalOnConflictExpr(const OnConflictExpr *a, const OnConflictExpr *b)
818818
return true;
819819
}
820820

821+
static bool
822+
_equalJsonValueExpr(const JsonValueExpr *a, const JsonValueExpr *b)
823+
{
824+
COMPARE_NODE_FIELD(expr);
825+
COMPARE_SCALAR_FIELD(format.type);
826+
COMPARE_SCALAR_FIELD(format.encoding);
827+
COMPARE_LOCATION_FIELD(format.location);
828+
829+
return true;
830+
}
831+
832+
static bool
833+
_equalJsonCtorOpts(const JsonCtorOpts *a, const JsonCtorOpts *b)
834+
{
835+
COMPARE_SCALAR_FIELD(returning.format.type);
836+
COMPARE_SCALAR_FIELD(returning.format.encoding);
837+
COMPARE_LOCATION_FIELD(returning.format.location);
838+
COMPARE_SCALAR_FIELD(returning.typid);
839+
COMPARE_SCALAR_FIELD(returning.typmod);
840+
COMPARE_SCALAR_FIELD(absent_on_null);
841+
COMPARE_SCALAR_FIELD(unique);
842+
843+
return true;
844+
}
845+
821846
/*
822847
* Stuff from relation.h
823848
*/
@@ -3154,6 +3179,12 @@ equal(const void *a, const void *b)
31543179
case T_JoinExpr:
31553180
retval = _equalJoinExpr(a, b);
31563181
break;
3182+
case T_JsonValueExpr:
3183+
retval = _equalJsonValueExpr(a, b);
3184+
break;
3185+
case T_JsonCtorOpts:
3186+
retval = _equalJsonCtorOpts(a, b);
3187+
break;
31573188

31583189
/*
31593190
* RELATION NODES

src/backend/nodes/nodeFuncs.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ exprType(const Node *expr)
259259
case T_PlaceHolderVar:
260260
type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
261261
break;
262+
case T_JsonValueExpr:
263+
type = exprType((Node *) ((const JsonValueExpr *) expr)->expr);
264+
break;
262265
default:
263266
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
264267
type = InvalidOid; /* keep compiler quiet */
@@ -492,6 +495,8 @@ exprTypmod(const Node *expr)
492495
return ((const SetToDefault *) expr)->typeMod;
493496
case T_PlaceHolderVar:
494497
return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
498+
case T_JsonValueExpr:
499+
return exprTypmod((Node *) ((const JsonValueExpr *) expr)->expr);
495500
default:
496501
break;
497502
}
@@ -903,6 +908,9 @@ exprCollation(const Node *expr)
903908
case T_PlaceHolderVar:
904909
coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
905910
break;
911+
case T_JsonValueExpr:
912+
coll = exprCollation((Node *) ((const JsonValueExpr *) expr)->expr);
913+
break;
906914
default:
907915
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
908916
coll = InvalidOid; /* keep compiler quiet */
@@ -1104,6 +1112,10 @@ exprSetCollation(Node *expr, Oid collation)
11041112
Assert(!OidIsValid(collation)); /* result is always an integer
11051113
* type */
11061114
break;
1115+
case T_JsonValueExpr:
1116+
exprSetCollation((Node *) ((const JsonValueExpr *) expr)->expr,
1117+
collation);
1118+
break;
11071119
default:
11081120
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
11091121
break;
@@ -1544,6 +1556,9 @@ exprLocation(const Node *expr)
15441556
case T_PartitionRangeDatum:
15451557
loc = ((const PartitionRangeDatum *) expr)->location;
15461558
break;
1559+
case T_JsonValueExpr:
1560+
loc = exprLocation((Node *) ((const JsonValueExpr *) expr)->expr);
1561+
break;
15471562
default:
15481563
/* for any other node type it's just unknown... */
15491564
loc = -1;
@@ -2218,6 +2233,8 @@ expression_tree_walker(Node *node,
22182233
return true;
22192234
}
22202235
break;
2236+
case T_JsonValueExpr:
2237+
return walker(((JsonValueExpr *) node)->expr, context);
22212238
default:
22222239
elog(ERROR, "unrecognized node type: %d",
22232240
(int) nodeTag(node));
@@ -3035,6 +3052,16 @@ expression_tree_mutator(Node *node,
30353052
return (Node *) newnode;
30363053
}
30373054
break;
3055+
case T_JsonValueExpr:
3056+
{
3057+
JsonValueExpr *jve = (JsonValueExpr *) node;
3058+
JsonValueExpr *newnode;
3059+
3060+
FLATCOPY(newnode, jve, JsonValueExpr);
3061+
MUTATE(newnode->expr, jve->expr, Expr *);
3062+
3063+
return (Node *) newnode;
3064+
}
30383065
default:
30393066
elog(ERROR, "unrecognized node type: %d",
30403067
(int) nodeTag(node));
@@ -3679,6 +3706,30 @@ raw_expression_tree_walker(Node *node,
36793706
break;
36803707
case T_CommonTableExpr:
36813708
return walker(((CommonTableExpr *) node)->ctequery, context);
3709+
case T_JsonValueExpr:
3710+
return walker(((JsonValueExpr *) node)->expr, context);
3711+
case T_JsonOutput:
3712+
return walker(((JsonOutput *) node)->typename, context);
3713+
case T_JsonKeyValue:
3714+
{
3715+
JsonKeyValue *jkv = (JsonKeyValue *) node;
3716+
3717+
if (walker(jkv->key, context))
3718+
return true;
3719+
if (walker(jkv->value, context))
3720+
return true;
3721+
}
3722+
break;
3723+
case T_JsonObjectCtor:
3724+
{
3725+
JsonObjectCtor *joc = (JsonObjectCtor *) node;
3726+
3727+
if (walker(joc->output, context))
3728+
return true;
3729+
if (walker(joc->exprs, context))
3730+
return true;
3731+
}
3732+
break;
36823733
default:
36833734
elog(ERROR, "unrecognized node type: %d",
36843735
(int) nodeTag(node));

src/backend/nodes/outfuncs.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,31 @@ _outOnConflictExpr(StringInfo str, const OnConflictExpr *node)
17021702
WRITE_NODE_FIELD(exclRelTlist);
17031703
}
17041704

1705+
static void
1706+
_outJsonValueExpr(StringInfo str, const JsonValueExpr *node)
1707+
{
1708+
WRITE_NODE_TYPE("JSONVALUEEXPR");
1709+
1710+
WRITE_NODE_FIELD(expr);
1711+
WRITE_ENUM_FIELD(format.type, JsonFormatType);
1712+
WRITE_ENUM_FIELD(format.encoding, JsonEncoding);
1713+
WRITE_LOCATION_FIELD(format.location);
1714+
}
1715+
1716+
static void
1717+
_outJsonCtorOpts(StringInfo str, const JsonCtorOpts *node)
1718+
{
1719+
WRITE_NODE_TYPE("JSONCTOROPTS");
1720+
1721+
WRITE_ENUM_FIELD(returning.format.type, JsonFormatType);
1722+
WRITE_ENUM_FIELD(returning.format.encoding, JsonEncoding);
1723+
WRITE_LOCATION_FIELD(returning.format.location);
1724+
WRITE_OID_FIELD(returning.typid);
1725+
WRITE_INT_FIELD(returning.typmod);
1726+
WRITE_BOOL_FIELD(unique);
1727+
WRITE_BOOL_FIELD(absent_on_null);
1728+
}
1729+
17051730
/*****************************************************************************
17061731
*
17071732
* Stuff from relation.h.
@@ -4244,6 +4269,12 @@ outNode(StringInfo str, const void *obj)
42444269
case T_PartitionRangeDatum:
42454270
_outPartitionRangeDatum(str, obj);
42464271
break;
4272+
case T_JsonValueExpr:
4273+
_outJsonValueExpr(str, obj);
4274+
break;
4275+
case T_JsonCtorOpts:
4276+
_outJsonCtorOpts(str, obj);
4277+
break;
42474278

42484279
default:
42494280

src/backend/nodes/readfuncs.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,41 @@ _readOnConflictExpr(void)
13291329
READ_DONE();
13301330
}
13311331

1332+
/*
1333+
* _readJsonValueExpr
1334+
*/
1335+
static JsonValueExpr *
1336+
_readJsonValueExpr(void)
1337+
{
1338+
READ_LOCALS(JsonValueExpr);
1339+
1340+
READ_NODE_FIELD(expr);
1341+
READ_ENUM_FIELD(format.type, JsonFormatType);
1342+
READ_ENUM_FIELD(format.encoding, JsonEncoding);
1343+
READ_LOCATION_FIELD(format.location);
1344+
1345+
READ_DONE();
1346+
}
1347+
1348+
/*
1349+
* _readJsonCtorOpts
1350+
*/
1351+
static JsonCtorOpts *
1352+
_readJsonCtorOpts(void)
1353+
{
1354+
READ_LOCALS(JsonCtorOpts);
1355+
1356+
READ_ENUM_FIELD(returning.format.type, JsonFormatType);
1357+
READ_ENUM_FIELD(returning.format.encoding, JsonEncoding);
1358+
READ_LOCATION_FIELD(returning.format.location);
1359+
READ_OID_FIELD(returning.typid);
1360+
READ_INT_FIELD(returning.typmod);
1361+
READ_BOOL_FIELD(unique);
1362+
READ_BOOL_FIELD(absent_on_null);
1363+
1364+
READ_DONE();
1365+
}
1366+
13321367
/*
13331368
* Stuff from parsenodes.h.
13341369
*/
@@ -2674,6 +2709,10 @@ parseNodeString(void)
26742709
return_value = _readPartitionBoundSpec();
26752710
else if (MATCH("PARTITIONRANGEDATUM", 19))
26762711
return_value = _readPartitionRangeDatum();
2712+
else if (MATCH("JSONVALUEEXPR", 13))
2713+
return_value = _readJsonValueExpr();
2714+
else if (MATCH("JSONCTOROPTS", 12))
2715+
return_value = _readJsonCtorOpts();
26772716
else
26782717
{
26792718
elog(ERROR, "badly formatted node string \"%.32s\"...", token);

0 commit comments

Comments
 (0)