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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
|
--
-- INT8
-- Test int8 64-bit integers.
--
CREATE TABLE INT8_TBL(q1 int8, q2 int8);
INSERT INTO INT8_TBL VALUES(' 123 ',' 456');
INSERT INTO INT8_TBL VALUES('123 ','4567890123456789');
INSERT INTO INT8_TBL VALUES('4567890123456789','123');
INSERT INTO INT8_TBL VALUES(+4567890123456789,'4567890123456789');
INSERT INTO INT8_TBL VALUES('+4567890123456789','-4567890123456789');
-- bad inputs
INSERT INTO INT8_TBL(q1) VALUES (' ');
ERROR: invalid input syntax for type bigint: " "
LINE 1: INSERT INTO INT8_TBL(q1) VALUES (' ');
^
INSERT INTO INT8_TBL(q1) VALUES ('xxx');
ERROR: invalid input syntax for type bigint: "xxx"
LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('xxx');
^
INSERT INTO INT8_TBL(q1) VALUES ('3908203590239580293850293850329485');
ERROR: value "3908203590239580293850293850329485" is out of range for type bigint
LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('39082035902395802938502938...
^
INSERT INTO INT8_TBL(q1) VALUES ('-1204982019841029840928340329840934');
ERROR: value "-1204982019841029840928340329840934" is out of range for type bigint
LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('-1204982019841029840928340...
^
INSERT INTO INT8_TBL(q1) VALUES ('- 123');
ERROR: invalid input syntax for type bigint: "- 123"
LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('- 123');
^
INSERT INTO INT8_TBL(q1) VALUES (' 345 5');
ERROR: invalid input syntax for type bigint: " 345 5"
LINE 1: INSERT INTO INT8_TBL(q1) VALUES (' 345 5');
^
INSERT INTO INT8_TBL(q1) VALUES ('');
ERROR: invalid input syntax for type bigint: ""
LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('');
^
SELECT * FROM INT8_TBL;
q1 | q2
------------------+-------------------
123 | 456
123 | 4567890123456789
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(5 rows)
-- int8/int8 cmp
SELECT * FROM INT8_TBL WHERE q2 = 4567890123456789;
q1 | q2
------------------+------------------
123 | 4567890123456789
4567890123456789 | 4567890123456789
(2 rows)
SELECT * FROM INT8_TBL WHERE q2 <> 4567890123456789;
q1 | q2
------------------+-------------------
123 | 456
4567890123456789 | 123
4567890123456789 | -4567890123456789
(3 rows)
SELECT * FROM INT8_TBL WHERE q2 < 4567890123456789;
q1 | q2
------------------+-------------------
123 | 456
4567890123456789 | 123
4567890123456789 | -4567890123456789
(3 rows)
SELECT * FROM INT8_TBL WHERE q2 > 4567890123456789;
q1 | q2
----+----
(0 rows)
SELECT * FROM INT8_TBL WHERE q2 <= 4567890123456789;
q1 | q2
------------------+-------------------
123 | 456
123 | 4567890123456789
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(5 rows)
SELECT * FROM INT8_TBL WHERE q2 >= 4567890123456789;
q1 | q2
------------------+------------------
123 | 4567890123456789
4567890123456789 | 4567890123456789
(2 rows)
-- int8/int4 cmp
SELECT * FROM INT8_TBL WHERE q2 = 456;
q1 | q2
-----+-----
123 | 456
(1 row)
SELECT * FROM INT8_TBL WHERE q2 <> 456;
q1 | q2
------------------+-------------------
123 | 4567890123456789
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(4 rows)
SELECT * FROM INT8_TBL WHERE q2 < 456;
q1 | q2
------------------+-------------------
4567890123456789 | 123
4567890123456789 | -4567890123456789
(2 rows)
SELECT * FROM INT8_TBL WHERE q2 > 456;
q1 | q2
------------------+------------------
123 | 4567890123456789
4567890123456789 | 4567890123456789
(2 rows)
SELECT * FROM INT8_TBL WHERE q2 <= 456;
q1 | q2
------------------+-------------------
123 | 456
4567890123456789 | 123
4567890123456789 | -4567890123456789
(3 rows)
SELECT * FROM INT8_TBL WHERE q2 >= 456;
q1 | q2
------------------+------------------
123 | 456
123 | 4567890123456789
4567890123456789 | 4567890123456789
(3 rows)
-- int4/int8 cmp
SELECT * FROM INT8_TBL WHERE 123 = q1;
q1 | q2
-----+------------------
123 | 456
123 | 4567890123456789
(2 rows)
SELECT * FROM INT8_TBL WHERE 123 <> q1;
q1 | q2
------------------+-------------------
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(3 rows)
SELECT * FROM INT8_TBL WHERE 123 < q1;
q1 | q2
------------------+-------------------
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(3 rows)
SELECT * FROM INT8_TBL WHERE 123 > q1;
q1 | q2
----+----
(0 rows)
SELECT * FROM INT8_TBL WHERE 123 <= q1;
q1 | q2
------------------+-------------------
123 | 456
123 | 4567890123456789
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(5 rows)
SELECT * FROM INT8_TBL WHERE 123 >= q1;
q1 | q2
-----+------------------
123 | 456
123 | 4567890123456789
(2 rows)
-- int8/int2 cmp
SELECT * FROM INT8_TBL WHERE q2 = '456'::int2;
q1 | q2
-----+-----
123 | 456
(1 row)
SELECT * FROM INT8_TBL WHERE q2 <> '456'::int2;
q1 | q2
------------------+-------------------
123 | 4567890123456789
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(4 rows)
SELECT * FROM INT8_TBL WHERE q2 < '456'::int2;
q1 | q2
------------------+-------------------
4567890123456789 | 123
4567890123456789 | -4567890123456789
(2 rows)
SELECT * FROM INT8_TBL WHERE q2 > '456'::int2;
q1 | q2
------------------+------------------
123 | 4567890123456789
4567890123456789 | 4567890123456789
(2 rows)
SELECT * FROM INT8_TBL WHERE q2 <= '456'::int2;
q1 | q2
------------------+-------------------
123 | 456
4567890123456789 | 123
4567890123456789 | -4567890123456789
(3 rows)
SELECT * FROM INT8_TBL WHERE q2 >= '456'::int2;
q1 | q2
------------------+------------------
123 | 456
123 | 4567890123456789
4567890123456789 | 4567890123456789
(3 rows)
-- int2/int8 cmp
SELECT * FROM INT8_TBL WHERE '123'::int2 = q1;
q1 | q2
-----+------------------
123 | 456
123 | 4567890123456789
(2 rows)
SELECT * FROM INT8_TBL WHERE '123'::int2 <> q1;
q1 | q2
------------------+-------------------
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(3 rows)
SELECT * FROM INT8_TBL WHERE '123'::int2 < q1;
q1 | q2
------------------+-------------------
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(3 rows)
SELECT * FROM INT8_TBL WHERE '123'::int2 > q1;
q1 | q2
----+----
(0 rows)
SELECT * FROM INT8_TBL WHERE '123'::int2 <= q1;
q1 | q2
------------------+-------------------
123 | 456
123 | 4567890123456789
4567890123456789 | 123
4567890123456789 | 4567890123456789
4567890123456789 | -4567890123456789
(5 rows)
SELECT * FROM INT8_TBL WHERE '123'::int2 >= q1;
q1 | q2
-----+------------------
123 | 456
123 | 4567890123456789
(2 rows)
SELECT q1 AS plus, -q1 AS minus FROM INT8_TBL;
plus | minus
------------------+-------------------
123 | -123
123 | -123
4567890123456789 | -4567890123456789
4567890123456789 | -4567890123456789
4567890123456789 | -4567890123456789
(5 rows)
SELECT q1, q2, q1 + q2 AS plus FROM INT8_TBL;
q1 | q2 | plus
------------------+-------------------+------------------
123 | 456 | 579
123 | 4567890123456789 | 4567890123456912
4567890123456789 | 123 | 4567890123456912
4567890123456789 | 4567890123456789 | 9135780246913578
4567890123456789 | -4567890123456789 | 0
(5 rows)
SELECT q1, q2, q1 - q2 AS minus FROM INT8_TBL;
q1 | q2 | minus
------------------+-------------------+-------------------
123 | 456 | -333
123 | 4567890123456789 | -4567890123456666
4567890123456789 | 123 | 4567890123456666
4567890123456789 | 4567890123456789 | 0
4567890123456789 | -4567890123456789 | 9135780246913578
(5 rows)
SELECT q1, q2, q1 * q2 AS multiply FROM INT8_TBL;
ERROR: bigint out of range
SELECT q1, q2, q1 * q2 AS multiply FROM INT8_TBL
WHERE q1 < 1000 or (q2 > 0 and q2 < 1000);
q1 | q2 | multiply
------------------+------------------+--------------------
123 | 456 | 56088
123 | 4567890123456789 | 561850485185185047
4567890123456789 | 123 | 561850485185185047
(3 rows)
SELECT q1, q2, q1 / q2 AS divide, q1 % q2 AS mod FROM INT8_TBL;
q1 | q2 | divide | mod
------------------+-------------------+----------------+-----
123 | 456 | 0 | 123
123 | 4567890123456789 | 0 | 123
4567890123456789 | 123 | 37137318076884 | 57
4567890123456789 | 4567890123456789 | 1 | 0
4567890123456789 | -4567890123456789 | -1 | 0
(5 rows)
SELECT q1, float8(q1) FROM INT8_TBL;
q1 | float8
------------------+-----------------------
123 | 123
123 | 123
4567890123456789 | 4.567890123456789e+15
4567890123456789 | 4.567890123456789e+15
4567890123456789 | 4.567890123456789e+15
(5 rows)
SELECT q2, float8(q2) FROM INT8_TBL;
q2 | float8
-------------------+------------------------
456 | 456
4567890123456789 | 4.567890123456789e+15
123 | 123
4567890123456789 | 4.567890123456789e+15
-4567890123456789 | -4.567890123456789e+15
(5 rows)
SELECT 37 + q1 AS plus4 FROM INT8_TBL;
plus4
------------------
160
160
4567890123456826
4567890123456826
4567890123456826
(5 rows)
SELECT 37 - q1 AS minus4 FROM INT8_TBL;
minus4
-------------------
-86
-86
-4567890123456752
-4567890123456752
-4567890123456752
(5 rows)
SELECT 2 * q1 AS "twice int4" FROM INT8_TBL;
twice int4
------------------
246
246
9135780246913578
9135780246913578
9135780246913578
(5 rows)
SELECT q1 * 2 AS "twice int4" FROM INT8_TBL;
twice int4
------------------
246
246
9135780246913578
9135780246913578
9135780246913578
(5 rows)
-- int8 op int4
SELECT q1 + 42::int4 AS "8plus4", q1 - 42::int4 AS "8minus4", q1 * 42::int4 AS "8mul4", q1 / 42::int4 AS "8div4" FROM INT8_TBL;
8plus4 | 8minus4 | 8mul4 | 8div4
------------------+------------------+--------------------+-----------------
165 | 81 | 5166 | 2
165 | 81 | 5166 | 2
4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
(5 rows)
-- int4 op int8
SELECT 246::int4 + q1 AS "4plus8", 246::int4 - q1 AS "4minus8", 246::int4 * q1 AS "4mul8", 246::int4 / q1 AS "4div8" FROM INT8_TBL;
4plus8 | 4minus8 | 4mul8 | 4div8
------------------+-------------------+---------------------+-------
369 | 123 | 30258 | 2
369 | 123 | 30258 | 2
4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
(5 rows)
-- int8 op int2
SELECT q1 + 42::int2 AS "8plus2", q1 - 42::int2 AS "8minus2", q1 * 42::int2 AS "8mul2", q1 / 42::int2 AS "8div2" FROM INT8_TBL;
8plus2 | 8minus2 | 8mul2 | 8div2
------------------+------------------+--------------------+-----------------
165 | 81 | 5166 | 2
165 | 81 | 5166 | 2
4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
(5 rows)
-- int2 op int8
SELECT 246::int2 + q1 AS "2plus8", 246::int2 - q1 AS "2minus8", 246::int2 * q1 AS "2mul8", 246::int2 / q1 AS "2div8" FROM INT8_TBL;
2plus8 | 2minus8 | 2mul8 | 2div8
------------------+-------------------+---------------------+-------
369 | 123 | 30258 | 2
369 | 123 | 30258 | 2
4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
(5 rows)
SELECT q2, abs(q2) FROM INT8_TBL;
q2 | abs
-------------------+------------------
456 | 456
4567890123456789 | 4567890123456789
123 | 123
4567890123456789 | 4567890123456789
-4567890123456789 | 4567890123456789
(5 rows)
SELECT min(q1), min(q2) FROM INT8_TBL;
min | min
-----+-------------------
123 | -4567890123456789
(1 row)
SELECT max(q1), max(q2) FROM INT8_TBL;
max | max
------------------+------------------
4567890123456789 | 4567890123456789
(1 row)
-- TO_CHAR()
--
SELECT to_char(q1, '9G999G999G999G999G999'), to_char(q2, '9,999,999,999,999,999')
FROM INT8_TBL;
to_char | to_char
------------------------+------------------------
123 | 456
123 | 4,567,890,123,456,789
4,567,890,123,456,789 | 123
4,567,890,123,456,789 | 4,567,890,123,456,789
4,567,890,123,456,789 | -4,567,890,123,456,789
(5 rows)
SELECT to_char(q1, '9G999G999G999G999G999D999G999'), to_char(q2, '9,999,999,999,999,999.999,999')
FROM INT8_TBL;
to_char | to_char
--------------------------------+--------------------------------
123.000,000 | 456.000,000
123.000,000 | 4,567,890,123,456,789.000,000
4,567,890,123,456,789.000,000 | 123.000,000
4,567,890,123,456,789.000,000 | 4,567,890,123,456,789.000,000
4,567,890,123,456,789.000,000 | -4,567,890,123,456,789.000,000
(5 rows)
SELECT to_char( (q1 * -1), '9999999999999999PR'), to_char( (q2 * -1), '9999999999999999.999PR')
FROM INT8_TBL;
to_char | to_char
--------------------+------------------------
<123> | <456.000>
<123> | <4567890123456789.000>
<4567890123456789> | <123.000>
<4567890123456789> | <4567890123456789.000>
<4567890123456789> | 4567890123456789.000
(5 rows)
SELECT to_char( (q1 * -1), '9999999999999999S'), to_char( (q2 * -1), 'S9999999999999999')
FROM INT8_TBL;
to_char | to_char
-------------------+-------------------
123- | -456
123- | -4567890123456789
4567890123456789- | -123
4567890123456789- | -4567890123456789
4567890123456789- | +4567890123456789
(5 rows)
SELECT to_char(q2, 'MI9999999999999999') FROM INT8_TBL;
to_char
-------------------
456
4567890123456789
123
4567890123456789
-4567890123456789
(5 rows)
SELECT to_char(q2, 'FMS9999999999999999') FROM INT8_TBL;
to_char
-------------------
+456
+4567890123456789
+123
+4567890123456789
-4567890123456789
(5 rows)
SELECT to_char(q2, 'FM9999999999999999THPR') FROM INT8_TBL;
to_char
--------------------
456TH
4567890123456789TH
123RD
4567890123456789TH
<4567890123456789>
(5 rows)
SELECT to_char(q2, 'SG9999999999999999th') FROM INT8_TBL;
to_char
---------------------
+ 456th
+4567890123456789th
+ 123rd
+4567890123456789th
-4567890123456789
(5 rows)
SELECT to_char(q2, '0999999999999999') FROM INT8_TBL;
to_char
-------------------
0000000000000456
4567890123456789
0000000000000123
4567890123456789
-4567890123456789
(5 rows)
SELECT to_char(q2, 'S0999999999999999') FROM INT8_TBL;
to_char
-------------------
+0000000000000456
+4567890123456789
+0000000000000123
+4567890123456789
-4567890123456789
(5 rows)
SELECT to_char(q2, 'FM0999999999999999') FROM INT8_TBL;
to_char
-------------------
0000000000000456
4567890123456789
0000000000000123
4567890123456789
-4567890123456789
(5 rows)
SELECT to_char(q2, 'FM9999999999999999.000') FROM INT8_TBL;
to_char
-----------------------
456.000
4567890123456789.000
123.000
4567890123456789.000
-4567890123456789.000
(5 rows)
SELECT to_char(q2, 'L9999999999999999.000') FROM INT8_TBL;
to_char
------------------------
456.000
4567890123456789.000
123.000
4567890123456789.000
-4567890123456789.000
(5 rows)
SELECT to_char(q2, 'FM9999999999999999.999') FROM INT8_TBL;
to_char
--------------------
456.
4567890123456789.
123.
4567890123456789.
-4567890123456789.
(5 rows)
SELECT 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;
to_char
-------------------------------------------
+4 5 6 . 0 0 0
+4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
+1 2 3 . 0 0 0
+4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
-4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
(5 rows)
SELECT to_char(q2, E'99999 "text" 9999 "9999" 999 "\\"text between quote marks\\"" 9999') FROM INT8_TBL;
to_char
-----------------------------------------------------------
text 9999 "text between quote marks" 456
45678 text 9012 9999 345 "text between quote marks" 6789
text 9999 "text between quote marks" 123
45678 text 9012 9999 345 "text between quote marks" 6789
-45678 text 9012 9999 345 "text between quote marks" 6789
(5 rows)
SELECT to_char(q2, '999999SG9999999999') FROM INT8_TBL;
to_char
-------------------
+ 456
456789+0123456789
+ 123
456789+0123456789
456789-0123456789
(5 rows)
-- check min/max values and overflow behavior
select '-9223372036854775808'::int8;
int8
----------------------
-9223372036854775808
(1 row)
select '-9223372036854775809'::int8;
ERROR: value "-9223372036854775809" is out of range for type bigint
LINE 1: select '-9223372036854775809'::int8;
^
select '9223372036854775807'::int8;
int8
---------------------
9223372036854775807
(1 row)
select '9223372036854775808'::int8;
ERROR: value "9223372036854775808" is out of range for type bigint
LINE 1: select '9223372036854775808'::int8;
^
select -('-9223372036854775807'::int8);
?column?
---------------------
9223372036854775807
(1 row)
select -('-9223372036854775808'::int8);
ERROR: bigint out of range
select '9223372036854775800'::int8 + '9223372036854775800'::int8;
ERROR: bigint out of range
select '-9223372036854775800'::int8 + '-9223372036854775800'::int8;
ERROR: bigint out of range
select '9223372036854775800'::int8 - '-9223372036854775800'::int8;
ERROR: bigint out of range
select '-9223372036854775800'::int8 - '9223372036854775800'::int8;
ERROR: bigint out of range
select '9223372036854775800'::int8 * '9223372036854775800'::int8;
ERROR: bigint out of range
select '9223372036854775800'::int8 / '0'::int8;
ERROR: division by zero
select '9223372036854775800'::int8 % '0'::int8;
ERROR: division by zero
select abs('-9223372036854775808'::int8);
ERROR: bigint out of range
select '9223372036854775800'::int8 + '100'::int4;
ERROR: bigint out of range
select '-9223372036854775800'::int8 - '100'::int4;
ERROR: bigint out of range
select '9223372036854775800'::int8 * '100'::int4;
ERROR: bigint out of range
select '100'::int4 + '9223372036854775800'::int8;
ERROR: bigint out of range
select '-100'::int4 - '9223372036854775800'::int8;
ERROR: bigint out of range
select '100'::int4 * '9223372036854775800'::int8;
ERROR: bigint out of range
select '9223372036854775800'::int8 + '100'::int2;
ERROR: bigint out of range
select '-9223372036854775800'::int8 - '100'::int2;
ERROR: bigint out of range
select '9223372036854775800'::int8 * '100'::int2;
ERROR: bigint out of range
select '-9223372036854775808'::int8 / '0'::int2;
ERROR: division by zero
select '100'::int2 + '9223372036854775800'::int8;
ERROR: bigint out of range
select '-100'::int2 - '9223372036854775800'::int8;
ERROR: bigint out of range
select '100'::int2 * '9223372036854775800'::int8;
ERROR: bigint out of range
select '100'::int2 / '0'::int8;
ERROR: division by zero
SELECT CAST(q1 AS int4) FROM int8_tbl WHERE q2 = 456;
q1
-----
123
(1 row)
SELECT CAST(q1 AS int4) FROM int8_tbl WHERE q2 <> 456;
ERROR: integer out of range
SELECT CAST(q1 AS int2) FROM int8_tbl WHERE q2 = 456;
q1
-----
123
(1 row)
SELECT CAST(q1 AS int2) FROM int8_tbl WHERE q2 <> 456;
ERROR: smallint out of range
SELECT CAST('42'::int2 AS int8), CAST('-37'::int2 AS int8);
int8 | int8
------+------
42 | -37
(1 row)
SELECT CAST(q1 AS float4), CAST(q2 AS float8) FROM INT8_TBL;
q1 | q2
-------------+------------------------
123 | 456
123 | 4.567890123456789e+15
4.56789e+15 | 123
4.56789e+15 | 4.567890123456789e+15
4.56789e+15 | -4.567890123456789e+15
(5 rows)
SELECT CAST('36854775807.0'::float4 AS int8);
int8
-------------
36854775808
(1 row)
SELECT CAST('922337203685477580700.0'::float8 AS int8);
ERROR: bigint out of range
SELECT CAST(q1 AS oid) FROM INT8_TBL;
ERROR: OID out of range
SELECT oid::int8 FROM pg_class WHERE relname = 'pg_class';
oid
------
1259
(1 row)
-- bit operations
SELECT q1, q2, q1 & q2 AS "and", q1 | q2 AS "or", q1 # q2 AS "xor", ~q1 AS "not" FROM INT8_TBL;
q1 | q2 | and | or | xor | not
------------------+-------------------+------------------+------------------+------------------+-------------------
123 | 456 | 72 | 507 | 435 | -124
123 | 4567890123456789 | 17 | 4567890123456895 | 4567890123456878 | -124
4567890123456789 | 123 | 17 | 4567890123456895 | 4567890123456878 | -4567890123456790
4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 0 | -4567890123456790
4567890123456789 | -4567890123456789 | 1 | -1 | -2 | -4567890123456790
(5 rows)
SELECT q1, q1 << 2 AS "shl", q1 >> 3 AS "shr" FROM INT8_TBL;
q1 | shl | shr
------------------+-------------------+-----------------
123 | 492 | 15
123 | 492 | 15
4567890123456789 | 18271560493827156 | 570986265432098
4567890123456789 | 18271560493827156 | 570986265432098
4567890123456789 | 18271560493827156 | 570986265432098
(5 rows)
-- generate_series
SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8);
generate_series
------------------
4567890123456789
4567890123456790
4567890123456791
4567890123456792
4567890123456793
4567890123456794
4567890123456795
4567890123456796
4567890123456797
4567890123456798
4567890123456799
(11 rows)
SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 0);
ERROR: step size cannot equal zero
SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 2);
generate_series
------------------
4567890123456789
4567890123456791
4567890123456793
4567890123456795
4567890123456797
4567890123456799
(6 rows)
-- corner case
SELECT (-1::int8<<63)::text;
text
----------------------
-9223372036854775808
(1 row)
SELECT ((-1::int8<<63)+1)::text;
text
----------------------
-9223372036854775807
(1 row)
-- check sane handling of INT64_MIN overflow cases
SELECT (-9223372036854775808)::int8 * (-1)::int8;
ERROR: bigint out of range
SELECT (-9223372036854775808)::int8 / (-1)::int8;
ERROR: bigint out of range
SELECT (-9223372036854775808)::int8 % (-1)::int8;
?column?
----------
0
(1 row)
SELECT (-9223372036854775808)::int8 * (-1)::int4;
ERROR: bigint out of range
SELECT (-9223372036854775808)::int8 / (-1)::int4;
ERROR: bigint out of range
SELECT (-9223372036854775808)::int8 % (-1)::int4;
?column?
----------
0
(1 row)
SELECT (-9223372036854775808)::int8 * (-1)::int2;
ERROR: bigint out of range
SELECT (-9223372036854775808)::int8 / (-1)::int2;
ERROR: bigint out of range
SELECT (-9223372036854775808)::int8 % (-1)::int2;
?column?
----------
0
(1 row)
-- check rounding when casting from float
SELECT x, x::int8 AS int8_value
FROM (VALUES (-2.5::float8),
(-1.5::float8),
(-0.5::float8),
(0.0::float8),
(0.5::float8),
(1.5::float8),
(2.5::float8)) t(x);
x | int8_value
------+------------
-2.5 | -2
-1.5 | -2
-0.5 | 0
0 | 0
0.5 | 0
1.5 | 2
2.5 | 2
(7 rows)
-- check rounding when casting from numeric
SELECT x, x::int8 AS int8_value
FROM (VALUES (-2.5::numeric),
(-1.5::numeric),
(-0.5::numeric),
(0.0::numeric),
(0.5::numeric),
(1.5::numeric),
(2.5::numeric)) t(x);
x | int8_value
------+------------
-2.5 | -3
-1.5 | -2
-0.5 | -1
0.0 | 0
0.5 | 1
1.5 | 2
2.5 | 3
(7 rows)
-- test gcd()
SELECT a, b, gcd(a, b), gcd(a, -b), gcd(b, a), gcd(-b, a)
FROM (VALUES (0::int8, 0::int8),
(0::int8, 29893644334::int8),
(288484263558::int8, 29893644334::int8),
(-288484263558::int8, 29893644334::int8),
((-9223372036854775808)::int8, 1::int8),
((-9223372036854775808)::int8, 9223372036854775807::int8),
((-9223372036854775808)::int8, 4611686018427387904::int8)) AS v(a, b);
a | b | gcd | gcd | gcd | gcd
----------------------+---------------------+---------------------+---------------------+---------------------+---------------------
0 | 0 | 0 | 0 | 0 | 0
0 | 29893644334 | 29893644334 | 29893644334 | 29893644334 | 29893644334
288484263558 | 29893644334 | 6835958 | 6835958 | 6835958 | 6835958
-288484263558 | 29893644334 | 6835958 | 6835958 | 6835958 | 6835958
-9223372036854775808 | 1 | 1 | 1 | 1 | 1
-9223372036854775808 | 9223372036854775807 | 1 | 1 | 1 | 1
-9223372036854775808 | 4611686018427387904 | 4611686018427387904 | 4611686018427387904 | 4611686018427387904 | 4611686018427387904
(7 rows)
SELECT gcd((-9223372036854775808)::int8, 0::int8); -- overflow
ERROR: bigint out of range
SELECT gcd((-9223372036854775808)::int8, (-9223372036854775808)::int8); -- overflow
ERROR: bigint out of range
-- test lcm()
SELECT a, b, lcm(a, b), lcm(a, -b), lcm(b, a), lcm(-b, a)
FROM (VALUES (0::int8, 0::int8),
(0::int8, 29893644334::int8),
(29893644334::int8, 29893644334::int8),
(288484263558::int8, 29893644334::int8),
(-288484263558::int8, 29893644334::int8),
((-9223372036854775808)::int8, 0::int8)) AS v(a, b);
a | b | lcm | lcm | lcm | lcm
----------------------+-------------+------------------+------------------+------------------+------------------
0 | 0 | 0 | 0 | 0 | 0
0 | 29893644334 | 0 | 0 | 0 | 0
29893644334 | 29893644334 | 29893644334 | 29893644334 | 29893644334 | 29893644334
288484263558 | 29893644334 | 1261541684539134 | 1261541684539134 | 1261541684539134 | 1261541684539134
-288484263558 | 29893644334 | 1261541684539134 | 1261541684539134 | 1261541684539134 | 1261541684539134
-9223372036854775808 | 0 | 0 | 0 | 0 | 0
(6 rows)
SELECT lcm((-9223372036854775808)::int8, 1::int8); -- overflow
ERROR: bigint out of range
SELECT lcm(9223372036854775807::int8, 9223372036854775806::int8); -- overflow
ERROR: bigint out of range
|