PostgreSQL Source Code: src/include/utils/jsonpath.h File Reference (original) (raw)

Go to the source code of this file.

Data Structures
struct JsonPath
struct JsonPathItem
struct JsonPathParseItem
struct JsonPathParseResult
struct JsonPathVariable
Macros
#define JSONPATH_VERSION (0x01)
#define JSONPATH_LAX (0x80000000)
#define JSONPATH_HDRSZ (offsetof(JsonPath, data))
#define PG_GETARG_JSONPATH_P(x) DatumGetJsonPathP(PG_GETARG_DATUM(x))
#define PG_GETARG_JSONPATH_P_COPY(x) DatumGetJsonPathPCopy(PG_GETARG_DATUM(x))
#define PG_RETURN_JSONPATH_P(p) PG_RETURN_POINTER(p)
#define jspIsScalar(type) ((type) >= jpiNull && (type) <= jpiBool)
#define JSP_REGEX_ICASE 0x01 /* i flag, case insensitive */
#define JSP_REGEX_DOTALL 0x02 /* s flag, dot matches newline */
#define JSP_REGEX_MLINE 0x04 /* m flag, ^/$ match at newlines */
#define JSP_REGEX_WSPACE 0x08 /* x flag, ignore whitespace in pattern */
#define JSP_REGEX_QUOTE 0x10 /* q flag, no special characters */
#define jspHasNext(jsp) ((jsp)->nextPos > 0)
Typedefs
typedef enum JsonPathItemType JsonPathItemType
typedef struct JsonPathItem JsonPathItem
typedef struct JsonPathParseItem JsonPathParseItem
typedef struct JsonPathParseResult JsonPathParseResult
typedef struct JsonPathVariable JsonPathVariable
Enumerations
enum JsonPathItemType { jpiNull = jbvNull , jpiString = jbvString , jpiNumeric = jbvNumeric , jpiBool = jbvBool , jpiAnd, jpiOr, jpiNot, jpiIsUnknown, jpiEqual, jpiNotEqual, jpiLess, jpiGreater, jpiLessOrEqual, jpiGreaterOrEqual, jpiAdd, jpiSub, jpiMul, jpiDiv, jpiMod, jpiPlus, jpiMinus, jpiAnyArray, jpiAnyKey, jpiIndexArray, jpiAny, jpiKey, jpiCurrent, jpiRoot, jpiVariable, jpiFilter, jpiExists, jpiType, jpiSize, jpiAbs, jpiFloor, jpiCeiling, jpiDouble, jpiDatetime, jpiKeyValue, jpiSubscript, jpiLast, jpiStartsWith, jpiLikeRegex, jpiBigint, jpiBoolean, jpiDate, jpiDecimal, jpiInteger, jpiNumber, jpiStringFunc, jpiTime, jpiTimeTz, jpiTimestamp, jpiTimestampTz }
Functions
static JsonPath * DatumGetJsonPathP (Datum d)
static JsonPath * DatumGetJsonPathPCopy (Datum d)
void jspInit (JsonPathItem *v, JsonPath *js)
void jspInitByBuffer (JsonPathItem *v, char *base, int32 pos)
bool jspGetNext (JsonPathItem *v, JsonPathItem *a)
void jspGetArg (JsonPathItem *v, JsonPathItem *a)
void jspGetLeftArg (JsonPathItem *v, JsonPathItem *a)
void jspGetRightArg (JsonPathItem *v, JsonPathItem *a)
Numeric jspGetNumeric (JsonPathItem *v)
bool jspGetBool (JsonPathItem *v)
char * jspGetString (JsonPathItem *v, int32 *len)
bool jspGetArraySubscript (JsonPathItem *v, JsonPathItem *from, JsonPathItem *to, int i)
bool jspIsMutable (JsonPath *path, List *varnames, List *varexprs)
const char * jspOperationName (JsonPathItemType type)
JsonPathParseResult * parsejsonpath (const char *str, int len, struct Node *escontext)
bool jspConvertRegexFlags (uint32 xflags, int *result, struct Node *escontext)
bool JsonPathExists (Datum jb, JsonPath *jp, bool *error, List *vars)
Datum JsonPathQuery (Datum jb, JsonPath *jp, JsonWrapper wrapper, bool *empty, bool *error, List *vars, const char *column_name)
JsonbValue * JsonPathValue (Datum jb, JsonPath *jp, bool *empty, bool *error, List *vars, const char *column_name)

JSONPATH_HDRSZ

JSONPATH_LAX

#define JSONPATH_LAX (0x80000000)

JSONPATH_VERSION

#define JSONPATH_VERSION (0x01)

JSP_REGEX_DOTALL

#define JSP_REGEX_DOTALL 0x02 /* s flag, dot matches newline */

JSP_REGEX_ICASE

#define JSP_REGEX_ICASE 0x01 /* i flag, case insensitive */

JSP_REGEX_MLINE

#define JSP_REGEX_MLINE 0x04 /* m flag, ^/$ match at newlines */

JSP_REGEX_QUOTE

#define JSP_REGEX_QUOTE 0x10 /* q flag, no special characters */

JSP_REGEX_WSPACE

#define JSP_REGEX_WSPACE 0x08 /* x flag, ignore whitespace in pattern */

jspHasNext

| #define jspHasNext | ( | | jsp | ) | ((jsp)->nextPos > 0) | | ------------------ | - | | --- | - | -------------------- |

jspIsScalar

PG_GETARG_JSONPATH_P

PG_GETARG_JSONPATH_P_COPY

PG_RETURN_JSONPATH_P

JsonPathItem

JsonPathItemType

JsonPathParseItem

JsonPathParseResult

JsonPathVariable

JsonPathItemType

Enumerator
jpiNull
jpiString
jpiNumeric
jpiBool
jpiAnd
jpiOr
jpiNot
jpiIsUnknown
jpiEqual
jpiNotEqual
jpiLess
jpiGreater
jpiLessOrEqual
jpiGreaterOrEqual
jpiAdd
jpiSub
jpiMul
jpiDiv
jpiMod
jpiPlus
jpiMinus
jpiAnyArray
jpiAnyKey
jpiIndexArray
jpiAny
jpiKey
jpiCurrent
jpiRoot
jpiVariable
jpiFilter
jpiExists
jpiType
jpiSize
jpiAbs
jpiFloor
jpiCeiling
jpiDouble
jpiDatetime
jpiKeyValue
jpiSubscript
jpiLast
jpiStartsWith
jpiLikeRegex
jpiBigint
jpiBoolean
jpiDate
jpiDecimal
jpiInteger
jpiNumber
jpiStringFunc
jpiTime
jpiTimeTz
jpiTimestamp
jpiTimestampTz

Definition at line 62 of file jsonpath.h.

63{

67 jpiBool = jbvBool,

68 jpiAnd,

69 jpiOr,

70 jpiNot,

74 jpiLess,

78 jpiAdd,

79 jpiSub,

80 jpiMul,

81 jpiDiv,

82 jpiMod,

94 jpiExists,

95 jpiType,

96 jpiSize,

97 jpiAbs,

98 jpiFloor,

99 jpiCeiling,

100 jpiDouble,

101 jpiDatetime,

102 jpiKeyValue,

103 jpiSubscript,

104 jpiLast,

107 jpiBigint,

108 jpiBoolean,

109 jpiDate,

110 jpiDecimal,

111 jpiInteger,

112 jpiNumber,

114 jpiTime,

115 jpiTimeTz,

DatumGetJsonPathP()

DatumGetJsonPathPCopy()

JsonPathExists()

Definition at line 3887 of file jsonpath_exec.c.

3888{

3890

3894

3896

3899

3900 return res == jperOk;

3901}

Assert(PointerIsAligned(start, uint64))

static Jsonb * DatumGetJsonbP(Datum d)

static JsonbValue * GetJsonPathVar(void *cxt, char *varName, int varNameLen, JsonbValue *baseObject, int *baseObjectId)

#define jperIsError(jper)

static int CountJsonPathVars(void *cxt)

static JsonPathExecResult executeJsonPath(JsonPath *path, void *vars, JsonPathGetVarCallback getVar, JsonPathCountVarsCallback countVars, Jsonb *json, bool throwErrors, JsonValueList *result, bool useTz)

References Assert(), CountJsonPathVars(), DatumGetJsonbP(), error(), executeJsonPath(), GetJsonPathVar(), jperIsError, and jperOk.

Referenced by ExecEvalJsonExprPath().

JsonPathQuery()

Definition at line 3910 of file jsonpath_exec.c.

3913{

3915 bool wrap;

3918 int count;

3919

3925 {

3927 *empty = false;

3928 return (Datum) 0;

3929 }

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3951 if (singleton == NULL)

3952 wrap = false;

3954 wrap = false;

3956 wrap = true;

3958 wrap = count > 1;

3959 else

3960 {

3961 elog(ERROR, "unrecognized json wrapper %d", (int) wrapper);

3962 wrap = false;

3963 }

3964

3965 if (wrap)

3967

3968

3969 if (count > 1)

3970 {

3972 {

3974 return (Datum) 0;

3975 }

3976

3977 if (column_name)

3979 (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM),

3980 errmsg("JSON path expression for column \"%s\" must return single item when no wrapper is requested",

3981 column_name),

3982 errhint("Use the WITH WRAPPER clause to wrap SQL/JSON items into an array.")));

3983 else

3985 (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM),

3986 errmsg("JSON path expression in JSON_QUERY must return single item when no wrapper is requested"),

3987 errhint("Use the WITH WRAPPER clause to wrap SQL/JSON items into an array.")));

3988 }

3989

3990 if (singleton)

3992

3993 *empty = true;

3995}

int errhint(const char *fmt,...)

int errcode(int sqlerrcode)

int errmsg(const char *fmt,...)

#define ereport(elevel,...)

static Datum JsonbPGetDatum(const Jsonb *p)

Jsonb * JsonbValueToJsonb(JsonbValue *val)

static JsonbValue * wrapItemsInArray(const JsonValueList *items)

static int JsonValueListLength(const JsonValueList *jvl)

static JsonbValue * JsonValueListHead(JsonValueList *jvl)

static Datum PointerGetDatum(const void *X)

References Assert(), CountJsonPathVars(), DatumGetJsonbP(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, error(), executeJsonPath(), GetJsonPathVar(), jperIsError, JsonbPGetDatum(), JsonbValueToJsonb(), JsonValueListHead(), JsonValueListLength(), JSW_CONDITIONAL, JSW_NONE, JSW_UNCONDITIONAL, JSW_UNSPEC, PointerGetDatum(), and wrapItemsInArray().

Referenced by ExecEvalJsonExprPath().

JsonPathValue()

Definition at line 4004 of file jsonpath_exec.c.

4006{

4010 int count;

4011

4014 error, &found, true);

4015

4017

4019 {

4021 *empty = false;

4022 return NULL;

4023 }

4024

4026

4027 *empty = (count == 0);

4028

4029 if (*empty)

4030 return NULL;

4031

4032

4033 if (count > 1)

4034 {

4036 {

4038 return NULL;

4039 }

4040

4041 if (column_name)

4043 (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM),

4044 errmsg("JSON path expression for column \"%s\" must return single scalar item",

4045 column_name)));

4046 else

4048 (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM),

4049 errmsg("JSON path expression in JSON_VALUE must return single scalar item")));

4050 }

4051

4055

4056

4058 {

4060 {

4062 return NULL;

4063 }

4064

4065 if (column_name)

4067 (errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED),

4068 errmsg("JSON path expression for column \"%s\" must return single scalar item",

4069 column_name)));

4070 else

4072 (errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED),

4073 errmsg("JSON path expression in JSON_VALUE must return single scalar item")));

4074 }

4075

4077 return NULL;

4078

4079 return res;

4080}

#define PG_USED_FOR_ASSERTS_ONLY

bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res)

#define JsonContainerIsScalar(jc)

#define IsAJsonbScalar(jsonbval)

