[MDEV-10305] CAST(string_expr AS SIGNED) does not truncate huge numbers well Created: 2016-06-29  Updated: 2016-06-29

Status: Open
Project: MariaDB Server
Component/s: OTHER
Affects Version/s: 10.0, 10.1, 10.2
Fix Version/s: 10.2

Type: Bug Priority: Minor
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: None


 Description   

Implicit CAST from string to signed bigint truncates the number the to maximum possible 64-bit signed value:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a BIGINT);
INSERT INTO t1 VALUES ('99999999999999999999');
SELECT * FROM t1;

+---------------------+
| a                   |
+---------------------+
| 9223372036854775807 |
+---------------------+

So does explicit CAST from DECIMAL to SIGNED:

SELECT CAST(99999999999999999999 AS SIGNED);

+--------------------------------------+
| CAST(99999999999999999999 AS SIGNED) |
+--------------------------------------+
|                  9223372036854775807 |
+--------------------------------------+

So does explicit CAST from DOUBLE to SIGNED:

SELECT CAST(99999999999999999999e0 AS SIGNED);

+----------------------------------------+
| CAST(99999999999999999999e0 AS SIGNED) |
+----------------------------------------+
|                    9223372036854775807 |
+----------------------------------------+

Explicit CAST from string to SIGNED works differently:

SELECT CAST('99999999999999999999' AS SIGNED);

+----------------------------------------+
| CAST('99999999999999999999' AS SIGNED) |
+----------------------------------------+
|                                     -1 |
+----------------------------------------+

This looks wrong. Explicit CAST from string to SIGNED should also return 9223372036854775807.



 Comments   
Comment by Alexander Barkov [ 2016-06-29 ]

Dynamic columns also do not work well:

SELECT COLUMN_GET(COLUMN_CREATE(0,'99999999999999999999'), 0 AS SIGNED);

+------------------------------------------------------------------+
| COLUMN_GET(COLUMN_CREATE(0,'99999999999999999999'), 0 AS SIGNED) |
+------------------------------------------------------------------+
|                                                               -1 |
+------------------------------------------------------------------+

Generated at Thu Feb 08 07:41:12 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.