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

    • Bug
    • Status: Open (View Workflow)
    • Minor
    • Resolution: Unresolved
    • 10.0, 10.1, 10.2
    • 10.2
    • OTHER
    • 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

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

            Dates

              Created:
              Updated:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.