Debian Codegraph @

contrib/jsonb_plperl/jsonb_plperl.c

raw
1 #include "postgres.h"
2
3 #include <math.h>
4
5 #include "fmgr.h"
6 #include "miscadmin.h"
7 #include "plperl.h"
8 #include "utils/fmgrprotos.h"
9 #include "utils/jsonb.h"
10
11 PG_MODULE_MAGIC_EXT(
12 .name = "jsonb_plperl",
13 .version = PG_VERSION
14 );
15
16 static SV *Jsonb_to_SV(JsonbContainer *jsonb);
17 static JsonbValue *SV_to_JsonbValue(SV *obj, JsonbParseState **ps, bool is_elem);
18
19
20 static SV *
21 JsonbValue_to_SV(JsonbValue *jbv)
22 {
23 dTHX;
24
25 switch (jbv->type)
26 {
27 case jbvBinary:
28 return Jsonb_to_SV(jbv->val.binary.data);
29
30 case jbvNumeric:
31 {
32 char *str = DatumGetCString(DirectFunctionCall1(numeric_out,
33 NumericGetDatum(jbv->val.numeric)));
34 SV *result = newSVnv(SvNV(cstr2sv(str)));
35
36 pfree(str);
37 return result;
38 }
39
40 case jbvString:
41 {
42 char *str = pnstrdup(jbv->val.string.val,
43 jbv->val.string.len);
44 SV *result = cstr2sv(str);
45
46 pfree(str);
47 return result;
48 }
49
50 case jbvBool:
51 return newSVnv(SvNV(jbv->val.boolean ? &PL_sv_yes : &PL_sv_no));
52
53 case jbvNull:
54 return newSV(0);
55
56 default:
57 elog(ERROR, "unexpected jsonb value type: %d", jbv->type);
58 return NULL;
59 }
60 }
61
62 static SV *
63 Jsonb_to_SV(JsonbContainer *jsonb)
64 {
65 dTHX;
66 JsonbValue v;
67 JsonbIterator *it;
68 JsonbIteratorToken r;
69
70 /* this can recurse via JsonbValue_to_SV() */
71 check_stack_depth();
72
73 it = JsonbIteratorInit(jsonb);
74 r = JsonbIteratorNext(&it, &v, true);
75
76 switch (r)
77 {
78 case WJB_BEGIN_ARRAY:
79 if (v.val.array.rawScalar)
80 {
81 JsonbValue tmp;
82
83 if ((r = JsonbIteratorNext(&it, &v, true)) != WJB_ELEM ||
84 (r = JsonbIteratorNext(&it, &tmp, true)) != WJB_END_ARRAY ||
85 (r = JsonbIteratorNext(&it, &tmp, true)) != WJB_DONE)
86 elog(ERROR, "unexpected jsonb token: %d", r);
87
88 return JsonbValue_to_SV(&v);
89 }
90 else
91 {
92 AV *av = newAV();
93
94 while ((r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE)
95 {
96 if (r == WJB_ELEM)
97 av_push(av, JsonbValue_to_SV(&v));
98 }
99
100 return newRV((SV *) av);
101 }
102
103 case WJB_BEGIN_OBJECT:
104 {
105 HV *hv = newHV();
106
107 while ((r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE)
108 {
109 if (r == WJB_KEY)
110 {
111 /* json key in v, json value in val */
112 JsonbValue val;
113
114 if (JsonbIteratorNext(&it, &val, true) == WJB_VALUE)
115 {
116 SV *value = JsonbValue_to_SV(&val);
117
118 (void) hv_store(hv,
119 v.val.string.val, v.val.string.len,
120 value, 0);
121 }
122 }
123 }
124
125 return newRV((SV *) hv);
126 }
127
128 default:
129 elog(ERROR, "unexpected jsonb token: %d", r);
130 return NULL;
131 }
132 }
133
134 static JsonbValue *
135 AV_to_JsonbValue(AV *in, JsonbParseState **jsonb_state)
136 {
137 dTHX;
138 SSize_t pcount = av_len(in) + 1;
139 SSize_t i;
140
141 pushJsonbValue(jsonb_state, WJB_BEGIN_ARRAY, NULL);
142
143 for (i = 0; i < pcount; i++)
144 {
145 SV **value = av_fetch(in, i, FALSE);
146
147 if (value)
148 (void) SV_to_JsonbValue(*value, jsonb_state, true);
149 }
150
151 return pushJsonbValue(jsonb_state, WJB_END_ARRAY, NULL);
152 }
153
154 static JsonbValue *
155 HV_to_JsonbValue(HV *obj, JsonbParseState **jsonb_state)
156 {
157 dTHX;
158 JsonbValue key;
159 SV *val;
160 char *kstr;
161 I32 klen;
162
163 key.type = jbvString;
164
165 pushJsonbValue(jsonb_state, WJB_BEGIN_OBJECT, NULL);
166
167 (void) hv_iterinit(obj);
168
169 while ((val = hv_iternextsv(obj, &kstr, &klen)))
170 {
171 key.val.string.val = pnstrdup(kstr, klen);
172 key.val.string.len = klen;
173 pushJsonbValue(jsonb_state, WJB_KEY, &key);
174 (void) SV_to_JsonbValue(val, jsonb_state, false);
175 }
176
177 return pushJsonbValue(jsonb_state, WJB_END_OBJECT, NULL);
178 }
179
180 static JsonbValue *
181 SV_to_JsonbValue(SV *in, JsonbParseState **jsonb_state, bool is_elem)
182 {
183 dTHX;
184 JsonbValue out; /* result */
185
186 /* this can recurse via AV_to_JsonbValue() or HV_to_JsonbValue() */
187 check_stack_depth();
188
189 /* Dereference references recursively. */
190 while (SvROK(in))
191 {
192 /*
193 * It's possible for circular references to make this an infinite
194 * loop. Checking for such a situation seems like much more trouble
195 * than it's worth, but let's provide a way to break out of the loop.
196 */
197 CHECK_FOR_INTERRUPTS();
198 in = SvRV(in);
199 }
200
201 switch (SvTYPE(in))
202 {
203 case SVt_PVAV:
204 return AV_to_JsonbValue((AV *) in, jsonb_state);
205
206 case SVt_PVHV:
207 return HV_to_JsonbValue((HV *) in, jsonb_state);
208
209 default:
210 if (!SvOK(in))
211 {
212 out.type = jbvNull;
213 }
214 else if (SvUOK(in))
215 {
216 /*
217 * If UV is >=64 bits, we have no better way to make this
218 * happen than converting to text and back. Given the low
219 * usage of UV in Perl code, it's not clear it's worth working
220 * hard to provide alternate code paths.
221 */
222 const char *strval = SvPV_nolen(in);
223
224 out.type = jbvNumeric;
225 out.val.numeric =
226 DatumGetNumeric(DirectFunctionCall3(numeric_in,
227 CStringGetDatum(strval),
228 ObjectIdGetDatum(InvalidOid),
229 Int32GetDatum(-1)));
230 }
231 else if (SvIOK(in))
232 {
233 IV ival = SvIV(in);
234
235 out.type = jbvNumeric;
236 out.val.numeric = int64_to_numeric(ival);
237 }
238 else if (SvNOK(in))
239 {
240 double nval = SvNV(in);
241
242 /*
243 * jsonb doesn't allow infinity or NaN (per JSON
244 * specification), but the numeric type that is used for the
245 * storage accepts those, so we have to reject them here
246 * explicitly.
247 */
248 if (isinf(nval))
249 ereport(ERROR,
250 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
251 errmsg("cannot convert infinity to jsonb")));
252 if (isnan(nval))
253 ereport(ERROR,
254 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
255 errmsg("cannot convert NaN to jsonb")));
256
257 out.type = jbvNumeric;
258 out.val.numeric =
259 DatumGetNumeric(DirectFunctionCall1(float8_numeric,
260 Float8GetDatum(nval)));
261 }
262 else if (SvPOK(in))
263 {
264 out.type = jbvString;
265 out.val.string.val = sv2cstr(in);
266 out.val.string.len = strlen(out.val.string.val);
267 }
268 else
269 {
270 /*
271 * XXX It might be nice if we could include the Perl type in
272 * the error message.
273 */
274 ereport(ERROR,
275 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
276 errmsg("cannot transform this Perl type to jsonb")));
277 return NULL;
278 }
279 }
280
281 /* Push result into 'jsonb_state' unless it is a raw scalar. */
282 return *jsonb_state
283 ? pushJsonbValue(jsonb_state, is_elem ? WJB_ELEM : WJB_VALUE, &out)
284 : memcpy(palloc(sizeof(JsonbValue)), &out, sizeof(JsonbValue));
285 }
286
287
288 PG_FUNCTION_INFO_V1(jsonb_to_plperl);
289
290 Datum
291 jsonb_to_plperl(PG_FUNCTION_ARGS)
292 {
293 dTHX;
294 Jsonb *in = PG_GETARG_JSONB_P(0);
295 SV *sv = Jsonb_to_SV(&in->root);
296
297 return PointerGetDatum(sv);
298 }
299
300
301 PG_FUNCTION_INFO_V1(plperl_to_jsonb);
302
303 Datum
304 plperl_to_jsonb(PG_FUNCTION_ARGS)
305 {
306 dTHX;
307 JsonbParseState *jsonb_state = NULL;
308 SV *in = (SV *) PG_GETARG_POINTER(0);
309 JsonbValue *out = SV_to_JsonbValue(in, &jsonb_state, true);
310 Jsonb *result = JsonbValueToJsonb(out);
311
312 PG_RETURN_JSONB_P(result);
313 }