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

Out-of-range errors when CAST(1-2 AS UNSIGNED)

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • 10.1(EOL), 10.2(EOL), 10.3(EOL)
    • 10.3.1
    • OTHER

    Description

      This problem is repeatable in 10.2 with the default sql_mode, as well as in 10.1 with sql_mode=STRICT_ALL_TABLES.

      It's very similar to MDEV-12849.

      Cast -1 as UNSIGNED returns 18446744073709551615. This script proves that:

      SELECT CAST(1-2 AS UNSIGNED);
      

      +-----------------------+
      | CAST(1-2 AS UNSIGNED) |
      +-----------------------+
      |  18446744073709551615 |
      +-----------------------+
      

      Now I want to create a table from the result of the same SELECT query. It unexpectedly returns an error:

      CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED);
      

      ERROR 1264 (22003): Out of range value for column 'CAST(1-2 AS UNSIGNED)' at row 1
      

      Adding LIMIT 0 helps to figure out what's going on:

      CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED), CAST(-1 AS UNSIGNED) LIMIT 0;
      DESCRIBE t1;
      

      +-----------------------+-----------------+------+-----+---------+-------+
      | Field                 | Type            | Null | Key | Default | Extra |
      +-----------------------+-----------------+------+-----+---------+-------+
      | CAST(1-2 AS UNSIGNED) | int(3) unsigned | NO   |     | NULL    |       |
      | CAST(-1 AS UNSIGNED)  | int(2) unsigned | NO   |     | NULL    |       |
      +-----------------------+-----------------+------+-----+---------+-------+
      

      Notice, the column type is wrong. The expected column type should be BIGINT for both columns, to be able to store 18446744073709551615.

      Attachments

        Issue Links

          Activity

            bar Alexander Barkov created issue -
            bar Alexander Barkov made changes -
            Field Original Value New Value
            bar Alexander Barkov made changes -
            Summary Out-of-range errors when CREATE..SELECT CAST(0xFFFFFFFF AS SIGNED) ut-of-range errors when CAST(1-2 AS UNSIGNED)
            bar Alexander Barkov made changes -
            Summary ut-of-range errors when CAST(1-2 AS UNSIGNED) Out-of-range errors when CAST(1-2 AS UNSIGNED)
            bar Alexander Barkov made changes -
            Description This problem is repeatable in {{10.2}} with the default {{sql_mode}}, as well as in {{10.1}} with {{sql_mode=STRICT_ALL_TABLES}}.

            It's very similar to MDEV-12849.

            This query erroneously returns an error:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(0xFFFFFFFF AS SIGNED);
            {code}
            {noformat}
            ERROR 1264 (22003): Out of range value for column 'CAST(0xFFFFFFFF AS SIGNED)' at row 1
            {noformat}

            This query with {{LIMIT 0}} shows what's going on:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(0xFFFFFFFF AS SIGNED) LIMIT 0;
            SHOW CREATE TABLE t1;
            {code}
            {noformat}
            +-------+-----------------------------------------------------------------------------------------------------------+
            | Table | Create Table |
            +-------+-----------------------------------------------------------------------------------------------------------+
            | t1 | CREATE TABLE `t1` (
              `CAST(0xFFFFFFFF AS SIGNED)` int(4) NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
            +-------+-----------------------------------------------------------------------------------------------------------+
            {noformat}

            Notice, a column of a wrong type is created. It should create a {{BIGINT}} column, to be able to store the value {{4294967295}}.
            This problem is repeatable in {{10.2}} with the default {{sql_mode}}, as well as in {{10.1}} with {{sql_mode=STRICT_ALL_TABLES}}.

            It's very similar to MDEV-12849.

            Cast {{-1}} as {{UNSIGNED}} returns {{18446744073709551615}}. This script proves that:
            {code:sql}
            SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            +-----------------------+
            | CAST(1-2 AS UNSIGNED) |
            +-----------------------+
            | 18446744073709551615 |
            +-----------------------+
            {noformat}


            Now I want to create a table from the result of the same {{SELECT}} query. It unexpectedly returns an error:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            ERROR 1264 (22003): Out of range value for column 'CAST(1-2 AS UNSIGNED)' at row 1
            {noformat}

            Adding {{LIMIT 0}} helps to figure out what's going on:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED) LIMIT 0;
            DESCRIBE t1;
            {code}
            {noformat}
            +-----------------------+-----------------+------+-----+---------+-------+
            | Field | Type | Null | Key | Default | Extra |
            +-----------------------+-----------------+------+-----+---------+-------+
            | CAST(1-2 AS UNSIGNED) | int(3) unsigned | NO | | NULL | |
            +-----------------------+-----------------+------+-----+---------+-------+
            {noformat}
            Notice, the column type is wrong. The expected column type should be {{BIGINT}} to be able to store {{18446744073709551615}}.
            bar Alexander Barkov made changes -
            Description This problem is repeatable in {{10.2}} with the default {{sql_mode}}, as well as in {{10.1}} with {{sql_mode=STRICT_ALL_TABLES}}.

            It's very similar to MDEV-12849.

            Cast {{-1}} as {{UNSIGNED}} returns {{18446744073709551615}}. This script proves that:
            {code:sql}
            SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            +-----------------------+
            | CAST(1-2 AS UNSIGNED) |
            +-----------------------+
            | 18446744073709551615 |
            +-----------------------+
            {noformat}


            Now I want to create a table from the result of the same {{SELECT}} query. It unexpectedly returns an error:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            ERROR 1264 (22003): Out of range value for column 'CAST(1-2 AS UNSIGNED)' at row 1
            {noformat}

            Adding {{LIMIT 0}} helps to figure out what's going on:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED) LIMIT 0;
            DESCRIBE t1;
            {code}
            {noformat}
            +-----------------------+-----------------+------+-----+---------+-------+
            | Field | Type | Null | Key | Default | Extra |
            +-----------------------+-----------------+------+-----+---------+-------+
            | CAST(1-2 AS UNSIGNED) | int(3) unsigned | NO | | NULL | |
            +-----------------------+-----------------+------+-----+---------+-------+
            {noformat}
            Notice, the column type is wrong. The expected column type should be {{BIGINT}} to be able to store {{18446744073709551615}}.
            This problem is repeatable in {{10.2}} with the default {{sql_mode}}, as well as in {{10.1}} with {{sql_mode=STRICT_ALL_TABLES}}.

            It's very similar to MDEV-12849.

            Cast {{-1}} as {{UNSIGNED}} returns {{18446744073709551615}}. This script proves that:
            {code:sql}
            SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            +-----------------------+
            | CAST(1-2 AS UNSIGNED) |
            +-----------------------+
            | 18446744073709551615 |
            +-----------------------+
            {noformat}


            Now I want to create a table from the result of the same {{SELECT}} query. It unexpectedly returns an error:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            ERROR 1264 (22003): Out of range value for column 'CAST(1-2 AS UNSIGNED)' at row 1
            {noformat}

            Adding {{LIMIT 0}} helps to figure out what's going on:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED), CAST(-1 AS UNSIGNED) LIMIT 0;
            DESCRIBE t1;
            {code}
            {noformat}
            +-----------------------+-----------------+------+-----+---------+-------+
            | Field | Type | Null | Key | Default | Extra |
            +-----------------------+-----------------+------+-----+---------+-------+
            | CAST(1-2 AS UNSIGNED) | int(3) unsigned | NO | | NULL | |
            | CAST(-1 AS UNSIGNED) | int(2) unsigned | NO | | NULL | |
            +-----------------------+-----------------+------+-----+---------+-------+
            {noformat}
            Notice, the column type is wrong. The expected column type should be {{BIGINT}} to be able to store {{18446744073709551615}}.
            bar Alexander Barkov made changes -
            Description This problem is repeatable in {{10.2}} with the default {{sql_mode}}, as well as in {{10.1}} with {{sql_mode=STRICT_ALL_TABLES}}.

            It's very similar to MDEV-12849.

            Cast {{-1}} as {{UNSIGNED}} returns {{18446744073709551615}}. This script proves that:
            {code:sql}
            SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            +-----------------------+
            | CAST(1-2 AS UNSIGNED) |
            +-----------------------+
            | 18446744073709551615 |
            +-----------------------+
            {noformat}


            Now I want to create a table from the result of the same {{SELECT}} query. It unexpectedly returns an error:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            ERROR 1264 (22003): Out of range value for column 'CAST(1-2 AS UNSIGNED)' at row 1
            {noformat}

            Adding {{LIMIT 0}} helps to figure out what's going on:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED), CAST(-1 AS UNSIGNED) LIMIT 0;
            DESCRIBE t1;
            {code}
            {noformat}
            +-----------------------+-----------------+------+-----+---------+-------+
            | Field | Type | Null | Key | Default | Extra |
            +-----------------------+-----------------+------+-----+---------+-------+
            | CAST(1-2 AS UNSIGNED) | int(3) unsigned | NO | | NULL | |
            | CAST(-1 AS UNSIGNED) | int(2) unsigned | NO | | NULL | |
            +-----------------------+-----------------+------+-----+---------+-------+
            {noformat}
            Notice, the column type is wrong. The expected column type should be {{BIGINT}} to be able to store {{18446744073709551615}}.
            This problem is repeatable in {{10.2}} with the default {{sql_mode}}, as well as in {{10.1}} with {{sql_mode=STRICT_ALL_TABLES}}.

            It's very similar to MDEV-12849.

            Cast {{-1}} as {{UNSIGNED}} returns {{18446744073709551615}}. This script proves that:
            {code:sql}
            SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            +-----------------------+
            | CAST(1-2 AS UNSIGNED) |
            +-----------------------+
            | 18446744073709551615 |
            +-----------------------+
            {noformat}


            Now I want to create a table from the result of the same {{SELECT}} query. It unexpectedly returns an error:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED);
            {code}
            {noformat}
            ERROR 1264 (22003): Out of range value for column 'CAST(1-2 AS UNSIGNED)' at row 1
            {noformat}

            Adding {{LIMIT 0}} helps to figure out what's going on:
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT CAST(1-2 AS UNSIGNED), CAST(-1 AS UNSIGNED) LIMIT 0;
            DESCRIBE t1;
            {code}
            {noformat}
            +-----------------------+-----------------+------+-----+---------+-------+
            | Field | Type | Null | Key | Default | Extra |
            +-----------------------+-----------------+------+-----+---------+-------+
            | CAST(1-2 AS UNSIGNED) | int(3) unsigned | NO | | NULL | |
            | CAST(-1 AS UNSIGNED) | int(2) unsigned | NO | | NULL | |
            +-----------------------+-----------------+------+-----+---------+-------+
            {noformat}
            Notice, the column type is wrong. The expected column type should be {{BIGINT}} for both columns, to be able to store {{18446744073709551615}}.
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            Comment [ The same problem is repeatable with {{CAST('-1' AS UNSIGNED)}}:

            {code:sql}
            CREATE TABLE t1 AS SELECT CAST('-1' AS UNSIGNED);
            {code}
            {noformat}
            ERROR 1264 (22003): Out of range value for column 'CAST('-1' AS UNSIGNED)' at row 1
            {noformat}

            {code:sql}
            CREATE TABLE t1 AS SELECT CAST('-1' AS UNSIGNED) LIMIT 0;
            SHOW CREATE TABLE t1;
            {code}
            {noformat}
            +-------+----------------------------------------------------------------------------------------------------------------+
            | Table | Create Table |
            +-------+----------------------------------------------------------------------------------------------------------------+
            | t1 | CREATE TABLE `t1` (
              `CAST('-1' AS UNSIGNED)` int(2) unsigned NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
            +-------+----------------------------------------------------------------------------------------------------------------+
            {noformat}
            Notice, {{int(2)}} is wrong. The expected type is {{BIGINT(20)}}.
            ]

            Pushed into bb-10.2-ext

            bar Alexander Barkov added a comment - Pushed into bb-10.2-ext
            bar Alexander Barkov made changes -
            issue.field.resolutiondate 2017-05-23 08:50:27.0 2017-05-23 08:50:27.787
            bar Alexander Barkov made changes -
            Fix Version/s 10.3.1 [ 22532 ]
            Fix Version/s 10.3 [ 22126 ]
            Resolution Fixed [ 1 ]
            Status Open [ 1 ] Closed [ 6 ]
            bar Alexander Barkov made changes -
            Labels datatype
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 80861 ] MariaDB v4 [ 152183 ]

            People

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

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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