PostgreSQL Source Code  git master
jsonpath.h File Reference
#include "fmgr.h"
#include "executor/tablefunc.h"
#include "nodes/pg_list.h"
#include "nodes/primnodes.h"
#include "utils/jsonb.h"
#include "utils/jsonfuncs.h"
Include dependency graph for jsonpath.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  JsonPath
 
struct  JsonPathItem
 
struct  JsonPathParseItem
 
struct  JsonPathParseResult
 
struct  JsonPathVariableEvalContext
 

Macros

#define JSONPATH_VERSION   (0x01)
 
#define JSONPATH_LAX   (0x80000000)
 
#define JSONPATH_HDRSZ   (offsetof(JsonPath, data))
 
#define DatumGetJsonPathP(d)   ((JsonPath *) DatumGetPointer(PG_DETOAST_DATUM(d)))
 
#define DatumGetJsonPathPCopy(d)   ((JsonPath *) DatumGetPointer(PG_DETOAST_DATUM_COPY(d)))
 
#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 JsonPathVariableEvalContext JsonPathVariableEvalContext
 

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
}
 

Functions

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)
 
JsonPathParseResultparsejsonpath (const char *str, int len)
 
int jspConvertRegexFlags (uint32 xflags)
 
void JsonItemFromDatum (Datum val, Oid typid, int32 typmod, JsonbValue *res)
 
bool JsonPathExists (Datum jb, JsonPath *path, List *vars, bool *error)
 
Datum JsonPathQuery (Datum jb, JsonPath *jp, JsonWrapper wrapper, bool *empty, bool *error, List *vars)
 
JsonbValueJsonPathValue (Datum jb, JsonPath *jp, bool *empty, bool *error, List *vars)
 
int EvalJsonPathVar (void *vars, char *varName, int varNameLen, JsonbValue *val, JsonbValue *baseObject)
 

Variables

PGDLLIMPORT const TableFuncRoutine JsonbTableRoutine
 

Macro Definition Documentation

◆ DatumGetJsonPathP

#define DatumGetJsonPathP (   d)    ((JsonPath *) DatumGetPointer(PG_DETOAST_DATUM(d)))

Definition at line 35 of file jsonpath.h.

◆ DatumGetJsonPathPCopy

#define DatumGetJsonPathPCopy (   d)    ((JsonPath *) DatumGetPointer(PG_DETOAST_DATUM_COPY(d)))

Definition at line 36 of file jsonpath.h.

◆ JSONPATH_HDRSZ

#define JSONPATH_HDRSZ   (offsetof(JsonPath, data))

Definition at line 33 of file jsonpath.h.

◆ JSONPATH_LAX

#define JSONPATH_LAX   (0x80000000)

Definition at line 32 of file jsonpath.h.

◆ JSONPATH_VERSION

#define JSONPATH_VERSION   (0x01)

Definition at line 31 of file jsonpath.h.

◆ JSP_REGEX_DOTALL

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

Definition at line 95 of file jsonpath.h.

◆ JSP_REGEX_ICASE

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

Definition at line 94 of file jsonpath.h.

◆ JSP_REGEX_MLINE

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

Definition at line 96 of file jsonpath.h.

◆ JSP_REGEX_QUOTE

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

Definition at line 98 of file jsonpath.h.

◆ JSP_REGEX_WSPACE

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

Definition at line 97 of file jsonpath.h.

◆ jspHasNext

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

Definition at line 167 of file jsonpath.h.

◆ jspIsScalar

#define jspIsScalar (   type)    ((type) >= jpiNull && (type) <= jpiBool)

Definition at line 41 of file jsonpath.h.

◆ PG_GETARG_JSONPATH_P

#define PG_GETARG_JSONPATH_P (   x)    DatumGetJsonPathP(PG_GETARG_DATUM(x))

Definition at line 37 of file jsonpath.h.

◆ PG_GETARG_JSONPATH_P_COPY

#define PG_GETARG_JSONPATH_P_COPY (   x)    DatumGetJsonPathPCopy(PG_GETARG_DATUM(x))

Definition at line 38 of file jsonpath.h.

◆ PG_RETURN_JSONPATH_P

#define PG_RETURN_JSONPATH_P (   p)    PG_RETURN_POINTER(p)

Definition at line 39 of file jsonpath.h.

Typedef Documentation

◆ JsonPathItem

typedef struct JsonPathItem JsonPathItem

◆ JsonPathItemType

◆ JsonPathParseItem

Definition at line 182 of file jsonpath.h.

◆ JsonPathParseResult

◆ JsonPathVariableEvalContext

Enumeration Type Documentation

◆ 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 

Definition at line 46 of file jsonpath.h.

