[Commits] 898320f640b02308995847532a2bc6aac446bd67 Fixed bug mdev-10884. If a materialized derived table / view is specified by a unit with SELECTs containing ORDER BY ... LIMIT then condition pushdown cannot be done for these SELECTs. If a materialized derived table / view is specified by a unit containing global ORDER BY ... LIMIT then condition pushdown cannot be done for this unit.

Igor Babaev igor at askmonty.org
Mon Sep 26 03:28:50 EEST 2016


commit 898320f640b02308995847532a2bc6aac446bd67
Author: Igor Babaev <igor at askmonty.org>
Commit: Igor Babaev <igor at askmonty.org>

    Fixed bug mdev-10884.
    If a materialized derived table / view is specified by a unit
    with SELECTs containing ORDER BY ... LIMIT then condition pushdown
    cannot be done for these SELECTs.
    If a materialized derived table / view is specified by a unit
    containing global ORDER BY ... LIMIT then condition pushdown
    cannot be done for this unit.
---
 mysql-test/r/derived_cond_pushdown.result |   51 +++++++++++++++++++++++++++++
 mysql-test/t/derived_cond_pushdown.test   |   28 ++++++++++++++++
 sql/sql_derived.cc                        |   26 ++++++++------
 sql/sql_lex.h                             |    2 +-
 4 files changed, 95 insertions(+), 12 deletions(-)

diff --git a/mysql-test/r/derived_cond_pushdown.result b/mysql-test/r/derived_cond_pushdown.result
index 91c6e62..3922a7d 100644
--- a/mysql-test/r/derived_cond_pushdown.result
+++ b/mysql-test/r/derived_cond_pushdown.result
@@ -7032,3 +7032,54 @@ i
 DROP FUNCTION f;
 DROP VIEW v2,v1;
 DROP TABLE t1;
+#
+# MDEV-10884: condition pushdown into derived specified by
+#             1. unit with  SELECT containing ORDER BY ... LIMIT
+#             2. unit containing global ORDER BY ... LIMIT
+#
+create table t1(a int);
+insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+select a from t1 order by a limit 5;
+a
+0
+1
+2
+3
+4
+set statement optimizer_switch='condition_pushdown_for_derived=off' for
+select * from (select a from t1 order by a limit 5) t where t.a not in (1,2,3);
+a
+0
+4
+set statement optimizer_switch='condition_pushdown_for_derived=on' for 
+select * from (select a from t1 order by a limit 5) t where t.a not in (1,2,3);
+a
+0
+4
+select a from t1 where a < 4 union select a from t1 where a > 5
+order by a limit 5;
+a
+0
+1
+2
+3
+6
+set statement optimizer_switch='condition_pushdown_for_derived=off' for
+select * from 
+(select a from t1 where a < 4 union select a from t1 where a > 5
+order by a limit 5) t where t.a not in (2,9);
+a
+0
+1
+3
+6
+set statement optimizer_switch='condition_pushdown_for_derived=on' for 
+select * from 
+(select a from t1 where a < 4 union select a from t1 where a > 5
+order by a limit 5) t where t.a not in (2,9);
+a
+0
+1
+3
+6
+drop table t1;
diff --git a/mysql-test/t/derived_cond_pushdown.test b/mysql-test/t/derived_cond_pushdown.test
index 141c705..9aef1d7 100644
--- a/mysql-test/t/derived_cond_pushdown.test
+++ b/mysql-test/t/derived_cond_pushdown.test
@@ -923,3 +923,31 @@ EXECUTE stmt;
 DROP FUNCTION f;
 DROP VIEW v2,v1;
 DROP TABLE t1;
+
+--echo #
+--echo # MDEV-10884: condition pushdown into derived specified by
+--echo #             1. unit with  SELECT containing ORDER BY ... LIMIT
+--echo #             2. unit containing global ORDER BY ... LIMIT
+--echo #
+
+create table t1(a int);
+insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+select a from t1 order by a limit 5;
+
+set statement optimizer_switch='condition_pushdown_for_derived=off' for
+select * from (select a from t1 order by a limit 5) t where t.a not in (1,2,3);
+set statement optimizer_switch='condition_pushdown_for_derived=on' for 
+select * from (select a from t1 order by a limit 5) t where t.a not in (1,2,3);
+
+select a from t1 where a < 4 union select a from t1 where a > 5
+  order by a limit 5;
+set statement optimizer_switch='condition_pushdown_for_derived=off' for
+select * from 
+(select a from t1 where a < 4 union select a from t1 where a > 5
+ order by a limit 5) t where t.a not in (2,9);
+set statement optimizer_switch='condition_pushdown_for_derived=on' for 
+select * from 
+(select a from t1 where a < 4 union select a from t1 where a > 5
+ order by a limit 5) t where t.a not in (2,9);
+
+drop table t1;
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc
index 85a85b9..0b67b95 100644
--- a/sql/sql_derived.cc
+++ b/sql/sql_derived.cc
@@ -1131,27 +1131,31 @@ bool pushdown_cond_for_derived(THD *thd, Item *cond, TABLE_LIST *derived)
   st_select_lex_unit *unit= derived->get_unit();
   st_select_lex *sl= unit->first_select();
 
+  /* Do not push conditions into constant derived */
+  if (unit->executed)
+    return false;
+
+  /* Do not push conditions into recursive with tables */
+  if (derived->is_recursive_with_table())
+    return false;
+
+  /* Do not push conditions into unit with global ORDER BY ... LIMIT */
+  if (unit->fake_select_lex && unit->fake_select_lex->explicit_limit)
+    return false;
+
   /* Check whether any select of 'unit' allows condition pushdown */
-  bool any_select_allows_cond_pushdown= false;
+  bool some_select_allows_cond_pushdown= false;
   for (; sl; sl= sl->next_select())
   {
     if (sl->cond_pushdown_is_allowed())
     {
-      any_select_allows_cond_pushdown= true;
+      some_select_allows_cond_pushdown= true;
       break;
     }
   }
-  if (!any_select_allows_cond_pushdown)
+  if (!some_select_allows_cond_pushdown)
     return false; 
 
-  /* Do not push conditions into constant derived */
-  if (unit->executed)
-    return false;
-
-  /* Do not push conditions into recursive with tables */
-  if (derived->is_recursive_with_table())
-    return false;
-
   /*
     Build the most restrictive condition extractable from 'cond'
     that can be pushed into the derived table 'derived'.
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index e499cc7..6387b6d 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -1160,7 +1160,7 @@ class st_select_lex: public st_select_lex_node
   bool have_window_funcs() const { return (window_funcs.elements !=0); }
 
   bool cond_pushdown_is_allowed() const
-  { return !have_window_funcs() && !olap; }
+  { return !have_window_funcs() && !olap && !explicit_limit; }
   
 private:
   bool m_non_agg_field_used;


More information about the commits mailing list