Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-9495

Wrong field type for a UNION of a signed and an unsigned INT expression

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 10.0, 10.1, 10.2
    • Fix Version/s: 10.3.1
    • Component/s: OTHER
    • Labels:

      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 |
      +---------------+---------------+

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                bar Alexander Barkov
                Reporter:
                bar Alexander Barkov
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: