[MDEV-14661] CAST from double to UNSIGNED does not work well Created: 2017-12-15  Updated: 2023-04-27

Status: Open
Project: MariaDB Server
Component/s: Data types
Affects Version/s: 10.1, 10.2, 10.3
Fix Version/s: 10.4

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

Issue Links:
Relates
relates to MDEV-14008 Assertion failing: `!is_set() || (m_s... Closed
Epic Link: Data type cleanups

 Description   

This script:

SELECT CAST(10000000000000000000e0 AS UNSIGNED) AS a,
       CAST(10000000000000000000.0 AS UNSIGNED) AS b,
       CAST(10000000000000000000   AS UNSIGNED) AS c;

returns a wrong result, without warnings:

+---------------------+----------------------+----------------------+
| a                   | b                    | c                    |
+---------------------+----------------------+----------------------+
| 9223372036854775807 | 10000000000000000000 | 10000000000000000000 |
+---------------------+----------------------+----------------------+

Notice, the value for a is wrong. It truncated the value to the maximum positive signed value.
The expected result would be to return 10000000000000000000 for all columns.

If I rewrite the script slightly, it still returns the same wrong result for a, but now with a warning:

SELECT CAST(CAST(10000000000000000000 AS DOUBLE) AS UNSIGNED) AS a,
       CAST(CAST(10000000000000000000 AS DECIMAL(30,0)) AS UNSIGNED) AS b,
       CAST(CAST(10000000000000000000 AS UNSIGNED) AS UNSIGNED) AS c;
SHOW WARNINGS;

+--------------------------------------------------------+
| cast(cast(10000000000000000000 as double) as unsigned) |
+--------------------------------------------------------+
|                                    9223372036854775808 |
+--------------------------------------------------------+

+-------+------+-------------------------------------------------------------------------+
| Level | Code | Message                                                                 |
+-------+------+-------------------------------------------------------------------------+
| Note  | 1105 | Cast to unsigned converted negative integer to it's positive complement |
+-------+------+-------------------------------------------------------------------------+

If I rewrite the script using table columns, it also returns the same wrong result, with a warning:

CREATE OR REPLACE TABLE t1 (a DOUBLE, b DECIMAL(30,0), c BIGINT UNSIGNED);
INSERT INTO t1 VALUES (10000000000000000000,10000000000000000000,10000000000000000000);
SELECT CAST(a AS UNSIGNED) AS a,
       CAST(b AS UNSIGNED) AS b,
       CAST(c AS UNSIGNED) AS c
  FROM t1;
SHOW WARNINGS;

+---------------------+----------------------+----------------------+
| a                   | b                    | c                    |
+---------------------+----------------------+----------------------+
| 9223372036854775807 | 10000000000000000000 | 10000000000000000000 |
+---------------------+----------------------+----------------------+

+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1292 | Truncated incorrect INTEGER value: '1e19' |
+---------+------+-------------------------------------------+


Generated at Thu Feb 08 08:15:18 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.