mirror of
https://github.com/postgres/postgres.git
synced 2025-11-04 20:11:56 +03:00
SQL/JSON: add standard JSON constructor functions
This commit introduces the SQL/JSON standard-conforming constructors for JSON types: JSON_ARRAY() JSON_ARRAYAGG() JSON_OBJECT() JSON_OBJECTAGG() Most of the functionality was already present in PostgreSQL-specific functions, but these include some new functionality such as the ability to skip or include NULL values, and to allow duplicate keys or throw error when they are found, as well as the standard specified syntax to specify output type and format. 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: 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. Discussion: https://postgr.es/m/CAF4Au4w2x-5LTnN_bxky-mq4=WOqsGsxSpENCzHRAzSnEd8+WQ@mail.gmail.com 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
This commit is contained in:
@@ -108,4 +108,10 @@ extern GroupingSet *makeGroupingSet(GroupingSetKind kind, List *content, int loc
|
||||
|
||||
extern VacuumRelation *makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols);
|
||||
|
||||
extern JsonFormat *makeJsonFormat(JsonFormatType type, JsonEncoding encoding,
|
||||
int location);
|
||||
extern JsonValueExpr *makeJsonValueExpr(Expr *expr, JsonFormat *format);
|
||||
extern Node *makeJsonKeyValue(Node *key, Node *value);
|
||||
extern JsonEncoding makeJsonEncoding(char *name);
|
||||
|
||||
#endif /* MAKEFUNC_H */
|
||||
|
||||
@@ -1713,6 +1713,113 @@ typedef struct TriggerTransition
|
||||
bool isTable;
|
||||
} TriggerTransition;
|
||||
|
||||
/* Nodes for SQL/JSON support */
|
||||
|
||||
/*
|
||||
* JsonOutput -
|
||||
* representation of JSON output clause (RETURNING type [FORMAT format])
|
||||
*/
|
||||
typedef struct JsonOutput
|
||||
{
|
||||
NodeTag type;
|
||||
TypeName *typeName; /* RETURNING type name, if specified */
|
||||
JsonReturning *returning; /* RETURNING FORMAT clause and type Oids */
|
||||
} JsonOutput;
|
||||
|
||||
/*
|
||||
* JsonKeyValue -
|
||||
* untransformed representation of JSON object key-value pair for
|
||||
* JSON_OBJECT() and JSON_OBJECTAGG()
|
||||
*/
|
||||
typedef struct JsonKeyValue
|
||||
{
|
||||
NodeTag type;
|
||||
Expr *key; /* key expression */
|
||||
JsonValueExpr *value; /* JSON value expression */
|
||||
} JsonKeyValue;
|
||||
|
||||
/*
|
||||
* JsonObjectConstructor -
|
||||
* untransformed representation of JSON_OBJECT() constructor
|
||||
*/
|
||||
typedef struct JsonObjectConstructor
|
||||
{
|
||||
NodeTag type;
|
||||
List *exprs; /* list of JsonKeyValue pairs */
|
||||
JsonOutput *output; /* RETURNING clause, if specified */
|
||||
bool absent_on_null; /* skip NULL values? */
|
||||
bool unique; /* check key uniqueness? */
|
||||
int location; /* token location, or -1 if unknown */
|
||||
} JsonObjectConstructor;
|
||||
|
||||
/*
|
||||
* JsonArrayConstructor -
|
||||
* untransformed representation of JSON_ARRAY(element,...) constructor
|
||||
*/
|
||||
typedef struct JsonArrayConstructor
|
||||
{
|
||||
NodeTag type;
|
||||
List *exprs; /* list of JsonValueExpr elements */
|
||||
JsonOutput *output; /* RETURNING clause, if specified */
|
||||
bool absent_on_null; /* skip NULL elements? */
|
||||
int location; /* token location, or -1 if unknown */
|
||||
} JsonArrayConstructor;
|
||||
|
||||
/*
|
||||
* JsonArrayQueryConstructor -
|
||||
* untransformed representation of JSON_ARRAY(subquery) constructor
|
||||
*/
|
||||
typedef struct JsonArrayQueryConstructor
|
||||
{
|
||||
NodeTag type;
|
||||
Node *query; /* subquery */
|
||||
JsonOutput *output; /* RETURNING clause, if specified */
|
||||
JsonFormat *format; /* FORMAT clause for subquery, if specified */
|
||||
bool absent_on_null; /* skip NULL elements? */
|
||||
int location; /* token location, or -1 if unknown */
|
||||
} JsonArrayQueryConstructor;
|
||||
|
||||
/*
|
||||
* JsonAggConstructor -
|
||||
* common fields of untransformed representation of
|
||||
* JSON_ARRAYAGG() and JSON_OBJECTAGG()
|
||||
*/
|
||||
typedef struct JsonAggConstructor
|
||||
{
|
||||
NodeTag type;
|
||||
JsonOutput *output; /* RETURNING clause, if any */
|
||||
Node *agg_filter; /* FILTER clause, if any */
|
||||
List *agg_order; /* ORDER BY clause, if any */
|
||||
struct WindowDef *over; /* OVER clause, if any */
|
||||
int location; /* token location, or -1 if unknown */
|
||||
} JsonAggConstructor;
|
||||
|
||||
/*
|
||||
* JsonObjectAgg -
|
||||
* untransformed representation of JSON_OBJECTAGG()
|
||||
*/
|
||||
typedef struct JsonObjectAgg
|
||||
{
|
||||
NodeTag type;
|
||||
JsonAggConstructor *constructor; /* common fields */
|
||||
JsonKeyValue *arg; /* object key-value pair */
|
||||
bool absent_on_null; /* skip NULL values? */
|
||||
bool unique; /* check key uniqueness? */
|
||||
} JsonObjectAgg;
|
||||
|
||||
/*
|
||||
* JsonArrayAgg -
|
||||
* untransformed representation of JSON_ARRRAYAGG()
|
||||
*/
|
||||
typedef struct JsonArrayAgg
|
||||
{
|
||||
NodeTag type;
|
||||
JsonAggConstructor *constructor; /* common fields */
|
||||
JsonValueExpr *arg; /* array element expression */
|
||||
bool absent_on_null; /* skip NULL elements? */
|
||||
} JsonArrayAgg;
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Raw Grammar Output Statements
|
||||
*****************************************************************************/
|
||||
|
||||
@@ -1498,6 +1498,91 @@ typedef struct XmlExpr
|
||||
int location;
|
||||
} XmlExpr;
|
||||
|
||||
/*
|
||||
* JsonEncoding -
|
||||
* representation of JSON ENCODING clause
|
||||
*/
|
||||
typedef enum JsonEncoding
|
||||
{
|
||||
JS_ENC_DEFAULT, /* unspecified */
|
||||
JS_ENC_UTF8,
|
||||
JS_ENC_UTF16,
|
||||
JS_ENC_UTF32,
|
||||
} JsonEncoding;
|
||||
|
||||
/*
|
||||
* JsonFormatType -
|
||||
* enumeration of JSON formats used in JSON FORMAT clause
|
||||
*/
|
||||
typedef enum JsonFormatType
|
||||
{
|
||||
JS_FORMAT_DEFAULT, /* unspecified */
|
||||
JS_FORMAT_JSON, /* FORMAT JSON [ENCODING ...] */
|
||||
JS_FORMAT_JSONB /* implicit internal format for RETURNING
|
||||
* jsonb */
|
||||
} JsonFormatType;
|
||||
|
||||
/*
|
||||
* JsonFormat -
|
||||
* representation of JSON FORMAT clause
|
||||
*/
|
||||
typedef struct JsonFormat
|
||||
{
|
||||
NodeTag type;
|
||||
JsonFormatType format_type; /* format type */
|
||||
JsonEncoding encoding; /* JSON encoding */
|
||||
int location; /* token location, or -1 if unknown */
|
||||
} JsonFormat;
|
||||
|
||||
/*
|
||||
* JsonReturning -
|
||||
* transformed representation of JSON RETURNING clause
|
||||
*/
|
||||
typedef struct JsonReturning
|
||||
{
|
||||
NodeTag type;
|
||||
JsonFormat *format; /* output JSON format */
|
||||
Oid typid; /* target type Oid */
|
||||
int32 typmod; /* target type modifier */
|
||||
} JsonReturning;
|
||||
|
||||
/*
|
||||
* JsonValueExpr -
|
||||
* representation of JSON value expression (expr [FORMAT json_format])
|
||||
*/
|
||||
typedef struct JsonValueExpr
|
||||
{
|
||||
NodeTag type;
|
||||
Expr *raw_expr; /* raw expression */
|
||||
Expr *formatted_expr; /* formatted expression or NULL */
|
||||
JsonFormat *format; /* FORMAT clause, if specified */
|
||||
} JsonValueExpr;
|
||||
|
||||
typedef enum JsonConstructorType
|
||||
{
|
||||
JSCTOR_JSON_OBJECT = 1,
|
||||
JSCTOR_JSON_ARRAY = 2,
|
||||
JSCTOR_JSON_OBJECTAGG = 3,
|
||||
JSCTOR_JSON_ARRAYAGG = 4
|
||||
} JsonConstructorType;
|
||||
|
||||
/*
|
||||
* JsonConstructorExpr -
|
||||
* wrapper over FuncExpr/Aggref/WindowFunc for SQL/JSON constructors
|
||||
*/
|
||||
typedef struct JsonConstructorExpr
|
||||
{
|
||||
Expr xpr;
|
||||
JsonConstructorType type; /* constructor type */
|
||||
List *args;
|
||||
Expr *func; /* underlying json[b]_xxx() function call */
|
||||
Expr *coercion; /* coercion to RETURNING type */
|
||||
JsonReturning *returning; /* RETURNING clause */
|
||||
bool absent_on_null; /* ABSENT ON NULL? */
|
||||
bool unique; /* WITH UNIQUE KEYS? (JSON_OBJECT[AGG] only) */
|
||||
int location;
|
||||
} JsonConstructorExpr;
|
||||
|
||||
/* ----------------
|
||||
* NullTest
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user