47 {
48  jpiNull = jbvNull, /* NULL literal */
49  jpiString = jbvString, /* string literal */
50  jpiNumeric = jbvNumeric, /* numeric literal */
51  jpiBool = jbvBool, /* boolean literal: TRUE or FALSE */
52  jpiAnd, /* predicate && predicate */
53  jpiOr, /* predicate || predicate */
54  jpiNot, /* ! predicate */
55  jpiIsUnknown, /* (predicate) IS UNKNOWN */
56  jpiEqual, /* expr == expr */
57  jpiNotEqual, /* expr != expr */
58  jpiLess, /* expr < expr */
59  jpiGreater, /* expr > expr */
60  jpiLessOrEqual, /* expr <= expr */
61  jpiGreaterOrEqual, /* expr >= expr */
62  jpiAdd, /* expr + expr */
63  jpiSub, /* expr - expr */
64  jpiMul, /* expr * expr */
65  jpiDiv, /* expr / expr */
66  jpiMod, /* expr % expr */
67  jpiPlus, /* + expr */
68  jpiMinus, /* - expr */
69  jpiAnyArray, /* [*] */
70  jpiAnyKey, /* .* */
71  jpiIndexArray, /* [subscript, ...] */
72  jpiAny, /* .** */
73  jpiKey, /* .key */
74  jpiCurrent, /* @ */
75  jpiRoot, /* $ */
76  jpiVariable, /* $variable */
77  jpiFilter, /* ? (predicate) */
78  jpiExists, /* EXISTS (expr) predicate */
79  jpiType, /* .type() item method */
80  jpiSize, /* .size() item method */
81  jpiAbs, /* .abs() item method */
82  jpiFloor, /* .floor() item method */
83  jpiCeiling, /* .ceiling() item method */
84  jpiDouble, /* .double() item method */
85  jpiDatetime, /* .datetime() item method */
86  jpiKeyValue, /* .keyvalue() item method */
87  jpiSubscript, /* array subscript: 'expr' or 'expr TO expr' */
88  jpiLast, /* LAST array subscript */
89  jpiStartsWith, /* STARTS WITH predicate */
90  jpiLikeRegex, /* LIKE_REGEX predicate */
@ jbvNumeric
Definition: jsonb.h:238
@ jbvBool
Definition: jsonb.h:239
@ jbvNull
Definition: jsonb.h:236
@ jbvString
Definition: jsonb.h:237
JsonPathItemType
Definition: jsonpath.h:47
@ jpiAdd
Definition: jsonpath.h:62
@ jpiString
Definition: jsonpath.h:49
@ jpiAbs
Definition: jsonpath.h:81
@ jpiIndexArray
Definition: jsonpath.h:71
@ jpiAny
Definition: jsonpath.h:72
@ jpiDatetime
Definition: jsonpath.h:85
@ jpiBool
Definition: jsonpath.h:51
@ jpiType
Definition: jsonpath.h:79
@ jpiFloor
Definition: jsonpath.h:82
@ jpiAnyArray
Definition: jsonpath.h:69
@ jpiExists
Definition: jsonpath.h:78
@ jpiSize
Definition: jsonpath.h:80
@ jpiSub
Definition: jsonpath.h:63
@ jpiSubscript
Definition: jsonpath.h:87
@ jpiNotEqual
Definition: jsonpath.h:57
@ jpiMul
Definition: jsonpath.h:64
@ jpiVariable
Definition: jsonpath.h:76
@ jpiNot
Definition: jsonpath.h:54
@ jpiGreaterOrEqual
Definition: jsonpath.h:61
@ jpiPlus
Definition: jsonpath.h:67
@ jpiDouble
Definition: jsonpath.h:84
@ jpiGreater
Definition: jsonpath.h:59
@ jpiAnd
Definition: jsonpath.h:52
@ jpiStartsWith
Definition: jsonpath.h:89
@ jpiOr
Definition: jsonpath.h:53
@ jpiMod
Definition: jsonpath.h:66
@ jpiLikeRegex
Definition: jsonpath.h:90
@ jpiRoot
Definition: jsonpath.h:75
@ jpiFilter
Definition: jsonpath.h:77
@ jpiNull
Definition: jsonpath.h:48
@ jpiLess
Definition: jsonpath.h:58
@ jpiCurrent
Definition: jsonpath.h:74
@ jpiEqual
Definition: jsonpath.h:56
@ jpiKey
Definition: jsonpath.h:73
@ jpiDiv
Definition: jsonpath.h:65
@ jpiLast
Definition: jsonpath.h:88
@ jpiMinus
Definition: jsonpath.h:68
@ jpiLessOrEqual
Definition: jsonpath.h:60
@ jpiCeiling
Definition: jsonpath.h:83
@ jpiIsUnknown
Definition: jsonpath.h:55
@ jpiKeyValue
Definition: jsonpath.h:86
@ jpiNumeric
Definition: jsonpath.h:50
@ jpiAnyKey
Definition: jsonpath.h:70

Function Documentation

◆ EvalJsonPathVar()

int EvalJsonPathVar ( void *  vars,
char *  varName,
int  varNameLen,
JsonbValue val,
JsonbValue baseObject 
)

Definition at line 4695 of file execExprInterp.c.

4697 {
4698  JsonPathVariableEvalContext *var = NULL;
4699  List *vars = cxt;
4700  ListCell *lc;
4701  int id = 1;
4702 
4703  if (!varName)
4704  return list_length(vars);
4705 
4706  foreach(lc, vars)
4707  {
4708  var = lfirst(lc);
4709 
4710  if (!strncmp(var->name, varName, varNameLen))
4711  break;
4712 
4713  var = NULL;
4714  id++;
4715  }
4716 
4717  if (!var)
4718  return -1;
4719 
4720  if (!var->evaluated)
4721  {
4722  MemoryContext oldcxt = var->mcxt ?
4723  MemoryContextSwitchTo(var->mcxt) : NULL;
4724 
4725  var->value = ExecEvalExpr(var->estate, var->econtext, &var->isnull);
4726  var->evaluated = true;
4727 
4728  if (oldcxt)
4729  MemoryContextSwitchTo(oldcxt);
4730  }
4731 
4732  if (var->isnull)
4733  {
4734  val->type = jbvNull;
4735  return 0;
4736  }
4737 
4738  JsonItemFromDatum(var->value, var->typid, var->typmod, val);
4739 
4740  *baseObject = *val;
4741  return id;
4742 }
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:320
long val
Definition: informix.c:664
void JsonItemFromDatum(Datum val, Oid typid, int32 typmod, JsonbValue *res)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define lfirst(lc)
Definition: pg_list.h:170
static int list_length(const List *l)
Definition: pg_list.h:150
struct ExprState * estate
Definition: jsonpath.h:266
struct ExprContext * econtext
Definition: jsonpath.h:265
Definition: pg_list.h:52
Definition: regcomp.c:238

References ExecEvalExpr(), jbvNull, JsonItemFromDatum(), lfirst, list_length(), MemoryContextSwitchTo(), val, and ExprEvalStep::var.

Referenced by JsonPathExists(), JsonPathQuery(), JsonPathValue(), and JsonTableResetContextItem().

◆ JsonItemFromDatum()

void JsonItemFromDatum ( Datum  val,
Oid  typid,
int32  typmod,
JsonbValue res 
)

Definition at line 3062 of file jsonpath_exec.c.

3063 {
3064  switch (typid)
3065  {
3066  case BOOLOID:
3067  res->type = jbvBool;
3068  res->val.boolean = DatumGetBool(val);
3069  break;
3070  case NUMERICOID:
3072  break;
3073  case INT2OID:
3075  break;
3076  case INT4OID:
3078  break;
3079  case INT8OID:
3081  break;
3082  case FLOAT4OID:
3084  break;
3085  case FLOAT8OID:
3087  break;
3088  case TEXTOID:
3089  case VARCHAROID:
3090  res->type = jbvString;
3091  res->val.string.val = VARDATA_ANY(val);
3092  res->val.string.len = VARSIZE_ANY_EXHDR(val);
3093  break;
3094  case DATEOID:
3095  case TIMEOID:
3096  case TIMETZOID:
3097  case TIMESTAMPOID:
3098  case TIMESTAMPTZOID:
3099  res->type = jbvDatetime;
3100  res->val.datetime.value = val;
3101  res->val.datetime.typid = typid;
3102  res->val.datetime.typmod = typmod;
3103  res->val.datetime.tz = 0;
3104  break;
3105  case JSONBOID:
3106  {
3107  JsonbValue *jbv = res;
3108  Jsonb *jb = DatumGetJsonbP(val);
3109 
3110  if (JsonContainerIsScalar(&jb->root))
3111  {
3113 
3114  res = JsonbExtractScalar(&jb->root, jbv);
3115  Assert(res);
3116  }
3117  else
3118  JsonbInitBinary(jbv, jb);
3119  break;
3120  }
3121  case JSONOID:
3122  {
3123  text *txt = DatumGetTextP(val);
3124  char *str = text_to_cstring(txt);
3125  Jsonb *jb =
3127  CStringGetDatum(str)));
3128 
3129  pfree(str);
3130 
3131  JsonItemFromDatum(JsonbPGetDatum(jb), JSONBOID, -1, res);
3132  break;
3133  }
3134  default:
3135  ereport(ERROR,
3136  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3137  errmsg("only bool, numeric, and text types could be "
3138  "casted to supported jsonpath types.")));
3139  }
3140 }
Datum float8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4392
Datum float4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4485
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4215
Datum int2_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4343
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4303
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
#define DatumGetTextP(X)
Definition: fmgr.h:332
Datum jsonb_in(PG_FUNCTION_ARGS)
Definition: jsonb.c:76
bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res)
Definition: jsonb.c:2027
@ jbvDatetime
Definition: jsonb.h:252
#define JsonContainerIsScalar(jc)
Definition: jsonb.h:215
#define JsonbPGetDatum(p)
Definition: jsonb.h:73
#define DatumGetJsonbP(d)
Definition: jsonb.h:71
static void JsonbValueInitNumericDatum(JsonbValue *jbv, Datum num)
static JsonbValue * JsonbInitBinary(JsonbValue *jbv, Jsonb *jb)
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1175
#define CStringGetDatum(X)
Definition: postgres.h:622
#define DatumGetBool(X)
Definition: postgres.h:437
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: jsonb.h:221
JsonbContainer root
Definition: jsonb.h:223
Definition: c.h:633
char * text_to_cstring(const text *t)
Definition: varlena.c:221

