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

Commit c9e0662

Browse files
author
Nikita Glukhov
committed
Add JSON_OBJECT() transformation
1 parent 37246b2 commit c9e0662

File tree

20 files changed

+1494
-18
lines changed

20 files changed

+1494
-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
@@ -2812,6 +2812,27 @@ JumbleExpr(pgssJumbleState *jstate, Node *node)
28122812
JumbleExpr(jstate, (Node *) conf->exclRelTlist);
28132813
}
28142814
break;
2815+
case T_JsonValueExpr:
2816+
{
2817+
JsonValueExpr *expr = (JsonValueExpr *) node;
2818+
2819+
JumbleExpr(jstate, (Node *) expr->expr);
2820+
APP_JUMB(expr->format.type);
2821+
APP_JUMB(expr->format.encoding);
2822+
}
2823+
break;
2824+
case T_JsonCtorOpts:
2825+
{
2826+
JsonCtorOpts *opts = (JsonCtorOpts *) node;
2827+
2828+
APP_JUMB(opts->returning.format.type);
2829+
APP_JUMB(opts->returning.format.encoding);
2830+
APP_JUMB(opts->returning.typid);
2831+
APP_JUMB(opts->returning.typmod);
2832+
APP_JUMB(opts->unique);
2833+
APP_JUMB(opts->absent_on_null);
2834+
}
2835+
break;
28152836
case T_List:
28162837
foreach(temp, (List *) node)
28172838
{

src/backend/executor/execExpr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,11 @@ ExecInitExprRec(Expr *node, ExprState *state,
21132113
break;
21142114
}
21152115

2116+
case T_JsonValueExpr:
2117+
ExecInitExprRec(((JsonValueExpr *) node)->expr, state, resv,
2118+
resnull);
2119+
break;
2120+
21162121
default:
21172122
elog(ERROR, "unrecognized node type: %d",
21182123
(int) nodeTag(node));

src/backend/nodes/copyfuncs.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,84 @@ _copyOnConflictExpr(const OnConflictExpr *from)
21922192
return newnode;
21932193
}
21942194

2195+
/*
2196+
* _copyJsonValueExpr
2197+
*/
2198+
static JsonValueExpr *
2199+
_copyJsonValueExpr(const JsonValueExpr *from)
2200+
{
2201+
JsonValueExpr *newnode = makeNode(JsonValueExpr);
2202+
2203+
COPY_NODE_FIELD(expr);
2204+
COPY_SCALAR_FIELD(format);
2205+
2206+
return newnode;
2207+
}
2208+
2209+
/*
2210+
* _copyJsonKeyValue
2211+
*/
2212+
static JsonKeyValue *
2213+
_copyJsonKeyValue(const JsonKeyValue *from)
2214+
{
2215+
JsonKeyValue *newnode = makeNode(JsonKeyValue);
2216+
2217+
COPY_NODE_FIELD(key);
2218+
COPY_NODE_FIELD(value);
2219+
2220+
return newnode;
2221+
}
2222+
2223+
/*
2224+
* _copyJsonObjectCtor
2225+
*/
2226+
static JsonObjectCtor *
2227+
_copyJsonObjectCtor(const JsonObjectCtor *from)
2228+
{
2229+
JsonObjectCtor *newnode = makeNode(JsonObjectCtor);
2230+
2231+
COPY_NODE_FIELD(exprs);
2232+
COPY_NODE_FIELD(output);
2233+
COPY_SCALAR_FIELD(absent_on_null);
2234+
COPY_SCALAR_FIELD(unique);
2235+
COPY_LOCATION_FIELD(location);
2236+
2237+
return newnode;
2238+
}
2239+
2240+
/*
2241+
* _copyJsonCtorOpts
2242+
*/
2243+
static JsonCtorOpts *
2244+
_copyJsonCtorOpts(const JsonCtorOpts *from)
2245+
{
2246+
JsonCtorOpts *newnode = makeNode(JsonCtorOpts);
2247+
2248+
COPY_SCALAR_FIELD(returning.format.type);
2249+
COPY_SCALAR_FIELD(returning.format.encoding);
2250+
COPY_LOCATION_FIELD(returning.format.location);
2251+
COPY_SCALAR_FIELD(returning.typid);
2252+
COPY_SCALAR_FIELD(returning.typmod);
2253+
COPY_SCALAR_FIELD(unique);
2254+
COPY_SCALAR_FIELD(absent_on_null);
2255+
2256+
return newnode;
2257+
}
2258+
2259+
/*
2260+
* _copyJsonOutput
2261+
*/
2262+
static JsonOutput *
2263+
_copyJsonOutput(const JsonOutput *from)
2264+
{
2265+
JsonOutput *newnode = makeNode(JsonOutput);
2266+
2267+
COPY_NODE_FIELD(typename);
2268+
COPY_SCALAR_FIELD(returning);
2269+
2270+
return newnode;
2271+
}
2272+
21952273
/* ****************************************************************
21962274
* relation.h copy functions
21972275
*
@@ -5079,6 +5157,21 @@ copyObjectImpl(const void *from)
50795157
case T_OnConflictExpr:
50805158
retval = _copyOnConflictExpr(from);
50815159
break;
5160+
case T_JsonValueExpr:
5161+
retval = _copyJsonValueExpr(from);
5162+
break;
5163+
case T_JsonKeyValue:
5164+
retval = _copyJsonKeyValue(from);
5165+
break;
5166+
case T_JsonCtorOpts:
5167+
retval = _copyJsonCtorOpts(from);
5168+
break;
5169+
case T_JsonObjectCtor:
5170+
retval = _copyJsonObjectCtor(from);
5171+
break;
5172+
case T_JsonOutput:
5173+
retval = _copyJsonOutput(from);
5174+
break;
50825175

50835176
/*
50845177
* 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
*/
@@ -3166,6 +3191,12 @@ equal(const void *a, const void *b)
31663191
case T_JoinExpr:
31673192
retval = _equalJoinExpr(a, b);
31683193
break;
3194+
case T_JsonValueExpr:
3195+
retval = _equalJsonValueExpr(a, b);
3196+
break;
3197+
case T_JsonCtorOpts:
3198+
retval = _equalJsonCtorOpts(a, b);
3199+
break;
31693200

