-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomposite.sql
More file actions
228 lines (191 loc) · 7.78 KB
/
Copy pathcomposite.sql
File metadata and controls
228 lines (191 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
-- Test the composite Python interfaces.
--
DROP TYPE IF EXISTS ctyp CASCADE;
CREATE TYPE ctyp AS (
i int,
b bigint,
t text,
n numeric,
c "char"
);
DROP TABLE IF EXISTS reltyp CASCADE;
CREATE TABLE reltyp (
i int,
b bigint,
t text,
n numeric,
c "char"
);
-- Yes, it fails to create a domain.
-- Leave this here, so if that ever changes, we'll know more
-- tests will be needed.
DROP DOMAIN IF EXISTS dtyp CASCADE;
CREATE DOMAIN dtyp AS ctyp;
CREATE OR REPLACE FUNCTION check_composite_access(arg ctyp) RETURNS text LANGUAGE python AS
$python$
import Postgres
def main(a):
assert [a[name] for name in type(a).descriptor.column_names] == [a[i] for i in range(len(a))]
t = tuple(a)
assert t == tuple([a[i] for i in range(len(a))])
l = list(t)
l.reverse()
assert l == [a[i] for i in range(-1, -(len(t)+1), -1)]
i, b, t, n, c = a
return 'success'
$python$;
SELECT check_composite_access(ROW(1,2,'t',3.1,'x')::ctyp);
CREATE OR REPLACE FUNCTION check_composite_access(arg reltyp) RETURNS text LANGUAGE python AS
$python$
def main(a):
assert [a[name] for name in type(a).descriptor.column_names] == [a[i] for i in range(len(a))]
t = tuple(a)
assert t == tuple([a[i] for i in range(len(a))])
i, b, t, n, c = a
return 'success'
$python$;
SELECT check_composite_access(ROW(1,2,'t',3.1,'x')::reltyp);
ALTER TABLE reltyp DROP COLUMN n;
ALTER TABLE reltyp DROP COLUMN c;
SELECT check_composite_access(ROW(1,2,'t')::reltyp);
ALTER TABLE reltyp ADD COLUMN n numeric;
ALTER TABLE reltyp ADD COLUMN c "char";
SELECT check_composite_access(ROW(1,2,'t',3.1,'x')::reltyp);
CREATE OR REPLACE FUNCTION check_composite_slicing(arg reltyp) RETURNS text LANGUAGE python AS
$python$
def main(a):
n_index = type(a).descriptor.column_names.index("n")
assert a[0:"n"] == a["i":n_index] == a[0:n_index]
assert a[0:"n":2] == a["i":n_index:2] == a[0:n_index:2]
assert a["n":0:-1] == a[n_index:"i":-1] == a[n_index:0:-1]
return 'success'
$python$;
SELECT check_composite_slicing(ROW(1,2,'t',3.1,'x')::reltyp);
CREATE OR REPLACE FUNCTION slice_it(arg ctyp, s_start int, s_end int, s_step int) RETURNS text LANGUAGE python AS
$python$
def main(a, start, end, step):
s = a[start:end:step]
return str([str(x) for x in s])
$python$;
--
-- slicing is plagued with potential off-by-one errors, so go nuts here.
--
-- can't slice with zero step(throws error)
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, 2, 0);
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, 2, 1), '1,2' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 1, 3, 1), '2,t' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 2, 0, -1), 't,2' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, -1, NULL, -1), 'x,3.1,t,2,1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, NULL, NULL, -1), 'x,3.1,t,2,1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, NULL, NULL, -2), 'x,t,1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, NULL, NULL, 2), '1,t,x' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, NULL, NULL, -3), 'x,2' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, NULL, NULL, 3), '1,3.1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, NULL, NULL, 1), '1,2,t,3.1,x' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, NULL, 1), '1,2,t,3.1,x' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, -1, 1), '1,2,t,3.1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, -2, 1), '1,2,t' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, -3, 1), '1,2' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, -4, 1), '1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, -5, 1), '[]' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, -6, 1), '[]' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 4, 4, -1), '[]' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 4, 5, -1), '[]' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, -1, 0, -2), 'x,t' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, -1, 2), '1,t' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, 1, 2), '1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, 1, 234324234), '1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, NULL, -234324234), '1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, 4, 2), '1,t' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 0, 0, 234324234), '[]' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 1203282, -213123, 234324234), '[]' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, -1203283, 213122, 234324234), '1' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, -1203284, 213121, -234324234), '[]' AS answer;
SELECT slice_it(ROW(1,2,'t',3.1,'x')::ctyp, 1203288, -213120, -234324234), 'x' AS answer;
--
-- checks that None is ignored, and keywords override
--
CREATE OR REPLACE FUNCTION check_transform(OUT i int, OUT i2 int, OUT f float) RETURNS SETOF record LANGUAGE python AS
$python$
def main():
for x in prepare('select i::int, i::int AS i2, i::float AS f FROM generate_series(1,300) AS g(i)'):
yield x.transform(None, lambda x: x*2, lambda y: y, f = lambda x: x / 2)
$python$;
SELECT * FROM check_transform() LIMIT 10;
-- kw only
CREATE OR REPLACE FUNCTION check_transform_kw(OUT i int, OUT i2 int, OUT f float) RETURNS SETOF record LANGUAGE python AS
$python$
def main():
for x in prepare('select i::int, i::int AS i2, i::float AS f FROM generate_series(1,300) AS g(i)'):
yield x.transform(i2 = lambda x: x * 2)
$python$;
SELECT * FROM check_transform_kw() LIMIT 10;
-- mixed positional and kw
CREATE OR REPLACE FUNCTION check_replace(OUT i int, OUT i2 int, OUT f float) RETURNS SETOF record LANGUAGE python AS
$python$
def main():
for x in prepare('select i::int, i::int AS i2, i::float AS f FROM generate_series(1,300) AS g(i)'):
yield x.replace(None, 0, f = 1.2)
$python$;
SELECT * FROM check_replace() LIMIT 10;
-- kw only
CREATE OR REPLACE FUNCTION check_replace_kw(OUT i int, OUT i2 int, OUT f float) RETURNS SETOF record LANGUAGE python AS
$python$
def main():
for x in prepare('select i::int, i::int AS i2, i::float AS f FROM generate_series(1,300) AS g(i)'):
yield x.replace(i2 = 0)
$python$;
SELECT * FROM check_replace_kw() LIMIT 10;
-- Other record APIs
CREATE OR REPLACE FUNCTION check_record_map_apis() RETURNS text LANGUAGE python AS
$python$
import Postgres
from Postgres.types import int4, int8, text, numeric, regtype
ctyp = Postgres.Type(regtype('ctyp'))
char = Postgres.Type(Postgres.CONST['CHAROID'])
# i int,
# b bigint,
# t text,
# n numeric,
# c "char"
def main():
x = ctyp((100,10000,'text',1.505,'x'))
assert x.column_names == ('i','b','t','n','c')
assert x.keys() is x.column_names
assert x.values() is x
assert list(x.items()) == list(zip(x.keys(), x.values()))
assert x.column_types == (
int4, int8, text, numeric, char
)
assert x.pg_column_types == (
int4.oid, int8.oid, text.oid, numeric.oid, char.oid
)
return 'success'
$python$;
SELECT check_record_map_apis();
-- Find out what happens when we compare two distinct revisions of the same type
DROP TABLE IF EXISTS compared_revisions;
CREATE TABLE compared_revisions (i int, t text);
CREATE OR REPLACE FUNCTION compare_different_same_type() RETURNS BOOLEAN LANGUAGE python AS
$python$
from Postgres import WARNING
from Postgres import Type
from Postgres.types import regtype
original = Type(regtype('compared_revisions'))
o_x = original((100, 'some text'))
def main():
# grab the latest version
revision = Type(regtype('compared_revisions'))
colnames = revision.column_names
WARNING(str(colnames))
if len(colnames) > 2:
r_x = revision({'i' : 100, 't' : 'some text', 'n' : '200000'})
# exercise it in the face of a new version..
new_o_x = original((321, 'eek'))
else:
r_x = revision({'i' : 100, 't' : 'some text'})
return r_x == o_x
$python$;
SELECT compare_different_same_type();
ALTER TABLE compared_revisions ADD COLUMN n numeric;
SELECT compare_different_same_type();