References Assert(), CStringGetDatum, DatumGetBool, DatumGetJsonbP, DatumGetTextP, DirectFunctionCall1, ereport, errcode(), errmsg(), ERROR, float4_numeric(), float8_numeric(), int2_numeric(), int4_numeric(), int8_numeric(), jbvBool, jbvDatetime, jbvString, jsonb_in(), JsonbExtractScalar(), JsonbInitBinary(), JsonbPGetDatum, JsonbValueInitNumericDatum(), JsonContainerIsScalar, pfree(), PG_USED_FOR_ASSERTS_ONLY, res, Jsonb::root, generate_unaccent_rules::str, text_to_cstring(), val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by EvalJsonPathVar().

◆ JsonPathExists()

bool JsonPathExists ( Datum  jb,
JsonPath path,
List vars,
bool error 
)

Definition at line 2904 of file jsonpath_exec.c.

2905 {
2907  DatumGetJsonbP(jb), !error, NULL,
2908  true);
2909 
2910  Assert(error || !jperIsError(res));
2911 
2912  if (error && jperIsError(res))
2913  *error = true;
2914 
2915  return res == jperOk;
2916 }
int EvalJsonPathVar(void *cxt, char *varName, int varNameLen, JsonbValue *val, JsonbValue *baseObject)
static JsonPathExecResult executeJsonPath(JsonPath *path, void *vars, JsonPathVarCallback getVar, Jsonb *json, bool throwErrors, JsonValueList *result, bool useTz)
#define jperIsError(jper)
JsonPathExecResult
@ jperOk
static void error(void)
Definition: sql-dyntest.c:147

