Skip to content

Commit 231064a

Browse files
committed
plpython: Add test for returning Python set from SETOF function
This is claimed in the documentation but there was a no test case for it. Reported-by: Bogdan Grigorenko <gri.bogdan.2020@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/173543330569.680.6706329879058172623%40wrigleys.postgresql.org
1 parent d1d8382 commit 231064a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/pl/plpython/expected/plpython_setof.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ for i in range(count):
1717
t += ( content, )
1818
return t
1919
$$ LANGUAGE plpython3u;
20+
CREATE FUNCTION test_setof_as_set(count integer, content text) RETURNS SETOF text AS $$
21+
s = set()
22+
for i in range(count):
23+
s.add(content * (i + 1) if content is not None else None)
24+
return s
25+
$$ LANGUAGE plpython3u;
2026
CREATE FUNCTION test_setof_as_iterator(count integer, content text) RETURNS SETOF text AS $$
2127
class producer:
2228
def __init__ (self, icount, icontent):
@@ -90,6 +96,30 @@ SELECT test_setof_as_tuple(2, null);
9096

9197
(2 rows)
9298

99+
SELECT * FROM test_setof_as_set(0, 'set') ORDER BY 1;
100+
test_setof_as_set
101+
-------------------
102+
(0 rows)
103+
104+
SELECT * FROM test_setof_as_set(1, 'set') ORDER BY 1;
105+
test_setof_as_set
106+
-------------------
107+
set
108+
(1 row)
109+
110+
SELECT * FROM test_setof_as_set(2, 'set') ORDER BY 1;
111+
test_setof_as_set
112+
-------------------
113+
set
114+
setset
115+
(2 rows)
116+
117+
SELECT * FROM test_setof_as_set(2, null) ORDER BY 1;
118+
test_setof_as_set
119+
-------------------
120+
121+
(1 row)
122+
93123
SELECT test_setof_as_iterator(0, 'list');
94124
test_setof_as_iterator
95125
------------------------

src/pl/plpython/sql/plpython_setof.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ for i in range(count):
2020
return t
2121
$$ LANGUAGE plpython3u;
2222

23+
CREATE FUNCTION test_setof_as_set(count integer, content text) RETURNS SETOF text AS $$
24+
s = set()
25+
for i in range(count):
26+
s.add(content * (i + 1) if content is not None else None)
27+
return s
28+
$$ LANGUAGE plpython3u;
29+
2330
CREATE FUNCTION test_setof_as_iterator(count integer, content text) RETURNS SETOF text AS $$
2431
class producer:
2532
def __init__ (self, icount, icontent):
@@ -56,6 +63,11 @@ SELECT test_setof_as_tuple(1, 'tuple');
5663
SELECT test_setof_as_tuple(2, 'tuple');
5764
SELECT test_setof_as_tuple(2, null);
5865

66+
SELECT * FROM test_setof_as_set(0, 'set') ORDER BY 1;
67+
SELECT * FROM test_setof_as_set(1, 'set') ORDER BY 1;
68+
SELECT * FROM test_setof_as_set(2, 'set') ORDER BY 1;
69+
SELECT * FROM test_setof_as_set(2, null) ORDER BY 1;
70+
5971
SELECT test_setof_as_iterator(0, 'list');
6072
SELECT test_setof_as_iterator(1, 'list');
6173
SELECT test_setof_as_iterator(2, 'list');

0 commit comments

Comments
 (0)