]> git.8kb.co.uk Git - postgresql/pg_jsonb_opx/blob - jsonb_opx.c
Initial commit
[postgresql/pg_jsonb_opx] / jsonb_opx.c
1 /* 
2  * jsonb_opx.c 
3  *     Test jsonb delete and concatenate operator functions for 9.4
4  *
5  * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  * Author: Glyn Astill <glyn@8kb.co.uk>
8  *
9  * This is purely experimentation and will contain many errors and bad form
10  * DO NOT USE ON PRODUCTION SYSTEMS.
11  *
12  */
13
14 #include "postgres.h"
15 #include "fmgr.h"
16 #include "utils/array.h"
17 #include "utils/jsonb.h"
18 #include "catalog/pg_type.h"
19 #include "utils/builtins.h"
20
21 #ifdef PG_MODULE_MAGIC
22         PG_MODULE_MAGIC;
23 #endif 
24
25 Datum jsonb_delete_text(PG_FUNCTION_ARGS);
26
27 PG_FUNCTION_INFO_V1(jsonb_delete_text);
28
29 /*
30  * Operator function to delete keys from left operand where a match is found in
31  * the right operand.
32  *
33  * jsonb, text[] -> jsonb
34  *
35  */
36 Datum 
37 jsonb_delete_text(PG_FUNCTION_ARGS)
38 {
39     /* general loops */
40         int i;
41
42     /* pointers to incoming jsonb and text[] data */
43     Jsonb *input_jsonb = PG_GETARG_JSONB(0);
44         ArrayType *input_array = PG_GETARG_ARRAYTYPE_P(1);
45     
46     /* pointers to return jsonb_value data and state to be converted to jsonb on return */
47     JsonbParseState *state = NULL;
48     JsonbValue *return_jsonb_value = NULL;
49
50     /* pointer to iterator for input_jsonb and lookup value data */
51     JsonbValue  jsonb_lookup_value;
52     JsonbValue *jsonb_value = NULL;
53     JsonbIterator *jsonb_iterator;
54     JsonbValue  jsonb_iterator_value;
55     int32 jsonb_iterator_token;
56
57     /* variables used for skip logic */
58     int32 skip_key = 0;
59     int32 nest_level = 0;
60     int32 array_level = 0;
61
62         /* array element variables for use during deconstruction */
63         Datum *datums;
64         bool *nulls;
65         int32 count;
66
67     /* individual array values values from incoming text[] */
68     text *array_element_text;
69     bool exists = false;
70
71     /* check that supplied jsonb isn't non object, i.e. scalar or array */
72     if (!JB_ROOT_IS_OBJECT(input_jsonb))
73         ereport(ERROR,
74             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
75                 errmsg("cannot call on a non-object")));
76
77     /* assert input_array is a text array type */
78     Assert(ARR_ELEMTYPE(input_array) == TEXTOID);
79
80     /* check input_array is one-dimensional */
81     if (ARR_NDIM(input_array) > 1)
82         ereport(ERROR, 
83             (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
84                 errmsg("1 dimensional text array expected")));
85
86     /* deconstruct array elements */
87     deconstruct_array(input_array, TEXTOID, -1, false, 'i', 
88                        &datums, &nulls, &count);
89
90     /* first check to make sure at least one key exists - this is potentially just extra unwanted work */
91     for (i=0; i<count; i++)
92     {
93         if (nulls[i])
94             continue;
95
96         array_element_text = DatumGetTextP(datums[i]);
97
98         jsonb_lookup_value.type = jbvString;
99         jsonb_lookup_value.val.string.val = VARDATA_ANY(array_element_text);
100         jsonb_lookup_value.val.string.len = VARSIZE_ANY_EXHDR(array_element_text);
101             
102         jsonb_value = findJsonbValueFromContainer(&input_jsonb->root,
103             JB_FOBJECT | JB_FARRAY, &jsonb_lookup_value);
104
105         if (jsonb_value != NULL) {
106             exists = true;
107             break;
108         }
109     }
110
111     if (!exists) 
112         PG_RETURN_JSONB(input_jsonb);
113
114     /*
115     * If we've been supplied with existing keys iterate round json data matching those keys.
116     *
117     * skip_key, nest_level and array_level are crude counts to check if the the value for the key is closed
118     * and ensure we don't match on keys within nested objects.  Because we are recursing into nested elements
119     * but blindly just pushing them onto the return value we can get away without deeper knowledge of the json?
120     */
121
122     jsonb_iterator = JsonbIteratorInit(&input_jsonb->root);
123
124     while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, false)) != WJB_DONE) {
125
126         switch (jsonb_iterator_token)
127         {
128         case WJB_BEGIN_ARRAY:
129             array_level++;
130             if (skip_key == 0)
131                 return_jsonb_value = pushJsonbValue(&state, WJB_BEGIN_ARRAY, &jsonb_iterator_value);
132             break;
133         case WJB_BEGIN_OBJECT:
134             nest_level++;
135             if (skip_key == 0)
136                 return_jsonb_value = pushJsonbValue(&state, WJB_BEGIN_OBJECT, &jsonb_iterator_value);
137             break;
138         case WJB_KEY:
139             /* Check each key against our array of keys */
140             if (skip_key > 0) {
141                 skip_key++;
142             }
143             else if (nest_level == 1){
144                 for (i=0; i<count; i++)
145                 {
146                     if (nulls[i])
147                         continue;
148
149                     array_element_text = DatumGetTextP(datums[i]);
150
151                     if (strcmp(VARDATA(array_element_text), pnstrdup(jsonb_iterator_value.val.string.val,jsonb_iterator_value.val.string.len)) == 0) {
152                         skip_key++;
153                         break;
154                     }
155                 }
156             }
157
158             if (skip_key == 0)
159                 return_jsonb_value = pushJsonbValue(&state, WJB_KEY, &jsonb_iterator_value);
160             break;
161         case WJB_VALUE:
162             if (skip_key == 0)
163                 return_jsonb_value = pushJsonbValue(&state, WJB_VALUE, &jsonb_iterator_value);
164             else if (skip_key > 0)
165                 skip_key--;
166             break;
167         case WJB_ELEM:
168             if (skip_key == 0)
169                 return_jsonb_value = pushJsonbValue(&state, WJB_ELEM, &jsonb_iterator_value);
170             break;
171         case WJB_END_ARRAY:
172             array_level--;
173             if (skip_key == 0)
174                 return_jsonb_value = pushJsonbValue(&state, WJB_END_ARRAY, NULL);
175             else if (skip_key > 0 && array_level == 0)
176                 skip_key--;
177             break;
178         case WJB_END_OBJECT:
179             nest_level--;
180             if (skip_key == 0)
181                 return_jsonb_value = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
182             else if (skip_key > 0)
183                 skip_key--;
184             break;
185         default:
186             elog(ERROR, "invalid JsonbIteratorNext rc: %d", jsonb_iterator_token);
187         } 
188     }
189     PG_FREE_IF_COPY(input_jsonb, 0); 
190     PG_FREE_IF_COPY(input_array, 1); 
191
192     PG_RETURN_JSONB(JsonbValueToJsonb(return_jsonb_value));
193 }
194
195 Datum jsonb_delete_jsonb(PG_FUNCTION_ARGS);
196
197 PG_FUNCTION_INFO_V1(jsonb_delete_jsonb);
198
199 /*
200  * Operator function to delete keys and values from left operand where a match 
201  * is found in the right operand.
202  *
203  * jsonb, jsonb -> jsonb
204  *
205  */
206 Datum 
207 jsonb_delete_jsonb(PG_FUNCTION_ARGS)
208 {
209     /* pointers to incoming jsonb and text[] data */
210     Jsonb *input_jsonb_a = PG_GETARG_JSONB(0);
211     Jsonb *input_jsonb_b = PG_GETARG_JSONB(1);
212     
213     /* pointers to return jsonb_value data and state to be converted to jsonb on return */
214     JsonbValue *return_jsonb_value = NULL;
215     JsonbParseState *state = NULL;
216
217     /* pointer to iterator for input_jsonb_a and temporary value data */
218     JsonbIterator *jsonb_iterator;
219     JsonbValue  jsonb_iterator_value;
220     JsonbValue  jsonb_iterator_key;
221     int32 jsonb_iterator_token;
222     bool skip_nested = false;
223     
224     /* pointer to iterator and container for pushing nested parts of input_jsonb_a */
225     JsonbContainer *nest_jsonb_container_a;
226     JsonbIterator *nest_jsonb_iterator;
227     bool push = true;
228
229     /* inner iterator for iterating around jbvBinary types */
230     JsonbValue  nest_jsonb_iterator_value;
231     int32 nest_jsonb_iterator_token;
232
233     /* pointer to lookup on input_jsonb_b */
234     JsonbValue *jsonb_lookup_value = NULL;
235     
236     jsonb_iterator = JsonbIteratorInit(&input_jsonb_a->root);
237
238     while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, skip_nested)) != WJB_DONE) {
239         skip_nested = true;
240
241         switch (jsonb_iterator_token)
242         {
243         case WJB_BEGIN_ARRAY:
244         case WJB_BEGIN_OBJECT:
245         case WJB_ELEM:
246         case WJB_END_ARRAY:
247         case WJB_END_OBJECT:
248             return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
249         break;
250         case WJB_KEY:
251              jsonb_lookup_value = findJsonbValueFromContainer(&input_jsonb_b->root, JB_FOBJECT | JB_FARRAY, &jsonb_iterator_value);
252              jsonb_iterator_key = jsonb_iterator_value;
253
254              jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, skip_nested);
255              if (jsonb_iterator_token != WJB_VALUE)
256                  elog(ERROR, "invalid JsonbIteratorNext (expected WJB_VALUE) rc: %d", jsonb_iterator_token);
257
258              if (jsonb_lookup_value != NULL) {
259                 if (jsonb_lookup_value->type == jsonb_iterator_value.type) {
260                     switch (jsonb_lookup_value->type) {
261                         case jbvNumeric:
262                             if (strcmp(
263                                 DatumGetCString(DirectFunctionCall1(numeric_out, PointerGetDatum(jsonb_lookup_value->val.numeric))),
264                                 DatumGetCString(DirectFunctionCall1(numeric_out, PointerGetDatum(jsonb_iterator_value.val.numeric)))
265                                 ) == 0) 
266                                 push = false;
267                         break;
268                         case jbvString:
269                             if (strcmp(
270                                 pnstrdup(jsonb_lookup_value->val.string.val,jsonb_lookup_value->val.string.len),
271                                 pnstrdup(jsonb_iterator_value.val.string.val,jsonb_iterator_value.val.string.len)
272                                 ) == 0)
273                                 push = false;
274                         break;
275                         case jbvBinary:
276                             if (strcmp(
277                                 JsonbToCString(NULL, jsonb_lookup_value->val.binary.data, jsonb_lookup_value->val.binary.len),
278                                 JsonbToCString(NULL, jsonb_iterator_value.val.binary.data, jsonb_lookup_value->val.binary.len)
279                                 ) == 0)
280                                 push = false;
281                         break;
282                         case jbvBool:
283                             if (jsonb_lookup_value->val.boolean == jsonb_iterator_value.val.boolean)
284                                 push = false;
285                         break;
286                         case jbvArray:
287                         /* should not be possible? */
288                         case jbvObject:
289                         /* should not be possible? */
290                         default:
291                         ereport(NOTICE, (errcode(ERRCODE_SUCCESSFUL_COMPLETION), errmsg("unexpected lookup type")));
292                     }
293                 }
294             }
295
296             if (push) {                
297                 return_jsonb_value = pushJsonbValue(&state, WJB_KEY, &jsonb_iterator_key);
298
299                 /* if our value is nested binary data, iterate separately pushing each val */
300                 if (jsonb_iterator_value.type == jbvBinary) {
301                     nest_jsonb_container_a = jsonb_iterator_value.val.binary.data;
302
303                     nest_jsonb_iterator = JsonbIteratorInit(nest_jsonb_container_a);
304                     while ((nest_jsonb_iterator_token = JsonbIteratorNext(&nest_jsonb_iterator, &nest_jsonb_iterator_value, false)) != WJB_DONE) {
305                         return_jsonb_value = pushJsonbValue(&state, nest_jsonb_iterator_token, &nest_jsonb_iterator_value);
306                     }
307                 }
308                 else {
309                     return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
310                 }
311             }
312             else 
313                 push = true;
314             break;
315         case WJB_VALUE:
316             /* should not be possible */
317         default:
318             elog(ERROR, "invalid JsonbIteratorNext rc: %d", jsonb_iterator_token);
319         }
320
321     }
322
323     PG_FREE_IF_COPY(input_jsonb_a, 0); 
324     PG_FREE_IF_COPY(input_jsonb_b, 1); 
325
326     PG_RETURN_JSONB(JsonbValueToJsonb(return_jsonb_value));
327 }
328
329 Datum jsonb_concat_jsonb(PG_FUNCTION_ARGS);
330
331 PG_FUNCTION_INFO_V1(jsonb_concat_jsonb);
332
333 /*
334  * Operator function to concatenate json from left operand where a match 
335  * is found in the right operand.
336  *
337  * jsonb, jsonb -> jsonb
338  *
339  */
340 Datum 
341 jsonb_concat_jsonb(PG_FUNCTION_ARGS)
342 {
343     /* incoming jsonb and text[] data */
344     Jsonb *input_jsonb_a = PG_GETARG_JSONB(0);
345     Jsonb *input_jsonb_b = PG_GETARG_JSONB(1);
346     
347     /* return jsonb_value data to be converted to jsonb on return */
348     JsonbValue *return_jsonb_value = NULL;
349
350     /* iterator for input_jsonb_b */
351     JsonbIterator *jsonb_iterator;
352     JsonbValue  jsonb_iterator_value;
353     int32 jsonb_iterator_token;
354
355     JsonbParseState *state = NULL;
356     bool skip_nested = false;
357
358     int32 nest_level = 0;
359    
360     /* check that supplied jsonb isn't non object, i.e. scalar or array */
361     if (!JB_ROOT_IS_OBJECT(input_jsonb_a) || !JB_ROOT_IS_OBJECT(input_jsonb_b))
362         ereport(ERROR,
363             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
364                 errmsg("cannot call on a non-object")));
365
366     /*
367      * The following is essentially a cut 'n shut job; discarding the closing root 
368      * object token from the first jsonb value and the opening one from the second.
369      * Values from each are just blindly pushed onto the return value leaving
370      * deduplication down to lower level jsonb logic.
371      */
372
373     jsonb_iterator = JsonbIteratorInit(&input_jsonb_a->root);
374
375     while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, skip_nested)) != WJB_DONE) {
376
377         switch (jsonb_iterator_token)
378         {
379         case WJB_BEGIN_ARRAY:
380         case WJB_KEY:
381         case WJB_VALUE:
382         case WJB_ELEM:
383         case WJB_END_ARRAY:
384             return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
385             break;
386         case WJB_BEGIN_OBJECT:
387             return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
388             nest_level++;
389             break;
390         case WJB_END_OBJECT:
391             nest_level--;
392             if (nest_level != 0)
393                 return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
394             break;
395         default:
396             elog(ERROR, "invalid JsonbIteratorNext rc: %d", jsonb_iterator_token);
397         }
398
399     }
400
401     nest_level = 0;
402     jsonb_iterator = JsonbIteratorInit(&input_jsonb_b->root);
403
404     while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, skip_nested)) != WJB_DONE) {
405
406         switch (jsonb_iterator_token)
407         {
408         case WJB_BEGIN_ARRAY:
409         case WJB_KEY:
410         case WJB_VALUE:
411         case WJB_ELEM:
412         case WJB_END_ARRAY:
413             return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
414             break;
415         case WJB_BEGIN_OBJECT:
416             if (nest_level != 0)
417                 return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
418             nest_level++;
419             break;
420         case WJB_END_OBJECT:
421             nest_level--;
422             return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
423             break;
424         default:
425             elog(ERROR, "invalid JsonbIteratorNext rc: %d", jsonb_iterator_token);
426         }
427
428     }
429
430     PG_FREE_IF_COPY(input_jsonb_a, 0); 
431     PG_FREE_IF_COPY(input_jsonb_b, 1); 
432
433     PG_RETURN_JSONB(JsonbValueToJsonb(return_jsonb_value));
434 }