2000-01-04 17:19:34 +01:00
|
|
|
--
|
|
|
|
-- INT4
|
|
|
|
--
|
2022-02-08 21:30:38 +01:00
|
|
|
-- int4_tbl was already created and filled in test_setup.sql.
|
|
|
|
-- Here we just try to insert bad values.
|
2000-01-04 17:19:34 +01:00
|
|
|
INSERT INTO INT4_TBL(f1) VALUES ('34.5');
|
2018-07-22 23:58:01 +02:00
|
|
|
ERROR: invalid input syntax for type integer: "34.5"
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('34.5');
|
|
|
|
^
|
2000-01-04 17:19:34 +01:00
|
|
|
INSERT INTO INT4_TBL(f1) VALUES ('1000000000000');
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: value "1000000000000" is out of range for type integer
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('1000000000000');
|
|
|
|
^
|
2000-01-04 17:19:34 +01:00
|
|
|
INSERT INTO INT4_TBL(f1) VALUES ('asdf');
|
2018-07-22 23:58:01 +02:00
|
|
|
ERROR: invalid input syntax for type integer: "asdf"
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('asdf');
|
|
|
|
^
|
2004-03-11 03:11:14 +01:00
|
|
|
INSERT INTO INT4_TBL(f1) VALUES (' ');
|
2018-07-22 23:58:01 +02:00
|
|
|
ERROR: invalid input syntax for type integer: " "
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: INSERT INTO INT4_TBL(f1) VALUES (' ');
|
|
|
|
^
|
2004-03-11 03:11:14 +01:00
|
|
|
INSERT INTO INT4_TBL(f1) VALUES (' asdf ');
|
2018-07-22 23:58:01 +02:00
|
|
|
ERROR: invalid input syntax for type integer: " asdf "
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: INSERT INTO INT4_TBL(f1) VALUES (' asdf ');
|
|
|
|
^
|
2004-03-11 03:11:14 +01:00
|
|
|
INSERT INTO INT4_TBL(f1) VALUES ('- 1234');
|
2018-07-22 23:58:01 +02:00
|
|
|
ERROR: invalid input syntax for type integer: "- 1234"
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('- 1234');
|
|
|
|
^
|
2004-03-11 03:11:14 +01:00
|
|
|
INSERT INTO INT4_TBL(f1) VALUES ('123 5');
|
2018-07-22 23:58:01 +02:00
|
|
|
ERROR: invalid input syntax for type integer: "123 5"
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('123 5');
|
|
|
|
^
|
2004-03-11 03:11:14 +01:00
|
|
|
INSERT INTO INT4_TBL(f1) VALUES ('');
|
2018-07-22 23:58:01 +02:00
|
|
|
ERROR: invalid input syntax for type integer: ""
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: INSERT INTO INT4_TBL(f1) VALUES ('');
|
|
|
|
^
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT * FROM INT4_TBL;
|
|
|
|
f1
|
|
|
|
-------------
|
|
|
|
0
|
|
|
|
123456
|
|
|
|
-123456
|
|
|
|
2147483647
|
|
|
|
-2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(5 rows)
|
|
|
|
|
Convert a few datatype input functions to use "soft" error reporting.
This patch converts the input functions for bool, int2, int4, int8,
float4, float8, numeric, and contrib/cube to the new soft-error style.
array_in and record_in are also converted. There's lots more to do,
but this is enough to provide proof-of-concept that the soft-error
API is usable, as well as reference examples for how to convert
input functions.
This patch is mostly by me, but it owes very substantial debt to
earlier work by Nikita Glukhov, Andrew Dunstan, and Amul Sul.
Thanks to Andres Freund for review.
Discussion: https://postgr.es/m/3bbbb0df-7382-bf87-9737-340ba096e034@postgrespro.ru
2022-12-09 16:14:53 +01:00
|
|
|
-- Also try it with non-error-throwing API
|
|
|
|
SELECT pg_input_is_valid('34', 'int4');
|
|
|
|
pg_input_is_valid
|
|
|
|
-------------------
|
|
|
|
t
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT pg_input_is_valid('asdf', 'int4');
|
|
|
|
pg_input_is_valid
|
|
|
|
-------------------
|
|
|
|
f
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT pg_input_is_valid('1000000000000', 'int4');
|
|
|
|
pg_input_is_valid
|
|
|
|
-------------------
|
|
|
|
f
|
|
|
|
(1 row)
|
|
|
|
|
2023-02-28 00:04:13 +01:00
|
|
|
SELECT * FROM pg_input_error_info('1000000000000', 'int4');
|
|
|
|
message | detail | hint | sql_error_code
|
|
|
|
--------------------------------------------------------+--------+------+----------------
|
|
|
|
value "1000000000000" is out of range for type integer | | | 22003
|
Convert a few datatype input functions to use "soft" error reporting.
This patch converts the input functions for bool, int2, int4, int8,
float4, float8, numeric, and contrib/cube to the new soft-error style.
array_in and record_in are also converted. There's lots more to do,
but this is enough to provide proof-of-concept that the soft-error
API is usable, as well as reference examples for how to convert
input functions.
This patch is mostly by me, but it owes very substantial debt to
earlier work by Nikita Glukhov, Andrew Dunstan, and Amul Sul.
Thanks to Andres Freund for review.
Discussion: https://postgr.es/m/3bbbb0df-7382-bf87-9737-340ba096e034@postgrespro.ru
2022-12-09 16:14:53 +01:00
|
|
|
(1 row)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 <> int2 '0';
|
|
|
|
f1
|
|
|
|
-------------
|
|
|
|
123456
|
|
|
|
-123456
|
|
|
|
2147483647
|
|
|
|
-2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(4 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 <> int4 '0';
|
|
|
|
f1
|
|
|
|
-------------
|
|
|
|
123456
|
|
|
|
-123456
|
|
|
|
2147483647
|
|
|
|
-2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(4 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 = int2 '0';
|
|
|
|
f1
|
|
|
|
----
|
|
|
|
0
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 = int4 '0';
|
|
|
|
f1
|
|
|
|
----
|
|
|
|
0
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 < int2 '0';
|
|
|
|
f1
|
|
|
|
-------------
|
|
|
|
-123456
|
|
|
|
-2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(2 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 < int4 '0';
|
|
|
|
f1
|
|
|
|
-------------
|
|
|
|
-123456
|
|
|
|
-2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(2 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 <= int2 '0';
|
|
|
|
f1
|
|
|
|
-------------
|
|
|
|
0
|
|
|
|
-123456
|
|
|
|
-2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(3 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 <= int4 '0';
|
|
|
|
f1
|
|
|
|
-------------
|
|
|
|
0
|
|
|
|
-123456
|
|
|
|
-2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(3 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 > int2 '0';
|
|
|
|
f1
|
|
|
|
------------
|
|
|
|
123456
|
|
|
|
2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(2 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 > int4 '0';
|
|
|
|
f1
|
|
|
|
------------
|
|
|
|
123456
|
|
|
|
2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(2 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 >= int2 '0';
|
|
|
|
f1
|
|
|
|
------------
|
|
|
|
0
|
|
|
|
123456
|
|
|
|
2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(3 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE i.f1 >= int4 '0';
|
|
|
|
f1
|
|
|
|
------------
|
|
|
|
0
|
|
|
|
123456
|
|
|
|
2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(3 rows)
|
|
|
|
|
2004-03-11 03:11:14 +01:00
|
|
|
-- positive odds
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE (i.f1 % int2 '2') = int2 '1';
|
|
|
|
f1
|
|
|
|
------------
|
|
|
|
2147483647
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2004-03-11 03:11:14 +01:00
|
|
|
-- any evens
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.* FROM INT4_TBL i WHERE (i.f1 % int4 '2') = int2 '0';
|
|
|
|
f1
|
|
|
|
---------
|
|
|
|
0
|
|
|
|
123456
|
|
|
|
-123456
|
1997-04-05 13:24:54 +02:00
|
|
|
(3 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 * int2 '2' AS x FROM INT4_TBL i;
|
2004-10-04 16:42:48 +02:00
|
|
|
ERROR: integer out of range
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 * int2 '2' AS x FROM INT4_TBL i
|
2004-10-04 16:42:48 +02:00
|
|
|
WHERE abs(f1) < 1073741824;
|
2020-12-15 21:54:06 +01:00
|
|
|
f1 | x
|
|
|
|
---------+---------
|
|
|
|
0 | 0
|
|
|
|
123456 | 246912
|
|
|
|
-123456 | -246912
|
2004-10-04 16:42:48 +02:00
|
|
|
(3 rows)
|
1997-04-05 13:24:54 +02:00
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 * int4 '2' AS x FROM INT4_TBL i;
|
2004-10-04 16:42:48 +02:00
|
|
|
ERROR: integer out of range
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 * int4 '2' AS x FROM INT4_TBL i
|
2004-10-04 16:42:48 +02:00
|
|
|
WHERE abs(f1) < 1073741824;
|
2020-12-15 21:54:06 +01:00
|
|
|
f1 | x
|
|
|
|
---------+---------
|
|
|
|
0 | 0
|
|
|
|
123456 | 246912
|
|
|
|
-123456 | -246912
|
2004-10-04 16:42:48 +02:00
|
|
|
(3 rows)
|
1997-04-05 13:24:54 +02:00
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 + int2 '2' AS x FROM INT4_TBL i;
|
2004-10-04 16:42:48 +02:00
|
|
|
ERROR: integer out of range
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 + int2 '2' AS x FROM INT4_TBL i
|
2004-10-04 16:42:48 +02:00
|
|
|
WHERE f1 < 2147483646;
|
2020-12-15 21:54:06 +01:00
|
|
|
f1 | x
|
|
|
|
-------------+-------------
|
|
|
|
0 | 2
|
|
|
|
123456 | 123458
|
|
|
|
-123456 | -123454
|
|
|
|
-2147483647 | -2147483645
|
2004-10-04 16:42:48 +02:00
|
|
|
(4 rows)
|
1997-04-05 13:24:54 +02:00
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 + int4 '2' AS x FROM INT4_TBL i;
|
2004-10-04 16:42:48 +02:00
|
|
|
ERROR: integer out of range
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 + int4 '2' AS x FROM INT4_TBL i
|
2004-10-04 16:42:48 +02:00
|
|
|
WHERE f1 < 2147483646;
|
2020-12-15 21:54:06 +01:00
|
|
|
f1 | x
|
|
|
|
-------------+-------------
|
|
|
|
0 | 2
|
|
|
|
123456 | 123458
|
|
|
|
-123456 | -123454
|
|
|
|
-2147483647 | -2147483645
|
2004-10-04 16:42:48 +02:00
|
|
|
(4 rows)
|
1997-04-05 13:24:54 +02:00
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 - int2 '2' AS x FROM INT4_TBL i;
|
2004-10-04 16:42:48 +02:00
|
|
|
ERROR: integer out of range
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 - int2 '2' AS x FROM INT4_TBL i
|
2004-10-04 16:42:48 +02:00
|
|
|
WHERE f1 > -2147483647;
|
2020-12-15 21:54:06 +01:00
|
|
|
f1 | x
|
|
|
|
------------+------------
|
|
|
|
0 | -2
|
|
|
|
123456 | 123454
|
|
|
|
-123456 | -123458
|
|
|
|
2147483647 | 2147483645
|
2004-10-04 16:42:48 +02:00
|
|
|
(4 rows)
|
1997-04-05 13:24:54 +02:00
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 - int4 '2' AS x FROM INT4_TBL i;
|
2004-10-04 16:42:48 +02:00
|
|
|
ERROR: integer out of range
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 - int4 '2' AS x FROM INT4_TBL i
|
2004-10-04 16:42:48 +02:00
|
|
|
WHERE f1 > -2147483647;
|
2020-12-15 21:54:06 +01:00
|
|
|
f1 | x
|
|
|
|
------------+------------
|
|
|
|
0 | -2
|
|
|
|
123456 | 123454
|
|
|
|
-123456 | -123458
|
|
|
|
2147483647 | 2147483645
|
2004-10-04 16:42:48 +02:00
|
|
|
(4 rows)
|
1997-04-05 13:24:54 +02:00
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 / int2 '2' AS x FROM INT4_TBL i;
|
|
|
|
f1 | x
|
|
|
|
-------------+-------------
|
|
|
|
0 | 0
|
|
|
|
123456 | 61728
|
|
|
|
-123456 | -61728
|
|
|
|
2147483647 | 1073741823
|
|
|
|
-2147483647 | -1073741823
|
1997-04-05 13:24:54 +02:00
|
|
|
(5 rows)
|
|
|
|
|
2020-12-15 21:54:06 +01:00
|
|
|
SELECT i.f1, i.f1 / int4 '2' AS x FROM INT4_TBL i;
|
|
|
|
f1 | x
|
|
|
|
-------------+-------------
|
|
|
|
0 | 0
|
|
|
|
123456 | 61728
|
|
|
|
-123456 | -61728
|
|
|
|
2147483647 | 1073741823
|
|
|
|
-2147483647 | -1073741823
|
1997-04-05 13:24:54 +02:00
|
|
|
(5 rows)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
--
|
|
|
|
-- more complex expressions
|
|
|
|
--
|
|
|
|
-- variations on unary minus parsing
|
|
|
|
SELECT -2+3 AS one;
|
|
|
|
one
|
|
|
|
-----
|
|
|
|
1
|
1997-09-13 05:20:18 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT 4-2 AS two;
|
|
|
|
two
|
1997-09-13 05:20:18 +02:00
|
|
|
-----
|
2000-01-04 17:19:34 +01:00
|
|
|
2
|
1997-09-13 05:20:18 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT 2- -1 AS three;
|
|
|
|
three
|
|
|
|
-------
|
|
|
|
3
|
1997-09-13 05:20:18 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT 2 - -2 AS four;
|
|
|
|
four
|
|
|
|
------
|
|
|
|
4
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT int2 '2' * int2 '2' = int2 '16' / int2 '4' AS true;
|
|
|
|
true
|
|
|
|
------
|
|
|
|
t
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT int4 '2' * int2 '2' = int2 '16' / int4 '4' AS true;
|
|
|
|
true
|
|
|
|
------
|
|
|
|
t
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT int2 '2' * int4 '2' = int4 '16' / int2 '4' AS true;
|
|
|
|
true
|
|
|
|
------
|
|
|
|
t
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT int4 '1000' < int4 '999' AS false;
|
|
|
|
false
|
|
|
|
-------
|
|
|
|
f
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 AS ten;
|
|
|
|
ten
|
1997-04-05 13:24:54 +02:00
|
|
|
-----
|
2000-01-04 17:19:34 +01:00
|
|
|
10
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT 2 + 2 / 2 AS three;
|
|
|
|
three
|
|
|
|
-------
|
|
|
|
3
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2000-01-04 17:19:34 +01:00
|
|
|
SELECT (2 + 2) / 2 AS two;
|
|
|
|
two
|
1997-04-05 13:24:54 +02:00
|
|
|
-----
|
2000-01-04 17:19:34 +01:00
|
|
|
2
|
1997-04-05 13:24:54 +02:00
|
|
|
(1 row)
|
|
|
|
|
2010-11-20 07:07:04 +01:00
|
|
|
-- corner case
|
|
|
|
SELECT (-1::int4<<31)::text;
|
|
|
|
text
|
|
|
|
-------------
|
|
|
|
-2147483648
|
2010-11-20 04:13:11 +01:00
|
|
|
(1 row)
|
|
|
|
|
2010-11-20 07:07:04 +01:00
|
|
|
SELECT ((-1::int4<<31)+1)::text;
|
2010-11-20 04:13:11 +01:00
|
|
|
text
|
|
|
|
-------------
|
2010-11-20 07:07:04 +01:00
|
|
|
-2147483647
|
2010-11-20 04:13:11 +01:00
|
|
|
(1 row)
|
|
|
|
|
2012-11-19 18:24:25 +01:00
|
|
|
-- check sane handling of INT_MIN overflow cases
|
|
|
|
SELECT (-2147483648)::int4 * (-1)::int4;
|
|
|
|
ERROR: integer out of range
|
|
|
|
SELECT (-2147483648)::int4 / (-1)::int4;
|
|
|
|
ERROR: integer out of range
|
|
|
|
SELECT (-2147483648)::int4 % (-1)::int4;
|
|
|
|
?column?
|
|
|
|
----------
|
|
|
|
0
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT (-2147483648)::int4 * (-1)::int2;
|
|
|
|
ERROR: integer out of range
|
|
|
|
SELECT (-2147483648)::int4 / (-1)::int2;
|
|
|
|
ERROR: integer out of range
|
|
|
|
SELECT (-2147483648)::int4 % (-1)::int2;
|
|
|
|
?column?
|
|
|
|
----------
|
|
|
|
0
|
|
|
|
(1 row)
|
|
|
|
|
2015-03-25 20:54:08 +01:00
|
|
|
-- check rounding when casting from float
|
|
|
|
SELECT x, x::int4 AS int4_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 | int4_value
|
|
|
|
------+------------
|
|
|
|
-2.5 | -2
|
|
|
|
-1.5 | -2
|
|
|
|
-0.5 | 0
|
|
|
|
0 | 0
|
|
|
|
0.5 | 0
|
|
|
|
1.5 | 2
|
|
|
|
2.5 | 2
|
|
|
|
(7 rows)
|
|
|
|
|
2015-07-03 23:04:39 +02:00
|
|
|
-- check rounding when casting from numeric
|
|
|
|
SELECT x, x::int4 AS int4_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 | int4_value
|
|
|
|
------+------------
|
|
|
|
-2.5 | -3
|
|
|
|
-1.5 | -2
|
|
|
|
-0.5 | -1
|
|
|
|
0.0 | 0
|
|
|
|
0.5 | 1
|
|
|
|
1.5 | 2
|
|
|
|
2.5 | 3
|
|
|
|
(7 rows)
|
|
|
|
|
2020-01-25 15:00:59 +01:00
|
|
|
-- test gcd()
|
|
|
|
SELECT a, b, gcd(a, b), gcd(a, -b), gcd(b, a), gcd(-b, a)
|
|
|
|
FROM (VALUES (0::int4, 0::int4),
|
|
|
|
(0::int4, 6410818::int4),
|
|
|
|
(61866666::int4, 6410818::int4),
|
|
|
|
(-61866666::int4, 6410818::int4),
|
|
|
|
((-2147483648)::int4, 1::int4),
|
|
|
|
((-2147483648)::int4, 2147483647::int4),
|
|
|
|
((-2147483648)::int4, 1073741824::int4)) AS v(a, b);
|
|
|
|
a | b | gcd | gcd | gcd | gcd
|
|
|
|
-------------+------------+------------+------------+------------+------------
|
|
|
|
0 | 0 | 0 | 0 | 0 | 0
|
|
|
|
0 | 6410818 | 6410818 | 6410818 | 6410818 | 6410818
|
|
|
|
61866666 | 6410818 | 1466 | 1466 | 1466 | 1466
|
|
|
|
-61866666 | 6410818 | 1466 | 1466 | 1466 | 1466
|
|
|
|
-2147483648 | 1 | 1 | 1 | 1 | 1
|
|
|
|
-2147483648 | 2147483647 | 1 | 1 | 1 | 1
|
|
|
|
-2147483648 | 1073741824 | 1073741824 | 1073741824 | 1073741824 | 1073741824
|
|
|
|
(7 rows)
|
|
|
|
|
|
|
|
SELECT gcd((-2147483648)::int4, 0::int4); -- overflow
|
|
|
|
ERROR: integer out of range
|
|
|
|
SELECT gcd((-2147483648)::int4, (-2147483648)::int4); -- overflow
|
|
|
|
ERROR: integer out of range
|
|
|
|
-- test lcm()
|
|
|
|
SELECT a, b, lcm(a, b), lcm(a, -b), lcm(b, a), lcm(-b, a)
|
|
|
|
FROM (VALUES (0::int4, 0::int4),
|
|
|
|
(0::int4, 42::int4),
|
|
|
|
(42::int4, 42::int4),
|
|
|
|
(330::int4, 462::int4),
|
|
|
|
(-330::int4, 462::int4),
|
|
|
|
((-2147483648)::int4, 0::int4)) AS v(a, b);
|
|
|
|
a | b | lcm | lcm | lcm | lcm
|
|
|
|
-------------+-----+------+------+------+------
|
|
|
|
0 | 0 | 0 | 0 | 0 | 0
|
|
|
|
0 | 42 | 0 | 0 | 0 | 0
|
|
|
|
42 | 42 | 42 | 42 | 42 | 42
|
|
|
|
330 | 462 | 2310 | 2310 | 2310 | 2310
|
|
|
|
-330 | 462 | 2310 | 2310 | 2310 | 2310
|
|
|
|
-2147483648 | 0 | 0 | 0 | 0 | 0
|
|
|
|
(6 rows)
|
|
|
|
|
|
|
|
SELECT lcm((-2147483648)::int4, 1::int4); -- overflow
|
|
|
|
ERROR: integer out of range
|
|
|
|
SELECT lcm(2147483647::int4, 2147483646::int4); -- overflow
|
|
|
|
ERROR: integer out of range
|
2022-12-14 05:40:38 +01:00
|
|
|
-- non-decimal literals
|
|
|
|
SELECT int4 '0b100101';
|
|
|
|
int4
|
|
|
|
------
|
|
|
|
37
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0o273';
|
|
|
|
int4
|
|
|
|
------
|
|
|
|
187
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0x42F';
|
|
|
|
int4
|
|
|
|
------
|
|
|
|
1071
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0b';
|
|
|
|
ERROR: invalid input syntax for type integer: "0b"
|
|
|
|
LINE 1: SELECT int4 '0b';
|
|
|
|
^
|
|
|
|
SELECT int4 '0o';
|
|
|
|
ERROR: invalid input syntax for type integer: "0o"
|
|
|
|
LINE 1: SELECT int4 '0o';
|
|
|
|
^
|
|
|
|
SELECT int4 '0x';
|
|
|
|
ERROR: invalid input syntax for type integer: "0x"
|
|
|
|
LINE 1: SELECT int4 '0x';
|
|
|
|
^
|
|
|
|
-- cases near overflow
|
|
|
|
SELECT int4 '0b1111111111111111111111111111111';
|
|
|
|
int4
|
|
|
|
------------
|
|
|
|
2147483647
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0b10000000000000000000000000000000';
|
|
|
|
ERROR: value "0b10000000000000000000000000000000" is out of range for type integer
|
|
|
|
LINE 1: SELECT int4 '0b10000000000000000000000000000000';
|
|
|
|
^
|
|
|
|
SELECT int4 '0o17777777777';
|
|
|
|
int4
|
|
|
|
------------
|
|
|
|
2147483647
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0o20000000000';
|
|
|
|
ERROR: value "0o20000000000" is out of range for type integer
|
|
|
|
LINE 1: SELECT int4 '0o20000000000';
|
|
|
|
^
|
|
|
|
SELECT int4 '0x7FFFFFFF';
|
|
|
|
int4
|
|
|
|
------------
|
|
|
|
2147483647
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0x80000000';
|
|
|
|
ERROR: value "0x80000000" is out of range for type integer
|
|
|
|
LINE 1: SELECT int4 '0x80000000';
|
|
|
|
^
|
|
|
|
SELECT int4 '-0b10000000000000000000000000000000';
|
|
|
|
int4
|
|
|
|
-------------
|
|
|
|
-2147483648
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '-0b10000000000000000000000000000001';
|
|
|
|
ERROR: value "-0b10000000000000000000000000000001" is out of range for type integer
|
|
|
|
LINE 1: SELECT int4 '-0b10000000000000000000000000000001';
|
|
|
|
^
|
|
|
|
SELECT int4 '-0o20000000000';
|
|
|
|
int4
|
|
|
|
-------------
|
|
|
|
-2147483648
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '-0o20000000001';
|
|
|
|
ERROR: value "-0o20000000001" is out of range for type integer
|
|
|
|
LINE 1: SELECT int4 '-0o20000000001';
|
|
|
|
^
|
|
|
|
SELECT int4 '-0x80000000';
|
|
|
|
int4
|
|
|
|
-------------
|
|
|
|
-2147483648
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '-0x80000001';
|
|
|
|
ERROR: value "-0x80000001" is out of range for type integer
|
|
|
|
LINE 1: SELECT int4 '-0x80000001';
|
|
|
|
^
|
2023-02-04 10:48:51 +01:00
|
|
|
-- underscores
|
|
|
|
SELECT int4 '1_000_000';
|
|
|
|
int4
|
|
|
|
---------
|
|
|
|
1000000
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '1_2_3';
|
|
|
|
int4
|
|
|
|
------
|
|
|
|
123
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0x1EEE_FFFF';
|
|
|
|
int4
|
|
|
|
-----------
|
|
|
|
518979583
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0o2_73';
|
|
|
|
int4
|
|
|
|
------
|
|
|
|
187
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT int4 '0b_10_0101';
|
|
|
|
int4
|
|
|
|
------
|
|
|
|
37
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- error cases
|
|
|
|
SELECT int4 '_100';
|
|
|
|
ERROR: invalid input syntax for type integer: "_100"
|
|
|
|
LINE 1: SELECT int4 '_100';
|
|
|
|
^
|
|
|
|
SELECT int4 '100_';
|
|
|
|
ERROR: invalid input syntax for type integer: "100_"
|
|
|
|
LINE 1: SELECT int4 '100_';
|
|
|
|
^
|
|
|
|
SELECT int4 '100__000';
|
|
|
|
ERROR: invalid input syntax for type integer: "100__000"
|
|
|
|
LINE 1: SELECT int4 '100__000';
|
|
|
|
^
|