1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-22 12:22:45 +03:00

Add SQL/JSON query functions

This introduces the following SQL/JSON functions for querying JSON
data using jsonpath expressions:

JSON_EXISTS(), which can be used to apply a jsonpath expression to a
JSON value to check if it yields any values.

JSON_QUERY(), which can be used to to apply a jsonpath expression to
a JSON value to get a JSON object, an array, or a string.  There are
various options to control whether multi-value result uses array
wrappers and whether the singleton scalar strings are quoted or not.

JSON_VALUE(), which can be used to apply a jsonpath expression to a
JSON value to return a single scalar value, producing an error if it
multiple values are matched.

Both JSON_VALUE() and JSON_QUERY() functions have options for
handling EMPTY and ERROR conditions, which can be used to specify
the behavior when no values are matched and when an error occurs
during jsonpath evaluation, respectively.

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>
Author: Peter Eisentraut <peter@eisentraut.org>
Author: Jian He <jian.universality@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, Jian He, Anton A. Melnikov,
Nikita Malakhov, Peter Eisentraut, Tomas Vondra

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+HiwqHROpf9e644D8BRqYvaAPmgBZVup-xKMDPk-nd4EpgzHw@mail.gmail.com
Discussion: https://postgr.es/m/CA+HiwqE4XTdfb1nW=Ojoy_tQSRhYt-q_kb6i5d4xcKyrLC1Nbg@mail.gmail.com
This commit is contained in:
Amit Langote
2024-03-21 17:06:27 +09:00
parent a145f424d5
commit 6185c9737c
34 changed files with 4815 additions and 36 deletions

View File

@@ -4583,6 +4583,50 @@ parse_datetime(text *date_txt, text *fmt, Oid collid, bool strict,
}
}
/*
* Parses the datetime format string in 'fmt_str' and returns true if it
* contains a timezone specifier, false if not.
*/
bool
datetime_format_has_tz(const char *fmt_str)
{
bool incache;
int fmt_len = strlen(fmt_str);
int result;
FormatNode *format;
if (fmt_len > DCH_CACHE_SIZE)
{
/*
* Allocate new memory if format picture is bigger than static cache
* and do not use cache (call parser always)
*/
incache = false;
format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
parse_format(format, fmt_str, DCH_keywords,
DCH_suff, DCH_index, DCH_FLAG, NULL);
}
else
{
/*
* Use cache buffers
*/
DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, false);
incache = true;
format = ent->format;
}
result = DCH_datetime_type(format);
if (!incache)
pfree(format);
return result & DCH_ZONED;
}
/*
* do_to_timestamp: shared code for to_timestamp and to_date
*

View File

@@ -2158,3 +2158,34 @@ jsonb_float8(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(retValue);
}
/*
* Convert jsonb to a C-string stripping quotes from scalar strings.
*/
char *
JsonbUnquote(Jsonb *jb)
{
if (JB_ROOT_IS_SCALAR(jb))
{
JsonbValue v;
(void) JsonbExtractScalar(&jb->root, &v);
if (v.type == jbvString)
return pnstrdup(v.val.string.val, v.val.string.len);
else if (v.type == jbvBool)
return pstrdup(v.val.boolean ? "true" : "false");
else if (v.type == jbvNumeric)
return DatumGetCString(DirectFunctionCall1(numeric_out,
PointerGetDatum(v.val.numeric)));
else if (v.type == jbvNull)
return pstrdup("null");
else
{
elog(ERROR, "unrecognized jsonb value type %d", v.type);
return NULL;
}
}
else
return JsonbToCString(NULL, &jb->root, VARSIZE(jb));
}

View File

