mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Add more SQL/JSON constructor functions
This Patch introduces three SQL standard JSON functions: JSON() JSON_SCALAR() JSON_SERIALIZE() JSON() produces json values from text, bytea, json or jsonb values, and has facilitites for handling duplicate keys. JSON_SCALAR() produces a json value from any scalar sql value, including json and jsonb. JSON_SERIALIZE() produces text or bytea from input which containis or represents json or jsonb; For the most part these functions don't add any significant new capabilities, but they will be of use to users wanting standard compliant JSON handling. Catversion bumped as this changes ruleutils.c. Author: Nikita Glukhov <n.gluhov@postgrespro.ru> Author: Teodor Sigaev <teodor@sigaev.ru> Author: Oleg Bartunov <obartunov@gmail.com> Author: Alexander Korotkov <aekorotkov@gmail.com> Author: Andrew Dunstan <andrew@dunslane.net> Author: Amit Langote <amitlangote09@gmail.com> 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, Álvaro Herrera, Peter Eisentraut Discussion: https://postgr.es/m/cd0bb935-0158-78a7-08b5-904886deac4b@postgrespro.ru Discussion: https://postgr.es/m/20220616233130.rparivafipt6doj3@alap3.anarazel.de Discussion: https://postgr.es/m/abd9b83b-aa66-f230-3d6d-734817f0995d%40postgresql.org Discussion: https://postgr.es/m/CA+HiwqE4XTdfb1nW=Ojoy_tQSRhYt-q_kb6i5d4xcKyrLC1Nbg@mail.gmail.com
This commit is contained in:
@ -48,6 +48,7 @@
|
||||
#include "utils/array.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/datum.h"
|
||||
#include "utils/jsonfuncs.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/typcache.h"
|
||||
|
||||
@ -2311,6 +2312,12 @@ ExecInitExprRec(Expr *node, ExprState *state,
|
||||
{
|
||||
ExecInitExprRec(ctor->func, state, resv, resnull);
|
||||
}
|
||||
else if ((ctor->type == JSCTOR_JSON_PARSE && !ctor->unique) ||
|
||||
ctor->type == JSCTOR_JSON_SERIALIZE)
|
||||
{
|
||||
/* Use the value of the first argument as result */
|
||||
ExecInitExprRec(linitial(args), state, resv, resnull);
|
||||
}
|
||||
else
|
||||
{
|
||||
JsonConstructorExprState *jcstate;
|
||||
@ -2349,6 +2356,29 @@ ExecInitExprRec(Expr *node, ExprState *state,
|
||||
argno++;
|
||||
}
|
||||
|
||||
/* prepare type cache for datum_to_json[b]() */
|
||||
if (ctor->type == JSCTOR_JSON_SCALAR)
|
||||
{
|
||||
bool is_jsonb =
|
||||
ctor->returning->format->format_type == JS_FORMAT_JSONB;
|
||||
|
||||
jcstate->arg_type_cache =
|
||||
palloc(sizeof(*jcstate->arg_type_cache) * nargs);
|
||||
|
||||
for (int i = 0; i < nargs; i++)
|
||||
{
|
||||
JsonTypeCategory category;
|
||||
Oid outfuncid;
|
||||
Oid typid = jcstate->arg_types[i];
|
||||
|
||||
json_categorize_type(typid, is_jsonb,
|
||||
&category, &outfuncid);
|
||||
|
||||
jcstate->arg_type_cache[i].outfuncid = outfuncid;
|
||||
jcstate->arg_type_cache[i].category = (int) category;
|
||||
}
|
||||
}
|
||||
|
||||
ExprEvalPushStep(state, &scratch);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user