PostgreSQL Source Code  git master
plpy_typeio.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "plpython.h"
#include "plpy_typeio.h"
#include "plpy_elog.h"
#include "plpy_main.h"
Include dependency graph for plpy_typeio.c:

Go to the source code of this file.

Functions

static PyObject * PLyBool_FromBool (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyFloat_FromFloat4 (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyFloat_FromFloat8 (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyDecimal_FromNumeric (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyInt_FromInt16 (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyInt_FromInt32 (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyLong_FromInt64 (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyLong_FromOid (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyBytes_FromBytea (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyString_FromScalar (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyObject_FromTransform (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyList_FromArray (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyList_FromArray_recurse (PLyDatumToOb *elm, int *dims, int ndim, int dim, char **dataptr_p, bits8 **bitmap_p, int *bitmask_p)
 
static PyObject * PLyDict_FromComposite (PLyDatumToOb *arg, Datum d)
 
static PyObject * PLyDict_FromTuple (PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
 
static Datum PLyObject_ToBool (PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
 
static Datum PLyObject_ToBytea (PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
 
static Datum PLyObject_ToComposite (PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
 
static Datum PLyObject_ToScalar (PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
 
static Datum PLyObject_ToDomain (PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
 
static Datum PLyObject_ToTransform (PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
 
static Datum PLySequence_ToArray (PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
 
static void PLySequence_ToArray_recurse (PLyObToDatum *elm, PyObject *list, int *dims, int ndim, int dim, Datum *elems, bool *nulls, int *currelem)
 
static Datum PLyString_ToComposite (PLyObToDatum *arg, PyObject *string, bool inarray)
 
static Datum PLyMapping_ToComposite (PLyObToDatum *arg, TupleDesc desc, PyObject *mapping)
 
static Datum PLySequence_ToComposite (PLyObToDatum *arg, TupleDesc desc, PyObject *sequence)
 
static Datum PLyGenericObject_ToComposite (PLyObToDatum *arg, TupleDesc desc, PyObject *object, bool inarray)
 
PyObject * PLy_input_convert (PLyDatumToOb *arg, Datum val)
 
Datum PLy_output_convert (PLyObToDatum *arg, PyObject *val, bool *isnull)
 
PyObject * PLy_input_from_tuple (PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
 
void PLy_input_setup_tuple (PLyDatumToOb *arg, TupleDesc desc, PLyProcedure *proc)
 
void PLy_output_setup_tuple (PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
 
void PLy_output_setup_record (PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
 
void PLy_output_setup_func (PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
 
void PLy_input_setup_func (PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
 
char * PLyObject_AsString (PyObject *plrv)
 

Function Documentation

◆ PLy_input_convert()

PyObject* PLy_input_convert ( PLyDatumToOb arg,
Datum  val 
)

Definition at line 84 of file plpy_typeio.c.

References PLyDatumToOb::func, MemoryContextReset(), MemoryContextSwitchTo(), PLy_current_execution_context(), and PLy_get_scratch_context().

Referenced by PLy_function_build_args().

85 {
86  PyObject *result;
88  MemoryContext scratch_context = PLy_get_scratch_context(exec_ctx);
89  MemoryContext oldcontext;
90 
91  /*
92  * Do the work in the scratch context to avoid leaking memory from the
93  * datatype output function calls. (The individual PLyDatumToObFunc
94  * functions can't reset the scratch context, because they recurse and an
95  * inner one might clobber data an outer one still needs. So we do it
96  * once at the outermost recursion level.)
97  *
98  * We reset the scratch context before, not after, each conversion cycle.
99  * This way we aren't on the hook to release a Python refcount on the
100  * result object in case MemoryContextReset throws an error.
101  */
102  MemoryContextReset(scratch_context);
103 
104  oldcontext = MemoryContextSwitchTo(scratch_context);
105 
106  result = arg->func(arg, val);
107 
108  MemoryContextSwitchTo(oldcontext);
109 
110  return result;
111 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:414
PLyDatumToObFunc func
Definition: plpy_typeio.h:60
long val
Definition: informix.c:684
MemoryContext PLy_get_scratch_context(PLyExecutionContext *context)
Definition: plpy_main.c:423

◆ PLy_input_from_tuple()

PyObject* PLy_input_from_tuple ( PLyDatumToOb arg,
HeapTuple  tuple,
TupleDesc  desc,
bool  include_generated 
)

Definition at line 137 of file plpy_typeio.c.

References MemoryContextReset(), MemoryContextSwitchTo(), PLy_current_execution_context(), PLy_get_scratch_context(), and PLyDict_FromTuple().

Referenced by PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_spi_execute_fetch_result(), and PLy_trigger_build_args().

138 {
139  PyObject *dict;
141  MemoryContext scratch_context = PLy_get_scratch_context(exec_ctx);
142  MemoryContext oldcontext;
143 
144  /*
145  * As in PLy_input_convert, do the work in the scratch context.
146  */
147  MemoryContextReset(scratch_context);
148 
149  oldcontext = MemoryContextSwitchTo(scratch_context);
150 
151  dict = PLyDict_FromTuple(arg, tuple, desc, include_generated);
152 
153  MemoryContextSwitchTo(oldcontext);
154 
155  return dict;
156 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:414
static PyObject * PLyDict_FromTuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
Definition: plpy_typeio.c:818
MemoryContext PLy_get_scratch_context(PLyExecutionContext *context)
Definition: plpy_main.c:423

◆ PLy_input_setup_func()

void PLy_input_setup_func ( PLyDatumToOb arg,
MemoryContext  arg_mcxt,
Oid  typeOid,
int32  typmod,
PLyProcedure proc 
)

Definition at line 421 of file plpy_typeio.c.

References PLyDatumToOb::array, PLyTupleToOb::atts, check_stack_depth(), TypeCacheEntry::domainBaseType, TypeCacheEntry::domainBaseTypmod, PLyArrayToOb::elm, fmgr_info_cxt(), PLyDatumToOb::func, get_transform_fromsql(), getTypeOutputInfo(), INVALID_TUPLEDESC_IDENTIFIER, PLyProcedure::langid, lookup_type_cache(), PLyDatumToOb::mcxt, MemoryContextAllocZero(), PLyTupleToOb::natts, OidIsValid, PLy_input_setup_func(), PLyBool_FromBool(), PLyBytes_FromBytea(), PLyDecimal_FromNumeric(), PLyDict_FromComposite(), PLyFloat_FromFloat4(), PLyFloat_FromFloat8(), PLyInt_FromInt16(), PLyInt_FromInt32(), PLyList_FromArray(), PLyLong_FromInt64(), PLyLong_FromOid(), PLyObject_FromTransform(), PLyString_FromScalar(), PLyTupleToOb::recdesc, PLyDatumToOb::scalar, PLyDatumToOb::transform, PLyProcedure::trftypes, PLyTupleToOb::tupdescid, PLyDatumToOb::tuple, TypeCacheEntry::typalign, PLyDatumToOb::typalign, TypeCacheEntry::typbyval, PLyDatumToOb::typbyval, TYPECACHE_DOMAIN_BASE_INFO, TypeCacheEntry::typelem, PLyTupleToOb::typentry, PLyScalarToOb::typfunc, TypeCacheEntry::typlen, PLyDatumToOb::typlen, PLyDatumToOb::typmod, PLyDatumToOb::typoid, PLyTransformToOb::typtransform, TypeCacheEntry::typtype, and PLyDatumToOb::u.

Referenced by PLy_cursor_plan(), PLy_cursor_query(), PLy_exec_trigger(), PLy_input_setup_func(), PLy_input_setup_tuple(), PLy_procedure_create(), and PLy_spi_execute_fetch_result().

424 {
425  TypeCacheEntry *typentry;
426  char typtype;
427  Oid trfuncid;
428  Oid typoutput;
429  bool typisvarlena;
430 
431  /* Since this is recursive, it could theoretically be driven to overflow */
433 
434  arg->typoid = typeOid;
435  arg->typmod = typmod;
436  arg->mcxt = arg_mcxt;
437 
438  /*
439  * Fetch typcache entry for the target type, asking for whatever info
440  * we'll need later. RECORD is a special case: just treat it as composite
441  * without bothering with the typcache entry.
442  */
443  if (typeOid != RECORDOID)
444  {
445  typentry = lookup_type_cache(typeOid, TYPECACHE_DOMAIN_BASE_INFO);
446  typtype = typentry->typtype;
447  arg->typbyval = typentry->typbyval;
448  arg->typlen = typentry->typlen;
449  arg->typalign = typentry->typalign;
450  }
451  else
452  {
453  typentry = NULL;
454  typtype = TYPTYPE_COMPOSITE;
455  /* hard-wired knowledge about type RECORD: */
456  arg->typbyval = false;
457  arg->typlen = -1;
458  arg->typalign = 'd';
459  }
460 
461  /*
462  * Choose conversion method. Note that transform functions are checked
463  * for composite and scalar types, but not for arrays or domains. This is
464  * somewhat historical, but we'd have a problem allowing them on domains,
465  * since we drill down through all levels of a domain nest without looking
466  * at the intermediate levels at all.
467  */
468  if (typtype == TYPTYPE_DOMAIN)
469  {
470  /* Domain --- we don't care, just recurse down to the base type */
471  PLy_input_setup_func(arg, arg_mcxt,
472  typentry->domainBaseType,
473  typentry->domainBaseTypmod,
474  proc);
475  }
476  else if (typentry &&
477  OidIsValid(typentry->typelem) && typentry->typlen == -1)
478  {
479  /* Standard varlena array (cf. get_element_type) */
480  arg->func = PLyList_FromArray;
481  /* Recursively set up conversion info for the element type */
482  arg->u.array.elm = (PLyDatumToOb *)
483  MemoryContextAllocZero(arg_mcxt, sizeof(PLyDatumToOb));
484  PLy_input_setup_func(arg->u.array.elm, arg_mcxt,
485  typentry->typelem, typmod,
486  proc);
487  }
488  else if ((trfuncid = get_transform_fromsql(typeOid,
489  proc->langid,
490  proc->trftypes)))
491  {
493  fmgr_info_cxt(trfuncid, &arg->u.transform.typtransform, arg_mcxt);
494  }
495  else if (typtype == TYPTYPE_COMPOSITE)
496  {
497  /* Named composite type, or RECORD */
499  /* We'll set up the per-field data later */
500  arg->u.tuple.recdesc = NULL;
501  arg->u.tuple.typentry = typentry;
503  arg->u.tuple.atts = NULL;
504  arg->u.tuple.natts = 0;
505  }
506  else
507  {
508  /* Scalar type, but we have a couple of special cases */
509  switch (typeOid)
510  {
511  case BOOLOID:
512  arg->func = PLyBool_FromBool;
513  break;
514  case FLOAT4OID:
515  arg->func = PLyFloat_FromFloat4;
516  break;
517  case FLOAT8OID:
518  arg->func = PLyFloat_FromFloat8;
519  break;
520  case NUMERICOID:
522  break;
523  case INT2OID:
524  arg->func = PLyInt_FromInt16;
525  break;
526  case INT4OID:
527  arg->func = PLyInt_FromInt32;
528  break;
529  case INT8OID:
530  arg->func = PLyLong_FromInt64;
531  break;
532  case OIDOID:
533  arg->func = PLyLong_FromOid;
534  break;
535  case BYTEAOID:
536  arg->func = PLyBytes_FromBytea;
537  break;
538  default:
539  arg->func = PLyString_FromScalar;
540  getTypeOutputInfo(typeOid, &typoutput, &typisvarlena);
541  fmgr_info_cxt(typoutput, &arg->u.scalar.typfunc, arg_mcxt);
542  break;
543  }
544  }
545 }
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1900
static PyObject * PLyLong_FromInt64(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:619
TupleDesc recdesc
Definition: plpy_typeio.h:44
static PyObject * PLyInt_FromInt32(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:613
PLyDatumToOb * atts
Definition: plpy_typeio.h:49
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
FmgrInfo typfunc
Definition: plpy_typeio.h:33
static PyObject * PLyBool_FromBool(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:553
uint64 tupdescid
Definition: plpy_typeio.h:47
TypeCacheEntry * typentry
Definition: plpy_typeio.h:46
unsigned int Oid
Definition: postgres_ext.h:31
int16 typlen
Definition: typcache.h:37
#define OidIsValid(objectId)
Definition: c.h:638
bool typbyval
Definition: typcache.h:38
#define INVALID_TUPLEDESC_IDENTIFIER
Definition: typcache.h:146
static PyObject * PLyString_FromScalar(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:645
FmgrInfo typtransform
Definition: plpy_typeio.h:55
static PyObject * PLyDict_FromComposite(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:784
Oid domainBaseType
Definition: typcache.h:105
static PyObject * PLyInt_FromInt16(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:607
PLyArrayToOb array
Definition: plpy_typeio.h:70
static PyObject * PLyFloat_FromFloat8(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:567
PLyDatumToObFunc func
Definition: plpy_typeio.h:60
PLyTransformToOb transform
Definition: plpy_typeio.h:72
static PyObject * PLyFloat_FromFloat4(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:561
static PyObject * PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:631
void check_stack_depth(void)
Definition: postgres.c:3262
int32 typmod
Definition: plpy_typeio.h:62
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:421
int32 domainBaseTypmod
Definition: typcache.h:106
int16 typlen
Definition: plpy_typeio.h:64
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
static PyObject * PLyDecimal_FromNumeric(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:573
PLyDatumToOb * elm
Definition: plpy_typeio.h:38
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:140
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:814
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:322
char typtype
Definition: typcache.h:41
static PyObject * PLyList_FromArray(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:670
static PyObject * PLyLong_FromOid(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:625
PLyScalarToOb scalar
Definition: plpy_typeio.h:69
static PyObject * PLyObject_FromTransform(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:658
char typalign
Definition: typcache.h:39
union PLyDatumToOb::@165 u
MemoryContext mcxt
Definition: plpy_typeio.h:66
PLyTupleToOb tuple
Definition: plpy_typeio.h:71

◆ PLy_input_setup_tuple()

void PLy_input_setup_tuple ( PLyDatumToOb arg,
TupleDesc  desc,
PLyProcedure proc 
)

Definition at line 168 of file plpy_typeio.c.

References Assert, PLyTupleToOb::atts, PLyDatumToOb::func, i, PLyDatumToOb::mcxt, MemoryContextAllocZero(), PLyTupleToOb::natts, TupleDescData::natts, pfree(), PLy_input_setup_func(), PLyDict_FromComposite(), PLyTupleToOb::recdesc, PLyDatumToOb::tuple, TupleDescAttr, PLyDatumToOb::typmod, PLyDatumToOb::typoid, and PLyDatumToOb::u.

Referenced by PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_exec_trigger(), PLy_spi_execute_fetch_result(), and PLyDict_FromComposite().

169 {
170  int i;
171 
172  /* We should be working on a previously-set-up struct */
174 
175  /* Save pointer to tupdesc, but only if this is an anonymous record type */
176  if (arg->typoid == RECORDOID && arg->typmod < 0)
177  arg->u.tuple.recdesc = desc;
178 
179  /* (Re)allocate atts array as needed */
180  if (arg->u.tuple.natts != desc->natts)
181  {
182  if (arg->u.tuple.atts)
183  pfree(arg->u.tuple.atts);
184  arg->u.tuple.natts = desc->natts;
185  arg->u.tuple.atts = (PLyDatumToOb *)
187  desc->natts * sizeof(PLyDatumToOb));
188  }
189 
190  /* Fill the atts entries, except for dropped columns */
191  for (i = 0; i < desc->natts; i++)
192  {
193  Form_pg_attribute attr = TupleDescAttr(desc, i);
194  PLyDatumToOb *att = &arg->u.tuple.atts[i];
195 
196  if (attr->attisdropped)
197  continue;
198 
199  if (att->typoid == attr->atttypid && att->typmod == attr->atttypmod)
200  continue; /* already set up this entry */
201 
202  PLy_input_setup_func(att, arg->mcxt,
203  attr->atttypid, attr->atttypmod,
204  proc);
205  }
206 }
TupleDesc recdesc
Definition: plpy_typeio.h:44
PLyDatumToOb * atts
Definition: plpy_typeio.h:49
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static PyObject * PLyDict_FromComposite(PLyDatumToOb *arg, Datum d)
Definition: plpy_typeio.c:784
void pfree(void *pointer)
Definition: mcxt.c:1031
PLyDatumToObFunc func
Definition: plpy_typeio.h:60
int32 typmod
Definition: plpy_typeio.h:62
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:421
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:814
#define Assert(condition)
Definition: c.h:732
int i
union PLyDatumToOb::@165 u
MemoryContext mcxt
Definition: plpy_typeio.h:66
PLyTupleToOb tuple
Definition: plpy_typeio.h:71

◆ PLy_output_convert()

Datum PLy_output_convert ( PLyObToDatum arg,
PyObject *  val,
bool isnull 
)

Definition at line 123 of file plpy_typeio.c.

References PLyObToDatum::func.

Referenced by PLy_cursor_plan(), PLy_exec_function(), PLy_modify_tuple(), and PLy_spi_execute_plan().

124 {
125  /* at outer level, we are not considering an array element */
126  return arg->func(arg, val, isnull, false);
127 }
PLyObToDatumFunc func
Definition: plpy_typeio.h:133
long val
Definition: informix.c:684

◆ PLy_output_setup_func()

void PLy_output_setup_func ( PLyObToDatum arg,
MemoryContext  arg_mcxt,
Oid  typeOid,
int32  typmod,
PLyProcedure proc 
)

Definition at line 299 of file plpy_typeio.c.

References PLyObToDatum::array, PLyObToTuple::atts, PLyObToDomain::base, check_stack_depth(), PLyObToDatum::domain, PLyObToDomain::domain_info, TypeCacheEntry::domainBaseType, TypeCacheEntry::domainBaseTypmod, PLyObToArray::elm, PLyObToArray::elmbasetype, fmgr_info_cxt(), FmgrInfo::fn_oid, PLyObToDatum::func, get_transform_tosql(), getBaseType(), getTypeInputInfo(), INVALID_TUPLEDESC_IDENTIFIER, InvalidOid, PLyProcedure::langid, lookup_type_cache(), PLyObToDatum::mcxt, MemoryContextAllocZero(), PLyObToTuple::natts, OidIsValid, PLy_output_setup_func(), PLyObject_ToBool(), PLyObject_ToBytea(), PLyObject_ToComposite(), PLyObject_ToDomain(), PLyObject_ToScalar(), PLyObject_ToTransform(), PLySequence_ToArray(), PLyObToTuple::recdesc, PLyObToTuple::recinfunc, PLyObToDatum::scalar, PLyObToDatum::transform, PLyProcedure::trftypes, PLyObToTuple::tupdescid, PLyObToDatum::tuple, TypeCacheEntry::typalign, PLyObToDatum::typalign, TypeCacheEntry::typbyval, PLyObToDatum::typbyval, TYPECACHE_DOMAIN_BASE_INFO, TypeCacheEntry::typelem, PLyObToTuple::typentry, PLyObToScalar::typfunc, PLyObToScalar::typioparam, TypeCacheEntry::typlen, PLyObToDatum::typlen, PLyObToDatum::typmod, PLyObToDatum::typoid, PLyObToTransform::typtransform, TypeCacheEntry::typtype, and PLyObToDatum::u.

Referenced by PLy_exec_trigger(), PLy_output_setup_func(), PLy_output_setup_tuple(), PLy_procedure_create(), and PLy_spi_prepare().

302 {
303  TypeCacheEntry *typentry;
304  char typtype;
305  Oid trfuncid;
306  Oid typinput;
307 
308  /* Since this is recursive, it could theoretically be driven to overflow */
310 
311  arg->typoid = typeOid;
312  arg->typmod = typmod;
313  arg->mcxt = arg_mcxt;
314 
315  /*
316  * Fetch typcache entry for the target type, asking for whatever info
317  * we'll need later. RECORD is a special case: just treat it as composite
318  * without bothering with the typcache entry.
319  */
320  if (typeOid != RECORDOID)
321  {
322  typentry = lookup_type_cache(typeOid, TYPECACHE_DOMAIN_BASE_INFO);
323  typtype = typentry->typtype;
324  arg->typbyval = typentry->typbyval;
325  arg->typlen = typentry->typlen;
326  arg->typalign = typentry->typalign;
327  }
328  else
329  {
330  typentry = NULL;
331  typtype = TYPTYPE_COMPOSITE;
332  /* hard-wired knowledge about type RECORD: */
333  arg->typbyval = false;
334  arg->typlen = -1;
335  arg->typalign = 'd';
336  }
337 
338  /*
339  * Choose conversion method. Note that transform functions are checked
340  * for composite and scalar types, but not for arrays or domains. This is
341  * somewhat historical, but we'd have a problem allowing them on domains,
342  * since we drill down through all levels of a domain nest without looking
343  * at the intermediate levels at all.
344  */
345  if (typtype == TYPTYPE_DOMAIN)
346  {
347  /* Domain */
348  arg->func = PLyObject_ToDomain;
349  arg->u.domain.domain_info = NULL;
350  /* Recursively set up conversion info for the element type */
351  arg->u.domain.base = (PLyObToDatum *)
352  MemoryContextAllocZero(arg_mcxt, sizeof(PLyObToDatum));
353  PLy_output_setup_func(arg->u.domain.base, arg_mcxt,
354  typentry->domainBaseType,
355  typentry->domainBaseTypmod,
356  proc);
357  }
358  else if (typentry &&
359  OidIsValid(typentry->typelem) && typentry->typlen == -1)
360  {
361  /* Standard varlena array (cf. get_element_type) */
362  arg->func = PLySequence_ToArray;
363  /* Get base type OID to insert into constructed array */
364  /* (note this might not be the same as the immediate child type) */
365  arg->u.array.elmbasetype = getBaseType(typentry->typelem);
366  /* Recursively set up conversion info for the element type */
367  arg->u.array.elm = (PLyObToDatum *)
368  MemoryContextAllocZero(arg_mcxt, sizeof(PLyObToDatum));
369  PLy_output_setup_func(arg->u.array.elm, arg_mcxt,
370  typentry->typelem, typmod,
371  proc);
372  }
373  else if ((trfuncid = get_transform_tosql(typeOid,
374  proc->langid,
375  proc->trftypes)))
376  {
378  fmgr_info_cxt(trfuncid, &arg->u.transform.typtransform, arg_mcxt);
379  }
380  else if (typtype == TYPTYPE_COMPOSITE)
381  {
382  /* Named composite type, or RECORD */
384  /* We'll set up the per-field data later */
385  arg->u.tuple.recdesc = NULL;
386  arg->u.tuple.typentry = typentry;
388  arg->u.tuple.atts = NULL;
389  arg->u.tuple.natts = 0;
390  /* Mark this invalid till needed, too */
392  }
393  else
394  {
395  /* Scalar type, but we have a couple of special cases */
396  switch (typeOid)
397  {
398  case BOOLOID:
399  arg->func = PLyObject_ToBool;
400  break;
401  case BYTEAOID:
402  arg->func = PLyObject_ToBytea;
403  break;
404  default:
405  arg->func = PLyObject_ToScalar;
406  getTypeInputInfo(typeOid, &typinput, &arg->u.scalar.typioparam);
407  fmgr_info_cxt(typinput, &arg->u.scalar.typfunc, arg_mcxt);
408  break;
409  }
410  }
411 }
void * domain_info
Definition: plpy_typeio.h:123
FmgrInfo typfunc
Definition: plpy_typeio.h:96
unsigned int Oid
Definition: postgres_ext.h:31
int16 typlen
Definition: typcache.h:37
#define OidIsValid(objectId)
Definition: c.h:638
bool typbyval
Definition: typcache.h:38
PLyObToTransform transform
Definition: plpy_typeio.h:146
#define INVALID_TUPLEDESC_IDENTIFIER
Definition: typcache.h:146
PLyObToDatum * base
Definition: plpy_typeio.h:122
PLyObToDatumFunc func
Definition: plpy_typeio.h:133
PLyObToDomain domain
Definition: plpy_typeio.h:145
Oid domainBaseType
Definition: typcache.h:105
static Datum PLyObject_ToScalar(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
Definition: plpy_typeio.c:1088
PLyObToArray array
Definition: plpy_typeio.h:143
TupleDesc recdesc
Definition: plpy_typeio.h:109
uint64 tupdescid
Definition: plpy_typeio.h:112
PLyObToScalar scalar
Definition: plpy_typeio.h:142
void check_stack_depth(void)
Definition: postgres.c:3262
static Datum PLyObject_ToDomain(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
Definition: plpy_typeio.c:1113
static Datum PLyObject_ToBool(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
Definition: plpy_typeio.c:882
int32 domainBaseTypmod
Definition: typcache.h:106
FmgrInfo typtransform
Definition: plpy_typeio.h:128
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2641
static Datum PLyObject_ToComposite(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
Definition: plpy_typeio.c:947
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:140
TypeCacheEntry * typentry
Definition: plpy_typeio.h:111
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:814
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:322
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
char typtype
Definition: typcache.h:41
PLyObToDatum * elm
Definition: plpy_typeio.h:102
MemoryContext mcxt
Definition: plpy_typeio.h:139
Oid get_transform_tosql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1921
static Datum PLyObject_ToTransform(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
Definition: plpy_typeio.c:1130
void PLy_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:299
PLyObToTuple tuple
Definition: plpy_typeio.h:144
char typalign
Definition: typcache.h:39
static Datum PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
Definition: plpy_typeio.c:1147
PLyObToDatum * atts
Definition: plpy_typeio.h:114
static Datum PLyObject_ToBytea(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
Definition: plpy_typeio.c:900
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2299
FmgrInfo recinfunc
Definition: plpy_typeio.h:117
union PLyObToDatum::@166 u

◆ PLy_output_setup_record()

void PLy_output_setup_record ( PLyObToDatum arg,
TupleDesc  desc,
PLyProcedure proc 
)

Definition at line 264 of file plpy_typeio.c.

References Assert, BlessTupleDesc(), PLy_output_setup_tuple(), PLyObToTuple::recdesc, TupleDescData::tdtypeid, TupleDescData::tdtypmod, PLyObToDatum::tuple, PLyObToDatum::typmod, PLyObToDatum::typoid, and PLyObToDatum::u.

Referenced by PLy_function_build_args().

265 {
266  /* Makes no sense unless RECORD */
267  Assert(arg->typoid == RECORDOID);
268  Assert(desc->tdtypeid == RECORDOID);
269 
270  /*
271  * Bless the record type if not already done. We'd have to do this anyway
272  * to return a tuple, so we might as well force the issue so we can use
273  * the known-record-type code path.
274  */
275  BlessTupleDesc(desc);
276 
277  /*
278  * Update arg->typmod, and clear the recdesc link if it's changed. The
279  * next call of PLyObject_ToComposite will look up a long-lived tupdesc
280  * for the record type.
281  */
282  arg->typmod = desc->tdtypmod;
283  if (arg->u.tuple.recdesc &&
284  arg->u.tuple.recdesc->tdtypmod != arg->typmod)
285  arg->u.tuple.recdesc = NULL;
286 
287  /* Update derived data if necessary */
288  PLy_output_setup_tuple(arg, desc, proc);
289 }
TupleDesc recdesc
Definition: plpy_typeio.h:109
int32 tdtypmod
Definition: tupdesc.h:83
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2048
void PLy_output_setup_tuple(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:218
#define Assert(condition)
Definition: c.h:732
Oid tdtypeid
Definition: tupdesc.h:82
PLyObToTuple tuple
Definition: plpy_typeio.h:144
union PLyObToDatum::@166 u

◆ PLy_output_setup_tuple()

void PLy_output_setup_tuple ( PLyObToDatum arg,
TupleDesc  desc,
PLyProcedure proc 
)

Definition at line 218 of file plpy_typeio.c.

References Assert, PLyObToTuple::atts, PLyObToDatum::func, i, PLyObToDatum::mcxt, MemoryContextAllocZero(), TupleDescData::natts, PLyObToTuple::natts, pfree(), PLy_output_setup_func(), PLyObject_ToComposite(), PLyObToTuple::recdesc, PLyObToDatum::tuple, TupleDescAttr, PLyObToDatum::typmod, PLyObToDatum::typoid, and PLyObToDatum::u.

Referenced by PLy_exec_trigger(), PLy_output_setup_record(), and PLyObject_ToComposite().

219 {
220  int i;
221 
222  /* We should be working on a previously-set-up struct */
224 
225  /* Save pointer to tupdesc, but only if this is an anonymous record type */
226  if (arg->typoid == RECORDOID && arg->typmod < 0)
227  arg->u.tuple.recdesc = desc;
228 
229  /* (Re)allocate atts array as needed */
230  if (arg->u.tuple.natts != desc->natts)
231  {
232  if (arg->u.tuple.atts)
233  pfree(arg->u.tuple.atts);
234  arg->u.tuple.natts = desc->natts;
235  arg->u.tuple.atts = (PLyObToDatum *)
237  desc->natts * sizeof(PLyObToDatum));
238  }
239 
240  /* Fill the atts entries, except for dropped columns */
241  for (i = 0; i < desc->natts; i++)
242  {
243  Form_pg_attribute attr = TupleDescAttr(desc, i);
244  PLyObToDatum *att = &arg->u.tuple.atts[i];
245 
246  if (attr->attisdropped)
247  continue;
248 
249  if (att->typoid == attr->atttypid && att->typmod == attr->atttypmod)
250  continue; /* already set up this entry */
251 
252  PLy_output_setup_func(att, arg->mcxt,
253  attr->atttypid, attr->atttypmod,
254  proc);
255  }
256 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
PLyObToDatumFunc func
Definition: plpy_typeio.h:133
void pfree(void *pointer)
Definition: mcxt.c:1031
TupleDesc recdesc
Definition: plpy_typeio.h:109
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
static Datum PLyObject_ToComposite(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray)
Definition: plpy_typeio.c:947
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:814
#define Assert(condition)
Definition: c.h:732
MemoryContext mcxt
Definition: plpy_typeio.h:139
void PLy_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:299
PLyObToTuple tuple
Definition: plpy_typeio.h:144
int i
PLyObToDatum * atts
Definition: plpy_typeio.h:114
union PLyObToDatum::@166 u

◆ PLyBool_FromBool()

static PyObject * PLyBool_FromBool ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 553 of file plpy_typeio.c.

References DatumGetBool.

Referenced by PLy_input_setup_func().

554 {
555  if (DatumGetBool(d))
556  Py_RETURN_TRUE;
557  Py_RETURN_FALSE;
558 }
#define DatumGetBool(X)
Definition: postgres.h:393

◆ PLyBytes_FromBytea()

static PyObject * PLyBytes_FromBytea ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 631 of file plpy_typeio.c.

References DatumGetByteaPP, PyBytes_FromStringAndSize, generate_unaccent_rules::str, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by PLy_input_setup_func().

632 {
633  text *txt = DatumGetByteaPP(d);
634  char *str = VARDATA_ANY(txt);
635  size_t size = VARSIZE_ANY_EXHDR(txt);
636 
637  return PyBytes_FromStringAndSize(str, size);
638 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PyBytes_FromStringAndSize
Definition: plpython.h:88
#define DatumGetByteaPP(X)
Definition: fmgr.h:285
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:549

◆ PLyDecimal_FromNumeric()

static PyObject * PLyDecimal_FromNumeric ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 573 of file plpy_typeio.c.

References DatumGetCString, decimal_constructor, DirectFunctionCall1, ERROR, numeric_out(), PLy_elog, and generate_unaccent_rules::str.

Referenced by PLy_input_setup_func().

574 {
575  static PyObject *decimal_constructor;
576  char *str;
577  PyObject *pyvalue;
578 
579  /* Try to import cdecimal. If it doesn't exist, fall back to decimal. */
580  if (!decimal_constructor)
581  {
582  PyObject *decimal_module;
583 
584  decimal_module = PyImport_ImportModule("cdecimal");
585  if (!decimal_module)
586  {
587  PyErr_Clear();
588  decimal_module = PyImport_ImportModule("decimal");
589  }
590  if (!decimal_module)
591  PLy_elog(ERROR, "could not import a module for Decimal constructor");
592 
593  decimal_constructor = PyObject_GetAttrString(decimal_module, "Decimal");
594  if (!decimal_constructor)
595  PLy_elog(ERROR, "no Decimal attribute in module");
596  }
597 
599  pyvalue = PyObject_CallFunction(decimal_constructor, "s", str);
600  if (!pyvalue)
601  PLy_elog(ERROR, "conversion from numeric to Decimal failed");
602 
603  return pyvalue;
604 }
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:655
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
#define ERROR
Definition: elog.h:43
static PyObject * decimal_constructor
#define DatumGetCString(X)
Definition: postgres.h:566
#define PLy_elog

◆ PLyDict_FromComposite()

static PyObject * PLyDict_FromComposite ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 784 of file plpy_typeio.c.

References DatumGetHeapTupleHeader, HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, lookup_rowtype_tupdesc(), PLy_current_execution_context(), PLy_input_setup_tuple(), PLyDict_FromTuple(), ReleaseTupleDesc, HeapTupleData::t_data, and HeapTupleData::t_len.

Referenced by PLy_input_setup_func(), and PLy_input_setup_tuple().

785 {
786  PyObject *dict;
787  HeapTupleHeader td;
788  Oid tupType;
789  int32 tupTypmod;
790  TupleDesc tupdesc;
791  HeapTupleData tmptup;
792 
793  td = DatumGetHeapTupleHeader(d);
794  /* Extract rowtype info and find a tupdesc */
795  tupType = HeapTupleHeaderGetTypeId(td);
796  tupTypmod = HeapTupleHeaderGetTypMod(td);
797  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
798 
799  /* Set up I/O funcs if not done yet */
800  PLy_input_setup_tuple(arg, tupdesc,
801  PLy_current_execution_context()->curr_proc);
802 
803  /* Build a temporary HeapTuple control structure */
805  tmptup.t_data = td;
806 
807  dict = PLyDict_FromTuple(arg, &tmptup, tupdesc, true);
808 
809  ReleaseTupleDesc(tupdesc);
810 
811  return dict;
812 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1652
unsigned int Oid
Definition: postgres_ext.h:31
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:414
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:289
signed int int32
Definition: c.h:346
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:468
static PyObject * PLyDict_FromTuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
Definition: plpy_typeio.c:818
uint32 t_len
Definition: htup.h:64
void PLy_input_setup_tuple(PLyDatumToOb *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:168
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:458
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:452

◆ PLyDict_FromTuple()

static PyObject * PLyDict_FromTuple ( PLyDatumToOb arg,
HeapTuple  tuple,
TupleDesc  desc,
bool  include_generated 
)
static

Definition at line 818 of file plpy_typeio.c.

References Assert, PLyTupleToOb::atts, PLyDatumToOb::func, heap_getattr, i, sort-test::key, NameStr, PLyTupleToOb::natts, TupleDescData::natts, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLyDatumToOb::tuple, TupleDescAttr, PLyDatumToOb::u, and value.

Referenced by PLy_input_from_tuple(), and PLyDict_FromComposite().

819 {
820  PyObject *volatile dict;
821 
822  /* Simple sanity check that desc matches */
823  Assert(desc->natts == arg->u.tuple.natts);
824 
825  dict = PyDict_New();
826  if (dict == NULL)
827  return NULL;
828 
829  PG_TRY();
830  {
831  int i;
832 
833  for (i = 0; i < arg->u.tuple.natts; i++)
834  {
835  PLyDatumToOb *att = &arg->u.tuple.atts[i];
836  Form_pg_attribute attr = TupleDescAttr(desc, i);
837  char *key;
838  Datum vattr;
839  bool is_null;
840  PyObject *value;
841 
842  if (attr->attisdropped)
843  continue;
844 
845  if (attr->attgenerated)
846  {
847  /* don't include unless requested */
848  if (!include_generated)
849  continue;
850  }
851 
852  key = NameStr(attr->attname);
853  vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
854 
855  if (is_null)
856  PyDict_SetItemString(dict, key, Py_None);
857  else
858  {
859  value = att->func(att, vattr);
860  PyDict_SetItemString(dict, key, value);
861  Py_DECREF(value);
862  }
863  }
864  }
865  PG_CATCH();
866  {
867  Py_DECREF(dict);
868  PG_RE_THROW();
869  }
870  PG_END_TRY();
871 
872  return dict;
873 }
PLyDatumToOb * atts
Definition: plpy_typeio.h:49
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static struct @144 value
PLyDatumToObFunc func
Definition: plpy_typeio.h:60
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
uintptr_t Datum
Definition: postgres.h:367
#define PG_CATCH()
Definition: elog.h:310
#define Assert(condition)
Definition: c.h:732
#define PG_RE_THROW()
Definition: elog.h:331
int i
#define NameStr(name)
Definition: c.h:609
union PLyDatumToOb::@165 u
#define PG_TRY()
Definition: elog.h:301
PLyTupleToOb tuple
Definition: plpy_typeio.h:71
#define PG_END_TRY()
Definition: elog.h:317

◆ PLyFloat_FromFloat4()

static PyObject * PLyFloat_FromFloat4 ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 561 of file plpy_typeio.c.

References DatumGetFloat4.

Referenced by PLy_input_setup_func().

562 {
563  return PyFloat_FromDouble(DatumGetFloat4(d));
564 }
#define DatumGetFloat4(X)
Definition: postgres.h:680

◆ PLyFloat_FromFloat8()

static PyObject * PLyFloat_FromFloat8 ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 567 of file plpy_typeio.c.

References DatumGetFloat8.

Referenced by PLy_input_setup_func().

568 {
569  return PyFloat_FromDouble(DatumGetFloat8(d));
570 }
#define DatumGetFloat8(X)
Definition: postgres.h:728

◆ PLyGenericObject_ToComposite()

static Datum PLyGenericObject_ToComposite ( PLyObToDatum arg,
TupleDesc  desc,
PyObject *  object,
bool  inarray 
)
static

Definition at line 1502 of file plpy_typeio.c.

References PLyObToTuple::atts, ereport, errcode(), errhint(), errmsg(), ERROR, PLyObToDatum::func, heap_copy_tuple_as_datum(), heap_form_tuple(), heap_freetuple(), i, sort-test::key, NameStr, TupleDescData::natts, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLyObToDatum::tuple, TupleDescAttr, PLyObToDatum::u, value, and values.

Referenced by PLyObject_ToComposite().

1503 {
1504  Datum result;
1505  HeapTuple tuple;
1506  Datum *values;
1507  bool *nulls;
1508  volatile int i;
1509 
1510  /* Build tuple */
1511  values = palloc(sizeof(Datum) * desc->natts);
1512  nulls = palloc(sizeof(bool) * desc->natts);
1513  for (i = 0; i < desc->natts; ++i)
1514  {
1515  char *key;
1516  PyObject *volatile value;
1517  PLyObToDatum *att;
1518  Form_pg_attribute attr = TupleDescAttr(desc, i);
1519 
1520  if (attr->attisdropped)
1521  {
1522  values[i] = (Datum) 0;
1523  nulls[i] = true;
1524  continue;
1525  }
1526 
1527  key = NameStr(attr->attname);
1528  value = NULL;
1529  att = &arg->u.tuple.atts[i];
1530  PG_TRY();
1531  {
1532  value = PyObject_GetAttrString(object, key);
1533  if (!value)
1534  {
1535  /*
1536  * No attribute for this column in the object.
1537  *
1538  * If we are parsing a composite type in an array, a likely
1539  * cause is that the function contained something like "[[123,
1540  * 'foo']]". Before PostgreSQL 10, that was interpreted as an
1541  * array, with a composite type (123, 'foo') in it. But now
1542  * it's interpreted as a two-dimensional array, and we try to
1543  * interpret "123" as the composite type. See also similar
1544  * heuristic in PLyObject_ToScalar().
1545  */
1546  ereport(ERROR,
1547  (errcode(ERRCODE_UNDEFINED_COLUMN),
1548  errmsg("attribute \"%s\" does not exist in Python object", key),
1549  inarray ?
1550  errhint("To return a composite type in an array, return the composite type as a Python tuple, e.g., \"[('foo',)]\".") :
1551  errhint("To return null in a column, let the returned object have an attribute named after column with value None.")));
1552  }
1553 
1554  values[i] = att->func(att, value, &nulls[i], false);
1555 
1556  Py_XDECREF(value);
1557  value = NULL;
1558  }
1559  PG_CATCH();
1560  {
1561  Py_XDECREF(value);
1562  PG_RE_THROW();
1563  }
1564  PG_END_TRY();
1565  }
1566 
1567  tuple = heap_form_tuple(desc, values, nulls);
1568  result = heap_copy_tuple_as_datum(tuple, desc);
1569  heap_freetuple(tuple);
1570 
1571  pfree(values);
1572  pfree(nulls);
1573 
1574  return result;
1575 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static struct @144 value
int errcode(int sqlerrcode)
Definition: elog.c:570
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
PLyObToDatumFunc func
Definition: plpy_typeio.h:133
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:984
uintptr_t Datum
Definition: postgres.h:367
#define PG_CATCH()
Definition: elog.h:310
#define PG_RE_THROW()
Definition: elog.h:331
static Datum values[MAXATTR]
Definition: bootstrap.c:167
PLyObToTuple tuple
Definition: plpy_typeio.h:144
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
#define NameStr(name)
Definition: c.h:609
PLyObToDatum * atts
Definition: plpy_typeio.h:114
#define PG_TRY()
Definition: elog.h:301
#define PG_END_TRY()
Definition: elog.h:317
union PLyObToDatum::@166 u

◆ PLyInt_FromInt16()

static PyObject * PLyInt_FromInt16 ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 607 of file plpy_typeio.c.

References DatumGetInt16.

Referenced by PLy_input_setup_func().

608 {
609  return PyInt_FromLong(DatumGetInt16(d));
610 }
#define DatumGetInt16(X)
Definition: postgres.h:444

◆ PLyInt_FromInt32()

static PyObject * PLyInt_FromInt32 ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 613 of file plpy_typeio.c.

References DatumGetInt32.

Referenced by PLy_input_setup_func().

614 {
615  return PyInt_FromLong(DatumGetInt32(d));
616 }
#define DatumGetInt32(X)
Definition: postgres.h:472

◆ PLyList_FromArray()

static PyObject * PLyList_FromArray ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 670 of file plpy_typeio.c.

References ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, ARR_NULLBITMAP, PLyDatumToOb::array, Assert, DatumGetArrayTypeP, PLyArrayToOb::elm, MAXDIM, PLyList_FromArray_recurse(), and PLyDatumToOb::u.

Referenced by PLy_input_setup_func().

671 {
672  ArrayType *array = DatumGetArrayTypeP(d);
673  PLyDatumToOb *elm = arg->u.array.elm;
674  int ndim;
675  int *dims;
676  char *dataptr;
677  bits8 *bitmap;
678  int bitmask;
679 
680  if (ARR_NDIM(array) == 0)
681  return PyList_New(0);
682 
683  /* Array dimensions and left bounds */
684  ndim = ARR_NDIM(array);
685  dims = ARR_DIMS(array);
686  Assert(ndim < MAXDIM);
687 
688  /*
689  * We iterate the SQL array in the physical order it's stored in the
690  * datum. For example, for a 3-dimensional array the order of iteration
691  * would be the following: [0,0,0] elements through [0,0,k], then [0,1,0]
692  * through [0,1,k] till [0,m,k], then [1,0,0] through [1,0,k] till
693  * [1,m,k], and so on.
694  *
695  * In Python, there are no multi-dimensional lists as such, but they are
696  * represented as a list of lists. So a 3-d array of [n,m,k] elements is a
697  * list of n m-element arrays, each element of which is k-element array.
698  * PLyList_FromArray_recurse() builds the Python list for a single
699  * dimension, and recurses for the next inner dimension.
700  */
701  dataptr = ARR_DATA_PTR(array);
702  bitmap = ARR_NULLBITMAP(array);
703  bitmask = 1;
704 
705  return PLyList_FromArray_recurse(elm, dims, ndim, 0,
706  &dataptr, &bitmap, &bitmask);
707 }
#define MAXDIM
Definition: c.h:529
PLyArrayToOb array
Definition: plpy_typeio.h:70
#define ARR_DIMS(a)
Definition: array.h:282
#define ARR_DATA_PTR(a)
Definition: array.h:310
PLyDatumToOb * elm
Definition: plpy_typeio.h:38
uint8 bits8
Definition: c.h:365
#define Assert(condition)
Definition: c.h:732
#define ARR_NDIM(a)
Definition: array.h:278
static PyObject * PLyList_FromArray_recurse(PLyDatumToOb *elm, int *dims, int ndim, int dim, char **dataptr_p, bits8 **bitmap_p, int *bitmask_p)
Definition: plpy_typeio.c:710
union PLyDatumToOb::@165 u
#define ARR_NULLBITMAP(a)
Definition: array.h:288
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ PLyList_FromArray_recurse()

static PyObject * PLyList_FromArray_recurse ( PLyDatumToOb elm,
int *  dims,
int  ndim,
int  dim,
char **  dataptr_p,
bits8 **  bitmap_p,
int *  bitmask_p 
)
static

Definition at line 710 of file plpy_typeio.c.

References att_addlength_pointer, att_align_nominal, fetch_att, PLyDatumToOb::func, i, sort-test::list, PLyDatumToOb::typalign, PLyDatumToOb::typbyval, and PLyDatumToOb::typlen.

Referenced by PLyList_FromArray().

712 {
713  int i;
714  PyObject *list;
715 
716  list = PyList_New(dims[dim]);
717  if (!list)
718  return NULL;
719 
720  if (dim < ndim - 1)
721  {
722  /* Outer dimension. Recurse for each inner slice. */
723  for (i = 0; i < dims[dim]; i++)
724  {
725  PyObject *sublist;
726 
727  sublist = PLyList_FromArray_recurse(elm, dims, ndim, dim + 1,
728  dataptr_p, bitmap_p, bitmask_p);
729  PyList_SET_ITEM(list, i, sublist);
730  }
731  }
732  else
733  {
734  /*
735  * Innermost dimension. Fill the list with the values from the array
736  * for this slice.
737  */
738  char *dataptr = *dataptr_p;
739  bits8 *bitmap = *bitmap_p;
740  int bitmask = *bitmask_p;
741 
742  for (i = 0; i < dims[dim]; i++)
743  {
744  /* checking for NULL */
745  if (bitmap && (*bitmap & bitmask) == 0)
746  {
747  Py_INCREF(Py_None);
748  PyList_SET_ITEM(list, i, Py_None);
749  }
750  else
751  {
752  Datum itemvalue;
753 
754  itemvalue = fetch_att(dataptr, elm->typbyval, elm->typlen);
755  PyList_SET_ITEM(list, i, elm->func(elm, itemvalue));
756  dataptr = att_addlength_pointer(dataptr, elm->typlen, dataptr);
757  dataptr = (char *) att_align_nominal(dataptr, elm->typalign);
758  }
759 
760  /* advance bitmap pointer if any */
761  if (bitmap)
762  {
763  bitmask <<= 1;
764  if (bitmask == 0x100 /* (1<<8) */ )
765  {
766  bitmap++;
767  bitmask = 1;
768  }
769  }
770  }
771 
772  *dataptr_p = dataptr;
773  *bitmap_p = bitmap;
774  *bitmask_p = bitmask;
775  }
776 
777  return list;
778 }
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:146
PLyDatumToObFunc func
Definition: plpy_typeio.h:60
int16 typlen
Definition: plpy_typeio.h:64
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:174
uint8 bits8
Definition: c.h:365
uintptr_t Datum
Definition: postgres.h:367
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:73
int i
static PyObject * PLyList_FromArray_recurse(PLyDatumToOb *elm, int *dims, int ndim, int dim, char **dataptr_p, bits8 **bitmap_p, int *bitmask_p)
Definition: plpy_typeio.c:710

◆ PLyLong_FromInt64()

static PyObject * PLyLong_FromInt64 ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 619 of file plpy_typeio.c.

References DatumGetInt64.

Referenced by PLy_input_setup_func().

620 {
621  return PyLong_FromLongLong(DatumGetInt64(d));
622 }
#define DatumGetInt64(X)
Definition: postgres.h:607

◆ PLyLong_FromOid()

static PyObject * PLyLong_FromOid ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 625 of file plpy_typeio.c.

References DatumGetObjectId.

Referenced by PLy_input_setup_func().

626 {
627  return PyLong_FromUnsignedLong(DatumGetObjectId(d));
628 }
#define DatumGetObjectId(X)
Definition: postgres.h:500

◆ PLyMapping_ToComposite()

static Datum PLyMapping_ToComposite ( PLyObToDatum arg,
TupleDesc  desc,
PyObject *  mapping 
)
static

Definition at line 1360 of file plpy_typeio.c.

References Assert, PLyObToTuple::atts, ereport, errcode(), errhint(), errmsg(), ERROR, PLyObToDatum::func, heap_copy_tuple_as_datum(), heap_form_tuple(), heap_freetuple(), i, sort-test::key, NameStr, TupleDescData::natts, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLyObToDatum::tuple, TupleDescAttr, PLyObToDatum::u, value, and values.

Referenced by PLyObject_ToComposite().

1361 {
1362  Datum result;
1363  HeapTuple tuple;
1364  Datum *values;
1365  bool *nulls;
1366  volatile int i;
1367 
1368  Assert(PyMapping_Check(mapping));
1369 
1370  /* Build tuple */
1371  values = palloc(sizeof(Datum) * desc->natts);
1372  nulls = palloc(sizeof(bool) * desc->natts);
1373  for (i = 0; i < desc->natts; ++i)
1374  {
1375  char *key;
1376  PyObject *volatile value;
1377  PLyObToDatum *att;
1378  Form_pg_attribute attr = TupleDescAttr(desc, i);
1379 
1380  if (attr->attisdropped)
1381  {
1382  values[i] = (Datum) 0;
1383  nulls[i] = true;
1384  continue;
1385  }
1386 
1387  key = NameStr(attr->attname);
1388  value = NULL;
1389  att = &arg->u.tuple.atts[i];
1390  PG_TRY();
1391  {
1392  value = PyMapping_GetItemString(mapping, key);
1393  if (!value)
1394  ereport(ERROR,
1395  (errcode(ERRCODE_UNDEFINED_COLUMN),
1396  errmsg("key \"%s\" not found in mapping", key),
1397  errhint("To return null in a column, "
1398  "add the value None to the mapping with the key named after the column.")));
1399 
1400  values[i] = att->func(att, value, &nulls[i], false);
1401 
1402  Py_XDECREF(value);
1403  value = NULL;
1404  }
1405  PG_CATCH();
1406  {
1407  Py_XDECREF(value);
1408  PG_RE_THROW();
1409  }
1410  PG_END_TRY();
1411  }
1412 
1413  tuple = heap_form_tuple(desc, values, nulls);
1414  result = heap_copy_tuple_as_datum(tuple, desc);
1415  heap_freetuple(tuple);
1416 
1417  pfree(values);
1418  pfree(nulls);
1419 
1420  return result;
1421 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static struct @144 value
int errcode(int sqlerrcode)
Definition: elog.c:570
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
PLyObToDatumFunc func
Definition: plpy_typeio.h:133
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:984
uintptr_t Datum
Definition: postgres.h:367
#define PG_CATCH()
Definition: elog.h:310
#define Assert(condition)
Definition: c.h:732
#define PG_RE_THROW()
Definition: elog.h:331
static Datum values[MAXATTR]
Definition: bootstrap.c:167
PLyObToTuple tuple
Definition: plpy_typeio.h:144
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
#define NameStr(name)
Definition: c.h:609
PLyObToDatum * atts
Definition: plpy_typeio.h:114
#define PG_TRY()
Definition: elog.h:301
#define PG_END_TRY()
Definition: elog.h:317
union PLyObToDatum::@166 u

◆ PLyObject_AsString()

char* PLyObject_AsString ( PyObject *  plrv)

Definition at line 1030 of file plpy_typeio.c.

References elog, ereport, errcode(), errmsg(), ERROR, pg_verifymbstr(), PLy_elog, PLyUnicode_Bytes(), pstrdup(), PyBytes_AsString, and PyBytes_Size.

Referenced by PLyObject_ToScalar(), and PLyString_ToComposite().

1031 {
1032  PyObject *plrv_bo;
1033  char *plrv_sc;
1034  size_t plen;
1035  size_t slen;
1036 
1037  if (PyUnicode_Check(plrv))
1038  plrv_bo = PLyUnicode_Bytes(plrv);
1039  else if (PyFloat_Check(plrv))
1040  {
1041  /* use repr() for floats, str() is lossy */
1042 #if PY_MAJOR_VERSION >= 3
1043  PyObject *s = PyObject_Repr(plrv);
1044 
1045  plrv_bo = PLyUnicode_Bytes(s);
1046  Py_XDECREF(s);
1047 #else
1048  plrv_bo = PyObject_Repr(plrv);
1049 #endif
1050  }
1051  else
1052  {
1053 #if PY_MAJOR_VERSION >= 3
1054  PyObject *s = PyObject_Str(plrv);
1055 
1056  plrv_bo = PLyUnicode_Bytes(s);
1057  Py_XDECREF(s);
1058 #else
1059  plrv_bo = PyObject_Str(plrv);
1060 #endif
1061  }
1062  if (!plrv_bo)
1063  PLy_elog(ERROR, "could not create string representation of Python object");
1064 
1065  plrv_sc = pstrdup(PyBytes_AsString(plrv_bo));
1066  plen = PyBytes_Size(plrv_bo);
1067  slen = strlen(plrv_sc);
1068 
1069  Py_XDECREF(plrv_bo);
1070 
1071  if (slen < plen)
1072  ereport(ERROR,
1073  (errcode(ERRCODE_DATATYPE_MISMATCH),
1074  errmsg("could not convert Python object into cstring: Python string representation appears to contain null bytes")));
1075  else if (slen > plen)
1076  elog(ERROR, "could not convert Python object into cstring: Python string longer than reported length");
1077  pg_verifymbstr(plrv_sc, slen, false);
1078 
1079  return plrv_sc;
1080 }
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PyBytes_AsString
Definition: plpython.h:87
#define ERROR
Definition: elog.h:43
#define PLy_elog
#define ereport(elevel, rest)
Definition: elog.h:141
#define PyBytes_Size
Definition: plpython.h:89
PyObject * PLyUnicode_Bytes(PyObject *unicode)
Definition: plpy_util.c:25
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1914

◆ PLyObject_FromTransform()

static PyObject * PLyObject_FromTransform ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 658 of file plpy_typeio.c.

References DatumGetPointer, FunctionCall1, PLyDatumToOb::transform, PLyTransformToOb::typtransform, and PLyDatumToOb::u.

Referenced by PLy_input_setup_func().

659 {
660  Datum t;
661 
662  t = FunctionCall1(&arg->u.transform.typtransform, d);
663  return (PyObject *) DatumGetPointer(t);
664 }
FmgrInfo typtransform
Definition: plpy_typeio.h:55
PLyTransformToOb transform
Definition: plpy_typeio.h:72
uintptr_t Datum
Definition: postgres.h:367
#define DatumGetPointer(X)
Definition: postgres.h:549
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:634
union PLyDatumToOb::@165 u

◆ PLyObject_ToBool()

static Datum PLyObject_ToBool ( PLyObToDatum arg,
PyObject *  plrv,
bool isnull,
bool  inarray 
)
static

Definition at line 882 of file plpy_typeio.c.

References BoolGetDatum.

Referenced by PLy_output_setup_func().

884 {
885  if (plrv == Py_None)
886  {
887  *isnull = true;
888  return (Datum) 0;
889  }
890  *isnull = false;
891  return BoolGetDatum(PyObject_IsTrue(plrv));
892 }
uintptr_t Datum
Definition: postgres.h:367
#define BoolGetDatum(X)
Definition: postgres.h:402

◆ PLyObject_ToBytea()

static Datum PLyObject_ToBytea ( PLyObToDatum arg,
PyObject *  plrv,
bool isnull,
bool  inarray 
)
static

Definition at line 900 of file plpy_typeio.c.

References ERROR, palloc(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLy_elog, PointerGetDatum, PyBytes_AsString, PyBytes_Size, PyObject_Bytes, SET_VARSIZE, VARDATA, and VARHDRSZ.

Referenced by PLy_output_setup_func().

902 {
903  PyObject *volatile plrv_so = NULL;
904  Datum rv;
905 
906  if (plrv == Py_None)
907  {
908  *isnull = true;
909  return (Datum) 0;
910  }
911  *isnull = false;
912 
913  plrv_so = PyObject_Bytes(plrv);
914  if (!plrv_so)
915  PLy_elog(ERROR, "could not create bytes representation of Python object");
916 
917  PG_TRY();
918  {
919  char *plrv_sc = PyBytes_AsString(plrv_so);
920  size_t len = PyBytes_Size(plrv_so);
921  size_t size = len + VARHDRSZ;
922  bytea *result = palloc(size);
923 
924  SET_VARSIZE(result, size);
925  memcpy(VARDATA(result), plrv_sc, len);
926  rv = PointerGetDatum(result);
927  }
928  PG_CATCH();
929  {
930  Py_XDECREF(plrv_so);
931  PG_RE_THROW();
932  }
933  PG_END_TRY();
934 
935  Py_XDECREF(plrv_so);
936 
937  return rv;
938 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define PyObject_Bytes
Definition: plpython.h:90
#define PointerGetDatum(X)
Definition: postgres.h:556
#define VARHDRSZ
Definition: c.h:555
#define PyBytes_AsString
Definition: plpython.h:87
#define ERROR
Definition: elog.h:43
#define PLy_elog
#define PyBytes_Size
Definition: plpython.h:89
uintptr_t Datum
Definition: postgres.h:367
#define PG_CATCH()
Definition: elog.h:310
#define PG_RE_THROW()
Definition: elog.h:331
void * palloc(Size size)
Definition: mcxt.c:924
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_TRY()
Definition: elog.h:301
#define PG_END_TRY()
Definition: elog.h:317

◆ PLyObject_ToComposite()

static Datum PLyObject_ToComposite ( PLyObToDatum arg,
PyObject *  plrv,
bool isnull,
bool  inarray 
)
static

Definition at line 947 of file plpy_typeio.c.

References Assert, lookup_rowtype_tupdesc(), TupleDescData::natts, PLyObToTuple::natts, PinTupleDesc, PLy_current_execution_context(), PLy_output_setup_tuple(), PLyGenericObject_ToComposite(), PLyMapping_ToComposite(), PLySequence_ToComposite(), PLyString_ToComposite(), PLyObToTuple::recdesc, ReleaseTupleDesc, TypeCacheEntry::tupDesc, TypeCacheEntry::tupDesc_identifier, PLyObToTuple::tupdescid, PLyObToDatum::tuple, PLyObToTuple::typentry, PLyObToDatum::typmod, PLyObToDatum::typoid, and PLyObToDatum::u.

Referenced by PLy_output_setup_func(), and PLy_output_setup_tuple().

949 {
950  Datum rv;
951  TupleDesc desc;
952 
953  if (plrv == Py_None)
954  {
955  *isnull = true;
956  return (Datum) 0;
957  }
958  *isnull = false;
959 
960  /*
961  * The string conversion case doesn't require a tupdesc, nor per-field
962  * conversion data, so just go for it if that's the case to use.
963  */
964  if (PyString_Check(plrv) || PyUnicode_Check(plrv))
965  return PLyString_ToComposite(arg, plrv, inarray);
966 
967  /*
968  * If we're dealing with a named composite type, we must look up the
969  * tupdesc every time, to protect against possible changes to the type.
970  * RECORD types can't change between calls; but we must still be willing
971  * to set up the info the first time, if nobody did yet.
972  */
973  if (arg->typoid != RECORDOID)
974  {
975  desc = lookup_rowtype_tupdesc(arg->typoid, arg->typmod);
976  /* We should have the descriptor of the type's typcache entry */
977  Assert(desc == arg->u.tuple.typentry->tupDesc);
978  /* Detect change of descriptor, update cache if needed */
979  if (arg->u.tuple.tupdescid != arg->u.tuple.typentry->tupDesc_identifier)
980  {
981  PLy_output_setup_tuple(arg, desc,
982  PLy_current_execution_context()->curr_proc);
984  }
985  }
986  else
987  {
988  desc = arg->u.tuple.recdesc;
989  if (desc == NULL)
990  {
991  desc = lookup_rowtype_tupdesc(arg->typoid, arg->typmod);
992  arg->u.tuple.recdesc = desc;
993  }
994  else
995  {
996  /* Pin descriptor to match unpin below */
997  PinTupleDesc(desc);
998  }
999  }
1000 
1001  /* Simple sanity check on our caching */
1002  Assert(desc->natts == arg->u.tuple.natts);
1003 
1004  /*
1005  * Convert, using the appropriate method depending on the type of the
1006  * supplied Python object.
1007  */
1008  if (PySequence_Check(plrv))
1009  /* composite type as sequence (tuple, list etc) */
1010  rv = PLySequence_ToComposite(arg, desc, plrv);
1011  else if (PyMapping_Check(plrv))
1012  /* composite type as mapping (currently only dict) */
1013  rv = PLyMapping_ToComposite(arg, desc, plrv);
1014  else
1015  /* returned as smth, must provide method __getattr__(name) */
1016  rv = PLyGenericObject_ToComposite(arg, desc, plrv, inarray);
1017 
1018  ReleaseTupleDesc(desc);
1019 
1020  return rv;
1021 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1652
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:414
TupleDesc recdesc
Definition: plpy_typeio.h:109
uint64 tupdescid
Definition: plpy_typeio.h:112
static Datum PLyGenericObject_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *object, bool inarray)
Definition: plpy_typeio.c:1502
TypeCacheEntry * typentry
Definition: plpy_typeio.h:111
uintptr_t Datum
Definition: postgres.h:367
void PLy_output_setup_tuple(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:218
static Datum PLyString_ToComposite(PLyObToDatum *arg, PyObject *string, bool inarray)
Definition: plpy_typeio.c:1299
#define Assert(condition)
Definition: c.h:732
uint64 tupDesc_identifier
Definition: typcache.h:87
#define PinTupleDesc(tupdesc)
Definition: tupdesc.h:116
PLyObToTuple tuple
Definition: plpy_typeio.h:144
static Datum PLySequence_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *sequence)
Definition: plpy_typeio.c:1425
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
static Datum PLyMapping_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *mapping)
Definition: plpy_typeio.c:1360
TupleDesc tupDesc
Definition: typcache.h:86
union PLyObToDatum::@166 u

◆ PLyObject_ToDomain()

static Datum PLyObject_ToDomain ( PLyObToDatum arg,
PyObject *  plrv,
bool isnull,
bool  inarray 
)
static

Definition at line 1113 of file plpy_typeio.c.

References PLyObToDomain::base, PLyObToDatum::domain, domain_check(), PLyObToDomain::domain_info, PLyObToDatum::func, PLyObToDatum::mcxt, PLyObToDatum::typoid, and PLyObToDatum::u.

Referenced by PLy_output_setup_func().

1115 {
1116  Datum result;
1117  PLyObToDatum *base = arg->u.domain.base;
1118 
1119  result = base->func(base, plrv, isnull, inarray);
1120  domain_check(result, *isnull, arg->typoid,
1121  &arg->u.domain.domain_info, arg->mcxt);
1122  return result;
1123 }
void * domain_info
Definition: plpy_typeio.h:123
PLyObToDatum * base
Definition: plpy_typeio.h:122
PLyObToDatumFunc func
Definition: plpy_typeio.h:133
PLyObToDomain domain
Definition: plpy_typeio.h:145
uintptr_t Datum
Definition: postgres.h:367
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:327
MemoryContext mcxt
Definition: plpy_typeio.h:139
union PLyObToDatum::@166 u

◆ PLyObject_ToScalar()

static Datum PLyObject_ToScalar ( PLyObToDatum arg,
PyObject *  plrv,
bool isnull,
bool  inarray 
)
static

Definition at line 1088 of file plpy_typeio.c.

References InputFunctionCall(), PLyObject_AsString(), PLyObToDatum::scalar, generate_unaccent_rules::str, PLyObToScalar::typfunc, PLyObToScalar::typioparam, PLyObToDatum::typmod, and PLyObToDatum::u.

Referenced by PLy_output_setup_func().

1090 {
1091  char *str;
1092 
1093  if (plrv == Py_None)
1094  {
1095  *isnull = true;
1096  return (Datum) 0;
1097  }
1098  *isnull = false;
1099 
1100  str = PLyObject_AsString(plrv);
1101 
1102  return InputFunctionCall(&arg->u.scalar.typfunc,
1103  str,
1104  arg->u.scalar.typioparam,
1105  arg->typmod);
1106 }
FmgrInfo typfunc
Definition: plpy_typeio.h:96
char * PLyObject_AsString(PyObject *plrv)
Definition: plpy_typeio.c:1030
PLyObToScalar scalar
Definition: plpy_typeio.h:142
uintptr_t Datum
Definition: postgres.h:367
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1531
union PLyObToDatum::@166 u

◆ PLyObject_ToTransform()

static Datum PLyObject_ToTransform ( PLyObToDatum arg,
PyObject *  plrv,
bool isnull,
bool  inarray 
)
static

Definition at line 1130 of file plpy_typeio.c.

References FunctionCall1, PointerGetDatum, PLyObToDatum::transform, PLyObToTransform::typtransform, and PLyObToDatum::u.

Referenced by PLy_output_setup_func().

1132 {
1133  if (plrv == Py_None)
1134  {
1135  *isnull = true;
1136  return (Datum) 0;
1137  }
1138  *isnull = false;
1139  return FunctionCall1(&arg->u.transform.typtransform, PointerGetDatum(plrv));
1140 }
#define PointerGetDatum(X)
Definition: postgres.h:556
PLyObToTransform transform
Definition: plpy_typeio.h:146
FmgrInfo typtransform
Definition: plpy_typeio.h:128
uintptr_t Datum
Definition: postgres.h:367
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:634
union PLyObToDatum::@166 u

◆ PLySequence_ToArray()

static Datum PLySequence_ToArray ( PLyObToDatum arg,
PyObject *  plrv,
bool isnull,
bool  inarray 
)
static

Definition at line 1147 of file plpy_typeio.c.

References PLyObToDatum::array, construct_md_array(), PLyObToArray::elm, PLyObToArray::elmbasetype, ERROR, i, MaxAllocSize, MAXDIM, next, palloc(), PLy_elog, PLySequence_ToArray_recurse(), PointerGetDatum, PLyObToDatum::typalign, PLyObToDatum::typbyval, PLyObToDatum::typlen, and PLyObToDatum::u.

Referenced by PLy_output_setup_func().

1149 {
1150  ArrayType *array;
1151  int i;
1152  Datum *elems;
1153  bool *nulls;
1154  int64 len;
1155  int ndim;
1156  int dims[MAXDIM];
1157  int lbs[MAXDIM];
1158  int currelem;
1159  PyObject *pyptr = plrv;
1160  PyObject *next;
1161 
1162  if (plrv == Py_None)
1163  {
1164  *isnull = true;
1165  return (Datum) 0;
1166  }
1167  *isnull = false;
1168 
1169  /*
1170  * Determine the number of dimensions, and their sizes.
1171  */
1172  ndim = 0;
1173  len = 1;
1174 
1175  Py_INCREF(plrv);
1176 
1177  for (;;)
1178  {
1179  if (!PyList_Check(pyptr))
1180  break;
1181 
1182  if (ndim == MAXDIM)
1183  PLy_elog(ERROR, "number of array dimensions exceeds the maximum allowed (%d)", MAXDIM);
1184 
1185  dims[ndim] = PySequence_Length(pyptr);
1186  if (dims[ndim] < 0)
1187  PLy_elog(ERROR, "could not determine sequence length for function return value");
1188 
1189  if (dims[ndim] > MaxAllocSize)
1190  PLy_elog(ERROR, "array size exceeds the maximum allowed");
1191 
1192  len *= dims[ndim];
1193  if (len > MaxAllocSize)
1194  PLy_elog(ERROR, "array size exceeds the maximum allowed");
1195 
1196  if (dims[ndim] == 0)
1197  {
1198  /* empty sequence */
1199  break;
1200  }
1201 
1202  ndim++;
1203 
1204  next = PySequence_GetItem(pyptr, 0);
1205  Py_XDECREF(pyptr);
1206  pyptr = next;
1207  }
1208  Py_XDECREF(pyptr);
1209 
1210  /*
1211  * Check for zero dimensions. This happens if the object is a tuple or a
1212  * string, rather than a list, or is not a sequence at all. We don't map
1213  * tuples or strings to arrays in general, but in the first level, be
1214  * lenient, for historical reasons. So if the object is a sequence of any
1215  * kind, treat it as a one-dimensional array.
1216  */
1217  if (ndim == 0)
1218  {
1219  if (!PySequence_Check(plrv))
1220  PLy_elog(ERROR, "return value of function with array return type is not a Python sequence");
1221 
1222  ndim = 1;
1223  len = dims[0] = PySequence_Length(plrv);
1224  }
1225 
1226  /*
1227  * Traverse the Python lists, in depth-first order, and collect all the
1228  * elements at the bottom level into 'elems'/'nulls' arrays.
1229  */
1230  elems = palloc(sizeof(Datum) * len);
1231  nulls = palloc(sizeof(bool) * len);
1232  currelem = 0;
1234  dims, ndim, 0,
1235  elems, nulls, &currelem);
1236 
1237  for (i = 0; i < ndim; i++)
1238  lbs[i] = 1;
1239 
1240  array = construct_md_array(elems,
1241  nulls,
1242  ndim,
1243  dims,
1244  lbs,
1245  arg->u.array.elmbasetype,
1246  arg->u.array.elm->typlen,
1247  arg->u.array.elm->typbyval,
1248  arg->u.array.elm->typalign);
1249 
1250  return PointerGetDatum(array);
1251 }
static int32 next
Definition: blutils.c:213
#define MAXDIM
Definition: c.h:529
#define PointerGetDatum(X)
Definition: postgres.h:556
PLyObToArray array
Definition: plpy_typeio.h:143
#define ERROR
Definition: elog.h:43
#define PLy_elog
#define MaxAllocSize
Definition: memutils.h:40
uintptr_t Datum
Definition: postgres.h:367
PLyObToDatum * elm
Definition: plpy_typeio.h:102
void * palloc(Size size)
Definition: mcxt.c:924
int i
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3326
static void PLySequence_ToArray_recurse(PLyObToDatum *elm, PyObject *list, int *dims, int ndim, int dim, Datum *elems, bool *nulls, int *currelem)
Definition: plpy_typeio.c:1258
union PLyObToDatum::@166 u

◆ PLySequence_ToArray_recurse()

static void PLySequence_ToArray_recurse ( PLyObToDatum elm,
PyObject *  list,
int *  dims,
int  ndim,
int  dim,
Datum elems,
bool nulls,
int *  currelem 
)
static

Definition at line 1258 of file plpy_typeio.c.

References ereport, errdetail(), errmsg(), ERROR, PLyObToDatum::func, and i.

Referenced by PLySequence_ToArray().

1261 {
1262  int i;
1263 
1264  if (PySequence_Length(list) != dims[dim])
1265  ereport(ERROR,
1266  (errmsg("wrong length of inner sequence: has length %d, but %d was expected",
1267  (int) PySequence_Length(list), dims[dim]),
1268  (errdetail("To construct a multidimensional array, the inner sequences must all have the same length."))));
1269 
1270  if (dim < ndim - 1)
1271  {
1272  for (i = 0; i < dims[dim]; i++)
1273  {
1274  PyObject *sublist = PySequence_GetItem(list, i);
1275 
1276  PLySequence_ToArray_recurse(elm, sublist, dims, ndim, dim + 1,
1277  elems, nulls, currelem);
1278  Py_XDECREF(sublist);
1279  }
1280  }
1281  else
1282  {
1283  for (i = 0; i < dims[dim]; i++)
1284  {
1285  PyObject *obj = PySequence_GetItem(list, i);
1286 
1287  elems[*currelem] = elm->func(elm, obj, &nulls[*currelem], true);
1288  Py_XDECREF(obj);
1289  (*currelem)++;
1290  }
1291  }
1292 }
PLyObToDatumFunc func
Definition: plpy_typeio.h:133
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
static void PLySequence_ToArray_recurse(PLyObToDatum *elm, PyObject *list, int *dims, int ndim, int dim, Datum *elems, bool *nulls, int *currelem)
Definition: plpy_typeio.c:1258

◆ PLySequence_ToComposite()

static Datum PLySequence_ToComposite ( PLyObToDatum arg,
TupleDesc  desc,
PyObject *  sequence 
)
static

Definition at line 1425 of file plpy_typeio.c.

References Assert, PLyObToTuple::atts, ereport, errcode(), errmsg(), ERROR, PLyObToDatum::func, heap_copy_tuple_as_datum(), heap_form_tuple(), heap_freetuple(), i, idx(), TupleDescData::natts, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLyObToDatum::tuple, TupleDescAttr, PLyObToDatum::u, value, and values.

Referenced by PLyObject_ToComposite().

1426 {
1427  Datum result;
1428  HeapTuple tuple;
1429  Datum *values;
1430  bool *nulls;
1431  volatile int idx;
1432  volatile int i;
1433 
1434  Assert(PySequence_Check(sequence));
1435 
1436  /*
1437  * Check that sequence length is exactly same as PG tuple's. We actually
1438  * can ignore exceeding items or assume missing ones as null but to avoid
1439  * plpython developer's errors we are strict here
1440  */
1441  idx = 0;
1442  for (i = 0; i < desc->natts; i++)
1443  {
1444  if (!TupleDescAttr(desc, i)->attisdropped)
1445  idx++;
1446  }
1447  if (PySequence_Length(sequence) != idx)
1448  ereport(ERROR,
1449  (errcode(ERRCODE_DATATYPE_MISMATCH),
1450  errmsg("length of returned sequence did not match number of columns in row")));
1451 
1452  /* Build tuple */
1453  values = palloc(sizeof(Datum) * desc->natts);
1454  nulls = palloc(sizeof(bool) * desc->natts);
1455  idx = 0;
1456  for (i = 0; i < desc->natts; ++i)
1457  {
1458  PyObject *volatile value;
1459  PLyObToDatum *att;
1460 
1461  if (TupleDescAttr(desc, i)->attisdropped)
1462  {
1463  values[i] = (Datum) 0;
1464  nulls[i] = true;
1465  continue;
1466  }
1467 
1468  value = NULL;
1469  att = &arg->u.tuple.atts[i];
1470  PG_TRY();
1471  {
1472  value = PySequence_GetItem(sequence, idx);
1473  Assert(value);
1474 
1475  values[i] = att->func(att, value, &nulls[i], false);
1476 
1477  Py_XDECREF(value);
1478  value = NULL;
1479  }
1480  PG_CATCH();
1481  {
1482  Py_XDECREF(value);
1483  PG_RE_THROW();
1484  }
1485  PG_END_TRY();
1486 
1487  idx++;
1488  }
1489 
1490  tuple = heap_form_tuple(desc, values, nulls);
1491  result = heap_copy_tuple_as_datum(tuple, desc);
1492  heap_freetuple(tuple);
1493 
1494  pfree(values);
1495  pfree(nulls);
1496 
1497  return result;
1498 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static struct @144 value
int errcode(int sqlerrcode)
Definition: elog.c:570
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
PLyObToDatumFunc func
Definition: plpy_typeio.h:133
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:984
uintptr_t Datum
Definition: postgres.h:367
#define PG_CATCH()
Definition: elog.h:310
#define Assert(condition)
Definition: c.h:732
#define PG_RE_THROW()
Definition: elog.h:331
static Datum values[MAXATTR]
Definition: bootstrap.c:167
PLyObToTuple tuple
Definition: plpy_typeio.h:144
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
PLyObToDatum * atts
Definition: plpy_typeio.h:114
#define PG_TRY()
Definition: elog.h:301
#define PG_END_TRY()
Definition: elog.h:317
union PLyObToDatum::@166 u

◆ PLyString_FromScalar()

static PyObject * PLyString_FromScalar ( PLyDatumToOb arg,
Datum  d 
)
static

Definition at line 645 of file plpy_typeio.c.

References OutputFunctionCall(), pfree(), PLyDatumToOb::scalar, PLyScalarToOb::typfunc, and PLyDatumToOb::u.

Referenced by PLy_input_setup_func().

646 {
647  char *x = OutputFunctionCall(&arg->u.scalar.typfunc, d);
648  PyObject *r = PyString_FromString(x);
649 
650  pfree(x);
651  return r;
652 }
FmgrInfo typfunc
Definition: plpy_typeio.h:33
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1575
void pfree(void *pointer)
Definition: mcxt.c:1031
PLyScalarToOb scalar
Definition: plpy_typeio.h:69
union PLyDatumToOb::@165 u

◆ PLyString_ToComposite()

static Datum PLyString_ToComposite ( PLyObToDatum arg,
PyObject *  string,
bool  inarray 
)
static

Definition at line 1299 of file plpy_typeio.c.

References ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, fmgr_info_cxt(), FmgrInfo::fn_oid, InputFunctionCall(), PLyObToDatum::mcxt, OidIsValid, PLyObject_AsString(), PLyObToTuple::recinfunc, generate_unaccent_rules::str, PLyObToDatum::tuple, PLyObToDatum::typmod, PLyObToDatum::typoid, and PLyObToDatum::u.

Referenced by PLyObject_ToComposite().

1300 {
1301  char *str;
1302 
1303  /*
1304  * Set up call data for record_in, if we didn't already. (We can't just
1305  * use DirectFunctionCall, because record_in needs a fn_extra field.)
1306  */
1307  if (!OidIsValid(arg->u.tuple.recinfunc.fn_oid))
1308  fmgr_info_cxt(F_RECORD_IN, &arg->u.tuple.recinfunc, arg->mcxt);
1309 
1310  str = PLyObject_AsString(string);
1311 
1312  /*
1313  * If we are parsing a composite type within an array, and the string
1314  * isn't a valid record literal, there's a high chance that the function
1315  * did something like:
1316  *
1317  * CREATE FUNCTION .. RETURNS comptype[] AS $$ return [['foo', 'bar']] $$
1318  * LANGUAGE plpython;
1319  *
1320  * Before PostgreSQL 10, that was interpreted as a single-dimensional
1321  * array, containing record ('foo', 'bar'). PostgreSQL 10 added support
1322  * for multi-dimensional arrays, and it is now interpreted as a
1323  * two-dimensional array, containing two records, 'foo', and 'bar'.
1324  * record_in() will throw an error, because "foo" is not a valid record
1325  * literal.
1326  *
1327  * To make that less confusing to users who are upgrading from older
1328  * versions, try to give a hint in the typical instances of that. If we
1329  * are parsing an array of composite types, and we see a string literal
1330  * that is not a valid record literal, give a hint. We only want to give
1331  * the hint in the narrow case of a malformed string literal, not any
1332  * error from record_in(), so check for that case here specifically.
1333  *
1334  * This check better match the one in record_in(), so that we don't forbid
1335  * literals that are actually valid!
1336  */
1337  if (inarray)
1338  {
1339  char *ptr = str;
1340 
1341  /* Allow leading whitespace */
1342  while (*ptr && isspace((unsigned char) *ptr))
1343  ptr++;
1344  if (*ptr++ != '(')
1345  ereport(ERROR,
1346  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1347  errmsg("malformed record literal: \"%s\"", str),
1348  errdetail("Missing left parenthesis."),
1349  errhint("To return a composite type in an array, return the composite type as a Python tuple, e.g., \"[('foo',)]\".")));
1350  }
1351 
1352  return InputFunctionCall(&arg->u.tuple.recinfunc,
1353  str,
1354  arg->typoid,
1355  arg->typmod);
1356 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
char * PLyObject_AsString(PyObject *plrv)
Definition: plpy_typeio.c:1030
int errdetail(const char *fmt,...)
Definition: elog.c:860
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
#define ereport(elevel, rest)
Definition: elog.h:141
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1531
Oid fn_oid
Definition: fmgr.h:59
MemoryContext mcxt
Definition: plpy_typeio.h:139
PLyObToTuple tuple
Definition: plpy_typeio.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:784
FmgrInfo recinfunc
Definition: plpy_typeio.h:117
union PLyObToDatum::@166 u