Changeset: fbc06a07b6e3 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=fbc06a07b6e3
Added Files:
        monetdb5/extras/pyapi/50_pyapi.mal
        monetdb5/extras/pyapi/Makefile.ag
        monetdb5/extras/pyapi/Tests/All
        monetdb5/extras/pyapi/Tests/pyapi00.malC
        monetdb5/extras/pyapi/pyapi.c
        monetdb5/extras/pyapi/pyapi.h
        monetdb5/extras/pyapi/pyapi.mal
Modified Files:
        configure.ag
        monetdb5/extras/Makefile.ag
        testing/Mtest.py.in
Branch: pyapi
Log Message:

Python API: MAL level import


diffs (truncated from 600 to 300 lines):

diff --git a/configure.ag b/configure.ag
--- a/configure.ag
+++ b/configure.ag
@@ -226,6 +226,13 @@ AC_ARG_ENABLE(rintegration,
        enable_rintegration=$enableval,
        enable_rintegration=$dft_rintegration)
 
+dft_pyintegration=auto
+AC_ARG_ENABLE(pyintegration,
+       AS_HELP_STRING([--enable-pyintegration],
+               [enable support for Python integration into MonetDB 
(default=auto)]),
+       enable_pyintegration=$enableval,
+       enable_pyintegration=$dft_pyintegration)
+
 dft_odbc=auto
 AC_ARG_ENABLE(odbc,
        AS_HELP_STRING([--enable-odbc],
@@ -2211,6 +2218,7 @@ AC_SUBST(SPHINXCLIENT_CFLAGS, $SPHINXCLI
 AC_SUBST(SPHINXCLIENT_LIBS, $SPHINXCLIENT_LIBS)
 AM_CONDITIONAL(HAVE_SPHINXCLIENT, test x"$have_sphinxclient" != xno)
 
+# R API (embedded R)
 have_libr=no
 if test "x$enable_rintegration" != xno; then
        case "$enable_rintegration" in
@@ -2246,6 +2254,41 @@ if test "x$enable_rintegration" != xno; 
 fi
 AM_CONDITIONAL(HAVE_LIBR, test x"$have_libr" != xno)
 
+# Python API (embedded Python)
+have_libpy=no
+if test "x$enable_pyintegration" != xno; then
+       case "$enable_pyintegration" in
+       yes|auto)
+               XPATH="$PATH"
+               ;;
+       /*)
+               XPATH="$enable_pyintegration"
+               enable_pyintegration=yes
+               ;;
+       *)
+               AC_MSG_ERROR([--enable-pyintegration value must be 
yes|no|auto|absolute path of python-config])
+               ;;
+       esac
+       AC_PATH_PROG(PYCMD,python-config,,$XPATH)
+       if test "x$PYCMD" = x; then
+               if test "x$enable_pyintegration" = xyes; then
+                       AC_MSG_ERROR([python-config library required for Python 
integration support])
+               else
+                       have_libpy="no"
+                       why_have_libpy="(python-config command not found)"
+                       enable_pyintegration=no
+                       disable_pyintegration="(python-config command not 
found)"
+               fi
+       elif libpy_CFLAGS=`$PYCMD --cflags ` && libpy_LIBS=`$PYCMD --ldflags`; 
then
+               have_libpy=yes
+               AC_DEFINE(HAVE_LIBPY, 1, [Define if we can link to python])
+               AC_SUBST(libpy_CFLAGS, $libpy_CFLAGS)
+               AC_SUBST(libpy_LIBS, $libpy_LIBS)
+       fi
+fi
+AM_CONDITIONAL(HAVE_LIBPY, test x"$have_libpy" != xno)
+
+
 # ODBC, only used by ODBC driver
 if test "x$enable_odbc" != xno; then
        have_unixodbc=auto
@@ -3270,25 +3313,26 @@ done
 echo
 echo "* Enabled/disabled components:"
 for comp in \
-       'gdk         ' \
-       'monetdb5    ' \
-       'sql         ' \
-       'geom        ' \
-       'console     ' \
-       'gsl         ' \
-       'fits        ' \
-       'jsonstore   ' \
-       'microhttpd  ' \
-       'rintegration' \
-       'odbc        ' \
-       'jdbc        ' \
-       'control     ' \
-       'testing     ' \
-       'developer   ' \
-       'strict      ' \
-       'debug       ' \
-       'assert      ' \
-       'optimize    ' \
+       'gdk          ' \
+       'monetdb5     ' \
+       'sql          ' \
+       'geom         ' \
+       'console      ' \
+       'gsl          ' \
+       'fits         ' \
+       'jsonstore    ' \
+       'microhttpd   ' \
+       'rintegration ' \
+       'pyintegration' \
+       'odbc         ' \
+       'jdbc         ' \
+       'control      ' \
+       'testing      ' \
+       'developer    ' \
+       'strict       ' \
+       'debug        ' \
+       'assert       ' \
+       'optimize     ' \
 ; do
        eval "dft=\$dft_$comp"
        eval "enable=\$enable_$comp"
diff --git a/monetdb5/extras/Makefile.ag b/monetdb5/extras/Makefile.ag
--- a/monetdb5/extras/Makefile.ag
+++ b/monetdb5/extras/Makefile.ag
@@ -4,5 +4,5 @@
 #
 # Copyright 2008-2015 MonetDB B.V.
 
-SUBDIRS = HAVE_SPHINXCLIENT?sphinx mal_optimizer_template HAVE_LIBR?rapi
+SUBDIRS = HAVE_SPHINXCLIENT?sphinx mal_optimizer_template HAVE_LIBR?rapi 
HAVE_LIBPY?pyapi
 
diff --git a/monetdb5/extras/pyapi/50_pyapi.mal 
b/monetdb5/extras/pyapi/50_pyapi.mal
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/pyapi/50_pyapi.mal
@@ -0,0 +1,8 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0.  If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# Copyright 2008-2015 MonetDB B.V.
+
+# This loads the MonetDB/PyAPI module
+include pyapi;
diff --git a/monetdb5/extras/pyapi/Makefile.ag 
b/monetdb5/extras/pyapi/Makefile.ag
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/pyapi/Makefile.ag
@@ -0,0 +1,40 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0.  If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# Copyright 2008-2015 MonetDB B.V.
+
+INCLUDES = \
+       ../../../common/options \
+       ../../../common/stream \
+       ../../../gdk \
+       ../../mal \
+       ../../../sql/include \
+       $(libpy_CFLAGS)
+
+MTSAFE
+
+lib__pyapi = {
+       MODULE
+       DIR = libdir/monetdb5
+       SOURCES = pyapi.c pyapi.h
+       LIBS = ../../tools/libmonetdb5 \
+       ../../../gdk/libbat \
+       $(MALLOC_LIBS) $(libpy_LIBS)
+}
+
+
+headers_pyapi_mal = {
+       HEADERS = mal
+       DIR = libdir/monetdb5
+       SOURCES = pyapi.mal
+}
+
+
+headers_autoload = {
+       HEADERS = mal
+       DIR = libdir/monetdb5/autoload
+       SOURCES = 50_pyapi.mal
+}
+
+EXTRA_DIST_DIR = Tests
diff --git a/monetdb5/extras/pyapi/Tests/All b/monetdb5/extras/pyapi/Tests/All
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/pyapi/Tests/All
@@ -0,0 +1,1 @@
+HAVE_LIBPY?pyapi00
diff --git a/monetdb5/extras/pyapi/Tests/pyapi00.malC 
b/monetdb5/extras/pyapi/Tests/pyapi00.malC
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/pyapi/Tests/pyapi00.malC
@@ -0,0 +1,18 @@
+b:= bat.new(:oid,:int);
+bat.append(b,1804289383);
+bat.append(b,846930886);
+bat.append(b,1681692777);
+bat.append(b,1714636915);
+bat.append(b,1957747793);
+bat.append(b,424238335);
+bat.append(b,719885386);
+bat.append(b,1649760492);
+bat.append(b,596516649);
+bat.append(b,1189641421);
+
+io.print(b);
+
+r:bat[:oid,:dbl] := pyapi.eval(nil:ptr,"TODO",b);
+io.print(r);
+
+
diff --git a/monetdb5/extras/pyapi/pyapi.c b/monetdb5/extras/pyapi/pyapi.c
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/pyapi/pyapi.c
@@ -0,0 +1,273 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 2008-2015 MonetDB B.V.
+ */
+
+#include "monetdb_config.h"
+#include "mal.h"
+#include "mal_stack.h"
+#include "mal_linker.h"
+#include "gdk_utils.h"
+#include "gdk.h"
+#include "sql_catalog.h"
+
+#include "pyapi.h"
+
+#include <Python.h>
+
+// other headers
+#include <string.h>
+
+const char* pyapi_enableflag = "embedded_py";
+
+int PyAPIEnabled(void) {
+       return (GDKgetenv_istrue(pyapi_enableflag)
+                       || GDKgetenv_isyes(pyapi_enableflag));
+}
+
+// TODO: can we call the Python interpreter in a multi-thread environment?
+static MT_Lock pyapiLock;
+static int pyapiInitialized = FALSE;
+
+
+static int PyAPIinitialize(void) {
+       Py_Initialize();
+       pyapiInitialized++;
+       return 0;
+}
+
+pyapi_export str PyAPIevalStd(Client cntxt, MalBlkPtr mb, MalStkPtr stk,
+                                                       InstrPtr pci) {
+       return PyAPIeval(cntxt, mb, stk, pci, 0);
+}
+pyapi_export str PyAPIevalAggr(Client cntxt, MalBlkPtr mb, MalStkPtr stk,
+                                                        InstrPtr pci) {
+       return PyAPIeval(cntxt, mb, stk, pci, 1);
+}
+
+str PyAPIeval(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci, bit 
grouped) {
+
+       sql_func * sqlfun = *(sql_func**) getArgReference(stk, pci, pci->retc);
+               str exprStr = *getArgReference_str(stk, pci, pci->retc + 1);
+
+               int i = 1, ai = 0;
+               char argbuf[64];
+               char argnames[1000] = "";
+               size_t pos;
+               char* rcall = NULL;
+               size_t rcalllen;
+               size_t ret_rows = 0;
+               //int ret_cols = 0; /* int because pci->retc is int, too*/
+               str *args;
+               //int evalErr;
+               char *msg = MAL_SUCCEED;
+               BAT *b;
+               BUN cnt;
+               node * argnode;
+               int seengrp = FALSE;
+               PyObject *pArgs; // this is going to be the parameter tuple!
+
+               // we don't need no context, but the compiler needs us to touch 
it (...)
+               (void) cntxt;
+
+               if (!PyAPIEnabled()) {
+                       throw(MAL, "pyapi.eval",
+                                 "Embedded Python has not been enabled. Start 
server with --set %s=true",
+                                 pyapi_enableflag);
+               }
+
+               rcalllen = strlen(exprStr) + sizeof(argnames) + 100;
+               rcall = malloc(rcalllen);
+               args = (str*) GDKzalloc(sizeof(str) * pci->argc);
+
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to