List:Commits« Previous MessageNext Message »
From:Olav Sandstaa Date:January 28 2011 10:09am
Subject:bzr commit into mysql-trunk branch (olav.sandstaa:3563) Bug#58838
View as plain text  
#At file:///home/olav/mysql/develop/opt-bug58838/ based on revid:tor.didriksen@stripped

 3563 Olav Sandstaa	2011-01-28
      Fix for Bug#58838 Wrong results with HAVING + LIMIT without GROUP BY 
                        when ICP is enabled.
      
      The wrong result was caused due to "loosing" the HAVING condition
      when changing to use a different index than originally selected by the
      optimizer. 
      
      During the optimization phase it was decided to use a secondary index
      for retrieving data from the table. Since ICP was enabled the WHERE
      condition was pushed down to the storage engine. A copy of the WHERE
      condition was kept in the JOINTAB's pre_idx_push_select_cond
      member. In the start of the execution phase it was determined that the
      HAVING condition could be included in the JOINTAB's select
      condition. Then in test_if_skip_sort_order() we (since part of the
      select condition has been pushed down to the storage engine) replace
      the JOINTAB's select condition with the original select condition
      (found in the pre_idx_push_select_cond member). Since
      test_if_skip_sort_order() determines that is will be less costly to
      use the primary key for doing the retrieval of data (due to LIMIT) it
      changes to use the primary key. If it had not changed index then
      test_if_skip_sort_order() would have restored the select condition to
      what it was when it was called. But due to changing index the part of
      the select condition that has been pushed down must also be included
      in the select condition. To achieve this the condition in
      pre_idx_push_select_cond is kept as the JOINTAB's select
      condition. This does not include the HAVING condition and as a result
      the HAVING condition is no longer part of the JOINTAB's select
      condition.
      
      The fix for this problem: When adding the HAVING condition to the
      JOINTAB's select condition also add it to the pre_idx_push_select_cond
      (if this exists).
     @ mysql-test/include/icp_tests.inc
        Test case for Bug#58838 "Wrong results with HAVING + LIMIT without
        GROUP BY when ICP is enabled".
     @ mysql-test/r/innodb_icp.result
        Test case for Bug#58838 "Wrong results with HAVING + LIMIT without
        GROUP BY when ICP is enabled".
     @ mysql-test/r/innodb_icp_all.result
        Test case for Bug#58838 "Wrong results with HAVING + LIMIT without
        GROUP BY when ICP is enabled".
     @ mysql-test/r/innodb_icp_none.result
        Test case for Bug#58838 "Wrong results with HAVING + LIMIT without
        GROUP BY when ICP is enabled".
     @ mysql-test/r/myisam_icp.result
        Test case for Bug#58838 "Wrong results with HAVING + LIMIT without
        GROUP BY when ICP is enabled".
     @ mysql-test/r/myisam_icp_all.result
        Test case for Bug#58838 "Wrong results with HAVING + LIMIT without
        GROUP BY when ICP is enabled".
     @ mysql-test/r/myisam_icp_none.result
        Test case for Bug#58838 "Wrong results with HAVING + LIMIT without
        GROUP BY when ICP is enabled".
     @ sql/sql_select.cc
        When including the HAVING condition as part of the JOINTAB's
        select condition it should also be added to the original
        select condition (stored in pre_idx_push_select_cond) if 
        parts of the select condition has been pushed down to the storage
        engine. This is necessary since the pre_idx_push_select_cond
        is used in test_if_skip_sort_order() and might replace the
        select condition in the cases where test_if_skip_sort_order()
        decides to change to use a different index for retrieving data.

    modified:
      mysql-test/include/icp_tests.inc
      mysql-test/r/innodb_icp.result
      mysql-test/r/innodb_icp_all.result
      mysql-test/r/innodb_icp_none.result
      mysql-test/r/myisam_icp.result
      mysql-test/r/myisam_icp_all.result
      mysql-test/r/myisam_icp_none.result
      sql/sql_select.cc
=== modified file 'mysql-test/include/icp_tests.inc'
--- a/mysql-test/include/icp_tests.inc	2011-01-20 14:57:03 +0000
+++ b/mysql-test/include/icp_tests.inc	2011-01-28 10:09:49 +0000
@@ -724,3 +724,24 @@ WHERE NOT EXISTS
 --echo
 
 DROP TABLE t1,t2;
