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

Wrong result for CAST(AVG(bigint_column) AS SIGNED)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 10.1, 10.0
    • Fix Version/s: 10.1.8
    • Component/s: OTHER
    • Labels:
      None

      Description

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (id INT, a BIGINT);
      INSERT INTO t1 VALUES (1,0x7FFFFFFFFFFFFFFF),(2,0x7FFFFFFFFFFFFFFF);
      SELECT id, AVG(a) AS avg, CAST(MIN(a) AS SIGNED) AS cast_min FROM t1 GROUP BY id HAVING avg!=123 ORDER BY id;

      returns

      +------+--------------------------+---------------------+
      | id   | avg                      | cast_min            |
      +------+--------------------------+---------------------+
      |    1 | 9223372036854775807.0000 | 9223372036854775807 |
      |    2 | 9223372036854775807.0000 | 9223372036854775807 |
      +------+--------------------------+---------------------+

      So far so good. Now if I change MIN to AVG:

      SELECT id, AVG(a) AS avg, CAST(AVG(a) AS SIGNED) AS cast_avg FROM t1 GROUP BY id HAVING avg!=123 ORDER BY id;

      it erroneously returns negative numbers:

      +------+--------------------------+----------------------+
      | id   | avg                      | cast_avg             |
      +------+--------------------------+----------------------+
      |    1 | 9223372036854775807.0000 | -9223372036854775808 |
      |    2 | 9223372036854775807.0000 | -9223372036854775808 |
      +------+--------------------------+----------------------+

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: