forked from koctep/jsonb-extend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonb_extend.c
351 lines (318 loc) · 10.1 KB
/
jsonb_extend.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*-------------------------------------------------------------------------
*
* jsonb_extend.c
* Jsonb extend function
*
* Copyright (c) 2014, Ilya Ashchepkov
*
*
*-------------------------------------------------------------------------
*/
#include <postgres.h>
#include <utils/jsonb.h>
#include <utils/lsyscache.h>
#include <access/tupmacs.h>
PG_MODULE_MAGIC;
/* export */
PG_FUNCTION_INFO_V1(jsonb_extend);
PG_FUNCTION_INFO_V1(jsonb_deep_extend);
/* internal functions */
JsonbParseState *JsonbCopyObjectValues(JsonbParseState *state, Jsonb *object, bool copyToken);
JsonbParseState *JsonbCopyIteratorValues(JsonbParseState *state, JsonbIterator *it, bool copyToken);
int jsonb_inc(JsonbIterator **it, JsonbValue *val, bool skipNested, int *lvl);
int jsonb_key_cmp(JsonbValue *val1, JsonbValue *val2);
JsonbValue *pushJsonbValue1(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *scalarVal);
/*
* JsonbCopyValue
*
* Creates new JsonbValue coping all entries from input objects/arrays/scalars
* JsonbValueToJsonb provides "one key - one value, last value wins"
*
********** JavaScript code for objects
* function jsonb_extend() {
* var res = {};
* var i = 0, len = arguments.length;
* var key;
*
* for (i; i < len; i++)
* for (key in arguments[i])
* res[key] = arguments[i][key];
* return res;
* }
********** End of JavaScript code
*/
Datum
jsonb_extend(PG_FUNCTION_ARGS)
{
bool variadic= get_fn_expr_variadic(fcinfo->flinfo);
ArrayType *arr = variadic && (PG_NARGS() > 0) ? PG_GETARG_ARRAYTYPE_P(0) : NULL;
int nargs = variadic ? ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)) : PG_NARGS(),
i,
type;
int16 typlen;
char typalign;
bool typbyval,
copyToken= false;
Jsonb *first,
*current;
JsonbValue *res;
JsonbParseState *state = NULL;
if (nargs == 0)
PG_RETURN_NULL();
if (variadic) {
get_typlenbyvalalign(ARR_ELEMTYPE(arr), &typlen, &typbyval, &typalign);
first = (Jsonb *) ARR_DATA_PTR(arr);
} else
#ifdef PG_GETARG_JSONB_P
first = PG_GETARG_JSONB_P(0);
#else
first = PG_GETARG_JSONB(0);
#endif
if (nargs == 1)
#ifdef PG_RETURN_JSONB_P
PG_RETURN_JSONB_P(first);
#else
PG_RETURN_JSONB(first);
#endif
/* Scalar as first object may be useful only if we want to prepend an array */
if (JB_ROOT_IS_SCALAR(first))
elog(ERROR, "jsonb_extend: cannot extend scalar");
type = JB_ROOT_IS_OBJECT(first) ? WJB_BEGIN_OBJECT : WJB_BEGIN_ARRAY;
pushJsonbValue1(&state, type, NULL);
for (i = 0; i < nargs; i++)
{
if (i == 0)
current = first;
else {
if (variadic) {
current = (Jsonb *)att_addlength_pointer((char *) current, typlen, (char *) current);
current = (Jsonb *)att_align_nominal((char *)current, typalign);
} else {
#ifdef PG_GETARG_JSONB_P
current = PG_GETARG_JSONB_P(i);
#else
current = PG_GETARG_JSONB(i);
#endif
}
}
if (JB_ROOT_IS_OBJECT(first) && !JB_ROOT_IS_OBJECT(current))
elog(ERROR, "jsonb_extend: object should be extended by object");
copyToken = (i != 0) && JB_ROOT_IS_ARRAY(first) && JB_ROOT_IS_OBJECT(current);
state = JsonbCopyObjectValues(state, current, copyToken);
}
type = JB_ROOT_IS_OBJECT(first) ? WJB_END_OBJECT : WJB_END_ARRAY;
res = pushJsonbValue1(&state, type, NULL);
#ifdef PG_RETURN_JSONB_P
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
#else
PG_RETURN_JSONB(JsonbValueToJsonb(res));
#endif
}
JsonbParseState*
JsonbCopyObjectValues(JsonbParseState *state, Jsonb *object, bool copyToken)
{
JsonbIterator *it;
it = JsonbIteratorInit(&(object->root));
return JsonbCopyIteratorValues(state, it, copyToken);
}
JsonbParseState*
JsonbCopyIteratorValues(JsonbParseState *state, JsonbIterator *it, bool copyToken)
{
JsonbValue val;
/* we going throught nested objects 'cause limitation of pushJsonbValue */
int skipNested = false;
int lvl = 0;
int r;
r = JsonbIteratorNext(&it, &val, skipNested);
if (copyToken)
pushJsonbValue1(&state, r, &val);
for (r = JsonbIteratorNext(&it, &val, skipNested);
r != WJB_DONE &&
(
lvl != 0 || (r != WJB_END_OBJECT && r != WJB_END_ARRAY)
);
r = JsonbIteratorNext(&it, &val, skipNested))
{
switch (r) {
case WJB_ELEM:
case WJB_KEY:
case WJB_VALUE:
pushJsonbValue1(&state, r, &val);
break;
case WJB_BEGIN_OBJECT:
case WJB_BEGIN_ARRAY:
lvl++;
pushJsonbValue1(&state, r, &val);
break;
case WJB_END_OBJECT:
case WJB_END_ARRAY:
lvl--;
pushJsonbValue1(&state, r, &val);
break;
default:
ereport(WARNING, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unparsed type %d", val.type)));
}
}
if (copyToken && (r != WJB_DONE))
pushJsonbValue1(&state, r, &val);
if (r != WJB_DONE)
r = JsonbIteratorNext(&it, &val, skipNested);
if (r != WJB_DONE)
elog(ERROR, "jsonb_extend: last token should be WJB_DONE");
return state;
}
Datum
jsonb_deep_extend(PG_FUNCTION_ARGS)
{
bool variadic = get_fn_expr_variadic(fcinfo->flinfo),
typbyval;
int16 typlen;
char typalign;
ArrayType *arr = variadic && (PG_NARGS() > 1) ? PG_GETARG_ARRAYTYPE_P(1) : NULL;
int nargs = variadic ? ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)) : PG_NARGS() - 1,
skipNested= PG_GETARG_BOOL(0),
cl = 0,
current = 0,
r[nargs],
i,
lvl[nargs];
Jsonb *first,
*object;
JsonbValue val[nargs],
*res,
key;
JsonbIterator *it[nargs];
JsonbParseState *state = NULL;
if (nargs == 0)
PG_RETURN_NULL();
if (variadic) {
get_typlenbyvalalign(ARR_ELEMTYPE(arr), &typlen, &typbyval, &typalign);
first = (Jsonb *) ARR_DATA_PTR(arr);
} else
#ifdef PG_GETARG_JSONB_P
first = PG_GETARG_JSONB_P(1);
#else
first = PG_GETARG_JSONB(1);
#endif
if (nargs == 1)
#ifdef PG_RETURN_JSONB_P
PG_RETURN_JSONB_P(first);
#else
PG_RETURN_JSONB(first);
#endif
if (!JB_ROOT_IS_OBJECT(first))
elog(ERROR, "jsonb_deep_extend: implemented only for objects");
for (i = 0; i < nargs; i++) {
lvl[i] = 0;
if (i == 0) {
object = first;
} else {
if (variadic) {
object = (Jsonb *)att_addlength_pointer((char *) object, typlen, (char *) object);
object = (Jsonb *)att_align_nominal((char *)object, typalign);
} else {
#ifdef PG_GETARG_JSONB_P
object = PG_GETARG_JSONB_P(i + 1);
#else
object = PG_GETARG_JSONB(i + 1);
#endif
}
}
it[i] = JsonbIteratorInit(&(object->root));
r[i] = jsonb_inc(&it[i], &val[i], skipNested, &lvl[i]);
r[i] = jsonb_inc(&it[i], &val[i], skipNested, &lvl[i]);
}
res = pushJsonbValue1(&state, WJB_BEGIN_OBJECT, NULL);
while(true)
{
for (i = 0, current = 0; i < nargs; i++) {
while ((r[i] != WJB_KEY) && (r[i] != WJB_DONE))
r[i] = jsonb_inc(&it[i], &val[i], skipNested, &lvl[i]);
if ((lvl[i] == lvl[current] && r[i] == WJB_KEY && jsonb_key_cmp(&val[i], &val[current]) <= 0)
|| (lvl[i] > lvl[current])
|| (r[current] == WJB_DONE))
current = i;
}
if (cl > lvl[current])
for (; cl > lvl[current]; cl--)
res = pushJsonbValue1(&state, WJB_END_OBJECT, NULL);
if (r[current] == WJB_DONE)
break;
cl = lvl[current];
key.val.string.len = val[current].val.string.len;
key.val.string.val = palloc(key.val.string.len);
strncpy(key.val.string.val, val[current].val.string.val, key.val.string.len);
res = pushJsonbValue1(&state, r[current], &val[current]);
for (i = 0; i < nargs; i++)
if (cl == lvl[i] && jsonb_key_cmp(&key, &val[i]) == 0)
r[i] = jsonb_inc(&it[i], &val[i], skipNested, &lvl[i]);
if (r[current] == WJB_BEGIN_OBJECT) {
res = pushJsonbValue1(&state, WJB_BEGIN_OBJECT, NULL);
r[current] = jsonb_inc(&it[current], &val[current], skipNested, &lvl[current]);
for (i = 0; i < nargs; i++) {
if (r[i] == WJB_KEY) continue;
if (lvl[i] < cl) continue;
if (i < current)
if (r[i] != WJB_BEGIN_OBJECT)
while (true) {
if (lvl[i] < cl) break;
if (lvl[i] == cl && r[current] == WJB_KEY) break;
r[i] = jsonb_inc(&it[i], &val[i], skipNested, &lvl[i]);
}
}
} else {
while (true) {
if (lvl[current] < cl)
break;
if (lvl[current] == cl && r[current] == WJB_KEY)
break;
res = pushJsonbValue1(&state, r[current], &val[current]);
r[current] = jsonb_inc(&it[current], &val[current], skipNested, &lvl[current]);
}
for (i = 0; i < current; i++) {
if (r[i] == WJB_KEY) continue;
while(true) {
if (lvl[i] < cl) break;
if (lvl[i] == cl && r[i] == WJB_KEY) break;
r[i] = jsonb_inc(&it[i], &val[i], skipNested, &lvl[i]);
}
}
}
pfree(key.val.string.val);
}
#ifdef PG_RETURN_JSONB_P
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
#else
PG_RETURN_JSONB(JsonbValueToJsonb(res));
#endif
}
int
jsonb_inc(JsonbIterator **it, JsonbValue *val, bool skipNested, int *lvl)
{
int r = JsonbIteratorNext(it, val, skipNested);
if (r == WJB_BEGIN_OBJECT) {
++(*lvl);
}
if (r == WJB_END_OBJECT) {
--(*lvl);
}
return r;
}
int
jsonb_key_cmp(JsonbValue *val1, JsonbValue *val2)
{
if (val1->val.string.len < val2->val.string.len)
return -1;
if (val1->val.string.len > val2->val.string.len)
return 1;
return strncmp(val1->val.string.val, val2->val.string.val, val1->val.string.len);
}
JsonbValue *
pushJsonbValue1(JsonbParseState **pstate, JsonbIteratorToken seq,
JsonbValue *scalarVal)
{
if (seq == WJB_BEGIN_ARRAY || seq == WJB_BEGIN_OBJECT || seq == WJB_END_ARRAY || seq == WJB_END_OBJECT)
return pushJsonbValue(pstate, seq, NULL);
return pushJsonbValue(pstate, seq, scalarVal);
}