+
+--echo #
+--echo # Bug#58838 "Wrong results with HAVING + LIMIT without GROUP BY when 
+--echo #            ICP is enabled"
+--echo # 
+
+CREATE TABLE t1 (
+  pk INT NOT NULL,
+  c1 INT,
+  PRIMARY KEY (pk),
+  KEY col_int_key (c1)
+);
+
+INSERT INTO t1 VALUES (1,37),(2,8),(3,-25),(4,NULL),(5,55);
+
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 0;
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 1;
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 2;
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 5;
+
+DROP TABLE t1;

=== modified file 'mysql-test/r/innodb_icp.result'
--- a/mysql-test/r/innodb_icp.result	2011-01-20 14:57:03 +0000
+++ b/mysql-test/r/innodb_icp.result	2011-01-28 10:09:49 +0000
@@ -672,5 +672,28 @@ id	select_type	table	type	possible_keys
 3	DEPENDENT SUBQUERY	t2	unique_subquery	PRIMARY	PRIMARY	4	func	1	Using where; Full scan on NULL key
 
 DROP TABLE t1,t2;
+#
+# Bug#58838 "Wrong results with HAVING + LIMIT without GROUP BY when 
+#            ICP is enabled"
+# 
+CREATE TABLE t1 (
+pk INT NOT NULL,
+c1 INT,
+PRIMARY KEY (pk),
+KEY col_int_key (c1)
+);
+INSERT INTO t1 VALUES (1,37),(2,8),(3,-25),(4,NULL),(5,55);
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 0;
+pk
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 1;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 2;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 5;
+pk
+3
+DROP TABLE t1;
 set default_storage_engine= @save_storage_engine;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/innodb_icp_all.result'
--- a/mysql-test/r/innodb_icp_all.result	2011-01-13 15:37:54 +0000
+++ b/mysql-test/r/innodb_icp_all.result	2011-01-28 10:09:49 +0000
@@ -670,5 +670,28 @@ id	select_type	table	type	possible_keys
 3	DEPENDENT SUBQUERY	t2	unique_subquery	PRIMARY	PRIMARY	4	func	1	Using where; Full scan on NULL key
 
 DROP TABLE t1,t2;
+#
+# Bug#58838 "Wrong results with HAVING + LIMIT without GROUP BY when 
+#            ICP is enabled"
+# 
+CREATE TABLE t1 (
+pk INT NOT NULL,
+c1 INT,
+PRIMARY KEY (pk),
+KEY col_int_key (c1)
+);
+INSERT INTO t1 VALUES (1,37),(2,8),(3,-25),(4,NULL),(5,55);
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 0;
+pk
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 1;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 2;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 5;
+pk
+3
+DROP TABLE t1;
 set default_storage_engine= @save_storage_engine;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/innodb_icp_none.result'
--- a/mysql-test/r/innodb_icp_none.result	2011-01-20 14:57:03 +0000
+++ b/mysql-test/r/innodb_icp_none.result	2011-01-28 10:09:49 +0000
@@ -671,5 +671,28 @@ id	select_type	table	type	possible_keys
 3	DEPENDENT SUBQUERY	t2	unique_subquery	PRIMARY	PRIMARY	4	func	1	Using where; Full scan on NULL key
 
 DROP TABLE t1,t2;
+#
+# Bug#58838 "Wrong results with HAVING + LIMIT without GROUP BY when 
+#            ICP is enabled"
+# 
+CREATE TABLE t1 (
+pk INT NOT NULL,
+c1 INT,
+PRIMARY KEY (pk),
+KEY col_int_key (c1)
+);
+INSERT INTO t1 VALUES (1,37),(2,8),(3,-25),(4,NULL),(5,55);
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 0;
+pk
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 1;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 2;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 5;
+pk
+3
+DROP TABLE t1;
 set default_storage_engine= @save_storage_engine;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/myisam_icp.result'
--- a/mysql-test/r/myisam_icp.result	2011-01-20 14:57:03 +0000
+++ b/mysql-test/r/myisam_icp.result	2011-01-28 10:09:49 +0000
@@ -670,4 +670,27 @@ id	select_type	table	type	possible_keys
 3	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
 
 DROP TABLE t1,t2;
