Repository navigation
Expand file tree
/
Copy pathdecode_org.sql
More file actions
353 lines (307 loc) · 10.7 KB
/
Copy pathdecode_org.sql
File metadata and controls
353 lines (307 loc) · 10.7 KB
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
352
353
create or replace function msgpack_decode(_data bytea) returns jsonb language plpgsql
as $function$
declare _length integer = octet_length(_data);
declare _cursor integer = 0;
declare _size integer;
declare _wrapper jsonb[];
declare _counter integer[];
declare _key text[];
declare _json jsonb;
declare _binary text;
declare _exponent integer;
declare _mantissa text;
declare _float double precision;
declare _index integer;
begin
while _cursor < _length loop
case get_byte(_data, _cursor)
when 192 then -- null
_json = 'null'::jsonb;
_cursor = _cursor + 1;
when 194 then -- false
_json = to_jsonb(false);
_cursor = _cursor + 1;
when 195 then -- true
_json = to_jsonb(true);
_cursor = _cursor + 1;
when 196 then -- bin 8
_json = 'null'::jsonb;
_size = get_byte(_data, _cursor + 1);
_cursor = _cursor + 2 + _size;
when 197 then -- bin 16
_json = 'null'::jsonb;
_size = (get_byte(_data, _cursor + 1) << 8)
+ get_byte(_data, _cursor + 2);
_cursor = _cursor + 3 + _size;
when 198 then -- bin 32
_json = 'null'::jsonb;
_size = (get_byte(_data, _cursor + 1) << 24)
+ (get_byte(_data, _cursor + 2) << 16)
+ (get_byte(_data, _cursor + 3) << 8)
+ get_byte(_data, _cursor + 4);
_cursor = _cursor + 5 + _size;
when 199 then -- ext 8
_json = 'null'::jsonb;
_size = get_byte(_data, _cursor + 1);
_cursor = _cursor + 3 + _size;
when 200 then -- ext 16
_json = 'null'::jsonb;
_size = (get_byte(_data, _cursor + 1) << 8)
+ get_byte(_data, _cursor + 2);
_cursor = _cursor + 4 + _size;
when 201 then -- ext 32
_json = 'null'::jsonb;
_size = (get_byte(_data, _cursor + 1) << 24)
+ (get_byte(_data, _cursor + 2) << 16)
+ (get_byte(_data, _cursor + 3) << 8)
+ get_byte(_data, _cursor + 4);
_cursor = _cursor + 6 + _size;
when 202 then -- float 32
_binary = get_byte(_data, _cursor + 1)::bit(8)
|| get_byte(_data, _cursor + 2)::bit(8)
|| get_byte(_data, _cursor + 3)::bit(8)
|| get_byte(_data, _cursor + 4)::bit(8);
_exponent = substring(_binary, 2, 8)::bit(8)::integer;
_mantissa = substring(_binary, 10, 23);
if _exponent = 255 then
-- Infinity, -Infinity, NaN
_json = 'null'::jsonb;
else
_float = 1;
_index = 1;
_exponent = _exponent - 127;
while _index < 24 loop
if substring(_mantissa, _index, 1) = '1' then
_float = _float + (2 ^ -(_index));
end if;
_index = _index + 1;
end loop;
_float = _float * (2 ^ _exponent);
if substring(_binary, 1, 1) = '1' then
_float = -_float;
end if;
end if;
_json = to_jsonb(_float);
_cursor = _cursor + 5;
when 203 then -- float 64
_binary = get_byte(_data, _cursor + 1)::bit(8)
|| get_byte(_data, _cursor + 2)::bit(8)
|| get_byte(_data, _cursor + 3)::bit(8)
|| get_byte(_data, _cursor + 4)::bit(8)
|| get_byte(_data, _cursor + 5)::bit(8)
|| get_byte(_data, _cursor + 6)::bit(8)
|| get_byte(_data, _cursor + 7)::bit(8)
|| get_byte(_data, _cursor + 8)::bit(8);
_exponent = substring(_binary, 2, 11)::bit(11)::integer;
_mantissa = substring(_binary, 13, 52);
if _exponent = 2047 then
-- Infinity, -Infinity, NaN
_json = 'null'::jsonb;
else
_float = 1;
_index = 1;
_exponent = _exponent - 1023;
while _index < 53 loop
if substring(_mantissa, _index, 1) = '1' then
_float = _float + (2 ^ -(_index));
end if;
_index = _index + 1;
end loop;
_float = _float * (2 ^ _exponent);
if substring(_binary, 1, 1) = '1' then
_float = -_float;
end if;
end if;
_json = to_jsonb(_float);
_cursor = _cursor + 9;
when 204 then -- uint 8
_json = to_jsonb(get_byte(_data, _cursor + 1));
_cursor = _cursor + 2;
when 205 then -- uint 16
_json = to_jsonb((get_byte(_data, _cursor + 1) << 8)
+ get_byte(_data, _cursor + 2));
_cursor = _cursor + 3;
when 206 then -- uint 32
_json = to_jsonb((get_byte(_data, _cursor + 1) << 24)
+ (get_byte(_data, _cursor + 2) << 16)
+ (get_byte(_data, _cursor + 3) << 8)
+ get_byte(_data, _cursor + 4));
_cursor = _cursor + 5;
when 207 then -- uint 64
_json = to_jsonb((get_byte(_data, _cursor + 1)::bigint << 56)
+ (get_byte(_data, _cursor + 2)::bigint << 48)
+ (get_byte(_data, _cursor + 3)::bigint << 40)
+ (get_byte(_data, _cursor + 4)::bigint << 32)
+ (get_byte(_data, _cursor + 5)::bigint << 24)
+ (get_byte(_data, _cursor + 6)::bigint << 16)
+ (get_byte(_data, _cursor + 7)::bigint << 8)
+ get_byte(_data, _cursor + 8)::bigint);
_cursor = _cursor + 9;
when 208 then -- int 8
_json = to_jsonb(-(2 ^ 8 - get_byte(_data, _cursor + 1)));
_cursor = _cursor + 2;
when 209 then -- int 16
_json = to_jsonb(-(2 ^ 16 - (get_byte(_data, _cursor + 1) << 8)
- get_byte(_data, _cursor + 2)));
_cursor = _cursor + 3;
when 210 then -- int 32
_json = to_jsonb((get_byte(_data, _cursor + 1) << 24)
+ (get_byte(_data, _cursor + 2) << 16)
+ (get_byte(_data, _cursor + 3) << 8)
+ get_byte(_data, _cursor + 4));
_cursor = _cursor + 5;
when 211 then -- int 64
_json = to_jsonb((get_byte(_data, _cursor + 1)::bigint << 56)
+ (get_byte(_data, _cursor + 2)::bigint << 48)
+ (get_byte(_data, _cursor + 3)::bigint << 40)
+ (get_byte(_data, _cursor + 4)::bigint << 32)
+ (get_byte(_data, _cursor + 5)::bigint << 24)
+ (get_byte(_data, _cursor + 6)::bigint << 16)
+ (get_byte(_data, _cursor + 7)::bigint << 8)
+ get_byte(_data, _cursor + 8)::bigint);
_cursor = _cursor + 9;
when 212 then -- fixext 1
_json = 'null'::jsonb;
_cursor = _cursor + 3;
when 213 then -- fixext 2
_json = 'null'::jsonb;
_cursor = _cursor + 4;
when 214 then -- fixext 4
_json = 'null'::jsonb;
_cursor = _cursor + 6;
when 215 then -- fixext 8
_json = 'null'::jsonb;
_cursor = _cursor + 10;
when 216 then -- fixext 16
_json = 'null'::jsonb;
_cursor = _cursor + 18;
when 217 then -- str 8
_size = get_byte(_data, _cursor + 1);
_json = to_jsonb(convert_from(substring(_data, _cursor + 3, _size), 'utf8'));
_cursor = _cursor + 2 + _size;
when 218 then -- str 16
_size = (get_byte(_data, _cursor + 1) << 8)
+ get_byte(_data, _cursor + 2);
_json = to_jsonb(convert_from(substring(_data, _cursor + 4, _size), 'utf8'));
_cursor = _cursor + 3 + _size;
when 219 then -- str 32
_size = (get_byte(_data, _cursor + 1) << 24)
+ (get_byte(_data, _cursor + 2) << 16)
+ (get_byte(_data, _cursor + 3) << 8)
+ get_byte(_data, _cursor + 4);
_json = to_jsonb(convert_from(substring(_data, _cursor + 6, _size), 'utf8'));
_cursor = _cursor + 5 + _size;
when 220 then -- array 16
_size = (get_byte(_data, _cursor + 1) << 8)
+ get_byte(_data, _cursor + 2);
_json = jsonb_build_array();
_cursor = _cursor + 3;
if _size > 0 then
_wrapper = array_prepend(_json, _wrapper);
_counter = array_prepend(_size, _counter);
_key = array_prepend(null, _key);
continue;
end if;
when 221 then -- array 32
_size = (get_byte(_data, _cursor + 1) << 24)
+ (get_byte(_data, _cursor + 2) << 16)
+ (get_byte(_data, _cursor + 3) << 8)
+ get_byte(_data, _cursor + 4);
_json = jsonb_build_array();
_cursor = _cursor + 5;
if _size > 0 then
_wrapper = array_prepend(_json, _wrapper);
_counter = array_prepend(_size, _counter);
_key = array_prepend(null, _key);
continue;
end if;
when 222 then -- map 16
_size = (get_byte(_data, _cursor + 1) << 8)
+ get_byte(_data, _cursor + 2);
_json = jsonb_build_object();
_cursor = _cursor + 3;
if _size > 0 then
_wrapper = array_prepend(_json, _wrapper);
_counter = array_prepend(_size, _counter);
_key = array_prepend(null, _key);
continue;
end if;
when 223 then -- map 32
_size = (get_byte(_data, _cursor + 1) << 24)
+ (get_byte(_data, _cursor + 2) << 16)
+ (get_byte(_data, _cursor + 3) << 8)
+ get_byte(_data, _cursor + 4);
_json = jsonb_build_object();
_cursor = _cursor + 5;
if _size > 0 then
_wrapper = array_prepend(_json, _wrapper);
_counter = array_prepend(_size, _counter);
_key = array_prepend(null, _key);
continue;
end if;
else
if get_byte(_data, _cursor) & 128 = 0 then -- positive fixint
_json = to_jsonb(get_byte(_data, _cursor));
_cursor = _cursor + 1;
elsif get_byte(_data, _cursor) & 224 = 224 then -- negative fixint
_json = to_jsonb(-(255 - get_byte(_data, _cursor) + 1));
_cursor = _cursor + 1;
elsif get_byte(_data, _cursor) & 224 = 160 then -- fixstr
_size = get_byte(_data, _cursor) & 31;
_json = to_jsonb(convert_from(substring(_data, _cursor + 2, _size), 'utf8'));
_cursor = _cursor + 1 + _size;
elsif get_byte(_data, _cursor) & 240 = 144 then -- fixarray
_size = get_byte(_data, _cursor) & 15;
_json = jsonb_build_array();
_cursor = _cursor + 1;
if _size > 0 then
_wrapper = array_prepend(_json, _wrapper);
_counter = array_prepend(_size, _counter);
_key = array_prepend(null, _key);
continue;
end if;
elsif get_byte(_data, _cursor) & 240 = 128 then -- fixmap
_size = get_byte(_data, _cursor) & 15;
_json = jsonb_build_object();
_cursor = _cursor + 1;
if _size > 0 then
_wrapper = array_prepend(_json, _wrapper);
_counter = array_prepend(_size, _counter);
_key = array_prepend(null, _key);
continue;
end if;
else
raise exception 'Unknown type %.', get_byte(_data, _cursor);
end if;
end case;
if _wrapper is null then
return _json;
elsif jsonb_typeof(_wrapper[1]) = 'array' then
_wrapper[1] = _wrapper[1] || jsonb_build_array(_json);
elsif jsonb_typeof(_wrapper[1]) = 'object' then
if _key[1] is null then
_key[1] = _json#>>'{}';
continue;
end if;
_wrapper[1] = _wrapper[1] || jsonb_build_object(_key[1], _json);
_key[1] = null;
end if;
_counter[1] = _counter[1] - 1;
while _counter[1] <= 0 loop
_json = _wrapper[1];
_wrapper = _wrapper[2:array_upper(_wrapper, 1)];
_counter = _counter[2:array_upper(_counter, 1)];
_key = _key[2:array_upper(_key, 1)];
if array_length(_wrapper, 1) is null then
return _json;
elsif jsonb_typeof(_wrapper[1]) = 'array' then
_wrapper[1] = _wrapper[1] || jsonb_build_array(_json);
elsif jsonb_typeof(_wrapper[1]) = 'object' then
_wrapper[1] = _wrapper[1] || jsonb_build_object(_key[1], _json);
_key[1] = null;
end if;
_counter[1] = _counter[1] - 1;
end loop;
end loop;
end;
$function$;