-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonb_schema.c
323 lines (305 loc) · 8.8 KB
/
jsonb_schema.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
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/json.h"
#include "utils/jsonb.h"
#include "utils/jsonfuncs.h"
#include "catalog/pg_type.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(jsonb_extract_schema);
PG_FUNCTION_INFO_V1(jsonb_add_schema);
typedef enum {
jsbNull = 1, /* avoid '\0' character */
jsbObject,
jsbString,
jsbNumeric,
jsbBool,
jsbTuple, /* fixed length array of elements of different types */
jsbArray, /* varying length array of elements of the same type */
jsbEnd, /* end of object/array indicator */
} JsonbSchemaTag;
/* Store array/string length as 1/3/5 bytes */
static void
jsonb_encode_length(StringInfo str, unsigned len)
{
char buf[5];
if (len < 0xFE)
{
appendStringInfoCharMacro(str, (char)len);
}
else if (len <= 0xFFFF)
{
buf[0] = (char)0xFE;
buf[1] = (char)(len >> 8);
buf[2] = (char)len;
appendBinaryStringInfo(str, buf, 3);
}
else
{
buf[0] = (char)0xFF;
buf[1] = (char)(len >> 24);
buf[2] = (char)(len >> 16);
buf[3] = (char)(len >> 8);
buf[4] = (char)len;
appendBinaryStringInfo(str, buf, 5);
}
}
static unsigned
jsonb_decode_length(char** data)
{
unsigned len = *(*data)++ & 0xFF;
if (len < 0xFE)
{
return len;
}
else if (len == 0xFE)
{
len = ((**data & 0xFF) << 8) | (*(*data + 1) & 0xFF);
*data += 2;
}
else
{
len = ((**data & 0xFF) << 24) | ((*(*data + 1) & 0xFF) << 16) | ((*(*data + 2) & 0xFF) << 8) | (*(*data + 3) & 0xFF);
*data += 4;
}
return len;
}
static void jsonb_extract_object_schema(JsonbIterator **it, StringInfo schema, StringInfo data, JsonbValue* v, JsonbIteratorToken type);
static void
jsonb_extract_schema_recursive(JsonbIterator **it, StringInfo schema, StringInfo data)
{
JsonbValue v;
JsonbIteratorToken type = JsonbIteratorNext(it, &v, false);
jsonb_extract_object_schema(it, schema, data, &v, type);
}
static void
jsonb_append_value(StringInfo schema, StringInfo data, JsonbValue* v)
{
switch (v->type)
{
case jbvNull:
appendStringInfoCharMacro(schema, jsbNull);
break;
case jbvString:
appendStringInfoCharMacro(schema, jsbString);
jsonb_encode_length(data, v->val.string.len);
appendBinaryStringInfo(data, v->val.string.val, v->val.string.len);
break;
case jbvNumeric:
{
char* num_str = DatumGetCString(DirectFunctionCall1(numeric_out,
PointerGetDatum(v->val.numeric)));
size_t num_str_len = strlen(num_str);
appendStringInfoCharMacro(schema, jsbNumeric);
jsonb_encode_length(data, num_str_len);
appendBinaryStringInfo(data, num_str, num_str_len);
pfree(num_str);
break;
}
case jbvBool:
appendStringInfoCharMacro(schema, jsbBool);
appendStringInfoCharMacro(data, v->val.boolean ? 't' : 'f');
break;
default:
Assert(false);
}
}
static void
jsonb_extract_object_schema(JsonbIterator **it, StringInfo schema, StringInfo data, JsonbValue* v, JsonbIteratorToken type)
{
switch (type)
{
case WJB_BEGIN_ARRAY:
{
int arrayStart = schema->len; /* Start of array schema defintion */
int schemaSize = 0; /* zero is used as indicator of first element */
bool isTuple = false; /* true is array elements has different schemas */
appendStringInfoCharMacro(schema, jsbArray);
jsonb_encode_length(data, v->val.array.nElems);
while ((type = JsonbIteratorNext(it, v, false)) != WJB_END_ARRAY)
{
int schemaStart = schema->len; /* remember array element schema start position */
jsonb_extract_object_schema(it, schema, data, v, type);
/*
* If all array elements has the same schema, then we store this schema just once (jsbArray).
* Otherwise (jsbTuple) we store schemas for each element terminated with jsbEnd byte.
*/
if (!isTuple) /* If we found difference in element schema, no need to repeat this check */
{
if (schemaSize != 0) /* schemaSize is zero for first element */
{
isTuple = schema->len - schemaStart != schemaSize /* length of schemas is different... */
|| memcmp(&schema->data[arrayStart+1], &schema->data[schemaStart], schemaSize) != 0; /* ... or content */
}
else
{
schemaSize = schema->len - schemaStart; /* remember length of schema of first element: used in schemas comparison */
}
}
}
if (!isTuple)
{
schema->len = arrayStart + schemaSize + 1; /* keep just one schema */
}
else
{
schema->data[arrayStart] = jsbTuple; /* mark it as tuple... */
appendStringInfoCharMacro(schema, jsbEnd); /* ... and add terminator */
}
break;
}
case WJB_BEGIN_OBJECT:
{
appendStringInfoCharMacro(schema, jsbObject);
while ((type = JsonbIteratorNext(it, v, false)) != WJB_END_OBJECT)
{
Assert(type == WJB_KEY);
jsonb_extract_object_schema(it, schema, data, v, type);
}
appendStringInfoCharMacro(schema, 0); /* empty key name */
appendStringInfoCharMacro(schema, jsbEnd);
break;
}
case WJB_KEY:
{
jsonb_encode_length(schema, v->val.string.len);
appendBinaryStringInfo(schema, v->val.string.val, v->val.string.len);
type = JsonbIteratorNext(it, v, false);
if (type == WJB_VALUE) /* scalar value */
{
jsonb_append_value(schema, data, v);
}
else /* compound value */
{
Assert(type == WJB_BEGIN_OBJECT || type == WJB_BEGIN_ARRAY);
jsonb_extract_object_schema(it, schema, data, v, type);
}
break;
}
case WJB_ELEM:
{
jsonb_append_value(schema, data, v);
break;
}
default:
elog(ERROR, "unknown jsonb iterator token type %d", type);
}
}
/*
* Split JSONB object into schema and data
*/
Datum
jsonb_extract_schema(PG_FUNCTION_ARGS)
{
Jsonb *in = PG_GETARG_JSONB_P(0);
StringInfoData schema;
StringInfoData data;
JsonbIterator *it = JsonbIteratorInit(&in->root);
Datum values[2];
bool isnull[2];
TupleDesc resultTupleDesc;
get_call_result_type(fcinfo, NULL, &resultTupleDesc);
initStringInfo(&schema);
initStringInfo(&data);
schema.len = VARHDRSZ;
data.len = VARHDRSZ;
jsonb_extract_schema_recursive(&it, &schema, &data);
SET_VARSIZE(schema.data, schema.len);
SET_VARSIZE(data.data, data.len);
values[0] = PointerGetDatum(schema.data);
values[1] = PointerGetDatum(data.data);
isnull[0] = isnull[1] = false;
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(resultTupleDesc, values, isnull)));
}
static JsonbValue *
jsonb_add_schema_recursive(JsonbParseState **state, JsonbIteratorToken token, char** schema, char** data)
{
JsonbValue v;
int len;
char* arraySchema;
char* num_str;
switch (*(*schema)++)
{
case jsbNull:
v.type = jbvNull;
return pushJsonbValue(state, token, &v);
case jsbBool:
v.type = jbvBool;
v.val.boolean = *(*data)++ == 't';
return pushJsonbValue(state, token, &v);
case jsbTuple:
len = jsonb_decode_length(data);
pushJsonbValue(state, WJB_BEGIN_ARRAY, NULL);
while (**schema != jsbEnd)
{
jsonb_add_schema_recursive(state, WJB_ELEM, schema, data);
len -= 1;
}
Assert(len == 0);
*schema += 1; /* skip jsbEnd */
return pushJsonbValue(state, WJB_END_ARRAY, NULL);
case jsbArray:
len = jsonb_decode_length(data);
arraySchema = *schema;
pushJsonbValue(state, WJB_BEGIN_ARRAY, NULL);
while (--len >= 0)
{
*schema = arraySchema; /* reset pointer to element schema */
jsonb_add_schema_recursive(state, WJB_ELEM, schema, data);
}
return pushJsonbValue(state, WJB_END_ARRAY, NULL);
case jsbObject:
pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL);
while (true)
{
v.type = jbvString;
v.val.string.len = jsonb_decode_length(schema);
v.val.string.val = *schema;
*schema += v.val.string.len;
if (**schema == jsbEnd)
{
*schema += 1; /* skip jsbEnd */
return pushJsonbValue(state, WJB_END_OBJECT, NULL);
}
pushJsonbValue(state, WJB_KEY, &v);
jsonb_add_schema_recursive(state, WJB_VALUE, schema, data);
}
case jsbString:
v.type = jbvString;
v.val.string.len = jsonb_decode_length(data);
v.val.string.val = *data;
*data += v.val.string.len;
return pushJsonbValue(state, token, &v);
case jsbNumeric:
len = jsonb_decode_length(data);
num_str = (char*)palloc(len+1);
memcpy(num_str, *data, len);
num_str[len] = '\0';
v.type = jbvNumeric;
v.val.numeric = DatumGetNumeric(DirectFunctionCall3(numeric_in,
CStringGetDatum(num_str),
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(-1)));
pfree(num_str);
*data += len;
return pushJsonbValue(state, token, &v);
default:
Assert(false);
}
return NULL;
}
/*
* Combine schema and data to restore original JSONB object
*/
Datum
jsonb_add_schema(PG_FUNCTION_ARGS)
{
char* schema = VARDATA(PG_GETARG_BYTEA_P(0));
char* data = VARDATA(PG_GETARG_BYTEA_P(1));
JsonbParseState *state = NULL;
JsonbValue *res = jsonb_add_schema_recursive(&state, WJB_VALUE, &schema, &data);
PG_RETURN_POINTER(JsonbValueToJsonb(res));
}