References Assert(), DatumGetJsonbP, error(), EvalJsonPathVar(), executeJsonPath(), jperIsError, jperOk, and res.

Referenced by ExecEvalJsonExpr().

◆ JsonPathQuery()

Datum JsonPathQuery ( Datum  jb,
JsonPath jp,
JsonWrapper  wrapper,
bool empty,
bool error,
List vars 
)

Definition at line 2919 of file jsonpath_exec.c.

2921 {
2922  JsonbValue *first;
2923  bool wrap;
2924  JsonValueList found = {0};
2926  int count;
2927 
2929  &found, true);
2930 
2931  Assert(error || !jperIsError(res));
2932 
2933  if (error && jperIsError(res))
2934  {
2935  *error = true;
2936  *empty = false;
2937  return (Datum) 0;
2938  }
2939 
2940  count = JsonValueListLength(&found);
2941 
2942  first = count ? JsonValueListHead(&found) : NULL;
2943 
2944  if (!first)
2945  wrap = false;
2946  else if (wrapper == JSW_NONE)
2947  wrap = false;
2948  else if (wrapper == JSW_UNCONDITIONAL)
2949  wrap = true;
2950  else if (wrapper == JSW_CONDITIONAL)
2951  wrap = count > 1 ||
2952  IsAJsonbScalar(first) ||
2953  (first->type == jbvBinary &&
2954  JsonContainerIsScalar(first->val.binary.data));
2955  else
2956  {
2957  elog(ERROR, "unrecognized json wrapper %d", wrapper);
2958  wrap = false;
2959  }
2960 
2961  if (wrap)
2963 
2964  if (count > 1)
2965  {
2966  if (error)
2967  {
2968  *error = true;
2969  return (Datum) 0;
2970  }
2971 
2972  ereport(ERROR,
2973  (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM),
2974  errmsg("JSON path expression in JSON_QUERY should return "
2975  "singleton item without wrapper"),
2976  errhint("use WITH WRAPPER clause to wrap SQL/JSON item "
2977  "sequence into array")));
2978  }
2979 
2980  if (first)
2981  return JsonbPGetDatum(JsonbValueToJsonb(first));
2982 
2983  *empty = true;
2984  return PointerGetDatum(NULL);
2985 }
int errhint(const char *fmt,...)
Definition: elog.c:1151
@ jbvBinary
Definition: jsonb.h:244
#define IsAJsonbScalar(jsonbval)
Definition: jsonb.h:305
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:94
static JsonbValue * wrapItemsInArray(const JsonValueList *items)
static int JsonValueListLength(const JsonValueList *jvl)
static JsonbValue * JsonValueListHead(JsonValueList *jvl)
uintptr_t Datum
Definition: postgres.h:411
#define PointerGetDatum(X)
Definition: postgres.h:600
@ JSW_UNCONDITIONAL
Definition: primnodes.h:1421
@ JSW_CONDITIONAL
Definition: primnodes.h:1420
@ JSW_NONE
Definition: primnodes.h:1419
enum jbvType type
Definition: jsonb.h:263
char * val
Definition: jsonb.h:272

