-
Notifications
You must be signed in to change notification settings - Fork 49
/
jsquery.h
261 lines (225 loc) · 5.44 KB
/
jsquery.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*-------------------------------------------------------------------------
*
* jsquery.h
* Definitions of jsquery datatype
*
* Copyright (c) 2014, PostgreSQL Global Development Group
* Portions Copyright (c) 2017-2021, Postgres Professional
* Author: Teodor Sigaev <teodor@sigaev.ru>
*
* IDENTIFICATION
* contrib/jsquery/jsquery.h
*
*-------------------------------------------------------------------------
*/
#ifndef __JSQUERY_H__
#define __JSQUERY_H__
#include "access/gin.h"
#include "fmgr.h"
#include "utils/numeric.h"
#include "utils/jsonb.h"
typedef struct
{
int32 vl_len_; /* varlena header (do not touch directly!) */
} JsQuery;
#define DatumGetJsQueryP(d) ((JsQuery*) PG_DETOAST_DATUM(d))
#define PG_GETARG_JSQUERY(x) DatumGetJsQueryP(PG_GETARG_DATUM(x))
#define PG_RETURN_JSQUERY(p) PG_RETURN_POINTER(p)
typedef enum JsQueryItemType {
jqiNull = jbvNull,
jqiString = jbvString,
jqiNumeric = jbvNumeric,
jqiBool = jbvBool,
jqiArray = jbvArray,
jqiAnd,
jqiOr,
jqiNot,
jqiEqual,
jqiLess,
jqiGreater,
jqiLessOrEqual,
jqiGreaterOrEqual,
jqiContains,
jqiContained,
jqiOverlap,
jqiAny,
jqiAnyArray,
jqiAnyKey,
jqiAll,
jqiAllArray,
jqiAllKey,
jqiKey,
jqiCurrent,
jqiLength,
jqiIn,
jqiIs,
jqiIndexArray,
jqiFilter
} JsQueryItemType;
/*
* JsQueryHint is stored in the same byte as JsQueryItemType so
* JsQueryItemType should not use three high bits
*/
typedef enum JsQueryHint {
jsqIndexDefault = 0x00,
jsqForceIndex = 0x80,
jsqNoIndex = 0x40
} JsQueryHint;
#define JSQ_HINT_MASK (jsqIndexDefault | jsqForceIndex | jsqNoIndex)
/*
* Support functions to parse/construct binary value.
* Unlike many other representation of expression the first/main
* node is not an operation but left operand of expression. That
* allows to implement cheep follow-path descending in jsonb
* structure and then execute operator with right operand which
* is always a constant.
*/
typedef struct JsQueryItem {
JsQueryItemType type;
JsQueryHint hint;
uint32 nextPos;
char *base;
union {
struct {
char *data; /* for bool, numeric and string/key */
int datalen; /* filled only for string/key */
} value;
struct {
int32 left;
int32 right;
} args;
int32 arg;
struct {
int nelems;
int current;
int32 *arrayPtr;
} array;
uint32 arrayIndex;
};
} JsQueryItem;
extern void jsqInit(JsQueryItem *v, JsQuery *js);
extern void jsqInitByBuffer(JsQueryItem *v, char *base, int32 pos);
extern bool jsqGetNext(JsQueryItem *v, JsQueryItem *a);
extern void jsqGetArg(JsQueryItem *v, JsQueryItem *a);
extern void jsqGetLeftArg(JsQueryItem *v, JsQueryItem *a);
extern void jsqGetRightArg(JsQueryItem *v, JsQueryItem *a);
extern Numeric jsqGetNumeric(JsQueryItem *v);
extern bool jsqGetBool(JsQueryItem *v);
extern int32 jsqGetIsType(JsQueryItem *v);
extern char * jsqGetString(JsQueryItem *v, int32 *len);
extern void jsqIterateInit(JsQueryItem *v);
extern bool jsqIterateArray(JsQueryItem *v, JsQueryItem *e);
extern void jsqIterateDestroy(JsQueryItem *v);
void alignStringInfoInt(StringInfo buf);
/*
* Parsing
*/
typedef struct JsQueryParseItem JsQueryParseItem;
struct JsQueryParseItem {
JsQueryItemType type;
JsQueryHint hint;
JsQueryParseItem *next; /* next in path */
bool filter;
union {
struct {
JsQueryParseItem *left;
JsQueryParseItem *right;
} args;
JsQueryParseItem *arg;
int8 isType; /* jbv* values */
Numeric numeric;
bool boolean;
struct {
uint32 len;
char *val; /* could not be not null-terminated */
} string;
struct {
int nelems;
JsQueryParseItem **elems;
} array;
uint32 arrayIndex;
};
};
extern JsQueryParseItem* parsejsquery(const char *str, int len);
/* jsquery_extract.c */
typedef enum
{
iAny = jqiAny,
iAnyArray = jqiAnyArray,
iKey = jqiKey,
iAnyKey = jqiAnyKey,
iIndexArray = jqiIndexArray
} PathItemType;
typedef struct PathItem PathItem;
struct PathItem
{
PathItemType type;
int len;
int arrayIndex;
char *s;
PathItem *parent;
};
typedef enum
{
eExactValue = 1,
eEmptyArray,
eInequality,
eIs,
eAny,
eAnd = jqiAnd,
eOr = jqiOr,
} ExtractedNodeType;
typedef enum
{
sEqual = 1,
sRange,
sInequal,
sIs,
sAny
} SelectivityClass;
typedef struct ExtractedNode ExtractedNode;
struct ExtractedNode
{
ExtractedNodeType type;
JsQueryHint hint;
PathItem *path;
bool indirect;
SelectivityClass sClass;
bool forceIndex;
int number;
int entryNum;
union
{
struct
{
ExtractedNode **items;
int count;
} args;
struct
{
bool leftInclusive;
bool rightInclusive;
JsQueryItem *leftBound;
JsQueryItem *rightBound;
} bounds;
JsQueryItem *exactValue;
int32 isType;
};
};
typedef int (*MakeEntryHandler)(ExtractedNode *node, Pointer extra);
typedef bool (*CheckEntryHandler)(ExtractedNode *node, Pointer extra);
bool isLogicalNodeType(ExtractedNodeType type);
ExtractedNode *extractJsQuery(JsQuery *jq, MakeEntryHandler makeHandler,
CheckEntryHandler checkHandler, Pointer extra);
char *debugJsQuery(JsQuery *jq, MakeEntryHandler makeHandler,
CheckEntryHandler checkHandler, Pointer extra);
bool queryNeedRecheck(ExtractedNode *node);
bool execRecursive(ExtractedNode *node, bool *check);
bool execRecursiveTristate(ExtractedNode *node, GinTernaryValue *check);
#ifndef PG_RETURN_JSONB_P
#define PG_RETURN_JSONB_P(x) PG_RETURN_JSONB(x)
#endif
#ifndef PG_GETARG_JSONB_P
#define PG_GETARG_JSONB_P(x) PG_GETARG_JSONB(x)
#endif
#endif