From 54133bab9b44143566ba2d4c21af32fa618b5b1c Mon Sep 17 00:00:00 2001
From: Alexey Botchkov <holyfoot@mysql.com>
Date: Tue, 22 Dec 2009 16:37:21 +0400
Subject: [PATCH 01/11] Bug#46570      test udf fails with valgrind     the
 value obtained by String::c_ptr() method not always     has the ending zero.
 Particularly in this bug the     dlsym() expects zero-ending string.     The
 String::c_ptr_safe() is more correct here.

per-file comments:
  sql/item_func.cc
Bug#46570      test udf fails with valgrind
    c_ptr_safe() used for UDF parameters as the library expects
    zero-ending strings.
---
 sql/item_func.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sql/item_func.cc b/sql/item_func.cc
index 6f14e69e101..c9aaa80d7d0 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -2952,7 +2952,7 @@ udf_handler::fix_fields(THD *thd, Item_result_field *func,
           String *res= arguments[i]->val_str(&buffers[i]);
           if (arguments[i]->null_value)
             continue;
-          f_args.args[i]= (char*) res->c_ptr();
+          f_args.args[i]= (char*) res->c_ptr_safe();
           f_args.lengths[i]= res->length();
           break;
         }

From ff16e34df74385a7517fdfee34ab6f9d0be5c23e Mon Sep 17 00:00:00 2001
From:  <Dao-Gang.Qu@sun.com>
Date: Thu, 24 Dec 2009 11:01:39 +0800
Subject: [PATCH 02/11] Enable rpl_get_master_version_and clock and
 rpl_cross_version as the bug#46931 and bug#43913 have been closed.

---
 mysql-test/suite/rpl/t/disabled.def | 2 --
 1 file changed, 2 deletions(-)

diff --git a/mysql-test/suite/rpl/t/disabled.def b/mysql-test/suite/rpl/t/disabled.def
index 446c233c8a9..a5017b1a02c 100644
--- a/mysql-test/suite/rpl/t/disabled.def
+++ b/mysql-test/suite/rpl/t/disabled.def
@@ -10,7 +10,5 @@
 #
 ##############################################################################
 
-rpl_get_master_version_and_clock:  # Bug#46931 2009-10-17 joro rpl.rpl_get_master_version_and_clock fails
 rpl_row_create_table            : Bug#45576 2009-12-01 joro rpl_row_create_table fails on PB2
-rpl_cross_version               : BUG#43913 2009-10-22 luis rpl_cross_version fails with symptom in described in bug report
 rpl_spec_variables		: BUG#47661 2009-10-27 jasonh rpl_spec_variables fails on PB2 hpux

From 0c4e5f784b191222c871711286f90651e33da046 Mon Sep 17 00:00:00 2001
From:  <Dao-Gang.Qu@sun.com>
Date: Fri, 25 Dec 2009 14:12:49 +0800
Subject: [PATCH 03/11] Backport Bug #43913  rpl_cross_version can't pass on
 conflicts complainig clash with --slave-load-tm

The 'rpl_cross_version' fails on mysql-next-mr-bugfixing as following:
mysqltest: In included file "./include/setup_fake_relay_log.inc": At line 80: query
'select './$_fake_filename-fake.000001\n' into dumpfile '$_fake_relay_index'' failed:
1290: The MySQL server is running with the --secure-file-priv option so it cannot execute
this statement.

To fix the problem by removeing the --secure-file-priv option
for adapting the update of the 'setup_fake_relay_log.inc'.
---
 mysql-test/suite/rpl/t/rpl_cross_version-master.opt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mysql-test/suite/rpl/t/rpl_cross_version-master.opt b/mysql-test/suite/rpl/t/rpl_cross_version-master.opt
index 0ea05290c11..815a8f81d32 100644
--- a/mysql-test/suite/rpl/t/rpl_cross_version-master.opt
+++ b/mysql-test/suite/rpl/t/rpl_cross_version-master.opt
@@ -1 +1 @@
---replicate-same-server-id --relay-log=slave-relay-bin --secure-file-priv=$MYSQL_TMP_DIR
+--replicate-same-server-id --relay-log=slave-relay-bin

From 3dab08f157b33a248dee7037b7a42eb9c06798f6 Mon Sep 17 00:00:00 2001
From: Jorgen Loland <jorgen.loland@sun.com>
Date: Mon, 4 Jan 2010 10:39:42 +0100
Subject: [PATCH 04/11] Bug#48920: COUNT DISTINCT returns 1 for NULL values
 when in a            subquery in the select list

When a dependent subquery with count(distinct <col>) was
evaluated multiple times, the Distinct_Aggregator was reused.
However, the Aggregator was not reset, so when the subquery was
evaluated for the next record in the outer select, old dependent
info was used.

The fix is to clear() the existing aggregator in
Item_sum::set_aggregator(). This ensures that the aggregator is
reevaluated with the new dependent information.
---
 mysql-test/r/subselect3.result | 50 ++++++++++++++++++++++++++++++++++
 mysql-test/t/subselect3.test   | 47 ++++++++++++++++++++++++++++++++
 sql/item_sum.cc                |  7 +++++
 3 files changed, 104 insertions(+)

diff --git a/mysql-test/r/subselect3.result b/mysql-test/r/subselect3.result
index 3a0576768b2..a51cef6833d 100644
--- a/mysql-test/r/subselect3.result
+++ b/mysql-test/r/subselect3.result
@@ -964,3 +964,53 @@ Variable_name	Value
 Handler_read_rnd_next	18
 DROP TABLE t1,t2;
 End of 5.1 tests
+#
+# BUG#48920: COUNT DISTINCT returns 1 for NULL values when in a subquery 
+#            in the select list
+#
+
+CREATE TABLE t1 (
+i int(11) DEFAULT NULL,
+v varchar(1) DEFAULT NULL
+);
+
+INSERT INTO t1 VALUES (8,'v');
+INSERT INTO t1 VALUES (9,'r');
+INSERT INTO t1 VALUES (NULL,'y');
+
+CREATE TABLE t2 (
+i int(11) DEFAULT NULL,
+v varchar(1) DEFAULT NULL,
+KEY i_key (i)
+);
+
+INSERT INTO t2 VALUES (NULL,'r');
+INSERT INTO t2 VALUES (0,'c');
+INSERT INTO t2 VALUES (0,'o');
+INSERT INTO t2 VALUES (2,'v');
+INSERT INTO t2 VALUES (7,'c');
+
+SELECT i, v, (SELECT COUNT(DISTINCT i)
+FROM t1
+WHERE v  = t2.v) as subsel
+FROM t2;
+i	v	subsel
+NULL	r	1
+0	c	0
+0	o	0
+2	v	1
+7	c	0
+
+EXPLAIN EXTENDED
+SELECT i, v, (SELECT COUNT(DISTINCT i)
+FROM t1
+WHERE v  = t2.v) as subsel
+FROM t2;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	
+2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
+Warnings:
+Note	1276	Field or reference 'test.t2.v' of SELECT #2 was resolved in SELECT #1
+Note	1003	select `test`.`t2`.`i` AS `i`,`test`.`t2`.`v` AS `v`,(select count(distinct `test`.`t1`.`i`) AS `COUNT(DISTINCT i)` from `test`.`t1` where (`test`.`t1`.`v` = `test`.`t2`.`v`)) AS `subsel` from `test`.`t2`
+DROP TABLE t1,t2;
+End of 5.6 tests
diff --git a/mysql-test/t/subselect3.test b/mysql-test/t/subselect3.test
index fab0a462157..39c97941031 100644
--- a/mysql-test/t/subselect3.test
+++ b/mysql-test/t/subselect3.test
@@ -794,3 +794,50 @@ SHOW STATUS LIKE '%Handler_read_rnd_next';
 DROP TABLE t1,t2;
 
 --echo End of 5.1 tests