References Assert(), DatumGetJsonbP, elog(), ereport, errcode(), errhint(), errmsg(), ERROR, error(), EvalJsonPathVar(), executeJsonPath(), IsAJsonbScalar, jbvBinary, jperIsError, JsonbPGetDatum, JsonbValueToJsonb(), JsonContainerIsScalar, JsonValueListHead(), JsonValueListLength(), JSW_CONDITIONAL, JSW_NONE, JSW_UNCONDITIONAL, PG_USED_FOR_ASSERTS_ONLY, PointerGetDatum, res, JsonbValue::type, JsonbValue::val, and wrapItemsInArray().

Referenced by ExecEvalJsonExpr().

◆ JsonPathValue()

JsonbValue* JsonPathValue ( Datum  jb,
JsonPath jp,
bool empty,
bool error,
List vars 
)

Definition at line 2988 of file jsonpath_exec.c.

2989 {
2990  JsonbValue *res;
2991  JsonValueList found = {0};
2993  int count;
2994 
2996  &found, true);
2997 
2998  Assert(error || !jperIsError(jper));
2999 
3000  if (error && jperIsError(jper))
3001  {
3002  *error = true;
3003  *empty = false;
3004  return NULL;
3005  }
3006 
3007  count = JsonValueListLength(&found);
3008 
3009  *empty = !count;
3010 
3011  if (*empty)
3012  return NULL;
3013 
3014  if (count > 1)
3015  {
3016  if (error)
3017  {
3018  *error = true;
3019  return NULL;
3020  }
3021 
3022  ereport(ERROR,
3023  (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM),
3024  errmsg("JSON path expression in JSON_VALUE should return "
3025  "singleton scalar item")));
3026  }
3027 
3028  res = JsonValueListHead(&found);
3029 
3030  if (res->type == jbvBinary &&
3031  JsonContainerIsScalar(res->val.binary.data))
3032  JsonbExtractScalar(res->val.binary.data, res);
3033 
3034  if (!IsAJsonbScalar(res))
3035  {
3036  if (error)
3037  {
3038  *error = true;
3039  return NULL;
3040  }
3041 
3042  ereport(ERROR,
3043  (errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED),
3044  errmsg("JSON path expression in JSON_VALUE should return "
3045  "singleton scalar item")));
3046  }
3047 
3048  if (res->type == jbvNull)
3049  return NULL;
3050 
3051  return res;
3052 }
int binary
Definition: libpq-int.h:180

References Assert(), pg_result::binary, DatumGetJsonbP, ereport, errcode(), errmsg(), ERROR, error(), EvalJsonPathVar(), executeJsonPath(), IsAJsonbScalar, jbvBinary, jbvNull, jperIsError, JsonbExtractScalar(), JsonContainerIsScalar, JsonValueListHead(), JsonValueListLength(), PG_USED_FOR_ASSERTS_ONLY, and res.

Referenced by ExecEvalJsonExpr().

◆ jspConvertRegexFlags()

int jspConvertRegexFlags ( uint32  xflags)

Referenced by executeLikeRegex().

◆ jspGetArg()

void jspGetArg ( JsonPathItem v,
JsonPathItem a 
)

Definition at line 929 of file jsonpath.c.

930 {
931  Assert(v->type == jpiFilter ||
932  v->type == jpiNot ||
933  v->type == jpiIsUnknown ||
934  v->type == jpiExists ||
935  v->type == jpiPlus ||
936  v->type == jpiMinus ||
937  v->type == jpiDatetime);
938 
939  jspInitByBuffer(a, v->base, v->content.arg);
940 }
int a
Definition: isn.c:69
void jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
Definition: jsonpath.c:849
char * base
Definition: jsonpath.h:119
int32 arg
Definition: jsonpath.h:131
JsonPathItemType type
Definition: jsonpath.h:110
union JsonPathItem::@136 content

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

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

◆ jspGetArraySubscript()

bool jspGetArraySubscript ( JsonPathItem v,
JsonPathItem from,
JsonPathItem to,
int  i 
)

Definition at line 1068 of file jsonpath.c.

