[MDEV-20548] Unexpected error on CREATE..SELECT HEX(num) Created: 2019-09-10  Updated: 2021-05-10

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

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

Issue Links:
Blocks
blocks MCOL-3762 HEX(<decimal>) gets truncated Stalled
Duplicate
is duplicated by MCOL-3475 HEX() returns incorrect results Closed

 Description   

SET sql_mode=STRICT_ALL_TABLES;
DROP TABLE IF EXISTS t1, t2;
CREATE TABLE t1 (a TINYINT);
INSERT INTO t1 VALUES (-1);
CREATE TABLE t2 AS SELECT a, HEX(a) AS h FROM t1;

ERROR 1406 (22001): Data too long for column 'h' at row 1

Looks wrong.

This script:

CREATE TABLE t2 AS SELECT a, HEX(a) AS h FROM t1 LIMIT 0;
SHOW CREATE TABLE t2;

demonstrates that the data type for the column h is too short:

+-------+--------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                               |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `a` tinyint(4) DEFAULT NULL,
  `h` varchar(8) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------+

It should be varchar(16) to fit hex representation of positive completents for negative numbers:

SELECT a, HEX(a) FROM t1;

+------+------------------+
| a    | HEX(a)           |
+------+------------------+
|   -1 | FFFFFFFFFFFFFFFF |
+------+------------------+


Generated at Thu Feb 08 09:00:19 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.