1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-09 22:41:56 +03:00

SQL/JSON constructors

This patch introduces the SQL/JSON standard constructors for JSON:

JSON()
JSON_ARRAY()
JSON_ARRAYAGG()
JSON_OBJECT()
JSON_OBJECTAGG()

For the most part these functions provide facilities that mimic
existing json/jsonb functions. However, they also offer some useful
additional functionality. In addition to text input, the JSON() function
accepts bytea input, which it will decode and constuct a json value from.
The other functions provide useful options for handling duplicate keys
and null values.

This series of patches will be followed by a consolidated documentation
patch.

Nikita Glukhov

Reviewers have included (in no particular order) Andres Freund, Alexander
Korotkov, Pavel Stehule, Andrew Alsup, Erik Rijkers, Zihong Yu,
Himanshu Upadhyaya, Daniel Gustafsson, Justin Pryzby.

Discussion: https://postgr.es/m/cd0bb935-0158-78a7-08b5-904886deac4b@postgrespro.ru
This commit is contained in:
Andrew Dunstan
2022-03-03 13:02:10 -05:00
parent f79b803dcc
commit f4fb45d15c
37 changed files with 3615 additions and 130 deletions

View File

@ -2344,6 +2344,152 @@ _copyJsonValueExpr(const JsonValueExpr *from)
return newnode;
}
/*
* _copyJsonConstructorExpr
*/
static JsonConstructorExpr *
_copyJsonConstructorExpr(const JsonConstructorExpr *from)
{
JsonConstructorExpr *newnode = makeNode(JsonConstructorExpr);
COPY_SCALAR_FIELD(type);
COPY_NODE_FIELD(args);
COPY_NODE_FIELD(func);
COPY_NODE_FIELD(coercion);
COPY_NODE_FIELD(returning);
COPY_SCALAR_FIELD(absent_on_null);
COPY_SCALAR_FIELD(unique);
COPY_LOCATION_FIELD(location);
return newnode;
}
/*
* _copyJsonKeyValue
*/
static JsonKeyValue *
_copyJsonKeyValue(const JsonKeyValue *from)
{
JsonKeyValue *newnode = makeNode(JsonKeyValue);
COPY_NODE_FIELD(key);
COPY_NODE_FIELD(value);
return newnode;
}
/*
* _copyJsonObjectConstructor
*/
static JsonObjectConstructor *
_copyJsonObjectConstructor(const JsonObjectConstructor *from)
{
JsonObjectConstructor *newnode = makeNode(JsonObjectConstructor);
COPY_NODE_FIELD(exprs);
COPY_NODE_FIELD(output);
COPY_SCALAR_FIELD(absent_on_null);
COPY_SCALAR_FIELD(unique);
COPY_LOCATION_FIELD(location);
return newnode;
}
/*
* _copyJsonAggConstructor
*/
static JsonAggConstructor *
_copyJsonAggConstructor(const JsonAggConstructor *from)
{
JsonAggConstructor *newnode = makeNode(JsonAggConstructor);
COPY_NODE_FIELD(output);
COPY_NODE_FIELD(agg_filter);
COPY_NODE_FIELD(agg_order);
COPY_NODE_FIELD(over);
COPY_LOCATION_FIELD(location);
return newnode;
}
/*
* _copyJsonObjectAgg
*/
static JsonObjectAgg *
_copyJsonObjectAgg(const JsonObjectAgg *from)
{
JsonObjectAgg *newnode = makeNode(JsonObjectAgg);
COPY_NODE_FIELD(constructor);
COPY_NODE_FIELD(arg);
COPY_SCALAR_FIELD(absent_on_null);
COPY_SCALAR_FIELD(unique);
return newnode;
}
/*
* _copyJsonOutput
*/
static JsonOutput *
_copyJsonOutput(const JsonOutput *from)
{
JsonOutput *newnode = makeNode(JsonOutput);
COPY_NODE_FIELD(typeName);
COPY_NODE_FIELD(returning);
return newnode;
}
/*
* _copyJsonArrayConstructor
*/
static JsonArrayConstructor *
_copyJsonArrayConstructor(const JsonArrayConstructor *from)
{
JsonArrayConstructor *newnode = makeNode(JsonArrayConstructor);
COPY_NODE_FIELD(exprs);
COPY_NODE_FIELD(output);
COPY_SCALAR_FIELD(absent_on_null);
COPY_LOCATION_FIELD(location);
return newnode;
}
/*
* _copyJsonArrayAgg
*/
static JsonArrayAgg *
_copyJsonArrayAgg(const JsonArrayAgg *from)
{
JsonArrayAgg *newnode = makeNode(JsonArrayAgg);
COPY_NODE_FIELD(constructor);
COPY_NODE_FIELD(arg);
COPY_SCALAR_FIELD(absent_on_null);
return newnode;
}
/*
* _copyJsonArrayQueryConstructor
*/
static JsonArrayQueryConstructor *
_copyJsonArrayQueryConstructor(const JsonArrayQueryConstructor *from)
{
JsonArrayQueryConstructor *newnode = makeNode(JsonArrayQueryConstructor);
COPY_NODE_FIELD(query);
COPY_NODE_FIELD(output);
COPY_NODE_FIELD(format);
COPY_SCALAR_FIELD(absent_on_null);
COPY_LOCATION_FIELD(location);
return newnode;
}
/* ****************************************************************
* pathnodes.h copy functions
*
@ -5406,6 +5552,33 @@ copyObjectImpl(const void *from)
case T_JsonValueExpr:
retval = _copyJsonValueExpr(from);
break;
case T_JsonKeyValue:
retval = _copyJsonKeyValue(from);
break;
case T_JsonConstructorExpr:
retval = _copyJsonConstructorExpr(from);
break;
case T_JsonObjectConstructor:
retval = _copyJsonObjectConstructor(from);
break;
case T_JsonAggConstructor:
retval = _copyJsonAggConstructor(from);
break;
case T_JsonObjectAgg:
retval = _copyJsonObjectAgg(from);
break;
case T_JsonOutput:
retval = _copyJsonOutput(from);
break;
case T_JsonArrayConstructor:
retval = _copyJsonArrayConstructor(from);
break;
case T_JsonArrayQueryConstructor:
retval = _copyJsonArrayQueryConstructor(from);
break;
case T_JsonArrayAgg:
retval = _copyJsonArrayAgg(from);
break;
/*
* RELATION NODES