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-2020, 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 #define DatumGetJsonPathP(d) ((JsonPath *) DatumGetPointer(PG_DETOAST_DATUM(d)))
33 #define DatumGetJsonPathPCopy(d) ((JsonPath *) DatumGetPointer(PG_DETOAST_DATUM_COPY(d)))
34 #define PG_GETARG_JSONPATH_P(x) DatumGetJsonPathP(PG_GETARG_DATUM(x))
35 #define PG_GETARG_JSONPATH_P_COPY(x) DatumGetJsonPathPCopy(PG_GETARG_DATUM(x))
36 #define PG_RETURN_JSONPATH_P(p) PG_RETURN_POINTER(p)
37 
38 #define jspIsScalar(type) ((type) >= jpiNull && (type) <= jpiBool)
39 
40 /*
41  * All node's type of jsonpath expression
42  */
43 typedef enum JsonPathItemType
44 {
45  jpiNull = jbvNull, /* NULL literal */
46  jpiString = jbvString, /* string literal */
47  jpiNumeric = jbvNumeric, /* numeric literal */
48  jpiBool = jbvBool, /* boolean literal: TRUE or FALSE */
49  jpiAnd, /* predicate && predicate */
50  jpiOr, /* predicate || predicate */
51  jpiNot, /* ! predicate */
52  jpiIsUnknown, /* (predicate) IS UNKNOWN */
53  jpiEqual, /* expr == expr */
54  jpiNotEqual, /* expr != expr */
55  jpiLess, /* expr < expr */
56  jpiGreater, /* expr > expr */
57  jpiLessOrEqual, /* expr <= expr */
58  jpiGreaterOrEqual, /* expr >= expr */
59  jpiAdd, /* expr + expr */
60  jpiSub, /* expr - expr */
61  jpiMul, /* expr * expr */
62  jpiDiv, /* expr / expr */
63  jpiMod, /* expr % expr */
64  jpiPlus, /* + expr */
65  jpiMinus, /* - expr */
66  jpiAnyArray, /* [*] */
67  jpiAnyKey, /* .* */
68  jpiIndexArray, /* [subscript, ...] */
69  jpiAny, /* .** */
70  jpiKey, /* .key */
71  jpiCurrent, /* @ */
72  jpiRoot, /* $ */
73  jpiVariable, /* $variable */
74  jpiFilter, /* ? (predicate) */
75  jpiExists, /* EXISTS (expr) predicate */
76  jpiType, /* .type() item method */
77  jpiSize, /* .size() item method */
78  jpiAbs, /* .abs() item method */
79  jpiFloor, /* .floor() item method */
80  jpiCeiling, /* .ceiling() item method */
81  jpiDouble, /* .double() item method */
82  jpiDatetime, /* .datetime() item method */
83  jpiKeyValue, /* .keyvalue() item method */
84  jpiSubscript, /* array subscript: 'expr' or 'expr TO expr' */
85  jpiLast, /* LAST array subscript */
86  jpiStartsWith, /* STARTS WITH predicate */
87  jpiLikeRegex, /* LIKE_REGEX predicate */
89 
90 /* XQuery regex mode flags for LIKE_REGEX predicate */
91 #define JSP_REGEX_ICASE 0x01 /* i flag, case insensitive */
92 #define JSP_REGEX_DOTALL 0x02 /* s flag, dot matches newline */
93 #define JSP_REGEX_MLINE 0x04 /* m flag, ^/$ match at newlines */
94 #define JSP_REGEX_WSPACE 0x08 /* x flag, ignore whitespace in pattern */
95 #define JSP_REGEX_QUOTE 0x10 /* q flag, no special characters */
96 
97 /*
98  * Support functions to parse/construct binary value.
99  * Unlike many other representation of expression the first/main
100  * node is not an operation but left operand of expression. That
101  * allows to implement cheap follow-path descending in jsonb
102  * structure and then execute operator with right operand
103  */
104 
105 typedef struct JsonPathItem
106 {
108 
109  /* position form base to next node */
111 
112  /*
113  * pointer into JsonPath value to current node, all positions of current
114  * are relative to this base
115  */
116  char *base;
117 
118  union
119  {
120  /* classic operator with two operands: and, or etc */
121  struct
122  {
125  } args;
126 
127  /* any unary operation */
129 
130  /* storage for jpiIndexArray: indexes of array */
131  struct
132  {
134  struct
135  {
138  } *elems;
139  } array;
140 
141  /* jpiAny: levels */
142  struct
143  {
146  } anybounds;
147 
148  struct
149  {
150  char *data; /* for bool, numeric and string/key */
151  int32 datalen; /* filled only for string/key */
152  } value;
153 
154  struct
155  {
157  char *pattern;
160  } like_regex;
161  } content;
162 } JsonPathItem;
163 
164 #define jspHasNext(jsp) ((jsp)->nextPos > 0)
165 
166 extern void jspInit(JsonPathItem *v, JsonPath *js);
167 extern void jspInitByBuffer(JsonPathItem *v, char *base, int32 pos);
168 extern bool jspGetNext(JsonPathItem *v, JsonPathItem *a);
169 extern void jspGetArg(JsonPathItem *v, JsonPathItem *a);
170 extern void jspGetLeftArg(JsonPathItem *v, JsonPathItem *a);
171 extern void jspGetRightArg(JsonPathItem *v, JsonPathItem *a);
173 extern bool jspGetBool(JsonPathItem *v);
174 extern char *jspGetString(JsonPathItem *v, int32 *len);
176  JsonPathItem *to, int i);
177 
178 extern const char *jspOperationName(JsonPathItemType type);
179 
180 /*
181  * Parsing support data structures.
182  */
183 
185 
187 {
189  JsonPathParseItem *next; /* next in path */
190 
191  union
192  {
193 
194  /* classic operator with two operands: and, or etc */
195  struct
196  {
199  } args;
200 
201  /* any unary operation */
203 
204  /* storage for jpiIndexArray: indexes of array */
205  struct
206  {
207  int nelems;
208  struct
209  {
212  } *elems;
213  } array;
214 
215  /* jpiAny: levels */
216  struct
217  {
220  } anybounds;
221 
222  struct
223  {
225  char *pattern; /* could not be not null-terminated */
228  } like_regex;
229 
230  /* scalars */
232  bool boolean;
233  struct
234  {
236  char *val; /* could not be not null-terminated */
237  } string;
238  } value;
239 };
240 
241 typedef struct JsonPathParseResult
242 {
244  bool lax;
246 
247 extern JsonPathParseResult *parsejsonpath(const char *str, int len);
248 
249 extern int jspConvertRegexFlags(uint32 xflags);
250 
251 #endif
int32 to
Definition: jsonpath.h:137
struct JsonPathItem::@130::@134 value
struct JsonPathItem::@130::@132::@136 * elems
void jspInit(JsonPathItem *v, JsonPath *js)
Definition: jsonpath.c:833
struct JsonPathItem JsonPathItem
char * pattern
Definition: jsonpath.h:157
bool jspGetBool(JsonPathItem *v)
Definition: jsonpath.c:1034
Definition: jsonpath.h:50
struct JsonPathItem::@130::@133 anybounds
void jspGetArg(JsonPathItem *v, JsonPathItem *a)
Definition: jsonpath.c:923
char * jspGetString(JsonPathItem *v, int32 *len)
Definition: jsonpath.c:1050
JsonPathParseResult * parsejsonpath(const char *str, int len)
char * base
Definition: jsonpath.h:116
Definition: jsonb.h:239
const char * jspOperationName(JsonPathItemType type)
Definition: jsonpath.c:718
JsonPathParseItem * arg
Definition: jsonpath.h:202
void jspGetRightArg(JsonPathItem *v, JsonPathItem *a)
Definition: jsonpath.c:1013
int32 expr
Definition: jsonpath.h:156
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:276
int32 from
Definition: jsonpath.h:136
Definition: jsonb.h:236
JsonPathParseItem * next
Definition: jsonpath.h:189
JsonPathParseItem * from
Definition: jsonpath.h:210
int32 arg
Definition: jsonpath.h:128
signed int int32
Definition: c.h:355
JsonPathParseItem * expr
Definition: jsonpath.h:243
JsonPathItemType
Definition: jsonpath.h:43
uint32 last
Definition: jsonpath.h:145
int32 vl_len_
Definition: jsonpath.h:23
uint32 patternlen
Definition: jsonpath.h:226
int32 left
Definition: jsonpath.h:123
void jspGetLeftArg(JsonPathItem *v, JsonPathItem *a)
Definition: jsonpath.c:992
char string[11]
Definition: preproc-type.c:46
union JsonPathItem::@130 content
JsonPathParseItem * left
Definition: jsonpath.h:197
unsigned int uint32
Definition: c.h:367
JsonPathParseItem * right
Definition: jsonpath.h:198
struct JsonPathParseResult JsonPathParseResult
char * data
Definition: jsonpath.h:150
JsonPathParseItem * to
Definition: jsonpath.h:211
uint32 first
Definition: jsonpath.h:144
int32 nelems
Definition: jsonpath.h:133
bool jspGetNext(JsonPathItem *v, JsonPathItem *a)
Definition: jsonpath.c:937
JsonPathItemType type
Definition: jsonpath.h:107
JsonPathItemType type
Definition: jsonpath.h:188
Numeric numeric
Definition: jsonpath.h:231
Numeric jspGetNumeric(JsonPathItem *v)
Definition: jsonpath.c:1042
int32 patternlen
Definition: jsonpath.h:158
int jspConvertRegexFlags(uint32 xflags)
struct JsonPathItem::@130::@135 like_regex
JsonPathParseItem * expr
Definition: jsonpath.h:224
int i
struct JsonPathItem::@130::@131 args
uint32 header
Definition: jsonpath.h:24
int32 datalen
Definition: jsonpath.h:151
uint32 flags
Definition: jsonpath.h:159
bool jspGetArraySubscript(JsonPathItem *v, JsonPathItem *from, JsonPathItem *to, int i)
Definition: jsonpath.c:1062
int32 nextPos
Definition: jsonpath.h:110
struct JsonPathItem::@130::@132 array
void jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
Definition: jsonpath.c:843
int32 right
Definition: jsonpath.h:124