-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjsonb_deep_sum.c
195 lines (172 loc) · 5.11 KB
/
jsonb_deep_sum.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
#include "postgres.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/jsonb.h"
PG_MODULE_MAGIC;
static int lengthCompareJsonbStringValue(const void *a, const void *b);
PG_FUNCTION_INFO_V1(jsonb_deep_add);
Datum
jsonb_deep_add(PG_FUNCTION_ARGS)
{
// https://trac.osgeo.org/postgis/ticket/3867
#ifdef PG_GETARG_JSONB_P
Jsonb *jb1 = PG_GETARG_JSONB_P(0);
Jsonb *jb2 = PG_GETARG_JSONB_P(1);
#else
Jsonb *jb1 = PG_GETARG_JSONB(0);
Jsonb *jb2 = PG_GETARG_JSONB(1);
#endif
JsonbIterator *it1,
*it2;
JsonbValue v1,
v2;
JsonbIteratorToken r1,
r2;
JsonbValue *res;
JsonbParseState *state = NULL;
/* Here we create the new jsonb */
int nestedLevel = 0;
if (jb1 == NULL)
#ifdef PG_RETURN_JSONB_P
PG_RETURN_JSONB_P(jb2);
#else
PG_RETURN_JSONB(jb2);
#endif
if (jb2 == NULL)
#ifdef PG_RETURN_JSONB_P
PG_RETURN_JSONB_P(jb1);
#else
PG_RETURN_JSONB(jb1);
#endif
if (!JB_ROOT_IS_OBJECT(jb1) || !JB_ROOT_IS_OBJECT(jb2))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Can only sum objects")));
it1 = JsonbIteratorInit(&jb1->root);
it2 = JsonbIteratorInit(&jb2->root);
r1 = JsonbIteratorNext(&it1, &v1, false);
r2 = JsonbIteratorNext(&it2, &v2, false);
if (r1 != WJB_BEGIN_OBJECT || r2 != WJB_BEGIN_OBJECT)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Iterator was not an object")));
pushJsonbValue(&state, r1, NULL);
/* Start object */
r1 = JsonbIteratorNext(&it1, &v1, false);
r2 = JsonbIteratorNext(&it2, &v2, false);
while (!(r1 == WJB_END_OBJECT && r2 == WJB_END_OBJECT && nestedLevel == 0))
{
int difference;
/* Datum sum; */
JsonbValue newValue;
if (r1 == WJB_END_OBJECT && r2 == WJB_END_OBJECT && nestedLevel > 0)
{
pushJsonbValue(&state, WJB_END_OBJECT, NULL);
r1 = JsonbIteratorNext(&it1, &v1, false);
r2 = JsonbIteratorNext(&it2, &v2, false);
nestedLevel--;
continue;
}
if (r1 == WJB_END_OBJECT)
{
pushJsonbValue(&state, r2, &v2);
r2 = JsonbIteratorNext(&it2, &v2, true);
continue;
}
if (r2 == WJB_END_OBJECT)
{
pushJsonbValue(&state, r1, &v1);
r1 = JsonbIteratorNext(&it1, &v1, true);
continue;
}
difference = lengthCompareJsonbStringValue(&v1, &v2);
/* first key is smaller */
if (difference < 0)
{
pushJsonbValue(&state, r1, &v1);
r1 = JsonbIteratorNext(&it1, &v1, true);
/* jbvBinary is returned in skipNested mode */
if ((&v1)->type == jbvNumeric || (&v1)->type == jbvBinary)
{
pushJsonbValue(&state, r1, &v1);
}
else
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Only numeric values allowed")));
}
r1 = JsonbIteratorNext(&it1, &v1, true);
continue;
}
else if (difference > 0)
{
pushJsonbValue(&state, r2, &v2);
r2 = JsonbIteratorNext(&it2, &v2, true);
if ((&v2)->type == jbvNumeric || (&v2)->type == jbvBinary)
{
pushJsonbValue(&state, r2, &v2);
}
else
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Only numeric values allowed")));
}
r2 = JsonbIteratorNext(&it2, &v2, true);
continue;
}
pushJsonbValue(&state, r1, &v1);
r2 = JsonbIteratorNext(&it2, &v2, false);
r1 = JsonbIteratorNext(&it1, &v1, false);
if ((&v1)->type == jbvNumeric && (&v2)->type == jbvNumeric)
{
/* TODO jbvString, jbvNull and jbvBool */
newValue.type = jbvNumeric;
newValue.val.numeric = DatumGetNumeric((DirectFunctionCall2(numeric_add, PointerGetDatum(
(&v1)->val.numeric), PointerGetDatum((&v2)->val.numeric))));
pushJsonbValue(&state, WJB_VALUE, &newValue);
}
else if ((&v1)->type == jbvObject && (&v2)->type == jbvObject)
{
pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
nestedLevel++;
}
else
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Incompatible types")));
}
r2 = JsonbIteratorNext(&it2, &v2, false);
r1 = JsonbIteratorNext(&it1, &v1, false);
}
res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
Assert(res != NULL);
#ifdef PG_RETURN_JSONB_P
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
#else
PG_RETURN_JSONB(JsonbValueToJsonb(res));
#endif
}
/* Taken from src/backend/adt/jsonb_utils.c
* Compare two jbvString JsonbValue values, a and b.
*
* This is a special qsort() comparator used to sort strings in certain
* internal contexts where it is sufficient to have a well-defined sort order.
* In particular, object pair keys are sorted according to this criteria to
* facilitate cheap binary searches where we don't care about lexical sort
* order.
*
* a and b are first sorted based on their length. If a tie-breaker is
* required, only then do we consider string binary equality.
*/
static int
lengthCompareJsonbStringValue(const void *a, const void *b)
{
const JsonbValue *va = (const JsonbValue *) a;
const JsonbValue *vb = (const JsonbValue *) b;
int res;
Assert(va->type == jbvString);
Assert(vb->type == jbvString);
if (va->val.string.len == vb->val.string.len)
{
res = memcmp(va->val.string.val, vb->val.string.val, va->val.string.len);
}
else
{
res = (va->val.string.len > vb->val.string.len) ? 1 : -1;
}
return res;
}