2019-03-20 09:09:07 +01:00
|
|
|
%{
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* jsonpath_gram.y
|
|
|
|
* Grammar definitions for jsonpath datatype
|
|
|
|
*
|
2019-03-25 13:42:51 +01:00
|
|
|
* Transforms tokenized jsonpath into tree of JsonPathParseItem structs.
|
|
|
|
*
|
2021-01-02 19:06:25 +01:00
|
|
|
* Copyright (c) 2019-2021, PostgreSQL Global Development Group
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/utils/adt/jsonpath_gram.y
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "catalog/pg_collation.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "nodes/pg_list.h"
|
|
|
|
#include "regex/regex.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/jsonpath.h"
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
/* struct JsonPathString is shared between scan and gram */
|
|
|
|
typedef struct JsonPathString
|
|
|
|
{
|
|
|
|
char *val;
|
|
|
|
int len;
|
|
|
|
int total;
|
|
|
|
} JsonPathString;
|
|
|
|
|
|
|
|
union YYSTYPE;
|
|
|
|
|
|
|
|
/* flex 2.5.4 doesn't bother with a decl for this */
|
|
|
|
int jsonpath_yylex(union YYSTYPE *yylval_param);
|
|
|
|
int jsonpath_yyparse(JsonPathParseResult **result);
|
|
|
|
void jsonpath_yyerror(JsonPathParseResult **result, const char *message);
|
|
|
|
|
2019-03-25 13:42:51 +01:00
|
|
|
static JsonPathParseItem *makeItemType(JsonPathItemType type);
|
2019-03-20 09:09:07 +01:00
|
|
|
static JsonPathParseItem *makeItemString(JsonPathString *s);
|
|
|
|
static JsonPathParseItem *makeItemVariable(JsonPathString *s);
|
|
|
|
static JsonPathParseItem *makeItemKey(JsonPathString *s);
|
|
|
|
static JsonPathParseItem *makeItemNumeric(JsonPathString *s);
|
|
|
|
static JsonPathParseItem *makeItemBool(bool val);
|
2019-03-25 13:42:51 +01:00
|
|
|
static JsonPathParseItem *makeItemBinary(JsonPathItemType type,
|
|
|
|
JsonPathParseItem *la,
|
2019-03-20 09:09:07 +01:00
|
|
|
JsonPathParseItem *ra);
|
2019-03-25 13:42:51 +01:00
|
|
|
static JsonPathParseItem *makeItemUnary(JsonPathItemType type,
|
|
|
|
JsonPathParseItem *a);
|
2019-03-20 09:09:07 +01:00
|
|
|
static JsonPathParseItem *makeItemList(List *list);
|
|
|
|
static JsonPathParseItem *makeIndexArray(List *list);
|
|
|
|
static JsonPathParseItem *makeAny(int first, int last);
|
|
|
|
static JsonPathParseItem *makeItemLikeRegex(JsonPathParseItem *expr,
|
|
|
|
JsonPathString *pattern,
|
|
|
|
JsonPathString *flags);
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bison doesn't allocate anything that needs to live across parser calls,
|
|
|
|
* so we can easily have it use palloc instead of malloc. This prevents
|
|
|
|
* memory leaks if we error out during parsing. Note this only works with
|
|
|
|
* bison >= 2.0. However, in bison 1.875 the default is to use alloca()
|
|
|
|
* if possible, so there's not really much problem anyhow, at least if
|
|
|
|
* you're building with gcc.
|
|
|
|
*/
|
|
|
|
#define YYMALLOC palloc
|
|
|
|
#define YYFREE pfree
|
|
|
|
|
|
|
|
%}
|
|
|
|
|
|
|
|
/* BISON Declarations */
|
|
|
|
%pure-parser
|
|
|
|
%expect 0
|
|
|
|
%name-prefix="jsonpath_yy"
|
|
|
|
%parse-param {JsonPathParseResult **result}
|
|
|
|
|
|
|
|
%union {
|
2019-03-19 18:56:13 +01:00
|
|
|
JsonPathString str;
|
2019-03-25 13:42:51 +01:00
|
|
|
List *elems; /* list of JsonPathParseItem */
|
|
|
|
List *indexs; /* list of integers */
|
|
|
|
JsonPathParseItem *value;
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
JsonPathParseResult *result;
|
|
|
|
JsonPathItemType optype;
|
|
|
|
bool boolean;
|
|
|
|
int integer;
|
|
|
|
}
|
|
|
|
|
|
|
|
%token <str> TO_P NULL_P TRUE_P FALSE_P IS_P UNKNOWN_P EXISTS_P
|
|
|
|
%token <str> IDENT_P STRING_P NUMERIC_P INT_P VARIABLE_P
|
|
|
|
%token <str> OR_P AND_P NOT_P
|
|
|
|
%token <str> LESS_P LESSEQUAL_P EQUAL_P NOTEQUAL_P GREATEREQUAL_P GREATER_P
|
|
|
|
%token <str> ANY_P STRICT_P LAX_P LAST_P STARTS_P WITH_P LIKE_REGEX_P FLAG_P
|
|
|
|
%token <str> ABS_P SIZE_P TYPE_P FLOOR_P DOUBLE_P CEILING_P KEYVALUE_P
|
Implement jsonpath .datetime() method
This commit implements jsonpath .datetime() method as it's specified in
SQL/JSON standard. There are no-argument and single-argument versions of
this method. No-argument version selects first of ISO datetime formats
matching input string. Single-argument version accepts template string as
its argument.
Additionally to .datetime() method itself this commit also implements
comparison ability of resulting date and time values. There is some difficulty
because exising jsonb_path_*() functions are immutable, while comparison of
timezoned and non-timezoned types involves current timezone. At first, current
timezone could be changes in session. Moreover, timezones themselves are not
immutable and could be updated. This is why we let existing immutable functions
throw errors on such non-immutable comparison. In the same time this commit
provides jsonb_path_*_tz() functions which are stable and support operations
involving timezones. As new functions are added to the system catalog,
catversion is bumped.
Support of .datetime() method was the only blocker prevents T832 from being
marked as supported. sql_features.txt is updated correspondingly.
Extracted from original patch by Nikita Glukhov, Teodor Sigaev, Oleg Bartunov.
Heavily revised by me. Comments were adjusted by Liudmila Mantrova.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Discussion: https://postgr.es/m/CAPpHfdsZgYEra_PeCLGNoXOWYx6iU-S3wF8aX0ObQUcZU%2B4XTw%40mail.gmail.com
Author: Alexander Korotkov, Nikita Glukhov, Teodor Sigaev, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Anastasia Lubennikova, Peter Eisentraut
2019-09-25 20:54:14 +02:00
|
|
|
%token <str> DATETIME_P
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
|
|
|
|
%type <result> result
|
|
|
|
|
|
|
|
%type <value> scalar_value path_primary expr array_accessor
|
|
|
|
any_path accessor_op key predicate delimited_predicate
|
|
|
|
index_elem starts_with_initial expr_or_predicate
|
Implement jsonpath .datetime() method
This commit implements jsonpath .datetime() method as it's specified in
SQL/JSON standard. There are no-argument and single-argument versions of
this method. No-argument version selects first of ISO datetime formats
matching input string. Single-argument version accepts template string as
its argument.
Additionally to .datetime() method itself this commit also implements
comparison ability of resulting date and time values. There is some difficulty
because exising jsonb_path_*() functions are immutable, while comparison of
timezoned and non-timezoned types involves current timezone. At first, current
timezone could be changes in session. Moreover, timezones themselves are not
immutable and could be updated. This is why we let existing immutable functions
throw errors on such non-immutable comparison. In the same time this commit
provides jsonb_path_*_tz() functions which are stable and support operations
involving timezones. As new functions are added to the system catalog,
catversion is bumped.
Support of .datetime() method was the only blocker prevents T832 from being
marked as supported. sql_features.txt is updated correspondingly.
Extracted from original patch by Nikita Glukhov, Teodor Sigaev, Oleg Bartunov.
Heavily revised by me. Comments were adjusted by Liudmila Mantrova.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Discussion: https://postgr.es/m/CAPpHfdsZgYEra_PeCLGNoXOWYx6iU-S3wF8aX0ObQUcZU%2B4XTw%40mail.gmail.com
Author: Alexander Korotkov, Nikita Glukhov, Teodor Sigaev, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Anastasia Lubennikova, Peter Eisentraut
2019-09-25 20:54:14 +02:00
|
|
|
datetime_template opt_datetime_template
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
|
|
|
|
%type <elems> accessor_expr
|
|
|
|
|
|
|
|
%type <indexs> index_list
|
|
|
|
|
|
|
|
%type <optype> comp_op method
|
|
|
|
|
|
|
|
%type <boolean> mode
|
|
|
|
|
|
|
|
%type <str> key_name
|
|
|
|
|
|
|
|
%type <integer> any_level
|
|
|
|
|
|
|
|
%left OR_P
|
|
|
|
%left AND_P
|
|
|
|
%right NOT_P
|
|
|
|
%left '+' '-'
|
|
|
|
%left '*' '/' '%'
|
|
|
|
%left UMINUS
|
|
|
|
%nonassoc '(' ')'
|
|
|
|
|
|
|
|
/* Grammar follows */
|
|
|
|
%%
|
|
|
|
|
|
|
|
result:
|
|
|
|
mode expr_or_predicate {
|
|
|
|
*result = palloc(sizeof(JsonPathParseResult));
|
|
|
|
(*result)->expr = $2;
|
|
|
|
(*result)->lax = $1;
|
Suppress variable-set-but-not-used warnings from clang 15.
clang 15+ will issue a set-but-not-used warning when the only
use of a variable is in autoincrements (e.g., "foo++;").
That's perfectly sensible, but it detects a few more cases that
we'd not noticed before. Silence the warnings with our usual
methods, such as PG_USED_FOR_ASSERTS_ONLY, or in one case by
actually removing a useless variable.
One thing that we can't nicely get rid of is that with %pure-parser,
Bison emits "yynerrs" as a local variable that falls foul of this
warning. To silence those, I inserted "(void) yynerrs;" in the
top-level productions of affected grammars.
Per recently-established project policy, this is a candidate
for back-patching into out-of-support branches: it suppresses
annoying compiler warnings but changes no behavior. Hence,
back-patch to 9.5, which is as far as these patches go without
issues. (A preliminary check shows that the prior branches
need some other set-but-not-used cleanups too, so I'll leave
them for another day.)
Discussion: https://postgr.es/m/514615.1663615243@sss.pgh.pa.us
2022-09-20 18:04:37 +02:00
|
|
|
(void) yynerrs;
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
}
|
|
|
|
| /* EMPTY */ { *result = NULL; }
|
|
|
|
;
|
|
|
|
|
|
|
|
expr_or_predicate:
|
|
|
|
expr { $$ = $1; }
|
|
|
|
| predicate { $$ = $1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
mode:
|
|
|
|
STRICT_P { $$ = false; }
|
|
|
|
| LAX_P { $$ = true; }
|
|
|
|
| /* EMPTY */ { $$ = true; }
|
|
|
|
;
|
|
|
|
|
|
|
|
scalar_value:
|
|
|
|
STRING_P { $$ = makeItemString(&$1); }
|
|
|
|
| NULL_P { $$ = makeItemString(NULL); }
|
|
|
|
| TRUE_P { $$ = makeItemBool(true); }
|
|
|
|
| FALSE_P { $$ = makeItemBool(false); }
|
|
|
|
| NUMERIC_P { $$ = makeItemNumeric(&$1); }
|
|
|
|
| INT_P { $$ = makeItemNumeric(&$1); }
|
2020-11-11 17:37:18 +01:00
|
|
|
| VARIABLE_P { $$ = makeItemVariable(&$1); }
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
comp_op:
|
|
|
|
EQUAL_P { $$ = jpiEqual; }
|
|
|
|
| NOTEQUAL_P { $$ = jpiNotEqual; }
|
|
|
|
| LESS_P { $$ = jpiLess; }
|
|
|
|
| GREATER_P { $$ = jpiGreater; }
|
|
|
|
| LESSEQUAL_P { $$ = jpiLessOrEqual; }
|
|
|
|
| GREATEREQUAL_P { $$ = jpiGreaterOrEqual; }
|
|
|
|
;
|
|
|
|
|
|
|
|
delimited_predicate:
|
2019-03-25 13:42:51 +01:00
|
|
|
'(' predicate ')' { $$ = $2; }
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
| EXISTS_P '(' expr ')' { $$ = makeItemUnary(jpiExists, $3); }
|
|
|
|
;
|
|
|
|
|
|
|
|
predicate:
|
|
|
|
delimited_predicate { $$ = $1; }
|
|
|
|
| expr comp_op expr { $$ = makeItemBinary($2, $1, $3); }
|
|
|
|
| predicate AND_P predicate { $$ = makeItemBinary(jpiAnd, $1, $3); }
|
|
|
|
| predicate OR_P predicate { $$ = makeItemBinary(jpiOr, $1, $3); }
|
2020-11-11 17:37:18 +01:00
|
|
|
| NOT_P delimited_predicate { $$ = makeItemUnary(jpiNot, $2); }
|
2019-03-25 13:42:51 +01:00
|
|
|
| '(' predicate ')' IS_P UNKNOWN_P
|
|
|
|
{ $$ = makeItemUnary(jpiIsUnknown, $2); }
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
| expr STARTS_P WITH_P starts_with_initial
|
2019-03-25 13:42:51 +01:00
|
|
|
{ $$ = makeItemBinary(jpiStartsWith, $1, $4); }
|
2020-11-11 17:37:18 +01:00
|
|
|
| expr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1, &$3, NULL); }
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
| expr LIKE_REGEX_P STRING_P FLAG_P STRING_P
|
|
|
|
{ $$ = makeItemLikeRegex($1, &$3, &$5); }
|
|
|
|
;
|
|
|
|
|
|
|
|
starts_with_initial:
|
|
|
|
STRING_P { $$ = makeItemString(&$1); }
|
|
|
|
| VARIABLE_P { $$ = makeItemVariable(&$1); }
|
|
|
|
;
|
|
|
|
|
|
|
|
path_primary:
|
|
|
|
scalar_value { $$ = $1; }
|
|
|
|
| '$' { $$ = makeItemType(jpiRoot); }
|
|
|
|
| '@' { $$ = makeItemType(jpiCurrent); }
|
|
|
|
| LAST_P { $$ = makeItemType(jpiLast); }
|
|
|
|
;
|
|
|
|
|
|
|
|
accessor_expr:
|
|
|
|
path_primary { $$ = list_make1($1); }
|
|
|
|
| '(' expr ')' accessor_op { $$ = list_make2($2, $4); }
|
|
|
|
| '(' predicate ')' accessor_op { $$ = list_make2($2, $4); }
|
|
|
|
| accessor_expr accessor_op { $$ = lappend($1, $2); }
|
|
|
|
;
|
|
|
|
|
|
|
|
expr:
|
|
|
|
accessor_expr { $$ = makeItemList($1); }
|
|
|
|
| '(' expr ')' { $$ = $2; }
|
|
|
|
| '+' expr %prec UMINUS { $$ = makeItemUnary(jpiPlus, $2); }
|
|
|
|
| '-' expr %prec UMINUS { $$ = makeItemUnary(jpiMinus, $2); }
|
|
|
|
| expr '+' expr { $$ = makeItemBinary(jpiAdd, $1, $3); }
|
|
|
|
| expr '-' expr { $$ = makeItemBinary(jpiSub, $1, $3); }
|
|
|
|
| expr '*' expr { $$ = makeItemBinary(jpiMul, $1, $3); }
|
|
|
|
| expr '/' expr { $$ = makeItemBinary(jpiDiv, $1, $3); }
|
|
|
|
| expr '%' expr { $$ = makeItemBinary(jpiMod, $1, $3); }
|
|
|
|
;
|
|
|
|
|
|
|
|
index_elem:
|
|
|
|
expr { $$ = makeItemBinary(jpiSubscript, $1, NULL); }
|
|
|
|
| expr TO_P expr { $$ = makeItemBinary(jpiSubscript, $1, $3); }
|
|
|
|
;
|
|
|
|
|
|
|
|
index_list:
|
|
|
|
index_elem { $$ = list_make1($1); }
|
|
|
|
| index_list ',' index_elem { $$ = lappend($1, $3); }
|
|
|
|
;
|
|
|
|
|
|
|
|
array_accessor:
|
|
|
|
'[' '*' ']' { $$ = makeItemType(jpiAnyArray); }
|
|
|
|
| '[' index_list ']' { $$ = makeIndexArray($2); }
|
|
|
|
;
|
|
|
|
|
|
|
|
any_level:
|
|
|
|
INT_P { $$ = pg_atoi($1.val, 4, 0); }
|
|
|
|
| LAST_P { $$ = -1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
any_path:
|
|
|
|
ANY_P { $$ = makeAny(0, -1); }
|
|
|
|
| ANY_P '{' any_level '}' { $$ = makeAny($3, $3); }
|
2019-03-25 13:42:51 +01:00
|
|
|
| ANY_P '{' any_level TO_P any_level '}'
|
|
|
|
{ $$ = makeAny($3, $5); }
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
accessor_op:
|
|
|
|
'.' key { $$ = $2; }
|
|
|
|
| '.' '*' { $$ = makeItemType(jpiAnyKey); }
|
|
|
|
| array_accessor { $$ = $1; }
|
|
|
|
| '.' any_path { $$ = $2; }
|
|
|
|
| '.' method '(' ')' { $$ = makeItemType($2); }
|
Implement jsonpath .datetime() method
This commit implements jsonpath .datetime() method as it's specified in
SQL/JSON standard. There are no-argument and single-argument versions of
this method. No-argument version selects first of ISO datetime formats
matching input string. Single-argument version accepts template string as
its argument.
Additionally to .datetime() method itself this commit also implements
comparison ability of resulting date and time values. There is some difficulty
because exising jsonb_path_*() functions are immutable, while comparison of
timezoned and non-timezoned types involves current timezone. At first, current
timezone could be changes in session. Moreover, timezones themselves are not
immutable and could be updated. This is why we let existing immutable functions
throw errors on such non-immutable comparison. In the same time this commit
provides jsonb_path_*_tz() functions which are stable and support operations
involving timezones. As new functions are added to the system catalog,
catversion is bumped.
Support of .datetime() method was the only blocker prevents T832 from being
marked as supported. sql_features.txt is updated correspondingly.
Extracted from original patch by Nikita Glukhov, Teodor Sigaev, Oleg Bartunov.
Heavily revised by me. Comments were adjusted by Liudmila Mantrova.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Discussion: https://postgr.es/m/CAPpHfdsZgYEra_PeCLGNoXOWYx6iU-S3wF8aX0ObQUcZU%2B4XTw%40mail.gmail.com
Author: Alexander Korotkov, Nikita Glukhov, Teodor Sigaev, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Anastasia Lubennikova, Peter Eisentraut
2019-09-25 20:54:14 +02:00
|
|
|
| '.' DATETIME_P '(' opt_datetime_template ')'
|
|
|
|
{ $$ = makeItemUnary(jpiDatetime, $4); }
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
| '?' '(' predicate ')' { $$ = makeItemUnary(jpiFilter, $3); }
|
|
|
|
;
|
|
|
|
|
Implement jsonpath .datetime() method
This commit implements jsonpath .datetime() method as it's specified in
SQL/JSON standard. There are no-argument and single-argument versions of
this method. No-argument version selects first of ISO datetime formats
matching input string. Single-argument version accepts template string as
its argument.
Additionally to .datetime() method itself this commit also implements
comparison ability of resulting date and time values. There is some difficulty
because exising jsonb_path_*() functions are immutable, while comparison of
timezoned and non-timezoned types involves current timezone. At first, current
timezone could be changes in session. Moreover, timezones themselves are not
immutable and could be updated. This is why we let existing immutable functions
throw errors on such non-immutable comparison. In the same time this commit
provides jsonb_path_*_tz() functions which are stable and support operations
involving timezones. As new functions are added to the system catalog,
catversion is bumped.
Support of .datetime() method was the only blocker prevents T832 from being
marked as supported. sql_features.txt is updated correspondingly.
Extracted from original patch by Nikita Glukhov, Teodor Sigaev, Oleg Bartunov.
Heavily revised by me. Comments were adjusted by Liudmila Mantrova.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Discussion: https://postgr.es/m/CAPpHfdsZgYEra_PeCLGNoXOWYx6iU-S3wF8aX0ObQUcZU%2B4XTw%40mail.gmail.com
Author: Alexander Korotkov, Nikita Glukhov, Teodor Sigaev, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Anastasia Lubennikova, Peter Eisentraut
2019-09-25 20:54:14 +02:00
|
|
|
datetime_template:
|
|
|
|
STRING_P { $$ = makeItemString(&$1); }
|
|
|
|
;
|
|
|
|
|
|
|
|
opt_datetime_template:
|
|
|
|
datetime_template { $$ = $1; }
|
|
|
|
| /* EMPTY */ { $$ = NULL; }
|
|
|
|
;
|
|
|
|
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
key:
|
|
|
|
key_name { $$ = makeItemKey(&$1); }
|
|
|
|
;
|
|
|
|
|
|
|
|
key_name:
|
|
|
|
IDENT_P
|
|
|
|
| STRING_P
|
|
|
|
| TO_P
|
|
|
|
| NULL_P
|
|
|
|
| TRUE_P
|
|
|
|
| FALSE_P
|
|
|
|
| IS_P
|
|
|
|
| UNKNOWN_P
|
|
|
|
| EXISTS_P
|
|
|
|
| STRICT_P
|
|
|
|
| LAX_P
|
|
|
|
| ABS_P
|
|
|
|
| SIZE_P
|
|
|
|
| TYPE_P
|
|
|
|
| FLOOR_P
|
|
|
|
| DOUBLE_P
|
|
|
|
| CEILING_P
|
Implement jsonpath .datetime() method
This commit implements jsonpath .datetime() method as it's specified in
SQL/JSON standard. There are no-argument and single-argument versions of
this method. No-argument version selects first of ISO datetime formats
matching input string. Single-argument version accepts template string as
its argument.
Additionally to .datetime() method itself this commit also implements
comparison ability of resulting date and time values. There is some difficulty
because exising jsonb_path_*() functions are immutable, while comparison of
timezoned and non-timezoned types involves current timezone. At first, current
timezone could be changes in session. Moreover, timezones themselves are not
immutable and could be updated. This is why we let existing immutable functions
throw errors on such non-immutable comparison. In the same time this commit
provides jsonb_path_*_tz() functions which are stable and support operations
involving timezones. As new functions are added to the system catalog,
catversion is bumped.
Support of .datetime() method was the only blocker prevents T832 from being
marked as supported. sql_features.txt is updated correspondingly.
Extracted from original patch by Nikita Glukhov, Teodor Sigaev, Oleg Bartunov.
Heavily revised by me. Comments were adjusted by Liudmila Mantrova.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Discussion: https://postgr.es/m/CAPpHfdsZgYEra_PeCLGNoXOWYx6iU-S3wF8aX0ObQUcZU%2B4XTw%40mail.gmail.com
Author: Alexander Korotkov, Nikita Glukhov, Teodor Sigaev, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Anastasia Lubennikova, Peter Eisentraut
2019-09-25 20:54:14 +02:00
|
|
|
| DATETIME_P
|
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
2019-03-16 10:15:37 +01:00
|
|
|
| KEYVALUE_P
|
|
|
|
| LAST_P
|
|
|
|
| STARTS_P
|
|
|
|
| WITH_P
|
|
|
|
| LIKE_REGEX_P
|
|
|
|
| FLAG_P
|
|
|
|
;
|
|
|
|
|
|
|
|
method:
|
|
|
|
ABS_P { $$ = jpiAbs; }
|
|
|
|
| SIZE_P { $$ = jpiSize; }
|
|
|
|
| TYPE_P { $$ = jpiType; }
|
|
|
|
| FLOOR_P { $$ = jpiFloor; }
|
|
|
|
| DOUBLE_P { $$ = jpiDouble; }
|
|
|
|
| CEILING_P { $$ = jpiCeiling; }
|
|
|
|
| KEYVALUE_P { $$ = jpiKeyValue; }
|
|
|
|
;
|
|
|
|
%%
|
2019-03-20 09:09:07 +01:00
|
|
|
|
2019-03-25 13:42:51 +01:00
|
|
|
/*
|
|
|
|
* The helper functions below allocate and fill JsonPathParseItem's of various
|
|
|
|
* types.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeItemType(JsonPathItemType type)
|
2019-03-20 09:09:07 +01:00
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v = palloc(sizeof(*v));
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
|
|
|
v->type = type;
|
|
|
|
v->next = NULL;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-03-25 13:42:51 +01:00
|
|
|
static JsonPathParseItem *
|
2019-03-20 09:09:07 +01:00
|
|
|
makeItemString(JsonPathString *s)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v;
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
{
|
|
|
|
v = makeItemType(jpiNull);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v = makeItemType(jpiString);
|
|
|
|
v->value.string.val = s->val;
|
|
|
|
v->value.string.len = s->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeItemVariable(JsonPathString *s)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v;
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
v = makeItemType(jpiVariable);
|
|
|
|
v->value.string.val = s->val;
|
|
|
|
v->value.string.len = s->len;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeItemKey(JsonPathString *s)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v;
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
v = makeItemString(s);
|
|
|
|
v->type = jpiKey;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeItemNumeric(JsonPathString *s)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v;
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
v = makeItemType(jpiNumeric);
|
|
|
|
v->value.numeric =
|
|
|
|
DatumGetNumeric(DirectFunctionCall3(numeric_in,
|
2019-04-28 18:45:55 +02:00
|
|
|
CStringGetDatum(s->val),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
Int32GetDatum(-1)));
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeItemBool(bool val)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v = makeItemType(jpiBool);
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
v->value.boolean = val;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
2019-03-25 13:42:51 +01:00
|
|
|
makeItemBinary(JsonPathItemType type, JsonPathParseItem *la, JsonPathParseItem *ra)
|
2019-03-20 09:09:07 +01:00
|
|
|
{
|
|
|
|
JsonPathParseItem *v = makeItemType(type);
|
|
|
|
|
|
|
|
v->value.args.left = la;
|
|
|
|
v->value.args.right = ra;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
2019-03-25 13:42:51 +01:00
|
|
|
makeItemUnary(JsonPathItemType type, JsonPathParseItem *a)
|
2019-03-20 09:09:07 +01:00
|
|
|
{
|
|
|
|
JsonPathParseItem *v;
|
|
|
|
|
|
|
|
if (type == jpiPlus && a->type == jpiNumeric && !a->next)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
if (type == jpiMinus && a->type == jpiNumeric && !a->next)
|
|
|
|
{
|
|
|
|
v = makeItemType(jpiNumeric);
|
|
|
|
v->value.numeric =
|
|
|
|
DatumGetNumeric(DirectFunctionCall1(numeric_uminus,
|
|
|
|
NumericGetDatum(a->value.numeric)));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = makeItemType(type);
|
|
|
|
|
|
|
|
v->value.arg = a;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeItemList(List *list)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *head,
|
|
|
|
*end;
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
ListCell *cell;
|
2019-03-20 09:09:07 +01:00
|
|
|
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
head = end = (JsonPathParseItem *) linitial(list);
|
2019-03-20 09:09:07 +01:00
|
|
|
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
if (list_length(list) == 1)
|
2019-03-20 09:09:07 +01:00
|
|
|
return head;
|
|
|
|
|
|
|
|
/* append items to the end of already existing list */
|
|
|
|
while (end->next)
|
|
|
|
end = end->next;
|
|
|
|
|
Add for_each_from, to simplify loops starting from non-first list cells.
We have a dozen or so places that need to iterate over all but the
first cell of a List. Prior to v13 this was typically written as
for_each_cell(lc, lnext(list_head(list)))
Commit 1cff1b95a changed these to
for_each_cell(lc, list, list_second_cell(list))
This patch introduces a new macro for_each_from() which expresses
the start point as a list index, allowing these to be written as
for_each_from(lc, list, 1)
This is marginally more efficient, since ForEachState.i can be
initialized directly instead of backing into it from a ListCell
address. It also seems clearer and less typo-prone.
Some of the remaining uses of for_each_cell() look like they could
profitably be changed to for_each_from(), but here I confined myself
to changing uses of list_second_cell().
Also, fix for_each_cell_setup() and for_both_cell_setup() to
const-ify their arguments; that's a simple oversight in 1cff1b95a.
Back-patch into v13, on the grounds that (1) the const-ification
is a minor bug fix, and (2) it's better for back-patching purposes
if we only have two ways to write these loops rather than three.
In HEAD, also remove list_third_cell() and list_fourth_cell(),
which were also introduced in 1cff1b95a, and are unused as of
cc99baa43. It seems unlikely that any third-party code would
have started to use them already; anyone who has can be directed
to list_nth_cell instead.
Discussion: https://postgr.es/m/CAApHDvpo1zj9KhEpU2cCRZfSM3Q6XGdhzuAS2v79PH7WJBkYVA@mail.gmail.com
2020-09-29 02:32:53 +02:00
|
|
|
for_each_from(cell, list, 1)
|
2019-03-20 09:09:07 +01:00
|
|
|
{
|
|
|
|
JsonPathParseItem *c = (JsonPathParseItem *) lfirst(cell);
|
|
|
|
|
|
|
|
end->next = c;
|
|
|
|
end = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeIndexArray(List *list)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v = makeItemType(jpiIndexArray);
|
|
|
|
ListCell *cell;
|
2019-03-20 09:09:07 +01:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
Assert(list_length(list) > 0);
|
|
|
|
v->value.array.nelems = list_length(list);
|
|
|
|
|
|
|
|
v->value.array.elems = palloc(sizeof(v->value.array.elems[0]) *
|
|
|
|
v->value.array.nelems);
|
|
|
|
|
|
|
|
foreach(cell, list)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *jpi = lfirst(cell);
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
Assert(jpi->type == jpiSubscript);
|
|
|
|
|
|
|
|
v->value.array.elems[i].from = jpi->value.args.left;
|
|
|
|
v->value.array.elems[i++].to = jpi->value.args.right;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeAny(int first, int last)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v = makeItemType(jpiAny);
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
v->value.anybounds.first = (first >= 0) ? first : PG_UINT32_MAX;
|
|
|
|
v->value.anybounds.last = (last >= 0) ? last : PG_UINT32_MAX;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathParseItem *
|
|
|
|
makeItemLikeRegex(JsonPathParseItem *expr, JsonPathString *pattern,
|
|
|
|
JsonPathString *flags)
|
|
|
|
{
|
2019-03-25 13:42:51 +01:00
|
|
|
JsonPathParseItem *v = makeItemType(jpiLikeRegex);
|
|
|
|
int i;
|
Fix bogus handling of XQuery regex option flags.
The SQL spec defers to XQuery to define what the option flags are
for LIKE_REGEX patterns. XQuery says that:
* 's' allows the dot character to match newlines, which by
default it will not;
* 'm' allows ^ and $ to match at newlines, not only at the
start/end of the whole string.
Thus, these are *not* inverses as they are for the similarly-named
POSIX options, and neither one corresponds to the POSIX 'n' option.
Fortunately, Spencer's library does expose these two behaviors as
separately twiddlable flags, so we just have to fix the mapping from
JSP flag bits to REG flag bits. I also chose to rename the symbol
for 's' to DOTALL, to make it clearer that it's not the inverse
of MLINE.
Also, XQuery says that if the 'q' flag "is used together with the m, s,
or x flag, that flag has no effect". I read this as saying that 'q'
overrides the other flags; whoever wrote our code seems to have read
it backwards.
Lastly, while XQuery's 'x' flag is related to what Spencer's code
does for REG_EXPANDED, it's not the same or a subset. It seems best
to treat XQuery's 'x' as unimplemented for now. Maybe later we can
expand our regex code to offer 'x'-style parsing as a separate option.
While at it, refactor the jsonpath code so that (a) there's only
one copy of the flag transformation logic not two, and (b) the
processing of flags is independent of the order in which the flags
are written.
We need some documentation updates to go with this, but I'll
tackle that separately.
Back-patch to v12 where this code originated.
Discussion: https://postgr.es/m/CAPpHfdvDci4iqNF9fhRkTqhe-5_8HmzeLt56drH%2B_Rv2rNRqfg@mail.gmail.com
Reference: https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#flags
2019-09-17 21:39:51 +02:00
|
|
|
int cflags;
|
2019-03-20 09:09:07 +01:00
|
|
|
|
|
|
|
v->value.like_regex.expr = expr;
|
|
|
|
v->value.like_regex.pattern = pattern->val;
|
|
|
|
v->value.like_regex.patternlen = pattern->len;
|
|
|
|
|
Fix bogus handling of XQuery regex option flags.
The SQL spec defers to XQuery to define what the option flags are
for LIKE_REGEX patterns. XQuery says that:
* 's' allows the dot character to match newlines, which by
default it will not;
* 'm' allows ^ and $ to match at newlines, not only at the
start/end of the whole string.
Thus, these are *not* inverses as they are for the similarly-named
POSIX options, and neither one corresponds to the POSIX 'n' option.
Fortunately, Spencer's library does expose these two behaviors as
separately twiddlable flags, so we just have to fix the mapping from
JSP flag bits to REG flag bits. I also chose to rename the symbol
for 's' to DOTALL, to make it clearer that it's not the inverse
of MLINE.
Also, XQuery says that if the 'q' flag "is used together with the m, s,
or x flag, that flag has no effect". I read this as saying that 'q'
overrides the other flags; whoever wrote our code seems to have read
it backwards.
Lastly, while XQuery's 'x' flag is related to what Spencer's code
does for REG_EXPANDED, it's not the same or a subset. It seems best
to treat XQuery's 'x' as unimplemented for now. Maybe later we can
expand our regex code to offer 'x'-style parsing as a separate option.
While at it, refactor the jsonpath code so that (a) there's only
one copy of the flag transformation logic not two, and (b) the
processing of flags is independent of the order in which the flags
are written.
We need some documentation updates to go with this, but I'll
tackle that separately.
Back-patch to v12 where this code originated.
Discussion: https://postgr.es/m/CAPpHfdvDci4iqNF9fhRkTqhe-5_8HmzeLt56drH%2B_Rv2rNRqfg@mail.gmail.com
Reference: https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#flags
2019-09-17 21:39:51 +02:00
|
|
|
/* Parse the flags string, convert to bitmask. Duplicate flags are OK. */
|
|
|
|
v->value.like_regex.flags = 0;
|
2019-03-20 09:09:07 +01:00
|
|
|
for (i = 0; flags && i < flags->len; i++)
|
|
|
|
{
|
|
|
|
switch (flags->val[i])
|
|
|
|
{
|
|
|
|
case 'i':
|
|
|
|
v->value.like_regex.flags |= JSP_REGEX_ICASE;
|
|
|
|
break;
|
|
|
|
case 's':
|
Fix bogus handling of XQuery regex option flags.
The SQL spec defers to XQuery to define what the option flags are
for LIKE_REGEX patterns. XQuery says that:
* 's' allows the dot character to match newlines, which by
default it will not;
* 'm' allows ^ and $ to match at newlines, not only at the
start/end of the whole string.
Thus, these are *not* inverses as they are for the similarly-named
POSIX options, and neither one corresponds to the POSIX 'n' option.
Fortunately, Spencer's library does expose these two behaviors as
separately twiddlable flags, so we just have to fix the mapping from
JSP flag bits to REG flag bits. I also chose to rename the symbol
for 's' to DOTALL, to make it clearer that it's not the inverse
of MLINE.
Also, XQuery says that if the 'q' flag "is used together with the m, s,
or x flag, that flag has no effect". I read this as saying that 'q'
overrides the other flags; whoever wrote our code seems to have read
it backwards.
Lastly, while XQuery's 'x' flag is related to what Spencer's code
does for REG_EXPANDED, it's not the same or a subset. It seems best
to treat XQuery's 'x' as unimplemented for now. Maybe later we can
expand our regex code to offer 'x'-style parsing as a separate option.
While at it, refactor the jsonpath code so that (a) there's only
one copy of the flag transformation logic not two, and (b) the
processing of flags is independent of the order in which the flags
are written.
We need some documentation updates to go with this, but I'll
tackle that separately.
Back-patch to v12 where this code originated.
Discussion: https://postgr.es/m/CAPpHfdvDci4iqNF9fhRkTqhe-5_8HmzeLt56drH%2B_Rv2rNRqfg@mail.gmail.com
Reference: https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#flags
2019-09-17 21:39:51 +02:00
|
|
|
v->value.like_regex.flags |= JSP_REGEX_DOTALL;
|
2019-03-20 09:09:07 +01:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
v->value.like_regex.flags |= JSP_REGEX_MLINE;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
v->value.like_regex.flags |= JSP_REGEX_WSPACE;
|
|
|
|
break;
|
2019-06-19 21:40:58 +02:00
|
|
|
case 'q':
|
|
|
|
v->value.like_regex.flags |= JSP_REGEX_QUOTE;
|
|
|
|
break;
|
2019-03-20 09:09:07 +01:00
|
|
|
default:
|
2019-04-23 16:43:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("invalid input syntax for type %s", "jsonpath"),
|
Avoid using %c printf format for potentially non-ASCII characters.
Since %c only passes a C "char" to printf, it's incapable of dealing
with multibyte characters. Passing just the first byte of such a
character leads to an output string that is visibly not correctly
encoded, resulting in undesirable behavior such as encoding conversion
failures while sending error messages to clients.
We've lived with this issue for a long time because it was inconvenient
to avoid in a portable fashion. However, now that we always use our own
snprintf code, it's reasonable to use the %.*s format to print just one
possibly-multibyte character in a string. (We previously avoided that
obvious-looking answer in order to work around glibc's bug #6530, cf
commits 54cd4f045 and ed437e2b2.)
Hence, run around and fix a bunch of places that used %c to report
a character found in a user-supplied string. For simplicity, I did
not touch places that were emitting non-user-facing debug messages,
or reporting catalog data that should always be ASCII. (It's also
unclear how useful this approach could be in frontend code, where
it's less certain that we know what encoding we're dealing with.)
In passing, improve a couple of poorly-written error messages in
pageinspect/heapfuncs.c.
This is a longstanding issue, but I'm hesitant to back-patch because
of the impact on translatable message strings. In any case this fix
would not work reliably before v12.
Tom Lane and Quan Zongliang
Discussion: https://postgr.es/m/a120087c-4c88-d9d4-1ec5-808d7a7f133d@gmail.com
2020-06-29 17:41:19 +02:00
|
|
|
errdetail("unrecognized flag character \"%.*s\" in LIKE_REGEX predicate",
|
|
|
|
pg_mblen(flags->val + i), flags->val + i)));
|
2019-03-20 09:09:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Fix bogus handling of XQuery regex option flags.
The SQL spec defers to XQuery to define what the option flags are
for LIKE_REGEX patterns. XQuery says that:
* 's' allows the dot character to match newlines, which by
default it will not;
* 'm' allows ^ and $ to match at newlines, not only at the
start/end of the whole string.
Thus, these are *not* inverses as they are for the similarly-named
POSIX options, and neither one corresponds to the POSIX 'n' option.
Fortunately, Spencer's library does expose these two behaviors as
separately twiddlable flags, so we just have to fix the mapping from
JSP flag bits to REG flag bits. I also chose to rename the symbol
for 's' to DOTALL, to make it clearer that it's not the inverse
of MLINE.
Also, XQuery says that if the 'q' flag "is used together with the m, s,
or x flag, that flag has no effect". I read this as saying that 'q'
overrides the other flags; whoever wrote our code seems to have read
it backwards.
Lastly, while XQuery's 'x' flag is related to what Spencer's code
does for REG_EXPANDED, it's not the same or a subset. It seems best
to treat XQuery's 'x' as unimplemented for now. Maybe later we can
expand our regex code to offer 'x'-style parsing as a separate option.
While at it, refactor the jsonpath code so that (a) there's only
one copy of the flag transformation logic not two, and (b) the
processing of flags is independent of the order in which the flags
are written.
We need some documentation updates to go with this, but I'll
tackle that separately.
Back-patch to v12 where this code originated.
Discussion: https://postgr.es/m/CAPpHfdvDci4iqNF9fhRkTqhe-5_8HmzeLt56drH%2B_Rv2rNRqfg@mail.gmail.com
Reference: https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#flags
2019-09-17 21:39:51 +02:00
|
|
|
/* Convert flags to what RE_compile_and_cache needs */
|
|
|
|
cflags = jspConvertRegexFlags(v->value.like_regex.flags);
|
|
|
|
|
2019-03-20 09:09:07 +01:00
|
|
|
/* check regex validity */
|
|
|
|
(void) RE_compile_and_cache(cstring_to_text_with_len(pattern->val,
|
|
|
|
pattern->len),
|
|
|
|
cflags, DEFAULT_COLLATION_OID);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
Fix bogus handling of XQuery regex option flags.
The SQL spec defers to XQuery to define what the option flags are
for LIKE_REGEX patterns. XQuery says that:
* 's' allows the dot character to match newlines, which by
default it will not;
* 'm' allows ^ and $ to match at newlines, not only at the
start/end of the whole string.
Thus, these are *not* inverses as they are for the similarly-named
POSIX options, and neither one corresponds to the POSIX 'n' option.
Fortunately, Spencer's library does expose these two behaviors as
separately twiddlable flags, so we just have to fix the mapping from
JSP flag bits to REG flag bits. I also chose to rename the symbol
for 's' to DOTALL, to make it clearer that it's not the inverse
of MLINE.
Also, XQuery says that if the 'q' flag "is used together with the m, s,
or x flag, that flag has no effect". I read this as saying that 'q'
overrides the other flags; whoever wrote our code seems to have read
it backwards.
Lastly, while XQuery's 'x' flag is related to what Spencer's code
does for REG_EXPANDED, it's not the same or a subset. It seems best
to treat XQuery's 'x' as unimplemented for now. Maybe later we can
expand our regex code to offer 'x'-style parsing as a separate option.
While at it, refactor the jsonpath code so that (a) there's only
one copy of the flag transformation logic not two, and (b) the
processing of flags is independent of the order in which the flags
are written.
We need some documentation updates to go with this, but I'll
tackle that separately.
Back-patch to v12 where this code originated.
Discussion: https://postgr.es/m/CAPpHfdvDci4iqNF9fhRkTqhe-5_8HmzeLt56drH%2B_Rv2rNRqfg@mail.gmail.com
Reference: https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#flags
2019-09-17 21:39:51 +02:00
|
|
|
/*
|
|
|
|
* Convert from XQuery regex flags to those recognized by our regex library.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
jspConvertRegexFlags(uint32 xflags)
|
|
|
|
{
|
|
|
|
/* By default, XQuery is very nearly the same as Spencer's AREs */
|
|
|
|
int cflags = REG_ADVANCED;
|
|
|
|
|
|
|
|
/* Ignore-case means the same thing, too, modulo locale issues */
|
|
|
|
if (xflags & JSP_REGEX_ICASE)
|
|
|
|
cflags |= REG_ICASE;
|
|
|
|
|
|
|
|
/* Per XQuery spec, if 'q' is specified then 'm', 's', 'x' are ignored */
|
|
|
|
if (xflags & JSP_REGEX_QUOTE)
|
|
|
|
{
|
|
|
|
cflags &= ~REG_ADVANCED;
|
|
|
|
cflags |= REG_QUOTE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Note that dotall mode is the default in POSIX */
|
|
|
|
if (!(xflags & JSP_REGEX_DOTALL))
|
|
|
|
cflags |= REG_NLSTOP;
|
|
|
|
if (xflags & JSP_REGEX_MLINE)
|
|
|
|
cflags |= REG_NLANCH;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XQuery's 'x' mode is related to Spencer's expanded mode, but it's
|
|
|
|
* not really enough alike to justify treating JSP_REGEX_WSPACE as
|
|
|
|
* REG_EXPANDED. For now we treat 'x' as unimplemented; perhaps in
|
|
|
|
* future we'll modify the regex library to have an option for
|
|
|
|
* XQuery-style ignore-whitespace mode.
|
|
|
|
*/
|
|
|
|
if (xflags & JSP_REGEX_WSPACE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("XQuery \"x\" flag (expanded regular expressions) is not implemented")));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cflags;
|
|
|
|
}
|
|
|
|
|
2019-03-25 13:42:51 +01:00
|
|
|
/*
|
|
|
|
* jsonpath_scan.l is compiled as part of jsonpath_gram.y. Currently, this is
|
|
|
|
* unavoidable because jsonpath_gram does not create a .h file to export its
|
|
|
|
* token symbols. If these files ever grow large enough to be worth compiling
|
|
|
|
* separately, that could be fixed; but for now it seems like useless
|
|
|
|
* complication.
|
|
|
|
*/
|
|
|
|
|
2019-03-20 09:09:07 +01:00
|
|
|
#include "jsonpath_scan.c"
|