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

CAST(double AS SIGNED) returns bad results with prepared statements

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Minor
    • Resolution: Fixed
    • 5.5.36, 10.0.8
    • N/A
    • Data types
    • None
    • Not for Release Notes

    Description

      This SQL script:

      PREPARE stmt FROM 'SELECT CAST(? AS SIGNED),CAST(1e20 AS SIGNED)';
      SET @a=1e20;
      EXECUTE stmt USING @a;

      returns different results for the same number when used
      as a prepared statement parameter vs passing it directly:

      +----------------------+----------------------+
      | CAST(? AS SIGNED)    | CAST(1e20 AS SIGNED) |
      +----------------------+----------------------+
      | -9223372036854775808 |  9223372036854775807 |
      +----------------------+----------------------+
      1 row in set (0.00 sec)

      The value for the parameter is wrong.
      It should be fixed so both CASTs return 9223372036854775807.

      When casting the same double number to UNSIGNED:

      PREPARE stmt FROM 'SELECT CAST(? AS UNSIGNED),CAST(1e20 AS UNSIGNED)';
      SET @a=1e20;
      EXECUTE stmt USING @a;

      it also returns different values:

      +---------------------+------------------------+
      | CAST(? AS UNSIGNED) | CAST(1e20 AS UNSIGNED) |
      +---------------------+------------------------+
      | 9223372036854775808 |    9223372036854775807 |
      +---------------------+------------------------+
      1 row in set, 1 warning (0.00 sec)

      and the warning is wrong:

      mysql> SHOW WARNINGS;
      +-------+------+-------------------------------------------------------------------------+
      | Level | Code | Message                                                                 |
      +-------+------+-------------------------------------------------------------------------+
      | Note  | 1105 | Cast to unsigned converted negative integer to it's positive complement |
      +-------+------+-------------------------------------------------------------------------+

      because there are actually no any negative numbers involved.

      It should be fixed to return the same value for both CASTs.
      The warning should be either removed, or changed to something more
      applicable to this case.

      Btw, the value 9223372036854775807, returned by CAST(1e20 AS UNSIGNED),
      looks wrong. It's the maximum possible signed number.
      It should return the maximum possible unsigned number instead.

      Attachments

        Activity

          People

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