mirror of
https://github.com/postgres/postgres.git
synced 2025-06-27 23:21:58 +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:
@ -2450,6 +2450,69 @@ ExecInitExprRec(Expr *node, ExprState *state,
|
||||
break;
|
||||
}
|
||||
|
||||
case T_JsonConstructorExpr:
|
||||
{
|
||||
JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
|
||||
List *args = ctor->args;
|
||||
ListCell *lc;
|
||||
int nargs = list_length(args);
|
||||
int argno = 0;
|
||||
|
||||
if (ctor->func)
|
||||
{
|
||||
ExecInitExprRec(ctor->func, state, resv, resnull);
|
||||
}
|
||||
else
|
||||
{
|
||||
scratch.opcode = EEOP_JSON_CONSTRUCTOR;
|
||||
scratch.d.json_constructor.constructor = ctor;
|
||||
scratch.d.json_constructor.arg_values = palloc(sizeof(Datum) * nargs);
|
||||
scratch.d.json_constructor.arg_nulls = palloc(sizeof(bool) * nargs);
|
||||
scratch.d.json_constructor.arg_types = palloc(sizeof(Oid) * nargs);
|
||||
scratch.d.json_constructor.nargs = nargs;
|
||||
|
||||
foreach(lc, args)
|
||||
{
|
||||
Expr *arg = (Expr *) lfirst(lc);
|
||||
|
||||
scratch.d.json_constructor.arg_types[argno] = exprType((Node *) arg);
|
||||
|
||||
if (IsA(arg, Const))
|
||||
{
|
||||
/* Don't evaluate const arguments every round */
|
||||
Const *con = (Const *) arg;
|
||||
|
||||
scratch.d.json_constructor.arg_values[argno] = con->constvalue;
|
||||
scratch.d.json_constructor.arg_nulls[argno] = con->constisnull;
|
||||
}
|
||||
else
|
||||
{
|
||||
ExecInitExprRec(arg, state,
|
||||
&scratch.d.json_constructor.arg_values[argno],
|
||||
&scratch.d.json_constructor.arg_nulls[argno]);
|
||||
}
|
||||
argno++;
|
||||
}
|
||||
|
||||
ExprEvalPushStep(state, &scratch);
|
||||
}
|
||||
|
||||
if (ctor->coercion)
|
||||
{
|
||||
Datum *innermost_caseval = state->innermost_caseval;
|
||||
bool *innermost_isnull = state->innermost_casenull;
|
||||
|
||||
state->innermost_caseval = resv;
|
||||
state->innermost_casenull = resnull;
|
||||
|
||||
ExecInitExprRec(ctor->coercion, state, resv, resnull);
|
||||
|
||||
state->innermost_caseval = innermost_caseval;
|
||||
state->innermost_casenull = innermost_isnull;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d",
|
||||
(int) nodeTag(node));
|
||||
|
Reference in New Issue
Block a user