@@ -2830,7 +2830,9 @@ populate_array_dim_jsonb(PopulateArrayContext *ctx, /* context */
check_stack_depth();
if (jbv->type != jbvBinary || !JsonContainerIsArray(jbc))
/* Even scalars can end up here thanks to ExecEvalJsonCoercion(). */
if (jbv->type != jbvBinary || !JsonContainerIsArray(jbc) ||
JsonContainerIsScalar(jbc))
{
populate_array_report_expected_array(ctx, ndim - 1);
/* Getting here means the error was reported softly. */
@@ -2838,8 +2840,6 @@ populate_array_dim_jsonb(PopulateArrayContext *ctx, /* context */
return false;
}
Assert(!JsonContainerIsScalar(jbc));
it = JsonbIteratorInit(jbc);
tok = JsonbIteratorNext(&it, &val, true);
@@ -3323,6 +3323,62 @@ prepare_column_cache(ColumnIOData *column,
ReleaseSysCache(tup);
}
/*
* Populate and return the value of specified type from a given json/jsonb
* value 'json_val'. 'cache' is caller-specified pointer to save the
* ColumnIOData that will be initialized on the 1st call and then reused
* during any subsequent calls. 'mcxt' gives the memory context to allocate
* the ColumnIOData and any other subsidiary memory in. 'escontext',
* if not NULL, tells that any errors that occur should be handled softly.
*/
Datum
json_populate_type(Datum json_val, Oid json_type,
Oid typid, int32 typmod,
void **cache, MemoryContext mcxt,
bool *isnull,
Node *escontext)
{
JsValue jsv = {0};
JsonbValue jbv;
jsv.is_json = json_type == JSONOID;
if (*isnull)
{
if (jsv.is_json)
jsv.val.json.str = NULL;
else
jsv.val.jsonb = NULL;
}
else if (jsv.is_json)
{
text *json = DatumGetTextPP(json_val);
jsv.val.json.str = VARDATA_ANY(json);
jsv.val.json.len = VARSIZE_ANY_EXHDR(json);
jsv.val.json.type = JSON_TOKEN_INVALID; /* not used in
* populate_composite() */
}
else
{
Jsonb *jsonb = DatumGetJsonbP(json_val);
jsv.val.jsonb = &jbv;
/* fill binary jsonb value pointing to jb */
jbv.type = jbvBinary;
jbv.val.binary.data = &jsonb->root;
jbv.val.binary.len = VARSIZE(jsonb) - VARHDRSZ;
}
if (*cache == NULL)
*cache = MemoryContextAllocZero(mcxt, sizeof(ColumnIOData));
return populate_record_field(*cache, typid, typmod, NULL, mcxt,
PointerGetDatum(NULL), &jsv, isnull,
escontext);
}
/* recursively populate a record field or an array element from a json/jsonb value */
static Datum
populate_record_field(ColumnIOData *col,

View File

@@ -63,11 +63,14 @@
#include "postgres.h"
#include "catalog/pg_type.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/miscnodes.h"
#include "nodes/nodeFuncs.h"
#include "utils/fmgrprotos.h"
#include "utils/formatting.h"
#include "utils/json.h"
#include "utils/jsonpath.h"
@@ -1239,3 +1242,281 @@ jspGetArraySubscript(JsonPathItem *v, JsonPathItem *from, JsonPathItem *to,
return true;
}
/* SQL/JSON datatype status: */
enum JsonPathDatatypeStatus
{
jpdsNonDateTime, /* null, bool, numeric, string, array, object */
jpdsUnknownDateTime, /* unknown datetime type */
jpdsDateTimeZoned, /* timetz, timestamptz */
jpdsDateTimeNonZoned, /* time, timestamp, date */
};
/* Context for jspIsMutableWalker() */
struct JsonPathMutableContext
{
List *varnames; /* list of variable names */
List *varexprs; /* list of variable expressions */
enum JsonPathDatatypeStatus current; /* status of @ item */
bool lax; /* jsonpath is lax or strict */
bool mutable; /* resulting mutability status */
};
static enum JsonPathDatatypeStatus jspIsMutableWalker(JsonPathItem *jpi,
struct JsonPathMutableContext *cxt);
/*
* Function to check whether jsonpath expression is mutable to be used in the
* planner function contain_mutable_functions().
*/
bool
jspIsMutable(JsonPath *path, List *varnames, List *varexprs)
{
struct JsonPathMutableContext cxt;
JsonPathItem jpi;
cxt.varnames = varnames;
cxt.varexprs = varexprs;
cxt.current = jpdsNonDateTime;
cxt.lax = (path->header & JSONPATH_LAX) != 0;
cxt.mutable = false;
jspInit(&jpi, path);
(void) jspIsMutableWalker(&jpi, &cxt);
return cxt.mutable;
}
/*
* Recursive walker for jspIsMutable()
*/
static enum JsonPathDatatypeStatus
jspIsMutableWalker(JsonPathItem *jpi, struct JsonPathMutableContext *cxt)
{
JsonPathItem next;
enum JsonPathDatatypeStatus status = jpdsNonDateTime;
while (!cxt->mutable)
{
JsonPathItem arg;
enum JsonPathDatatypeStatus leftStatus;
enum JsonPathDatatypeStatus rightStatus;
switch (jpi->type)
{
case jpiRoot:
Assert(status == jpdsNonDateTime);
break;
case jpiCurrent:
Assert(status == jpdsNonDateTime);
status = cxt->current;
break;
case jpiFilter:
{
enum JsonPathDatatypeStatus prevStatus = cxt->current;
cxt->current = status;
jspGetArg(jpi, &arg);
jspIsMutableWalker(&arg, cxt);
cxt->current = prevStatus;
break;
}
case jpiVariable:
{
int32 len;
const char *name = jspGetString(jpi, &len);
ListCell *lc1;
ListCell *lc2;
Assert(status == jpdsNonDateTime);
forboth(lc1, cxt->varnames, lc2, cxt->varexprs)
{
String *varname = lfirst_node(String, lc1);
Node *varexpr = lfirst(lc2);
if (strncmp(varname->sval, name, len))
continue;
switch (exprType(varexpr))
{
case DATEOID:
case TIMEOID:
case TIMESTAMPOID:
status = jpdsDateTimeNonZoned;
break;
case TIMETZOID:
case TIMESTAMPTZOID:
status = jpdsDateTimeZoned;
break;
default:
status = jpdsNonDateTime;
break;
}
break;
}
break;
}
case jpiEqual:
case jpiNotEqual:
case jpiLess:
case jpiGreater:
case jpiLessOrEqual:
case jpiGreaterOrEqual:
Assert(status == jpdsNonDateTime);
jspGetLeftArg(jpi, &arg);
leftStatus = jspIsMutableWalker(&arg, cxt);
jspGetRightArg(jpi, &arg);
rightStatus = jspIsMutableWalker(&arg, cxt);
/*
* Comparison of datetime type with different timezone status
* is mutable.
*/
if (leftStatus != jpdsNonDateTime &&
rightStatus != jpdsNonDateTime &&
(leftStatus == jpdsUnknownDateTime ||
rightStatus == jpdsUnknownDateTime ||
leftStatus != rightStatus))
cxt->mutable = true;
break;
case jpiNot:
case jpiIsUnknown:
case jpiExists:
case jpiPlus:
case jpiMinus:
Assert(status == jpdsNonDateTime);
jspGetArg(jpi, &arg);
jspIsMutableWalker(&arg, cxt);
break;
case jpiAnd:
case jpiOr:
case jpiAdd:
case jpiSub:
case jpiMul:
case jpiDiv:
case jpiMod:
case jpiStartsWith:
Assert(status == jpdsNonDateTime);
jspGetLeftArg(jpi, &arg);
jspIsMutableWalker(&arg, cxt);
jspGetRightArg(jpi, &arg);
jspIsMutableWalker(&arg, cxt);
break;
case jpiIndexArray:
for (int i = 0; i < jpi->content.array.nelems; i++)
{
JsonPathItem from;
JsonPathItem to;
if (jspGetArraySubscript(jpi, &from, &to, i))
jspIsMutableWalker(&to, cxt);
jspIsMutableWalker(&from, cxt);
}
/* FALLTHROUGH */
case jpiAnyArray:
if (!cxt->lax)
status = jpdsNonDateTime;
break;
case jpiAny:
if (jpi->content.anybounds.first > 0)
status = jpdsNonDateTime;
break;
case jpiDatetime:
if (jpi->content.arg)
{
char *template;
jspGetArg(jpi, &arg);
if (arg.type != jpiString)
{
status = jpdsNonDateTime;
break; /* there will be runtime error */
}
template = jspGetString(&arg, NULL);
if (datetime_format_has_tz(template))
status = jpdsDateTimeZoned;
else
status = jpdsDateTimeNonZoned;
}
else
{
status = jpdsUnknownDateTime;
}
break;
case jpiLikeRegex:
Assert(status == jpdsNonDateTime);
jspInitByBuffer(&arg, jpi->base, jpi->content.like_regex.expr);
jspIsMutableWalker(&arg, cxt);
break;
/* literals */
case jpiNull:
case jpiString:
case jpiNumeric:
case jpiBool:
break;
/* accessors */
case jpiKey:
case jpiAnyKey:
/* special items */
case jpiSubscript:
case jpiLast:
/* item methods */
case jpiType:
case jpiSize:
case jpiAbs:
case jpiFloor:
case jpiCeiling:
case jpiDouble:
case jpiKeyValue:
case jpiBigint:
case jpiBoolean:
case jpiDecimal:
case jpiInteger:
case jpiNumber:
case jpiStringFunc:
status = jpdsNonDateTime;
break;
case jpiTime:
case jpiDate:
case jpiTimestamp:
status = jpdsDateTimeNonZoned;
cxt->mutable = true;
break;
case jpiTimeTz:
case jpiTimestampTz:
status = jpdsDateTimeNonZoned;
cxt->mutable = true;
break;
}
if (!jspGetNext(jpi, &next))
break;
jpi = &next;
}
return status;
}

View File

@@ -229,6 +229,12 @@ static JsonPathExecResult appendBoolResult(JsonPathExecContext *cxt,
JsonPathItem *jsp, JsonValueList *found, JsonPathBool res);
static void getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item,
JsonbValue *value);
static JsonbValue *GetJsonPathVar(void *cxt, char *varName, int varNameLen,
JsonbValue *baseObject, int *baseObjectId);
static int CountJsonPathVars(void *cxt);
static void JsonItemFromDatum(Datum val, Oid typid, int32 typmod,
JsonbValue *res);
static void JsonbValueInitNumericDatum(JsonbValue *jbv, Datum num);
static void getJsonPathVariable(JsonPathExecContext *cxt,
JsonPathItem *variable, JsonbValue *value);
static int countVariablesFromJsonb(void *varsJsonb);
@@ -2860,6 +2866,155 @@ getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item,
}
}
/*
* Returns the computed value of a JSON path variable with given name.
*/
static JsonbValue *
GetJsonPathVar(void *cxt, char *varName, int varNameLen,
JsonbValue *baseObject, int *baseObjectId)
{
JsonPathVariable *var = NULL;
List *vars = cxt;
ListCell *lc;
JsonbValue *result;
int id = 1;
foreach(lc, vars)
{
JsonPathVariable *curvar = lfirst(lc);
if (!strncmp(curvar->name, varName, varNameLen))
{
var = curvar;
break;
}
id++;
}
if (var == NULL)
{
*baseObjectId = -1;
return NULL;
}
result = palloc(sizeof(JsonbValue));
if (var->isnull)
{
*baseObjectId = 0;
result->type = jbvNull;
}
else
JsonItemFromDatum(var->value, var->typid, var->typmod, result);
*baseObject = *result;
*baseObjectId = id;
return result;
}
static int
CountJsonPathVars(void *cxt)
{
List *vars = (List *) cxt;
return list_length(vars);
}
/*
* Initialize JsonbValue to pass to jsonpath executor from given
* datum value of the specified type.
*/
static void
JsonItemFromDatum(Datum val, Oid typid, int32 typmod, JsonbValue *res)
{
switch (typid)
{
case BOOLOID:
res->type = jbvBool;
res->val.boolean = DatumGetBool(val);
break;
case NUMERICOID:
JsonbValueInitNumericDatum(res, val);
break;
case INT2OID:
JsonbValueInitNumericDatum(res, DirectFunctionCall1(int2_numeric, val));
break;
case INT4OID:
JsonbValueInitNumericDatum(res, DirectFunctionCall1(int4_numeric, val));
break;
case INT8OID:
JsonbValueInitNumericDatum(res, DirectFunctionCall1(int8_numeric, val));
break;
case FLOAT4OID:
JsonbValueInitNumericDatum(res, DirectFunctionCall1(float4_numeric, val));
break;
case FLOAT8OID:
JsonbValueInitNumericDatum(res, DirectFunctionCall1(float8_numeric, val));
break;
case TEXTOID:
case VARCHAROID:
res->type = jbvString;
res->val.string.val = VARDATA_ANY(val);
res->val.string.len = VARSIZE_ANY_EXHDR(val);
break;
case DATEOID:
case TIMEOID:
case TIMETZOID:
case TIMESTAMPOID:
case TIMESTAMPTZOID:
res->type = jbvDatetime;
res->val.datetime.value = val;
res->val.datetime.typid = typid;
res->val.datetime.typmod = typmod;
res->val.datetime.tz = 0;
break;
case JSONBOID:
{
JsonbValue *jbv = res;
Jsonb *jb = DatumGetJsonbP(val);
if (JsonContainerIsScalar(&jb->root))
{
bool result PG_USED_FOR_ASSERTS_ONLY;
result = JsonbExtractScalar(&jb->root, jbv);
Assert(result);
}
else
JsonbInitBinary(jbv, jb);
break;
}
case JSONOID:
{
text *txt = DatumGetTextP(val);
char *str = text_to_cstring(txt);
Jsonb *jb;
jb = DatumGetJsonbP(DirectFunctionCall1(jsonb_in,
CStringGetDatum(str)));
pfree(str);
JsonItemFromDatum(JsonbPGetDatum(jb), JSONBOID, -1, res);
break;
}
default:
ereport(ERROR,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not convert value of type %s to jsonpath",
format_type_be(typid)));
}
}
/* Initialize numeric value from the given datum */
static void
JsonbValueInitNumericDatum(JsonbValue *jbv, Datum num)
{
jbv->type = jbvNumeric;
jbv->val.numeric = DatumGetNumeric(num);
}
/*
* Get the value of variable passed to jsonpath executor
*/
@@ -3596,3 +3751,170 @@ compareDatetime(Datum val1, Oid typid1, Datum val2, Oid typid2,
return DatumGetInt32(DirectFunctionCall2(cmpfunc, val1, val2));
}
/*
* Executor-callable JSON_EXISTS implementation
*
* Returns NULL instead of throwing errors if 'error' is not NULL, setting
* *error to true.
*/
bool
JsonPathExists(Datum jb, JsonPath *jp, bool *error, List *vars)
{
JsonPathExecResult res;
res = executeJsonPath(jp, vars,
GetJsonPathVar, CountJsonPathVars,
DatumGetJsonbP(jb), !error, NULL, true);
Assert(error || !jperIsError(res));
if (error && jperIsError(res))
*error = true;
return res == jperOk;
}
/*
* Executor-callable JSON_QUERY implementation
*
* Returns NULL instead of throwing errors if 'error' is not NULL, setting
* *error to true. *empty is set to true if no match is found.
*/
Datum
JsonPathQuery(Datum jb, JsonPath *jp, JsonWrapper wrapper, bool *empty,
bool *error, List *vars)
{
JsonbValue *singleton;
bool wrap;
JsonValueList found = {0};
JsonPathExecResult res;
int count;
res = executeJsonPath(jp, vars,
GetJsonPathVar, CountJsonPathVars,
DatumGetJsonbP(jb), !error, &found, true);
Assert(error || !jperIsError(res));
if (error && jperIsError(res))
{
*error = true;
*empty = false;
return (Datum) 0;
}
/* WRAP or not? */
count = JsonValueListLength(&found);
singleton = count > 0 ? JsonValueListHead(&found) : NULL;
if (singleton == NULL)
wrap = false;
else if (wrapper == JSW_NONE || wrapper == JSW_UNSPEC)
wrap = false;
else if (wrapper == JSW_UNCONDITIONAL)
wrap = true;
else if (wrapper == JSW_CONDITIONAL)
wrap = count > 1 ||
IsAJsonbScalar(singleton) ||
(singleton->type == jbvBinary &&
JsonContainerIsScalar(singleton->val.binary.data));
else
{
elog(ERROR, "unrecognized json wrapper %d", wrapper);
wrap = false;
}
if (wrap)
return JsonbPGetDatum(JsonbValueToJsonb(wrapItemsInArray(&found)));
/* No wrapping means only one item is expected. */
if (count > 1)
{
if (error)
{
*error = true;
return (Datum) 0;
}
ereport(ERROR,
(errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM),
errmsg("JSON path expression in JSON_QUERY should return singleton item without wrapper"),
errhint("Use WITH WRAPPER clause to wrap SQL/JSON item sequence into array.")));
}
if (singleton)
return JsonbPGetDatum(JsonbValueToJsonb(singleton));
*empty = true;
return PointerGetDatum(NULL);
}
/*
* Executor-callable JSON_VALUE implementation
*
* Returns NULL instead of throwing errors if 'error' is not NULL, setting
* *error to true. *empty is set to true if no match is found.
*/
JsonbValue *
JsonPathValue(Datum jb, JsonPath *jp, bool *empty, bool *error, List *vars)
{
JsonbValue *res;
JsonValueList found = {0};
JsonPathExecResult jper PG_USED_FOR_ASSERTS_ONLY;
int count;
jper = executeJsonPath(jp, vars, GetJsonPathVar, CountJsonPathVars,
DatumGetJsonbP(jb),
!error, &found, true);
Assert(error || !jperIsError(jper));
if (error && jperIsError(jper))
{
*error = true;
*empty = false;
return NULL;
}
count = JsonValueListLength(&found);
*empty = (count == 0);
if (*empty)
return NULL;
/* JSON_VALUE expects to get only singletons. */
if (count > 1)
{
if (error)
{
*error = true;
return NULL;
}
ereport(ERROR,
(errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM),
errmsg("JSON path expression in JSON_VALUE should return singleton scalar item")));
}
res = JsonValueListHead(&found);
if (res->type == jbvBinary && JsonContainerIsScalar(res->val.binary.data))
JsonbExtractScalar(res->val.binary.data, res);
/* JSON_VALUE expects to get only scalars. */
if (!IsAJsonbScalar(res))
{
if (error)
{
*error = true;
return NULL;
}
ereport(ERROR,
(errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED),
errmsg("JSON path expression in JSON_VALUE should return singleton scalar item")));
}
if (res->type == jbvNull)
return NULL;
return res;
}

