Debian Codegraph @

contrib/hstore_plpython/expected/hstore_plpython.out

raw
1 CREATE EXTENSION hstore_plpython3u CASCADE;
2 NOTICE: installing required extension "plpython3u"
3 -- test hstore -> python
4 CREATE FUNCTION test1(val hstore) RETURNS int
5 LANGUAGE plpython3u
6 TRANSFORM FOR TYPE hstore
7 AS $$
8 assert isinstance(val, dict)
9 plpy.info(sorted(val.items()))
10 return len(val)
11 $$;
12 SELECT test1('aa=>bb, cc=>NULL'::hstore);
13 INFO: [('aa', 'bb'), ('cc', None)]
14 test1
15 -------
16 2
17 (1 row)
18
19 -- the same with the versioned language name
20 CREATE FUNCTION test1n(val hstore) RETURNS int
21 LANGUAGE plpython3u
22 TRANSFORM FOR TYPE hstore
23 AS $$
24 assert isinstance(val, dict)
25 plpy.info(sorted(val.items()))
26 return len(val)
27 $$;
28 SELECT test1n('aa=>bb, cc=>NULL'::hstore);
29 INFO: [('aa', 'bb'), ('cc', None)]
30 test1n
31 --------
32 2
33 (1 row)
34
35 -- test that a non-mapping result is correctly rejected
36 CREATE FUNCTION test1bad() RETURNS hstore
37 LANGUAGE plpython3u
38 TRANSFORM FOR TYPE hstore
39 AS $$
40 return "foo"
41 $$;
42 SELECT test1bad();
43 ERROR: not a Python mapping
44 CONTEXT: while creating return value
45 PL/Python function "test1bad"
46 -- A mapping whose items() raises should be reported as an error, not crash
47 -- the backend
48 CREATE FUNCTION test1broken() RETURNS hstore
49 LANGUAGE plpython3u
50 TRANSFORM FOR TYPE hstore
51 AS $$
52 class C(dict):
53 def items(self):
54 raise ValueError('items failed')
55 d = C()
56 d['x'] = 1
57 return d
58 $$;
59 SELECT test1broken();
60 ERROR: could not get items from Python mapping
61 CONTEXT: while creating return value
62 PL/Python function "test1broken"
63 -- Likewise for a mapping whose items() does not return key/value pairs
64 CREATE FUNCTION test1malformed() RETURNS hstore
65 LANGUAGE plpython3u
66 TRANSFORM FOR TYPE hstore
67 AS $$
68 class C(dict):
69 def items(self):
70 return [42]
71 d = C()
72 d['x'] = 1
73 return d
74 $$;
75 SELECT test1malformed();
76 ERROR: items() of a Python mapping must return key/value pairs
77 CONTEXT: while creating return value
78 PL/Python function "test1malformed"
79 -- Likewise for a mapping whose items() yields fewer pairs than its length
80 CREATE FUNCTION test1short() RETURNS hstore
81 LANGUAGE plpython3u
82 TRANSFORM FOR TYPE hstore
83 AS $$
84 class C(dict):
85 def items(self):
86 return []
87 d = C()
88 d['x'] = 1
89 return d
90 $$;
91 SELECT test1short();
92 ERROR: items() of a Python mapping must return key/value pairs
93 CONTEXT: while creating return value
94 PL/Python function "test1short"
95 -- Likewise for a mapping whose __len__() raises
96 CREATE FUNCTION test1brokenlen() RETURNS hstore
97 LANGUAGE plpython3u
98 TRANSFORM FOR TYPE hstore
99 AS $$
100 class C(dict):
101 def __len__(self):
102 raise ValueError('len failed')
103 d = C()
104 d['x'] = 1
105 return d
106 $$;
107 SELECT test1brokenlen();
108 ERROR: could not get size of Python mapping
109 CONTEXT: while creating return value
110 PL/Python function "test1brokenlen"
111 -- test hstore[] -> python
112 CREATE FUNCTION test1arr(val hstore[]) RETURNS int
113 LANGUAGE plpython3u
114 TRANSFORM FOR TYPE hstore
115 AS $$
116 assert(val == [{'aa': 'bb', 'cc': None}, {'dd': 'ee'}])
117 return len(val)
118 $$;
119 SELECT test1arr(array['aa=>bb, cc=>NULL'::hstore, 'dd=>ee']);
120 test1arr
121 ----------
122 2
123 (1 row)
124
125 -- test python -> hstore
126 CREATE FUNCTION test2(a int, b text) RETURNS hstore
127 LANGUAGE plpython3u
128 TRANSFORM FOR TYPE hstore
129 AS $$
130 val = {'a': a, 'b': b, 'c': None}
131 return val
132 $$;
133 SELECT test2(1, 'boo');
134 test2
135 ---------------------------------
136 "a"=>"1", "b"=>"boo", "c"=>NULL
137 (1 row)
138
139 --- test ruleutils
140 \sf test2
141 CREATE OR REPLACE FUNCTION public.test2(a integer, b text)
142 RETURNS hstore
143 TRANSFORM FOR TYPE hstore
144 LANGUAGE plpython3u
145 AS $function$
146 val = {'a': a, 'b': b, 'c': None}
147 return val
148 $function$
149 -- test python -> hstore[]
150 CREATE FUNCTION test2arr() RETURNS hstore[]
151 LANGUAGE plpython3u
152 TRANSFORM FOR TYPE hstore
153 AS $$
154 val = [{'a': 1, 'b': 'boo', 'c': None}, {'d': 2}]
155 return val
156 $$;
157 SELECT test2arr();
158 test2arr
159 --------------------------------------------------------------
160 {"\"a\"=>\"1\", \"b\"=>\"boo\", \"c\"=>NULL","\"d\"=>\"2\""}
161 (1 row)
162
163 -- test python -> domain over hstore
164 CREATE DOMAIN hstore_foo AS hstore CHECK(VALUE ? 'foo');
165 CREATE FUNCTION test2dom(fn text) RETURNS hstore_foo
166 LANGUAGE plpython3u
167 TRANSFORM FOR TYPE hstore
168 AS $$
169 return {'a': 1, fn: 'boo', 'c': None}
170 $$;
171 SELECT test2dom('foo');
172 test2dom
173 -----------------------------------
174 "a"=>"1", "c"=>NULL, "foo"=>"boo"
175 (1 row)
176
177 SELECT test2dom('bar'); -- fail
178 ERROR: value for domain hstore_foo violates check constraint "hstore_foo_check"
179 CONTEXT: while creating return value
180 PL/Python function "test2dom"
181 -- test as part of prepare/execute
182 CREATE FUNCTION test3() RETURNS void
183 LANGUAGE plpython3u
184 TRANSFORM FOR TYPE hstore
185 AS $$
186 rv = plpy.execute("SELECT 'aa=>bb, cc=>NULL'::hstore AS col1")
187 assert(rv[0]["col1"] == {'aa': 'bb', 'cc': None})
188
189 val = {'a': 1, 'b': 'boo', 'c': None}
190 plan = plpy.prepare("SELECT $1::text AS col1", ["hstore"])
191 rv = plpy.execute(plan, [val])
192 assert(rv[0]["col1"] == '"a"=>"1", "b"=>"boo", "c"=>NULL')
193 $$;
194 SELECT test3();
195 test3
196 -------
197
198 (1 row)
199
200 -- test trigger
201 CREATE TABLE test1 (a int, b hstore);
202 INSERT INTO test1 VALUES (1, 'aa=>bb, cc=>NULL');
203 SELECT * FROM test1;
204 a | b
205 ---+------------------------
206 1 | "aa"=>"bb", "cc"=>NULL
207 (1 row)
208
209 CREATE FUNCTION test4() RETURNS trigger
210 LANGUAGE plpython3u
211 TRANSFORM FOR TYPE hstore
212 AS $$
213 assert(TD["new"] == {'a': 1, 'b': {'aa': 'bb', 'cc': None}})
214 if TD["new"]["a"] == 1:
215 TD["new"]["b"] = {'a': 1, 'b': 'boo', 'c': None}
216
217 return "MODIFY"
218 $$;
219 CREATE TRIGGER test4 BEFORE UPDATE ON test1 FOR EACH ROW EXECUTE PROCEDURE test4();
220 UPDATE test1 SET a = a;
221 SELECT * FROM test1;
222 a | b
223 ---+---------------------------------
224 1 | "a"=>"1", "b"=>"boo", "c"=>NULL
225 (1 row)
226