PostgreSQL Source Code  git master
jsonpath.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * jsonpath.h
4  * Definitions for jsonpath datatype
5  *
6  * Copyright (c) 2019-2022, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/include/utils/jsonpath.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #ifndef JSONPATH_H
15 #define JSONPATH_H
16 
17 #include "fmgr.h"
18 #include "nodes/pg_list.h"
19 #include "utils/jsonb.h"
20 
21 typedef struct
22 {
23  int32 vl_len_; /* varlena header (do not touch directly!) */
24  uint32 header; /* version and flags (see below) */
26 } JsonPath;
27 
28 #define JSONPATH_VERSION (0x01)
29 #define JSONPATH_LAX (0x80000000)
30 #define JSONPATH_HDRSZ (offsetof(JsonPath, data))
31 
32 static inline JsonPath *
34 {
35  return (JsonPath *) PG_DETOAST_DATUM(d);
36 }
37 
38 static inline JsonPath *
40 {
41  return (JsonPath *) PG_DETOAST_DATUM_COPY(d);
42 }
43 
44 #define PG_GETARG_JSONPATH_P(x) DatumGetJsonPathP(PG_GETARG_DATUM(x))
45 #define PG_GETARG_JSONPATH_P_COPY(x) DatumGetJsonPathPCopy(PG_GETARG_DATUM(x))
46 #define PG_RETURN_JSONPATH_P(p) PG_RETURN_POINTER(p)
47 
48 #define jspIsScalar(type) ((type) >= jpiNull && (type) <= jpiBool)
49 
50 /*
51  * All node's type of jsonpath expression
52  */
53 typedef enum JsonPathItemType
54 {
55  jpiNull = jbvNull, /* NULL literal */
56  jpiString = jbvString, /* string literal */
57  jpiNumeric = jbvNumeric, /* numeric literal */
58  jpiBool = jbvBool, /* boolean literal: TRUE or FALSE */
59  jpiAnd, /* predicate && predicate */
60  jpiOr, /* predicate || predicate */
61  jpiNot, /* ! predicate */
62  jpiIsUnknown, /* (predicate) IS UNKNOWN */
63  jpiEqual, /* expr == expr */
64  jpiNotEqual, /* expr != expr */
65  jpiLess, /* expr < expr */
66  jpiGreater, /* expr > expr */
67  jpiLessOrEqual, /* expr <= expr */
68  jpiGreaterOrEqual, /* expr >= expr */
69  jpiAdd, /* expr + expr */
70  jpiSub, /* expr - expr */
71  jpiMul, /* expr * expr */
72  jpiDiv, /* expr / expr */
73  jpiMod, /* expr % expr */
74  jpiPlus, /* + expr */
75  jpiMinus, /* - expr */
76  jpiAnyArray, /* [*] */
77  jpiAnyKey, /* .* */
78  jpiIndexArray, /* [subscript, ...] */
79  jpiAny, /* .** */
80  jpiKey, /* .key */
81  jpiCurrent, /* @ */
82  jpiRoot, /* $ */
83  jpiVariable, /* $variable */
84  jpiFilter, /* ? (predicate) */
85  jpiExists, /* EXISTS (expr) predicate */
86  jpiType, /* .type() item method */
87  jpiSize, /* .size() item method */
88  jpiAbs, /* .abs() item method */
89  jpiFloor, /* .floor() item method */
90  jpiCeiling, /* .ceiling() item method */
91  jpiDouble, /* .double() item method */
92  jpiDatetime, /* .datetime() item method */
93  jpiKeyValue, /* .keyvalue() item method */
94  jpiSubscript, /* array subscript: 'expr' or 'expr TO expr' */
95  jpiLast, /* LAST array subscript */
96  jpiStartsWith, /* STARTS WITH predicate */
97  jpiLikeRegex, /* LIKE_REGEX predicate */
99 
100 /* XQuery regex mode flags for LIKE_REGEX predicate */
101 #define JSP_REGEX_ICASE 0x01 /* i flag, case insensitive */
102 #define JSP_REGEX_DOTALL 0x02 /* s flag, dot matches newline */
103 #define JSP_REGEX_MLINE 0x04 /* m flag, ^/$ match at newlines */
104 #define JSP_REGEX_WSPACE 0x08 /* x flag, ignore whitespace in pattern */
105 #define JSP_REGEX_QUOTE 0x10 /* q flag, no special characters */
106 
107 /*
108  * Support functions to parse/construct binary value.
109  * Unlike many other representation of expression the first/main
110  * node is not an operation but left operand of expression. That
111  * allows to implement cheap follow-path descending in jsonb
112  * structure and then execute operator with right operand
113  */
114 
115 typedef struct JsonPathItem
116 {
118 
119  /* position form base to next node */
121 
122  /*
123  * pointer into JsonPath value to current node, all positions of current
124  * are relative to this base
125  */
126  char *base;
127 
128  union
129  {
130  /* classic operator with two operands: and, or etc */
131  struct
132  {
135  } args;
136 
137  /* any unary operation */
139 
140  /* storage for jpiIndexArray: indexes of array */
141  struct
142  {
144  struct
145  {
148  } *elems;
149  } array;
150 
151  /* jpiAny: levels */
152  struct
153  {
157 
158  struct
159  {
160  char *data; /* for bool, numeric and string/key */
161  int32 datalen; /* filled only for string/key */
162  } value;
163 
164  struct
165  {
167  char *pattern;
173 
174 #define jspHasNext(jsp) ((jsp)->nextPos > 0)
175 
176 extern void jspInit(JsonPathItem *v, JsonPath *js);
177 extern void jspInitByBuffer(JsonPathItem *v, char *base, int32 pos);
178 extern bool jspGetNext(JsonPathItem *v, JsonPathItem *a);
179 extern void jspGetArg(JsonPathItem *v, JsonPathItem *a);
180 extern void jspGetLeftArg(JsonPathItem *v, JsonPathItem *a);
181 extern void jspGetRightArg(JsonPathItem *v, JsonPathItem *a);
183 extern bool jspGetBool(JsonPathItem *v);
184 extern char *jspGetString(JsonPathItem *v, int32 *len);
185 extern bool jspGetArraySubscript(JsonPathItem *v, JsonPathItem *from,
186  JsonPathItem *to, int i);
187 
189 
190 /*
191  * Parsing support data structures.
192  */
193 
194 typedef struct JsonPathParseItem JsonPathParseItem;
195 
197 {
199  JsonPathParseItem *next; /* next in path */
200 
201  union
202  {
203 
204  /* classic operator with two operands: and, or etc */
205  struct
206  {
209  } args;
210 
211  /* any unary operation */
213 
214  /* storage for jpiIndexArray: indexes of array */
215  struct
216  {
217  int nelems;
218  struct
219  {
222  } *elems;
223  } array;
224 
225  /* jpiAny: levels */
226  struct
227  {
231 
232  struct
233  {
235  char *pattern; /* could not be not null-terminated */
239 
240  /* scalars */
242  bool boolean;
243  struct
244  {
246  char *val; /* could not be not null-terminated */
248  } value;
249 };
250 
251 typedef struct JsonPathParseResult
252 {
254  bool lax;
256 
257 extern JsonPathParseResult *parsejsonpath(const char *str, int len);
258 
259 extern int jspConvertRegexFlags(uint32 xflags);
260 
261 #endif
unsigned int uint32
Definition: c.h:442
signed int int32
Definition: c.h:430
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:362
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:242
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
int a
Definition: isn.c:69
int i
Definition: isn.c:73
@ jbvNumeric
Definition: jsonb.h:230
@ jbvBool
Definition: jsonb.h:231
@ jbvNull
Definition: jsonb.h:228
@ jbvString
Definition: jsonb.h:229
void jspGetLeftArg(JsonPathItem *v, JsonPathItem *a)
Definition: jsonpath.c:996
int jspConvertRegexFlags(uint32 xflags)
void jspGetArg(JsonPathItem *v, JsonPathItem *a)
Definition: jsonpath.c:927
void jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
Definition: jsonpath.c:847
bool jspGetBool(JsonPathItem *v)
Definition: jsonpath.c:1038
void jspInit(JsonPathItem *v, JsonPath *js)
Definition: jsonpath.c:837
const char * jspOperationName(JsonPathItemType type)
Definition: jsonpath.c:722
Numeric jspGetNumeric(JsonPathItem *v)
Definition: jsonpath.c:1046
bool jspGetArraySubscript(JsonPathItem *v, JsonPathItem *from, JsonPathItem *to, int i)
Definition: jsonpath.c:1066
bool jspGetNext(JsonPathItem *v, JsonPathItem *a)
Definition: jsonpath.c:941
static JsonPath * DatumGetJsonPathP(Datum d)
Definition: jsonpath.h:33
JsonPathParseResult * parsejsonpath(const char *str, int len)
struct JsonPathParseResult JsonPathParseResult
void jspGetRightArg(JsonPathItem *v, JsonPathItem *a)
Definition: jsonpath.c:1017
char * jspGetString(JsonPathItem *v, int32 *len)
Definition: jsonpath.c:1054
struct JsonPathItem JsonPathItem
JsonPathItemType
Definition: jsonpath.h:54
@ jpiAdd
Definition: jsonpath.h:69
@ jpiString
Definition: jsonpath.h:56
@ jpiAbs
Definition: jsonpath.h:88
@ jpiIndexArray
Definition: jsonpath.h:78
@ jpiAny
Definition: jsonpath.h:79
@ jpiDatetime
Definition: jsonpath.h:92
@ jpiBool
Definition: jsonpath.h:58
@ jpiType
Definition: jsonpath.h:86
@ jpiFloor
Definition: jsonpath.h:89
@ jpiAnyArray
Definition: jsonpath.h:76
@ jpiExists
Definition: jsonpath.h:85
@ jpiSize
Definition: jsonpath.h:87
@ jpiSub
Definition: jsonpath.h:70
@ jpiSubscript
Definition: jsonpath.h:94
@ jpiNotEqual
Definition: jsonpath.h:64
@ jpiMul
Definition: jsonpath.h:71
@ jpiVariable
Definition: jsonpath.h:83
@ jpiNot
Definition: jsonpath.h:61
@ jpiGreaterOrEqual
Definition: jsonpath.h:68
@ jpiPlus
Definition: jsonpath.h:74
@ jpiDouble
Definition: jsonpath.h:91
@ jpiGreater
Definition: jsonpath.h:66
@ jpiAnd
Definition: jsonpath.h:59
@ jpiStartsWith
Definition: jsonpath.h:96
@ jpiOr
Definition: jsonpath.h:60
@ jpiMod
Definition: jsonpath.h:73
@ jpiLikeRegex
Definition: jsonpath.h:97
@ jpiRoot
Definition: jsonpath.h:82
@ jpiFilter
Definition: jsonpath.h:84
@ jpiNull
Definition: jsonpath.h:55
@ jpiLess
Definition: jsonpath.h:65
@ jpiCurrent
Definition: jsonpath.h:81
@ jpiEqual
Definition: jsonpath.h:63
@ jpiKey
Definition: jsonpath.h:80
@ jpiDiv
Definition: jsonpath.h:72
@ jpiLast
Definition: jsonpath.h:95
@ jpiMinus
Definition: jsonpath.h:75
@ jpiLessOrEqual
Definition: jsonpath.h:67
@ jpiCeiling
Definition: jsonpath.h:90
@ jpiIsUnknown
Definition: jsonpath.h:62
@ jpiKeyValue
Definition: jsonpath.h:93
@ jpiNumeric
Definition: jsonpath.h:57
@ jpiAnyKey
Definition: jsonpath.h:77
static JsonPath * DatumGetJsonPathPCopy(Datum d)
Definition: jsonpath.h:39
const void size_t len
const void * data
uintptr_t Datum
Definition: postgres.h:412
uint32 first
Definition: jsonpath.h:154
int32 datalen
Definition: jsonpath.h:161
struct JsonPathItem::@128::@133 like_regex
int32 nelems
Definition: jsonpath.h:143
uint32 flags
Definition: jsonpath.h:169
int32 to
Definition: jsonpath.h:147
int32 from
Definition: jsonpath.h:146
int32 left
Definition: jsonpath.h:133
union JsonPathItem::@128 content
char * base
Definition: jsonpath.h:126
int32 nextPos
Definition: jsonpath.h:120
struct JsonPathItem::@128::@130 array
int32 arg
Definition: jsonpath.h:138
JsonPathItemType type
Definition: jsonpath.h:117
char * data
Definition: jsonpath.h:160
struct JsonPathItem::@128::@130::@134 * elems
uint32 last
Definition: jsonpath.h:155
struct JsonPathItem::@128::@129 args
struct JsonPathItem::@128::@132 value
int32 right
Definition: jsonpath.h:134
int32 patternlen
Definition: jsonpath.h:168
struct JsonPathItem::@128::@131 anybounds
char * pattern
Definition: jsonpath.h:167
int32 expr
Definition: jsonpath.h:166
JsonPathParseItem * arg
Definition: jsonpath.h:212
struct JsonPathParseItem::@135::@136 args
JsonPathParseItem * from
Definition: jsonpath.h:220
struct JsonPathParseItem::@135::@137 array
JsonPathParseItem * expr
Definition: jsonpath.h:234
JsonPathParseItem * left
Definition: jsonpath.h:207
JsonPathParseItem * right
Definition: jsonpath.h:208
JsonPathParseItem * next
Definition: jsonpath.h:199
struct JsonPathParseItem::@135::@140 string
Numeric numeric
Definition: jsonpath.h:241
union JsonPathParseItem::@135 value
struct JsonPathParseItem::@135::@137::@141 * elems
JsonPathParseItem * to
Definition: jsonpath.h:221
struct JsonPathParseItem::@135::@138 anybounds
struct JsonPathParseItem::@135::@139 like_regex
JsonPathItemType type
Definition: jsonpath.h:198
uint32 patternlen
Definition: jsonpath.h:236
JsonPathParseItem * expr
Definition: jsonpath.h:253
int32 vl_len_
Definition: jsonpath.h:23
uint32 header
Definition: jsonpath.h:24