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

Unexpected "BIGINT UNSIGNED value is out of range" for minus

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.1(EOL), 10.2(EOL), 10.3(EOL), 10.4(EOL), 10.5, 10.6, 10.7(EOL), 10.11, 11.4, 11.6(EOL)
    • 10.5, 10.6, 10.11, 11.4
    • Data types
    • None

    Description

      This script returns an error unexpectedly:

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (
        c1 BIGINT UNSIGNED,
        c2 BIGINT UNSIGNED
      );
      INSERT INTO t1 VALUES (20030516235329,20000127231341);
      SELECT c2-c1 FROM t1;
      

      ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(`test`.`t1`.`c2` - `test`.`t1`.`c1`)'
      

      It tries to create a BIGIN UNSIGNED column as a result:

      DROP TABLE IF EXISTS t2;
      CREATE TABLE t2 AS SELECT c2-c1 FROM t1 LIMIT 0;
      SHOW CREATE TABLE t2;
      

      +-------+-------------------------------------------------------------------------------------------------------+
      | Table | Create Table                                                                                          |
      +-------+-------------------------------------------------------------------------------------------------------+
      | t2    | CREATE TABLE `t2` (
        `c2-c1` bigint(21) unsigned DEFAULT NULL
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
      +-------+-------------------------------------------------------------------------------------------------------+
      

      The choice beween BIGINT and UNSIGNED BIGINT is controlled by the NO_UNSIGNED_SUBTRACTION flag in sql_mode.
      There should probably be a way to create a DECIMAL column instead.

      Attachments

        Issue Links

          Activity

            The same problem is repeatable with TIMESTAMP columns, which are treated as UNSIGNED:

            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (
              `c1` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
              `c2` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'
            );
            INSERT INTO t1 VALUES ('2003-05-16 23:53:29','2000-01-27 23:13:41');
            SELECT c2-c1 FROM t1;
            

            bar Alexander Barkov added a comment - The same problem is repeatable with TIMESTAMP columns, which are treated as UNSIGNED : DROP TABLE IF EXISTS t1; CREATE TABLE t1 ( `c1` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP , `c2` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ); INSERT INTO t1 VALUES ( '2003-05-16 23:53:29' , '2000-01-27 23:13:41' ); SELECT c2-c1 FROM t1;

            The error reproduced in 11.1.2-MariaDB. Very undesired behavior. Workaround is to use cast function:

            SELECT cast(c2 AS INT)-cast(c1 AS INT) FROM t1;
            

            Badly that

            SELECT cast(c2 AS INT)-c1 FROM t1;
            

            still produces 1690 error.

            balta Tadas Balaišis added a comment - The error reproduced in 11.1.2-MariaDB. Very undesired behavior. Workaround is to use cast function: SELECT cast(c2 AS INT)-cast(c1 AS INT) FROM t1; Badly that SELECT cast(c2 AS INT)-c1 FROM t1; still produces 1690 error.

            People

              bar Alexander Barkov
              bar Alexander Barkov
              Votes:
              1 Vote for this issue
              Watchers:
              4 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.