1070 {
1071  Assert(v->type == jpiIndexArray);
1072 
1073  jspInitByBuffer(from, v->base, v->content.array.elems[i].from);
1074 
1075  if (!v->content.array.elems[i].to)
1076  return false;
1077 
1078  jspInitByBuffer(to, v->base, v->content.array.elems[i].to);
1079 
1080  return true;
1081 }
int i
Definition: isn.c:73
struct JsonPathItem::@136::@138 array

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

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

◆ jspGetBool()

bool jspGetBool ( JsonPathItem v)

Definition at line 1040 of file jsonpath.c.

1041 {
1042  Assert(v->type == jpiBool);
1043 
1044  return (bool) *v->content.value.data;
1045 }
struct JsonPathItem::@136::@140 value

References Assert(), JsonPathItem::content, jpiBool, JsonPathItem::type, and JsonPathItem::value.

Referenced by getJsonPathItem(), and printJsonPathItem().

◆ jspGetLeftArg()

void jspGetLeftArg ( JsonPathItem v,
JsonPathItem a 
)

Definition at line 998 of file jsonpath.c.

999 {
1000  Assert(v->type == jpiAnd ||
1001  v->type == jpiOr ||
1002  v->type == jpiEqual ||
1003  v->type == jpiNotEqual ||
1004  v->type == jpiLess ||
1005  v->type == jpiGreater ||
1006  v->type == jpiLessOrEqual ||
1007  v->type == jpiGreaterOrEqual ||
1008  v->type == jpiAdd ||
1009  v->type == jpiSub ||
1010  v->type == jpiMul ||
1011  v->type == jpiDiv ||
1012  v->type == jpiMod ||
1013  v->type == jpiStartsWith);
1014 
1015  jspInitByBuffer(a, v->base, v->content.args.left);
1016 }
struct JsonPathItem::@136::@137 args

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

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

◆ jspGetNext()

bool jspGetNext ( JsonPathItem v,
JsonPathItem a 
)

Definition at line 943 of file jsonpath.c.

944 {
945  if (jspHasNext(v))
946  {
947  Assert(v->type == jpiString ||
948  v->type == jpiNumeric ||
949  v->type == jpiBool ||
950  v->type == jpiNull ||
951  v->type == jpiKey ||
952  v->type == jpiAny ||
953  v->type == jpiAnyArray ||
954  v->type == jpiAnyKey ||
955  v->type == jpiIndexArray ||
956  v->type == jpiFilter ||
957  v->type == jpiCurrent ||
958  v->type == jpiExists ||
959  v->type == jpiRoot ||
960  v->type == jpiVariable ||
961  v->type == jpiLast ||
962  v->type == jpiAdd ||
963  v->type == jpiSub ||
964  v->type == jpiMul ||
965  v->type == jpiDiv ||
966  v->type == jpiMod ||
967  v->type == jpiPlus ||
968  v->type == jpiMinus ||
969  v->type == jpiEqual ||
970  v->type == jpiNotEqual ||
971  v->type == jpiGreater ||
972  v->type == jpiGreaterOrEqual ||
973  v->type == jpiLess ||
974  v->type == jpiLessOrEqual ||
975  v->type == jpiAnd ||
976  v->type == jpiOr ||
977  v->type == jpiNot ||
978  v->type == jpiIsUnknown ||
979  v->type == jpiType ||
980  v->type == jpiSize ||
981  v->type == jpiAbs ||
982  v->type == jpiFloor ||
983  v->type == jpiCeiling ||
984  v->type == jpiDouble ||
985  v->type == jpiDatetime ||
986  v->type == jpiKeyValue ||
987  v->type == jpiStartsWith);
988 
989  if (a)
990  jspInitByBuffer(a, v->base, v->nextPos);
991  return true;
992  }
993 
994  return false;
995 }
#define jspHasNext(jsp)
Definition: jsonpath.h:167
int32 nextPos
Definition: jsonpath.h:113

References a, Assert(), JsonPathItem::base, jpiAbs, jpiAdd, jpiAnd, jpiAny, jpiAnyArray, jpiAnyKey, jpiBool, jpiCeiling, jpiCurrent, jpiDatetime, jpiDiv, jpiDouble, jpiEqual, jpiExists, jpiFilter, jpiFloor, jpiGreater, jpiGreaterOrEqual, jpiIndexArray, jpiIsUnknown, jpiKey, jpiKeyValue, jpiLast, jpiLess, jpiLessOrEqual, jpiMinus, jpiMod, jpiMul, jpiNot, jpiNotEqual, jpiNull, jpiNumeric, jpiOr, jpiPlus, jpiRoot, jpiSize, jpiStartsWith, jpiString, jpiSub, 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()

Numeric jspGetNumeric ( JsonPathItem v)

Definition at line 1048 of file jsonpath.c.

1049 {
1050  Assert(v->type == jpiNumeric);
1051 
1052  return (Numeric) v->content.value.data;
1053 }

