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

Commit 3b188da

Browse files
author
Nikita Glukhov
committed
Add JSON_OBJECT() transformation
1 parent 042db01 commit 3b188da

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
@@ -2836,6 +2836,27 @@ JumbleExpr(pgssJumbleState *jstate, Node *node)
28362836
JumbleExpr(jstate, (Node *) conf->exclRelTlist);
28372837
}
28382838
break;
2839+
case T_JsonValueExpr:
2840+
{
2841+
JsonValueExpr *expr = (JsonValueExpr *) node;
2842+
2843+
JumbleExpr(jstate, (Node *) expr->expr);
2844+
APP_JUMB(expr->format.type);
2845+
APP_JUMB(expr->format.encoding);
2846+
}
2847+
break;
2848+
case T_JsonCtorOpts:
2849+
{
2850+
JsonCtorOpts *opts = (JsonCtorOpts *) node;
2851+
2852+
APP_JUMB(opts->returning.format.type);
2853+
APP_JUMB(opts->returning.format.encoding);
2854+
APP_JUMB(opts->returning.typid);
2855+
APP_JUMB(opts->returning.typmod);
2856+
APP_JUMB(opts->unique);
2857+
APP_JUMB(opts->absent_on_null);
2858+
}
2859+
break;
28392860
case T_List:
28402861
foreach(temp, (List *) node)
28412862
{

src/backend/executor/execExpr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,6 +2026,11 @@ ExecInitExprRec(Expr *node, PlanState *parent, ExprState *state,
20262026
break;
20272027
}
20282028

2029+
case T_JsonValueExpr:
2030+
ExecInitExprRec(((JsonValueExpr *) node)->expr,
2031+
parent, state, resv, resnull);
2032+
break;
2033+
20292034
default:
20302035
elog(ERROR, "unrecognized node type: %d",
20312036
(int) nodeTag(node));

src/backend/nodes/copyfuncs.c

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

2132+
/*
2133+
* _copyJsonValueExpr
2134+
*/
2135+
static JsonValueExpr *
2136+
_copyJsonValueExpr(const JsonValueExpr *from)
2137+
{
2138+
JsonValueExpr *newnode = makeNode(JsonValueExpr);
2139+
2140+
COPY_NODE_FIELD(expr);
2141+
COPY_SCALAR_FIELD(format);
2142+
2143+
return newnode;
2144+
}
2145+
2146+
/*
2147+
* _copyJsonKeyValue
2148+
*/
2149+
static JsonKeyValue *
2150+
_copyJsonKeyValue(const JsonKeyValue *from)
2151+
{
2152+
JsonKeyValue *newnode = makeNode(JsonKeyValue);
2153+
2154+
COPY_NODE_FIELD(key);
2155+
COPY_NODE_FIELD(value);
2156+
2157+
return newnode;
2158+
}
2159+
2160+
/*
2161+
* _copyJsonObjectCtor
2162+
*/
2163+
static JsonObjectCtor *
2164+
_copyJsonObjectCtor(const JsonObjectCtor *from)
2165+
{
2166+
JsonObjectCtor *newnode = makeNode(JsonObjectCtor);
2167+
2168+
COPY_NODE_FIELD(exprs);
2169+
COPY_NODE_FIELD(output);
2170+
COPY_SCALAR_FIELD(absent_on_null);
2171+
COPY_SCALAR_FIELD(unique);
2172+
COPY_LOCATION_FIELD(location);
2173+
2174+
return newnode;
2175+
}
2176+
2177+
/*
2178+
* _copyJsonCtorOpts
2179+
*/
2180+
static JsonCtorOpts *
2181+
_copyJsonCtorOpts(const JsonCtorOpts *from)
2182+
{
2183+
JsonCtorOpts *newnode = makeNode(JsonCtorOpts);
2184+
2185+
COPY_SCALAR_FIELD(returning.format.type);
2186+
COPY_SCALAR_FIELD(returning.format.encoding);
2187+
COPY_LOCATION_FIELD(returning.format.location);
2188+
COPY_SCALAR_FIELD(returning.typid);
2189+
COPY_SCALAR_FIELD(returning.typmod);
2190+
COPY_SCALAR_FIELD(unique);
2191+
COPY_SCALAR_FIELD(absent_on_null);
2192+
2193+
return newnode;
2194+
}
2195+
21322196
/* ****************************************************************
21332197
* relation.h copy functions
21342198
*
@@ -4981,6 +5045,18 @@ copyObjectImpl(const void *from)
49815045
case T_OnConflictExpr:
49825046
retval = _copyOnConflictExpr(from);
49835047
break;
5048+
case T_JsonValueExpr:
5049+
retval = _copyJsonValueExpr(from);
5050+
break;
5051+
case T_JsonKeyValue:
5052+
retval = _copyJsonKeyValue(from);
5053+
break;
5054+
case T_JsonCtorOpts:
5055+
retval = _copyJsonCtorOpts(from);
5056+
break;
5057+
case T_JsonObjectCtor:
5058+
retval = _copyJsonObjectCtor(from);
5059+
break;
49845060

49855061
/*
49865062
* RELATION NODES

src/backend/nodes/equalfuncs.c

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

822+
static bool
823+
_equalJsonValueExpr(const JsonValueExpr *a, const JsonValueExpr *b)
824+
{
825+
COMPARE_NODE_FIELD(expr);
826+
COMPARE_SCALAR_FIELD(format.type);
827+
COMPARE_SCALAR_FIELD(format.encoding);
828+
COMPARE_LOCATION_FIELD(format.location);
829+
830+
return true;
831+
}
832+
833+
static bool
834+
_equalJsonCtorOpts(const JsonCtorOpts *a, const JsonCtorOpts *b)
835+
{
836+
COMPARE_SCALAR_FIELD(returning.format.type);
837+
COMPARE_SCALAR_FIELD(returning.format.encoding);
838+
COMPARE_LOCATION_FIELD(returning.format.location);
839+
COMPARE_SCALAR_FIELD(returning.typid);
840+
COMPARE_SCALAR_FIELD(returning.typmod);
841+
COMPARE_SCALAR_FIELD(absent_on_null);
842+
COMPARE_SCALAR_FIELD(unique);
843+
844+
return true;
845+
}
846+
822847
/*
823848
* Stuff from relation.h
824849
*/
@@ -3142,6 +3167,12 @@ equal(const void *a, const void *b)
31423167
case T_JoinExpr:
31433168
retval = _equalJoinExpr(a, b);
31443169
break;
3170+
case T_JsonValueExpr:
3171+
retval = _equalJsonValueExpr(a, b);
3172+
break;
3173+
case T_JsonCtorOpts:
3174+
retval = _equalJsonCtorOpts(a, b);
3175+
break;
31453176

31463177
/*
31473178
* 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;
@@ -2219,6 +2234,8 @@ expression_tree_walker(Node *node,
22192234
return true;
22202235
}
22212236
break;
2237+
case T_JsonValueExpr:
2238+
return walker(((JsonValueExpr *) node)->expr, context);
22222239
default:
22232240
elog(ERROR, "unrecognized node type: %d",
22242241
(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
@@ -1698,6 +1698,31 @@ _outOnConflictExpr(StringInfo str, const OnConflictExpr *node)
16981698
WRITE_NODE_FIELD(exclRelTlist);
16991699
}
17001700

1701+
static void
1702+
_outJsonValueExpr(StringInfo str, const JsonValueExpr *node)
1703+
{
1704+
WRITE_NODE_TYPE("JSONVALUEEXPR");
1705+
1706+
WRITE_NODE_FIELD(expr);
1707+
WRITE_ENUM_FIELD(format.type, JsonFormatType);
1708+
WRITE_ENUM_FIELD(format.encoding, JsonEncoding);
1709+
WRITE_LOCATION_FIELD(format.location);
1710+
}
1711+
1712+
static void
1713+
_outJsonCtorOpts(StringInfo str, const JsonCtorOpts *node)
1714+
{
1715+
WRITE_NODE_TYPE("JSONCTOROPTS");
1716+
1717+
WRITE_ENUM_FIELD(returning.format.type, JsonFormatType);
1718+
WRITE_ENUM_FIELD(returning.format.encoding, JsonEncoding);
1719+
WRITE_LOCATION_FIELD(returning.format.location);
1720+
WRITE_OID_FIELD(returning.typid);
1721+
WRITE_INT_FIELD(returning.typmod);
1722+
WRITE_BOOL_FIELD(unique);
1723+
WRITE_BOOL_FIELD(absent_on_null);
1724+
}
1725+
17011726
/*****************************************************************************
17021727
*
17031728
* Stuff from relation.h.
@@ -4237,6 +4262,12 @@ outNode(StringInfo str, const void *obj)
42374262
case T_PartitionRangeDatum:
42384263
_outPartitionRangeDatum(str, obj);
42394264
break;
4265+
case T_JsonValueExpr:
4266+
_outJsonValueExpr(str, obj);
4267+
break;
4268+
case T_JsonCtorOpts:
4269+
_outJsonCtorOpts(str, obj);
4270+
break;
42404271

42414272
default:
42424273

src/backend/nodes/readfuncs.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,41 @@ _readOnConflictExpr(void)
13231323
READ_DONE();
13241324
}
13251325

1326+
/*
1327+
* _readJsonValueExpr
1328+
*/
1329+
static JsonValueExpr *
1330+
_readJsonValueExpr(void)
1331+
{
1332+
READ_LOCALS(JsonValueExpr);
1333+
1334+
READ_NODE_FIELD(expr);
1335+
READ_ENUM_FIELD(format.type, JsonFormatType);
1336+
READ_ENUM_FIELD(format.encoding, JsonEncoding);
1337+
READ_LOCATION_FIELD(format.location);
1338+
1339+
READ_DONE();
1340+
}
1341+
1342+
/*
1343+
* _readJsonCtorOpts
1344+
*/
1345+
static JsonCtorOpts *
1346+
_readJsonCtorOpts(void)
1347+
{
1348+
READ_LOCALS(JsonCtorOpts);
1349+
1350+
READ_ENUM_FIELD(returning.format.type, JsonFormatType);
1351+
READ_ENUM_FIELD(returning.format.encoding, JsonEncoding);
1352+
READ_LOCATION_FIELD(returning.format.location);
1353+
READ_OID_FIELD(returning.typid);
1354+
READ_INT_FIELD(returning.typmod);
1355+
READ_BOOL_FIELD(unique);
1356+
READ_BOOL_FIELD(absent_on_null);
1357+
1358+
READ_DONE();
1359+
}
1360+
13261361
/*
13271362
* Stuff from parsenodes.h.
13281363
*/
@@ -2665,6 +2700,10 @@ parseNodeString(void)
26652700
return_value = _readPartitionBoundSpec();
26662701
else if (MATCH("PARTITIONRANGEDATUM", 19))
26672702
return_value = _readPartitionRangeDatum();
2703+
else if (MATCH("JSONVALUEEXPR", 13))
2704+
return_value = _readJsonValueExpr();
2705+
else if (MATCH("JSONCTOROPTS", 12))
2706+
return_value = _readJsonCtorOpts();
26682707
else
26692708
{
26702709
elog(ERROR, "badly formatted node string \"%.32s\"...", token);

0 commit comments

Comments
 (0)