View File

@@ -478,6 +478,8 @@ static void get_const_expr(Const *constval, deparse_context *context,
int showtype);
static void get_const_collation(Const *constval, deparse_context *context);
static void get_json_format(JsonFormat *format, StringInfo buf);
static void get_json_returning(JsonReturning *returning, StringInfo buf,
bool json_format_by_default);
static void get_json_constructor(JsonConstructorExpr *ctor,
deparse_context *context, bool showimplicit);
static void get_json_constructor_options(JsonConstructorExpr *ctor,
@@ -520,6 +522,8 @@ static char *generate_qualified_type_name(Oid typid);
static text *string_to_text(char *str);
static char *flatten_reloptions(Oid relid);
static void get_reloptions(StringInfo buf, Datum reloptions);
static void get_json_path_spec(Node *path_spec, deparse_context *context,
bool showimplicit);
#define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
@@ -8466,6 +8470,7 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
case T_MergeSupportFunc:
case T_FuncExpr:
case T_JsonConstructorExpr:
case T_JsonExpr:
/* function-like: name(..) or name[..] */
return true;
@@ -8637,6 +8642,7 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
case T_GroupingFunc: /* own parentheses */
case T_WindowFunc: /* own parentheses */
case T_CaseExpr: /* other separators */
case T_JsonExpr: /* own parentheses */
return true;
default:
return false;
@@ -8752,6 +8758,64 @@ get_rule_expr_paren(Node *node, deparse_context *context,
appendStringInfoChar(context->buf, ')');
}
static void
get_json_behavior(JsonBehavior *behavior, deparse_context *context,
const char *on)
{
/*
* The order of array elements must correspond to the order of
* JsonBehaviorType members.
*/
const char *behavior_names[] =
{
" NULL",
" ERROR",
" EMPTY",
" TRUE",
" FALSE",
" UNKNOWN",
" EMPTY ARRAY",
" EMPTY OBJECT",
" DEFAULT "
};
if ((int) behavior->btype < 0 || behavior->btype >= lengthof(behavior_names))
elog(ERROR, "invalid json behavior type: %d", behavior->btype);
appendStringInfoString(context->buf, behavior_names[behavior->btype]);
if (behavior->btype == JSON_BEHAVIOR_DEFAULT)
get_rule_expr(behavior->expr, context, false);
appendStringInfo(context->buf, " ON %s", on);
}
/*
* get_json_expr_options
*
* Parse back common options for JSON_QUERY, JSON_VALUE, JSON_EXISTS.
*/
static void
get_json_expr_options(JsonExpr *jsexpr, deparse_context *context,
JsonBehaviorType default_behavior)
{
if (jsexpr->op == JSON_QUERY_OP)
{
if (jsexpr->wrapper == JSW_CONDITIONAL)
appendStringInfo(context->buf, " WITH CONDITIONAL WRAPPER");
else if (jsexpr->wrapper == JSW_UNCONDITIONAL)
appendStringInfo(context->buf, " WITH UNCONDITIONAL WRAPPER");
if (jsexpr->omit_quotes)
appendStringInfo(context->buf, " OMIT QUOTES");
}
if (jsexpr->on_empty && jsexpr->on_empty->btype != default_behavior)
get_json_behavior(jsexpr->on_empty, context, "EMPTY");
if (jsexpr->on_error && jsexpr->on_error->btype != default_behavior)
get_json_behavior(jsexpr->on_error, context, "ERROR");
}
/* ----------
* get_rule_expr - Parse back an expression
@@ -10031,6 +10095,67 @@ get_rule_expr(Node *node, deparse_context *context,
}
break;
case T_JsonExpr:
{
JsonExpr *jexpr = (JsonExpr *) node;
switch (jexpr->op)
{
case JSON_EXISTS_OP:
appendStringInfoString(buf, "JSON_EXISTS(");
break;
case JSON_QUERY_OP:
appendStringInfoString(buf, "JSON_QUERY(");
break;
case JSON_VALUE_OP:
appendStringInfoString(buf, "JSON_VALUE(");
break;
default:
elog(ERROR, "unrecognized JsonExpr op: %d",
(int) jexpr->op);
}
get_rule_expr(jexpr->formatted_expr, context, showimplicit);
appendStringInfoString(buf, ", ");
get_json_path_spec(jexpr->path_spec, context, showimplicit);
if (jexpr->passing_values)
{
ListCell *lc1,
*lc2;
bool needcomma = false;
appendStringInfoString(buf, " PASSING ");
forboth(lc1, jexpr->passing_names,
lc2, jexpr->passing_values)
{
if (needcomma)
appendStringInfoString(buf, ", ");
needcomma = true;
get_rule_expr((Node *) lfirst(lc2), context, showimplicit);
appendStringInfo(buf, " AS %s",
((String *) lfirst_node(String, lc1))->sval);
}
}
if (jexpr->op != JSON_EXISTS_OP ||
jexpr->returning->typid != BOOLOID)
get_json_returning(jexpr->returning, context->buf,
jexpr->op == JSON_QUERY_OP);
get_json_expr_options(jexpr, context,
jexpr->op != JSON_EXISTS_OP ?
JSON_BEHAVIOR_NULL :
JSON_BEHAVIOR_FALSE);
appendStringInfoString(buf, ")");
}
break;
case T_List:
{
char *sep;
@@ -10154,6 +10279,7 @@ looks_like_function(Node *node)
case T_MinMaxExpr:
case T_SQLValueFunction:
case T_XmlExpr:
case T_JsonExpr:
/* these are all accepted by func_expr_common_subexpr */
return true;
default:
@@ -11023,6 +11149,18 @@ get_const_collation(Const *constval, deparse_context *context)
}
}
/*
* get_json_path_spec - Parse back a JSON path specification
*/
static void
get_json_path_spec(Node *path_spec, deparse_context *context, bool showimplicit)
{
if (IsA(path_spec, Const))
get_const_expr((Const *) path_spec, context, -1);
else
get_rule_expr(path_spec, context, showimplicit);
}
/*
* get_json_format - Parse back a JsonFormat node
*/