References Assert(), JsonPathItem::content, jpiNumeric, JsonPathItem::type, and JsonPathItem::value.

Referenced by getJsonPathItem(), and printJsonPathItem().

◆ jspGetRightArg()

void jspGetRightArg ( JsonPathItem v,
JsonPathItem a 
)

Definition at line 1019 of file jsonpath.c.

1020 {
1021  Assert(v->type == jpiAnd ||
1022  v->type == jpiOr ||
1023  v->type == jpiEqual ||
1024  v->type == jpiNotEqual ||
1025  v->type == jpiLess ||
1026  v->type == jpiGreater ||
1027  v->type == jpiLessOrEqual ||
1028  v->type == jpiGreaterOrEqual ||
1029  v->type == jpiAdd ||
1030  v->type == jpiSub ||
1031  v->type == jpiMul ||
1032  v->type == jpiDiv ||
1033  v->type == jpiMod ||
1034  v->type == jpiStartsWith);
1035 
1036  jspInitByBuffer(a, v->base, v->content.args.right);
1037 }

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

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

◆ jspGetString()

char* jspGetString ( JsonPathItem v,
int32 len 
)

Definition at line 1056 of file jsonpath.c.

1057 {
1058  Assert(v->type == jpiKey ||
1059  v->type == jpiString ||
1060  v->type == jpiVariable);
1061 
1062  if (len)
1063  *len = v->content.value.datalen;
1064  return v->content.value.data;
1065 }
const void size_t len

References Assert(), JsonPathItem::content, 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()

void jspInit ( JsonPathItem v,
JsonPath js 
)

Definition at line 839 of file jsonpath.c.

840 {
842  jspInitByBuffer(v, js->data, 0);
843 }
#define JSONPATH_VERSION
Definition: jsonpath.h:31
#define JSONPATH_LAX
Definition: jsonpath.h:32
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: jsonpath.h:28
uint32 header
Definition: jsonpath.h:27

References Assert(), JsonPath::data, JsonPath::header, JSONPATH_LAX, JSONPATH_VERSION, and jspInitByBuffer().

Referenced by executeJsonPath(), extract_jsp_query(), jsonPathToCstring(), and jspIsMutable().

◆ jspInitByBuffer()

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

Definition at line 849 of file jsonpath.c.

850 {
851  v->base = base + pos;
852 
853  read_byte(v->type, base, pos);
854  pos = INTALIGN((uintptr_t) (base + pos)) - (uintptr_t) base;
855  read_int32(v->nextPos, base, pos);
856 
857  switch (v->type)
858  {
859  case jpiNull:
860  case jpiRoot:
861  case jpiCurrent:
862  case jpiAnyArray:
863  case jpiAnyKey:
864  case jpiType:
865  case jpiSize:
866  case jpiAbs:
867  case jpiFloor:
868  case jpiCeiling:
869  case jpiDouble:
870  case jpiKeyValue:
871  case jpiLast:
872  break;
873  case jpiKey:
874  case jpiString:
875  case jpiVariable:
876  read_int32(v->content.value.datalen, base, pos);
877  /* FALLTHROUGH */
878  case jpiNumeric:
879  case jpiBool:
880  v->content.value.data = base + pos;
881  break;
882  case jpiAnd:
883  case jpiOr:
884  case jpiAdd:
885  case jpiSub:
886  case jpiMul:
887  case jpiDiv:
888  case jpiMod:
889  case jpiEqual:
890  case jpiNotEqual:
891  case jpiLess:
892  case jpiGreater:
893  case jpiLessOrEqual:
894  case jpiGreaterOrEqual:
895  case jpiStartsWith:
896  read_int32(v->content.args.left, base, pos);
897  read_int32(v->content.args.right, base, pos);
898  break;
899  case jpiLikeRegex:
900  read_int32(v->content.like_regex.flags, base, pos);
901  read_int32(v->content.like_regex.expr, base, pos);
902  read_int32(v->content.like_regex.patternlen, base, pos);
903  v->content.like_regex.pattern = base + pos;
904  break;
905  case jpiNot:
906  case jpiExists:
907  case jpiIsUnknown:
908  case jpiPlus:
909  case jpiMinus:
910  case jpiFilter:
911  case jpiDatetime:
912  read_int32(v->content.arg, base, pos);
913  break;
914  case jpiIndexArray:
915  read_int32(v->content.array.nelems, base, pos);
916  read_int32_n(v->content.array.elems, base, pos,
917  v->content.array.nelems * 2);
918  break;
919  case jpiAny:
920  read_int32(v->content.anybounds.first, base, pos);
921  read_int32(v->content.anybounds.last, base, pos);
922  break;
923  default:
924  elog(ERROR, "unrecognized jsonpath item type: %d", v->type);
925  }
926 }
#define INTALIGN(LEN)
Definition: c.h:765
#define read_byte(v, b, p)
Definition: jsonpath.c:820
#define read_int32_n(v, b, p, n)
Definition: jsonpath.c:830
#define read_int32(v, b, p)
Definition: jsonpath.c:825
struct JsonPathItem::@136::@141 like_regex
struct JsonPathItem::@136::@139 anybounds

