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

SQL/JSON query functions

This introduces the SQL/JSON functions for querying JSON data using
jsonpath expressions. The functions are:

JSON_EXISTS()
JSON_QUERY()
JSON_VALUE()

All of these functions only operate on jsonb. The workaround for now is
to cast the argument to jsonb.

JSON_EXISTS() tests if the jsonpath expression applied to the jsonb
value yields any values. JSON_VALUE() must return a single value, and an
error occurs if it tries to return multiple values. JSON_QUERY() must
return a json object or array, and there are various WRAPPER options for
handling scalar or multi-value results. Both these functions have
options for handling EMPTY and ERROR conditions.

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:11:14 -05:00
parent 3d067c53b2
commit 1a36bc9dba
39 changed files with 4719 additions and 124 deletions

View File

@ -2491,6 +2491,90 @@ _copyJsonArrayQueryConstructor(const JsonArrayQueryConstructor *from)
return newnode;
}
/*
* _copyJsonExpr
*/
static JsonExpr *
_copyJsonExpr(const JsonExpr *from)
{
JsonExpr *newnode = makeNode(JsonExpr);
COPY_SCALAR_FIELD(op);
COPY_NODE_FIELD(formatted_expr);
COPY_NODE_FIELD(result_coercion);
COPY_NODE_FIELD(format);
COPY_NODE_FIELD(path_spec);
COPY_NODE_FIELD(passing_values);
COPY_NODE_FIELD(passing_names);
COPY_NODE_FIELD(returning);
COPY_NODE_FIELD(on_error);
COPY_NODE_FIELD(on_empty);
COPY_NODE_FIELD(coercions);
COPY_SCALAR_FIELD(wrapper);
COPY_SCALAR_FIELD(omit_quotes);
COPY_LOCATION_FIELD(location);
return newnode;
}
/*
* _copyJsonCoercion
*/
static JsonCoercion *
_copyJsonCoercion(const JsonCoercion *from)
{
JsonCoercion *newnode = makeNode(JsonCoercion);
COPY_NODE_FIELD(expr);
COPY_SCALAR_FIELD(via_populate);
COPY_SCALAR_FIELD(via_io);
COPY_SCALAR_FIELD(collation);
return newnode;
}
/*
* _copyJsonItemCoercions
*/
static JsonItemCoercions *
_copyJsonItemCoercions(const JsonItemCoercions *from)
{
JsonItemCoercions *newnode = makeNode(JsonItemCoercions);
COPY_NODE_FIELD(null);
COPY_NODE_FIELD(string);
COPY_NODE_FIELD(numeric);
COPY_NODE_FIELD(boolean);
COPY_NODE_FIELD(date);
COPY_NODE_FIELD(time);
COPY_NODE_FIELD(timetz);
COPY_NODE_FIELD(timestamp);
COPY_NODE_FIELD(timestamptz);
COPY_NODE_FIELD(composite);
return newnode;
}
/*
* _copyJsonFuncExpr
*/
static JsonFuncExpr *
_copyJsonFuncExpr(const JsonFuncExpr *from)
{
JsonFuncExpr *newnode = makeNode(JsonFuncExpr);
COPY_SCALAR_FIELD(op);
COPY_NODE_FIELD(common);
COPY_NODE_FIELD(output);
COPY_NODE_FIELD(on_empty);
COPY_NODE_FIELD(on_error);
COPY_SCALAR_FIELD(wrapper);
COPY_SCALAR_FIELD(omit_quotes);
COPY_LOCATION_FIELD(location);
return newnode;
}
/*
* _copyJsonIsPredicate
*/
@ -2508,6 +2592,51 @@ _copyJsonIsPredicate(const JsonIsPredicate *from)
return newnode;
}
/*
* _copyJsonBehavior
*/
static JsonBehavior *
_copyJsonBehavior(const JsonBehavior *from)
{
JsonBehavior *newnode = makeNode(JsonBehavior);
COPY_SCALAR_FIELD(btype);
COPY_NODE_FIELD(default_expr);
return newnode;
}
/*
* _copyJsonCommon
*/
static JsonCommon *
_copyJsonCommon(const JsonCommon *from)
{
JsonCommon *newnode = makeNode(JsonCommon);
COPY_NODE_FIELD(expr);
COPY_NODE_FIELD(pathspec);
COPY_STRING_FIELD(pathname);
COPY_NODE_FIELD(passing);
COPY_LOCATION_FIELD(location);
return newnode;
}
/*
* _copyJsonArgument
*/
static JsonArgument *
_copyJsonArgument(const JsonArgument *from)
{
JsonArgument *newnode = makeNode(JsonArgument);
COPY_NODE_FIELD(val);
COPY_STRING_FIELD(name);
return newnode;
}
/* ****************************************************************
* pathnodes.h copy functions
*
@ -5645,6 +5774,27 @@ copyObjectImpl(const void *from)
case T_JsonIsPredicate:
retval = _copyJsonIsPredicate(from);
break;
case T_JsonFuncExpr:
retval = _copyJsonFuncExpr(from);
break;
case T_JsonExpr:
retval = _copyJsonExpr(from);
break;
case T_JsonCommon:
retval = _copyJsonCommon(from);
break;
case T_JsonBehavior:
retval = _copyJsonBehavior(from);
break;
case T_JsonArgument:
retval = _copyJsonArgument(from);
break;
case T_JsonCoercion:
retval = _copyJsonCoercion(from);
break;
case T_JsonItemCoercions:
retval = _copyJsonItemCoercions(from);
break;
/*
* RELATION NODES