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

LP:612894 - Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:

      Description

      The following query

      SELECT table1 .`col_varchar_nokey`
      FROM CC table1 STRAIGHT_JOIN ( view_B table2 STRAIGHT_JOIN CC table3 ON table3 .`col_varchar_key` = table2 .`col_varchar_key` ) ON table3 .`col_varchar_nokey` = table2 .`col_varchar_key`
      WHERE EXISTS (
      SELECT DISTINCT `pk`
      FROM CC
      WHERE `pk` < table3 .`pk` ) OR table1 .`col_int_nokey` ;

      Returns 1 extra row when executed with subquery cache, as compared to execution with no subquery cache, maria-5.2 or mysql 5.5.5-m3.

        Gliffy Diagrams

          Attachments

            Activity

            Hide
            sanja Oleksandr Byelkin added a comment -

            Re: Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value
            CREATE TABLE `t1` (
            `col_int_nokey` int(11) NOT NULL
            ) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;
            INSERT INTO `t1` VALUES (2);
            INSERT INTO `t1` VALUES (0);
            INSERT INTO `t1` VALUES (8);
            INSERT INTO `t1` VALUES (5);
            INSERT INTO `t1` VALUES (2);
            INSERT INTO `t1` VALUES (33);
            INSERT INTO `t1` VALUES (5);
            INSERT INTO `t1` VALUES (1);
            INSERT INTO `t1` VALUES (9);
            INSERT INTO `t1` VALUES (1);
            INSERT INTO `t1` VALUES (3);
            INSERT INTO `t1` VALUES (8);
            INSERT INTO `t1` VALUES (231);
            CREATE TABLE `t2` (
            `pk` int(11) NOT NULL AUTO_INCREMENT,
            `col_int_nokey` int(11) NOT NULL,
            `col_int_key` int(11) NOT NULL,
            `col_int_key2` varchar(1) NOT NULL,
            PRIMARY KEY (`pk`),
            KEY `col_int_key` (`col_int_key`),
            KEY `col_int_key2` (`col_int_key2`)
            ) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
            INSERT INTO `t2` VALUES (3,6,1,1);
            INSERT INTO `t2` VALUES (4,7,0,20);
            INSERT INTO `t2` VALUES (5,0,1,30);
            INSERT INTO `t2` VALUES (6,97,190,40);

            SELECT t1.col_int_nokey, (select max(t2.col_int_key2) from t2 where
            t1.col_int_nokey) from t1;

            drop table t1, t2;

            Show
            sanja Oleksandr Byelkin added a comment - Re: Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value CREATE TABLE `t1` ( `col_int_nokey` int(11) NOT NULL ) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1; INSERT INTO `t1` VALUES (2); INSERT INTO `t1` VALUES (0); INSERT INTO `t1` VALUES (8); INSERT INTO `t1` VALUES (5); INSERT INTO `t1` VALUES (2); INSERT INTO `t1` VALUES (33); INSERT INTO `t1` VALUES (5); INSERT INTO `t1` VALUES (1); INSERT INTO `t1` VALUES (9); INSERT INTO `t1` VALUES (1); INSERT INTO `t1` VALUES (3); INSERT INTO `t1` VALUES (8); INSERT INTO `t1` VALUES (231); CREATE TABLE `t2` ( `pk` int(11) NOT NULL AUTO_INCREMENT, `col_int_nokey` int(11) NOT NULL, `col_int_key` int(11) NOT NULL, `col_int_key2` varchar(1) NOT NULL, PRIMARY KEY (`pk`), KEY `col_int_key` (`col_int_key`), KEY `col_int_key2` (`col_int_key2`) ) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1; INSERT INTO `t2` VALUES (3,6,1,1); INSERT INTO `t2` VALUES (4,7,0,20); INSERT INTO `t2` VALUES (5,0,1,30); INSERT INTO `t2` VALUES (6,97,190,40); SELECT t1.col_int_nokey, (select max(t2.col_int_key2) from t2 where t1.col_int_nokey) from t1; drop table t1, t2;
            Hide
            sanja Oleksandr Byelkin added a comment -

            Re: Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value
            CREATE TABLE `t1` (
            `pk` int(11) NOT NULL AUTO_INCREMENT,
            `col_int_nokey` int(11) NOT NULL,
            `col_int_key` int(11) NOT NULL,
            `col_varchar_key` varchar(1) NOT NULL,
            `col_varchar_nokey` varchar(1) NOT NULL,
            PRIMARY KEY (`pk`),
            KEY `col_int_key` (`col_int_key`),
            KEY `col_varchar_key` (`col_varchar_key`,`col_int_key`)
            ) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;
            INSERT INTO `t1` VALUES (13,2,8,'s','s');
            INSERT INTO `t1` VALUES (15,0,6,'p','p');
            INSERT INTO `t1` VALUES (16,8,7,'z','z');
            INSERT INTO `t1` VALUES (19,5,7,'h','h');
            INSERT INTO `t1` VALUES (21,2,9,'v','v');
            INSERT INTO `t1` VALUES (22,33,142,'b','b');
            INSERT INTO `t1` VALUES (23,5,3,'y','y');
            INSERT INTO `t1` VALUES (24,1,0,'v','v');
            INSERT INTO `t1` VALUES (25,9,3,'m','m');
            INSERT INTO `t1` VALUES (26,1,5,'z','z');
            INSERT INTO `t1` VALUES (27,3,9,'n','n');
            INSERT INTO `t1` VALUES (28,8,1,'d','d');
            INSERT INTO `t1` VALUES (29,231,107,'a','a');
            CREATE TABLE `t2` (
            `pk` int(11) NOT NULL AUTO_INCREMENT,
            `col_int_nokey` int(11) NOT NULL,
            `col_int_key` int(11) NOT NULL,
            `col_varchar_key` varchar(1) NOT NULL,
            `col_varchar_nokey` varchar(1) NOT NULL,
            PRIMARY KEY (`pk`),
            KEY `col_int_key` (`col_int_key`),
            KEY `col_varchar_key` (`col_varchar_key`,`col_int_key`)
            ) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
            INSERT INTO `t2` VALUES (3,6,1,'o','o');
            INSERT INTO `t2` VALUES (4,7,0,'g','g');
            INSERT INTO `t2` VALUES (5,0,1,'v','v');
            INSERT INTO `t2` VALUES (6,97,190,'m','m');

            SELECT table1 .`col_int_nokey`,(
            SELECT MIN( SUBQUERY1_t1 .`col_int_key` )
            FROM t2 SUBQUERY1_t1 JOIN ( t2 SUBQUERY1_t2 JOIN t1 SUBQUERY1_t3 ON SUBQUERY1_t2 .`pk` ) ON SUBQUERY1_t3 .`col_varchar_nokey` = SUBQUERY1_t2 .`col_varchar_key`
            WHERE table1 .`col_int_nokey` )
            FROM t1 table1 ;

            SELECT MIN( SUBQUERY1_t1 .`col_int_key` )
            FROM t2 SUBQUERY1_t1 JOIN ( t2 SUBQUERY1_t2 JOIN t1 SUBQUERY1_t3 ON SUBQUERY1_t2 .`pk` ) ON SUBQUERY1_t3 .`col_varchar_nokey` = SUBQUERY1_t2 .`col_varchar_key`
            WHERE rand()*0 != 0;

            select rand()*0 != 0;

            drop table t1, t2;

            Show
            sanja Oleksandr Byelkin added a comment - Re: Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value CREATE TABLE `t1` ( `pk` int(11) NOT NULL AUTO_INCREMENT, `col_int_nokey` int(11) NOT NULL, `col_int_key` int(11) NOT NULL, `col_varchar_key` varchar(1) NOT NULL, `col_varchar_nokey` varchar(1) NOT NULL, PRIMARY KEY (`pk`), KEY `col_int_key` (`col_int_key`), KEY `col_varchar_key` (`col_varchar_key`,`col_int_key`) ) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1; INSERT INTO `t1` VALUES (13,2,8,'s','s'); INSERT INTO `t1` VALUES (15,0,6,'p','p'); INSERT INTO `t1` VALUES (16,8,7,'z','z'); INSERT INTO `t1` VALUES (19,5,7,'h','h'); INSERT INTO `t1` VALUES (21,2,9,'v','v'); INSERT INTO `t1` VALUES (22,33,142,'b','b'); INSERT INTO `t1` VALUES (23,5,3,'y','y'); INSERT INTO `t1` VALUES (24,1,0,'v','v'); INSERT INTO `t1` VALUES (25,9,3,'m','m'); INSERT INTO `t1` VALUES (26,1,5,'z','z'); INSERT INTO `t1` VALUES (27,3,9,'n','n'); INSERT INTO `t1` VALUES (28,8,1,'d','d'); INSERT INTO `t1` VALUES (29,231,107,'a','a'); CREATE TABLE `t2` ( `pk` int(11) NOT NULL AUTO_INCREMENT, `col_int_nokey` int(11) NOT NULL, `col_int_key` int(11) NOT NULL, `col_varchar_key` varchar(1) NOT NULL, `col_varchar_nokey` varchar(1) NOT NULL, PRIMARY KEY (`pk`), KEY `col_int_key` (`col_int_key`), KEY `col_varchar_key` (`col_varchar_key`,`col_int_key`) ) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1; INSERT INTO `t2` VALUES (3,6,1,'o','o'); INSERT INTO `t2` VALUES (4,7,0,'g','g'); INSERT INTO `t2` VALUES (5,0,1,'v','v'); INSERT INTO `t2` VALUES (6,97,190,'m','m'); SELECT table1 .`col_int_nokey`,( SELECT MIN( SUBQUERY1_t1 .`col_int_key` ) FROM t2 SUBQUERY1_t1 JOIN ( t2 SUBQUERY1_t2 JOIN t1 SUBQUERY1_t3 ON SUBQUERY1_t2 .`pk` ) ON SUBQUERY1_t3 .`col_varchar_nokey` = SUBQUERY1_t2 .`col_varchar_key` WHERE table1 .`col_int_nokey` ) FROM t1 table1 ; SELECT MIN( SUBQUERY1_t1 .`col_int_key` ) FROM t2 SUBQUERY1_t1 JOIN ( t2 SUBQUERY1_t2 JOIN t1 SUBQUERY1_t3 ON SUBQUERY1_t2 .`pk` ) ON SUBQUERY1_t3 .`col_varchar_nokey` = SUBQUERY1_t2 .`col_varchar_key` WHERE rand()*0 != 0; select rand()*0 != 0; drop table t1, t2;
            Hide
            monty Michael Widenius added a comment -

            Re: Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value
            The bug is related to how we mark items when there is no rows in a group.
            The following query is related to this:

            CREATE TABLE t1 (a int(11) NOT NULL);
            INSERT INTO t1 VALUES (1),(2);
            CREATE TABLE t2 (
            key_col int(11) NOT NULL,
            KEY (key_col)
            );
            INSERT INTO t2 VALUES (1),(2);

            select min(t2.key_col) from t1,t2 where t1.a=1;
            > 1
            select min(t2.key_col) from t1,t2 where t1.a > 1000;
            > NULL
            select min(t2.key_col)+1 from t1,t2 where t1.a> 1000;
            > 2

            The last query should return NULL

            Show
            monty Michael Widenius added a comment - Re: Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value The bug is related to how we mark items when there is no rows in a group. The following query is related to this: CREATE TABLE t1 (a int(11) NOT NULL); INSERT INTO t1 VALUES (1),(2); CREATE TABLE t2 ( key_col int(11) NOT NULL, KEY (key_col) ); INSERT INTO t2 VALUES (1),(2); select min(t2.key_col) from t1,t2 where t1.a=1; > 1 select min(t2.key_col) from t1,t2 where t1.a > 1000; > NULL select min(t2.key_col)+1 from t1,t2 where t1.a> 1000; > 2 The last query should return NULL
            Hide
            philipstoev Philip Stoev added a comment -

            Re: Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value
            Sanja, what is the situation with the first query from this bug report and the first test case? They do not involve MIN and MAX and COUNT is only used for convenience in order to reduce the size of the result set.

            Show
            philipstoev Philip Stoev added a comment - Re: Some aggregate functions (such as MIN MAX) work incorrectly in subqueries after getting NULL value Sanja, what is the situation with the first query from this bug report and the first test case? They do not involve MIN and MAX and COUNT is only used for convenience in order to reduce the size of the result set.
            Hide
            ratzpo Rasmus Johansson added a comment -

            Launchpad bug id: 612894

            Show
            ratzpo Rasmus Johansson added a comment - Launchpad bug id: 612894

              People

              • Assignee:
                sanja Oleksandr Byelkin
                Reporter:
                philipstoev Philip Stoev
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: