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

Wrong result set from a query with DISTINCT and HAVING over a SF call

Details

    • Bug
    • Status: Confirmed (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.6.11, 5.5(EOL), 10.0(EOL), 10.1(EOL), 10.2(EOL), 10.3(EOL), 10.4(EOL), 10.6, 10.7(EOL), 10.8(EOL), 10.9(EOL), 10.10(EOL)
    • 10.5, 10.6
    • Optimizer

    Description

      The query with HAVING from the following test case

      SET @@sql_mode = '';
       
      CREATE TABLE series (
        val INT(10) UNSIGNED NOT NULL
      );
      INSERT INTO series VALUES(1);
      CREATE TABLE seq_calls (
        c INT
      );
      INSERT INTO seq_calls VALUES(0);
       
      DELIMITER |;
      CREATE FUNCTION next_seq_value() RETURNS INT
      BEGIN
       DECLARE next_val INT;
       SELECT val INTO next_val FROM series;
       UPDATE series SET val=mod(val + 1, 2);
       UPDATE seq_calls SET c=c+1;
       RETURN next_val;
      END;
      |
      DELIMITER ;|
       
      CREATE TABLE t1 (t INT, u INT, KEY(t));
      INSERT INTO t1 VALUES(10, 10), (11, 11), (12, 12), (12, 13),(14, 15), (15, 16),
                           (16, 17), (17, 18);
      ANALYZE TABLE t1;
       
      SELECT * FROM series, seq_calls;
      SELECT DISTINCT t1.t, next_seq_value() r  FROM t1, t1 P
             WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;
       
      DROP TABLE t1;
      DROP FUNCTION next_seq_value;
      DROP TABLE series, seq_calls;
      

      returns a wrong result set:

      MariaDB [test]> SELECT DISTINCT t1.t, next_seq_value() r  FROM t1, t1 P
          ->        WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;
      +------+------+
      | t    | r    |
      +------+------+
      |   10 |    0 |
      |   11 |    0 |
      |   12 |    0 |
      |   14 |    0 |
      |   15 |    0 |
      |   16 |    0 |
      |   17 |    0 |
      +------+------+
      

      Attachments

        Activity

          igor Igor Babaev created issue -
          igor Igor Babaev made changes -
          Field Original Value New Value
          Summary Wrong result set from a query with DISTINCT and SF call in select list Wrong result set from a query with DISTINCT and HAVING over a SF call
          igor Igor Babaev made changes -
          Description The query with HAVING from the following test case
          {noformat}
          SET @@sql_mode = '';

          CREATE TABLE series (
            val INT(10) UNSIGNED NOT NULL
          );
          INSERT INTO series VALUES(1);
          CREATE TABLE seq_calls (
            c INT
          );
          INSERT INTO seq_calls VALUES(0);

          DELIMITER |;
          CREATE FUNCTION next_seq_value() RETURNS INT
          BEGIN
           DECLARE next_val INT;
           SELECT val INTO next_val FROM series;
           UPDATE series SET val=mod(val + 1, 2);
           UPDATE seq_calls SET c=c+1;
           RETURN next_val;
          END;
          |
          DELIMITER ;|

          CREATE TABLE t1 (t INT, u INT, KEY(t));
          INSERT INTO t1 VALUES(10, 10), (11, 11), (12, 12), (12, 13),(14, 15), (15, 16),
                               (16, 17), (17, 18);
          ANALYZE TABLE t1;

          SELECT * FROM series, seq_calls;
          SELECT DISTINCT t1.t, next_seq_value() r FROM t1, t1 P
                 WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;

          DROP TABLE t1;
          DROP FUNCTION next_seq_value;
          DROP TABLE series, seq_calls;
          {noformat}
          returns a wrong result set:
          {noformat}
          MariaDB [test]> SELECT DISTINCT t1.t, next_seq_value() r FROM t1, t1 P
              -> WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;
          +------+------+
          | t | r |
          +------+------+
          | 10 | 0 |
          | 11 | 0 |
          | 12 | 0 |
          | 14 | 0 |
          | 15 | 0 |
          | 16 | 0 |
          | 17 | 0 |
          +------+------+
          {noformat}
          igor Igor Babaev made changes -
          Affects Version/s 5.5 [ 15800 ]
          Affects Version/s 10.0 [ 16000 ]
          Affects Version/s 10.1 [ 16100 ]
          Affects Version/s 10.2 [ 14601 ]
          igor Igor Babaev made changes -
          Component/s Optimizer [ 10200 ]
          igor Igor Babaev added a comment -

          rmysql-5.6, mysql-5.7 also return wrong result sets.
          mysql-8.0 returns the right result set.

          igor Igor Babaev added a comment - rmysql-5.6, mysql-5.7 also return wrong result sets. mysql-8.0 returns the right result set.
          elenst Elena Stepanova made changes -
          Fix Version/s 10.2 [ 14601 ]
          Assignee Igor Babaev [ igor ]
          serg Sergei Golubchik made changes -
          Workflow MariaDB v3 [ 83341 ] MariaDB v4 [ 140484 ]
          serg Sergei Golubchik made changes -
          Description The query with HAVING from the following test case
          {noformat}
          SET @@sql_mode = '';

          CREATE TABLE series (
            val INT(10) UNSIGNED NOT NULL
          );
          INSERT INTO series VALUES(1);
          CREATE TABLE seq_calls (
            c INT
          );
          INSERT INTO seq_calls VALUES(0);

          DELIMITER |;
          CREATE FUNCTION next_seq_value() RETURNS INT
          BEGIN
           DECLARE next_val INT;
           SELECT val INTO next_val FROM series;
           UPDATE series SET val=mod(val + 1, 2);
           UPDATE seq_calls SET c=c+1;
           RETURN next_val;
          END;
          |
          DELIMITER ;|

          CREATE TABLE t1 (t INT, u INT, KEY(t));
          INSERT INTO t1 VALUES(10, 10), (11, 11), (12, 12), (12, 13),(14, 15), (15, 16),
                               (16, 17), (17, 18);
          ANALYZE TABLE t1;

          SELECT * FROM series, seq_calls;
          SELECT DISTINCT t1.t, next_seq_value() r FROM t1, t1 P
                 WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;

          DROP TABLE t1;
          DROP FUNCTION next_seq_value;
          DROP TABLE series, seq_calls;
          {noformat}
          returns a wrong result set:
          {noformat}
          MariaDB [test]> SELECT DISTINCT t1.t, next_seq_value() r FROM t1, t1 P
              -> WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;
          +------+------+
          | t | r |
          +------+------+
          | 10 | 0 |
          | 11 | 0 |
          | 12 | 0 |
          | 14 | 0 |
          | 15 | 0 |
          | 16 | 0 |
          | 17 | 0 |
          +------+------+
          {noformat}
          The query with HAVING from the following test case
          {code:sql}
          SET @@sql_mode = '';

          CREATE TABLE series (
            val INT(10) UNSIGNED NOT NULL
          );
          INSERT INTO series VALUES(1);
          CREATE TABLE seq_calls (
            c INT
          );
          INSERT INTO seq_calls VALUES(0);

          DELIMITER |;
          CREATE FUNCTION next_seq_value() RETURNS INT
          BEGIN
           DECLARE next_val INT;
           SELECT val INTO next_val FROM series;
           UPDATE series SET val=mod(val + 1, 2);
           UPDATE seq_calls SET c=c+1;
           RETURN next_val;
          END;
          |
          DELIMITER ;|

          CREATE TABLE t1 (t INT, u INT, KEY(t));
          INSERT INTO t1 VALUES(10, 10), (11, 11), (12, 12), (12, 13),(14, 15), (15, 16),
                               (16, 17), (17, 18);
          ANALYZE TABLE t1;

          SELECT * FROM series, seq_calls;
          SELECT DISTINCT t1.t, next_seq_value() r FROM t1, t1 P
                 WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;

          DROP TABLE t1;
          DROP FUNCTION next_seq_value;
          DROP TABLE series, seq_calls;
          {code}
          returns a wrong result set:
          {noformat}
          MariaDB [test]> SELECT DISTINCT t1.t, next_seq_value() r FROM t1, t1 P
              -> WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;
          +------+------+
          | t | r |
          +------+------+
          | 10 | 0 |
          | 11 | 0 |
          | 12 | 0 |
          | 14 | 0 |
          | 15 | 0 |
          | 16 | 0 |
          | 17 | 0 |
          +------+------+
          {noformat}
          alice Alice Sherepa made changes -
          Affects Version/s 10.6.11 [ 28441 ]
          Affects Version/s 10.3 [ 22126 ]
          Affects Version/s 10.4 [ 22408 ]
          Affects Version/s 10.6 [ 24028 ]
          Affects Version/s 10.7 [ 24805 ]
          Affects Version/s 10.8 [ 26121 ]
          Affects Version/s 10.9 [ 26905 ]
          Affects Version/s 10.10 [ 27530 ]
          alice Alice Sherepa made changes -
          Fix Version/s 10.3 [ 22126 ]
          Fix Version/s 10.4 [ 22408 ]
          Fix Version/s 10.5 [ 23123 ]
          Fix Version/s 10.6 [ 24028 ]
          Fix Version/s 10.7 [ 24805 ]
          Fix Version/s 10.8 [ 26121 ]
          Fix Version/s 10.9 [ 26905 ]
          Fix Version/s 10.2 [ 14601 ]
          alice Alice Sherepa added a comment -

          Please check all variations of the query = with/without ORDER BY, etc

          currently 10.3 4e9206736c403206915c09-10.11:

          MariaDB [test]> SELECT  distinct t1.t, next_seq_value() r  FROM t1, t1 P WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;
          Empty set (0,017 sec)
          

          without DISTINCT- 10.3-10.5

          SELECT  t1.t, next_seq_value() r  FROM t1, t1 P WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;
          t	r
          10	0
          11	0
          12	0
          12	0
          12	0
          12	0
          14	0
          15	0
          16	0
          17	0
          

          10.6+:

          SELECT  t1.t, next_seq_value() r  FROM t1, t1 P WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u;
          t	r
          10	1
          12	1
          12	1
          14	1
          16	1
          

          without ORDER BY: 10.3-10.11:

          SELECT  t1.t, next_seq_value() r  FROM t1, t1 P WHERE P.t = t1.t HAVING r = 1;
          t	r
          10	0
          11	0
          12	0
          12	0
          12	0
          12	0
          14	0
          15	0
          16	0
          17	0
           
          SELECT  t1.t, next_seq_value() r  FROM t1, t1 P WHERE P.t = t1.t;
          t	r
          10	1
          11	0
          12	1
          12	0
          12	1
          12	0
          14	1
          15	0
          16	1
          17	0
          
          

          alice Alice Sherepa added a comment - Please check all variations of the query = with/without ORDER BY, etc currently 10.3 4e9206736c403206915c09-10.11: MariaDB [test]> SELECT distinct t1.t, next_seq_value() r FROM t1, t1 P WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u; Empty set (0,017 sec) without DISTINCT- 10.3-10.5 SELECT t1.t, next_seq_value() r FROM t1, t1 P WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u; t r 10 0 11 0 12 0 12 0 12 0 12 0 14 0 15 0 16 0 17 0 10.6+: SELECT t1.t, next_seq_value() r FROM t1, t1 P WHERE P.t = t1.t HAVING r = 1 ORDER BY t1.u; t r 10 1 12 1 12 1 14 1 16 1 without ORDER BY: 10.3-10.11: SELECT t1.t, next_seq_value() r FROM t1, t1 P WHERE P.t = t1.t HAVING r = 1; t r 10 0 11 0 12 0 12 0 12 0 12 0 14 0 15 0 16 0 17 0   SELECT t1.t, next_seq_value() r FROM t1, t1 P WHERE P.t = t1.t; t r 10 1 11 0 12 1 12 0 12 1 12 0 14 1 15 0 16 1 17 0
          alice Alice Sherepa made changes -
          Status Open [ 1 ] Confirmed [ 10101 ]
          alice Alice Sherepa made changes -
          Labels 11.0-sel
          monty Michael Widenius made changes -
          Assignee Igor Babaev [ igor ] Michael Widenius [ monty ]
          julien.fritsch Julien Fritsch made changes -
          Fix Version/s 10.7 [ 24805 ]
          julien.fritsch Julien Fritsch made changes -
          Fix Version/s 10.3 [ 22126 ]
          julien.fritsch Julien Fritsch made changes -
          Fix Version/s 10.8 [ 26121 ]
          julien.fritsch Julien Fritsch made changes -
          Fix Version/s 10.9 [ 26905 ]
          julien.fritsch Julien Fritsch made changes -
          Fix Version/s 10.4 [ 22408 ]

          People

            monty Michael Widenius
            igor Igor Babaev
            Votes:
            0 Vote for this issue
            Watchers:
            5 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.