+#
+# Bug#58838 "Wrong results with HAVING + LIMIT without GROUP BY when 
+#            ICP is enabled"
+# 
+CREATE TABLE t1 (
+pk INT NOT NULL,
+c1 INT,
+PRIMARY KEY (pk),
+KEY col_int_key (c1)
+);
+INSERT INTO t1 VALUES (1,37),(2,8),(3,-25),(4,NULL),(5,55);
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 0;
+pk
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 1;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 2;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 5;
+pk
+3
+DROP TABLE t1;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/myisam_icp_all.result'
--- a/mysql-test/r/myisam_icp_all.result	2011-01-13 15:37:54 +0000
+++ b/mysql-test/r/myisam_icp_all.result	2011-01-28 10:09:49 +0000
@@ -668,4 +668,27 @@ id	select_type	table	type	possible_keys
 3	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
 
 DROP TABLE t1,t2;
+#
+# Bug#58838 "Wrong results with HAVING + LIMIT without GROUP BY when 
+#            ICP is enabled"
+# 
+CREATE TABLE t1 (
+pk INT NOT NULL,
+c1 INT,
+PRIMARY KEY (pk),
+KEY col_int_key (c1)
+);
+INSERT INTO t1 VALUES (1,37),(2,8),(3,-25),(4,NULL),(5,55);
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 0;
+pk
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 1;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 2;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 5;
+pk
+3
+DROP TABLE t1;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/myisam_icp_none.result'
--- a/mysql-test/r/myisam_icp_none.result	2011-01-20 14:57:03 +0000
+++ b/mysql-test/r/myisam_icp_none.result	2011-01-28 10:09:49 +0000
@@ -669,4 +669,27 @@ id	select_type	table	type	possible_keys
 3	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
 
 DROP TABLE t1,t2;
+#
+# Bug#58838 "Wrong results with HAVING + LIMIT without GROUP BY when 
+#            ICP is enabled"
+# 
+CREATE TABLE t1 (
+pk INT NOT NULL,
+c1 INT,
+PRIMARY KEY (pk),
+KEY col_int_key (c1)
+);
+INSERT INTO t1 VALUES (1,37),(2,8),(3,-25),(4,NULL),(5,55);
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 0;
+pk
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 1;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 2;
+pk
+3
+SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 5;
+pk
+3
+DROP TABLE t1;
 set optimizer_switch=default;

=== modified file 'sql/sql_select.cc'
--- a/sql/sql_select.cc	2011-01-24 09:02:03 +0000
+++ b/sql/sql_select.cc	2011-01-28 10:09:49 +0000
@@ -1,4 +1,4 @@
-/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
+/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -3223,6 +3223,32 @@ JOIN::exec()
 	DBUG_EXECUTE("where",print_where(curr_table->select->cond,
 					 "select and having",
                                          QT_ORDINARY););
+
+        /*
+          If we have pushed parts of the condition down to the handler
+          then we need to add this to the original pre-ICP select
+          condition since the original select condition may be used in
+          test_if_skip_sort_order(). 
+          Note: here we call make_cond_for_table() a second time in order
+          to get sort_table_cond. An alternative could be to use
+          Item::copy_andor_structure() to make a copy of sort_table_cond.
+        */
+        if (curr_table->pre_idx_push_select_cond)
+        {
+          sort_table_cond= make_cond_for_table(curr_join->tmp_having,
+                                               used_tables, used_tables, 0);
+          if (!sort_table_cond)
+            DBUG_VOID_RETURN;
+          Item* new_pre_idx_push_select_cond= 
+            new Item_cond_and(curr_table->pre_idx_push_select_cond, 
+                              sort_table_cond);
+          if (!new_pre_idx_push_select_cond)
+            DBUG_VOID_RETURN;
+          if (new_pre_idx_push_select_cond->fix_fields(thd, 0))
+            DBUG_VOID_RETURN;
+          curr_table->pre_idx_push_select_cond= new_pre_idx_push_select_cond;
+	}
+
 	curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 						   ~ (table_map) 0,
 						   ~used_tables, 0);


Attachment: [text/bzr-bundle] bzr/olav.sandstaa@oracle.com-20110128100949-eow34ym98zqza53x.bundle
Thread
bzr commit into mysql-trunk branch (olav.sandstaa:3563) Bug#58838Olav Sandstaa28 Jan