31703201
/*
31713202
* 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;
@@ -2229,6 +2244,8 @@ expression_tree_walker(Node *node,
22292244
return true;
22302245
}
22312246
break;
2247+
case T_JsonValueExpr:
2248+
return walker(((JsonValueExpr *) node)->expr, context);
22322249
default:
22332250
elog(ERROR, "unrecognized node type: %d",
22342251
(int) nodeTag(node));
@@ -3060,6 +3077,16 @@ expression_tree_mutator(Node *node,
30603077
return (Node *) newnode;
30613078
}
30623079
break;
3080+
case T_JsonValueExpr:
3081+
{
3082+
JsonValueExpr *jve = (JsonValueExpr *) node;
3083+
JsonValueExpr *newnode;
3084+
3085+
FLATCOPY(newnode, jve, JsonValueExpr);
3086+
MUTATE(newnode->expr, jve->expr, Expr *);
3087+
3088+
return (Node *) newnode;
3089+
}
30633090
default:
30643091
elog(ERROR, "unrecognized node type: %d",
30653092
(int) nodeTag(node));
@@ -3704,6 +3731,30 @@ raw_expression_tree_walker(Node *node,
37043731
break;
37053732
case T_CommonTableExpr:
37063733
return walker(((CommonTableExpr *) node)->ctequery, context);
3734+
case T_JsonValueExpr:
3735+
return walker(((JsonValueExpr *) node)->expr, context);
3736+
case T_JsonOutput:
3737+
return walker(((JsonOutput *) node)->typename, context);
3738+
case T_JsonKeyValue:
3739+
{
3740+
JsonKeyValue *jkv = (JsonKeyValue *) node;
3741+
3742+
if (walker(jkv->key, context))
3743+
return true;
3744+
if (walker(jkv->value, context))
3745+
return true;
3746+
}
3747+
break;
3748+
case T_JsonObjectCtor:
3749+
{
3750+
JsonObjectCtor *joc = (JsonObjectCtor *) node;
3751+
3752+
if (walker(joc->output, context))
3753+
return true;
3754+
if (walker(joc->exprs, context))
3755+
return true;
3756+
}
3757+
break;
37073758
default:
37083759
elog(ERROR, "unrecognized node type: %d",
37093760
(int) nodeTag(node));

src/backend/nodes/outfuncs.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,31 @@ _outOnConflictExpr(StringInfo str, const OnConflictExpr *node)
17671767
WRITE_NODE_FIELD(exclRelTlist);
17681768
}
17691769

1770+
static void
1771+
_outJsonValueExpr(StringInfo str, const JsonValueExpr *node)
1772+
{
1773+
WRITE_NODE_TYPE("JSONVALUEEXPR");
1774+
1775+
WRITE_NODE_FIELD(expr);
1776+
WRITE_ENUM_FIELD(format.type, JsonFormatType);
1777+
WRITE_ENUM_FIELD(format.encoding, JsonEncoding);
1778+
WRITE_LOCATION_FIELD(format.location);
1779+
}
1780+
1781+
static void
1782+
_outJsonCtorOpts(StringInfo str, const JsonCtorOpts *node)
1783+
{
1784+
WRITE_NODE_TYPE("JSONCTOROPTS");
1785+
1786+
WRITE_ENUM_FIELD(returning.format.type, JsonFormatType);
1787+
WRITE_ENUM_FIELD(returning.format.encoding, JsonEncoding);
1788+
WRITE_LOCATION_FIELD(returning.format.location);
1789+
WRITE_OID_FIELD(returning.typid);
1790+
WRITE_INT_FIELD(returning.typmod);
1791+
WRITE_BOOL_FIELD(unique);
1792+
WRITE_BOOL_FIELD(absent_on_null);
1793+
}
1794+
17701795
/*****************************************************************************
17711796
*
17721797
* Stuff from relation.h.
@@ -4322,6 +4347,12 @@ outNode(StringInfo str, const void *obj)
43224347
case T_PartitionRangeDatum:
43234348
_outPartitionRangeDatum(str, obj);
43244349
break;
4350+
case T_JsonValueExpr:
4351+
_outJsonValueExpr(str, obj);
4352+
break;
4353+
case T_JsonCtorOpts:
4354+
_outJsonCtorOpts(str, obj);
4355+
break;
43254356

43264357
default:
43274358

src/backend/nodes/readfuncs.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,41 @@ _readOnConflictExpr(void)
13341334
READ_DONE();
13351335
}
13361336

1337+
/*
1338+
* _readJsonValueExpr
1339+
*/
1340+
static JsonValueExpr *
1341+
_readJsonValueExpr(void)
1342+
{
1343+
READ_LOCALS(JsonValueExpr);
1344+
1345+
READ_NODE_FIELD(expr);
1346+
READ_ENUM_FIELD(format.type, JsonFormatType);
1347+
READ_ENUM_FIELD(format.encoding, JsonEncoding);
1348+
READ_LOCATION_FIELD(format.location);
1349+
1350+
READ_DONE();
1351+
}
1352+
1353+
/*
1354+
* _readJsonCtorOpts
1355+
*/
1356+
static JsonCtorOpts *
1357+
_readJsonCtorOpts(void)
1358+
{
1359+
READ_LOCALS(JsonCtorOpts);
1360+
1361+
READ_ENUM_FIELD(returning.format.type, JsonFormatType);
1362+
READ_ENUM_FIELD(returning.format.encoding, JsonEncoding);
1363+
READ_LOCATION_FIELD(returning.format.location);
1364+
READ_OID_FIELD(returning.typid);
1365+
READ_INT_FIELD(returning.typmod);
1366+
READ_BOOL_FIELD(unique);
1367+
READ_BOOL_FIELD(absent_on_null);
1368+
1369+
READ_DONE();
1370+
}
1371+
13371372
/*
13381373
* Stuff from parsenodes.h.
13391374
*/
@@ -2747,6 +2782,10 @@ parseNodeString(void)
27472782
return_value = _readPartitionBoundSpec();
27482783
else if (MATCH("PARTITIONRANGEDATUM", 19))
27492784
return_value = _readPartitionRangeDatum();
2785+
else if (MATCH("JSONVALUEEXPR", 13))
2786+
return_value = _readJsonValueExpr();
2787+
else if (MATCH("JSONCTOROPTS", 12))
2788+
return_value = _readJsonCtorOpts();
27502789
else
27512790
{
27522791
elog(ERROR, "badly formatted node string \"%.32s\"...", token);

0 commit comments

Comments
 (0)