mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-15 08:20:16 +08:00
687f096ea9
Fix PL/Python so that it can handle domains over composite, and so that
it enforces domain constraints correctly in other cases that were not
always done properly before. Notably, it didn't do arrays of domains
right (oversight in commit c12d570fa
), and it failed to enforce domain
constraints when returning a composite type containing a domain field,
and if a transform function is being used for a domain's base type then
it failed to enforce domain constraints on the result. Also, in many
places it missed checking domain constraints on null values, because
the plpy_typeio code simply wasn't called for Py_None.
Rather than try to band-aid these problems, I made a significant
refactoring of the plpy_typeio logic. The existing design of recursing
for array and composite members is extended to also treat domains as
containers requiring recursion, and the APIs for the module are cleaned
up and simplified.
The patch also modifies plpy_typeio to rely on the typcache more than
it did before (which was pretty much not at all). This reduces the
need for repetitive lookups, and lets us get rid of an ad-hoc scheme
for detecting changes in composite types. I added a couple of small
features to typcache to help with that.
Although some of this is fixing bugs that long predate v11, I don't
think we should risk a back-patch: it's a significant amount of code
churn, and there've been no complaints from the field about the bugs.
Tom Lane, reviewed by Anthony Bykov
Discussion: https://postgr.es/m/24449.1509393613@sss.pgh.pa.us
141 lines
3.4 KiB
Plaintext
141 lines
3.4 KiB
Plaintext
CREATE EXTENSION hstore_plpython2u CASCADE;
|
|
NOTICE: installing required extension "plpython2u"
|
|
-- test hstore -> python
|
|
CREATE FUNCTION test1(val hstore) RETURNS int
|
|
LANGUAGE plpythonu
|
|
TRANSFORM FOR TYPE hstore
|
|
AS $$
|
|
assert isinstance(val, dict)
|
|
plpy.info(sorted(val.items()))
|
|
return len(val)
|
|
$$;
|
|
SELECT test1('aa=>bb, cc=>NULL'::hstore);
|
|
INFO: [('aa', 'bb'), ('cc', None)]
|
|
test1
|
|
-------
|
|
2
|
|
(1 row)
|
|
|
|
-- the same with the versioned language name
|
|
CREATE FUNCTION test1n(val hstore) RETURNS int
|
|
LANGUAGE plpython2u
|
|
TRANSFORM FOR TYPE hstore
|
|
AS $$
|
|
assert isinstance(val, dict)
|
|
plpy.info(sorted(val.items()))
|
|
return len(val)
|
|
$$;
|
|
SELECT test1n('aa=>bb, cc=>NULL'::hstore);
|
|
INFO: [('aa', 'bb'), ('cc', None)]
|
|
test1n
|
|
--------
|
|
2
|
|
(1 row)
|
|
|
|
-- test hstore[] -> python
|
|
CREATE FUNCTION test1arr(val hstore[]) RETURNS int
|
|
LANGUAGE plpythonu
|
|
TRANSFORM FOR TYPE hstore
|
|
AS $$
|
|
assert(val == [{'aa': 'bb', 'cc': None}, {'dd': 'ee'}])
|
|
return len(val)
|
|
$$;
|
|
SELECT test1arr(array['aa=>bb, cc=>NULL'::hstore, 'dd=>ee']);
|
|
test1arr
|
|
----------
|
|
2
|
|
(1 row)
|
|
|
|
-- test python -> hstore
|
|
CREATE FUNCTION test2() RETURNS hstore
|
|
LANGUAGE plpythonu
|
|
TRANSFORM FOR TYPE hstore
|
|
AS $$
|
|
val = {'a': 1, 'b': 'boo', 'c': None}
|
|
return val
|
|
$$;
|
|
SELECT test2();
|
|
test2
|
|
---------------------------------
|
|
"a"=>"1", "b"=>"boo", "c"=>NULL
|
|
(1 row)
|
|
|
|
-- test python -> hstore[]
|
|
CREATE FUNCTION test2arr() RETURNS hstore[]
|
|
LANGUAGE plpythonu
|
|
TRANSFORM FOR TYPE hstore
|
|
AS $$
|
|
val = [{'a': 1, 'b': 'boo', 'c': None}, {'d': 2}]
|
|
return val
|
|
$$;
|
|
SELECT test2arr();
|
|
test2arr
|
|
--------------------------------------------------------------
|
|
{"\"a\"=>\"1\", \"b\"=>\"boo\", \"c\"=>NULL","\"d\"=>\"2\""}
|
|
(1 row)
|
|
|
|
-- test python -> domain over hstore
|
|
CREATE DOMAIN hstore_foo AS hstore CHECK(VALUE ? 'foo');
|
|
CREATE FUNCTION test2dom(fn text) RETURNS hstore_foo
|
|
LANGUAGE plpythonu
|
|
TRANSFORM FOR TYPE hstore
|
|
AS $$
|
|
return {'a': 1, fn: 'boo', 'c': None}
|
|
$$;
|
|
SELECT test2dom('foo');
|
|
test2dom
|
|
-----------------------------------
|
|
"a"=>"1", "c"=>NULL, "foo"=>"boo"
|
|
(1 row)
|
|
|
|
SELECT test2dom('bar'); -- fail
|
|
ERROR: value for domain hstore_foo violates check constraint "hstore_foo_check"
|
|
CONTEXT: while creating return value
|
|
PL/Python function "test2dom"
|
|
-- test as part of prepare/execute
|
|
CREATE FUNCTION test3() RETURNS void
|
|
LANGUAGE plpythonu
|
|
TRANSFORM FOR TYPE hstore
|
|
AS $$
|
|
rv = plpy.execute("SELECT 'aa=>bb, cc=>NULL'::hstore AS col1")
|
|
assert(rv[0]["col1"] == {'aa': 'bb', 'cc': None})
|
|
|
|
val = {'a': 1, 'b': 'boo', 'c': None}
|
|
plan = plpy.prepare("SELECT $1::text AS col1", ["hstore"])
|
|
rv = plpy.execute(plan, [val])
|
|
assert(rv[0]["col1"] == '"a"=>"1", "b"=>"boo", "c"=>NULL')
|
|
$$;
|
|
SELECT test3();
|
|
test3
|
|
-------
|
|
|
|
(1 row)
|
|
|
|
-- test trigger
|
|
CREATE TABLE test1 (a int, b hstore);
|
|
INSERT INTO test1 VALUES (1, 'aa=>bb, cc=>NULL');
|
|
SELECT * FROM test1;
|
|
a | b
|
|
---+------------------------
|
|
1 | "aa"=>"bb", "cc"=>NULL
|
|
(1 row)
|
|
|
|
CREATE FUNCTION test4() RETURNS trigger
|
|
LANGUAGE plpythonu
|
|
TRANSFORM FOR TYPE hstore
|
|
AS $$
|
|
assert(TD["new"] == {'a': 1, 'b': {'aa': 'bb', 'cc': None}})
|
|
if TD["new"]["a"] == 1:
|
|
TD["new"]["b"] = {'a': 1, 'b': 'boo', 'c': None}
|
|
|
|
return "MODIFY"
|
|
$$;
|
|
CREATE TRIGGER test4 BEFORE UPDATE ON test1 FOR EACH ROW EXECUTE PROCEDURE test4();
|
|
UPDATE test1 SET a = a;
|
|
SELECT * FROM test1;
|
|
a | b
|
|
---+---------------------------------
|
|
1 | "a"=>"1", "b"=>"boo", "c"=>NULL
|
|
(1 row)
|
|
|