1--
2-- INT8
3-- Test int8 64-bit integers.
4--
5CREATE TABLE INT8_TBL(q1 int8, q2 int8);
6
7INSERT INTO INT8_TBL VALUES('  123   ','  456');
8INSERT INTO INT8_TBL VALUES('123   ','4567890123456789');
9INSERT INTO INT8_TBL VALUES('4567890123456789','123');
10INSERT INTO INT8_TBL VALUES(+4567890123456789,'4567890123456789');
11INSERT INTO INT8_TBL VALUES('+4567890123456789','-4567890123456789');
12
13-- bad inputs
14INSERT INTO INT8_TBL(q1) VALUES ('      ');
15INSERT INTO INT8_TBL(q1) VALUES ('xxx');
16INSERT INTO INT8_TBL(q1) VALUES ('3908203590239580293850293850329485');
17INSERT INTO INT8_TBL(q1) VALUES ('-1204982019841029840928340329840934');
18INSERT INTO INT8_TBL(q1) VALUES ('- 123');
19INSERT INTO INT8_TBL(q1) VALUES ('  345     5');
20INSERT INTO INT8_TBL(q1) VALUES ('');
21
22SELECT * FROM INT8_TBL;
23
24-- int8/int8 cmp
25SELECT * FROM INT8_TBL WHERE q2 = 4567890123456789;
26SELECT * FROM INT8_TBL WHERE q2 <> 4567890123456789;
27SELECT * FROM INT8_TBL WHERE q2 < 4567890123456789;
28SELECT * FROM INT8_TBL WHERE q2 > 4567890123456789;
29SELECT * FROM INT8_TBL WHERE q2 <= 4567890123456789;
30SELECT * FROM INT8_TBL WHERE q2 >= 4567890123456789;
31
32-- int8/int4 cmp
33SELECT * FROM INT8_TBL WHERE q2 = 456;
34SELECT * FROM INT8_TBL WHERE q2 <> 456;
35SELECT * FROM INT8_TBL WHERE q2 < 456;
36SELECT * FROM INT8_TBL WHERE q2 > 456;
37SELECT * FROM INT8_TBL WHERE q2 <= 456;
38SELECT * FROM INT8_TBL WHERE q2 >= 456;
39
40-- int4/int8 cmp
41SELECT * FROM INT8_TBL WHERE 123 = q1;
42SELECT * FROM INT8_TBL WHERE 123 <> q1;
43SELECT * FROM INT8_TBL WHERE 123 < q1;
44SELECT * FROM INT8_TBL WHERE 123 > q1;
45SELECT * FROM INT8_TBL WHERE 123 <= q1;
46SELECT * FROM INT8_TBL WHERE 123 >= q1;
47
48-- int8/int2 cmp
49SELECT * FROM INT8_TBL WHERE q2 = '456'::int2;
50SELECT * FROM INT8_TBL WHERE q2 <> '456'::int2;
51SELECT * FROM INT8_TBL WHERE q2 < '456'::int2;
52SELECT * FROM INT8_TBL WHERE q2 > '456'::int2;
53SELECT * FROM INT8_TBL WHERE q2 <= '456'::int2;
54SELECT * FROM INT8_TBL WHERE q2 >= '456'::int2;
55
56-- int2/int8 cmp
57SELECT * FROM INT8_TBL WHERE '123'::int2 = q1;
58SELECT * FROM INT8_TBL WHERE '123'::int2 <> q1;
59SELECT * FROM INT8_TBL WHERE '123'::int2 < q1;
60SELECT * FROM INT8_TBL WHERE '123'::int2 > q1;
61SELECT * FROM INT8_TBL WHERE '123'::int2 <= q1;
62SELECT * FROM INT8_TBL WHERE '123'::int2 >= q1;
63
64
65SELECT '' AS five, q1 AS plus, -q1 AS minus FROM INT8_TBL;
66
67SELECT '' AS five, q1, q2, q1 + q2 AS plus FROM INT8_TBL;
68SELECT '' AS five, q1, q2, q1 - q2 AS minus FROM INT8_TBL;
69SELECT '' AS three, q1, q2, q1 * q2 AS multiply FROM INT8_TBL;
70SELECT '' AS three, q1, q2, q1 * q2 AS multiply FROM INT8_TBL
71 WHERE q1 < 1000 or (q2 > 0 and q2 < 1000);
72SELECT '' AS five, q1, q2, q1 / q2 AS divide, q1 % q2 AS mod FROM INT8_TBL;
73
74SELECT '' AS five, q1, float8(q1) FROM INT8_TBL;
75SELECT '' AS five, q2, float8(q2) FROM INT8_TBL;
76
77SELECT 37 + q1 AS plus4 FROM INT8_TBL;
78SELECT 37 - q1 AS minus4 FROM INT8_TBL;
79SELECT '' AS five, 2 * q1 AS "twice int4" FROM INT8_TBL;
80SELECT '' AS five, q1 * 2 AS "twice int4" FROM INT8_TBL;
81
82-- int8 op int4
83SELECT q1 + 42::int4 AS "8plus4", q1 - 42::int4 AS "8minus4", q1 * 42::int4 AS "8mul4", q1 / 42::int4 AS "8div4" FROM INT8_TBL;
84-- int4 op int8
85SELECT 246::int4 + q1 AS "4plus8", 246::int4 - q1 AS "4minus8", 246::int4 * q1 AS "4mul8", 246::int4 / q1 AS "4div8" FROM INT8_TBL;
86
87-- int8 op int2
88SELECT q1 + 42::int2 AS "8plus2", q1 - 42::int2 AS "8minus2", q1 * 42::int2 AS "8mul2", q1 / 42::int2 AS "8div2" FROM INT8_TBL;
89-- int2 op int8
90SELECT 246::int2 + q1 AS "2plus8", 246::int2 - q1 AS "2minus8", 246::int2 * q1 AS "2mul8", 246::int2 / q1 AS "2div8" FROM INT8_TBL;
91
92SELECT q2, abs(q2) FROM INT8_TBL;
93SELECT min(q1), min(q2) FROM INT8_TBL;
94SELECT max(q1), max(q2) FROM INT8_TBL;
95
96
97-- TO_CHAR()
98--
99SELECT '' AS to_char_1, to_char(q1, '9G999G999G999G999G999'), to_char(q2, '9,999,999,999,999,999')
100	FROM INT8_TBL;
101
102SELECT '' AS to_char_2, to_char(q1, '9G999G999G999G999G999D999G999'), to_char(q2, '9,999,999,999,999,999.999,999')
103	FROM INT8_TBL;
104
105SELECT '' AS to_char_3, to_char( (q1 * -1), '9999999999999999PR'), to_char( (q2 * -1), '9999999999999999.999PR')
106	FROM INT8_TBL;
107
108SELECT '' AS to_char_4, to_char( (q1 * -1), '9999999999999999S'), to_char( (q2 * -1), 'S9999999999999999')
109	FROM INT8_TBL;
110
111SELECT '' AS to_char_5,  to_char(q2, 'MI9999999999999999')     FROM INT8_TBL;
112SELECT '' AS to_char_6,  to_char(q2, 'FMS9999999999999999')    FROM INT8_TBL;
113SELECT '' AS to_char_7,  to_char(q2, 'FM9999999999999999THPR') FROM INT8_TBL;
114SELECT '' AS to_char_8,  to_char(q2, 'SG9999999999999999th')   FROM INT8_TBL;
115SELECT '' AS to_char_9,  to_char(q2, '0999999999999999')       FROM INT8_TBL;
116SELECT '' AS to_char_10, to_char(q2, 'S0999999999999999')      FROM INT8_TBL;
117SELECT '' AS to_char_11, to_char(q2, 'FM0999999999999999')     FROM INT8_TBL;
118SELECT '' AS to_char_12, to_char(q2, 'FM9999999999999999.000') FROM INT8_TBL;
119SELECT '' AS to_char_13, to_char(q2, 'L9999999999999999.000')  FROM INT8_TBL;
120SELECT '' AS to_char_14, to_char(q2, 'FM9999999999999999.999') FROM INT8_TBL;
121SELECT '' AS to_char_15, to_char(q2, 'S 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 . 9 9 9') FROM INT8_TBL;
122SELECT '' AS to_char_16, to_char(q2, E'99999 "text" 9999 "9999" 999 "\\"text between quote marks\\"" 9999') FROM INT8_TBL;
123SELECT '' AS to_char_17, to_char(q2, '999999SG9999999999')     FROM INT8_TBL;
124
125-- check min/max values and overflow behavior
126
127select '-9223372036854775808'::int8;
128select '-9223372036854775809'::int8;
129select '9223372036854775807'::int8;
130select '9223372036854775808'::int8;
131
132select -('-9223372036854775807'::int8);
133select -('-9223372036854775808'::int8);
134
135select '9223372036854775800'::int8 + '9223372036854775800'::int8;
136select '-9223372036854775800'::int8 + '-9223372036854775800'::int8;
137
138select '9223372036854775800'::int8 - '-9223372036854775800'::int8;
139select '-9223372036854775800'::int8 - '9223372036854775800'::int8;
140
141select '9223372036854775800'::int8 * '9223372036854775800'::int8;
142
143select '9223372036854775800'::int8 / '0'::int8;
144select '9223372036854775800'::int8 % '0'::int8;
145
146select abs('-9223372036854775808'::int8);
147
148select '9223372036854775800'::int8 + '100'::int4;
149select '-9223372036854775800'::int8 - '100'::int4;
150select '9223372036854775800'::int8 * '100'::int4;
151
152select '100'::int4 + '9223372036854775800'::int8;
153select '-100'::int4 - '9223372036854775800'::int8;
154select '100'::int4 * '9223372036854775800'::int8;
155
156select '9223372036854775800'::int8 + '100'::int2;
157select '-9223372036854775800'::int8 - '100'::int2;
158select '9223372036854775800'::int8 * '100'::int2;
159select '-9223372036854775808'::int8 / '0'::int2;
160
161select '100'::int2 + '9223372036854775800'::int8;
162select '-100'::int2 - '9223372036854775800'::int8;
163select '100'::int2 * '9223372036854775800'::int8;
164select '100'::int2 / '0'::int8;
165
166SELECT CAST(q1 AS int4) FROM int8_tbl WHERE q2 = 456;
167SELECT CAST(q1 AS int4) FROM int8_tbl WHERE q2 <> 456;
168
169SELECT CAST(q1 AS int2) FROM int8_tbl WHERE q2 = 456;
170SELECT CAST(q1 AS int2) FROM int8_tbl WHERE q2 <> 456;
171
172SELECT CAST('42'::int2 AS int8), CAST('-37'::int2 AS int8);
173
174SELECT CAST(q1 AS float4), CAST(q2 AS float8) FROM INT8_TBL;
175SELECT CAST('36854775807.0'::float4 AS int8);
176SELECT CAST('922337203685477580700.0'::float8 AS int8);
177
178SELECT CAST(q1 AS oid) FROM INT8_TBL;
179SELECT oid::int8 FROM pg_class WHERE relname = 'pg_class';
180
181
182-- bit operations
183
184SELECT q1, q2, q1 & q2 AS "and", q1 | q2 AS "or", q1 # q2 AS "xor", ~q1 AS "not" FROM INT8_TBL;
185SELECT q1, q1 << 2 AS "shl", q1 >> 3 AS "shr" FROM INT8_TBL;
186
187
188-- generate_series
189
190SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8);
191SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 0);
192SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 2);
193
194-- corner case
195SELECT (-1::int8<<63)::text;
196SELECT ((-1::int8<<63)+1)::text;
197
198-- check sane handling of INT64_MIN overflow cases
199SELECT (-9223372036854775808)::int8 * (-1)::int8;
200SELECT (-9223372036854775808)::int8 / (-1)::int8;
201SELECT (-9223372036854775808)::int8 % (-1)::int8;
202SELECT (-9223372036854775808)::int8 * (-1)::int4;
203SELECT (-9223372036854775808)::int8 / (-1)::int4;
204SELECT (-9223372036854775808)::int8 % (-1)::int4;
205SELECT (-9223372036854775808)::int8 * (-1)::int2;
206SELECT (-9223372036854775808)::int8 / (-1)::int2;
207SELECT (-9223372036854775808)::int8 % (-1)::int2;
208
209-- check rounding when casting from float
210SELECT x, x::int8 AS int8_value
211FROM (VALUES (-2.5::float8),
212             (-1.5::float8),
213             (-0.5::float8),
214             (0.0::float8),
215             (0.5::float8),
216             (1.5::float8),
217             (2.5::float8)) t(x);
218
219-- check rounding when casting from numeric
220SELECT x, x::int8 AS int8_value
221FROM (VALUES (-2.5::numeric),
222             (-1.5::numeric),
223             (-0.5::numeric),
224             (0.0::numeric),
225             (0.5::numeric),
226             (1.5::numeric),
227             (2.5::numeric)) t(x);
228