References JsonPathItem::anybounds, JsonPathItem::arg, JsonPathItem::args, JsonPathItem::array, JsonPathItem::base, JsonPathItem::content, elog(), ERROR, INTALIGN, jpiAbs, jpiAdd, jpiAnd, jpiAny, jpiAnyArray, jpiAnyKey, jpiBool, jpiCeiling, jpiCurrent, jpiDatetime, jpiDiv, jpiDouble, jpiEqual, jpiExists, jpiFilter, jpiFloor, jpiGreater, jpiGreaterOrEqual, jpiIndexArray, jpiIsUnknown, jpiKey, jpiKeyValue, jpiLast, jpiLess, jpiLessOrEqual, jpiLikeRegex, jpiMinus, jpiMod, jpiMul, jpiNot, jpiNotEqual, jpiNull, jpiNumeric, jpiOr, jpiPlus, jpiRoot, jpiSize, jpiStartsWith, jpiString, jpiSub, jpiType, jpiVariable, JsonPathItem::like_regex, JsonPathItem::nextPos, read_byte, read_int32, read_int32_n, JsonPathItem::type, and JsonPathItem::value.

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

◆ jspIsMutable()

bool jspIsMutable ( JsonPath path,
List varnames,
List varexprs 
)

Definition at line 1321 of file jsonpath.c.

1322 {
1324  JsonPathItem jpi;
1325 
1326  cxt.varnames = varnames;
1327  cxt.varexprs = varexprs;
1328  cxt.current = jpdsNonDateTime;
1329  cxt.lax = (path->header & JSONPATH_LAX) != 0;
1330  cxt.mutable = false;
1331 
1332  jspInit(&jpi, path);
1333  jspIsMutableWalker(&jpi, &cxt);
1334 
1335  return cxt.mutable;
1336 }
void jspInit(JsonPathItem *v, JsonPath *js)
Definition: jsonpath.c:839
static JsonPathDatatypeStatus jspIsMutableWalker(JsonPathItem *jpi, JsonPathMutableContext *cxt)
Definition: jsonpath.c:1106
@ jpdsNonDateTime
Definition: jsonpath.c:1086
JsonPathDatatypeStatus current
Definition: jsonpath.c:1097

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()

const char* jspOperationName ( JsonPathItemType  type)

Definition at line 724 of file jsonpath.c.

725 {
726  switch (type)
727  {
728  case jpiAnd:
729  return "&&";
730  case jpiOr:
731  return "||";
732  case jpiEqual:
733  return "==";
734  case jpiNotEqual:
735  return "!=";
736  case jpiLess:
737  return "<";
738  case jpiGreater:
739  return ">";
740  case jpiLessOrEqual:
741  return "<=";
742  case jpiGreaterOrEqual:
743  return ">=";
744  case jpiPlus:
745  case jpiAdd:
746  return "+";
747  case jpiMinus:
748  case jpiSub:
749  return "-";
750  case jpiMul:
751  return "*";
752  case jpiDiv:
753  return "/";
754  case jpiMod:
755  return "%";
756  case jpiStartsWith:
757  return "starts with";
758  case jpiLikeRegex:
759  return "like_regex";
760  case jpiType:
761  return "type";
762  case jpiSize:
763  return "size";
764  case jpiKeyValue:
765  return "keyvalue";
766  case jpiDouble:
767  return "double";
768  case jpiAbs:
769  return "abs";
770  case jpiFloor:
771  return "floor";
772  case jpiCeiling:
773  return "ceiling";
774  case jpiDatetime:
775  return "datetime";
776  default:
777  elog(ERROR, "unrecognized jsonpath item type: %d", type);
778  return NULL;
779  }
780 }

References elog(), ERROR, jpiAbs, jpiAdd, jpiAnd, jpiCeiling, jpiDatetime, jpiDiv, jpiDouble, jpiEqual, jpiFloor, jpiGreater, jpiGreaterOrEqual, jpiKeyValue, jpiLess, jpiLessOrEqual, jpiLikeRegex, jpiMinus, jpiMod, jpiMul, jpiNotEqual, jpiOr, jpiPlus, jpiSize, jpiStartsWith, jpiSub, jpiType, and generate_unaccent_rules::type.

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

◆ parsejsonpath()

JsonPathParseResult* parsejsonpath ( const char *  str,
int  len 
)

Referenced by jsonPathFromCstring().

Variable Documentation

◆ JsonbTableRoutine

PGDLLIMPORT const TableFuncRoutine JsonbTableRoutine
extern

Definition at line 3553 of file jsonpath_exec.c.

Referenced by ExecInitTableFuncScan().