diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:15:05 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:15:05 +0000 |
commit | 46651ce6fe013220ed397add242004d764fc0153 (patch) | |
tree | 6e5299f990f88e60174a1d3ae6e48eedd2688b2b /contrib/ltree_plpython | |
parent | Initial commit. (diff) | |
download | postgresql-14-46651ce6fe013220ed397add242004d764fc0153.tar.xz postgresql-14-46651ce6fe013220ed397add242004d764fc0153.zip |
Adding upstream version 14.5.upstream/14.5upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | contrib/ltree_plpython/.gitignore | 6 | ||||
-rw-r--r-- | contrib/ltree_plpython/Makefile | 45 | ||||
-rw-r--r-- | contrib/ltree_plpython/expected/ltree_plpython.out | 43 | ||||
-rw-r--r-- | contrib/ltree_plpython/ltree_plpython.c | 64 | ||||
-rw-r--r-- | contrib/ltree_plpython/ltree_plpython2u--1.0.sql | 12 | ||||
-rw-r--r-- | contrib/ltree_plpython/ltree_plpython2u.control | 6 | ||||
-rw-r--r-- | contrib/ltree_plpython/ltree_plpython3u--1.0.sql | 12 | ||||
-rw-r--r-- | contrib/ltree_plpython/ltree_plpython3u.control | 6 | ||||
-rw-r--r-- | contrib/ltree_plpython/ltree_plpythonu--1.0.sql | 12 | ||||
-rw-r--r-- | contrib/ltree_plpython/ltree_plpythonu.control | 6 | ||||
-rw-r--r-- | contrib/ltree_plpython/sql/ltree_plpython.sql | 36 |
11 files changed, 248 insertions, 0 deletions
diff --git a/contrib/ltree_plpython/.gitignore b/contrib/ltree_plpython/.gitignore new file mode 100644 index 0000000..ce6fab9 --- /dev/null +++ b/contrib/ltree_plpython/.gitignore @@ -0,0 +1,6 @@ +# Generated subdirectories +/expected/python3/ +/log/ +/results/ +/sql/python3/ +/tmp_check/ diff --git a/contrib/ltree_plpython/Makefile b/contrib/ltree_plpython/Makefile new file mode 100644 index 0000000..12a0146 --- /dev/null +++ b/contrib/ltree_plpython/Makefile @@ -0,0 +1,45 @@ +# contrib/ltree_plpython/Makefile + +MODULE_big = ltree_plpython$(python_majorversion) +OBJS = \ + $(WIN32RES) \ + ltree_plpython.o +PGFILEDESC = "ltree_plpython - ltree transform for plpython" + +EXTENSION = ltree_plpythonu ltree_plpython2u ltree_plpython3u +DATA = ltree_plpythonu--1.0.sql ltree_plpython2u--1.0.sql ltree_plpython3u--1.0.sql + +REGRESS = ltree_plpython +REGRESS_PLPYTHON3_MANGLE := $(REGRESS) + +PG_CPPFLAGS = $(python_includespec) -DPLPYTHON_LIBNAME='"plpython$(python_majorversion)"' + +ifdef USE_PGXS +PG_CPPFLAGS += -I$(includedir_server)/extension +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +PG_CPPFLAGS += -I$(top_srcdir)/src/pl/plpython -I$(top_srcdir)/contrib +subdir = contrib/ltree_plpython +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif + +# We must link libpython explicitly +ifeq ($(PORTNAME), win32) +# ... see silliness in plpython Makefile ... +SHLIB_LINK_INTERNAL += $(sort $(wildcard ../../src/pl/plpython/libpython*.a)) +else +rpathdir = $(python_libdir) +SHLIB_LINK += $(python_libspec) $(python_additional_libs) +endif + +REGRESS_OPTS += --load-extension=ltree +ifeq ($(python_majorversion),2) +REGRESS_OPTS += --load-extension=plpythonu --load-extension=ltree_plpythonu +endif +EXTRA_INSTALL += contrib/ltree + +include $(top_srcdir)/src/pl/plpython/regress-python3-mangle.mk diff --git a/contrib/ltree_plpython/expected/ltree_plpython.out b/contrib/ltree_plpython/expected/ltree_plpython.out new file mode 100644 index 0000000..f28897f --- /dev/null +++ b/contrib/ltree_plpython/expected/ltree_plpython.out @@ -0,0 +1,43 @@ +CREATE EXTENSION ltree_plpython2u CASCADE; +NOTICE: installing required extension "plpython2u" +CREATE FUNCTION test1(val ltree) RETURNS int +LANGUAGE plpythonu +TRANSFORM FOR TYPE ltree +AS $$ +plpy.info(repr(val)) +return len(val) +$$; +SELECT test1('aa.bb.cc'::ltree); +INFO: ['aa', 'bb', 'cc'] + test1 +------- + 3 +(1 row) + +CREATE FUNCTION test1n(val ltree) RETURNS int +LANGUAGE plpython2u +TRANSFORM FOR TYPE ltree +AS $$ +plpy.info(repr(val)) +return len(val) +$$; +SELECT test1n('aa.bb.cc'::ltree); +INFO: ['aa', 'bb', 'cc'] + test1n +-------- + 3 +(1 row) + +CREATE FUNCTION test2() RETURNS ltree +LANGUAGE plpythonu +TRANSFORM FOR TYPE ltree +AS $$ +return ['foo', 'bar', 'baz'] +$$; +-- plpython to ltree is not yet implemented, so this will fail, +-- because it will try to parse the Python list as an ltree input +-- string. +SELECT test2(); +ERROR: ltree syntax error at character 1 +CONTEXT: while creating return value +PL/Python function "test2" diff --git a/contrib/ltree_plpython/ltree_plpython.c b/contrib/ltree_plpython/ltree_plpython.c new file mode 100644 index 0000000..1570e77 --- /dev/null +++ b/contrib/ltree_plpython/ltree_plpython.c @@ -0,0 +1,64 @@ +#include "postgres.h" + +#include "fmgr.h" +#include "ltree/ltree.h" +#include "plpython.h" + +PG_MODULE_MAGIC; + +extern void _PG_init(void); + +/* Linkage to functions in plpython module */ +#if PY_MAJOR_VERSION >= 3 +typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size); +static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p; +#endif + + +/* + * Module initialize function: fetch function pointers for cross-module calls. + */ +void +_PG_init(void) +{ + /* Asserts verify that typedefs above match original declarations */ +#if PY_MAJOR_VERSION >= 3 + AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t); + PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t) + load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize", + true, NULL); +#endif +} + + +/* These defines must be after the module init function */ +#define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p + + +PG_FUNCTION_INFO_V1(ltree_to_plpython); + +Datum +ltree_to_plpython(PG_FUNCTION_ARGS) +{ + ltree *in = PG_GETARG_LTREE_P(0); + int i; + PyObject *list; + ltree_level *curlevel; + + list = PyList_New(in->numlevel); + if (!list) + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + + curlevel = LTREE_FIRST(in); + for (i = 0; i < in->numlevel; i++) + { + PyList_SetItem(list, i, PyString_FromStringAndSize(curlevel->name, curlevel->len)); + curlevel = LEVEL_NEXT(curlevel); + } + + PG_FREE_IF_COPY(in, 0); + + return PointerGetDatum(list); +} diff --git a/contrib/ltree_plpython/ltree_plpython2u--1.0.sql b/contrib/ltree_plpython/ltree_plpython2u--1.0.sql new file mode 100644 index 0000000..5c4a703 --- /dev/null +++ b/contrib/ltree_plpython/ltree_plpython2u--1.0.sql @@ -0,0 +1,12 @@ +/* contrib/ltree_plpython/ltree_plpython2u--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION ltree_plpython2u" to load this file. \quit + +CREATE FUNCTION ltree_to_plpython2(val internal) RETURNS internal +LANGUAGE C STRICT IMMUTABLE +AS 'MODULE_PATHNAME', 'ltree_to_plpython'; + +CREATE TRANSFORM FOR ltree LANGUAGE plpython2u ( + FROM SQL WITH FUNCTION ltree_to_plpython2(internal) +); diff --git a/contrib/ltree_plpython/ltree_plpython2u.control b/contrib/ltree_plpython/ltree_plpython2u.control new file mode 100644 index 0000000..bedfd0a --- /dev/null +++ b/contrib/ltree_plpython/ltree_plpython2u.control @@ -0,0 +1,6 @@ +# ltree_plpython2u extension +comment = 'transform between ltree and plpython2u' +default_version = '1.0' +module_pathname = '$libdir/ltree_plpython2' +relocatable = true +requires = 'ltree,plpython2u' diff --git a/contrib/ltree_plpython/ltree_plpython3u--1.0.sql b/contrib/ltree_plpython/ltree_plpython3u--1.0.sql new file mode 100644 index 0000000..09ada3c --- /dev/null +++ b/contrib/ltree_plpython/ltree_plpython3u--1.0.sql @@ -0,0 +1,12 @@ +/* contrib/ltree_plpython/ltree_plpython3u--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION ltree_plpython3u" to load this file. \quit + +CREATE FUNCTION ltree_to_plpython3(val internal) RETURNS internal +LANGUAGE C STRICT IMMUTABLE +AS 'MODULE_PATHNAME', 'ltree_to_plpython'; + +CREATE TRANSFORM FOR ltree LANGUAGE plpython3u ( + FROM SQL WITH FUNCTION ltree_to_plpython3(internal) +); diff --git a/contrib/ltree_plpython/ltree_plpython3u.control b/contrib/ltree_plpython/ltree_plpython3u.control new file mode 100644 index 0000000..96c9764 --- /dev/null +++ b/contrib/ltree_plpython/ltree_plpython3u.control @@ -0,0 +1,6 @@ +# ltree_plpython3u extension +comment = 'transform between ltree and plpython3u' +default_version = '1.0' +module_pathname = '$libdir/ltree_plpython3' +relocatable = true +requires = 'ltree,plpython3u' diff --git a/contrib/ltree_plpython/ltree_plpythonu--1.0.sql b/contrib/ltree_plpython/ltree_plpythonu--1.0.sql new file mode 100644 index 0000000..ee93edf --- /dev/null +++ b/contrib/ltree_plpython/ltree_plpythonu--1.0.sql @@ -0,0 +1,12 @@ +/* contrib/ltree_plpython/ltree_plpythonu--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION ltree_plpythonu" to load this file. \quit + +CREATE FUNCTION ltree_to_plpython(val internal) RETURNS internal +LANGUAGE C STRICT IMMUTABLE +AS 'MODULE_PATHNAME'; + +CREATE TRANSFORM FOR ltree LANGUAGE plpythonu ( + FROM SQL WITH FUNCTION ltree_to_plpython(internal) +); diff --git a/contrib/ltree_plpython/ltree_plpythonu.control b/contrib/ltree_plpython/ltree_plpythonu.control new file mode 100644 index 0000000..b03c89a --- /dev/null +++ b/contrib/ltree_plpython/ltree_plpythonu.control @@ -0,0 +1,6 @@ +# ltree_plpythonu extension +comment = 'transform between ltree and plpythonu' +default_version = '1.0' +module_pathname = '$libdir/ltree_plpython2' +relocatable = true +requires = 'ltree,plpythonu' diff --git a/contrib/ltree_plpython/sql/ltree_plpython.sql b/contrib/ltree_plpython/sql/ltree_plpython.sql new file mode 100644 index 0000000..210f542 --- /dev/null +++ b/contrib/ltree_plpython/sql/ltree_plpython.sql @@ -0,0 +1,36 @@ +CREATE EXTENSION ltree_plpython2u CASCADE; + + +CREATE FUNCTION test1(val ltree) RETURNS int +LANGUAGE plpythonu +TRANSFORM FOR TYPE ltree +AS $$ +plpy.info(repr(val)) +return len(val) +$$; + +SELECT test1('aa.bb.cc'::ltree); + + +CREATE FUNCTION test1n(val ltree) RETURNS int +LANGUAGE plpython2u +TRANSFORM FOR TYPE ltree +AS $$ +plpy.info(repr(val)) +return len(val) +$$; + +SELECT test1n('aa.bb.cc'::ltree); + + +CREATE FUNCTION test2() RETURNS ltree +LANGUAGE plpythonu +TRANSFORM FOR TYPE ltree +AS $$ +return ['foo', 'bar', 'baz'] +$$; + +-- plpython to ltree is not yet implemented, so this will fail, +-- because it will try to parse the Python list as an ltree input +-- string. +SELECT test2(); |