-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy_plpython.c
More file actions
53 lines (41 loc) · 1.43 KB
/
numpy_plpython.c
File metadata and controls
53 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "postgres.h"
#include "plpython.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "fmgr.h"
#include "utils/array.h"
#include "utils/fmgrprotos.h"
#include "utils/numeric.h"
#include "catalog/pg_type.h"
#include "numpy/ndarrayobject.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(numpy_to_plpython);
Datum numpy_to_plpython(PG_FUNCTION_ARGS) {
PyObject *result;
ArrayType* arr = PG_GETARG_ARRAYTYPE_P_COPY(0);
const int nd = ARR_NDIM(arr);
npy_intp ndims[MAXDIM];
//not sure if this is required, believe can likely just pass in
//without this.
for (int i = 0; i < nd; ++i) {
ndims[i] = ARR_DIMS(arr)[i];
}
result = PyArray_SimpleNewFromData(nd, ndims, NPY_CFLOAT, ARR_DATA_PTR(arr) );
return PointerGetDatum(result);
}
PG_FUNCTION_INFO_V1(plpython_to_numpy);
Datum plpython_to_numpy(PG_FUNCTION_ARGS) {
import_array();
ArrayType* result;
PyObject *o = (PyObject *) PG_GETARG_POINTER(0);
elog(NOTICE, "The object we reiceved has a type of %s", Py_TYPE(o)->tp_name);
PyArrayObject *obj = (PyArrayObject*)(o);
elog(NOTICE, "New size is %d", PyArray_SIZE(obj));
const npy_intp size = PyArray_SIZE(obj);
Datum* arr = (Datum*)palloc(size * sizeof(Datum));
float* ptr = (float*)PyArray_DATA(obj);
for (npy_intp i = 0; i < size; ++i) {
arr[i] = Float4GetDatum(ptr[i]);
}
result = construct_array(arr, size, FLOAT4OID, 4, true, 'i');
PG_RETURN_ARRAYTYPE_P(result);
}