References Assert(), CountJsonPathVars(), DatumGetJsonbP(), ereport, errcode(), errmsg(), ERROR, error(), executeJsonPath(), GetJsonPathVar(), IsAJsonbScalar, jbvBinary, jbvNull, jperIsError, JsonbExtractScalar(), JsonContainerIsScalar, JsonValueListHead(), JsonValueListLength(), PG_USED_FOR_ASSERTS_ONLY, JsonbValue::type, and JsonbValue::val.

Referenced by ExecEvalJsonExprPath().

jspConvertRegexFlags()

bool jspConvertRegexFlags ( uint32 xflags,
int * result,
struct Node * escontext
)

jspGetArg()

Definition at line 1081 of file jsonpath.c.

1082{

1094

1096}

void jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)

union JsonPathItem::@147 content

References a, JsonPathItem::arg, Assert(), JsonPathItem::base, JsonPathItem::content, jpiDatetime, jpiExists, jpiFilter, jpiIsUnknown, jpiMinus, jpiNot, jpiPlus, jpiTime, jpiTimestamp, jpiTimestampTz, jpiTimeTz, jspInitByBuffer(), and JsonPathItem::type.

Referenced by executeBoolItem(), executeDateTimeMethod(), executeItemOptUnwrapTarget(), executeUnaryArithmExpr(), extract_jsp_bool_expr(), extract_jsp_path_expr_nodes(), jspIsMutableWalker(), and printJsonPathItem().

jspGetArraySubscript()

Definition at line 1238 of file jsonpath.c.

1240{

1242

1244

1246 return false;

1247

1249

1250 return true;

1251}

struct JsonPathItem::@147::@149::@153 * elems

struct JsonPathItem::@147::@149 array

References JsonPathItem::array, Assert(), JsonPathItem::base, JsonPathItem::content, JsonPathItem::elems, i, jpiIndexArray, jspInitByBuffer(), and JsonPathItem::type.

Referenced by executeItemOptUnwrapTarget(), jspIsMutableWalker(), and printJsonPathItem().

jspGetBool()

jspGetLeftArg()

Definition at line 1166 of file jsonpath.c.

1167{

1183

1185}

struct JsonPathItem::@147::@148 args

References a, JsonPathItem::args, Assert(), JsonPathItem::base, JsonPathItem::content, jpiAdd, jpiAnd, jpiDecimal, jpiDiv, jpiEqual, jpiGreater, jpiGreaterOrEqual, jpiLess, jpiLessOrEqual, jpiMod, jpiMul, jpiNotEqual, jpiOr, jpiStartsWith, jpiSub, jspInitByBuffer(), JsonPathItem::left, and JsonPathItem::type.

Referenced by executeBinaryArithmExpr(), executeBoolItem(), executeItemOptUnwrapTarget(), extract_jsp_bool_expr(), jspIsMutableWalker(), and printJsonPathItem().

jspGetNext()

Definition at line 1099 of file jsonpath.c.

1100{

1102 {

1156

1157 if (a)

1159 return true;

1160 }

1161

1162 return false;

1163}

References a, Assert(), JsonPathItem::base, jpiAbs, jpiAdd, jpiAnd, jpiAny, jpiAnyArray, jpiAnyKey, jpiBigint, jpiBool, jpiBoolean, jpiCeiling, jpiCurrent, jpiDate, jpiDatetime, jpiDecimal, jpiDiv, jpiDouble, jpiEqual, jpiExists, jpiFilter, jpiFloor, jpiGreater, jpiGreaterOrEqual, jpiIndexArray, jpiInteger, jpiIsUnknown, jpiKey, jpiKeyValue, jpiLast, jpiLess, jpiLessOrEqual, jpiLikeRegex, jpiMinus, jpiMod, jpiMul, jpiNot, jpiNotEqual, jpiNull, jpiNumber, jpiNumeric, jpiOr, jpiPlus, jpiRoot, jpiSize, jpiStartsWith, jpiString, jpiStringFunc, jpiSub, jpiTime, jpiTimestamp, jpiTimestampTz, jpiTimeTz, jpiType, jpiVariable, jspHasNext, jspInitByBuffer(), JsonPathItem::nextPos, and JsonPathItem::type.

Referenced by appendBoolResult(), executeBinaryArithmExpr(), executeDateTimeMethod(), executeItemOptUnwrapTarget(), executeKeyValueMethod(), executeNextItem(), executeNumericItemMethod(), executeUnaryArithmExpr(), extract_jsp_path_expr_nodes(), jspIsMutableWalker(), and printJsonPathItem().

jspGetNumeric()

jspGetRightArg()

Definition at line 1188 of file jsonpath.c.

References a, JsonPathItem::args, Assert(), JsonPathItem::base, JsonPathItem::content, jpiAdd, jpiAnd, jpiDecimal, jpiDiv, jpiEqual, jpiGreater, jpiGreaterOrEqual, jpiLess, jpiLessOrEqual, jpiMod, jpiMul, jpiNotEqual, jpiOr, jpiStartsWith, jpiSub, jspInitByBuffer(), JsonPathItem::right, and JsonPathItem::type.

Referenced by executeBinaryArithmExpr(), executeBoolItem(), executeItemOptUnwrapTarget(), extract_jsp_bool_expr(), jspIsMutableWalker(), and printJsonPathItem().

jspGetString()

Definition at line 1226 of file jsonpath.c.

1227{

1231

1232 if (len)

1235}

References Assert(), JsonPathItem::content, JsonPathItem::data, JsonPathItem::datalen, jpiKey, jpiString, jpiVariable, len, JsonPathItem::type, and JsonPathItem::value.

Referenced by executeDateTimeMethod(), executeItemOptUnwrapTarget(), getJsonPathItem(), getJsonPathVariable(), jsonb_ops__add_path_item(), jsonb_path_ops__add_path_item(), jspIsMutableWalker(), and printJsonPathItem().

jspInit()

jspInitByBuffer()

Definition at line 990 of file jsonpath.c.

991{

992 v->base = base + pos;

993

995 pos = INTALIGN((uintptr_t) (base + pos)) - (uintptr_t) base;

997

998 switch (v->type)

999 {

1019 break;

1024

1028 break;

1046 break;

1059 break;

1064 break;

1068 break;

1074 break;

1075 default:

1076 elog(ERROR, "unrecognized jsonpath item type: %d", v->type);

1077 }

1078}

#define read_byte(v, b, p)

#define read_int32_n(v, b, p, n)

#define read_int32(v, b, p)

struct JsonPathItem::@147::@150 anybounds

struct JsonPathItem::@147::@152 like_regex

References JsonPathItem::anybounds, JsonPathItem::arg, JsonPathItem::args, JsonPathItem::array, JsonPathItem::base, JsonPathItem::content, JsonPathItem::data, JsonPathItem::datalen, JsonPathItem::elems, elog, ERROR, JsonPathItem::expr, JsonPathItem::first, JsonPathItem::flags, INTALIGN, jpiAbs, jpiAdd, jpiAnd, jpiAny, jpiAnyArray, jpiAnyKey, jpiBigint, jpiBool, jpiBoolean, jpiCeiling, jpiCurrent, jpiDate, jpiDatetime, jpiDecimal, jpiDiv, jpiDouble, jpiEqual, jpiExists, jpiFilter, jpiFloor, jpiGreater, jpiGreaterOrEqual, jpiIndexArray, jpiInteger, jpiIsUnknown, jpiKey, jpiKeyValue, jpiLast, jpiLess, jpiLessOrEqual, jpiLikeRegex, jpiMinus, jpiMod, jpiMul, jpiNot, jpiNotEqual, jpiNull, jpiNumber, jpiNumeric, jpiOr, jpiPlus, jpiRoot, jpiSize, jpiStartsWith, jpiString, jpiStringFunc, jpiSub, jpiTime, jpiTimestamp, jpiTimestampTz, jpiTimeTz, jpiType, jpiVariable, JsonPathItem::last, JsonPathItem::left, JsonPathItem::like_regex, JsonPathItem::nelems, JsonPathItem::nextPos, JsonPathItem::pattern, JsonPathItem::patternlen, read_byte, read_int32, read_int32_n, JsonPathItem::right, JsonPathItem::type, and JsonPathItem::value.