+
+--echo #
+--echo # BUG#48920: COUNT DISTINCT returns 1 for NULL values when in a subquery 
+--echo #            in the select list
+--echo #
+
+--echo 
+CREATE TABLE t1 (
+  i int(11) DEFAULT NULL,
+  v varchar(1) DEFAULT NULL
+);
+
+--echo 
+INSERT INTO t1 VALUES (8,'v');
+INSERT INTO t1 VALUES (9,'r');
+INSERT INTO t1 VALUES (NULL,'y');
+
+--echo 
+CREATE TABLE t2 (
+  i int(11) DEFAULT NULL,
+  v varchar(1) DEFAULT NULL,
+  KEY i_key (i)
+);
+
+--echo 
+INSERT INTO t2 VALUES (NULL,'r');
+INSERT INTO t2 VALUES (0,'c');
+INSERT INTO t2 VALUES (0,'o');
+INSERT INTO t2 VALUES (2,'v');
+INSERT INTO t2 VALUES (7,'c');
+
+--echo 
+SELECT i, v, (SELECT COUNT(DISTINCT i)
+              FROM t1
+              WHERE v  = t2.v) as subsel
+FROM t2;
+
+--echo 
+EXPLAIN EXTENDED
+SELECT i, v, (SELECT COUNT(DISTINCT i)
+              FROM t1
+              WHERE v  = t2.v) as subsel
+FROM t2;
+
+DROP TABLE t1,t2;
+
+--echo End of 5.6 tests
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index c33088e0276..a61c5d59d67 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -578,7 +578,14 @@ int Item_sum::set_aggregator(Aggregator::Aggregator_type aggregator)
 {
   if (aggr)
   {
+    /* 
+      Dependent subselects may be executed multiple times, making
+      set_aggregator to be called multiple times. The aggregator type
+      will be the same, but it needs to be reset so that it is
+      reevaluated with the new dependent data.
+    */
     DBUG_ASSERT(aggregator == aggr->Aggrtype());
+    aggr->clear();
     return FALSE;
   }
   switch (aggregator)

From 284b836cd35fdfd585525622e390a244fc6ffd1f Mon Sep 17 00:00:00 2001
From: Guilhem Bichot <guilhem@mysql.com>
Date: Wed, 6 Jan 2010 11:27:35 +0100
Subject: [PATCH 05/11] fixing wrong indentation in two Makefile.am, which
 prevented the Anjuta IDE from parsing the MySQL tree.

---
 libmysqld/Makefile.am | 4 ++--
 sql/share/Makefile.am | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libmysqld/Makefile.am b/libmysqld/Makefile.am
index c3e3086b446..2dde00ad38a 100644
--- a/libmysqld/Makefile.am
+++ b/libmysqld/Makefile.am
@@ -155,8 +155,8 @@ libmysqld.a:	libmysqld_int.a $(INC_LIB) $(libmysqld_a_DEPENDENCIES) $(storageobj
 if DARWIN_MWCC
 	mwld -lib -o $@ libmysqld_int.a `echo $(INC_LIB) | sort -u` $(storageobjects)
 else
-		-rm -f libmysqld.a
-		if test "$(host_os)" = "netware" ; \
+	-rm -f libmysqld.a
+	if test "$(host_os)" = "netware" ; \
 		then \
 		  $(libmysqld_a_AR) libmysqld.a $(INC_LIB) libmysqld_int.a $(storageobjects); \
 		else \
diff --git a/sql/share/Makefile.am b/sql/share/Makefile.am
index 06b349d5de2..203dbe54254 100644
--- a/sql/share/Makefile.am
+++ b/sql/share/Makefile.am
@@ -43,8 +43,8 @@ install-data-local:
 			        $(DESTDIR)$(pkgdatadir)/$$lang/errmsg.sys; \
 	done
 	$(mkinstalldirs) $(DESTDIR)$(pkgdatadir)/charsets
-		$(INSTALL_DATA) $(srcdir)/errmsg-utf8.txt \
-	        $(DESTDIR)$(pkgdatadir)/errmsg-utf8.txt; \
+	$(INSTALL_DATA) $(srcdir)/errmsg-utf8.txt \
+	$(DESTDIR)$(pkgdatadir)/errmsg-utf8.txt; \
 	$(INSTALL_DATA) $(srcdir)/charsets/README $(DESTDIR)$(pkgdatadir)/charsets/README
 	$(INSTALL_DATA) $(srcdir)/charsets/*.xml $(DESTDIR)$(pkgdatadir)/charsets
 

From 716c1bce835435ef6c16a9306ac3185a5b9d72c0 Mon Sep 17 00:00:00 2001
From: Guilhem Bichot <guilhem@mysql.com>
Date: Wed, 6 Jan 2010 11:54:45 +0100
Subject: [PATCH 06/11] WL#5197 "Move @@engine_condition_pushdown to
 @@optimizer_switch" "set engine_condition_pushdown" is deprecated, engine
 condition pushdown is controlled by a new "set
 optimizer_switch=engine_condition_pushdown=on|off".

---
 mysql-test/r/index_merge_myisam.result        |  18 +--
 mysql-test/r/mysqld--help-notwin.result       |  10 +-
 mysql-test/r/mysqld--help-win.result          |  10 +-
 ...optimizer_switch_eng_cond_pushdown1.result |   5 +
 ...optimizer_switch_eng_cond_pushdown2.result |   5 +
 .../suite/ndb/r/ndb_condition_pushdown.result |  34 ++---
 mysql-test/suite/ndb/r/ndb_gis.result         |   2 +-
 .../suite/ndb/r/ndb_index_unique.result       |   6 +-
 .../suite/ndb/t/ndb_condition_pushdown.test   |  34 ++---
 mysql-test/suite/ndb/t/ndb_gis.test           |   2 +-
 mysql-test/suite/ndb/t/ndb_index_unique.test  |   6 +-
 .../r/engine_condition_pushdown_basic.result  | 122 ++++++++++++++++++
 .../sys_vars/r/optimizer_switch_basic.result  |  26 ++--
 .../t/engine_condition_pushdown_basic.test    |  48 +++++++
 ...mizer_switch_eng_cond_pushdown1-master.opt |   1 +
 .../optimizer_switch_eng_cond_pushdown1.test  |   5 +
 ...mizer_switch_eng_cond_pushdown2-master.opt |   1 +
 .../optimizer_switch_eng_cond_pushdown2.test  |   5 +
 sql/mysql_priv.h                              |  17 ++-
 sql/mysqld.cc                                 |  17 +++
 sql/records.cc                                |   3 +-
 sql/sql_select.cc                             |   6 +-
 sql/sys_vars.cc                               |  39 +++++-
 23 files changed, 334 insertions(+), 88 deletions(-)
 create mode 100644 mysql-test/r/optimizer_switch_eng_cond_pushdown1.result
 create mode 100644 mysql-test/r/optimizer_switch_eng_cond_pushdown2.result
 create mode 100644 mysql-test/t/optimizer_switch_eng_cond_pushdown1-master.opt
 create mode 100644 mysql-test/t/optimizer_switch_eng_cond_pushdown1.test
 create mode 100644 mysql-test/t/optimizer_switch_eng_cond_pushdown2-master.opt
 create mode 100644 mysql-test/t/optimizer_switch_eng_cond_pushdown2.test

diff --git a/mysql-test/r/index_merge_myisam.result b/mysql-test/r/index_merge_myisam.result
index 922f7241102..1be96da5c7d 100644
--- a/mysql-test/r/index_merge_myisam.result
+++ b/mysql-test/r/index_merge_myisam.result
@@ -1419,19 +1419,19 @@ drop table t1;
 #
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 set optimizer_switch='index_merge=off,index_merge_union=off';
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=off,index_merge_union=off,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=off,index_merge_union=off,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 set optimizer_switch='index_merge_union=on';
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=off,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=off,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 set optimizer_switch='default,index_merge_sort_union=off';
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=off,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=off,index_merge_intersection=on,engine_condition_pushdown=on
 set optimizer_switch=4;
 set optimizer_switch=NULL;
 ERROR 42000: Variable 'optimizer_switch' can't be set to the value of 'NULL'
@@ -1457,21 +1457,21 @@ set optimizer_switch=default;
 set optimizer_switch='index_merge=off,index_merge_union=off,default';
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=off,index_merge_union=off,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=off,index_merge_union=off,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 set optimizer_switch=default;
 select @@global.optimizer_switch;
 @@global.optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 set @@global.optimizer_switch=default;
 select @@global.optimizer_switch;
 @@global.optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 #
 # Check index_merge's @@optimizer_switch flags
 #
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 create table t0 (a int);
 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 create table t1 (a int, b int, c int, filler char(100), 
@@ -1581,5 +1581,5 @@ id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 set optimizer_switch=default;
 show variables like 'optimizer_switch';
 Variable_name	Value
-optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 drop table t0, t1;
diff --git a/mysql-test/r/mysqld--help-notwin.result b/mysql-test/r/mysqld--help-notwin.result
index 2ba7df6b7e3..1953e014e4b 100644
--- a/mysql-test/r/mysqld--help-notwin.result
+++ b/mysql-test/r/mysqld--help-notwin.result
@@ -132,7 +132,8 @@ The following options may be given as the first argument:
                       on that value
   --enable-locking    Deprecated option, use --external-locking instead.
   --engine-condition-pushdown 
-                      Push supported query conditions to the storage engine
+                      Push supported query conditions to the storage engine.
+                      Deprecated, use --optimizer-switch instead.
                       (Defaults to on; use --skip-engine-condition-pushdown to disable.)
   --event-scheduler[=name] 
                       Enable the event scheduler. Possible values are ON, OFF,
@@ -413,8 +414,9 @@ The following options may be given as the first argument:
   --optimizer-switch=name 
                       optimizer_switch=option=val[,option=val...], where option
                       is one of {index_merge, index_merge_union,
-                      index_merge_sort_union, index_merge_intersection} and val
-                      is one of {on, off, default}
+                      index_merge_sort_union, index_merge_intersection,
+                      engine_condition_pushdown} and val is one of {on, off,
+                      default}
   --partition[=name]  Enable or disable partition plugin. Possible values are
                       ON, OFF, FORCE (don't start if the plugin fails to load).
   --pid-file=name     Pid file used by safe_mysqld
@@ -853,7 +855,7 @@ old-passwords                       FALSE
 old-style-user-limits               FALSE
 optimizer-prune-level               1
 optimizer-search-depth              62
-optimizer-switch                    index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+optimizer-switch                    index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 partition                           ON
 plugin-dir                          MYSQL_LIBDIR/mysql/plugin
 plugin-load                         (No default value)
diff --git a/mysql-test/r/mysqld--help-win.result b/mysql-test/r/mysqld--help-win.result
index 5627716f6ec..95d3a48a6e1 100644
--- a/mysql-test/r/mysqld--help-win.result
+++ b/mysql-test/r/mysqld--help-win.result
@@ -132,7 +132,8 @@ The following options may be given as the first argument:
                       on that value
   --enable-locking    Deprecated option, use --external-locking instead.
   --engine-condition-pushdown 
-                      Push supported query conditions to the storage engine
+                      Push supported query conditions to the storage engine.
+                      Deprecated, use --optimizer-switch instead.
                       (Defaults to on; use --skip-engine-condition-pushdown to disable.)
   --event-scheduler[=name] 
                       Enable the event scheduler. Possible values are ON, OFF,
@@ -413,8 +414,9 @@ The following options may be given as the first argument:
   --optimizer-switch=name 
                       optimizer_switch=option=val[,option=val...], where option
                       is one of {index_merge, index_merge_union,
-                      index_merge_sort_union, index_merge_intersection} and val
-                      is one of {on, off, default}
+                      index_merge_sort_union, index_merge_intersection,
+                      engine_condition_pushdown} and val is one of {on, off,
+                      default}
   --partition[=name]  Enable or disable partition plugin. Possible values are
                       ON, OFF, FORCE (don't start if the plugin fails to load).
   --pid-file=name     Pid file used by safe_mysqld
@@ -857,7 +859,7 @@ old-passwords                     FALSE
 old-style-user-limits             FALSE
 optimizer-prune-level             1
 optimizer-search-depth            62
-optimizer-switch                  index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+optimizer-switch                  index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 partition                         ON
 plugin-dir                        MYSQL_LIBDIR/plugin
 plugin-load                       (No default value)
diff --git a/mysql-test/r/optimizer_switch_eng_cond_pushdown1.result b/mysql-test/r/optimizer_switch_eng_cond_pushdown1.result
new file mode 100644
index 00000000000..a8313ec246c
--- /dev/null
+++ b/mysql-test/r/optimizer_switch_eng_cond_pushdown1.result
@@ -0,0 +1,5 @@
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+1	1	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
diff --git a/mysql-test/r/optimizer_switch_eng_cond_pushdown2.result b/mysql-test/r/optimizer_switch_eng_cond_pushdown2.result
new file mode 100644
index 00000000000..d7e84c57f42
--- /dev/null
+++ b/mysql-test/r/optimizer_switch_eng_cond_pushdown2.result
@@ -0,0 +1,5 @@
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+0	0	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
diff --git a/mysql-test/suite/ndb/r/ndb_condition_pushdown.result b/mysql-test/suite/ndb/r/ndb_condition_pushdown.result
index 4b6f5031fb2..f2b5b882f10 100644
--- a/mysql-test/suite/ndb/r/ndb_condition_pushdown.result
+++ b/mysql-test/suite/ndb/r/ndb_condition_pushdown.result
@@ -51,8 +51,8 @@ CREATE TABLE  t3 (pk1 int unsigned NOT NULL PRIMARY KEY,   attr1 int unsigned NO
 insert into t3 values (0,0,0,0,"a"),(1,1,9223372036854775803,1,"b"),(2,2,9223372036854775804,2,"c"),(3,3,9223372036854775805,3,"d"),(4,4,9223372036854775806,4,"e"),(5,5,9223372036854775807,5,"f");
 CREATE TABLE  t4 (pk1 int unsigned NOT NULL PRIMARY KEY,   attr1 int unsigned NOT NULL,   attr2 bigint unsigned, attr3 tinyint unsigned,  attr4 VARCHAR(10) , KEY (attr1)) ENGINE=ndbcluster;
 insert into t4 values (0,0,0,0,"a"),(1,1,9223372036854775803,1,"b"),(2,2,9223372036854775804,2,"c"),(3,3,9223372036854775805,3,"d"),(4,4,9223372036854775806,4,"e"),(5,5,9223372036854775807,5,"f");
-set @old_ecpd = @@session.engine_condition_pushdown;
-set engine_condition_pushdown = off;
+set @old_optimizer_switch = @@session.optimizer_switch;
+set optimizer_switch = "engine_condition_pushdown=off";
 select auto from t1 where 
 string = "aaaa" and 
 vstring = "aaaa" and 
@@ -484,7 +484,7 @@ pk1	attr1	attr2	attr3	attr4	pk1	attr1	attr2	attr3	attr4
 2	2	9223372036854775804	2	c	2	2	9223372036854775804	2	c
 3	3	9223372036854775805	3	d	3	3	9223372036854775805	3	d
 4	4	9223372036854775806	4	e	4	4	9223372036854775806	4	e
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 explain
 select auto from t1 where 
 string = "aaaa" and 
@@ -1769,12 +1769,12 @@ id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 create table t5 (a int primary key auto_increment, b tinytext not null) 
 engine = ndb;
 insert into t5 (b) values ('jonas'), ('jensing'), ('johan');
-set engine_condition_pushdown = off;
+set optimizer_switch = "engine_condition_pushdown=off";
 select * from t5 where b like '%jo%' order by a;
 a	b
 1	jonas
 3	johan
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 explain select * from t5 where b like '%jo%';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t5	ALL	NULL	NULL	NULL	NULL	#	Using where
@@ -1782,7 +1782,7 @@ select * from t5 where b like '%jo%' order by a;
 a	b
 1	jonas
 3	johan
-set engine_condition_pushdown = off;
+set optimizer_switch = "engine_condition_pushdown=off";
 select auto from t1 where date_time like '1902-02-02 %' order by auto;
 auto
 2
@@ -1790,7 +1790,7 @@ select auto from t1 where date_time not like '1902-02-02 %' order by auto;
 auto
 3
 4
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 explain select auto from t1 where date_time like '1902-02-02 %';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	#	Using where
@@ -1808,7 +1808,7 @@ drop table t1;
 create table t1 (a int, b varchar(3), primary key using hash(a))
 engine=ndb;
 insert into t1 values (1,'a'), (2,'ab'), (3,'abc');
-set engine_condition_pushdown = off;
+set optimizer_switch = "engine_condition_pushdown=off";
 select * from t1 where b like 'ab';
 a	b
 2	ab
@@ -1821,7 +1821,7 @@ a	b
 select * from t1 where b like 'abc' or b like 'abc';
 a	b
 3	abc
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 select * from t1 where b like 'ab';
 a	b
 2	ab
@@ -1838,7 +1838,7 @@ drop table t1;
 create table t1 (a int, b char(3), primary key using hash(a))
 engine=ndb;
 insert into t1 values (1,'a'), (2,'ab'), (3,'abc');
-set engine_condition_pushdown = off;
+set optimizer_switch = "engine_condition_pushdown=off";
 select * from t1 where b like 'ab';
 a	b
 2	ab
@@ -1851,7 +1851,7 @@ a	b
 select * from t1 where b like 'abc' or b like 'abc';
 a	b
 3	abc
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 select * from t1 where b like 'ab';
 a	b
 2	ab
@@ -1868,11 +1868,11 @@ drop table t1;
 create table  t1 ( fname varchar(255), lname varchar(255) )
 engine=ndbcluster;
 insert into t1 values ("Young","Foo");
-set engine_condition_pushdown = 0;
+set optimizer_switch = "engine_condition_pushdown=off";
 SELECT fname, lname FROM t1 WHERE (fname like 'Y%') or (lname like 'F%');
 fname	lname
 Young	Foo
-set engine_condition_pushdown = 1;
+set optimizer_switch = "engine_condition_pushdown=on";
 SELECT fname, lname FROM t1 WHERE (fname like 'Y%') or (lname like 'F%');
 fname	lname
 Young	Foo
@@ -1880,11 +1880,11 @@ insert into t1 values ("aaa", "aaa");
 insert into t1 values ("bbb", "bbb");
 insert into t1 values ("ccc", "ccc");
 insert into t1 values ("ddd", "ddd");
-set engine_condition_pushdown = 0;
+set optimizer_switch = "engine_condition_pushdown=off";
 SELECT fname, lname FROM t1 WHERE (fname like 'Y%') or (lname like 'F%');
 fname	lname
 Young	Foo
-set engine_condition_pushdown = 1;
+set optimizer_switch = "engine_condition_pushdown=on";
 SELECT fname, lname FROM t1 WHERE (fname like 'Y%') or (lname like 'F%');
 fname	lname
 Young	Foo
@@ -1896,7 +1896,7 @@ insert into t1 values (20,2,200,0+0x2222);
 insert into t1 values (30,3,300,0+0x3333);
 insert into t1 values (40,4,400,0+0x4444);
 insert into t1 values (50,5,500,0+0x5555);
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 select a,b,d from t1
 where b in (0,1,2,5)
 order by b;
@@ -1916,5 +1916,5 @@ a	b	d
 50	5	21845
 Warnings:
 Warning	4294	Scan filter is too large, discarded
-set engine_condition_pushdown = @old_ecpd;
+set optimizer_switch = @old_optimizer_switch;
 DROP TABLE t1,t2,t3,t4,t5;
diff --git a/mysql-test/suite/ndb/r/ndb_gis.result b/mysql-test/suite/ndb/r/ndb_gis.result
index 54772f596c3..76a53804d8f 100644
--- a/mysql-test/suite/ndb/r/ndb_gis.result
+++ b/mysql-test/suite/ndb/r/ndb_gis.result
@@ -548,7 +548,7 @@ Overlaps(@horiz1, @point2)
 0
 DROP TABLE t1;
 End of 5.0 tests
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 DROP TABLE IF EXISTS t1, gis_point, gis_line, gis_polygon, gis_multi_point, gis_multi_line, gis_multi_polygon, gis_geometrycollection, gis_geometry;
 CREATE TABLE gis_point  (fid INTEGER PRIMARY KEY AUTO_INCREMENT, g POINT);
 CREATE TABLE gis_line  (fid INTEGER PRIMARY KEY AUTO_INCREMENT, g LINESTRING);
diff --git a/mysql-test/suite/ndb/r/ndb_index_unique.result b/mysql-test/suite/ndb/r/ndb_index_unique.result
index 1fe02d4b5c7..5d0f4038211 100644
--- a/mysql-test/suite/ndb/r/ndb_index_unique.result
+++ b/mysql-test/suite/ndb/r/ndb_index_unique.result
@@ -181,8 +181,8 @@ a	b	c
 5	5	NULL
 8	3	NULL
 9	3	NULL
-set @old_ecpd = @@session.engine_condition_pushdown;
-set engine_condition_pushdown = true;
+set @old_optimizer_switch = @@session.optimizer_switch;
+set optimizer_switch = "engine_condition_pushdown=on";
 explain select * from t2 where (b = 3 OR b = 5) AND c IS NULL AND a < 9 order by a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	PRIMARY,b	PRIMARY	4	NULL	1	Using where with pushed condition
@@ -191,7 +191,7 @@ a	b	c
 3	3	NULL
 5	5	NULL
 8	3	NULL
-set engine_condition_pushdown = @old_ecpd;
+set optimizer_switch = @old_optimizer_switch;
 drop table t2;
 CREATE TABLE t3 (
 a int unsigned NOT NULL,
diff --git a/mysql-test/suite/ndb/t/ndb_condition_pushdown.test b/mysql-test/suite/ndb/t/ndb_condition_pushdown.test
index a56c9dda01c..a6ab06eae31 100644
--- a/mysql-test/suite/ndb/t/ndb_condition_pushdown.test
+++ b/mysql-test/suite/ndb/t/ndb_condition_pushdown.test
@@ -68,8 +68,8 @@ CREATE TABLE  t4 (pk1 int unsigned NOT NULL PRIMARY KEY,   attr1 int unsigned NO
 
 insert into t4 values (0,0,0,0,"a"),(1,1,9223372036854775803,1,"b"),(2,2,9223372036854775804,2,"c"),(3,3,9223372036854775805,3,"d"),(4,4,9223372036854775806,4,"e"),(5,5,9223372036854775807,5,"f");
 
-set @old_ecpd = @@session.engine_condition_pushdown;
-set engine_condition_pushdown = off;
+set @old_optimizer_switch = @@session.optimizer_switch;
+set optimizer_switch = "engine_condition_pushdown=off";
 
 # Test all types and compare operators
 select auto from t1 where 
@@ -453,7 +453,7 @@ select * from t2,t3 where t2.attr1 < 1 and t2.attr2 = t3.attr2 and t3.attr1 < 5
 select * from t4 where attr1 < 5 and attr2 > 9223372036854775803 and attr3 != 3 order by t4.pk1;
 select * from t3,t4 where t4.attr1 > 1 and t4.attr2 = t3.attr2 and t4.attr3 < 5 order by t4.pk1;
 
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 
 # Test all types and compare operators
 --replace_column 9 #
@@ -1674,18 +1674,18 @@ select * from t3 left join t4 on t4.attr2 = t3.attr2 where t4.attr1 > 1 and t4.a
 create table t5 (a int primary key auto_increment, b tinytext not null) 
 engine = ndb;
 insert into t5 (b) values ('jonas'), ('jensing'), ('johan');
-set engine_condition_pushdown = off;
+set optimizer_switch = "engine_condition_pushdown=off";
 select * from t5 where b like '%jo%' order by a;
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 --replace_column 9 #
 explain select * from t5 where b like '%jo%';
 select * from t5 where b like '%jo%' order by a;
 
 # bug#21056  	ndb pushdown equal/setValue error on datetime
-set engine_condition_pushdown = off;
+set optimizer_switch = "engine_condition_pushdown=off";
 select auto from t1 where date_time like '1902-02-02 %' order by auto;
 select auto from t1 where date_time not like '1902-02-02 %' order by auto;
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 --replace_column 9 #
 explain select auto from t1 where date_time like '1902-02-02 %';
 select auto from t1 where date_time like '1902-02-02 %' order by auto;
@@ -1701,12 +1701,12 @@ insert into t1 values (1,'a'), (2,'ab'), (3,'abc');
 # in TUP the constants 'ab' 'abc' were expected in varchar format
 # "like" returned error which became "false"
 # scan filter negates "or" which exposes the bug
-set engine_condition_pushdown = off;
+set optimizer_switch = "engine_condition_pushdown=off";
 select * from t1 where b like 'ab';
 select * from t1 where b like 'ab' or b like 'ab';
 select * from t1 where b like 'abc';
 select * from t1 where b like 'abc' or b like 'abc';
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 select * from t1 where b like 'ab';
 select * from t1 where b like 'ab' or b like 'ab';
 select * from t1 where b like 'abc';
@@ -1719,12 +1719,12 @@ engine=ndb;
 insert into t1 values (1,'a'), (2,'ab'), (3,'abc');
 # test that incorrect MySQL behaviour is preserved
 # 'ab ' LIKE 'ab' is true in MySQL
-set engine_condition_pushdown = off;
+set optimizer_switch = "engine_condition_pushdown=off";
 select * from t1 where b like 'ab';
 select * from t1 where b like 'ab' or b like 'ab';
 select * from t1 where b like 'abc';
 select * from t1 where b like 'abc' or b like 'abc';
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 select * from t1 where b like 'ab';
 select * from t1 where b like 'ab' or b like 'ab';
 select * from t1 where b like 'abc';
@@ -1736,9 +1736,9 @@ create table  t1 ( fname varchar(255), lname varchar(255) )
 engine=ndbcluster;
 insert into t1 values ("Young","Foo");
 
-set engine_condition_pushdown = 0;
+set optimizer_switch = "engine_condition_pushdown=off";
 SELECT fname, lname FROM t1 WHERE (fname like 'Y%') or (lname like 'F%');
-set engine_condition_pushdown = 1;
+set optimizer_switch = "engine_condition_pushdown=on";
 SELECT fname, lname FROM t1 WHERE (fname like 'Y%') or (lname like 'F%');
 
 # make sure optimizer does not do some crazy shortcut
@@ -1747,9 +1747,9 @@ insert into t1 values ("bbb", "bbb");
 insert into t1 values ("ccc", "ccc");
 insert into t1 values ("ddd", "ddd");
 
-set engine_condition_pushdown = 0;
+set optimizer_switch = "engine_condition_pushdown=off";
 SELECT fname, lname FROM t1 WHERE (fname like 'Y%') or (lname like 'F%');
-set engine_condition_pushdown = 1;
+set optimizer_switch = "engine_condition_pushdown=on";
 SELECT fname, lname FROM t1 WHERE (fname like 'Y%') or (lname like 'F%');
 
 # bug#29390 (scan filter is too large, discarded)
@@ -1766,7 +1766,7 @@ insert into t1 values (30,3,300,0+0x3333);
 insert into t1 values (40,4,400,0+0x4444);
 insert into t1 values (50,5,500,0+0x5555);
 
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 
 select a,b,d from t1
     where b in (0,1,2,5)
@@ -2050,5 +2050,5 @@ select a,b,d from t1
     order by b;
 --enable_query_log
 
-set engine_condition_pushdown = @old_ecpd;
+set optimizer_switch = @old_optimizer_switch;
 DROP TABLE t1,t2,t3,t4,t5;
diff --git a/mysql-test/suite/ndb/t/ndb_gis.test b/mysql-test/suite/ndb/t/ndb_gis.test
index e14f462c32d..babff535f94 100644
--- a/mysql-test/suite/ndb/t/ndb_gis.test
+++ b/mysql-test/suite/ndb/t/ndb_gis.test
@@ -1,5 +1,5 @@
 --source include/have_ndb.inc
 SET storage_engine=ndbcluster;
 --source include/gis_generic.inc
-set engine_condition_pushdown = on;
+set optimizer_switch = "engine_condition_pushdown=on";
 --source include/gis_generic.inc
diff --git a/mysql-test/suite/ndb/t/ndb_index_unique.test b/mysql-test/suite/ndb/t/ndb_index_unique.test
index 78757c3bcf7..9178ace1ad0 100644
--- a/mysql-test/suite/ndb/t/ndb_index_unique.test
+++ b/mysql-test/suite/ndb/t/ndb_index_unique.test
@@ -112,11 +112,11 @@ insert t2 values(1,1,NULL),(2,2,2),(3,3,NULL),(4,4,4),(5,5,NULL),(6,6,6),(7,7,NU
 select * from t2 where c IS NULL order by a;
 select * from t2 where b = 3 AND c IS NULL order by a;
 select * from t2 where (b = 3 OR b = 5) AND c IS NULL order by a;
-set @old_ecpd = @@session.engine_condition_pushdown;
-set engine_condition_pushdown = true;
+set @old_optimizer_switch = @@session.optimizer_switch;
+set optimizer_switch = "engine_condition_pushdown=on";
 explain select * from t2 where (b = 3 OR b = 5) AND c IS NULL AND a < 9 order by a;
 select * from t2 where (b = 3 OR b = 5) AND c IS NULL AND a < 9 order by a;
-set engine_condition_pushdown = @old_ecpd;
+set optimizer_switch = @old_optimizer_switch;
 
 drop table t2;
 
diff --git a/mysql-test/suite/sys_vars/r/engine_condition_pushdown_basic.result b/mysql-test/suite/sys_vars/r/engine_condition_pushdown_basic.result
index 6a8052490c4..2b0b57d5b33 100644
--- a/mysql-test/suite/sys_vars/r/engine_condition_pushdown_basic.result
+++ b/mysql-test/suite/sys_vars/r/engine_condition_pushdown_basic.result
@@ -6,19 +6,33 @@ SET @global_start_value = @@global.engine_condition_pushdown;
 SELECT @global_start_value;
 @global_start_value
 1
+select @old_session_opt_switch:=@@session.optimizer_switch,
+@old_global_opt_switch:=@@global.optimizer_switch;
+@old_session_opt_switch:=@@session.optimizer_switch	@old_global_opt_switch:=@@global.optimizer_switch
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 '#--------------------FN_DYNVARS_028_01------------------------#'
 SET @@session.engine_condition_pushdown = 0;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SET @@session.engine_condition_pushdown = DEFAULT;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 1
 SET @@global.engine_condition_pushdown = 0;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SET @@global.engine_condition_pushdown = DEFAULT;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 1
 '#---------------------FN_DYNVARS_028_02-------------------------#'
 SET engine_condition_pushdown = 1;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@engine_condition_pushdown;
 @@engine_condition_pushdown
 1
@@ -29,27 +43,39 @@ ERROR 42S02: Unknown table 'local' in field list
 SELECT global.engine_condition_pushdown;
 ERROR 42S02: Unknown table 'global' in field list
 SET session engine_condition_pushdown = 0;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 0
 SET global engine_condition_pushdown = 0;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 0
 '#--------------------FN_DYNVARS_028_03------------------------#'
 SET @@session.engine_condition_pushdown = 0;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 0
 SET @@session.engine_condition_pushdown = 1;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 1
 SET @@global.engine_condition_pushdown = 0;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 0
 SET @@global.engine_condition_pushdown = 1;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 1
@@ -88,11 +114,17 @@ SET @@global.engine_condition_pushdown = 
 ERROR 42000: Variable 'engine_condition_pushdown' can't be set to the value of '�FF'
 '#-------------------FN_DYNVARS_028_05----------------------------#'
 SET @@global.engine_condition_pushdown = 0;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SET @@session.engine_condition_pushdown = 1;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown AS res_is_0;
 res_is_0
 0
 SET @@global.engine_condition_pushdown = 0;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown AS res_is_1;
 res_is_1
 1
@@ -126,43 +158,133 @@ VARIABLE_VALUE
 ON
 '#---------------------FN_DYNVARS_028_08-------------------------#'
 SET @@session.engine_condition_pushdown = OFF;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 0
 SET @@session.engine_condition_pushdown = ON;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 1
 SET @@global.engine_condition_pushdown = OFF;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 0
 SET @@global.engine_condition_pushdown = ON;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 1
 '#---------------------FN_DYNVARS_028_09----------------------#'
 SET @@session.engine_condition_pushdown = TRUE;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 1
 SET @@session.engine_condition_pushdown = FALSE;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 0
 SET @@global.engine_condition_pushdown = TRUE;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 1
 SET @@global.engine_condition_pushdown = FALSE;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 0
+Check that @@engine_condition_pushdown influences
+@@optimizer_switch and vice-versa
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+0	0	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
+set @@session.engine_condition_pushdown = TRUE;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+1	0	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
+set @@session.engine_condition_pushdown = FALSE;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+0	0	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
+set @@global.engine_condition_pushdown = TRUE;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+0	1	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
+set @@global.engine_condition_pushdown = FALSE;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+0	0	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
+set @@session.optimizer_switch = "engine_condition_pushdown=on";
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+1	0	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
+set @@session.optimizer_switch = "engine_condition_pushdown=off";
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+0	0	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
+set @@global.optimizer_switch = "engine_condition_pushdown=on";
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+0	1	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
+set @@global.optimizer_switch = "engine_condition_pushdown=off";
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+0	0	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
 SET @@session.engine_condition_pushdown = @session_start_value;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@session.engine_condition_pushdown;
 @@session.engine_condition_pushdown
 1
 SET @@global.engine_condition_pushdown = @global_start_value;
+Warnings:
+Warning	1287	The syntax '@@engine_condition_pushdown' is deprecated and will be removed in MySQL 7.0. Please use '@@optimizer_switch' instead
 SELECT @@global.engine_condition_pushdown;
 @@global.engine_condition_pushdown
 1
+set @session.optimizer_switch=@old_session_opt_switch,
+@@global.optimizer_switch=@old_global_opt_switch;
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+@@session.engine_condition_pushdown	@@global.engine_condition_pushdown	@@session.optimizer_switch	@@global.optimizer_switch
+1	1	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
diff --git a/mysql-test/suite/sys_vars/r/optimizer_switch_basic.result b/mysql-test/suite/sys_vars/r/optimizer_switch_basic.result
index d1e03f769c8..2d648259a26 100644
--- a/mysql-test/suite/sys_vars/r/optimizer_switch_basic.result
+++ b/mysql-test/suite/sys_vars/r/optimizer_switch_basic.result
@@ -1,45 +1,45 @@
 SET @start_global_value = @@global.optimizer_switch;
 SELECT @start_global_value;
 @start_global_value
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 select @@global.optimizer_switch;
 @@global.optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 select @@session.optimizer_switch;
 @@session.optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 show global variables like 'optimizer_switch';
 Variable_name	Value
-optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 show session variables like 'optimizer_switch';
 Variable_name	Value
-optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 select * from information_schema.global_variables where variable_name='optimizer_switch';
 VARIABLE_NAME	VARIABLE_VALUE
-OPTIMIZER_SWITCH	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+OPTIMIZER_SWITCH	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 select * from information_schema.session_variables where variable_name='optimizer_switch';
 VARIABLE_NAME	VARIABLE_VALUE
-OPTIMIZER_SWITCH	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+OPTIMIZER_SWITCH	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
 set global optimizer_switch=10;
 select @@global.optimizer_switch;
 @@global.optimizer_switch
-index_merge=off,index_merge_union=on,index_merge_sort_union=off,index_merge_intersection=on
+index_merge=off,index_merge_union=on,index_merge_sort_union=off,index_merge_intersection=on,engine_condition_pushdown=off
 set session optimizer_switch=5;
 select @@session.optimizer_switch;
 @@session.optimizer_switch
-index_merge=on,index_merge_union=off,index_merge_sort_union=on,index_merge_intersection=off
+index_merge=on,index_merge_union=off,index_merge_sort_union=on,index_merge_intersection=off,engine_condition_pushdown=off
 set global optimizer_switch="index_merge_sort_union=on";
 select @@global.optimizer_switch;
 @@global.optimizer_switch
-index_merge=off,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=off,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
 set session optimizer_switch="index_merge=off";
 select @@session.optimizer_switch;
 @@session.optimizer_switch
-index_merge=off,index_merge_union=off,index_merge_sort_union=on,index_merge_intersection=off
+index_merge=off,index_merge_union=off,index_merge_sort_union=on,index_merge_intersection=off,engine_condition_pushdown=off
 set session optimizer_switch="default";
 select @@session.optimizer_switch;
 @@session.optimizer_switch
-index_merge=off,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=off,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=off
 set global optimizer_switch=1.1;
 ERROR 42000: Incorrect argument type to variable 'optimizer_switch'
 set global optimizer_switch=1e1;
@@ -51,4 +51,4 @@ ERROR 42000: Variable 'optimizer_switch' can't be set to the value of 'foobar'
 SET @@global.optimizer_switch = @start_global_value;
 SELECT @@global.optimizer_switch;
 @@global.optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on
diff --git a/mysql-test/suite/sys_vars/t/engine_condition_pushdown_basic.test b/mysql-test/suite/sys_vars/t/engine_condition_pushdown_basic.test
index b153ac50e1e..ab9bfc3dd6a 100644
--- a/mysql-test/suite/sys_vars/t/engine_condition_pushdown_basic.test
+++ b/mysql-test/suite/sys_vars/t/engine_condition_pushdown_basic.test
@@ -40,6 +40,10 @@ SELECT @session_start_value;
 SET @global_start_value = @@global.engine_condition_pushdown;
 SELECT @global_start_value;
 
+# same for optimizer_switch
+select @old_session_opt_switch:=@@session.optimizer_switch,
+@old_global_opt_switch:=@@global.optimizer_switch;
+
 --echo '#--------------------FN_DYNVARS_028_01------------------------#'
 ######################################################################## 
 #           Display the DEFAULT value of engine_condition_pushdown     #
@@ -204,6 +208,44 @@ SELECT @@global.engine_condition_pushdown;
 SET @@global.engine_condition_pushdown = FALSE;
 SELECT @@global.engine_condition_pushdown;
 
+--echo Check that @@engine_condition_pushdown influences
+--echo @@optimizer_switch and vice-versa
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+set @@session.engine_condition_pushdown = TRUE;
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+set @@session.engine_condition_pushdown = FALSE;
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+set @@global.engine_condition_pushdown = TRUE;
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+set @@global.engine_condition_pushdown = FALSE;
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+set @@session.optimizer_switch = "engine_condition_pushdown=on";
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+set @@session.optimizer_switch = "engine_condition_pushdown=off";
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+set @@global.optimizer_switch = "engine_condition_pushdown=on";
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+set @@global.optimizer_switch = "engine_condition_pushdown=off";
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+
 ##############################  
 #   Restore initial value    #
 ##############################
@@ -214,6 +256,12 @@ SELECT @@session.engine_condition_pushdown;
 SET @@global.engine_condition_pushdown = @global_start_value;
 SELECT @@global.engine_condition_pushdown;
 
+set @session.optimizer_switch=@old_session_opt_switch,
+@@global.optimizer_switch=@old_global_opt_switch;
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
+
 ###############################################################
 #                    END OF engine_condition_pushdown TESTS   #
 ############################################################### 
diff --git a/mysql-test/t/optimizer_switch_eng_cond_pushdown1-master.opt b/mysql-test/t/optimizer_switch_eng_cond_pushdown1-master.opt
new file mode 100644
index 00000000000..89aa07976ac
--- /dev/null
+++ b/mysql-test/t/optimizer_switch_eng_cond_pushdown1-master.opt
@@ -0,0 +1 @@
+--optimizer-switch=engine_condition_pushdown=off --engine-condition-pushdown=1
diff --git a/mysql-test/t/optimizer_switch_eng_cond_pushdown1.test b/mysql-test/t/optimizer_switch_eng_cond_pushdown1.test
new file mode 100644
index 00000000000..187aa145408
--- /dev/null
+++ b/mysql-test/t/optimizer_switch_eng_cond_pushdown1.test
@@ -0,0 +1,5 @@
+# check how --engine-condition-pushdown and --optimizer-switch
+# influence each other when used together (last wins).
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
diff --git a/mysql-test/t/optimizer_switch_eng_cond_pushdown2-master.opt b/mysql-test/t/optimizer_switch_eng_cond_pushdown2-master.opt
new file mode 100644
index 00000000000..f48ab5b963b
--- /dev/null
+++ b/mysql-test/t/optimizer_switch_eng_cond_pushdown2-master.opt
@@ -0,0 +1 @@
+--engine-condition-pushdown=1 --optimizer-switch=engine_condition_pushdown=off
diff --git a/mysql-test/t/optimizer_switch_eng_cond_pushdown2.test b/mysql-test/t/optimizer_switch_eng_cond_pushdown2.test
new file mode 100644
index 00000000000..187aa145408
--- /dev/null
+++ b/mysql-test/t/optimizer_switch_eng_cond_pushdown2.test
@@ -0,0 +1,5 @@
+# check how --engine-condition-pushdown and --optimizer-switch
+# influence each other when used together (last wins).
+select @@session.engine_condition_pushdown,
+@@global.engine_condition_pushdown,
+@@session.optimizer_switch, @@global.optimizer_switch;
diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h
index 1615ca97073..bf0c587f7ae 100644
--- a/sql/mysql_priv.h
+++ b/sql/mysql_priv.h
@@ -622,17 +622,19 @@ protected:
 #define MODE_PAD_CHAR_TO_FULL_LENGTH    (ULL(1) << 31)
 
 /* @@optimizer_switch flags. These must be in sync with optimizer_switch_typelib */
-#define OPTIMIZER_SWITCH_INDEX_MERGE 1
-#define OPTIMIZER_SWITCH_INDEX_MERGE_UNION 2
-#define OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION 4
-#define OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT 8
-#define OPTIMIZER_SWITCH_LAST 16
+#define OPTIMIZER_SWITCH_INDEX_MERGE               (1ULL << 0)
+#define OPTIMIZER_SWITCH_INDEX_MERGE_UNION         (1ULL << 1)
+#define OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION    (1ULL << 2)
+#define OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT     (1ULL << 3)
+#define OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN (1ULL << 4)
+#define OPTIMIZER_SWITCH_LAST                      (1ULL << 5)
 
 /* The following must be kept in sync with optimizer_switch_str in mysqld.cc */
 #define OPTIMIZER_SWITCH_DEFAULT (OPTIMIZER_SWITCH_INDEX_MERGE | \
                                   OPTIMIZER_SWITCH_INDEX_MERGE_UNION | \
                                   OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION | \
-                                  OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT)
+                                  OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT | \
+                                  OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN)
 
 
 /*
@@ -2636,7 +2638,8 @@ enum options_mysqld
   OPT_SSL_CIPHER,
   OPT_SSL_KEY,
   OPT_UPDATE_LOG,
-  OPT_WANT_CORE
+  OPT_WANT_CORE,
+  OPT_ENGINE_CONDITION_PUSHDOWN
 };
 
 #endif /* MYSQL_SERVER */
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index d49e893aaa1..777f09c47ea 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -6995,6 +6995,18 @@ mysqld_get_one_option(int optid,
     }
     break;
 #endif /* defined(ENABLED_DEBUG_SYNC) */
+  case OPT_ENGINE_CONDITION_PUSHDOWN:
+    /*
+      The last of --engine-condition-pushdown and --optimizer_switch on
+      command line wins (see get_options().
+    */
+    if (global_system_variables.engine_condition_pushdown)
+      global_system_variables.optimizer_switch|=
+        OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN;
+    else
+      global_system_variables.optimizer_switch&=
+        ~OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN;
+    break;
   }
   return 0;
 }
@@ -7216,6 +7228,11 @@ static int get_options(int *argc,char **argv)
   else
     pool_of_threads_scheduler(&thread_scheduler);  /* purecov: tested */
 #endif
+
+  global_system_variables.engine_condition_pushdown=
+    test(global_system_variables.optimizer_switch &
+         OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN);
+
   return 0;
 }
 
diff --git a/sql/records.cc b/sql/records.cc
index 9ec19c55841..c97ffa152dc 100644
--- a/sql/records.cc
+++ b/sql/records.cc
@@ -270,7 +270,8 @@ void init_read_record(READ_RECORD *info,THD *thd, TABLE *table,
 				  thd->variables.read_buff_size);
   }
   /* Condition pushdown to storage engine */
-  if (thd->variables.engine_condition_pushdown && 
+  if ((thd->variables.optimizer_switch &
+       OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN) && 
       select && select->cond && 
       (select->cond->used_tables() & table->map) &&
       !table->file->pushed_cond)
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 5f1c5ac2a34..5647727089f 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -6347,7 +6347,8 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
           /* Push condition to storage engine if this is enabled
              and the condition is not guarded */
           tab->table->file->pushed_cond= NULL;
-	  if (thd->variables.engine_condition_pushdown)
+	  if (thd->variables.optimizer_switch &
+              OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN)
           {
             COND *push_cond= 
               make_cond_for_table(tmp, current_map, current_map);
@@ -16630,7 +16631,8 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
           {
             const COND *pushed_cond= tab->table->file->pushed_cond;
 
-            if (thd->variables.engine_condition_pushdown && pushed_cond)
+            if ((thd->variables.optimizer_switch &
+                 OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN) && pushed_cond)
             {
               extra.append(STRING_WITH_LEN("; Using where with pushed "
                                            "condition"));
diff --git a/sql/sys_vars.cc b/sql/sys_vars.cc
index da507edbdb4..31dc9e74902 100644
--- a/sql/sys_vars.cc
+++ b/sql/sys_vars.cc
@@ -1114,16 +1114,28 @@ static Sys_var_ulong Sys_optimizer_search_depth(
 static const char *optimizer_switch_names[]=
 {
   "index_merge", "index_merge_union", "index_merge_sort_union",
-  "index_merge_intersection",
+  "index_merge_intersection", "engine_condition_pushdown",
   "default", NullS
 };
+/** propagates changes to @@engine_condition_pushdown */
+static bool fix_optimizer_switch(sys_var *self, THD *thd,
+                                 enum_var_type type)
+{
+  SV *sv= (type == OPT_GLOBAL) ? &global_system_variables : &thd->variables;
+  sv->engine_condition_pushdown= 
+    test(sv->optimizer_switch & OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN);
+  return false;
+}
 static Sys_var_flagset Sys_optimizer_switch(
        "optimizer_switch",
        "optimizer_switch=option=val[,option=val...], where option is one of "
        "{index_merge, index_merge_union, index_merge_sort_union, "
-       "index_merge_intersection} and val is one of {on, off, default}",
+       "index_merge_intersection, engine_condition_pushdown}"
+       " and val is one of {on, off, default}",
        SESSION_VAR(optimizer_switch), CMD_LINE(REQUIRED_ARG),
-       optimizer_switch_names, DEFAULT(OPTIMIZER_SWITCH_DEFAULT));
+       optimizer_switch_names, DEFAULT(OPTIMIZER_SWITCH_DEFAULT),
+       NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(NULL),
+       ON_UPDATE(fix_optimizer_switch));
 
 static Sys_var_charptr Sys_pid_file(
        "pid_file", "Pid file used by safe_mysqld",
@@ -1820,11 +1832,26 @@ static Sys_var_ulong Sys_net_wait_timeout(
        VALID_RANGE(1, IF_WIN(INT_MAX32/1000, LONG_TIMEOUT)),
        DEFAULT(NET_WAIT_TIMEOUT), BLOCK_SIZE(1));
 
+/** propagates changes to the relevant flag of @@optimizer_switch */
+static bool fix_engine_condition_pushdown(sys_var *self, THD *thd,
+                                          enum_var_type type)
+{
+  SV *sv= (type == OPT_GLOBAL) ? &global_system_variables : &thd->variables;
+  if (sv->engine_condition_pushdown)
+    sv->optimizer_switch|= OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN;
+  else
+    sv->optimizer_switch&= ~OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN;
+  return false;
+}
 static Sys_var_mybool Sys_engine_condition_pushdown(
        "engine_condition_pushdown",
-       "Push supported query conditions to the storage engine",
-       SESSION_VAR(engine_condition_pushdown), CMD_LINE(OPT_ARG),
-       DEFAULT(TRUE));
+       "Push supported query conditions to the storage engine."
+       " Deprecated, use --optimizer-switch instead.",
+       SESSION_VAR(engine_condition_pushdown),
+       CMD_LINE(OPT_ARG, OPT_ENGINE_CONDITION_PUSHDOWN),
+       DEFAULT(TRUE), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(NULL),
+       ON_UPDATE(fix_engine_condition_pushdown),
+       DEPRECATED(70000, "'@@optimizer_switch'"));
 
 static Sys_var_plugin Sys_default_storage_engine(
        "default_storage_engine", "The default storage engine for new tables",

From 03d10b8cd082ea070de3cf344276b55dfe7fb524 Mon Sep 17 00:00:00 2001
From: Guilhem Bichot <guilhem@mysql.com>
Date: Wed, 6 Jan 2010 11:59:01 +0100
Subject: [PATCH 07/11] Fix for BUG#50081 "Tests: mysqld--help-notwin fails
 with --parallel": eliminate 3 more "directory path" variables from the test's
 output (it was already the case for other similar ones likes
 slow-query-log-file).

---
 mysql-test/include/mysqld--help.inc     | 3 ++-
 mysql-test/r/mysqld--help-notwin.result | 3 ---
 mysql-test/r/mysqld--help-win.result    | 3 ---
 3 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/mysql-test/include/mysqld--help.inc b/mysql-test/include/mysqld--help.inc
index 82bf65bc362..3c8d19107c9 100644
--- a/mysql-test/include/mysqld--help.inc
+++ b/mysql-test/include/mysqld--help.inc
@@ -11,7 +11,8 @@ exec $MYSQLD_BOOTSTRAP_CMD --symbolic-links=0 --lower-case-table-names=1 --help
 
 perl;
   @skipvars=qw/basedir open-files-limit general-log-file log
-               log-slow-queries pid-file slow-query-log-file/;
+               log-slow-queries pid-file slow-query-log-file
+               datadir slave-load-tmpdir tmpdir/;
   @plugins=qw/innodb ndb ndbcluster safemalloc debug temp-pool ssl des-key-file
               thread-concurrency super-large-pages mutex-deadlock-detector/;
   @env=qw/MYSQLTEST_VARDIR MYSQL_TEST_DIR MYSQL_LIBDIR MYSQL_SHAREDIR/;
diff --git a/mysql-test/r/mysqld--help-notwin.result b/mysql-test/r/mysqld--help-notwin.result
index 1953e014e4b..3975f085468 100644
--- a/mysql-test/r/mysqld--help-notwin.result
+++ b/mysql-test/r/mysqld--help-notwin.result
@@ -744,7 +744,6 @@ completion-type                     NO_CHAIN
 concurrent-insert                   AUTO
 connect-timeout                     10
 console                             FALSE
-datadir                             MYSQLTEST_VARDIR/install.db/
 date-format                         %Y-%m-%d
 datetime-format                     %Y-%m-%d %H:%i:%s
 default-character-set               latin1
@@ -898,7 +897,6 @@ skip-show-database                  FALSE
 skip-slave-start                    FALSE
 slave-compressed-protocol           FALSE
 slave-exec-mode                     STRICT
-slave-load-tmpdir                   MYSQLTEST_VARDIR/tmp/
 slave-net-timeout                   3600
 slave-skip-errors                   (No default value)
 slave-transaction-retries           10
@@ -926,7 +924,6 @@ thread-stack                        262144
 time-format                         %H:%i:%s
 timed-mutexes                       FALSE
 tmp-table-size                      16777216
-tmpdir                              MYSQLTEST_VARDIR/tmp/
 transaction-alloc-block-size        8192
 transaction-isolation               REPEATABLE-READ
 transaction-prealloc-size           4096
diff --git a/mysql-test/r/mysqld--help-win.result b/mysql-test/r/mysqld--help-win.result
index 95d3a48a6e1..c51fbd969ba 100644
--- a/mysql-test/r/mysqld--help-win.result
+++ b/mysql-test/r/mysqld--help-win.result
@@ -748,7 +748,6 @@ completion-type                   NO_CHAIN
 concurrent-insert                 AUTO
 connect-timeout                   10
 console                           FALSE
-datadir                           MYSQLTEST_VARDIR/install.db/
 date-format                       %Y-%m-%d
 datetime-format                   %Y-%m-%d %H:%i:%s
 default-character-set             latin1
@@ -904,7 +903,6 @@ skip-show-database                FALSE
 skip-slave-start                  FALSE
 slave-compressed-protocol         FALSE
 slave-exec-mode                   STRICT
-slave-load-tmpdir                 MYSQLTEST_VARDIR/tmp/
 slave-net-timeout                 3600
 slave-skip-errors                 (No default value)
 slave-transaction-retries         10
@@ -932,7 +930,6 @@ thread-stack                      262144
 time-format                       %H:%i:%s
 timed-mutexes                     FALSE
 tmp-table-size                    16777216
-tmpdir                            MYSQLTEST_VARDIR/tmp/
 transaction-alloc-block-size      8192
 transaction-isolation             REPEATABLE-READ
 transaction-prealloc-size         4096

From 9ffdd1cc9297a20189c0403c1156a095264391b0 Mon Sep 17 00:00:00 2001
From: Tor Didriksen <tor.didriksen@sun.com>
Date: Wed, 6 Jan 2010 15:00:51 +0100
Subject: [PATCH 08/11] Bug #50087  	Interval arithmetic for
 Event_queue_element is not portable.

Subtraction of two unsigned months yielded a (very large) positive value.
Conversion of this to a signed value was not necessarily well defined.

Solution: do the subtraction on signed values.
---
 mysql-test/r/events_scheduling.result | 19 +++++++++++++++++++
 mysql-test/t/events_scheduling.test   | 26 ++++++++++++++++++++++++++
 sql/event_data_objects.cc             |  5 +++--
 3 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/mysql-test/r/events_scheduling.result b/mysql-test/r/events_scheduling.result
index 7dfd10a53f8..262caea3d7f 100644
--- a/mysql-test/r/events_scheduling.result
+++ b/mysql-test/r/events_scheduling.result
@@ -82,5 +82,24 @@ DROP TABLE table_1;
 DROP TABLE table_2;
 DROP TABLE table_3;
 DROP TABLE table_4;
+
+Bug #50087 Interval arithmetic for Event_queue_element is not portable.
+
+CREATE TABLE t1(a int);
+CREATE EVENT e1 ON SCHEDULE EVERY 1 MONTH
+STARTS NOW() - INTERVAL 1 MONTH
+ENDS NOW() + INTERVAL 2 MONTH
+ON COMPLETION PRESERVE
+DO
+INSERT INTO t1 VALUES (1);
+CREATE EVENT e2 ON SCHEDULE EVERY 1 MONTH
+STARTS NOW()
+ENDS NOW() + INTERVAL 11 MONTH
+ON COMPLETION PRESERVE
+DO
+INSERT INTO t1 VALUES (1);
+DROP TABLE t1;
+DROP EVENT e1;
+DROP EVENT e2;
 DROP DATABASE events_test;
 SET GLOBAL event_scheduler=@event_scheduler;
diff --git a/mysql-test/t/events_scheduling.test b/mysql-test/t/events_scheduling.test
index 041a2def490..5f16f8bea6a 100644
--- a/mysql-test/t/events_scheduling.test
+++ b/mysql-test/t/events_scheduling.test
@@ -108,6 +108,32 @@ DROP TABLE table_1;
 DROP TABLE table_2;
 DROP TABLE table_3;
 DROP TABLE table_4;
+
+-- echo
+-- echo Bug #50087 Interval arithmetic for Event_queue_element is not portable.
+-- echo
+
+CREATE TABLE t1(a int);
+
+CREATE EVENT e1 ON SCHEDULE EVERY 1 MONTH
+STARTS NOW() - INTERVAL 1 MONTH
+ENDS NOW() + INTERVAL 2 MONTH
+ON COMPLETION PRESERVE
+DO
+  INSERT INTO t1 VALUES (1);
+
+CREATE EVENT e2 ON SCHEDULE EVERY 1 MONTH
+STARTS NOW()
+ENDS NOW() + INTERVAL 11 MONTH
+ON COMPLETION PRESERVE
+DO
+  INSERT INTO t1 VALUES (1);
+
+DROP TABLE t1;
+DROP EVENT e1;
+DROP EVENT e2;
+
+
 DROP DATABASE events_test;
 SET GLOBAL event_scheduler=@event_scheduler;
 
diff --git a/sql/event_data_objects.cc b/sql/event_data_objects.cc
index b2bbd340e14..7562e8d8207 100644
--- a/sql/event_data_objects.cc
+++ b/sql/event_data_objects.cc
@@ -834,8 +834,9 @@ bool get_next_time(const Time_zone *time_zone, my_time_t *next,
   }
   else
   {
-    long diff_months= (long) (local_now.year - local_start.year)*12 +
-                      (local_now.month - local_start.month);
+    long diff_months= ((long) local_now.year - (long) local_start.year)*12 +
+                      ((long) local_now.month - (long) local_start.month);
+
     /*
       Unlike for seconds above, the formula below returns the interval
       that, when added to the local_start, will give the time in the

From 42c5cff706466cd2da1e46f2f79177e1578538ba Mon Sep 17 00:00:00 2001
From: Tor Didriksen <tor.didriksen@sun.com>
Date: Wed, 6 Jan 2010 16:21:43 +0100
Subject: [PATCH 09/11] Follup fix for WL#4738

---
 mysql-test/lib/v1/mysql-test-run.pl | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mysql-test/lib/v1/mysql-test-run.pl b/mysql-test/lib/v1/mysql-test-run.pl
index 9ae3208fc24..4202334a244 100755
--- a/mysql-test/lib/v1/mysql-test-run.pl
+++ b/mysql-test/lib/v1/mysql-test-run.pl
@@ -3911,7 +3911,7 @@ sub mysqld_arguments ($$$$) {
 
   if ( $opt_valgrind_mysqld )
   {
-    mtr_add_arg($args, "%s--skip-safemalloc", $prefix);
+    mtr_add_arg($args, "%s--loose-skip-safemalloc", $prefix);
 
     if ( $mysql_version_id < 50100 )
     {
@@ -5208,7 +5208,6 @@ sub valgrind_arguments {
   else
   {
     mtr_add_arg($args, "--tool=memcheck"); # From >= 2.1.2 needs this option
-    mtr_add_arg($args, "--alignment=8");
     mtr_add_arg($args, "--leak-check=yes");
     mtr_add_arg($args, "--num-callers=16");
     mtr_add_arg($args, "--suppressions=%s/valgrind.supp", $glob_mysql_test_dir)

From c06a305001bc89116a718cf0c12dd03b58cfe92a Mon Sep 17 00:00:00 2001
From: Alexander Nozdrin <alik@sun.com>
Date: Mon, 11 Jan 2010 19:42:35 +0300
Subject: [PATCH 10/11] Backporting revision from mysql-6.0-codebase-bugfixing.
 Original revision:

------------------------------------------------------------
revno: 3789.1.9
revision-id: serg@mysql.com-20091229134448-phe834ukzmi0k2e3
parent: serg@mysql.com-20091227081418-bgfg952gzumn1k3h
committer: Sergei Golubchik <serg@mysql.com>
branch nick: 6.0-codebase
timestamp: Tue 2009-12-29 14:44:48 +0100
message:
  better fix for Bug#48758 mysqltest crashes on sys_vars.collation_server_basic in gcov builds

  use setenv instead of putenv
------------------------------------------------------------
---
 client/mysqltest.cc  | 20 ++++++++++----------
 include/config-win.h |  5 ++++-
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/client/mysqltest.cc b/client/mysqltest.cc
index 43107d838ee..9c5e7d9f466 100644
--- a/client/mysqltest.cc
+++ b/client/mysqltest.cc
@@ -72,6 +72,10 @@
 #define QUERY_SEND_FLAG  1
 #define QUERY_REAP_FLAG  2
 
+#ifndef HAVE_SETENV
+#error implement our portable setenv replacement in mysys
+#endif
+
 enum {
   OPT_SKIP_SAFEMALLOC=OPT_MAX_CLIENT_OPTION,
   OPT_PS_PROTOCOL, OPT_SP_PROTOCOL, OPT_CURSOR_PROTOCOL, OPT_VIEW_PROTOCOL,
@@ -216,7 +220,6 @@ typedef struct
   int alloced_len;
   int int_dirty; /* do not update string if int is updated until first read */
   int alloced;
-  char *env_s;
 } VAR;
 
 /*Perl/shell-like variable registers */
@@ -1941,7 +1944,6 @@ VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
   tmp_var->alloced_len = val_alloc_len;
   tmp_var->int_val = (val) ? atoi(val) : 0;
   tmp_var->int_dirty = 0;
-  tmp_var->env_s = 0;
   return tmp_var;
 }
 
@@ -2069,20 +2071,18 @@ void var_set(const char *var_name, const char *var_name_end,
 
   if (env_var)
   {
-    char buf[1024], *old_env_s= v->env_s;
     if (v->int_dirty)
     {
       sprintf(v->str_val, "%d", v->int_val);
       v->int_dirty= 0;
       v->str_val_len= strlen(v->str_val);
     }
-    my_snprintf(buf, sizeof(buf), "%.*s=%.*s",
-                v->name_len, v->name,
-                v->str_val_len, v->str_val);
-    if (!(v->env_s= my_strdup(buf, MYF(MY_WME))))
-      die("Out of memory");
-    putenv(v->env_s);
-    my_free(old_env_s, MYF(MY_ALLOW_ZERO_PTR));
+    char oldc= v->name[v->name_len];
+    if (oldc)
+      v->name[v->name_len]= 0;   // setenv() expects \0-terminated strings
+    setenv(v->name, v->str_val, 1); // v->str_val is always \0-terminated
+    if (oldc)
+      v->name[v->name_len]= oldc;
   }
   DBUG_VOID_RETURN;
 }
diff --git a/include/config-win.h b/include/config-win.h
index f8d51165bc2..5d64261076d 100644
--- a/include/config-win.h
+++ b/include/config-win.h
@@ -318,9 +318,12 @@ inline ulonglong double2ulonglong(double d)
 #define strcasecmp stricmp
 #define strncasecmp strnicmp
 
-#define HAVE_SNPRINTF		/* Gave link error */
+#define HAVE_SNPRINTF 1
 #define snprintf _snprintf
 
+#define HAVE_SETENV 1
+#define setenv(VAR,VAL,X)       _putenv_s(VAR,VAL)
+
 #ifdef _MSC_VER
 #define HAVE_LDIV		/* The optimizer breaks in zortech for ldiv */
 #define HAVE_ANSI_INCLUDE

From ba3b5a7eb660c7172da1cb4bf64abb9e9a2e0603 Mon Sep 17 00:00:00 2001
From: Alexander Nozdrin <alik@sun.com>
Date: Mon, 11 Jan 2010 19:43:55 +0300
Subject: [PATCH 11/11] Backporting revision from mysql-6.0-codebase-bugfixing.
 Original revision:
 ------------------------------------------------------------ revno: 3817
 revision-id: guilhem@mysql.com-20100108092756-k0zzf4kvx9b7bh38 parent:
 guilhem@mysql.com-20100107101133-hrrgcdqg508runuf committer: Guilhem Bichot
 <guilhem@mysql.com> branch nick: mysql-6.0-codebase-bugfixing timestamp: Fri
 2010-01-08 10:27:56 +0100 message:   fix for BUG#50120 "Valgrind errors in
 any test, inside mysqltest"   Problem was that as v->name[v->name_len] may be
 uninitialized (which is ok per se),   it shouldn't be used in an if(). We
 remove this zero_the_char/restore_it logic by   rather zero-terminating the
 v->name string when we create it in var_init().
 ------------------------------------------------------------

---
 client/mysqltest.cc | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/client/mysqltest.cc b/client/mysqltest.cc
index 9c5e7d9f466..95255b59afa 100644
--- a/client/mysqltest.cc
+++ b/client/mysqltest.cc
@@ -1927,13 +1927,20 @@ VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
                                                   + name_len+1, MYF(MY_WME))))
     die("Out of memory");
 
-  tmp_var->name = (name) ? (char*) tmp_var + sizeof(*tmp_var) : 0;
+  if (name != NULL)
+  {
+    tmp_var->name= reinterpret_cast<char*>(tmp_var) + sizeof(*tmp_var);
+    memcpy(tmp_var->name, name, name_len);
+    tmp_var->name[name_len]= 0;
+  }
+  else
+    tmp_var->name= NULL;
+
   tmp_var->alloced = (v == 0);
 
   if (!(tmp_var->str_val = (char*)my_malloc(val_alloc_len+1, MYF(MY_WME))))
     die("Out of memory");
 
-  memcpy(tmp_var->name, name, name_len);
   if (val)
   {
     memcpy(tmp_var->str_val, val, val_len);
@@ -2077,12 +2084,9 @@ void var_set(const char *var_name, const char *var_name_end,
       v->int_dirty= 0;
       v->str_val_len= strlen(v->str_val);
     }
-    char oldc= v->name[v->name_len];
-    if (oldc)
-      v->name[v->name_len]= 0;   // setenv() expects \0-terminated strings
-    setenv(v->name, v->str_val, 1); // v->str_val is always \0-terminated
-    if (oldc)
-      v->name[v->name_len]= oldc;
+    /* setenv() expects \0-terminated strings */
+    DBUG_ASSERT(v->name[v->name_len] == 0);
+    setenv(v->name, v->str_val, 1);
   }
   DBUG_VOID_RETURN;
 }