[MDEV-9495] Wrong field type for a UNION of a signed and an unsigned INT expression Created: 2016-01-30  Updated: 2018-01-16  Resolved: 2017-05-17

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

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: datatype

Issue Links:
Blocks
blocks MDEV-4912 Data type plugin API version 1 Closed
Duplicate
is duplicated by MDEV-14933 Wrong result caused by UNSIGNED colum... Closed
Relates
relates to MDEV-12775 Reuse data type aggregation code for ... Closed

 Description   

CREATE OR REPLACE TABLE t1 (a INT, b INT UNSIGNED);
INSERT INTO t1 VALUES (0x7FFFFFFF,0xFFFFFFFF);
CREATE OR REPLACE TABLE t2 AS SELECT a FROM t1 UNION SELECT b FROM t1;
SHOW CREATE TABLE t2;
SELECT * FROM t2;

returns

+-------+---------------------------------------------------------------------------------------+
| Table | Create Table                                                                          |
+-------+---------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------+

This is wrong, as huge positive unsigned values are converted to negative values:

+------------+
| a          |
+------------+
| 2147483647 |
|         -1 |
+------------+

Note, unlike UNION, hybrid functions aggregate data types correctly:

CREATE OR REPLACE TABLE t1 (a INT, b INT UNSIGNED);
INSERT INTO t1 VALUES (0x7FFFFFFF,0xFFFFFFFF);
CREATE OR REPLACE TABLE t2 AS SELECT COALESCE(a,b), COALESCE(b,a) FROM t1;
SHOW CREATE TABLE t2;
SELECT * FROM t2;

returns

+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                          |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `COALESCE(a,b)` decimal(10,0) DEFAULT NULL,
  `COALESCE(b,a)` decimal(10,0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+

and the values are preserved:

+---------------+---------------+
| COALESCE(a,b) | COALESCE(b,a) |
+---------------+---------------+
|    2147483647 |    4294967295 |
+---------------+---------------+



 Comments   
Comment by Alexander Barkov [ 2017-05-17 ]

This problem was fixed by the patch for MDEV-12775

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