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

CAST works differently for DECIMAL/INT vs DOUBLE for empty strings

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • 5.5(EOL), 10.0(EOL), 10.1(EOL)
    • 10.1.8
    • OTHER
    • None

    Description

      DECIMAL/INT vs DOUBLE work differently on CAST for empty strings:

      SET sql_mode='STRICT_ALL_TABLES';
      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a INT);
      INSERT INTO t1 VALUES (CAST('' AS INT));

      and

      SET sql_mode='STRICT_ALL_TABLES';
      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a DECIMAL);
      INSERT INTO t1 VALUES (CAST('' AS DECIMAL));

      correctly return

      ERROR 1292 (22007): Truncated incorrect DECIMAL value: ''

      DOUBLE works differently:

      SET sql_mode='STRICT_ALL_TABLES';
      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a DOUBLE);
      INSERT INTO t1 VALUES (CAST('' AS DOUBLE));

      It accepts the value silently:

      Query OK, 1 row affected (0.03 sec)

      This happens because the CAST alone does not produce any warnings for DOUBLE:

      SELECT CAST('' AS DOUBLE);

      and produces a warning for DECIMAL and INT:

      MariaDB [test]> SELECT CAST('' AS INT);
      +-----------------+
      | CAST('' AS INT) |
      +-----------------+
      |               0 |
      +-----------------+
      1 row in set, 1 warning (0.00 sec)
       
      MariaDB [test]> SELECT CAST('' AS INT);
      +-----------------+
      | CAST('' AS INT) |
      +-----------------+
      |               0 |
      +-----------------+
      1 row in set, 1 warning (0.00 sec)
       
      MariaDB [test]> SHOW WARNINGS;
      +---------+------+---------------------------------------+
      | Level   | Code | Message                               |
      +---------+------+---------------------------------------+
      | Warning | 1292 | Truncated incorrect INTEGER value: '' |
      +---------+------+---------------------------------------+
      1 row in set (0.00 sec)

      During a discussion on maria-developers, Sergei and Bar agreed that a warning should be generated for all numeric data types.

      Attachments

        Issue Links

          Activity

            People

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