3 -- Test int8 64-bit integers.
5 CREATE TABLE INT8_TBL(q1 int8, q2 int8);
6 INSERT INTO INT8_TBL VALUES(' 123 ',' 456');
7 INSERT INTO INT8_TBL VALUES('123 ','4567890123456789');
8 INSERT INTO INT8_TBL VALUES('4567890123456789','123');
9 INSERT INTO INT8_TBL VALUES(+4567890123456789,'4567890123456789');
10 INSERT INTO INT8_TBL VALUES('+4567890123456789','-4567890123456789');
12 INSERT INTO INT8_TBL(q1) VALUES (' ');
13 ERROR: invalid input syntax for integer: " "
14 LINE 1: INSERT INTO INT8_TBL(q1) VALUES (' ');
16 INSERT INTO INT8_TBL(q1) VALUES ('xxx');
17 ERROR: invalid input syntax for integer: "xxx"
18 LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('xxx');
20 INSERT INTO INT8_TBL(q1) VALUES ('3908203590239580293850293850329485');
21 ERROR: value "3908203590239580293850293850329485" is out of range for type bigint
22 LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('39082035902395802938502938...
24 INSERT INTO INT8_TBL(q1) VALUES ('-1204982019841029840928340329840934');
25 ERROR: value "-1204982019841029840928340329840934" is out of range for type bigint
26 LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('-1204982019841029840928340...
28 INSERT INTO INT8_TBL(q1) VALUES ('- 123');
29 ERROR: invalid input syntax for integer: "- 123"
30 LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('- 123');
32 INSERT INTO INT8_TBL(q1) VALUES (' 345 5');
33 ERROR: invalid input syntax for integer: " 345 5"
34 LINE 1: INSERT INTO INT8_TBL(q1) VALUES (' 345 5');
36 INSERT INTO INT8_TBL(q1) VALUES ('');
37 ERROR: invalid input syntax for integer: ""
38 LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('');
40 SELECT * FROM INT8_TBL;
42 ------------------+-------------------
44 123 | 4567890123456789
45 4567890123456789 | 123
46 4567890123456789 | 4567890123456789
47 4567890123456789 | -4567890123456789
51 SELECT * FROM INT8_TBL WHERE q2 = 4567890123456789;
53 ------------------+------------------
54 123 | 4567890123456789
55 4567890123456789 | 4567890123456789
58 SELECT * FROM INT8_TBL WHERE q2 <> 4567890123456789;
60 ------------------+-------------------
62 4567890123456789 | 123
63 4567890123456789 | -4567890123456789
66 SELECT * FROM INT8_TBL WHERE q2 < 4567890123456789;
68 ------------------+-------------------
70 4567890123456789 | 123
71 4567890123456789 | -4567890123456789
74 SELECT * FROM INT8_TBL WHERE q2 > 4567890123456789;
79 SELECT * FROM INT8_TBL WHERE q2 <= 4567890123456789;
81 ------------------+-------------------
83 123 | 4567890123456789
84 4567890123456789 | 123
85 4567890123456789 | 4567890123456789
86 4567890123456789 | -4567890123456789
89 SELECT * FROM INT8_TBL WHERE q2 >= 4567890123456789;
91 ------------------+------------------
92 123 | 4567890123456789
93 4567890123456789 | 4567890123456789
97 SELECT * FROM INT8_TBL WHERE q2 = 456;
103 SELECT * FROM INT8_TBL WHERE q2 <> 456;
105 ------------------+-------------------
106 123 | 4567890123456789
107 4567890123456789 | 123
108 4567890123456789 | 4567890123456789
109 4567890123456789 | -4567890123456789
112 SELECT * FROM INT8_TBL WHERE q2 < 456;
114 ------------------+-------------------
115 4567890123456789 | 123
116 4567890123456789 | -4567890123456789
119 SELECT * FROM INT8_TBL WHERE q2 > 456;
121 ------------------+------------------
122 123 | 4567890123456789
123 4567890123456789 | 4567890123456789
126 SELECT * FROM INT8_TBL WHERE q2 <= 456;
128 ------------------+-------------------
130 4567890123456789 | 123
131 4567890123456789 | -4567890123456789
134 SELECT * FROM INT8_TBL WHERE q2 >= 456;
136 ------------------+------------------
138 123 | 4567890123456789
139 4567890123456789 | 4567890123456789
143 SELECT * FROM INT8_TBL WHERE 123 = q1;
145 -----+------------------
147 123 | 4567890123456789
150 SELECT * FROM INT8_TBL WHERE 123 <> q1;
152 ------------------+-------------------
153 4567890123456789 | 123
154 4567890123456789 | 4567890123456789
155 4567890123456789 | -4567890123456789
158 SELECT * FROM INT8_TBL WHERE 123 < q1;
160 ------------------+-------------------
161 4567890123456789 | 123
162 4567890123456789 | 4567890123456789
163 4567890123456789 | -4567890123456789
166 SELECT * FROM INT8_TBL WHERE 123 > q1;
171 SELECT * FROM INT8_TBL WHERE 123 <= q1;
173 ------------------+-------------------
175 123 | 4567890123456789
176 4567890123456789 | 123
177 4567890123456789 | 4567890123456789
178 4567890123456789 | -4567890123456789
181 SELECT * FROM INT8_TBL WHERE 123 >= q1;
183 -----+------------------
185 123 | 4567890123456789
189 SELECT * FROM INT8_TBL WHERE q2 = '456'::int2;
195 SELECT * FROM INT8_TBL WHERE q2 <> '456'::int2;
197 ------------------+-------------------
198 123 | 4567890123456789
199 4567890123456789 | 123
200 4567890123456789 | 4567890123456789
201 4567890123456789 | -4567890123456789
204 SELECT * FROM INT8_TBL WHERE q2 < '456'::int2;
206 ------------------+-------------------
207 4567890123456789 | 123
208 4567890123456789 | -4567890123456789
211 SELECT * FROM INT8_TBL WHERE q2 > '456'::int2;
213 ------------------+------------------
214 123 | 4567890123456789
215 4567890123456789 | 4567890123456789
218 SELECT * FROM INT8_TBL WHERE q2 <= '456'::int2;
220 ------------------+-------------------
222 4567890123456789 | 123
223 4567890123456789 | -4567890123456789
226 SELECT * FROM INT8_TBL WHERE q2 >= '456'::int2;
228 ------------------+------------------
230 123 | 4567890123456789
231 4567890123456789 | 4567890123456789
235 SELECT * FROM INT8_TBL WHERE '123'::int2 = q1;
237 -----+------------------
239 123 | 4567890123456789
242 SELECT * FROM INT8_TBL WHERE '123'::int2 <> q1;
244 ------------------+-------------------
245 4567890123456789 | 123
246 4567890123456789 | 4567890123456789
247 4567890123456789 | -4567890123456789
250 SELECT * FROM INT8_TBL WHERE '123'::int2 < q1;
252 ------------------+-------------------
253 4567890123456789 | 123
254 4567890123456789 | 4567890123456789
255 4567890123456789 | -4567890123456789
258 SELECT * FROM INT8_TBL WHERE '123'::int2 > q1;
263 SELECT * FROM INT8_TBL WHERE '123'::int2 <= q1;
265 ------------------+-------------------
267 123 | 4567890123456789
268 4567890123456789 | 123
269 4567890123456789 | 4567890123456789
270 4567890123456789 | -4567890123456789
273 SELECT * FROM INT8_TBL WHERE '123'::int2 >= q1;
275 -----+------------------
277 123 | 4567890123456789
280 SELECT '' AS five, q1 AS plus, -q1 AS minus FROM INT8_TBL;
282 ------+------------------+-------------------
285 | 4567890123456789 | -4567890123456789
286 | 4567890123456789 | -4567890123456789
287 | 4567890123456789 | -4567890123456789
290 SELECT '' AS five, q1, q2, q1 + q2 AS plus FROM INT8_TBL;
291 five | q1 | q2 | plus
292 ------+------------------+-------------------+------------------
294 | 123 | 4567890123456789 | 4567890123456912
295 | 4567890123456789 | 123 | 4567890123456912
296 | 4567890123456789 | 4567890123456789 | 9135780246913578
297 | 4567890123456789 | -4567890123456789 | 0
300 SELECT '' AS five, q1, q2, q1 - q2 AS minus FROM INT8_TBL;
301 five | q1 | q2 | minus
302 ------+------------------+-------------------+-------------------
304 | 123 | 4567890123456789 | -4567890123456666
305 | 4567890123456789 | 123 | 4567890123456666
306 | 4567890123456789 | 4567890123456789 | 0
307 | 4567890123456789 | -4567890123456789 | 9135780246913578
310 SELECT '' AS three, q1, q2, q1 * q2 AS multiply FROM INT8_TBL;
311 ERROR: bigint out of range
312 SELECT '' AS three, q1, q2, q1 * q2 AS multiply FROM INT8_TBL
313 WHERE q1 < 1000 or (q2 > 0 and q2 < 1000);
314 three | q1 | q2 | multiply
315 -------+------------------+------------------+--------------------
317 | 123 | 4567890123456789 | 561850485185185047
318 | 4567890123456789 | 123 | 561850485185185047
321 SELECT '' AS five, q1, q2, q1 / q2 AS divide, q1 % q2 AS mod FROM INT8_TBL;
322 five | q1 | q2 | divide | mod
323 ------+------------------+-------------------+----------------+-----
324 | 123 | 456 | 0 | 123
325 | 123 | 4567890123456789 | 0 | 123
326 | 4567890123456789 | 123 | 37137318076884 | 57
327 | 4567890123456789 | 4567890123456789 | 1 | 0
328 | 4567890123456789 | -4567890123456789 | -1 | 0
331 SELECT '' AS five, q1, float8(q1) FROM INT8_TBL;
333 ------+------------------+-----------------------
336 | 4567890123456789 | 4.56789012345679e+015
337 | 4567890123456789 | 4.56789012345679e+015
338 | 4567890123456789 | 4.56789012345679e+015
341 SELECT '' AS five, q2, float8(q2) FROM INT8_TBL;
343 ------+-------------------+------------------------
345 | 4567890123456789 | 4.56789012345679e+015
347 | 4567890123456789 | 4.56789012345679e+015
348 | -4567890123456789 | -4.56789012345679e+015
351 SELECT 37 + q1 AS plus4 FROM INT8_TBL;
361 SELECT 37 - q1 AS minus4 FROM INT8_TBL;
371 SELECT '' AS five, 2 * q1 AS "twice int4" FROM INT8_TBL;
373 ------+------------------
381 SELECT '' AS five, q1 * 2 AS "twice int4" FROM INT8_TBL;
383 ------+------------------
392 SELECT q1 + 42::int4 AS "8plus4", q1 - 42::int4 AS "8minus4", q1 * 42::int4 AS "8mul4", q1 / 42::int4 AS "8div4" FROM INT8_TBL;
393 8plus4 | 8minus4 | 8mul4 | 8div4
394 ------------------+------------------+--------------------+-----------------
397 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
398 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
399 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
403 SELECT 246::int4 + q1 AS "4plus8", 246::int4 - q1 AS "4minus8", 246::int4 * q1 AS "4mul8", 246::int4 / q1 AS "4div8" FROM INT8_TBL;
404 4plus8 | 4minus8 | 4mul8 | 4div8
405 ------------------+-------------------+---------------------+-------
406 369 | 123 | 30258 | 2
407 369 | 123 | 30258 | 2
408 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
409 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
410 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
414 SELECT q1 + 42::int2 AS "8plus2", q1 - 42::int2 AS "8minus2", q1 * 42::int2 AS "8mul2", q1 / 42::int2 AS "8div2" FROM INT8_TBL;
415 8plus2 | 8minus2 | 8mul2 | 8div2
416 ------------------+------------------+--------------------+-----------------
419 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
420 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
421 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
425 SELECT 246::int2 + q1 AS "2plus8", 246::int2 - q1 AS "2minus8", 246::int2 * q1 AS "2mul8", 246::int2 / q1 AS "2div8" FROM INT8_TBL;
426 2plus8 | 2minus8 | 2mul8 | 2div8
427 ------------------+-------------------+---------------------+-------
428 369 | 123 | 30258 | 2
429 369 | 123 | 30258 | 2
430 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
431 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
432 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
435 SELECT q2, abs(q2) FROM INT8_TBL;
437 -------------------+------------------
439 4567890123456789 | 4567890123456789
441 4567890123456789 | 4567890123456789
442 -4567890123456789 | 4567890123456789
445 SELECT min(q1), min(q2) FROM INT8_TBL;
447 -----+-------------------
448 123 | -4567890123456789
451 SELECT max(q1), max(q2) FROM INT8_TBL;
453 ------------------+------------------
454 4567890123456789 | 4567890123456789
459 SELECT '' AS to_char_1, to_char(q1, '9G999G999G999G999G999'), to_char(q2, '9,999,999,999,999,999')
461 to_char_1 | to_char | to_char
462 -----------+------------------------+------------------------
464 | 123 | 4,567,890,123,456,789
465 | 4,567,890,123,456,789 | 123
466 | 4,567,890,123,456,789 | 4,567,890,123,456,789
467 | 4,567,890,123,456,789 | -4,567,890,123,456,789
470 SELECT '' AS to_char_2, to_char(q1, '9G999G999G999G999G999D999G999'), to_char(q2, '9,999,999,999,999,999.999,999')
472 to_char_2 | to_char | to_char
473 -----------+--------------------------------+--------------------------------
474 | 123.000,000 | 456.000,000
475 | 123.000,000 | 4,567,890,123,456,789.000,000
476 | 4,567,890,123,456,789.000,000 | 123.000,000
477 | 4,567,890,123,456,789.000,000 | 4,567,890,123,456,789.000,000
478 | 4,567,890,123,456,789.000,000 | -4,567,890,123,456,789.000,000
481 SELECT '' AS to_char_3, to_char( (q1 * -1), '9999999999999999PR'), to_char( (q2 * -1), '9999999999999999.999PR')
483 to_char_3 | to_char | to_char
484 -----------+--------------------+------------------------
486 | <123> | <4567890123456789.000>
487 | <4567890123456789> | <123.000>
488 | <4567890123456789> | <4567890123456789.000>
489 | <4567890123456789> | 4567890123456789.000
492 SELECT '' AS to_char_4, to_char( (q1 * -1), '9999999999999999S'), to_char( (q2 * -1), 'S9999999999999999')
494 to_char_4 | to_char | to_char
495 -----------+-------------------+-------------------
497 | 123- | -4567890123456789
498 | 4567890123456789- | -123
499 | 4567890123456789- | -4567890123456789
500 | 4567890123456789- | +4567890123456789
503 SELECT '' AS to_char_5, to_char(q2, 'MI9999999999999999') FROM INT8_TBL;
505 -----------+-------------------
513 SELECT '' AS to_char_6, to_char(q2, 'FMS9999999999999999') FROM INT8_TBL;
515 -----------+-------------------
523 SELECT '' AS to_char_7, to_char(q2, 'FM9999999999999999THPR') FROM INT8_TBL;
525 -----------+--------------------
533 SELECT '' AS to_char_8, to_char(q2, 'SG9999999999999999th') FROM INT8_TBL;
535 -----------+---------------------
537 | +4567890123456789th
539 | +4567890123456789th
543 SELECT '' AS to_char_9, to_char(q2, '0999999999999999') FROM INT8_TBL;
545 -----------+-------------------
553 SELECT '' AS to_char_10, to_char(q2, 'S0999999999999999') FROM INT8_TBL;
555 ------------+-------------------
563 SELECT '' AS to_char_11, to_char(q2, 'FM0999999999999999') FROM INT8_TBL;
565 ------------+-------------------
573 SELECT '' AS to_char_12, to_char(q2, 'FM9999999999999999.000') FROM INT8_TBL;
575 ------------+-----------------------
577 | 4567890123456789.000
579 | 4567890123456789.000
580 | -4567890123456789.000
583 SELECT '' AS to_char_13, to_char(q2, 'L9999999999999999.000') FROM INT8_TBL;
585 ------------+------------------------
587 | 4567890123456789.000
589 | 4567890123456789.000
590 | -4567890123456789.000
593 SELECT '' AS to_char_14, to_char(q2, 'FM9999999999999999.999') FROM INT8_TBL;
595 ------------+--------------------
603 SELECT '' 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;
605 ------------+-------------------------------------------
607 | +4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
609 | +4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
610 | -4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
613 SELECT '' AS to_char_16, to_char(q2, E'99999 "text" 9999 "9999" 999 "\\"text between quote marks\\"" 9999') FROM INT8_TBL;
615 ------------+-----------------------------------------------------------
616 | text 9999 "text between quote marks" 456
617 | 45678 text 9012 9999 345 "text between quote marks" 6789
618 | text 9999 "text between quote marks" 123
619 | 45678 text 9012 9999 345 "text between quote marks" 6789
620 | -45678 text 9012 9999 345 "text between quote marks" 6789
623 SELECT '' AS to_char_17, to_char(q2, '999999SG9999999999') FROM INT8_TBL;
625 ------------+-------------------
633 -- check min/max values and overflow behavior
634 select '-9223372036854775808'::int8;
636 ----------------------
640 select '-9223372036854775809'::int8;
641 ERROR: value "-9223372036854775809" is out of range for type bigint
642 LINE 1: select '-9223372036854775809'::int8;
644 select '9223372036854775807'::int8;
646 ---------------------
650 select '9223372036854775808'::int8;
651 ERROR: value "9223372036854775808" is out of range for type bigint
652 LINE 1: select '9223372036854775808'::int8;
654 select -('-9223372036854775807'::int8);
656 ---------------------
660 select -('-9223372036854775808'::int8);
661 ERROR: bigint out of range
662 select '9223372036854775800'::int8 + '9223372036854775800'::int8;
663 ERROR: bigint out of range
664 select '-9223372036854775800'::int8 + '-9223372036854775800'::int8;
665 ERROR: bigint out of range
666 select '9223372036854775800'::int8 - '-9223372036854775800'::int8;
667 ERROR: bigint out of range
668 select '-9223372036854775800'::int8 - '9223372036854775800'::int8;
669 ERROR: bigint out of range
670 select '9223372036854775800'::int8 * '9223372036854775800'::int8;
671 ERROR: bigint out of range
672 select '9223372036854775800'::int8 / '0'::int8;
673 ERROR: division by zero
674 select '9223372036854775800'::int8 % '0'::int8;
675 ERROR: division by zero
676 select abs('-9223372036854775808'::int8);
677 ERROR: bigint out of range
678 select '9223372036854775800'::int8 + '100'::int4;
679 ERROR: bigint out of range
680 select '-9223372036854775800'::int8 - '100'::int4;
681 ERROR: bigint out of range
682 select '9223372036854775800'::int8 * '100'::int4;
683 ERROR: bigint out of range
684 select '100'::int4 + '9223372036854775800'::int8;
685 ERROR: bigint out of range
686 select '-100'::int4 - '9223372036854775800'::int8;
687 ERROR: bigint out of range
688 select '100'::int4 * '9223372036854775800'::int8;
689 ERROR: bigint out of range
690 select '9223372036854775800'::int8 + '100'::int2;
691 ERROR: bigint out of range
692 select '-9223372036854775800'::int8 - '100'::int2;
693 ERROR: bigint out of range
694 select '9223372036854775800'::int8 * '100'::int2;
695 ERROR: bigint out of range
696 select '-9223372036854775808'::int8 / '0'::int2;
697 ERROR: division by zero
698 select '100'::int2 + '9223372036854775800'::int8;
699 ERROR: bigint out of range
700 select '-100'::int2 - '9223372036854775800'::int8;
701 ERROR: bigint out of range
702 select '100'::int2 * '9223372036854775800'::int8;
703 ERROR: bigint out of range
704 select '100'::int2 / '0'::int8;
705 ERROR: division by zero
706 SELECT CAST(q1 AS int4) FROM int8_tbl WHERE q2 = 456;
712 SELECT CAST(q1 AS int4) FROM int8_tbl WHERE q2 <> 456;
713 ERROR: integer out of range
714 SELECT CAST(q1 AS int2) FROM int8_tbl WHERE q2 = 456;
720 SELECT CAST(q1 AS int2) FROM int8_tbl WHERE q2 <> 456;
721 ERROR: smallint out of range
722 SELECT CAST('42'::int2 AS int8), CAST('-37'::int2 AS int8);
728 SELECT CAST(q1 AS float4), CAST(q2 AS float8) FROM INT8_TBL;
730 --------------+------------------------
732 123 | 4.56789012345679e+015
734 4.56789e+015 | 4.56789012345679e+015
735 4.56789e+015 | -4.56789012345679e+015
738 SELECT CAST('36854775807.0'::float4 AS int8);
744 SELECT CAST('922337203685477580700.0'::float8 AS int8);
745 ERROR: bigint out of range
746 SELECT CAST(q1 AS oid) FROM INT8_TBL;
747 ERROR: OID out of range
748 SELECT oid::int8 FROM pg_class WHERE relname = 'pg_class';
755 SELECT q1, q2, q1 & q2 AS "and", q1 | q2 AS "or", q1 # q2 AS "xor", ~q1 AS "not" FROM INT8_TBL;
756 q1 | q2 | and | or | xor | not
757 ------------------+-------------------+------------------+------------------+------------------+-------------------
758 123 | 456 | 72 | 507 | 435 | -124
759 123 | 4567890123456789 | 17 | 4567890123456895 | 4567890123456878 | -124
760 4567890123456789 | 123 | 17 | 4567890123456895 | 4567890123456878 | -4567890123456790
761 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 0 | -4567890123456790
762 4567890123456789 | -4567890123456789 | 1 | -1 | -2 | -4567890123456790
765 SELECT q1, q1 << 2 AS "shl", q1 >> 3 AS "shr" FROM INT8_TBL;
767 ------------------+-------------------+-----------------
770 4567890123456789 | 18271560493827156 | 570986265432098
771 4567890123456789 | 18271560493827156 | 570986265432098
772 4567890123456789 | 18271560493827156 | 570986265432098
776 SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8);
792 SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 0);
793 ERROR: step size cannot equal zero
794 SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 2);