Referenced by executeBoolItem(), jspGetArg(), jspGetArraySubscript(), jspGetLeftArg(), jspGetNext(), jspGetRightArg(), jspInit(), jspIsMutableWalker(), and printJsonPathItem().

jspIsMutable()

Definition at line 1280 of file jsonpath.c.

1281{

1284

1289 cxt.mutable = false;

1290

1293

1294 return cxt.mutable;

1295}

static enum JsonPathDatatypeStatus jspIsMutableWalker(JsonPathItem *jpi, struct JsonPathMutableContext *cxt)

void jspInit(JsonPathItem *v, JsonPath *js)

References JsonPathMutableContext::current, JsonPath::header, jpdsNonDateTime, JSONPATH_LAX, jspInit(), jspIsMutableWalker(), JsonPathMutableContext::lax, JsonPathMutableContext::mutable, JsonPathMutableContext::varexprs, and JsonPathMutableContext::varnames.

Referenced by contain_mutable_functions_walker().

jspOperationName()

Definition at line 843 of file jsonpath.c.

844{

845 switch (type)

846 {

848 return "&&";

850 return "||";

852 return "==";

854 return "!=";

856 return "<";

858 return ">";

860 return "<=";

862 return ">=";

865 return "+";

868 return "-";

870 return "*";

872 return "/";

874 return "%";

876 return "type";

878 return "size";

880 return "abs";

882 return "floor";

884 return "ceiling";

886 return "double";

888 return "datetime";

890 return "keyvalue";

892 return "starts with";

894 return "like_regex";

896 return "bigint";

898 return "boolean";

900 return "date";

902 return "decimal";

904 return "integer";

906 return "number";

908 return "string";

910 return "time";

912 return "time_tz";

914 return "timestamp";

916 return "timestamp_tz";

917 default:

918 elog(ERROR, "unrecognized jsonpath item type: %d", type);

919 return NULL;

920 }

921}

References elog, ERROR, jpiAbs, jpiAdd, jpiAnd, jpiBigint, jpiBoolean, jpiCeiling, jpiDate, jpiDatetime, jpiDecimal, jpiDiv, jpiDouble, jpiEqual, jpiFloor, jpiGreater, jpiGreaterOrEqual, jpiInteger, jpiKeyValue, jpiLess, jpiLessOrEqual, jpiLikeRegex, jpiMinus, jpiMod, jpiMul, jpiNotEqual, jpiNumber, jpiOr, jpiPlus, jpiSize, jpiStartsWith, jpiStringFunc, jpiSub, jpiTime, jpiTimestamp, jpiTimestampTz, jpiTimeTz, jpiType, and type.

Referenced by executeBinaryArithmExpr(), executeDateTimeMethod(), executeItemOptUnwrapTarget(), executeKeyValueMethod(), executeNumericItemMethod(), executeUnaryArithmExpr(), and printJsonPathItem().

parsejsonpath()

Definition at line 528 of file jsonpath_scan.l.

529{

533

534 if (jsonpath_yylex_init(&scanner) != 0)

535 elog(ERROR, "yylex_init() failed: %m");

536

537 yyset_extra(&yyext, scanner);

538

539 if (len <= 0)

541

542 jsonpath_yy_scan_bytes(str, len, scanner);

543

545 jsonpath_yyerror(NULL, escontext, scanner, "invalid input");

546

547 jsonpath_yylex_destroy(scanner);

548

550}

int jsonpath_yyparse(JsonPathParseResult **result, struct Node *escontext, yyscan_t yyscanner)

void jsonpath_yyerror(JsonPathParseResult **result, struct Node *escontext, yyscan_t yyscanner, const char *message)

References elog, ERROR, jsonpath_yyerror(), jsonpath_yyparse(), len, parseresult, and str.

Referenced by jsonPathFromCstring().

JsonbTableRoutine