PostgreSQL Source Code git master
Loading...
Searching...
No Matches
jsonb_plpython.c File Reference
#include "postgres.h"
#include "plpy_elog.h"
#include "plpy_typeio.h"
#include "plpy_util.h"
#include "utils/fmgrprotos.h"
#include "utils/jsonb.h"
#include "utils/numeric.h"
Include dependency graph for jsonb_plpython.c:

Go to the source code of this file.

Macros

#define PLyObject_AsString   (PLyObject_AsString_p)
 
#define PLyUnicode_FromStringAndSize   (PLyUnicode_FromStringAndSize_p)
 
#define PLy_elog   (PLy_elog_impl_p)
 

Typedefs

typedef char *(* PLyObject_AsString_t) (PyObject *plrv)
 
typedef void(* PLy_elog_impl_t) (int elevel, const char *fmt,...)
 
typedef PyObject *(* PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size)
 

Functions

 PG_MODULE_MAGIC_EXT (.name="jsonb_plpython",.version=PG_VERSION)
 
static PyObjectPLyObject_FromJsonbContainer (JsonbContainer *jsonb)
 
static void PLyObject_ToJsonbValue (PyObject *obj, JsonbInState *jsonb_state, bool is_elem)
 
void _PG_init (void)
 
static PyObjectPLyUnicode_FromJsonbValue (JsonbValue *jbv)
 
static void PLyUnicode_ToJsonbValue (PyObject *obj, JsonbValue *jbvElem)
 
static PyObjectPLyObject_FromJsonbValue (JsonbValue *jsonbValue)
 
static void PLyMapping_ToJsonbValue (PyObject *obj, JsonbInState *jsonb_state)
 
static void PLySequence_ToJsonbValue (PyObject *obj, JsonbInState *jsonb_state)
 
static JsonbValuePLyNumber_ToJsonbValue (PyObject *obj, JsonbValue *jbvNum)
 
 PG_FUNCTION_INFO_V1 (plpython_to_jsonb)
 
Datum plpython_to_jsonb (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (jsonb_to_plpython)
 
Datum jsonb_to_plpython (PG_FUNCTION_ARGS)
 

Variables

static PLyObject_AsString_t PLyObject_AsString_p
 
static PLy_elog_impl_t PLy_elog_impl_p
 
static PyObjectdecimal_constructor
 
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
 
StaticAssertVariableIsOfTypePLyObject_AsString
 
StaticAssertVariableIsOfTypePLyUnicode_FromStringAndSize
 
StaticAssertVariableIsOfTypePLy_elog_impl
 

Macro Definition Documentation

◆ PLy_elog

#define PLy_elog   (PLy_elog_impl_p)

Definition at line 63 of file jsonb_plpython.c.

◆ PLyObject_AsString

#define PLyObject_AsString   (PLyObject_AsString_p)

Definition at line 60 of file jsonb_plpython.c.

◆ PLyUnicode_FromStringAndSize

#define PLyUnicode_FromStringAndSize   (PLyUnicode_FromStringAndSize_p)

Definition at line 61 of file jsonb_plpython.c.

Typedef Documentation

◆ PLy_elog_impl_t

typedef void(* PLy_elog_impl_t) (int elevel, const char *fmt,...)

Definition at line 19 of file jsonb_plpython.c.

◆ PLyObject_AsString_t

typedef char *(* PLyObject_AsString_t) (PyObject *plrv)

Definition at line 16 of file jsonb_plpython.c.

◆ PLyUnicode_FromStringAndSize_t

typedef PyObject *(* PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size)

Definition at line 32 of file jsonb_plpython.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 46 of file jsonb_plpython.c.

47{
49 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
50 true, NULL);
52 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
53 true, NULL);
55 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLy_elog_impl",
56 true, NULL);
57}
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition dfmgr.c:95
static PLy_elog_impl_t PLy_elog_impl_p
PyObject *(* PLyUnicode_FromStringAndSize_t)(const char *s, Py_ssize_t size)
void(* PLy_elog_impl_t)(int elevel, const char *fmt,...)
static PLyObject_AsString_t PLyObject_AsString_p
char *(* PLyObject_AsString_t)(PyObject *plrv)
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
static int fb(int x)

References fb(), load_external_function(), PLy_elog_impl_p, PLyObject_AsString_p, and PLyUnicode_FromStringAndSize_p.

◆ jsonb_to_plpython()

Datum jsonb_to_plpython ( PG_FUNCTION_ARGS  )

