3 -- WARNING: int4 operators never check for over/underflow!
4 -- Some of these answers are consequently numerically incorrect.
6 CREATE TABLE INT4_TBL(f1 int4);
7 INSERT INTO INT4_TBL(f1) VALUES (' 0 ');
8 INSERT INTO INT4_TBL(f1) VALUES ('123456 ');
9 INSERT INTO INT4_TBL(f1) VALUES (' -123456');
10 INSERT INTO INT4_TBL(f1) VALUES ('34.5');
11 ERROR: invalid input syntax for integer: "34.5"
12 LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('34.5');
14 -- largest and smallest values
15 INSERT INTO INT4_TBL(f1) VALUES ('2147483647');
16 INSERT INTO INT4_TBL(f1) VALUES ('-2147483647');
17 -- bad input values -- should give errors
18 INSERT INTO INT4_TBL(f1) VALUES ('1000000000000');
19 ERROR: value "1000000000000" is out of range for type integer
20 LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('1000000000000');
22 INSERT INTO INT4_TBL(f1) VALUES ('asdf');
23 ERROR: invalid input syntax for integer: "asdf"
24 LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('asdf');
26 INSERT INTO INT4_TBL(f1) VALUES (' ');
27 ERROR: invalid input syntax for integer: " "
28 LINE 1: INSERT INTO INT4_TBL(f1) VALUES (' ');
30 INSERT INTO INT4_TBL(f1) VALUES (' asdf ');
31 ERROR: invalid input syntax for integer: " asdf "
32 LINE 1: INSERT INTO INT4_TBL(f1) VALUES (' asdf ');
34 INSERT INTO INT4_TBL(f1) VALUES ('- 1234');
35 ERROR: invalid input syntax for integer: "- 1234"
36 LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('- 1234');
38 INSERT INTO INT4_TBL(f1) VALUES ('123 5');
39 ERROR: invalid input syntax for integer: "123 5"
40 LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('123 5');
42 INSERT INTO INT4_TBL(f1) VALUES ('');
43 ERROR: invalid input syntax for integer: ""
44 LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('');
46 SELECT '' AS five, * FROM INT4_TBL;
56 SELECT '' AS four, i.* FROM INT4_TBL i WHERE i.f1 <> int2 '0';
65 SELECT '' AS four, i.* FROM INT4_TBL i WHERE i.f1 <> int4 '0';
74 SELECT '' AS one, i.* FROM INT4_TBL i WHERE i.f1 = int2 '0';
80 SELECT '' AS one, i.* FROM INT4_TBL i WHERE i.f1 = int4 '0';
86 SELECT '' AS two, i.* FROM INT4_TBL i WHERE i.f1 < int2 '0';
93 SELECT '' AS two, i.* FROM INT4_TBL i WHERE i.f1 < int4 '0';
100 SELECT '' AS three, i.* FROM INT4_TBL i WHERE i.f1 <= int2 '0';
102 -------+-------------
108 SELECT '' AS three, i.* FROM INT4_TBL i WHERE i.f1 <= int4 '0';
110 -------+-------------
116 SELECT '' AS two, i.* FROM INT4_TBL i WHERE i.f1 > int2 '0';
123 SELECT '' AS two, i.* FROM INT4_TBL i WHERE i.f1 > int4 '0';
130 SELECT '' AS three, i.* FROM INT4_TBL i WHERE i.f1 >= int2 '0';
138 SELECT '' AS three, i.* FROM INT4_TBL i WHERE i.f1 >= int4 '0';
147 SELECT '' AS one, i.* FROM INT4_TBL i WHERE (i.f1 % int2 '2') = int2 '1';
154 SELECT '' AS three, i.* FROM INT4_TBL i WHERE (i.f1 % int4 '2') = int2 '0';
162 SELECT '' AS five, i.f1, i.f1 * int2 '2' AS x FROM INT4_TBL i;
163 ERROR: integer out of range
164 SELECT '' AS five, i.f1, i.f1 * int2 '2' AS x FROM INT4_TBL i
165 WHERE abs(f1) < 1073741824;
167 ------+---------+---------
173 SELECT '' AS five, i.f1, i.f1 * int4 '2' AS x FROM INT4_TBL i;
174 ERROR: integer out of range
175 SELECT '' AS five, i.f1, i.f1 * int4 '2' AS x FROM INT4_TBL i
176 WHERE abs(f1) < 1073741824;
178 ------+---------+---------
184 SELECT '' AS five, i.f1, i.f1 + int2 '2' AS x FROM INT4_TBL i;
185 ERROR: integer out of range
186 SELECT '' AS five, i.f1, i.f1 + int2 '2' AS x FROM INT4_TBL i
187 WHERE f1 < 2147483646;
189 ------+-------------+-------------
193 | -2147483647 | -2147483645
196 SELECT '' AS five, i.f1, i.f1 + int4 '2' AS x FROM INT4_TBL i;
197 ERROR: integer out of range
198 SELECT '' AS five, i.f1, i.f1 + int4 '2' AS x FROM INT4_TBL i
199 WHERE f1 < 2147483646;
201 ------+-------------+-------------
205 | -2147483647 | -2147483645
208 SELECT '' AS five, i.f1, i.f1 - int2 '2' AS x FROM INT4_TBL i;
209 ERROR: integer out of range
210 SELECT '' AS five, i.f1, i.f1 - int2 '2' AS x FROM INT4_TBL i
211 WHERE f1 > -2147483647;
213 ------+------------+------------
217 | 2147483647 | 2147483645
220 SELECT '' AS five, i.f1, i.f1 - int4 '2' AS x FROM INT4_TBL i;
221 ERROR: integer out of range
222 SELECT '' AS five, i.f1, i.f1 - int4 '2' AS x FROM INT4_TBL i
223 WHERE f1 > -2147483647;
225 ------+------------+------------
229 | 2147483647 | 2147483645
232 SELECT '' AS five, i.f1, i.f1 / int2 '2' AS x FROM INT4_TBL i;
234 ------+-------------+-------------
238 | 2147483647 | 1073741823
239 | -2147483647 | -1073741823
242 SELECT '' AS five, i.f1, i.f1 / int4 '2' AS x FROM INT4_TBL i;
244 ------+-------------+-------------
248 | 2147483647 | 1073741823
249 | -2147483647 | -1073741823
253 -- more complex expressions
255 -- variations on unary minus parsing
268 SELECT 2- -1 AS three;
274 SELECT 2 - -2 AS four;
280 SELECT int2 '2' * int2 '2' = int2 '16' / int2 '4' AS true;
286 SELECT int4 '2' * int2 '2' = int2 '16' / int4 '4' AS true;
292 SELECT int2 '2' * int4 '2' = int4 '16' / int2 '4' AS true;
298 SELECT int4 '1000' < int4 '999' AS false;
304 SELECT 4! AS twenty_four;
316 SELECT 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 AS ten;
322 SELECT 2 + 2 / 2 AS three;
328 SELECT (2 + 2) / 2 AS two;