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

CAST(string_expr AS SIGNED) does not truncate huge numbers well

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open (View Workflow)
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 10.0, 10.1, 10.2
    • Fix Version/s: 10.2
    • Component/s: OTHER
    • 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.

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:

                Git Integration