Definition at line 490 of file jsonb_plpython.c.

491{
492 PyObject *result;
493 Jsonb *in = PG_GETARG_JSONB_P(0);
494
495 /*
496 * Initialize pointer to Decimal constructor. First we try "cdecimal", C
497 * version of decimal library. In case of failure we use slower "decimal"
498 * module.
499 */
501 {
503
504 if (!decimal_module)
505 {
506 PyErr_Clear();
508 }
511 }
512
513 result = PLyObject_FromJsonbContainer(&in->root);
514 if (!result)
515 PLy_elog(ERROR, "transformation from jsonb to Python failed");
516
517 return PointerGetDatum(result);
518}
#define Assert(condition)
Definition c.h:873
#define ERROR
Definition elog.h:39
#define PG_GETARG_JSONB_P(x)
Definition jsonb.h:418
#define PLy_elog
static PyObject * decimal_constructor
static PyObject * PLyObject_FromJsonbContainer(JsonbContainer *jsonb)
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
Definition jsonb.h:215
JsonbContainer root
Definition jsonb.h:217

References Assert, decimal_constructor, ERROR, fb(), PG_GETARG_JSONB_P, PLy_elog, PLyObject_FromJsonbContainer(), PointerGetDatum(), and Jsonb::root.

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( jsonb_to_plpython  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( plpython_to_jsonb  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "jsonb_plpython",
version = PG_VERSION 
)

◆ plpython_to_jsonb()

Datum plpython_to_jsonb ( PG_FUNCTION_ARGS  )

Definition at line 474 of file jsonb_plpython.c.

475{
476 PyObject *obj = (PyObject *) PG_GETARG_POINTER(0);
478
481}
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
static void PLyObject_ToJsonbValue(PyObject *obj, JsonbInState *jsonb_state, bool is_elem)
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition jsonb_util.c:96

References fb(), JsonbValueToJsonb(), PG_GETARG_POINTER, PG_RETURN_POINTER, and PLyObject_ToJsonbValue().

◆ PLyMapping_ToJsonbValue()

static void PLyMapping_ToJsonbValue ( PyObject obj,
JsonbInState jsonb_state 
)
static

Definition at line 267 of file jsonb_plpython.c.

268{
270 PyObject *volatile items;
271
272 pcount = PyMapping_Size(obj);
273 items = PyMapping_Items(obj);
274
275 PG_TRY();
276 {
278
280
281 for (i = 0; i < pcount; i++)
282 {
284 PyObject *item = PyList_GetItem(items, i);
285 PyObject *key = PyTuple_GetItem(item, 0);
286 PyObject *value = PyTuple_GetItem(item, 1);
287
288 /* Python dictionary can have None as key */
289 if (key == Py_None)
290 {
291 jbvKey.type = jbvString;
292 jbvKey.val.string.len = 0;
293 jbvKey.val.string.val = "";
294 }
295 else
296 {
297 /* All others types of keys we serialize to string */
299 }
300
303 }
304
306 }
307 PG_FINALLY();
308 {
310 }
311 PG_END_TRY();
312}
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define PG_FINALLY(...)
Definition elog.h:389
static struct @172 value
int i
Definition isn.c:77
@ jbvString
Definition jsonb.h:231
@ WJB_KEY
Definition jsonb.h:23
@ WJB_END_OBJECT
Definition jsonb.h:29
@ WJB_BEGIN_OBJECT
Definition jsonb.h:28
static void PLyUnicode_ToJsonbValue(PyObject *obj, JsonbValue *jbvElem)
void pushJsonbValue(JsonbInState *pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition jsonb_util.c:583
static ItemArray items

References fb(), i, items, jbvString, PG_END_TRY, PG_FINALLY, PG_TRY, PLyObject_ToJsonbValue(), PLyUnicode_ToJsonbValue(), pushJsonbValue(), value, WJB_BEGIN_OBJECT, WJB_END_OBJECT, and WJB_KEY.

Referenced by PLyObject_ToJsonbValue().

◆ PLyNumber_ToJsonbValue()

static JsonbValue * PLyNumber_ToJsonbValue ( PyObject obj,
JsonbValue jbvNum 
)
static

Definition at line 359 of file jsonb_plpython.c.

360{
361 Numeric num;
362 char *str = PLyObject_AsString(obj);
363
364 PG_TRY();
365 {
366 Datum numd;
367
371 Int32GetDatum(-1));
372 num = DatumGetNumeric(numd);
373 }
374 PG_CATCH();
375 {
378 errmsg("could not convert value \"%s\" to jsonb", str)));
379 }
380 PG_END_TRY();
381
382 pfree(str);
383
384 /*
385 * jsonb doesn't allow NaN or infinity (per JSON specification), so we
386 * have to reject those here explicitly.
387 */
388 if (numeric_is_nan(num))
391 errmsg("cannot convert NaN to jsonb")));
392 if (numeric_is_inf(num))
395 errmsg("cannot convert infinity to jsonb")));
396
397 jbvNum->type = jbvNumeric;
398 jbvNum->val.numeric = num;
399
400 return jbvNum;
401}
Datum numeric_in(PG_FUNCTION_ARGS)
Definition numeric.c:626
bool numeric_is_nan(Numeric num)
Definition numeric.c:834
bool numeric_is_inf(Numeric num)
Definition numeric.c:845
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define PG_CATCH(...)
Definition elog.h:382
#define ereport(elevel,...)
Definition elog.h:150
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition fmgr.h:688
const char * str
@ jbvNumeric
Definition jsonb.h:232
#define PLyObject_AsString
void pfree(void *pointer)
Definition mcxt.c:1616
static Numeric DatumGetNumeric(Datum X)
Definition numeric.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Datum CStringGetDatum(const char *X)
Definition postgres.h:380
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
#define InvalidOid

References CStringGetDatum(), DatumGetNumeric(), DirectFunctionCall3, ereport, errcode(), errmsg(), ERROR, fb(), Int32GetDatum(), InvalidOid, jbvNumeric, numeric_in(), numeric_is_inf(), numeric_is_nan(), ObjectIdGetDatum(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, PLyObject_AsString, and str.

Referenced by PLyObject_ToJsonbValue().

◆ PLyObject_FromJsonbContainer()

static PyObject * PLyObject_FromJsonbContainer ( JsonbContainer jsonb)
static

Definition at line 139 of file jsonb_plpython.c.

140{
142 JsonbValue v;
144 PyObject *result;
145
146 it = JsonbIteratorInit(jsonb);
147 r = JsonbIteratorNext(&it, &v, true);
148
149 switch (r)
150 {
151 case WJB_BEGIN_ARRAY:
152 if (v.val.array.rawScalar)
153 {
154 JsonbValue tmp;
155
156 if ((r = JsonbIteratorNext(&it, &v, true)) != WJB_ELEM ||
157 (r = JsonbIteratorNext(&it, &tmp, true)) != WJB_END_ARRAY ||
158 (r = JsonbIteratorNext(&it, &tmp, true)) != WJB_DONE)
159 elog(ERROR, "unexpected jsonb token: %d", r);
160
161 result = PLyObject_FromJsonbValue(&v);
162 }
163 else
164 {
165 PyObject *volatile elem = NULL;
166
167 result = PyList_New(0);
168 if (!result)
169 return NULL;
170
171 PG_TRY();
172 {
173 while ((r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE)
174 {
175 if (r != WJB_ELEM)
176 continue;
177
178 elem = PLyObject_FromJsonbValue(&v);
179
180 PyList_Append(result, elem);
181 Py_XDECREF(elem);
182 elem = NULL;
183 }
184 }
185 PG_CATCH();
186 {
187 Py_XDECREF(elem);
188 Py_XDECREF(result);
189 PG_RE_THROW();
190 }
191 PG_END_TRY();
192 }
193 break;
194
195 case WJB_BEGIN_OBJECT:
196 {
197 PyObject *volatile result_v = PyDict_New();
198 PyObject *volatile key = NULL;
199 PyObject *volatile val = NULL;
200
201 if (!result_v)
202 return NULL;
203
204 PG_TRY();
205 {
206 while ((r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE)
207 {
208 if (r != WJB_KEY)
209 continue;
210
212 if (!key)
213 {
215 result_v = NULL;
216 break;
217 }
218
219 if ((r = JsonbIteratorNext(&it, &v, true)) != WJB_VALUE)
220 elog(ERROR, "unexpected jsonb token: %d", r);
221
223 if (!val)
224 {
225 Py_XDECREF(key);
226 key = NULL;
228 result_v = NULL;
229 break;
230 }
231
233
234 Py_XDECREF(key);
235 key = NULL;
237 val = NULL;
238 }
239 }
240 PG_CATCH();
241 {
243 Py_XDECREF(key);
245 PG_RE_THROW();
246 }
247 PG_END_TRY();
248
249 result = result_v;
250 }
251 break;
252
253 default:
254 elog(ERROR, "unexpected jsonb token: %d", r);
255 return NULL;
256 }
257
258 return result;
259}
#define PG_RE_THROW()
Definition elog.h:405
#define elog(elevel,...)
Definition elog.h:226
long val
Definition informix.c:689
JsonbIteratorToken
Definition jsonb.h:21
@ WJB_DONE
Definition jsonb.h:22
@ WJB_END_ARRAY
Definition jsonb.h:27
@ WJB_VALUE
Definition jsonb.h:24
@ WJB_ELEM
Definition jsonb.h:25
@ WJB_BEGIN_ARRAY
Definition jsonb.h:26
static PyObject * PLyObject_FromJsonbValue(JsonbValue *jsonbValue)
static PyObject * PLyUnicode_FromJsonbValue(JsonbValue *jbv)
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition jsonb_util.c:935
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition jsonb_util.c:973
char * val
Definition jsonb.h:266

References elog, ERROR, fb(), JsonbIteratorInit(), JsonbIteratorNext(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLyObject_FromJsonbValue(), PLyUnicode_FromJsonbValue(), JsonbValue::val, val, WJB_BEGIN_ARRAY, WJB_BEGIN_OBJECT, WJB_DONE, WJB_ELEM, WJB_END_ARRAY, WJB_KEY, and WJB_VALUE.

Referenced by jsonb_to_plpython(), and PLyObject_FromJsonbValue().

◆ PLyObject_FromJsonbValue()

static PyObject * PLyObject_FromJsonbValue ( JsonbValue jsonbValue)
static

Definition at line 97 of file jsonb_plpython.c.

98{
99 switch (jsonbValue->type)
100 {
101 case jbvNull:
103
104 case jbvBinary:
105 return PLyObject_FromJsonbContainer(jsonbValue->val.binary.data);
106
107 case jbvNumeric:
108 {
109 Datum num;
110 char *str;
111
112 num = NumericGetDatum(jsonbValue->val.numeric);
114
116 }
117
118 case jbvString:
120
121 case jbvBool:
122 if (jsonbValue->val.boolean)
124 else
126
127 default:
128 elog(ERROR, "unexpected jsonb value type: %d", jsonbValue->type);
129 return NULL;
130 }
131}
Datum numeric_out(PG_FUNCTION_ARGS)
Definition numeric.c:799
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
@ jbvBool
Definition jsonb.h:233
@ jbvBinary
Definition jsonb.h:238
@ jbvNull
Definition jsonb.h:230
static Datum NumericGetDatum(Numeric X)
Definition numeric.h:76
static char * DatumGetCString(Datum X)
Definition postgres.h:365

References DatumGetCString(), decimal_constructor, DirectFunctionCall1, elog, ERROR, fb(), jbvBinary, jbvBool, jbvNull, jbvNumeric, jbvString, numeric_out(), NumericGetDatum(), PLyObject_FromJsonbContainer(), PLyUnicode_FromJsonbValue(), and str.

Referenced by PLyObject_FromJsonbContainer().

◆ PLyObject_ToJsonbValue()

static void PLyObject_ToJsonbValue ( PyObject obj,
JsonbInState jsonb_state,
bool  is_elem 
)
static

Definition at line 409 of file jsonb_plpython.c.

410{
411 JsonbValue *out;
412
413 if (!PyUnicode_Check(obj))
414 {
415 if (PySequence_Check(obj))
416 {
418 return;
419 }
420 else if (PyMapping_Check(obj))
421 {
423 return;
424 }
425 }
426
428
429 if (obj == Py_None)
430 out->type = jbvNull;
431 else if (PyUnicode_Check(obj))
432 PLyUnicode_ToJsonbValue(obj, out);
433
434 /*
435 * PyNumber_Check() returns true for booleans, so boolean check should
436 * come first.
437 */
438 else if (PyBool_Check(obj))
439 {
440 out->type = jbvBool;
441 out->val.boolean = (obj == Py_True);
442 }
443 else if (PyNumber_Check(obj))
444 out = PLyNumber_ToJsonbValue(obj, out);
445 else
448 errmsg("Python type \"%s\" cannot be transformed to jsonb",
449 PLyObject_AsString((PyObject *) obj->ob_type))));
450
451 if (jsonb_state->parseState)
452 {
453 /* We're in an array or object, so push value as element or field. */
455 }
456 else
457 {
458 /*
459 * We are at top level, so it's a raw scalar. If we just shove the
460 * scalar value into jsonb_state->result, JsonbValueToJsonb will take
461 * care of wrapping it into a dummy array.
462 */
463 jsonb_state->result = out;
464 }
465}
#define palloc_object(type)
Definition fe_memutils.h:74
static void PLyMapping_ToJsonbValue(PyObject *obj, JsonbInState *jsonb_state)
static JsonbValue * PLyNumber_ToJsonbValue(PyObject *obj, JsonbValue *jbvNum)
static void PLySequence_ToJsonbValue(PyObject *obj, JsonbInState *jsonb_state)
enum jbvType type
Definition jsonb.h:257

References ereport, errcode(), errmsg(), ERROR, fb(), jbvBool, jbvNull, palloc_object, PLyMapping_ToJsonbValue(), PLyNumber_ToJsonbValue(), PLyObject_AsString, PLySequence_ToJsonbValue(), PLyUnicode_ToJsonbValue(), pushJsonbValue(), JsonbValue::type, JsonbValue::val, WJB_ELEM, and WJB_VALUE.

Referenced by plpython_to_jsonb(), PLyMapping_ToJsonbValue(), and PLySequence_ToJsonbValue().

◆ PLySequence_ToJsonbValue()

static void PLySequence_ToJsonbValue ( PyObject obj,
JsonbInState jsonb_state 
)
static

Definition at line 321 of file jsonb_plpython.c.

322{
325 PyObject *volatile value = NULL;
326
327 pcount = PySequence_Size(obj);
328
330
331 PG_TRY();
332 {
333 for (i = 0; i < pcount; i++)
334 {
335 value = PySequence_GetItem(obj, i);
336 Assert(value);
337
340 value = NULL;
341 }
342 }
343 PG_CATCH();
344 {
346 PG_RE_THROW();
347 }
348 PG_END_TRY();
349
351}

References Assert, fb(), i, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLyObject_ToJsonbValue(), pushJsonbValue(), value, WJB_BEGIN_ARRAY, and WJB_END_ARRAY.

Referenced by PLyObject_ToJsonbValue().

◆ PLyUnicode_FromJsonbValue()

static PyObject * PLyUnicode_FromJsonbValue ( JsonbValue jbv)
static

Definition at line 71 of file jsonb_plpython.c.

72{
73 Assert(jbv->type == jbvString);
74
75 return PLyUnicode_FromStringAndSize(jbv->val.string.val, jbv->val.string.len);
76}
#define PLyUnicode_FromStringAndSize

References Assert, fb(), jbvString, and PLyUnicode_FromStringAndSize.

Referenced by PLyObject_FromJsonbContainer(), and PLyObject_FromJsonbValue().

◆ PLyUnicode_ToJsonbValue()

static void PLyUnicode_ToJsonbValue ( PyObject obj,
JsonbValue jbvElem 
)
static

Definition at line 84 of file jsonb_plpython.c.

85{
86 jbvElem->type = jbvString;
87 jbvElem->val.string.val = PLyObject_AsString(obj);
88 jbvElem->val.string.len = strlen(jbvElem->val.string.val);
89}

References fb(), jbvString, and PLyObject_AsString.

Referenced by PLyMapping_ToJsonbValue(), and PLyObject_ToJsonbValue().

Variable Documentation

◆ decimal_constructor

PyObject* decimal_constructor
static

◆ PLy_elog_impl

Definition at line 39 of file jsonb_plpython.c.

◆ PLy_elog_impl_p

PLy_elog_impl_t PLy_elog_impl_p
static

Definition at line 20 of file jsonb_plpython.c.

Referenced by _PG_init().

◆ PLyObject_AsString

StaticAssertVariableIsOfType& PLyObject_AsString

Definition at line 37 of file jsonb_plpython.c.

◆ PLyObject_AsString_p

PLyObject_AsString_t PLyObject_AsString_p
static

Definition at line 17 of file jsonb_plpython.c.

Referenced by _PG_init().

◆ PLyUnicode_FromStringAndSize

StaticAssertVariableIsOfType& PLyUnicode_FromStringAndSize

Definition at line 38 of file jsonb_plpython.c.

◆ PLyUnicode_FromStringAndSize_p

PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
static

Definition at line 34 of file jsonb_plpython.c.

Referenced by _PG_init().