diff --git a/BUILD/compile-pentium64-asan-max b/BUILD/compile-pentium64-asan-max
index 9a4de53162e..37acc9f74f3 100755
--- a/BUILD/compile-pentium64-asan-max
+++ b/BUILD/compile-pentium64-asan-max
@@ -17,7 +17,7 @@
 path=`dirname $0`
 . "$path/SETUP.sh"
 
-extra_flags="$pentium64_cflags $debug_cflags -lasan -O -g -fsanitize=address"
+extra_flags="$pentium64_cflags $debug_cflags -lasan -O -g -fsanitize=address -USAFEMALLOC -UFORCE_INIT_OF_VARS -Wno-uninitialized -Wno-maybe-uninitialized"
 extra_configs="$pentium_configs $debug_configs $valgrind_configs $max_configs $disable_asan_plugins"
 export LDFLAGS="-ldl"
 
diff --git a/extra/innochecksum.cc b/extra/innochecksum.cc
index b6f478b943b..40aefa24428 100644
--- a/extra/innochecksum.cc
+++ b/extra/innochecksum.cc
@@ -568,7 +568,8 @@ is_page_corrupted(
 								page_size);
 		if (is_corrupted && log_file) {
 			fprintf(log_file,
-				"Page " ULINTPF ":%llu may be corrupted;"
+				"[page id: space=" ULINTPF
+				", page_number=%llu] may be corrupted;"
 				" key_version=%u\n",
 				space_id, cur_page_num,
 				mach_read_from_4(
diff --git a/extra/mariabackup/xtrabackup.cc b/extra/mariabackup/xtrabackup.cc
index 8775584bff0..a3bcff03e51 100644
--- a/extra/mariabackup/xtrabackup.cc
+++ b/extra/mariabackup/xtrabackup.cc
@@ -1295,7 +1295,7 @@ struct my_option xb_server_options[] =
   {"innodb_log_file_size", OPT_INNODB_LOG_FILE_SIZE,
    "Ignored for mysqld option compatibility",
    (G_PTR*) &srv_log_file_size, (G_PTR*) &srv_log_file_size, 0,
-   GET_ULL, REQUIRED_ARG, 48 << 20, 1 << 20, 512ULL << 30, 0,
+   GET_ULL, REQUIRED_ARG, 48 << 20, 1 << 20, log_group_max_size, 0,
    UNIV_PAGE_SIZE_MAX, 0},
   {"innodb_log_files_in_group", OPT_INNODB_LOG_FILES_IN_GROUP,
    "Ignored for mysqld option compatibility",
diff --git a/include/my_valgrind.h b/include/my_valgrind.h
index 9159ec5b138..08ad3f46b96 100644
--- a/include/my_valgrind.h
+++ b/include/my_valgrind.h
@@ -55,12 +55,19 @@ https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning */
 #endif /* HAVE_VALGRIND_MEMCHECK_H */
 
 #if defined(TRASH_FREED_MEMORY)
+/* NOTE: Do not invoke TRASH_FILL directly! Use TRASH_ALLOC or TRASH_FREE.
+
+The MEM_UNDEFINED() call before memset() is for canceling the effect
+of any previous MEM_NOACCESS(). We must invoke MEM_UNDEFINED() after
+writing the dummy pattern, unless MEM_NOACCESS() is going to be invoked.
+On AddressSanitizer, the MEM_UNDEFINED() in TRASH_ALLOC() has no effect. */
 #define TRASH_FILL(A,B,C) do { const size_t trash_tmp= (B); MEM_UNDEFINED(A, trash_tmp); memset(A, C, trash_tmp); } while (0)
 #else
-#define TRASH_FILL(A,B,C) do { MEM_UNDEFINED((A), (B)); } while (0)
+#define TRASH_FILL(A,B,C) while (0)
 #endif
-
+/** Note that some memory became allocated or uninitialized. */
 #define TRASH_ALLOC(A,B) do { TRASH_FILL(A,B,0xA5); MEM_UNDEFINED(A,B); } while(0)
+/** Note that some memory became freed. (Prohibit further access to it.) */
 #define TRASH_FREE(A,B) do { TRASH_FILL(A,B,0x8F); MEM_NOACCESS(A,B); } while(0)
 
 #endif /* MY_VALGRIND_INCLUDED */
diff --git a/mysql-test/include/wait_for_slave_param.inc b/mysql-test/include/wait_for_slave_param.inc
index 25020d46ed9..b06dee3c640 100644
--- a/mysql-test/include/wait_for_slave_param.inc
+++ b/mysql-test/include/wait_for_slave_param.inc
@@ -69,7 +69,7 @@ if (!$_slave_param_comparison)
 
 if ($rpl_debug)
 {
-  --echo Waiting until '$slave_param' $_slave_param_comparison '$slave_param_value' [timeout='$_slave_timeout', \$slave_error_param='$slave_error_param']
+  --echo Waiting until '$slave_param' $_slave_param_comparison '$slave_param_value' [\$slave_error_param='$slave_error_param']
 }
 
 --let $_slave_check_configured= query_get_value("SHOW SLAVE STATUS", Slave_IO_Running, 1)
diff --git a/mysql-test/main/connect_debug.test b/mysql-test/main/connect_debug.test
index 7a2f2872b79..300a2de0fbd 100644
--- a/mysql-test/main/connect_debug.test
+++ b/mysql-test/main/connect_debug.test
@@ -17,6 +17,7 @@ drop user bad;
 #
 set global debug_dbug='+d,auth_invalid_plugin';
 create user 'bad' identified by 'worse';
+--replace_regex /loaded: [^\n]*/loaded: invalid plugin name/
 --error 1
 --exec $MYSQL --default-auth=mysql_old_password --user=bad --password=worse 2>&1
 set global debug_dbug=@old_dbug;
diff --git a/mysql-test/main/kill.result b/mysql-test/main/kill.result
index dc1cb9252da..e3fbb830df5 100644
--- a/mysql-test/main/kill.result
+++ b/mysql-test/main/kill.result
@@ -399,3 +399,19 @@ DROP USER u1@localhost;
 SET DEBUG_SYNC = 'RESET';
 DROP FUNCTION MY_KILL;
 set global sql_mode=default;
+#
+# MDEV-17998
+# Deadlock and eventual Assertion `!table->pos_in_locked_tables' failed
+# in tc_release_table on KILL_TIMEOUT
+#
+SET max_statement_time= 2;
+CREATE TABLE t1 (a INT);
+CREATE VIEW v1 AS SELECT * FROM t1;
+CREATE TABLE t2 (b INT, c INT);
+LOCK TABLES v1 READ, t2 WRITE, t1 WRITE;
+ALTER TABLE t1 CHANGE f1 f2 DOUBLE;
+Got one of the listed errors
+ALTER TABLE t2 DROP c;
+UNLOCK TABLES;
+DROP VIEW v1;
+DROP TABLE t1, t2;
diff --git a/mysql-test/main/kill.test b/mysql-test/main/kill.test
index b6000ffced1..e5ebdadd8a8 100644
--- a/mysql-test/main/kill.test
+++ b/mysql-test/main/kill.test
@@ -642,3 +642,23 @@ SET DEBUG_SYNC = 'RESET';
 DROP FUNCTION MY_KILL;
 
 set global sql_mode=default;
+
+--echo #
+--echo # MDEV-17998
+--echo # Deadlock and eventual Assertion `!table->pos_in_locked_tables' failed
+--echo # in tc_release_table on KILL_TIMEOUT
+--echo #
+
+SET max_statement_time= 2;
+
+CREATE TABLE t1 (a INT);
+CREATE VIEW v1 AS SELECT * FROM t1;
+CREATE TABLE t2 (b INT, c INT);
+ 
+LOCK TABLES v1 READ, t2 WRITE, t1 WRITE;
+--error ER_BAD_FIELD_ERROR,ER_STATEMENT_TIMEOUT
+ALTER TABLE t1 CHANGE f1 f2 DOUBLE;
+ALTER TABLE t2 DROP c;
+UNLOCK TABLES;
+DROP VIEW v1;
+DROP TABLE t1, t2;
diff --git a/mysql-test/mysql-test-run.pl b/mysql-test/mysql-test-run.pl
index bd1034adfe1..35fefec5e4f 100755
--- a/mysql-test/mysql-test-run.pl
+++ b/mysql-test/mysql-test-run.pl
@@ -3329,6 +3329,7 @@ sub mysql_install_db {
   # Create the bootstrap.sql file
   # ----------------------------------------------------------------------
   my $bootstrap_sql_file= "$opt_vardir/log/bootstrap.sql";
+  $ENV{'MYSQL_BOOTSTRAP_SQL_FILE'}= $bootstrap_sql_file;
 
   if (! -e $bootstrap_sql_file)
   {
diff --git a/mysql-test/suite/binlog/r/binlog_unsafe.result b/mysql-test/suite/binlog/r/binlog_unsafe.result
index d235237c69c..a8143ef086e 100644
--- a/mysql-test/suite/binlog/r/binlog_unsafe.result
+++ b/mysql-test/suite/binlog/r/binlog_unsafe.result
@@ -2675,6 +2675,8 @@ Warnings:
 Note	1592	Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. CREATE... REPLACE SELECT is unsafe because the order in which rows are retrieved by the SELECT determines which (if any) rows are replaced. This order cannot be predicted and may differ on master and the slave
 INSERT INTO insert_2_keys VALUES (1, 2) 
 ON DUPLICATE KEY UPDATE a=VALUES(a)+10, b=VALUES(b)+10;
+Warnings:
+Note	1592	Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT... ON DUPLICATE KEY UPDATE  on a table with more than one UNIQUE KEY is unsafe
 DROP TABLE filler_table;
 DROP TABLE insert_table;
 DROP TABLE update_table;
diff --git a/mysql-test/suite/galera/disabled.def b/mysql-test/suite/galera/disabled.def
index a688b6abd9a..47f95db937d 100644
--- a/mysql-test/suite/galera/disabled.def
+++ b/mysql-test/suite/galera/disabled.def
@@ -11,9 +11,6 @@
 ##############################################################################
 
 MW-286 : MDEV-19992 Galera test failure on MW-286
-MW-328A : MDEV-17847 Galera test failure on MW-328[A|B|C]
-MW-328B : MDEV-17847 Galera test failure on MW-328[A|B|C]
-MW-328C : MDEV-17847 Galera test failure on MW-328[A|B|C]
 MW-329 : MDEV-19962 Galera test failure on MW-329
 MW-388: MDEV-19803 Long semaphore wait error on galera.MW-388	
 galera_account_management : MariaDB 10.0 does not support ALTER USER
diff --git a/mysql-test/suite/galera/r/MW-328A.result b/mysql-test/suite/galera/r/MW-328A.result
index db0301b6bf2..535f04bfbab 100644
--- a/mysql-test/suite/galera/r/MW-328A.result
+++ b/mysql-test/suite/galera/r/MW-328A.result
@@ -14,14 +14,29 @@ connection node_1X;
 CALL proc_update();;
 connection node_2;
 SET SESSION wsrep_retry_autocommit = 0;
-have_successes
-1
-have_deadlocks
-1
 connection node_1;
 connection node_1X;
 Got one of the listed errors
 connection node_1;
 DROP PROCEDURE proc_update;
 DROP TABLE t1, t2;
-CALL mtr.add_suppression("conflict state 3 after post commit");
+CALL mtr.add_suppression("conflict state ABORTED after post commit");
+connection node_1;
+CREATE TABLE t1 (i int primary key, j int) engine=innodb;
+INSERT INTO t1 values (1,0);
+BEGIN;
+UPDATE t1 SET j=1 WHERE i=1;
+connection node_2;
+UPDATE t1 SET j=2 WHERE i=1;
+connection node_1;
+COMMIT;
+ERROR 40001: Deadlock: wsrep aborted transaction
+SELECT * FROM t1;
+i	j
+1	2
+connection node_2;
+SELECT * FROM t1;
+i	j
+1	2
+connection node_1;
+DROP TABLE t1;
diff --git a/mysql-test/suite/galera/r/MW-328B.result b/mysql-test/suite/galera/r/MW-328B.result
index e898e315ca8..531c5de8029 100644
--- a/mysql-test/suite/galera/r/MW-328B.result
+++ b/mysql-test/suite/galera/r/MW-328B.result
@@ -20,4 +20,4 @@ Got one of the listed errors
 connection node_1;
 DROP PROCEDURE proc_update;
 DROP TABLE t1, t2;
-CALL mtr.add_suppression("conflict state 3 after post commit");
+CALL mtr.add_suppression("conflict state ABORTED after post commit");
diff --git a/mysql-test/suite/galera/r/MW-328C.result b/mysql-test/suite/galera/r/MW-328C.result
index d8e164e7b4a..f8d747c5df1 100644
--- a/mysql-test/suite/galera/r/MW-328C.result
+++ b/mysql-test/suite/galera/r/MW-328C.result
@@ -20,4 +20,4 @@ Got one of the listed errors
 connection node_1;
 DROP PROCEDURE proc_update;
 DROP TABLE t1, t2;
-CALL mtr.add_suppression("conflict state 3 after post commit");
+CALL mtr.add_suppression("conflict state ABORTED after post commit");
diff --git a/mysql-test/suite/galera/r/galera_performance_schema.result b/mysql-test/suite/galera/r/galera_performance_schema.result
new file mode 100644
index 00000000000..5b4994556d6
--- /dev/null
+++ b/mysql-test/suite/galera/r/galera_performance_schema.result
@@ -0,0 +1,44 @@
+use performance_schema;
+SELECT name
+FROM threads
+WHERE name LIKE 'thread/sql/wsrep%'
+ORDER BY name;
+name	thread/sql/wsrep_applier_thread
+name	thread/sql/wsrep_rollbacker_thread
+use test;
+create table t1 (a int not null primary key) engine=innodb;
+insert into t1 values (1),(2);
+use performance_schema;
+select name from mutex_instances where name like 'wait/synch/mutex/sql/LOCK_wsrep%' order by name;
+name	wait/synch/mutex/sql/LOCK_wsrep_config_state
+name	wait/synch/mutex/sql/LOCK_wsrep_desync
+name	wait/synch/mutex/sql/LOCK_wsrep_ready
+name	wait/synch/mutex/sql/LOCK_wsrep_replaying
+name	wait/synch/mutex/sql/LOCK_wsrep_rollback
+name	wait/synch/mutex/sql/LOCK_wsrep_slave_threads
+name	wait/synch/mutex/sql/LOCK_wsrep_sst
+name	wait/synch/mutex/sql/LOCK_wsrep_sst_init
+select name from cond_instances where name like 'wait/synch/cond/sql/COND_wsrep%' order by name;
+name	wait/synch/cond/sql/COND_wsrep_ready
+name	wait/synch/cond/sql/COND_wsrep_replaying
+name	wait/synch/cond/sql/COND_wsrep_rollback
+name	wait/synch/cond/sql/COND_wsrep_sst
+name	wait/synch/cond/sql/COND_wsrep_sst_init
+connection node_2;
+use test;
+SET SESSION wsrep_on=OFF;
+CREATE TABLE t2 (f1 INTEGER) engine=innodb;
+connection node_1;
+use test;
+CREATE TABLE t2 (f1 INTEGER) engine=innodb;
+connection node_2;
+SET SESSION wsrep_on=ON;
+SELECT COUNT(*) FROM t1;
+COUNT(*)	2
+use performance_schema;
+select count(*)>=1 from file_instances where file_name like '%GRA_%.log';
+count(*)>=1	1
+CALL mtr.add_suppression("Slave SQL: Error 'Table 't2' already exists' on query");
+use test;
+drop table t1;
+drop table t2;
diff --git a/mysql-test/suite/galera/t/MW-328-footer.inc b/mysql-test/suite/galera/t/MW-328-footer.inc
index 5b736df220f..12a4bf12590 100644
--- a/mysql-test/suite/galera/t/MW-328-footer.inc
+++ b/mysql-test/suite/galera/t/MW-328-footer.inc
@@ -15,4 +15,4 @@
 DROP PROCEDURE proc_update;
 DROP TABLE t1, t2;
 
-CALL mtr.add_suppression("conflict state 3 after post commit");
+CALL mtr.add_suppression("conflict state ABORTED after post commit");
diff --git a/mysql-test/suite/galera/t/MW-328A.test b/mysql-test/suite/galera/t/MW-328A.test
index 09aad1bcf60..dd692a292b8 100644
--- a/mysql-test/suite/galera/t/MW-328A.test
+++ b/mysql-test/suite/galera/t/MW-328A.test
@@ -3,13 +3,17 @@
 # 
 
 #
-# Attempt to insert into t2 and check if insert actually inserted rows if 
-# a success was reported.
+# test phase 1 is not deterministic
+#
+# Here we attempt to insert into t2 and check if insert actually
+# inserted rows if a success was reported.
+#
+# However, deadlocks may or may not happen in this test execution
+# it all depends on timing.
 #
 
---source include/big_test.inc
 --source include/galera_cluster.inc
---source include/have_innodb.inc
+--source include/big_test.inc
 --source suite/galera/t/MW-328-header.inc
 
 --connection node_2
@@ -25,7 +29,7 @@ while ($count)
 {
   TRUNCATE TABLE t2;
 
-  --error 0,1213
+  --error 0,ER_LOCK_DEADLOCK
   INSERT IGNORE INTO t2 SELECT f2 FROM t1;
   if ($mysql_errno != 1213) {
     --inc $successes
@@ -44,14 +48,31 @@ while ($count)
 
 --enable_query_log
 
-#
-# Check that the test produced both deadlocks and successes
-#
-
---disable_query_log
---eval SELECT $successes > 0 AS have_successes
---eval SELECT $deadlocks > 0 AS have_deadlocks
---enable_query_log
-
 
 --source suite/galera/t/MW-328-footer.inc
+
+#
+# Test phase 2 is deterministic
+# Here we generate a sure conflict in node 1 and verify that
+# insert failed in both nodes
+#
+--connection node_1
+CREATE TABLE t1 (i int primary key, j int) engine=innodb;
+INSERT INTO t1 values (1,0);
+
+BEGIN;
+UPDATE t1 SET j=1 WHERE i=1;
+
+--connection node_2
+UPDATE t1 SET j=2 WHERE i=1;
+
+--connection node_1
+--error ER_LOCK_DEADLOCK
+COMMIT;
+
+SELECT * FROM t1;
+--connection node_2
+SELECT * FROM t1;
+--connection node_1
+
+DROP TABLE t1;
diff --git a/mysql-test/suite/galera/t/MW-328B.test b/mysql-test/suite/galera/t/MW-328B.test
index 000b0d8a9ab..11969dd0b47 100644
--- a/mysql-test/suite/galera/t/MW-328B.test
+++ b/mysql-test/suite/galera/t/MW-328B.test
@@ -7,9 +7,7 @@
 # gets the deadlock error
 #
 
---source include/big_test.inc
 --source include/galera_cluster.inc
---source include/have_innodb.inc
 --source suite/galera/t/MW-328-header.inc
 
 --connection node_2
diff --git a/mysql-test/suite/galera/t/MW-328C.test b/mysql-test/suite/galera/t/MW-328C.test
index 72a8480923c..1594547d0de 100644
--- a/mysql-test/suite/galera/t/MW-328C.test
+++ b/mysql-test/suite/galera/t/MW-328C.test
@@ -7,9 +7,8 @@
 # masks all deadlock errors
 #
 
---source include/big_test.inc
 --source include/galera_cluster.inc
---source include/have_innodb.inc
+--source include/big_test.inc
 --source suite/galera/t/MW-328-header.inc
 
 --connection node_2
diff --git a/mysql-test/suite/galera/t/MW-328D.test b/mysql-test/suite/galera/t/MW-328D.test
index d5cffdb8f47..e8a22f22a99 100644
--- a/mysql-test/suite/galera/t/MW-328D.test
+++ b/mysql-test/suite/galera/t/MW-328D.test
@@ -7,7 +7,6 @@
 #
 
 --source include/galera_cluster.inc
---source include/have_innodb.inc
 
 CREATE TABLE t1 (i INT) ENGINE = InnoDB;
 INSERT INTO t1 (i) VALUES(1);
diff --git a/mysql-test/suite/galera/t/MW-328E.test b/mysql-test/suite/galera/t/MW-328E.test
index fd4b0bf9039..34b17be7b08 100644
--- a/mysql-test/suite/galera/t/MW-328E.test
+++ b/mysql-test/suite/galera/t/MW-328E.test
@@ -7,7 +7,6 @@
 #
 
 --source include/galera_cluster.inc
---source include/have_innodb.inc
 
 create table t1 (i int primary key, j int) engine=innodb;
 create table t2 (i int primary key, j int) engine=innodb;
diff --git a/mysql-test/suite/galera/t/galera_performance_schema.test b/mysql-test/suite/galera/t/galera_performance_schema.test
new file mode 100644
index 00000000000..d54555ea301
--- /dev/null
+++ b/mysql-test/suite/galera/t/galera_performance_schema.test
@@ -0,0 +1,58 @@
+#
+# Test that wsrep mutexes, condition variables, files and
+# threads are shown in performance schema
+#
+--source include/galera_cluster.inc
+--source include/have_perfschema.inc
+
+use performance_schema;
+
+--vertical_results
+--disable_ps_protocol
+SELECT name
+FROM threads
+WHERE name LIKE 'thread/sql/wsrep%'
+ORDER BY name;
+--enable_ps_protocol
+
+use test;
+create table t1 (a int not null primary key) engine=innodb;
+insert into t1 values (1),(2);
+
+use performance_schema;
+select name from mutex_instances where name like 'wait/synch/mutex/sql/LOCK_wsrep%' order by name;
+select name from cond_instances where name like 'wait/synch/cond/sql/COND_wsrep%' order by name;
+# Whenever a node fails to apply an event on a slave node, the database server creates a
+# special binary log file of the event in the data directory. The naming convention the
+# node uses for the filename is GRA_*.log.
+# Thus, we need to produce a applier failure
+
+--connection node_2
+--exec rm -rf $MYSQLTEST_VARDIR/mysqld.2/data/GRA_*.log
+
+# Create applier failure
+
+use test;
+SET SESSION wsrep_on=OFF;
+CREATE TABLE t2 (f1 INTEGER) engine=innodb;
+
+--connection node_1
+use test;
+CREATE TABLE t2 (f1 INTEGER) engine=innodb;
+
+--connection node_2
+SET SESSION wsrep_on=ON;
+SELECT COUNT(*) FROM t1;
+
+use performance_schema;
+#
+# Below we can't just count number of files as if you run this test more
+# than once, test will create more files
+#
+select count(*)>=1 from file_instances where file_name like '%GRA_%.log';
+CALL mtr.add_suppression("Slave SQL: Error 'Table 't2' already exists' on query");
+
+use test;
+drop table t1;
+drop table t2;
+
diff --git a/mysql-test/suite/innodb/include/innodb_bulk_create_index_debug.inc b/mysql-test/suite/innodb/include/innodb_bulk_create_index_debug.inc
index 48de3a1962d..85466e5e4ae 100644
--- a/mysql-test/suite/innodb/include/innodb_bulk_create_index_debug.inc
+++ b/mysql-test/suite/innodb/include/innodb_bulk_create_index_debug.inc
@@ -4,17 +4,8 @@
 
 # Not supported in embedded
 -- source include/not_embedded.inc
-
-# This test case needs to crash the server. Needs a debug server.
 -- source include/have_debug.inc
-
-# Don't test this under valgrind, memory leaks will occur.
--- source include/not_valgrind.inc
-
-# Avoid CrashReporter popup on Mac
--- source include/not_crashrep.inc
-
--- source include/have_innodb.inc
+-- source include/have_debug_sync.inc
 
 # Create Insert Procedure
 DELIMITER |;
@@ -62,21 +53,11 @@ CALL populate_t1();
 
 SELECT COUNT(*) FROM t1;
 
+--enable_info
 CREATE INDEX idx_title ON t1(title);
+--disable_info
 
---source include/restart_mysqld.inc
-
-CHECK TABLE t1;
-
-SELECT * FROM t1 WHERE title = 'a10';
-
-SELECT * FROM t1 WHERE title = 'a5000';
-
-SELECT * FROM t1 WHERE title = 'a10000';
-
-SELECT * FROM t1 WHERE title = 'a10010';
-
-DROP TABLE t1;
+RENAME TABLE t1 TO t0;
 
 -- echo # Test Blob
 
@@ -104,16 +85,32 @@ INSERT INTO t1 VALUES
 
 SELECT CHAR_LENGTH(b) FROM t1;
 
-ALTER TABLE t1 DROP COLUMN c;
+--enable_info
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+--disable_info
 
 --source include/restart_mysqld.inc
 
-CHECK TABLE t1;
+CHECK TABLE t0,t1;
 
 SELECT CHAR_LENGTH(b) FROM t1;
 
 DROP TABLE t1;
 
+RENAME TABLE t0 to t1;
+
+CHECK TABLE t1;
+
+SELECT * FROM t1 WHERE title = 'a10';
+
+SELECT * FROM t1 WHERE title = 'a5000';
+
+SELECT * FROM t1 WHERE title = 'a10000';
+
+SELECT * FROM t1 WHERE title = 'a10010';
+
+DROP TABLE t1;
+
 # Test Crash Recovery
 
 if ($row_format != 'COMPRESSED')
@@ -140,17 +137,16 @@ if ($row_format == 'COMPRESSED')
 CALL populate_t1();
 -- enable_query_log
 
-SET debug_dbug='+d,crash_commit_before';
-
-# Write file to make mysql-test-run.pl start up the server again
---exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
-
---error 2013
+connect (hang,localhost,root);
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
+send
 CREATE INDEX idx_title ON t1(title);
 
---enable_reconnect
---source include/wait_until_connected_again.inc
---disable_reconnect
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+let $shutdown_timeout=0;
+--source include/restart_mysqld.inc
+disconnect hang;
 
 SELECT COUNT(*) FROM t1;
 
@@ -194,17 +190,16 @@ INSERT INTO t1 VALUES
 
 SELECT CHAR_LENGTH(b) FROM t1;
 
-SET debug_dbug='+d,crash_commit_before';
+connect (hang,localhost,root);
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
+send
+ALTER TABLE t1 DROP COLUMN c, FORCE;
 
-# Write file to make mysql-test-run.pl start up the server again
---exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
-
---error 2013
-ALTER TABLE t1 DROP COLUMN c;
-
---enable_reconnect
---source include/wait_until_connected_again.inc
---disable_reconnect
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+--source include/restart_mysqld.inc
+disconnect hang;
+let $shutdown_timeout=60;
 
 CHECK TABLE t1;
 
@@ -212,10 +207,4 @@ SELECT CHAR_LENGTH(b) FROM t1;
 
 DROP TABLE t1;
 
-# Restore global variables
-if ($row_format == 'COMPRESSED')
-{
-  SET GLOBAL innodb_file_per_table=default;
-}
-
 DROP PROCEDURE populate_t1;
diff --git a/mysql-test/suite/innodb/r/alter_copy.result b/mysql-test/suite/innodb/r/alter_copy.result
index ac25c6c6c18..35f4eb3b2c2 100644
--- a/mysql-test/suite/innodb/r/alter_copy.result
+++ b/mysql-test/suite/innodb/r/alter_copy.result
@@ -37,7 +37,9 @@ t1	CREATE TABLE `t1` (
   FULLTEXT KEY `b_2` (`b`,`c`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1
 ALTER TABLE t1 FORCE, ALGORITHM=COPY;
-SET DEBUG_DBUG='+d,crash_commit_before';
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_copy_trans_commit SIGNAL hung WAIT_FOR ever';
+# create 32 secondary indexes
 ALTER TABLE t ADD INDEX(b,c,d,a),ADD INDEX(b,c,a,d),ADD INDEX(b,a,c,d),ADD INDEX(b,a,d,c),
 ADD INDEX(b,d,a,c),ADD INDEX(b,d,c,a),ADD INDEX(a,b,c,d),ADD INDEX(a,b,d,c),
 ADD INDEX(a,c,b,d),ADD INDEX(a,c,d,b),ADD INDEX(a,d,b,c),ADD INDEX(a,d,c,b),
@@ -47,7 +49,9 @@ ADD INDEX(d,b,a,c),ADD INDEX(d,b,c,a),ADD INDEX(d,c,a,b),ADD INDEX(d,c,b,a),
 ADD INDEX(a,b,c), ADD INDEX(a,c,b), ADD INDEX(a,c,d), ADD INDEX(a,d,c),
 ADD INDEX(a,b,d), ADD INDEX(a,d,b), ADD INDEX(b,c,d), ADD INDEX(b,d,c),
 ALGORITHM=COPY;
-ERROR HY000: Lost connection to MySQL server during query
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 #sql-temporary.frm
 #sql-temporary.ibd
 FTS_INDEX_1.ibd
diff --git a/mysql-test/suite/innodb/r/auto_increment_dup,skip-log-bin.rdiff b/mysql-test/suite/innodb/r/auto_increment_dup,skip-log-bin.rdiff
deleted file mode 100644
index 7b4ec54eed8..00000000000
--- a/mysql-test/suite/innodb/r/auto_increment_dup,skip-log-bin.rdiff
+++ /dev/null
@@ -1,51 +0,0 @@
---- auto_increment_dup.result
-+++ auto_increment_dup,skip-log-bin.reject
-@@ -89,13 +89,14 @@
- SET DEBUG_SYNC='execute_command_after_close_tables SIGNAL continue';
- affected rows: 0
- INSERT INTO t1(k) VALUES (2), (4), (5) ON DUPLICATE KEY UPDATE c='2';
--ERROR HY000: Lock wait timeout exceeded; try restarting transaction
-+affected rows: 3
-+info: Records: 3  Duplicates: 0  Warnings: 0
- connection con1;
- #
- # 2 duplicates
- #
--affected rows: 3
--info: Records: 3  Duplicates: 0  Warnings: 0
-+affected rows: 4
-+info: Records: 3  Duplicates: 1  Warnings: 0
- connection default;
- #
- # 3 rows
-@@ -103,19 +104,21 @@
- SELECT * FROM t1 order by k;
- id	k	c
- 1	1	NULL
--2	2	NULL
--3	3	NULL
--affected rows: 3
-+4	2	1
-+2	3	NULL
-+5	4	NULL
-+6	5	NULL
-+affected rows: 5
- INSERT INTO t1(k) VALUES (2), (4), (5) ON DUPLICATE KEY UPDATE c='2';
--affected rows: 4
--info: Records: 3  Duplicates: 1  Warnings: 0
-+affected rows: 6
-+info: Records: 3  Duplicates: 3  Warnings: 0
- SELECT * FROM t1 order by k;
- id	k	c
- 1	1	NULL
--2	2	2
--3	3	NULL
--7	4	NULL
--8	5	NULL
-+4	2	2
-+2	3	NULL
-+5	4	2
-+6	5	2
- affected rows: 5
- disconnect con1;
- disconnect con2;
diff --git a/mysql-test/suite/innodb/r/auto_increment_dup.result b/mysql-test/suite/innodb/r/auto_increment_dup.result
index 1467a459fc1..9926047b665 100644
--- a/mysql-test/suite/innodb/r/auto_increment_dup.result
+++ b/mysql-test/suite/innodb/r/auto_increment_dup.result
@@ -89,13 +89,14 @@ affected rows: 0
 SET DEBUG_SYNC='execute_command_after_close_tables SIGNAL continue';
 affected rows: 0
 INSERT INTO t1(k) VALUES (2), (4), (5) ON DUPLICATE KEY UPDATE c='2';
-ERROR HY000: Lock wait timeout exceeded; try restarting transaction
+affected rows: 3
+info: Records: 3  Duplicates: 0  Warnings: 0
 connection con1;
 #
 # 2 duplicates
 #
-affected rows: 3
-info: Records: 3  Duplicates: 0  Warnings: 0
+affected rows: 4
+info: Records: 3  Duplicates: 1  Warnings: 0
 connection default;
 #
 # 3 rows
@@ -103,19 +104,21 @@ connection default;
 SELECT * FROM t1 order by k;
 id	k	c
 1	1	NULL
-2	2	NULL
-3	3	NULL
-affected rows: 3
+4	2	1
+2	3	NULL
+5	4	NULL
+6	5	NULL
+affected rows: 5
 INSERT INTO t1(k) VALUES (2), (4), (5) ON DUPLICATE KEY UPDATE c='2';
-affected rows: 4
-info: Records: 3  Duplicates: 1  Warnings: 0
+affected rows: 6
+info: Records: 3  Duplicates: 3  Warnings: 0
 SELECT * FROM t1 order by k;
 id	k	c
 1	1	NULL
-2	2	2
-3	3	NULL
-7	4	NULL
-8	5	NULL
+4	2	2
+2	3	NULL
+5	4	2
+6	5	2
 affected rows: 5
 disconnect con1;
 disconnect con2;
diff --git a/mysql-test/suite/innodb/r/innodb_bulk_create_index_debug.result b/mysql-test/suite/innodb/r/innodb_bulk_create_index_debug.result
index cd5a3c340da..295a9f1bed8 100644
--- a/mysql-test/suite/innodb/r/innodb_bulk_create_index_debug.result
+++ b/mysql-test/suite/innodb/r/innodb_bulk_create_index_debug.result
@@ -17,21 +17,9 @@ SELECT COUNT(*) FROM t1;
 COUNT(*)
 10000
 CREATE INDEX idx_title ON t1(title);
-CHECK TABLE t1;
-Table	Op	Msg_type	Msg_text
-test.t1	check	status	OK
-SELECT * FROM t1 WHERE title = 'a10';
-class	id	title
-10	10	a10
-SELECT * FROM t1 WHERE title = 'a5000';
-class	id	title
-5000	5000	a5000
-SELECT * FROM t1 WHERE title = 'a10000';
-class	id	title
-10000	10000	a10000
-SELECT * FROM t1 WHERE title = 'a10010';
-class	id	title
-DROP TABLE t1;
+affected rows: 0
+info: Records: 0  Duplicates: 0  Warnings: 0
+RENAME TABLE t1 TO t0;
 # Test Blob
 CREATE TABLE t1(
 a INT PRIMARY KEY,
@@ -48,9 +36,12 @@ CHAR_LENGTH(b)
 20000
 40000
 60000
-ALTER TABLE t1 DROP COLUMN c;
-CHECK TABLE t1;
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+affected rows: 0
+info: Records: 0  Duplicates: 0  Warnings: 0
+CHECK TABLE t0,t1;
 Table	Op	Msg_type	Msg_text
+test.t0	check	status	OK
 test.t1	check	status	OK
 SELECT CHAR_LENGTH(b) FROM t1;
 CHAR_LENGTH(b)
@@ -59,14 +50,33 @@ CHAR_LENGTH(b)
 40000
 60000
 DROP TABLE t1;
+RENAME TABLE t0 to t1;
+CHECK TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	check	status	OK
+SELECT * FROM t1 WHERE title = 'a10';
+class	id	title
+10	10	a10
+SELECT * FROM t1 WHERE title = 'a5000';
+class	id	title
+5000	5000	a5000
+SELECT * FROM t1 WHERE title = 'a10000';
+class	id	title
+10000	10000	a10000
+SELECT * FROM t1 WHERE title = 'a10010';
+class	id	title
+DROP TABLE t1;
 CREATE TABLE t1(
 class   INT,
 id      INT,
 title   VARCHAR(100)
 ) ENGINE=InnoDB ROW_FORMAT=REDUNDANT;
-SET debug_dbug='+d,crash_commit_before';
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
 CREATE INDEX idx_title ON t1(title);
-ERROR HY000: Lost connection to MySQL server during query
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 SELECT COUNT(*) FROM t1;
 COUNT(*)
 10000
@@ -104,9 +114,12 @@ CHAR_LENGTH(b)
 20000
 40000
 60000
-SET debug_dbug='+d,crash_commit_before';
-ALTER TABLE t1 DROP COLUMN c;
-ERROR HY000: Lost connection to MySQL server during query
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 CHECK TABLE t1;
 Table	Op	Msg_type	Msg_text
 test.t1	check	status	OK
@@ -137,21 +150,9 @@ SELECT COUNT(*) FROM t1;
 COUNT(*)
 10000
 CREATE INDEX idx_title ON t1(title);
-CHECK TABLE t1;
-Table	Op	Msg_type	Msg_text
-test.t1	check	status	OK
-SELECT * FROM t1 WHERE title = 'a10';
-class	id	title
-10	10	a10
-SELECT * FROM t1 WHERE title = 'a5000';
-class	id	title
-5000	5000	a5000
-SELECT * FROM t1 WHERE title = 'a10000';
-class	id	title
-10000	10000	a10000
-SELECT * FROM t1 WHERE title = 'a10010';
-class	id	title
-DROP TABLE t1;
+affected rows: 0
+info: Records: 0  Duplicates: 0  Warnings: 0
+RENAME TABLE t1 TO t0;
 # Test Blob
 CREATE TABLE t1(
 a INT PRIMARY KEY,
@@ -168,9 +169,12 @@ CHAR_LENGTH(b)
 20000
 40000
 60000
-ALTER TABLE t1 DROP COLUMN c;
-CHECK TABLE t1;
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+affected rows: 0
+info: Records: 0  Duplicates: 0  Warnings: 0
+CHECK TABLE t0,t1;
 Table	Op	Msg_type	Msg_text
+test.t0	check	status	OK
 test.t1	check	status	OK
 SELECT CHAR_LENGTH(b) FROM t1;
 CHAR_LENGTH(b)
@@ -179,14 +183,33 @@ CHAR_LENGTH(b)
 40000
 60000
 DROP TABLE t1;
+RENAME TABLE t0 to t1;
+CHECK TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	check	status	OK
+SELECT * FROM t1 WHERE title = 'a10';
+class	id	title
+10	10	a10
+SELECT * FROM t1 WHERE title = 'a5000';
+class	id	title
+5000	5000	a5000
+SELECT * FROM t1 WHERE title = 'a10000';
+class	id	title
+10000	10000	a10000
+SELECT * FROM t1 WHERE title = 'a10010';
+class	id	title
+DROP TABLE t1;
 CREATE TABLE t1(
 class   INT,
 id      INT,
 title   VARCHAR(100)
 ) ENGINE=InnoDB ROW_FORMAT=COMPACT;
-SET debug_dbug='+d,crash_commit_before';
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
 CREATE INDEX idx_title ON t1(title);
-ERROR HY000: Lost connection to MySQL server during query
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 SELECT COUNT(*) FROM t1;
 COUNT(*)
 10000
@@ -224,9 +247,12 @@ CHAR_LENGTH(b)
 20000
 40000
 60000
-SET debug_dbug='+d,crash_commit_before';
-ALTER TABLE t1 DROP COLUMN c;
-ERROR HY000: Lost connection to MySQL server during query
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 CHECK TABLE t1;
 Table	Op	Msg_type	Msg_text
 test.t1	check	status	OK
@@ -257,21 +283,9 @@ SELECT COUNT(*) FROM t1;
 COUNT(*)
 10000
 CREATE INDEX idx_title ON t1(title);
-CHECK TABLE t1;
-Table	Op	Msg_type	Msg_text
-test.t1	check	status	OK
-SELECT * FROM t1 WHERE title = 'a10';
-class	id	title
-10	10	a10
-SELECT * FROM t1 WHERE title = 'a5000';
-class	id	title
-5000	5000	a5000
-SELECT * FROM t1 WHERE title = 'a10000';
-class	id	title
-10000	10000	a10000
-SELECT * FROM t1 WHERE title = 'a10010';
-class	id	title
-DROP TABLE t1;
+affected rows: 0
+info: Records: 0  Duplicates: 0  Warnings: 0
+RENAME TABLE t1 TO t0;
 # Test Blob
 CREATE TABLE t1(
 a INT PRIMARY KEY,
@@ -288,9 +302,12 @@ CHAR_LENGTH(b)
 20000
 40000
 60000
-ALTER TABLE t1 DROP COLUMN c;
-CHECK TABLE t1;
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+affected rows: 0
+info: Records: 0  Duplicates: 0  Warnings: 0
+CHECK TABLE t0,t1;
 Table	Op	Msg_type	Msg_text
+test.t0	check	status	OK
 test.t1	check	status	OK
 SELECT CHAR_LENGTH(b) FROM t1;
 CHAR_LENGTH(b)
@@ -299,14 +316,33 @@ CHAR_LENGTH(b)
 40000
 60000
 DROP TABLE t1;
+RENAME TABLE t0 to t1;
+CHECK TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	check	status	OK
+SELECT * FROM t1 WHERE title = 'a10';
+class	id	title
+10	10	a10
+SELECT * FROM t1 WHERE title = 'a5000';
+class	id	title
+5000	5000	a5000
+SELECT * FROM t1 WHERE title = 'a10000';
+class	id	title
+10000	10000	a10000
+SELECT * FROM t1 WHERE title = 'a10010';
+class	id	title
+DROP TABLE t1;
 CREATE TABLE t1(
 class   INT,
 id      INT,
 title   VARCHAR(100)
 ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
-SET debug_dbug='+d,crash_commit_before';
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
 CREATE INDEX idx_title ON t1(title);
-ERROR HY000: Lost connection to MySQL server during query
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 SELECT COUNT(*) FROM t1;
 COUNT(*)
 10000
@@ -344,9 +380,12 @@ CHAR_LENGTH(b)
 20000
 40000
 60000
-SET debug_dbug='+d,crash_commit_before';
-ALTER TABLE t1 DROP COLUMN c;
-ERROR HY000: Lost connection to MySQL server during query
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 CHECK TABLE t1;
 Table	Op	Msg_type	Msg_text
 test.t1	check	status	OK
@@ -378,21 +417,9 @@ SELECT COUNT(*) FROM t1;
 COUNT(*)
 10000
 CREATE INDEX idx_title ON t1(title);
-CHECK TABLE t1;
-Table	Op	Msg_type	Msg_text
-test.t1	check	status	OK
-SELECT * FROM t1 WHERE title = 'a10';
-class	id	title
-10	10	a10
-SELECT * FROM t1 WHERE title = 'a5000';
-class	id	title
-5000	5000	a5000
-SELECT * FROM t1 WHERE title = 'a10000';
-class	id	title
-10000	10000	a10000
-SELECT * FROM t1 WHERE title = 'a10010';
-class	id	title
-DROP TABLE t1;
+affected rows: 0
+info: Records: 0  Duplicates: 0  Warnings: 0
+RENAME TABLE t1 TO t0;
 # Test Blob
 SET GLOBAL innodb_file_per_table=1;
 CREATE TABLE t1(
@@ -410,9 +437,12 @@ CHAR_LENGTH(b)
 20000
 40000
 60000
-ALTER TABLE t1 DROP COLUMN c;
-CHECK TABLE t1;
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+affected rows: 0
+info: Records: 0  Duplicates: 0  Warnings: 0
+CHECK TABLE t0,t1;
 Table	Op	Msg_type	Msg_text
+test.t0	check	status	OK
 test.t1	check	status	OK
 SELECT CHAR_LENGTH(b) FROM t1;
 CHAR_LENGTH(b)
@@ -421,15 +451,34 @@ CHAR_LENGTH(b)
 40000
 60000
 DROP TABLE t1;
+RENAME TABLE t0 to t1;
+CHECK TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	check	status	OK
+SELECT * FROM t1 WHERE title = 'a10';
+class	id	title
+10	10	a10
+SELECT * FROM t1 WHERE title = 'a5000';
+class	id	title
+5000	5000	a5000
+SELECT * FROM t1 WHERE title = 'a10000';
+class	id	title
+10000	10000	a10000
+SELECT * FROM t1 WHERE title = 'a10010';
+class	id	title
+DROP TABLE t1;
 SET GLOBAL innodb_file_per_table=1;
 CREATE TABLE t1(
 class   INT,
 id      INT,
 title   VARCHAR(100)
 ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4;
-SET debug_dbug='+d,crash_commit_before';
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
 CREATE INDEX idx_title ON t1(title);
-ERROR HY000: Lost connection to MySQL server during query
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 SELECT COUNT(*) FROM t1;
 COUNT(*)
 10000
@@ -468,9 +517,12 @@ CHAR_LENGTH(b)
 20000
 40000
 60000
-SET debug_dbug='+d,crash_commit_before';
-ALTER TABLE t1 DROP COLUMN c;
-ERROR HY000: Lost connection to MySQL server during query
+connect  hang,localhost,root;
+SET DEBUG_SYNC='alter_table_inplace_trans_commit SIGNAL hung WAIT_FOR ever';
+ALTER TABLE t1 DROP COLUMN c, FORCE;
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+disconnect hang;
 CHECK TABLE t1;
 Table	Op	Msg_type	Msg_text
 test.t1	check	status	OK
@@ -481,5 +533,4 @@ CHAR_LENGTH(b)
 40000
 60000
 DROP TABLE t1;
-SET GLOBAL innodb_file_per_table=default;
 DROP PROCEDURE populate_t1;
diff --git a/mysql-test/suite/innodb/t/alter_copy.test b/mysql-test/suite/innodb/t/alter_copy.test
index 0dce61994b5..b7ab05a061a 100644
--- a/mysql-test/suite/innodb/t/alter_copy.test
+++ b/mysql-test/suite/innodb/t/alter_copy.test
@@ -37,11 +37,11 @@ SELECT * FROM t1 WHERE MATCH(b,c) AGAINST ('column');
 SHOW CREATE TABLE t1;
 ALTER TABLE t1 FORCE, ALGORITHM=COPY;
 
-# crash right after the last write_row(), before the first commit of ALTER TABLE
---source include/expect_crash.inc
+# kill right after the last write_row(), before the first commit of ALTER TABLE
+connect (hang,localhost,root);
 
-SET DEBUG_DBUG='+d,crash_commit_before';
---error 2013
+SET DEBUG_SYNC='alter_table_copy_trans_commit SIGNAL hung WAIT_FOR ever';
+send
 # create 32 secondary indexes
 ALTER TABLE t ADD INDEX(b,c,d,a),ADD INDEX(b,c,a,d),ADD INDEX(b,a,c,d),ADD INDEX(b,a,d,c),
               ADD INDEX(b,d,a,c),ADD INDEX(b,d,c,a),ADD INDEX(a,b,c,d),ADD INDEX(a,b,d,c),
@@ -53,8 +53,13 @@ ALTER TABLE t ADD INDEX(b,c,d,a),ADD INDEX(b,c,a,d),ADD INDEX(b,a,c,d),ADD INDEX
               ADD INDEX(a,b,d), ADD INDEX(a,d,b), ADD INDEX(b,c,d), ADD INDEX(b,d,c),
  	      ALGORITHM=COPY;
 
+connection default;
+SET DEBUG_SYNC='now WAIT_FOR hung';
+let $shutdown_timeout=0;
 --let $restart_parameters= --innodb-force-recovery=3
---source include/start_mysqld.inc
+--source include/restart_mysqld.inc
+disconnect hang;
+let $shutdown_timeout=;
 let $datadir=`select @@datadir`;
 --replace_regex /#sql-[0-9a-f_]*/#sql-temporary/ /FTS_[0-9a-f]*_[0-9a-f]*/FTS/
 --list_files $datadir/test
diff --git a/mysql-test/suite/innodb/t/auto_increment_dup.test b/mysql-test/suite/innodb/t/auto_increment_dup.test
index aa399e5966d..9e54a6a8a66 100644
--- a/mysql-test/suite/innodb/t/auto_increment_dup.test
+++ b/mysql-test/suite/innodb/t/auto_increment_dup.test
@@ -8,8 +8,6 @@
 --source include/have_debug_sync.inc
 --source include/innodb_binlog.inc
 
-let $stmt= `SELECT @@GLOBAL.log_bin`;
-
 set global transaction isolation level repeatable read;
 
 CREATE TABLE t1(
@@ -84,13 +82,7 @@ SET DEBUG_SYNC='ha_write_row_end SIGNAL write_row_done WAIT_FOR continue';
 --reap
 
 SET DEBUG_SYNC='execute_command_after_close_tables SIGNAL continue';
-if ($stmt) {
---error ER_LOCK_WAIT_TIMEOUT
 INSERT INTO t1(k) VALUES (2), (4), (5) ON DUPLICATE KEY UPDATE c='2';
-}
-if (!$stmt) {
-INSERT INTO t1(k) VALUES (2), (4), (5) ON DUPLICATE KEY UPDATE c='2';
-}
 
 --connection con1
 --echo #
diff --git a/mysql-test/suite/mariabackup/big_innodb_log.result b/mysql-test/suite/mariabackup/big_innodb_log.result
new file mode 100644
index 00000000000..6577c0c9aae
--- /dev/null
+++ b/mysql-test/suite/mariabackup/big_innodb_log.result
@@ -0,0 +1,30 @@
+# Kill the server
+CREATE TABLE t(i INT) ENGINE InnoDB;
+INSERT INTO t VALUES
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+(0), (1), (2), (3), (4), (5), (6), (7), (8), (9);
+# xtrabackup backup, execute the following query after test.t is copied:
+# BEGIN NOT ATOMIC INSERT INTO test.t SELECT * FROM test.t; UPDATE test.t SET i = 10 WHERE i = 0; DELETE FROM test.t WHERE i = 1; END
+SELECT count(*) FROM t WHERE i = 0;
+count(*)
+0
+# xtrabackup prepare
+# shutdown server
+# remove datadir
+# xtrabackup move back
+# restart server
+SELECT count(*) FROM t WHERE i = 0;
+count(*)
+0
+Ok
+Ok
+DROP TABLE t;
+# Kill the server
diff --git a/mysql-test/suite/mariabackup/big_innodb_log.test b/mysql-test/suite/mariabackup/big_innodb_log.test
new file mode 100644
index 00000000000..05dc3aa39c5
--- /dev/null
+++ b/mysql-test/suite/mariabackup/big_innodb_log.test
@@ -0,0 +1,87 @@
+# The general reason why innodb redo log file is limited by 512G is that
+# log_block_convert_lsn_to_no() returns value limited by 1G. But there is no
+# need to have unique log block numbers in log group. This test forces innodb
+# to generate redo log files with non-unique log block numbers and tests
+# recovery process with such numbers.
+--source include/have_innodb.inc
+--source include/have_debug.inc
+
+--let MYSQLD_DATADIR= `select @@datadir`
+let $MYSQLD_BOOTSTRAP_CMD= $MYSQLD_BOOTSTRAP_CMD --datadir=$MYSQLD_DATADIR --debug-dbug=+d,innodb_small_log_block_no_limit;
+
+--source include/kill_mysqld.inc
+--rmdir $MYSQLD_DATADIR
+--mkdir $MYSQLD_DATADIR
+--mkdir $MYSQLD_DATADIR/mysql
+--mkdir $MYSQLD_DATADIR/test
+--exec $MYSQLD_BOOTSTRAP_CMD < $MYSQL_BOOTSTRAP_SQL_FILE >> $MYSQLTEST_VARDIR/tmp/bootstrap.log 2>&1
+let $old_restart_parameters=$restart_parameters;
+let $restart_parameters= $old_restart_parameters --debug-dbug=+d,innodb_small_log_block_no_limit;
+--source include/start_mysqld.inc
+
+CREATE TABLE t(i INT) ENGINE InnoDB;
+INSERT INTO t VALUES
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
+  (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);
+
+--let after_copy_test_t=BEGIN NOT ATOMIC INSERT INTO test.t SELECT * FROM test.t; UPDATE test.t SET i = 10 WHERE i = 0; DELETE FROM test.t WHERE i = 1; END
+
+--echo # xtrabackup backup, execute the following query after test.t is copied:
+--echo # $after_copy_test_t
+let $targetdir=$MYSQLTEST_VARDIR/tmp/backup;
+
+--disable_result_log
+exec $XTRABACKUP --defaults-file=$MYSQLTEST_VARDIR/my.cnf --backup --parallel=10 --target-dir=$targetdir --dbug=+d,mariabackup_events,innodb_small_log_block_no_limit;
+--enable_result_log
+
+--let $total_before=`SELECT count(*) FROM t`
+SELECT count(*) FROM t WHERE i = 0;
+--let $updated_before=`SELECT count(*) FROM t WHERE i = 10`
+
+echo # xtrabackup prepare;
+--disable_result_log
+exec $XTRABACKUP  --prepare --target-dir=$targetdir --dbug=+d,innodb_small_log_block_no_limit;
+--source include/restart_and_restore.inc
+--enable_result_log
+
+--let $total_after=`SELECT count(*) FROM t`
+SELECT count(*) FROM t WHERE i = 0;
+--let $updated_after=`SELECT count(*) FROM t WHERE i = 10`
+
+if ($total_before == $total_after) {
+--echo Ok
+}
+if ($total_before != $total_after) {
+--echo Failed
+}
+if ($updated_before == $updated_after) {
+--echo Ok
+}
+if ($updated_before != $updated_after) {
+--echo Failed
+}
+
+DROP TABLE t;
+rmdir $targetdir;
+--source include/kill_mysqld.inc
+--rmdir $MYSQLD_DATADIR
+
+perl;
+use lib "lib";
+use My::File::Path;
+my $install_db_dir = ($ENV{MTR_PARALLEL} == 1) ?
+  "$ENV{'MYSQLTEST_VARDIR'}/install.db" :
+  "$ENV{'MYSQLTEST_VARDIR'}/../install.db";
+copytree($install_db_dir, $ENV{'MYSQLD_DATADIR'});
+EOF
+
+--let $restart_parameters= $old_restart_parameters
+--source include/start_mysqld.inc
diff --git a/mysql-test/suite/rpl/r/rpl_create_or_replace_fail.result b/mysql-test/suite/rpl/r/rpl_create_or_replace_fail.result
new file mode 100644
index 00000000000..5cb2cd36d02
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_create_or_replace_fail.result
@@ -0,0 +1,20 @@
+include/master-slave.inc
+[connection master]
+CREATE TEMPORARY TABLE t1 (a INT NOT NULL);
+LOAD DATA INFILE 'x' INTO TABLE x;
+ERROR 42S02: Table 'test.x' doesn't exist
+CREATE OR REPLACE TEMPORARY TABLE t1 (x INT) PARTITION BY HASH(x);
+ERROR HY000: Cannot create temporary table with partitions
+"************** DROP TEMPORARY TABLE Should be present in Binary log **************"
+include/show_binlog_events.inc
+Log_name	Pos	Event_type	Server_id	End_log_pos	Info
+master-bin.000001	#	Gtid	#	#	GTID #-#-#
+master-bin.000001	#	Query	#	#	use `test`; CREATE TEMPORARY TABLE t1 (a INT NOT NULL)
+master-bin.000001	#	Gtid	#	#	GTID #-#-#
+master-bin.000001	#	Query	#	#	use `test`; CREATE OR REPLACE TEMPORARY TABLE t1 (x INT) PARTITION BY HASH(x)
+CREATE TABLE t1 (b INT);
+INSERT INTO t1 VALUES (NULL);
+connection slave;
+connection master;
+DROP TABLE t1;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_gtid_delete_domain.result b/mysql-test/suite/rpl/r/rpl_gtid_delete_domain.result
index 75a22b78a32..74648501fbe 100644
--- a/mysql-test/suite/rpl/r/rpl_gtid_delete_domain.result
+++ b/mysql-test/suite/rpl/r/rpl_gtid_delete_domain.result
@@ -41,7 +41,7 @@ START SLAVE;
 .. con='slave' warn='1' qlog='1' rlog='1' aborterr='1'
 ...==== BEGIN include/wait_for_slave_param.inc [Slave_IO_Running] ====
 ... con='slave' warn='1' qlog='1' rlog='1' aborterr='1'
-Waiting until 'Slave_IO_Running' = 'Yes' [timeout='300', $slave_error_param='Last_IO_Errno']
+Waiting until 'Slave_IO_Running' = 'Yes' [$slave_error_param='Last_IO_Errno']
 [connection slave]
 ...==== END include/wait_for_slave_param.inc [Slave_IO_Running] ====
 ... con='slave' warn='1' qlog='1' rlog='1' aborterr='1'
@@ -52,7 +52,7 @@ Waiting until 'Slave_IO_Running' = 'Yes' [timeout='300', $slave_error_param='Las
 .. con='slave' warn='1' qlog='1' rlog='1' aborterr='1'
 ...==== BEGIN include/wait_for_slave_param.inc [Slave_SQL_Running] ====
 ... con='slave' warn='1' qlog='1' rlog='1' aborterr='1'
-Waiting until 'Slave_SQL_Running' = 'Yes' [timeout='300', $slave_error_param='1']
+Waiting until 'Slave_SQL_Running' = 'Yes' [$slave_error_param='1']
 [connection slave]
 ...==== END include/wait_for_slave_param.inc [Slave_SQL_Running] ====
 ... con='slave' warn='1' qlog='1' rlog='1' aborterr='1'
diff --git a/mysql-test/suite/rpl/r/rpl_known_bugs_detection.result b/mysql-test/suite/rpl/r/rpl_known_bugs_detection.result
index 3cbb590b9a7..e935f8083c8 100644
--- a/mysql-test/suite/rpl/r/rpl_known_bugs_detection.result
+++ b/mysql-test/suite/rpl/r/rpl_known_bugs_detection.result
@@ -1,31 +1,5 @@
 call mtr.add_suppression("Unsafe statement written to the binary log using statement format");
 include/master-slave.inc
-[connection master]
-call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.");
-CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY AUTO_INCREMENT, b INT,
-UNIQUE(b));
-connection slave;
-connection master;
-INSERT INTO t1(b) VALUES(1),(1),(2) ON DUPLICATE KEY UPDATE t1.b=10;
-SELECT * FROM t1;
-a	b
-1	10
-2	2
-connection slave;
-call mtr.add_suppression("Slave SQL.*suffer.*http:..bugs.mysql.com.bug.php.id=24432");
-include/wait_for_slave_sql_error.inc [errno=1105]
-Last_SQL_Error = 'Error 'master may suffer from http://bugs.mysql.com/bug.php?id=24432 so slave stops; check error log on slave for more info' on query. Default database: 'test'. Query: 'INSERT INTO t1(b) VALUES(1),(1),(2) ON DUPLICATE KEY UPDATE t1.b=10''
-SELECT * FROM t1;
-a	b
-stop slave;
-include/wait_for_slave_to_stop.inc
-reset slave;
-connection master;
-reset master;
-drop table t1;
-connection slave;
-start slave;
-include/wait_for_slave_to_start.inc
 connection master;
 CREATE TABLE t1 (
 id bigint(20) unsigned NOT NULL auto_increment,
diff --git a/mysql-test/suite/rpl/r/rpl_mdev_17614.result b/mysql-test/suite/rpl/r/rpl_mdev_17614.result
new file mode 100644
index 00000000000..39057334926
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_mdev_17614.result
@@ -0,0 +1,129 @@
+include/master-slave.inc
+[connection master]
+call mtr.add_suppression("Unsafe statement written to the binary log using statement format");
+CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY , b INT,
+UNIQUE(b),  c int) engine=innodb;
+connection slave;
+connection master;
+INSERT INTO t1 VALUES (1, 1, 1);
+BEGIN;
+INSERT INTO t1 VALUES (2, 1, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+Warnings:
+Note	1592	Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT... ON DUPLICATE KEY UPDATE  on a table with more than one UNIQUE KEY is unsafe
+connection master1;
+INSERT INTO t1 VALUES(2, 2, 3) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+Warnings:
+Note	1592	Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT... ON DUPLICATE KEY UPDATE  on a table with more than one UNIQUE KEY is unsafe
+connection master;
+COMMIT;
+SELECT * FROM t1;
+a	b	c
+1	1	2
+2	2	3
+connection slave;
+include/wait_for_slave_sql_error.inc [errno=1062]
+Last_SQL_Error = 'Error 'Duplicate entry '1' for key 'b'' on query. Default database: 'test'. Query: 'INSERT INTO t1 VALUES (2, 1, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c)''
+#Different value from server
+SELECT * FROM t1;
+a	b	c
+1	1	1
+2	2	3
+stop slave;
+include/wait_for_slave_to_stop.inc
+reset slave;
+connection master;
+reset master;
+drop table t1;
+connection slave;
+start slave;
+include/wait_for_slave_to_start.inc
+connection master;
+CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY auto_increment, b INT,
+UNIQUE(b),  c int) engine=innodb;
+connection slave;
+connection master;
+INSERT INTO t1 VALUES (default, 1, 1);
+BEGIN;
+INSERT INTO t1 VALUES (default, 1, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+connection master1;
+INSERT INTO t1 VALUES(default, 2, 3) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+connection master;
+COMMIT;
+SELECT * FROM t1;
+a	b	c
+1	1	2
+3	2	3
+connection slave;
+#same data as master
+SELECT * FROM t1;
+a	b	c
+1	1	2
+3	2	3
+connection master;
+drop table t1;
+connection slave;
+connection master;
+CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY, b INT,
+UNIQUE(b),  c int, d int ) engine=innodb;
+connection slave;
+connection master;
+INSERT INTO t1 VALUES (1, 1, 1, 1);
+BEGIN;
+INSERT INTO t1 VALUES (2, NULL, 2, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+Warnings:
+Note	1592	Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT... ON DUPLICATE KEY UPDATE  on a table with more than one UNIQUE KEY is unsafe
+connection master1;
+INSERT INTO t1 VALUES(3, NULL, 2, 3) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+Warnings:
+Note	1592	Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT... ON DUPLICATE KEY UPDATE  on a table with more than one UNIQUE KEY is unsafe
+connection master;
+COMMIT;
+SELECT * FROM t1;
+a	b	c	d
+1	1	1	1
+2	NULL	2	2
+3	NULL	2	3
+connection slave;
+#same data as master
+SELECT * FROM t1;
+a	b	c	d
+1	1	1	1
+2	NULL	2	2
+3	NULL	2	3
+connection master;
+drop table t1;
+connection slave;
+connection master;
+CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY auto_increment, b INT,
+UNIQUE(b),  c int) engine=innodb;
+connection slave;
+connection master;
+INSERT INTO t1 VALUES (1, 1, 1);
+BEGIN;
+INSERT INTO t1 VALUES (2, 1, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+connection master1;
+INSERT INTO t1 VALUES(2, 2, 3) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+connection master;
+COMMIT;
+SELECT * FROM t1;
+a	b	c
+1	1	2
+2	2	3
+connection slave;
+include/wait_for_slave_sql_error.inc [errno=1062]
+Last_SQL_Error = 'Error 'Duplicate entry '1' for key 'b'' on query. Default database: 'test'. Query: 'INSERT INTO t1 VALUES (2, 1, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c)''
+#Different value from server
+SELECT * FROM t1;
+a	b	c
+1	1	1
+2	2	3
+stop slave;
+include/wait_for_slave_to_stop.inc
+reset slave;
+connection master;
+reset master;
+drop table t1;
+connection slave;
+start slave;
+include/wait_for_slave_to_start.inc
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_sync_with_innodb_thd_conc.result b/mysql-test/suite/rpl/r/rpl_sync_with_innodb_thd_conc.result
new file mode 100644
index 00000000000..cd9ac1db9b1
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_sync_with_innodb_thd_conc.result
@@ -0,0 +1,18 @@
+include/master-slave.inc
+[connection master]
+connection slave;
+SET @old_innodb_thread_concurrency := @@innodb_thread_concurrency;
+SET @old_innodb_thread_sleep_delay := @@innodb_thread_sleep_delay;
+SET GLOBAL innodb_thread_concurrency = 100;
+connection master;
+CREATE TABLE t(f INT) ENGINE=INNODB;
+INSERT INTO t VALUES (10);
+connection slave;
+include/diff_tables.inc [master:t, slave:t]
+"===== Clean up======="
+connection master;
+DROP TABLE t;
+connection slave;
+SET GLOBAL innodb_thread_concurrency = @old_innodb_thread_concurrency;
+SET GLOBAL innodb_thread_sleep_delay = @old_innodb_thread_sleep_delay;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_unsafe_statements.result b/mysql-test/suite/rpl/r/rpl_unsafe_statements.result
index ca790f5d148..0ce94ca63d0 100644
--- a/mysql-test/suite/rpl/r/rpl_unsafe_statements.result
+++ b/mysql-test/suite/rpl/r/rpl_unsafe_statements.result
@@ -1,5 +1,6 @@
 include/master-slave.inc
 [connection master]
+call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT");
 CREATE TABLE t1(id INT AUTO_INCREMENT, i INT, PRIMARY KEY (id)) ENGINE=INNODB;
 CREATE TABLE t2(id INT AUTO_INCREMENT, i INT, PRIMARY KEY (id)) ENGINE=INNODB;
 CREATE TRIGGER trig1 AFTER INSERT ON t1
@@ -49,9 +50,13 @@ connection master;
 DROP TABLE t1;
 CREATE TABLE t1(i INT, j INT, UNIQUE KEY(i), UNIQUE KEY(j)) ENGINE=INNODB;
 INSERT INTO t1 (i,j) VALUES (1,2) ON DUPLICATE KEY UPDATE j=j+1;
+Warnings:
+Note	1592	Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT... ON DUPLICATE KEY UPDATE  on a table with more than one UNIQUE KEY is unsafe
 START TRANSACTION;
 LOCK TABLES t1 WRITE;
 INSERT INTO t1 (i,j) VALUES (1,2) ON DUPLICATE KEY UPDATE j=j+1;
+Warnings:
+Note	1592	Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT... ON DUPLICATE KEY UPDATE  on a table with more than one UNIQUE KEY is unsafe
 UNLOCK TABLES;
 COMMIT;
 connection slave;
diff --git a/mysql-test/suite/rpl/t/rpl_create_or_replace_fail.test b/mysql-test/suite/rpl/t/rpl_create_or_replace_fail.test
new file mode 100644
index 00000000000..e75f34b0b56
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_create_or_replace_fail.test
@@ -0,0 +1,56 @@
+# ==== Purpose ====
+#
+# Test verifies that failed CREATE OR REPLACE TEMPORARY TABLE statement which
+# dropped the table but failed at a later stage of creation of temporary table
+# is written to binarylog in row based replication.
+#
+# ==== Implementation ====
+#
+# Steps:
+#    0 - Have mixed based replication mode.
+#    1 - Create a temporary table. It will be replicated as mixed replication
+#        mode is in use.
+#    2 - Execute an unsafe statement which will switch current statement
+#        binlog format to 'ROW'. i.e If binlog_format=MIXED, there are open
+#        temporary tables, and an unsafe statement is executed, then subsequent
+#        statements are logged in row format.
+#    3 - Execute a CREATE OR REPLACE TEMPORARY TABLE statement which tries to
+#        create partitions on temporary table. Since it is not supported it will
+#        fail.
+#    4 - Check the binary log output to ensure that the failed statement is
+#        written to the binary log.
+#    5 - Slave should be up and running and in sync with master.
+#
+# ==== References ====
+#
+# MDEV-18930: Failed CREATE OR REPLACE TEMPORARY not written into binary log
+# makes data on master and slave diverge
+#
+
+--source include/have_partition.inc
+--source include/have_binlog_format_mixed.inc
+--source include/master-slave.inc
+
+CREATE TEMPORARY TABLE t1 (a INT NOT NULL);
+
+# Execute an unsafe statement which switches replication mode internally from
+# "STATEMENT" to "ROW".
+--error ER_NO_SUCH_TABLE
+LOAD DATA INFILE 'x' INTO TABLE x;
+
+--error ER_PARTITION_NO_TEMPORARY
+CREATE OR REPLACE TEMPORARY TABLE t1 (x INT) PARTITION BY HASH(x);
+
+--echo "************** DROP TEMPORARY TABLE Should be present in Binary log **************"
+--source include/show_binlog_events.inc
+
+CREATE TABLE t1 (b INT);
+INSERT INTO t1 VALUES (NULL);
+--sync_slave_with_master
+
+# Cleanup
+--connection master
+DROP TABLE t1;
+
+--source include/rpl_end.inc
+
diff --git a/mysql-test/suite/rpl/t/rpl_known_bugs_detection.test b/mysql-test/suite/rpl/t/rpl_known_bugs_detection.test
index ab263ece407..5ea056d5f14 100644
--- a/mysql-test/suite/rpl/t/rpl_known_bugs_detection.test
+++ b/mysql-test/suite/rpl/t/rpl_known_bugs_detection.test
@@ -14,45 +14,6 @@ source include/have_binlog_checksum_off.inc;
 
 source include/master-slave.inc;
 
-call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.");
-
-#
-# This is to test that slave properly detects if
-# master may suffer from:
-# BUG#24432 "INSERT... ON DUPLICATE KEY UPDATE skips auto_increment values"
-# (i.e. on master, INSERT ON DUPLICATE KEY UPDATE is used and manipulates
-# an auto_increment column, and is binlogged statement-based).
-#
-
-# testcase with INSERT VALUES
-CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY AUTO_INCREMENT, b INT,
-UNIQUE(b));
-sync_slave_with_master;
-connection master;
-INSERT INTO t1(b) VALUES(1),(1),(2) ON DUPLICATE KEY UPDATE t1.b=10;
-SELECT * FROM t1;
-connection slave;
-
-# show the error message 
-#1105 = ER_UNKNOWN_ERROR
---let $slave_sql_errno= 1105
---let $show_slave_sql_error= 1
-call mtr.add_suppression("Slave SQL.*suffer.*http:..bugs.mysql.com.bug.php.id=24432");
---source include/wait_for_slave_sql_error.inc
-# show that it was not replicated
-SELECT * FROM t1;
-
-# restart replication for the next testcase
-stop slave;
---source include/wait_for_slave_to_stop.inc
-reset slave;
-connection master;
-reset master;
-drop table t1;
-connection slave;
-start slave;
---source include/wait_for_slave_to_start.inc
-
 # testcase with INSERT SELECT
 connection master;
 CREATE TABLE t1 (
diff --git a/mysql-test/suite/rpl/t/rpl_mdev_17614.test b/mysql-test/suite/rpl/t/rpl_mdev_17614.test
new file mode 100644
index 00000000000..9b86c8c15b5
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_mdev_17614.test
@@ -0,0 +1,121 @@
+source include/have_debug.inc;
+source include/have_innodb.inc;
+-- source include/have_binlog_format_statement.inc
+source include/master-slave.inc;
+# MDEV-17614
+# INSERT on dup key update is replication unsafe
+# There can be three case
+#  1. 2 unique key, Replication is unsafe.
+#  2. 2 unique key , with one auto increment key, Safe to replicate because Innodb will acquire gap lock
+#  3. n no of unique keys (n>1) but insert is only in 1 unique key
+#  4. 2 unique key , with one auto increment key(but user gives auto inc value), unsafe to replicate
+
+# Case 1
+call mtr.add_suppression("Unsafe statement written to the binary log using statement format");
+CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY , b INT,
+UNIQUE(b),  c int) engine=innodb;
+sync_slave_with_master;
+connection master;
+INSERT INTO t1 VALUES (1, 1, 1);
+BEGIN;
+INSERT INTO t1 VALUES (2, 1, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+  --connection master1
+  INSERT INTO t1 VALUES(2, 2, 3) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+--connection master
+COMMIT;
+SELECT * FROM t1;
+--connection slave
+# show the error message
+--let $slave_sql_errno= 1062
+--let $show_slave_sql_error= 1
+--source include/wait_for_slave_sql_error.inc
+--echo #Different value from server
+SELECT * FROM t1;
+
+# restart replication for the next testcase
+stop slave;
+--source include/wait_for_slave_to_stop.inc
+reset slave;
+connection master;
+reset master;
+drop table t1;
+connection slave;
+start slave;
+--source include/wait_for_slave_to_start.inc
+# Case 2
+--connection master
+CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY auto_increment, b INT,
+UNIQUE(b),  c int) engine=innodb;
+sync_slave_with_master;
+connection master;
+INSERT INTO t1 VALUES (default, 1, 1);
+BEGIN;
+INSERT INTO t1 VALUES (default, 1, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+  --connection master1
+  INSERT INTO t1 VALUES(default, 2, 3) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+--connection master
+COMMIT;
+SELECT * FROM t1;
+--sync_slave_with_master
+--echo #same data as master
+SELECT * FROM t1;
+
+connection master;
+drop table t1;
+--sync_slave_with_master
+
+# Case 3
+--connection master
+CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY, b INT,
+UNIQUE(b),  c int, d int ) engine=innodb;
+sync_slave_with_master;
+connection master;
+INSERT INTO t1 VALUES (1, 1, 1, 1);
+BEGIN;
+INSERT INTO t1 VALUES (2, NULL, 2, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+  --connection master1
+  INSERT INTO t1 VALUES(3, NULL, 2, 3) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+--connection master
+COMMIT;
+SELECT * FROM t1;
+--sync_slave_with_master
+--echo #same data as master
+SELECT * FROM t1;
+connection master;
+drop table t1;
+--sync_slave_with_master
+
+# Case 4
+--connection master
+CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY auto_increment, b INT,
+UNIQUE(b),  c int) engine=innodb;
+sync_slave_with_master;
+connection master;
+INSERT INTO t1 VALUES (1, 1, 1);
+BEGIN;
+INSERT INTO t1 VALUES (2, 1, 2) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+  --connection master1
+  INSERT INTO t1 VALUES(2, 2, 3) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c);
+--connection master
+COMMIT;
+SELECT * FROM t1;
+--connection slave
+# show the error message
+--let $slave_sql_errno= 1062
+--let $show_slave_sql_error= 1
+--source include/wait_for_slave_sql_error.inc
+--echo #Different value from server
+SELECT * FROM t1;
+
+# restart replication for the next testcase
+stop slave;
+--source include/wait_for_slave_to_stop.inc
+reset slave;
+connection master;
+reset master;
+drop table t1;
+connection slave;
+start slave;
+--source include/wait_for_slave_to_start.inc
+
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_sync_with_innodb_thd_conc.test b/mysql-test/suite/rpl/t/rpl_sync_with_innodb_thd_conc.test
new file mode 100644
index 00000000000..b4c2971d2fb
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_sync_with_innodb_thd_conc.test
@@ -0,0 +1,41 @@
+# ==== Purpose ====
+#
+# Test verifies that replication shouldn't hang when number of active threads
+# on the slave server are less than the allowed innodb_thread_concurrency value.
+#
+# ==== Implementation ====
+#
+# Steps:
+#    0 - Have master slave replication setup with engine being Innodb.
+#    1 - Configure innodb_thread_concurrency = 100.
+#    2 - Do some DML on master and sync the slave with master.
+#    3 - Ensure replication doesn't hang.
+#
+# ==== References ====
+#
+# MDEV-20247: Replication hangs with "preparing" and never starts
+#
+
+--source include/master-slave.inc
+--source include/have_innodb.inc
+
+--connection slave
+SET @old_innodb_thread_concurrency := @@innodb_thread_concurrency;
+SET @old_innodb_thread_sleep_delay := @@innodb_thread_sleep_delay;
+SET GLOBAL innodb_thread_concurrency = 100;
+
+--connection master
+CREATE TABLE t(f INT) ENGINE=INNODB;
+INSERT INTO t VALUES (10);
+--sync_slave_with_master
+
+--let $diff_tables=master:t, slave:t
+--source include/diff_tables.inc
+
+--echo "===== Clean up======="
+--connection master
+DROP TABLE t;
+--sync_slave_with_master
+SET GLOBAL innodb_thread_concurrency = @old_innodb_thread_concurrency;
+SET GLOBAL innodb_thread_sleep_delay = @old_innodb_thread_sleep_delay;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_unsafe_statements.test b/mysql-test/suite/rpl/t/rpl_unsafe_statements.test
index cbb4b54a220..aa0bd076398 100644
--- a/mysql-test/suite/rpl/t/rpl_unsafe_statements.test
+++ b/mysql-test/suite/rpl/t/rpl_unsafe_statements.test
@@ -24,7 +24,7 @@
 --source include/have_innodb.inc
 --source include/have_binlog_format_mixed.inc
 --source include/master-slave.inc
-
+call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT");
 # Case-1: BINLOG_STMT_UNSAFE_AUTOINC_COLUMNS
 #  Statement is unsafe because it invokes a trigger or a
 #  stored function that inserts into an AUTO_INCREMENT column.
diff --git a/mysql-test/suite/sys_vars/r/sysvars_innodb.result b/mysql-test/suite/sys_vars/r/sysvars_innodb.result
index b2a43e00742..591f31aa844 100644
--- a/mysql-test/suite/sys_vars/r/sysvars_innodb.result
+++ b/mysql-test/suite/sys_vars/r/sysvars_innodb.result
@@ -1473,7 +1473,7 @@ VARIABLE_SCOPE	GLOBAL
 VARIABLE_TYPE	BIGINT UNSIGNED
 VARIABLE_COMMENT	Size of each log file in a log group.
 NUMERIC_MIN_VALUE	1048576
-NUMERIC_MAX_VALUE	549755813888
+NUMERIC_MAX_VALUE	17592186044415
 NUMERIC_BLOCK_SIZE	65536
 ENUM_VALUE_LIST	NULL
 READ_ONLY	YES
diff --git a/mysql-test/suite/sys_vars/r/table_open_cache_basic.result b/mysql-test/suite/sys_vars/r/table_open_cache_basic.result
index 2130723e5ff..2d8e9005f81 100644
--- a/mysql-test/suite/sys_vars/r/table_open_cache_basic.result
+++ b/mysql-test/suite/sys_vars/r/table_open_cache_basic.result
@@ -1,18 +1,9 @@
 SET @start_value = @@global.table_open_cache ;
-SELECT @start_value;
-@start_value
-421
-'#--------------------FN_DYNVARS_001_01------------------------#'
-SET @@global.table_open_cache  = 99;
-SET @@global.table_open_cache  = DeFAULT;
-SELECT @@global.table_open_cache;
-@@global.table_open_cache
-2000
 '#---------------------FN_DYNVARS_001_02-------------------------#'
 SET @@global.table_open_cache = Default;
-SELECT @@global.table_open_cache  = 400;
-@@global.table_open_cache  = 400
-0
+SELECT @@global.table_open_cache > 0;
+@@global.table_open_cache > 0
+1
 '#--------------------FN_DYNVARS_001_03------------------------#'
 SET @@global.table_open_cache  = 8;
 Warnings:
@@ -106,6 +97,3 @@ ERROR 42S02: Unknown table 'global' in field list
 SELECT table_open_cache = @@session.table_open_cache ;
 ERROR 42S22: Unknown column 'table_open_cache' in 'field list'
 SET @@global.table_open_cache = @start_value;
-SELECT @@global.table_open_cache ;
-@@global.table_open_cache
-421
diff --git a/mysql-test/suite/sys_vars/t/table_open_cache_basic.test b/mysql-test/suite/sys_vars/t/table_open_cache_basic.test
index d2450e9586e..fc838d1a28b 100644
--- a/mysql-test/suite/sys_vars/t/table_open_cache_basic.test
+++ b/mysql-test/suite/sys_vars/t/table_open_cache_basic.test
@@ -4,8 +4,8 @@
 # Scope: GLOBAL                                                               #
 # Access Type: Dynamic                                                        #
 # Data Type: numeric                                                          #
-# Default Value: 400                                                         #
-# Range: 64-524288                                                             #
+# Default Value: 400                                                          #
+# Range: 64-524288                                                            #
 #                                                                             #
 #                                                                             #
 # Creation Date: 2008-02-13                                                   #
@@ -35,18 +35,6 @@
 ########################################################################## 
 
 SET @start_value = @@global.table_open_cache ;
-SELECT @start_value;
-
-
---echo '#--------------------FN_DYNVARS_001_01------------------------#'
-######################################################################## 
-#              Display the DEFAULT value of table_open_cache           #
-######################################################################## 
-
-SET @@global.table_open_cache  = 99;
-SET @@global.table_open_cache  = DeFAULT;
-SELECT @@global.table_open_cache;
-
 
 --echo '#---------------------FN_DYNVARS_001_02-------------------------#'
 ############################################### 
@@ -54,7 +42,7 @@ SELECT @@global.table_open_cache;
 ############################################### 
 
 SET @@global.table_open_cache = Default;
-SELECT @@global.table_open_cache  = 400;
+SELECT @@global.table_open_cache > 0;
 
 --echo '#--------------------FN_DYNVARS_001_03------------------------#'
 ######################################################################## 
@@ -163,10 +151,7 @@ SELECT table_open_cache = @@session.table_open_cache ;
 ##############################
 
 SET @@global.table_open_cache = @start_value;
-SELECT @@global.table_open_cache ;
-
 
 ##################################################################
 #              END OF table_open_cache  TESTS                    #
 ##################################################################
-
diff --git a/pcre/pcretest.c b/pcre/pcretest.c
index a0e65ed9808..dcf1e2c6760 100644
--- a/pcre/pcretest.c
+++ b/pcre/pcretest.c
@@ -2864,7 +2864,8 @@ strncmpic(pcre_uint8 *s, pcre_uint8 *t, int n)
 {
 while (n--)
   {
-  int c = tolower(*s++) - tolower(*t++);
+  int c = tolower(*s) - tolower(*t);
+  s++; t++;
   if (c) return c;
   }
 return 0;
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index ec7b3dbbd7a..57bd244e738 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -2434,6 +2434,7 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
     KEY_PART *key_parts;
     KEY *key_info;
     PARAM param;
+    bool force_group_by = false;
 
     if (check_stack_overrun(thd, 2*STACK_MIN_SIZE + sizeof(PARAM), buff))
       DBUG_RETURN(0);                           // Fatal error flag is set
@@ -2562,15 +2563,20 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
       Try to construct a QUICK_GROUP_MIN_MAX_SELECT.
       Notice that it can be constructed no matter if there is a range tree.
     */
+    DBUG_EXECUTE_IF("force_group_by", force_group_by = true; );
     group_trp= get_best_group_min_max(&param, tree, best_read_time);
     if (group_trp)
     {
       param.table->quick_condition_rows= MY_MIN(group_trp->records,
                                              head->stat_records());
-      if (group_trp->read_cost < best_read_time)
+      if (group_trp->read_cost < best_read_time || force_group_by)
       {
         best_trp= group_trp;
         best_read_time= best_trp->read_cost;
+        if (force_group_by)
+        {
+          goto force_plan;
+        }
       }
     }
 
@@ -2670,6 +2676,7 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
       }
     }
 
+force_plan:
     thd->mem_root= param.old_root;
 
     /* If we got a read plan, create a quick select from it. */
@@ -11534,13 +11541,28 @@ int QUICK_RANGE_SELECT::get_next_prefix(uint prefix_length,
       DBUG_ASSERT(cur_prefix != NULL);
       result= file->ha_index_read_map(record, cur_prefix, keypart_map,
                                       HA_READ_AFTER_KEY);
-      if (result || last_range->max_keypart_map == 0)
-        DBUG_RETURN(result);
-
-      key_range previous_endpoint;
-      last_range->make_max_endpoint(&previous_endpoint, prefix_length, keypart_map);
-      if (file->compare_key(&previous_endpoint) <= 0)
-        DBUG_RETURN(0);
+      if (result || last_range->max_keypart_map == 0) {
+        /*
+          Only return if actual failure occurred. For HA_ERR_KEY_NOT_FOUND
+          or HA_ERR_END_OF_FILE, we just want to continue to reach the next
+          set of ranges. It is possible for the storage engine to return
+          HA_ERR_KEY_NOT_FOUND/HA_ERR_END_OF_FILE even when there are more
+          keys if it respects the end range set by the read_range_first call
+          below.
+        */
+        if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
+          DBUG_RETURN(result);
+      } else {
+        /*
+          For storage engines that don't respect end range, check if we've
+          moved past the current range.
+        */
+        key_range previous_endpoint;
+        last_range->make_max_endpoint(&previous_endpoint, prefix_length,
+                                      keypart_map);
+        if (file->compare_key(&previous_endpoint) <= 0)
+          DBUG_RETURN(0);
+      }
     }
 
     uint count= ranges.elements - (uint)(cur_range - (QUICK_RANGE**) ranges.buffer);
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 0e6fe2a0c51..5c778d11c98 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -4804,12 +4804,6 @@ extern "C" int thd_slave_thread(const MYSQL_THD thd)
 }
 
 
-extern "C" int thd_rpl_stmt_based(const MYSQL_THD thd)
-{
-  return thd &&
-    !thd->is_current_stmt_binlog_format_row() &&
-    !thd->is_current_stmt_binlog_disabled();
-}
 
 
 /* Returns high resolution timestamp for the start
@@ -6381,6 +6375,48 @@ int THD::decide_logging_format(TABLE_LIST *tables)
   DBUG_RETURN(0);
 }
 
+int THD::decide_logging_format_low(TABLE *table)
+{
+  /*
+   INSERT...ON DUPLICATE KEY UPDATE on a table with more than one unique keys
+   can be unsafe.
+   */
+  if(wsrep_binlog_format() <= BINLOG_FORMAT_STMT &&
+       !is_current_stmt_binlog_format_row() &&
+       !lex->is_stmt_unsafe() &&
+       lex->sql_command == SQLCOM_INSERT &&
+       lex->duplicates == DUP_UPDATE)
+  {
+    uint unique_keys= 0;
+    uint keys= table->s->keys, i= 0;
+    Field *field;
+    for (KEY* keyinfo= table->s->key_info;
+             i < keys && unique_keys <= 1; i++, keyinfo++)
+      if (keyinfo->flags & HA_NOSAME &&
+         !(keyinfo->key_part->field->flags & AUTO_INCREMENT_FLAG &&
+             //User given auto inc can be unsafe
+             !keyinfo->key_part->field->val_int()))
+      {
+        for (uint j= 0; j < keyinfo->user_defined_key_parts; j++)
+        {
+          field= keyinfo->key_part[j].field;
+          if(!bitmap_is_set(table->write_set,field->field_index))
+            goto exit;
+        }
+        unique_keys++;
+exit:;
+      }
+
+    if (unique_keys > 1)
+    {
+      lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_INSERT_TWO_KEYS);
+      binlog_unsafe_warning_flags|= lex->get_stmt_unsafe_flags();
+      set_current_stmt_binlog_format_row_if_mixed();
+      return 1;
+    }
+  }
+  return 0;
+}
 
 /*
   Implementation of interface to write rows to the binary log through the
diff --git a/sql/sql_class.h b/sql/sql_class.h
index acf699f72e3..e5de03d4911 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -2452,6 +2452,20 @@ public:
   /* container for handler's private per-connection data */
   Ha_data ha_data[MAX_HA];
 
+  /**
+    Bit field for the state of binlog warnings.
+
+    The first Lex::BINLOG_STMT_UNSAFE_COUNT bits list all types of
+    unsafeness that the current statement has.
+
+    This must be a member of THD and not of LEX, because warnings are
+    detected and issued in different places (@c
+    decide_logging_format() and @c binlog_query(), respectively).
+    Between these calls, the THD->lex object may change; e.g., if a
+    stored routine is invoked.  Only THD persists between the calls.
+  */
+  uint32 binlog_unsafe_warning_flags;
+
 #ifndef MYSQL_CLIENT
   binlog_cache_mngr *  binlog_setup_trx_data();
 
@@ -2561,20 +2575,6 @@ private:
   */
   enum_binlog_format current_stmt_binlog_format;
 
-  /**
-    Bit field for the state of binlog warnings.
-
-    The first Lex::BINLOG_STMT_UNSAFE_COUNT bits list all types of
-    unsafeness that the current statement has.
-
-    This must be a member of THD and not of LEX, because warnings are
-    detected and issued in different places (@c
-    decide_logging_format() and @c binlog_query(), respectively).
-    Between these calls, the THD->lex object may change; e.g., if a
-    stored routine is invoked.  Only THD persists between the calls.
-  */
-  uint32 binlog_unsafe_warning_flags;
-
   /*
     Number of outstanding table maps, i.e., table maps in the
     transaction cache.
@@ -4455,6 +4455,18 @@ public:
   }
   void leave_locked_tables_mode();
   int decide_logging_format(TABLE_LIST *tables);
+  /*
+   In Some cases when decide_logging_format is called it does not have all
+   information to decide the logging format. So that cases we call decide_logging_format_2
+   at later stages in execution.
+   One example would be binlog format for IODKU but column with unique key is not inserted.
+   We dont have inserted columns info when we call decide_logging_format so on later stage we call
+   decide_logging_format_low
+
+   @returns 0 if no format is changed
+            1 if there is change in binlog format
+  */
+  int decide_logging_format_low(TABLE *table);
 
   enum need_invoker { INVOKER_NONE=0, INVOKER_USER, INVOKER_ROLE};
   void binlog_invoker(bool role) { m_binlog_invoker= role ? INVOKER_ROLE : INVOKER_USER; }
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index 29be1c4c645..709f3ad87a0 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -1057,6 +1057,7 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
         break;
       }
 
+      thd->decide_logging_format_low(table);
 #ifndef EMBEDDED_LIBRARY
       if (lock_type == TL_WRITE_DELAYED)
       {
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 3e1844a7f4a..9bd2e6040f0 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -6072,7 +6072,7 @@ finish:
       trans_rollback_stmt(thd);
     }
 #ifdef WITH_WSREP
-    if (thd->spcont &&
+    else if (thd->spcont &&
              (thd->wsrep_conflict_state == MUST_ABORT ||
               thd->wsrep_conflict_state == ABORTED    ||
               thd->wsrep_conflict_state == CERT_FAILURE))
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index eb623e14912..182254cfe60 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -5219,7 +5219,7 @@ bool mysql_create_table(THD *thd, TABLE_LIST *create_table,
 
 err:
   /* In RBR we don't need to log CREATE TEMPORARY TABLE */
-  if (thd->is_current_stmt_binlog_format_row() && create_info->tmp_table())
+  if (!result && thd->is_current_stmt_binlog_format_row() && create_info->tmp_table())
     DBUG_RETURN(result);
 
   if (create_info->tmp_table())
@@ -10131,6 +10131,7 @@ end_temporary:
 	      (ulong) (copied + deleted), (ulong) deleted,
 	      (ulong) thd->get_stmt_da()->current_statement_warn_count());
   my_ok(thd, copied + deleted, 0L, alter_ctx.tmp_buff);
+  DEBUG_SYNC(thd, "alter_table_inplace_trans_commit");
   DBUG_RETURN(false);
 
 err_new_table_cleanup:
@@ -10232,12 +10233,14 @@ bool mysql_trans_commit_alter_copy_data(THD *thd)
   uint save_unsafe_rollback_flags;
   DBUG_ENTER("mysql_trans_commit_alter_copy_data");
 
-  /* Save flags as transcommit_implicit_are_deleting_them */
+  /* Save flags as trans_commit_implicit are deleting them */
   save_unsafe_rollback_flags= thd->transaction.stmt.m_unsafe_rollback_flags;
 
+  DEBUG_SYNC(thd, "alter_table_copy_trans_commit");
+
   if (ha_enable_transaction(thd, TRUE))
     DBUG_RETURN(TRUE);
-  
+
   /*
     Ensure that the new table is saved properly to disk before installing
     the new .frm.
diff --git a/sql/wsrep_mysqld.cc b/sql/wsrep_mysqld.cc
index 21e2e5bd93f..28a9ddb0bea 100644
--- a/sql/wsrep_mysqld.cc
+++ b/sql/wsrep_mysqld.cc
@@ -183,7 +183,19 @@ static PSI_file_info wsrep_files[]=
 {
   { &key_file_wsrep_gra_log, "wsrep_gra_log", 0}
 };
-#endif
+
+PSI_thread_key key_wsrep_sst_joiner, key_wsrep_sst_donor,
+  key_wsrep_rollbacker, key_wsrep_applier;
+
+static PSI_thread_info wsrep_threads[]=
+{
+ {&key_wsrep_sst_joiner, "wsrep_sst_joiner_thread", PSI_FLAG_GLOBAL},
+ {&key_wsrep_sst_donor, "wsrep_sst_donor_thread", PSI_FLAG_GLOBAL},
+ {&key_wsrep_rollbacker, "wsrep_rollbacker_thread", PSI_FLAG_GLOBAL},
+ {&key_wsrep_applier, "wsrep_applier_thread", PSI_FLAG_GLOBAL}
+};
+
+#endif /* HAVE_PSI_INTERFACE */
 
 my_bool wsrep_inited                   = 0; // initialized ?
 
@@ -799,6 +811,7 @@ void wsrep_thr_init()
   mysql_mutex_register("sql", wsrep_mutexes, array_elements(wsrep_mutexes));
   mysql_cond_register("sql", wsrep_conds, array_elements(wsrep_conds));
   mysql_file_register("sql", wsrep_files, array_elements(wsrep_files));
+  mysql_thread_register("sql", wsrep_threads, array_elements(wsrep_threads));
 #endif
 
   mysql_mutex_init(key_LOCK_wsrep_ready, &LOCK_wsrep_ready, MY_MUTEX_INIT_FAST);
@@ -814,6 +827,7 @@ void wsrep_thr_init()
   mysql_mutex_init(key_LOCK_wsrep_slave_threads, &LOCK_wsrep_slave_threads, MY_MUTEX_INIT_FAST);
   mysql_mutex_init(key_LOCK_wsrep_desync, &LOCK_wsrep_desync, MY_MUTEX_INIT_FAST);
   mysql_mutex_init(key_LOCK_wsrep_config_state, &LOCK_wsrep_config_state, MY_MUTEX_INIT_FAST);
+
   DBUG_VOID_RETURN;
 }
 
diff --git a/sql/wsrep_mysqld.h b/sql/wsrep_mysqld.h
index 592d2f279f4..395dabb39be 100644
--- a/sql/wsrep_mysqld.h
+++ b/sql/wsrep_mysqld.h
@@ -286,7 +286,14 @@ extern PSI_mutex_key key_LOCK_wsrep_slave_threads;
 extern PSI_mutex_key key_LOCK_wsrep_desync;
 
 extern PSI_file_key key_file_wsrep_gra_log;
+
+extern PSI_thread_key key_wsrep_sst_joiner;
+extern PSI_thread_key key_wsrep_sst_donor;
+extern PSI_thread_key key_wsrep_rollbacker;
+extern PSI_thread_key key_wsrep_applier;
 #endif /* HAVE_PSI_INTERFACE */
+
+
 struct TABLE_LIST;
 class Alter_info;
 int wsrep_to_isolation_begin(THD *thd, const char *db_, const char *table_,
diff --git a/sql/wsrep_sst.cc b/sql/wsrep_sst.cc
index 318714dd29c..5c3e5642cda 100644
--- a/sql/wsrep_sst.cc
+++ b/sql/wsrep_sst.cc
@@ -729,10 +729,10 @@ static ssize_t sst_prepare_other (const char*  method,
   pthread_t tmp;
   sst_thread_arg arg(cmd_str(), env());
   mysql_mutex_lock (&arg.lock);
-  ret = pthread_create (&tmp, NULL, sst_joiner_thread, &arg);
+  ret = mysql_thread_create (key_wsrep_sst_joiner, &tmp, NULL, sst_joiner_thread, &arg);
   if (ret)
   {
-    WSREP_ERROR("sst_prepare_other(): pthread_create() failed: %d (%s)",
+    WSREP_ERROR("sst_prepare_other(): mysql_thread_create() failed: %d (%s)",
                 ret, strerror(ret));
     return -ret;
   }
@@ -1381,10 +1381,10 @@ static int sst_donate_other (const char*   method,
   pthread_t tmp;
   sst_thread_arg arg(cmd_str(), env);
   mysql_mutex_lock (&arg.lock);
-  ret = pthread_create (&tmp, NULL, sst_donor_thread, &arg);
+  ret = mysql_thread_create (key_wsrep_sst_donor, &tmp, NULL, sst_donor_thread, &arg);
   if (ret)
   {
-    WSREP_ERROR("sst_donate_other(): pthread_create() failed: %d (%s)",
+    WSREP_ERROR("sst_donate_other(): mysql_thread_create() failed: %d (%s)",
                 ret, strerror(ret));
     return ret;
   }
diff --git a/sql/wsrep_thd.cc b/sql/wsrep_thd.cc
index 34b39c6d1e3..a45828beab3 100644
--- a/sql/wsrep_thd.cc
+++ b/sql/wsrep_thd.cc
@@ -417,9 +417,26 @@ static void wsrep_replication_process(THD *thd)
 
 static bool create_wsrep_THD(wsrep_thread_args* args)
 {
-  ulong old_wsrep_running_threads= wsrep_running_threads;
   mysql_mutex_lock(&LOCK_thread_count);
-  bool res= pthread_create(&args->thread_id, &connection_attrib, start_wsrep_THD,
+  ulong old_wsrep_running_threads= wsrep_running_threads;
+#ifdef HAVE_PSI_THREAD_INTERFACE
+  PSI_thread_key key;
+
+  switch (args->thread_type)
+  {
+    case WSREP_APPLIER_THREAD:
+      key= key_wsrep_applier;
+      break;
+    case WSREP_ROLLBACKER_THREAD:
+      key= key_wsrep_rollbacker;
+      break;
+    default:
+      assert(0);
+      break;
+  }
+#endif
+
+  bool res= mysql_thread_create(key, &args->thread_id, &connection_attrib, start_wsrep_THD,
                            (void*)args);
   /*
     if starting a thread on server startup, wait until the this thread's THD
diff --git a/storage/connect/myconn.cpp b/storage/connect/myconn.cpp
index 253c42bb002..6de5a73875c 100644
--- a/storage/connect/myconn.cpp
+++ b/storage/connect/myconn.cpp
@@ -879,7 +879,7 @@ MYSQL_FIELD *MYSQLC::GetNextField(void)
 PQRYRES MYSQLC::GetResult(PGLOBAL g, bool pdb)
   {
 	PCSZ         fmt;
-  char        *name, v;
+  char        *name, v= 0;
   int          n;
   bool         uns;
   PCOLRES     *pcrp, crp;
diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
index e877de68bba..29cc4dc3c40 100644
--- a/storage/innobase/handler/ha_innodb.cc
+++ b/storage/innobase/handler/ha_innodb.cc
@@ -1708,9 +1708,9 @@ innobase_srv_conc_enter_innodb(
 			   && thd_is_replication_slave_thread(trx->mysql_thd)) {
 			const ulonglong end = my_interval_timer()
 				+ ulonglong(srv_replication_delay) * 1000000;
-			while (srv_conc_get_active_threads()
-			       >= srv_thread_concurrency
-			       || my_interval_timer() >= end) {
+			while ((srv_conc_get_active_threads()
+			        >= srv_thread_concurrency)
+			       && my_interval_timer() < end) {
 				os_thread_sleep(2000 /* 2 ms */);
 			}
 		} else {
@@ -3925,14 +3925,12 @@ static int innodb_init_params()
 		DBUG_RETURN(HA_ERR_INITIALIZATION);
 	}
 
-	if (srv_n_log_files * srv_log_file_size
-	    >= 512ULL * 1024ULL * 1024ULL * 1024ULL) {
-		/* log_block_convert_lsn_to_no() limits the returned block
-		number to 1G and given that OS_FILE_LOG_BLOCK_SIZE is 512
-		bytes, then we have a limit of 512 GB. If that limit is to
-		be raised, then log_block_convert_lsn_to_no() must be
-		modified. */
-		ib::error() << "Combined size of log files must be < 512 GB";
+	if (srv_n_log_files * srv_log_file_size >= log_group_max_size) {
+		/* Log group size is limited by the size of page number.
+		Remove this limitation when fil_io() is not used for
+		recovery log io. */
+		ib::error() << "Combined size of log files must be < "
+			<< log_group_max_size;
 		DBUG_RETURN(HA_ERR_INITIALIZATION);
 	}
 
@@ -19591,7 +19589,7 @@ static MYSQL_SYSVAR_ULONG(log_buffer_size, srv_log_buffer_size,
 static MYSQL_SYSVAR_ULONGLONG(log_file_size, srv_log_file_size,
   PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
   "Size of each log file in a log group.",
-  NULL, NULL, 48 << 20, 1 << 20, 512ULL << 30, UNIV_PAGE_SIZE_MAX);
+  NULL, NULL, 48 << 20, 1 << 20, log_group_max_size, UNIV_PAGE_SIZE_MAX);
 /* OS_FILE_LOG_BLOCK_SIZE would be more appropriate than UNIV_PAGE_SIZE_MAX,
 but fil_space_t is being used for the redo log, and it uses data pages. */
 
diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc
index 9b34354d946..d119fca82b6 100644
--- a/storage/innobase/handler/handler0alter.cc
+++ b/storage/innobase/handler/handler0alter.cc
@@ -56,6 +56,9 @@ Smart ALTER TABLE
 #include "row0sel.h"
 #include "ha_innodb.h"
 #include "ut0stage.h"
+#include "span.h"
+
+using st_::span;
 
 static const char *MSG_UNSUPPORTED_ALTER_ONLINE_ON_VIRTUAL_COLUMN=
 			"INPLACE ADD or DROP of virtual columns cannot be "
@@ -1636,20 +1639,22 @@ innobase_set_foreign_key_option(
 /*******************************************************************//**
 Check if a foreign key constraint can make use of an index
 that is being created.
+@param[in]	col_names	column names
+@param[in]	n_cols		number of columns
+@param[in]	keys		index information
+@param[in]	add		indexes being created
 @return useable index, or NULL if none found */
 static MY_ATTRIBUTE((nonnull, warn_unused_result))
 const KEY*
 innobase_find_equiv_index(
-/*======================*/
 	const char*const*	col_names,
-					/*!< in: column names */
-	uint			n_cols,	/*!< in: number of columns */
-	const KEY*		keys,	/*!< in: index information */
-	const uint*		add,	/*!< in: indexes being created */
-	uint			n_add)	/*!< in: number of indexes to create */
+	uint			n_cols,
+	const KEY*		keys,
+	span<uint>		add)
 {
-	for (uint i = 0; i < n_add; i++) {
-		const KEY*	key = &keys[add[i]];
+	for (span<uint>::iterator it = add.begin(), end = add.end(); it != end;
+	     ++it) {
+		const KEY*	key = &keys[*it];
 
 		if (key->user_defined_key_parts < n_cols
 		    || key->flags & HA_SPATIAL) {
@@ -1700,7 +1705,7 @@ no_match:
 Find an index whose first fields are the columns in the array
 in the same order and is not marked for deletion
 @return matching index, NULL if not found */
-static MY_ATTRIBUTE((nonnull(1,5), warn_unused_result))
+static MY_ATTRIBUTE((nonnull(1,4), warn_unused_result))
 dict_index_t*
 innobase_find_fk_index(
 /*===================*/
@@ -1708,10 +1713,8 @@ innobase_find_fk_index(
 	const char**		col_names,
 					/*!< in: column names, or NULL
 					to use table->col_names */
-	dict_index_t**		drop_index,
+	span<dict_index_t*>	drop_index,
 					/*!< in: indexes to be dropped */
-	ulint			n_drop_index,
-					/*!< in: size of drop_index[] */
 	const char**		columns,/*!< in: array of column names */
 	ulint			n_cols) /*!< in: number of columns */
 {
@@ -1720,21 +1723,14 @@ innobase_find_fk_index(
 	index = dict_table_get_first_index(table);
 
 	while (index != NULL) {
-		if (dict_foreign_qualify_index(
-			    table, col_names, columns, n_cols,
-			    index, NULL, true, 0,
-			    NULL, NULL, NULL)) {
-			for (ulint i = 0; i < n_drop_index; i++) {
-				if (index == drop_index[i]) {
-					/* Skip to-be-dropped indexes. */
-					goto next_rec;
-				}
-			}
-
-			return(index);
+		if (dict_foreign_qualify_index(table, col_names, columns,
+					       n_cols, index, NULL, true, 0,
+					       NULL, NULL, NULL)
+		    && std::find(drop_index.begin(), drop_index.end(), index)
+			   == drop_index.end()) {
+			return index;
 		}
 
-next_rec:
 		index = dict_table_get_next_index(index);
 	}
 
@@ -1880,7 +1876,7 @@ innobase_get_foreign_key_info(
 
 			index = innobase_find_fk_index(
 				table, col_names,
-				drop_index, n_drop_index,
+				span<dict_index_t*>(drop_index, n_drop_index),
 				column_names, i);
 
 			/* MySQL would add a index in the creation
@@ -1895,8 +1891,8 @@ innobase_get_foreign_key_info(
 			if (!index && !innobase_find_equiv_index(
 				    column_names, static_cast<uint>(i),
 				    ha_alter_info->key_info_buffer,
-				    ha_alter_info->index_add_buffer,
-				    ha_alter_info->index_add_count)) {
+				    span<uint>(ha_alter_info->index_add_buffer,
+					       ha_alter_info->index_add_count))) {
 				my_error(
 					ER_FK_NO_INDEX_CHILD,
 					MYF(0),
@@ -5938,8 +5934,8 @@ innobase_check_foreign_key_index(
 			    foreign->referenced_col_names,
 			    foreign->n_fields,
 			    ha_alter_info->key_info_buffer,
-			    ha_alter_info->index_add_buffer,
-			    ha_alter_info->index_add_count)) {
+			    span<uint>(ha_alter_info->index_add_buffer,
+				       ha_alter_info->index_add_count))) {
 
 			/* Index cannot be dropped. */
 			trx->error_info = index;
@@ -5973,8 +5969,8 @@ innobase_check_foreign_key_index(
 			    foreign->foreign_col_names,
 			    foreign->n_fields,
 			    ha_alter_info->key_info_buffer,
-			    ha_alter_info->index_add_buffer,
-			    ha_alter_info->index_add_count)) {
+			    span<uint>(ha_alter_info->index_add_buffer,
+				       ha_alter_info->index_add_count))) {
 
 			/* Index cannot be dropped. */
 			trx->error_info = index;
diff --git a/storage/innobase/include/data0type.ic b/storage/innobase/include/data0type.ic
index b80667e5799..f2c499716ce 100644
--- a/storage/innobase/include/data0type.ic
+++ b/storage/innobase/include/data0type.ic
@@ -1,7 +1,7 @@
 /*****************************************************************************
 
 Copyright (c) 1996, 2014, Oracle and/or its affiliates. All Rights Reserved.
-Copyright (c) 2017, 2018, MariaDB Corporation.
+Copyright (c) 2017, 2019, MariaDB Corporation.
 
 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 the Free Software
diff --git a/storage/innobase/include/ha_prototypes.h b/storage/innobase/include/ha_prototypes.h
index d94ca26fc0a..e3eb02d2fbf 100644
--- a/storage/innobase/include/ha_prototypes.h
+++ b/storage/innobase/include/ha_prototypes.h
@@ -119,9 +119,6 @@ thd_is_replication_slave_thread(
 /*============================*/
 	THD*	thd);	/*!< in: thread handle */
 
-/** @return whether statement-based replication is active */
-extern "C" int thd_rpl_stmt_based(const THD* thd);
-
 /******************************************************************//**
 Returns true if the transaction this thread is processing has edited
 non-transactional tables. Used by the deadlock detector when deciding
diff --git a/storage/innobase/include/log0log.h b/storage/innobase/include/log0log.h
index b90bc126e5b..da6f5505393 100644
--- a/storage/innobase/include/log0log.h
+++ b/storage/innobase/include/log0log.h
@@ -40,6 +40,10 @@ Created 12/9/1995 Heikki Tuuri
 #include "os0event.h"
 #include "os0file.h"
 
+#ifndef UINT32_MAX
+#define UINT32_MAX             (4294967295U)
+#endif
+
 /** Maximum number of srv_n_log_files, or innodb_log_files_in_group */
 #define SRV_N_LOG_FILES_MAX 100
 
@@ -493,6 +497,12 @@ Stored in LOG_HEADER_FORMAT. */
 					header */
 #define LOG_FILE_HDR_SIZE	(4 * OS_FILE_LOG_BLOCK_SIZE)
 
+/* As long as fil_io() is used to handle log io, log group max size is limited
+by (maximum page number) * (minimum page size). Page number type is uint32_t.
+Remove this limitation if page number is no longer used for log file io. */
+static const ulonglong log_group_max_size =
+	((ulonglong(UINT32_MAX) + 1) * UNIV_PAGE_SIZE_MIN - 1);
+
 typedef ib_mutex_t	LogSysMutex;
 typedef ib_mutex_t	FlushOrderMutex;
 
diff --git a/storage/innobase/include/log0log.ic b/storage/innobase/include/log0log.ic
index 902a79d2bf8..722e658a24b 100644
--- a/storage/innobase/include/log0log.ic
+++ b/storage/innobase/include/log0log.ic
@@ -185,7 +185,9 @@ log_block_convert_lsn_to_no(
 /*========================*/
 	lsn_t	lsn)	/*!< in: lsn of a byte within the block */
 {
-	return(((ulint) (lsn / OS_FILE_LOG_BLOCK_SIZE) & 0x3FFFFFFFUL) + 1);
+	return(((ulint) (lsn / OS_FILE_LOG_BLOCK_SIZE) &
+		DBUG_EVALUATE_IF("innodb_small_log_block_no_limit",
+			0xFUL, 0x3FFFFFFFUL)) + 1);
 }
 
 /************************************************************//**
diff --git a/storage/innobase/include/row0ins.h b/storage/innobase/include/row0ins.h
index 0906b516c35..82bb3c91171 100644
--- a/storage/innobase/include/row0ins.h
+++ b/storage/innobase/include/row0ins.h
@@ -198,10 +198,6 @@ struct ins_node_t{
 				entry_list and sys fields are stored here;
 				if this is NULL, entry list should be created
 				and buffers for sys fields in row allocated */
-	dict_index_t*   duplicate;
-				/* This is the first index that reported
-				DB_DUPLICATE_KEY.  Used in the case of REPLACE
-				or INSERT ... ON DUPLICATE UPDATE. */
 	ulint		magic_n;
 };
 
diff --git a/storage/innobase/include/span.h b/storage/innobase/include/span.h
new file mode 100644
index 00000000000..faeb41029b8
--- /dev/null
+++ b/storage/innobase/include/span.h
@@ -0,0 +1,145 @@
+/*****************************************************************************
+
+Copyright (c) 2019, MariaDB Corporation.
+
+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 the Free Software
+Foundation; version 2 of the License.
+
+This program is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
+
+*****************************************************************************/
+
+#pragma once
+
+#include <cstddef>
+#include <iterator>
+
+namespace st_ {
+
+template <class ElementType> class span {
+public:
+	typedef ElementType element_type;
+	typedef ElementType value_type;
+	typedef size_t index_type;
+	typedef ptrdiff_t difference_type;
+	typedef element_type* pointer;
+	typedef const element_type* const_pointer;
+	typedef element_type& reference;
+	typedef const element_type& const_reference;
+	typedef pointer iterator;
+	typedef const pointer const_iterator;
+	typedef std::reverse_iterator<iterator> reverse_iterator;
+	typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
+
+	span() : data_(NULL), size_(0) {}
+
+	span(pointer ptr, index_type count) : data_(ptr), size_(count) {}
+
+	span(pointer first, pointer last) : data_(first), size_(last - first) {}
+
+	template <size_t N> span(element_type (&arr)[N]) : data_(arr), size_(N)
+	{
+	}
+
+	template <class Container>
+	span(Container& cont) : data_(cont.begin()), size_(cont.size())
+	{
+	}
+
+	template <class Container>
+	span(const Container& cont) : data_(cont.begin()), size_(cont.size())
+	{
+	}
+
+	span(const span& other) : data_(other.data_), size_(other.size_) {}
+
+	~span(){};
+
+	span& operator=(const span& other)
+	{
+		data_ = other.data_;
+		size_ = other.size_;
+		return *this;
+	}
+
+	template <size_t Count> span<element_type> first() const
+	{
+		assert(!empty());
+		return span(data_, 1);
+	}
+	template <size_t Count> span<element_type> last() const
+	{
+		assert(!empty());
+		return span(data_ + size() - 1, 1);
+	}
+
+	span<element_type> first(index_type count) const
+	{
+		assert(!empty());
+		return span(data_, 1);
+	}
+	span<element_type> last(index_type count) const
+	{
+		assert(!empty());
+		return span(data_ + size() - 1, 1);
+	}
+	span<element_type> subspan(index_type offset, index_type count) const
+	{
+		assert(!empty());
+		assert(size() >= offset + count);
+		return span(data_ + offset, count);
+	}
+
+	index_type size() const { return size_; }
+	index_type size_bytes() const { return size_ * sizeof(ElementType); }
+	bool empty() const __attribute__((warn_unused_result))
+	{
+		return size_ == 0;
+	}
+
+	reference operator[](index_type idx) const
+	{
+		assert(size() > idx);
+		return data_[idx];
+	}
+	reference front() const
+	{
+		assert(!empty());
+		return data_[0];
+	}
+	reference back() const
+	{
+		assert(!empty());
+		return data_[size() - 1];
+	}
+	pointer data() const
+	{
+		assert(!empty());
+		return data_;
+	}
+
+	iterator begin() const { return data_; }
+	iterator end() const { return data_ + size_; }
+	reverse_iterator rbegin() const
+	{
+		return std::reverse_iterator<iterator>(std::advance(end(), -1));
+	}
+	reverse_iterator rend() const
+	{
+		return std::reverse_iterator<iterator>(
+		    std::advance(begin(), -1));
+	}
+
+private:
+	pointer data_;
+	index_type size_;
+};
+
+} // namespace st_
diff --git a/storage/innobase/log/log0log.cc b/storage/innobase/log/log0log.cc
index 31323a9a58c..6532a16b97d 100644
--- a/storage/innobase/log/log0log.cc
+++ b/storage/innobase/log/log0log.cc
@@ -757,11 +757,14 @@ loop:
 	the trailer fields of the log blocks */
 
 	for (i = 0; i < write_len / OS_FILE_LOG_BLOCK_SIZE; i++) {
+#ifdef UNIV_DEBUG
+		ulint hdr_no_2 = log_block_get_hdr_no(buf) + i;
+		DBUG_EXECUTE_IF("innodb_small_log_block_no_limit",
+				hdr_no_2 = ((hdr_no_2 - 1) & 0xFUL) + 1;);
+#endif
 		ut_ad(pad_len >= len
-		      || i * OS_FILE_LOG_BLOCK_SIZE >= len - pad_len
-		      || log_block_get_hdr_no(
-			      buf + i * OS_FILE_LOG_BLOCK_SIZE)
-			 == log_block_get_hdr_no(buf) + i);
+			|| i * OS_FILE_LOG_BLOCK_SIZE >= len - pad_len
+			|| log_block_get_hdr_no(buf + i * OS_FILE_LOG_BLOCK_SIZE) == hdr_no_2);
 		log_block_store_checksum(buf + i * OS_FILE_LOG_BLOCK_SIZE);
 	}
 
diff --git a/storage/innobase/que/que0que.cc b/storage/innobase/que/que0que.cc
index 52e22d8d881..878427e536e 100644
--- a/storage/innobase/que/que0que.cc
+++ b/storage/innobase/que/que0que.cc
@@ -684,11 +684,6 @@ que_thr_stop(
 		trx->lock.wait_thr = thr;
 		thr->state = QUE_THR_LOCK_WAIT;
 
-	} else if (trx->duplicates && trx->error_state == DB_DUPLICATE_KEY
-		   && thd_rpl_stmt_based(trx->mysql_thd)) {
-
-		return(FALSE);
-
 	} else if (trx->error_state != DB_SUCCESS
 		   && trx->error_state != DB_LOCK_WAIT) {
 
diff --git a/storage/innobase/row/row0ins.cc b/storage/innobase/row/row0ins.cc
index 3f7092d8d00..26e46aedeb5 100644
--- a/storage/innobase/row/row0ins.cc
+++ b/storage/innobase/row/row0ins.cc
@@ -88,7 +88,6 @@ ins_node_create(
 	node->select = NULL;
 
 	node->trx_id = 0;
-	node->duplicate = NULL;
 
 	node->entry_sys_heap = mem_heap_create(128);
 
@@ -193,7 +192,6 @@ ins_node_set_new_row(
 	node->state = INS_NODE_SET_IX_LOCK;
 	node->index = NULL;
 	node->entry = NULL;
-	node->duplicate = NULL;
 
 	node->row = row;
 
@@ -2362,12 +2360,6 @@ row_ins_duplicate_error_in_clust(
 						  true,
 						  ULINT_UNDEFINED, &heap);
 
-			ulint lock_type =
-				trx->isolation_level <= TRX_ISO_READ_COMMITTED
-				|| (trx->mysql_thd
-				    && !thd_rpl_stmt_based(trx->mysql_thd))
-				? LOCK_REC_NOT_GAP : LOCK_ORDINARY;
-
 			/* We set a lock on the possible duplicate: this
 			is needed in logical logging of MySQL to make
 			sure that in roll-forward we get the same duplicate
@@ -2384,13 +2376,13 @@ row_ins_duplicate_error_in_clust(
 				INSERT ON DUPLICATE KEY UPDATE). */
 
 				err = row_ins_set_exclusive_rec_lock(
-					lock_type,
+					LOCK_REC_NOT_GAP,
 					btr_cur_get_block(cursor),
 					rec, cursor->index, offsets, thr);
 			} else {
 
 				err = row_ins_set_shared_rec_lock(
-					lock_type,
+					LOCK_REC_NOT_GAP,
 					btr_cur_get_block(cursor), rec,
 					cursor->index, offsets, thr);
 			}
@@ -2405,7 +2397,10 @@ row_ins_duplicate_error_in_clust(
 
 			if (row_ins_dupl_error_with_rec(
 				    rec, entry, cursor->index, offsets)) {
-				goto duplicate;
+duplicate:
+				trx->error_info = cursor->index;
+				err = DB_DUPLICATE_KEY;
+				goto func_exit;
 			}
 		}
 	}
@@ -2448,10 +2443,7 @@ row_ins_duplicate_error_in_clust(
 
 			if (row_ins_dupl_error_with_rec(
 				    rec, entry, cursor->index, offsets)) {
-duplicate:
-				trx->error_info = cursor->index;
-				err = DB_DUPLICATE_KEY;
-				goto func_exit;
+				goto duplicate;
 			}
 		}
 
@@ -3102,46 +3094,6 @@ row_ins_sec_index_entry_low(
 			&cursor, 0, __FILE__, __LINE__, &mtr);
 	}
 
-	if (!(flags & BTR_NO_LOCKING_FLAG)
-	    && dict_index_is_unique(index)
-	    && thr_get_trx(thr)->duplicates
-	    && thr_get_trx(thr)->isolation_level >= TRX_ISO_REPEATABLE_READ
-	    && thd_rpl_stmt_based(thr_get_trx(thr)->mysql_thd)) {
-
-		/* In statement-based replication, when replicating a
-		REPLACE statement or ON DUPLICATE KEY UPDATE clause, a
-		gap lock is taken on the position of the to-be-inserted record,
-		to avoid other concurrent transactions from inserting the same
-		record. */
-
-		dberr_t	err;
-		const rec_t* rec = page_rec_get_next_const(
-			btr_cur_get_rec(&cursor));
-
-		ut_ad(!page_rec_is_infimum(rec));
-
-		offsets = rec_get_offsets(rec, index, offsets, true,
-					  ULINT_UNDEFINED, &offsets_heap);
-
-		err = row_ins_set_exclusive_rec_lock(
-			LOCK_GAP, btr_cur_get_block(&cursor), rec,
-			index, offsets, thr);
-
-		switch (err) {
-		case DB_SUCCESS:
-		case DB_SUCCESS_LOCKED_REC:
-			if (thr_get_trx(thr)->error_state != DB_DUPLICATE_KEY) {
-				break;
-			}
-			/* Fall through (skip actual insert) after we have
-			successfully acquired the gap lock. */
-		default:
-			goto func_exit;
-		}
-	}
-
-	ut_ad(thr_get_trx(thr)->error_state == DB_SUCCESS);
-
 	if (dup_chk_only) {
 		goto func_exit;
 	}
@@ -3664,13 +3616,6 @@ row_ins(
 
 	DBUG_PRINT("row_ins", ("table: %s", node->table->name.m_name));
 
-	trx_t* trx = thr_get_trx(thr);
-
-	if (node->duplicate) {
-		ut_ad(thd_rpl_stmt_based(trx->mysql_thd));
-		trx->error_state = DB_DUPLICATE_KEY;
-	}
-
 	if (node->state == INS_NODE_ALLOC_ROW_ID) {
 
 		row_ins_alloc_row_id_step(node);
@@ -3696,91 +3641,7 @@ row_ins(
 		if (node->index->type != DICT_FTS) {
 			dberr_t err = row_ins_index_entry_step(node, thr);
 
-			switch (err) {
-			case DB_SUCCESS:
-				break;
-			case DB_NO_REFERENCED_ROW:
-				if (!dict_index_is_unique(node->index)) {
-					DBUG_RETURN(err);
-				}
-				/* fall through */
-			case DB_DUPLICATE_KEY:
-				ut_ad(dict_index_is_unique(node->index));
-
-				if (trx->isolation_level
-				    >= TRX_ISO_REPEATABLE_READ
-				    && trx->duplicates
-				    && !node->table->is_temporary()
-				    && thd_rpl_stmt_based(trx->mysql_thd)) {
-
-					/* When we are in REPLACE statement or
-					INSERT ..  ON DUPLICATE UPDATE
-					statement, we process all the
-					unique secondary indexes, even after we
-					encounter a duplicate error. This is
-					done to take necessary gap locks in
-					secondary indexes to block concurrent
-					transactions from inserting the
-					searched records. */
-					if (err == DB_NO_REFERENCED_ROW
-					    && node->duplicate) {
-						/* A foreign key check on a
-						unique index may fail to
-						find the record.
-
-						Consider as a example
-						following:
-						create table child(a int not null
-						primary key, b int not null,
-						c int,
-						unique key (b),
-						foreign key (b) references
-						parent (id)) engine=innodb;
-
-						insert into child values
-						(1,1,2);
-
-						insert into child(a) values
-						(1) on duplicate key update
-						c = 3;
-
-						Now primary key value 1
-						naturally causes duplicate
-						key error that will be
-						stored on node->duplicate.
-						If there was no duplicate
-						key error, we should return
-						the actual no referenced
-						row error.
-
-						As value for
-						column b used in both unique
-						key and foreign key is not
-						provided, server uses 0 as a
-						search value. This is
-						naturally, not found leading
-						to DB_NO_REFERENCED_ROW.
-						But, we should update the
-						row with primay key value 1
-						anyway.
-
-						Return the
-						original  DB_DUPLICATE_KEY
-						error after
-						placing all gaplocks. */
-						err = DB_DUPLICATE_KEY;
-						break;
-					} else if (!node->duplicate) {
-						/* Save 1st dup error. Ignore
-						subsequent dup errors. */
-						node->duplicate = node->index;
-						trx->error_state
-							= DB_DUPLICATE_KEY;
-					}
-					break;
-				}
-				// fall through
-			default:
+			if (err != DB_SUCCESS) {
 				DBUG_RETURN(err);
 			}
 		}
@@ -3797,31 +3658,13 @@ row_ins(
 			node->index = dict_table_get_next_index(node->index);
 			node->entry = UT_LIST_GET_NEXT(tuple_list, node->entry);
 		}
-
-		/* After encountering a duplicate key error, we process
-		remaining indexes just to place gap locks and no actual
-		insertion will take place.  These gap locks are needed
-		only for unique indexes.  So skipping non-unique indexes. */
-		if (node->duplicate) {
-			ut_ad(thd_rpl_stmt_based(trx->mysql_thd));
-			while (node->index
-			       && !dict_index_is_unique(node->index)) {
-
-				node->index = dict_table_get_next_index(
-					node->index);
-				node->entry = UT_LIST_GET_NEXT(tuple_list,
-							       node->entry);
-			}
-			trx->error_state = DB_DUPLICATE_KEY;
-		}
 	}
 
 	ut_ad(node->entry == NULL);
 
-	trx->error_info = node->duplicate;
 	node->state = INS_NODE_ALLOC_ROW_ID;
 
-	DBUG_RETURN(node->duplicate ? DB_DUPLICATE_KEY : DB_SUCCESS);
+	DBUG_RETURN(DB_SUCCESS);
 }
 
 /***********************************************************//**
diff --git a/storage/innobase/row/row0mysql.cc b/storage/innobase/row/row0mysql.cc
index ad6d6330913..fe6214adefe 100644
--- a/storage/innobase/row/row0mysql.cc
+++ b/storage/innobase/row/row0mysql.cc
@@ -1479,7 +1479,6 @@ error_exit:
 			goto run_again;
 		}
 
-		node->duplicate = NULL;
 		trx->op_info = "";
 
 		if (blob_heap != NULL) {
@@ -1489,8 +1488,6 @@ error_exit:
 		return(err);
 	}
 
-	node->duplicate = NULL;
-
 	if (dict_table_has_fts_index(table)) {
 		doc_id_t	doc_id;
 
diff --git a/storage/innobase/srv/srv0start.cc b/storage/innobase/srv/srv0start.cc
index 2fb1bc4b6e2..2afe1f4fca5 100644
--- a/storage/innobase/srv/srv0start.cc
+++ b/storage/innobase/srv/srv0start.cc
@@ -1855,7 +1855,7 @@ dberr_t srv_start(bool create_new_db)
 		ut_a(fil_validate());
 		ut_a(log_space);
 
-		ut_a(srv_log_file_size <= 512ULL << 30);
+		ut_a(srv_log_file_size <= log_group_max_size);
 
 		const ulint size = 1 + ulint((srv_log_file_size - 1)
 					     >> srv_page_size_shift);
diff --git a/storage/maria/maria_chk.c b/storage/maria/maria_chk.c
index f4a7a207615..6ea40e65b29 100644
--- a/storage/maria/maria_chk.c
+++ b/storage/maria/maria_chk.c
@@ -1006,6 +1006,7 @@ static int maria_chk(HA_CHECK *param, char *filename)
   int error,lock_type,recreate;
   uint warning_printed_by_chk_status;
   my_bool rep_quick= MY_TEST(param->testflag & (T_QUICK | T_FORCE_UNIQUENESS));
+  my_bool born_transactional;
   MARIA_HA *info;
   File datafile;
   char llbuff[22],llbuff2[22];
@@ -1448,6 +1449,7 @@ static int maria_chk(HA_CHECK *param, char *filename)
   maria_lock_database(info, F_UNLCK);
 
 end2:
+  born_transactional= share->base.born_transactional;
   if (maria_close(info))
   {
     _ma_check_print_error(param, default_close_errmsg, my_errno, filename);
@@ -1463,7 +1465,7 @@ end2:
                                       MYF(MY_REDEL_MAKE_BACKUP) : MYF(0)));
   }
   if (opt_transaction_logging &&
-      share->base.born_transactional && !error &&
+      born_transactional && !error &&
       (param->testflag & (T_REP_ANY | T_SORT_RECORDS | T_SORT_INDEX |
                           T_ZEROFILL)))
     error= write_log_record(param);
diff --git a/storage/mroonga/vendor/groonga/lib/ts.c b/storage/mroonga/vendor/groonga/lib/ts.c
index 68e363a27d7..909f4864786 100644
--- a/storage/mroonga/vendor/groonga/lib/ts.c
+++ b/storage/mroonga/vendor/groonga/lib/ts.c
@@ -683,7 +683,7 @@ static grn_rc
 grn_ts_select_output(grn_ctx *ctx, grn_obj *table, grn_ts_str str,
                      const grn_ts_record *in, size_t n_in, size_t n_hits)
 {
-  grn_ts_writer *writer;
+  grn_ts_writer *writer= 0;
   grn_rc rc = grn_ts_writer_open(ctx, table, str, &writer);
   if (rc != GRN_SUCCESS) {
     return rc;
diff --git a/storage/mroonga/vendor/groonga/lib/ts/ts_expr_node.c b/storage/mroonga/vendor/groonga/lib/ts/ts_expr_node.c
index ddd69714b1e..4ae900034bb 100644
--- a/storage/mroonga/vendor/groonga/lib/ts/ts_expr_node.c
+++ b/storage/mroonga/vendor/groonga/lib/ts/ts_expr_node.c
@@ -5173,7 +5173,7 @@ grn_ts_expr_node_deref(grn_ctx *ctx, grn_ts_expr_node **node_ptr)
 {
   grn_ts_expr_node *node = *node_ptr, **in_ptr = NULL;
   while ((node->data_kind & ~GRN_TS_VECTOR_FLAG) == GRN_TS_REF) {
-    grn_ts_expr_node *new_node;
+    grn_ts_expr_node *new_node= 0;
     grn_rc rc = grn_ts_expr_node_deref_once(ctx, node, &new_node);
     if (rc != GRN_SUCCESS) {
       if (in_ptr) {
diff --git a/storage/rocksdb/ha_rocksdb.cc b/storage/rocksdb/ha_rocksdb.cc
index 3d91acea03d..474ff6d7fc1 100644
--- a/storage/rocksdb/ha_rocksdb.cc
+++ b/storage/rocksdb/ha_rocksdb.cc
@@ -14316,6 +14316,8 @@ static int rocksdb_validate_update_cf_options(
   // then there's no point to proceed.
   if (!Rdb_cf_options::parse_cf_options(str, &option_map)) {
     my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), "rocksdb_update_cf_options", str);
+    // Free what we've copied with my_strdup above.
+    my_free((void*)(*(const char **)save));
     return HA_EXIT_FAILURE;
   }
   // Loop through option_map and create missing column families
diff --git a/storage/rocksdb/mysql-test/rocksdb/include/group_min_max.inc b/storage/rocksdb/mysql-test/rocksdb/include/group_min_max.inc
index 79ac367a73b..6c3e29537b1 100644
--- a/storage/rocksdb/mysql-test/rocksdb/include/group_min_max.inc
+++ b/storage/rocksdb/mysql-test/rocksdb/include/group_min_max.inc
@@ -266,6 +266,17 @@ select a1,  max(c)           from t2 where a1 in ('a','b','d') group by a1,a2,b;
 
 # B) Equalities only over the non-group 'B' attributes
 # plans
+--echo #
+--echo # MariaDB: we dont have the following patch:
+--echo #
+--echo #  commit 60a92a79a3b7fde3c6efe91799e344b977c8e5c3
+--echo #  Author: Manuel Ung <mung@fb.com>
+--echo #  Date:   Thu Apr 19 23:06:27 2018 -0700
+--echo #
+--echo #   Enhance group-by loose index scan
+--echo #
+--echo #  So the following results are not very meaningful, but are still kept here
+
 explain select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a') and (b = 'b') group by a1;
 explain select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 explain select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
@@ -730,9 +741,9 @@ explain extended select a1,a2,count(a2) from t1 where (a1 > 'a') group by a1,a2,
 explain extended select sum(ord(a1)) from t1 where (a1 > 'a') group by a1,a2,b;
 
 # test multi_range_groupby flag
-set optimizer_switch = 'multi_range_groupby=off';
+#MariaDB: no support: set optimizer_switch = 'multi_range_groupby=off';
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'a' or b = 'b') group by a1;
-set optimizer_switch = 'default';
+#set optimizer_switch = 'default';
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'a' or b = 'b') group by a1;
 
 
@@ -1361,41 +1372,41 @@ drop table t1;
 eval CREATE TABLE t (a INT, b INT, KEY(a,b)) engine=$engine;
 INSERT INTO t VALUES (1,1), (2,2), (3,3), (4,4), (1,0), (3,2), (4,5);
 ANALYZE TABLE t;
-let $DEFAULT_TRACE_MEM_SIZE=1048576; # 1MB
-eval set optimizer_trace_max_mem_size=$DEFAULT_TRACE_MEM_SIZE;
-set @@session.optimizer_trace='enabled=on';
-set end_markers_in_json=on;
+# MariaDB: 10.2 doesn't have trace, yet: let $DEFAULT_TRACE_MEM_SIZE=1048576; # 1MB
+# eval set optimizer_trace_max_mem_size=$DEFAULT_TRACE_MEM_SIZE;
+# set @@session.optimizer_trace='enabled=on';
+# set end_markers_in_json=on;
 
 ANALYZE TABLE t;
 
 SELECT a, SUM(DISTINCT a), MIN(b) FROM t GROUP BY a;
 EXPLAIN SELECT a, SUM(DISTINCT a), MIN(b) FROM t GROUP BY a;
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
+#SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
+#  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
 
 SELECT a, SUM(DISTINCT a), MAX(b) FROM t GROUP BY a;
 EXPLAIN SELECT a, SUM(DISTINCT a), MAX(b) FROM t GROUP BY a;
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
+#SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
+#  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
 
 SELECT a, MAX(b) FROM t GROUP BY a HAVING SUM(DISTINCT a);
 EXPLAIN SELECT a, MAX(b) FROM t GROUP BY a HAVING SUM(DISTINCT a);
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
+#SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
+#  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
 
 SELECT SUM(DISTINCT a), MIN(b), MAX(b) FROM t;
 EXPLAIN SELECT SUM(DISTINCT a), MIN(b), MAX(b) FROM t;
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
+#SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
+#  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
 
 SELECT a, SUM(DISTINCT a), MIN(b), MAX(b) FROM t GROUP BY a;
 EXPLAIN SELECT a, SUM(DISTINCT a), MIN(b), MAX(b) FROM t GROUP BY a;
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
+#SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
+#  FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
 
-SET optimizer_trace_max_mem_size=DEFAULT;
-SET optimizer_trace=DEFAULT;
-SET end_markers_in_json=DEFAULT;
+#SET optimizer_trace_max_mem_size=DEFAULT;
+#SET optimizer_trace=DEFAULT;
+#SET end_markers_in_json=DEFAULT;
 
 DROP TABLE t;
 
diff --git a/storage/rocksdb/mysql-test/rocksdb/r/group_min_max.result b/storage/rocksdb/mysql-test/rocksdb/r/group_min_max.result
index 7fede0ac603..a1031f518e0 100644
--- a/storage/rocksdb/mysql-test/rocksdb/r/group_min_max.result
+++ b/storage/rocksdb/mysql-test/rocksdb/r/group_min_max.result
@@ -1,4 +1,5 @@
-set global debug="+d,force_group_by";
+set @debug_tmp= @@debug_dbug;
+set global debug_dbug="+d,force_group_by";
 drop table if exists t1;
 create table t1 (
 a1 char(64), a2 char(64), b char(16), c char(16) not null, d char(16), dummy char(248) default ' '
@@ -134,34 +135,34 @@ Table	Op	Msg_type	Msg_text
 test.t3	analyze	status	OK
 explain select a1, min(a2) from t1 group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	130	NULL	63	Using index for group-by
 explain select a1, max(a2) from t1 group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	65	NULL	126	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	65	NULL	63	Using index for group-by
 explain select a1, min(a2), max(a2) from t1 group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	130	NULL	63	Using index for group-by
 explain select a1, a2, b, min(c), max(c) from t1 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using index for group-by
 explain select a1,a2,b,max(c),min(c) from t1 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using index for group-by
 explain select a1,a2,b,max(c),min(c) from t2 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	#	NULL	#	Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	#	NULL	#	Using index for group-by
 explain select min(a2), a1, max(a2), min(a2), a1 from t1 group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	130	NULL	63	Using index for group-by
 explain select a1, b, min(c), a1, max(c), b, a2, max(c), max(c) from t1 group by a1, a2, b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using index for group-by
 explain select min(a2) from t1 group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	130	NULL	63	Using index for group-by
 explain select a2, min(c), max(c) from t1 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using index for group-by
 select a1, min(a2) from t1 group by a1;
 a1	min(a2)
 a	a
@@ -288,37 +289,37 @@ b	i421	l421
 b	m422	p422
 explain select a1,a2,b,min(c),max(c) from t1 where a1 < 'd' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where a1 >= 'b' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where a1 >= 'c' or a1 < 'b' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	1002	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	502	Using where; Using index for group-by
 explain select a1, max(c)            from t1 where a1 >= 'c' or a1 < 'b' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	1002	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	502	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where a1 >= 'c' or a2 < 'b' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where a1 = 'z' or a1 = 'b' or a1 = 'd' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	1503	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	753	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where a1 = 'z' or a1 = 'b' or a1 = 'd' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	1503	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	753	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where (a1 = 'b' or a1 = 'd' or a1 = 'a' or a1 = 'c') and (a2 > 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	2004	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	1004	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (a1 = 'b' or a1 = 'd' or a1 = 'a' or a1 = 'c') and (a2 > 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	2004	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	1004	Using where; Using index for group-by
 explain select a1,min(c),max(c)      from t1 where a1 >= 'b' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,  max(c)           from t1 where a1 in ('a','b','d') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	1503	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	753	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t2 where a1 < 'd' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
@@ -668,99 +669,109 @@ d	d411
 d	h412
 d	l421
 d	p422
+#
+# MariaDB: we dont have the following patch:
+#
+#  commit 60a92a79a3b7fde3c6efe91799e344b977c8e5c3
+#  Author: Manuel Ung <mung@fb.com>
+#  Date:   Thu Apr 19 23:06:27 2018 -0700
+#
+#   Enhance group-by loose index scan
+#
+#  So the following results are not very meaningful, but are still kept here
 explain select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	63	Using where; Using index for group-by
 explain select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a' or a2 = 'b') and (b = 'b' or b = 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	Using where; Using index
 explain select a1,max(c),min(c)      from t1 where (a2 = 'a') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	63	Using where; Using index for group-by
 explain select a1,max(c),min(c)      from t1 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	Using where; Using index
 explain select a1,max(c),min(c)      from t1 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,       max(c) from t1 where (b = 'b') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	126	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where (b = 'b' or b = 'a') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,min(c),max(c) from t1 where (b = 'b') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	126	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (b = 'b' or b = 'a') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	Using where; Using index
 explain select a1,a2, max(c)         from t1 where (b = 'b') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	126	Using where; Using index for group-by
 explain select a1,a2, max(c)         from t1 where (b = 'b' or b = 'a') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	63	Using where; Using index for group-by
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,max(c),min(c)      from t2 where (a2 = 'a') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	63	Using where; Using index for group-by
 explain select a1,max(c),min(c)      from t2 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,max(c),min(c)      from t2 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,       max(c) from t2 where (b = 'b') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	126	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t2 where (b = 'b' or b = 'a') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,min(c),max(c) from t2 where (b = 'b') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	126	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (b = 'b' or b = 'a') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2, max(c)         from t2 where (b = 'b') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	126	Using where; Using index for group-by
 explain select a1,a2, max(c)         from t2 where (b = 'b' or b = 'a') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t3 where (a2 = 'a') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t3	range	idx_t3_0,idx_t3_1,idx_t3_2	idx_t3_1	6	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t3	range	NULL	idx_t3_1	6	NULL	63	Using where; Using index for group-by
 explain select a1,a2,b,max(c),min(c) from t3 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t3	range	idx_t3_0,idx_t3_1,idx_t3_2	idx_t3_1	6	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t3	index	NULL	idx_t3_1	10	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t3 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t3	range	idx_t3_0,idx_t3_1,idx_t3_2	idx_t3_1	6	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t3	index	NULL	idx_t3_1	10	NULL	1000	Using where; Using index
 explain select a1,max(c),min(c)      from t3 where (a2 = 'a') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t3	range	idx_t3_0,idx_t3_1,idx_t3_2	idx_t3_1	6	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t3	range	NULL	idx_t3_1	6	NULL	63	Using where; Using index for group-by
 explain select a1,max(c),min(c)      from t3 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t3	range	idx_t3_0,idx_t3_1,idx_t3_2	idx_t3_1	6	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t3	index	NULL	idx_t3_1	10	NULL	1000	Using where; Using index
 explain select a1,max(c),min(c)      from t3 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t3	range	idx_t3_0,idx_t3_1,idx_t3_2	idx_t3_1	6	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t3	index	NULL	idx_t3_1	10	NULL	1000	Using where; Using index
 select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a') and (b = 'b') group by a1;
 a1	a2	b	max(c)	min(c)
 a	a	b	h112	e112
@@ -769,22 +780,22 @@ c	a	b	h312	e312
 d	a	b	h412	e412
 select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 a1	a2	b	max(c)	min(c)
-a	b	b	p122	e112
-b	b	b	p222	e212
-c	b	b	p322	e312
-d	b	b	p422	e412
+a	a	b	p122	e112
+b	a	b	p222	e212
+c	a	b	p322	e312
+d	a	b	p422	e412
 select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 a1	a2	b	max(c)	min(c)
-a	a	b	h112	a111
-b	a	b	h212	a211
-c	a	b	h312	a311
-d	a	b	h412	a411
+a	a	a	h112	a111
+b	a	a	h212	a211
+c	a	a	h312	a311
+d	a	a	h412	a411
 select a1,a2,b,max(c),min(c) from t1 where (a2 = 'a' or a2 = 'b') and (b = 'b' or b = 'a') group by a1;
 a1	a2	b	max(c)	min(c)
-a	b	b	p122	a111
-b	b	b	p222	a211
-c	b	b	p322	a311
-d	b	b	p422	a411
+a	a	a	p122	a111
+b	a	a	p222	a211
+c	a	a	p322	a311
+d	a	a	p422	a411
 select a1,max(c),min(c)      from t1 where (a2 = 'a') and (b = 'b') group by a1;
 a1	max(c)	min(c)
 a	h112	e112
@@ -815,14 +826,14 @@ d	a	b	h412
 d	b	b	p422
 select a1,a2,b,       max(c) from t1 where (b = 'b' or b = 'a') group by a1,a2;
 a1	a2	b	max(c)
-a	a	b	h112
-a	b	b	p122
-b	a	b	h212
-b	b	b	p222
-c	a	b	h312
-c	b	b	p322
-d	a	b	h412
-d	b	b	p422
+a	a	a	h112
+a	b	a	p122
+b	a	a	h212
+b	b	a	p222
+c	a	a	h312
+c	b	a	p322
+d	a	a	h412
+d	b	a	p422
 select a1,a2,b,min(c),max(c) from t1 where (b = 'b') group by a1,a2;
 a1	a2	b	min(c)	max(c)
 a	a	b	e112	h112
@@ -835,14 +846,14 @@ d	a	b	e412	h412
 d	b	b	m422	p422
 select a1,a2,b,min(c),max(c) from t1 where (b = 'b' or b = 'a') group by a1,a2;
 a1	a2	b	min(c)	max(c)
-a	a	b	a111	h112
-a	b	b	i121	p122
-b	a	b	a211	h212
-b	b	b	i221	p222
-c	a	b	a311	h312
-c	b	b	i321	p322
-d	a	b	a411	h412
-d	b	b	i421	p422
+a	a	a	a111	h112
+a	b	a	i121	p122
+b	a	a	a211	h212
+b	b	a	i221	p222
+c	a	a	a311	h312
+c	b	a	i321	p322
+d	a	a	a411	h412
+d	b	a	i421	p422
 select a1,a2, max(c)         from t1 where (b = 'b') group by a1,a2;
 a1	a2	max(c)
 a	a	h112
@@ -872,17 +883,18 @@ d	a	b	h412	e412
 e	a	b	NULL	NULL
 select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 a1	a2	b	max(c)	min(c)
-a	b	b	p122	e112
-b	b	b	p222	e212
-c	b	b	p322	e312
-d	b	b	p422	e412
+a	a	b	p122	e112
+b	a	b	p222	e212
+c	a	b	p322	e312
+d	a	b	p422	e412
+e	a	b	NULL	NULL
 select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 a1	a2	b	max(c)	min(c)
-a	a	b	h112	a111
-b	a	b	h212	a211
-c	a	b	h312	a311
-d	a	b	h412	a411
-e	a	b	NULL	NULL
+a	a	a	h112	a111
+b	a	a	h212	a211
+c	a	a	h312	a311
+d	a	a	h412	a411
+e	a	a	NULL	NULL
 select a1,max(c),min(c)      from t2 where (a2 = 'a') and (b = 'b') group by a1;
 a1	max(c)	min(c)
 a	h112	e112
@@ -896,6 +908,7 @@ a	p122	e112
 b	p222	e212
 c	p322	e312
 d	p422	e412
+e	NULL	NULL
 select a1,max(c),min(c)      from t2 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 a1	max(c)	min(c)
 a	h112	a111
@@ -916,15 +929,15 @@ d	b	b	p422
 e	a	b	NULL
 select a1,a2,b,       max(c) from t2 where (b = 'b' or b = 'a') group by a1,a2;
 a1	a2	b	max(c)
-a	a	b	h112
-a	b	b	p122
-b	a	b	h212
-b	b	b	p222
-c	a	b	h312
-c	b	b	p322
-d	a	b	h412
-d	b	b	p422
-e	a	b	NULL
+a	a	a	h112
+a	b	a	p122
+b	a	a	h212
+b	b	a	p222
+c	a	a	h312
+c	b	a	p322
+d	a	a	h412
+d	b	a	p422
+e	a	a	NULL
 select a1,a2,b,min(c),max(c) from t2 where (b = 'b') group by a1,a2;
 a1	a2	b	min(c)	max(c)
 a	a	b	e112	h112
@@ -938,15 +951,15 @@ d	b	b	m422	p422
 e	a	b	NULL	NULL
 select a1,a2,b,min(c),max(c) from t2 where (b = 'b' or b = 'a') group by a1,a2;
 a1	a2	b	min(c)	max(c)
-a	a	b	a111	h112
-a	b	b	i121	p122
-b	a	b	a211	h212
-b	b	b	i221	p222
-c	a	b	a311	h312
-c	b	b	i321	p322
-d	a	b	a411	h412
-d	b	b	i421	p422
-e	a	b	NULL	NULL
+a	a	a	a111	h112
+a	b	a	i121	p122
+b	a	a	a211	h212
+b	b	a	i221	p222
+c	a	a	a311	h312
+c	b	a	i321	p322
+d	a	a	a411	h412
+d	b	a	i421	p422
+e	a	a	NULL	NULL
 select a1,a2, max(c)         from t2 where (b = 'b') group by a1,a2;
 a1	a2	max(c)
 a	a	h112
@@ -976,14 +989,14 @@ b	a	b	h212	e212
 c	a	b	h312	e312
 select a1,a2,b,max(c),min(c) from t3 where (a2 = 'a' or a2 = 'b') and (b = 'b') group by a1;
 a1	a2	b	max(c)	min(c)
-a	b	b	p122	e112
-b	b	b	p222	e212
-c	b	b	p322	e312
+a	a	b	p122	e112
+b	a	b	p222	e212
+c	a	b	p322	e312
 select a1,a2,b,max(c),min(c) from t3 where (a2 = 'a') and (b = 'b' or b = 'a') group by a1;
 a1	a2	b	max(c)	min(c)
-a	a	b	h112	a111
-b	a	b	h212	a211
-c	a	b	h312	a311
+a	a	a	h112	a111
+b	a	a	h212	a211
+c	a	a	h312	a311
 select a1,max(c),min(c)      from t3 where (a2 = 'a') and (b = 'b') group by a1;
 a1	max(c)	min(c)
 a	h112	e112
@@ -1001,37 +1014,41 @@ b	h212	a211
 c	h312	a311
 explain select a1,a2,b,min(c) from t2 where (a2 = 'a') and b is NULL group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	63	Using where; Using index for group-by
 explain select a1,a2,b,min(c) from t2 where (a2 = 'a' or a2 = 'b') and b is NULL group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c) from t2 where (a2 = 'a') and b is NULL group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	63	Using where; Using index for group-by
 explain select a1,a2,b,max(c) from t2 where (a2 = 'a' or a2 = 'b') and b is NULL group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,min(c) from t2 where b is NULL group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	126	Using where; Using index for group-by
 explain select a1,a2,b,max(c) from t2 where b is NULL group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	126	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where b is NULL group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	126	Using where; Using index for group-by
 select a1,a2,b,min(c) from t2 where (a2 = 'a') and b is NULL group by a1;
 a1	a2	b	min(c)
 a	a	NULL	a777
 c	a	NULL	c777
 select a1,a2,b,min(c) from t2 where (a2 = 'a' or a2 = 'b') and b is NULL group by a1;
 a1	a2	b	min(c)
+a	a	NULL	a777
+c	a	NULL	c777
 select a1,a2,b,max(c) from t2 where (a2 = 'a') and b is NULL group by a1;
 a1	a2	b	max(c)
 a	a	NULL	a999
 c	a	NULL	c999
 select a1,a2,b,max(c) from t2 where (a2 = 'a' or a2 = 'b') and b is NULL group by a1;
 a1	a2	b	max(c)
+a	a	NULL	a999
+c	a	NULL	c999
 select a1,a2,b,min(c) from t2 where b is NULL group by a1,a2;
 a1	a2	b	min(c)
 a	a	NULL	a777
@@ -1050,97 +1067,97 @@ a	a	NULL	a777	a999
 c	a	NULL	c777	c999
 explain select a1,a2,b,       max(c) from t1 where (c > 'b1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c > 'b1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where (c > 'f123') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c > 'f123') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where (c < 'a0') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c < 'a0') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where (c < 'k321') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c < 'k321') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where (c < 'a0') or (c > 'b1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c < 'a0') or (c > 'b1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t1 where (c > 'b1') or (c <= 'g1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c > 'b1') or (c <= 'g1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c > 'b111') and (c <= 'g112') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c < 'c5') or (c = 'g412') or (c = 'k421') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where ((c > 'b111') and (c <= 'g112')) or ((c > 'd000') and (c <= 'i110')) group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (c between 'b111' and 'g112') or (c between 'd000' and 'i110') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t2 where (c > 'b1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (c > 'b1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t2 where (c > 'f123') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (c > 'f123') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t2 where (c < 'a0') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (c < 'a0') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t2 where (c < 'k321') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (c < 'k321') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t2 where (c < 'a0') or (c > 'b1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (c < 'a0') or (c > 'b1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,       max(c) from t2 where (c > 'b1') or (c <= 'g1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (c > 'b1') or (c <= 'g1') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (c > 'b111') and (c <= 'g112') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (c < 'c5') or (c = 'g412') or (c = 'k421') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where ((c > 'b111') and (c <= 'g112')) or ((c > 'd000') and (c <= 'i110')) group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 select a1,a2,b,       max(c) from t1 where (c > 'b1') group by a1,a2,b;
 a1	a2	b	max(c)
 a	a	a	d111
@@ -1562,35 +1579,36 @@ explain select a1,a2,b,min(c),max(c) from t1
 where exists ( select * from t2 where t2.c = t1.c )
 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	index	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	1000	Using where; Using index
-2	DEPENDENT SUBQUERY	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
+1	PRIMARY	t1	index	NULL	idx_t1_1	163	NULL	1000	Using index
+1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	16	func	1	
+2	MATERIALIZED	t2	index	NULL	idx_t2_1	163	NULL	1000	Using index
 explain select a1,a2,b,min(c),max(c) from t1
 where exists ( select * from t2 where t2.c > 'b1' )
 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using index for group-by
+1	PRIMARY	t1	index	NULL	idx_t1_1	163	NULL	1000	Using index
 2	SUBQUERY	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,min(c),max(c) from t1 where (a1 >= 'c' or a2 < 'b') and (b > 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (a1 >= 'c' or a2 < 'b') and (c > 'b111') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t1 where (a2 >= 'b') and (b = 'a') and (c > 'b111') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c) from t1 where ((a1 > 'a') or (a1 < '9'))  and ((a2 >= 'b') and (a2 < 'z')) and (b = 'a') and ((c < 'h112') or (c = 'j121') or (c > 'k121' and c < 'm122') or (c > 'o122')) group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	1002	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	502	Using where; Using index for group-by
 explain select a1,a2,b,min(c) from t1 where ((a1 > 'a') or (a1 < '9'))  and ((a2 >= 'b') and (a2 < 'z')) and (b = 'a') and ((c = 'j121') or (c > 'k121' and c < 'm122') or (c > 'o122') or (c < 'h112') or (c = 'c111')) group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	1002	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	502	Using where; Using index for group-by
 explain select a1,a2,b,min(c) from t1 where (a1 > 'a') and (a2 > 'a') and (b = 'c') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c) from t1 where (ord(a1) > 97) and (ord(a2) + ord(a1) > 194) and (b = 'c') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (a1 >= 'c' or a2 < 'b') and (b > 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
@@ -1599,7 +1617,7 @@ id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c),max(c) from t2 where (a2 >= 'b') and (b = 'a') and (c > 'b111') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,min(c) from t2 where ((a1 > 'a') or (a1 < '9'))  and ((a2 >= 'b') and (a2 < 'z')) and (b = 'a') and ((c < 'h112') or (c = 'j121') or (c > 'k121' and c < 'm122') or (c > 'o122')) group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
@@ -1695,31 +1713,31 @@ select a1,a2,b,min(c) from t2 where (a1 > 'a') and (a2 > 'a') and (b = 'c') grou
 a1	a2	b	min(c)
 explain select a1,a2,b from t1 where (a1 >= 'c' or a2 < 'b') and (b > 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b from t1 where (a2 >= 'b') and (b = 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,c from t1 where (a2 >= 'b') and (b = 'a') and (c = 'i121') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b,c from t1 where (a2 >= 'b') and (b = 'a') and (c = 'i121' or c = 'i121') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b from t1 where (a1 > 'a') and (a2 > 'a') and (b = 'c') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select a1,a2,b from t2 where (a1 >= 'c' or a2 < 'b') and (b > 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b from t2 where (a2 >= 'b') and (b = 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,c from t2 where (a2 >= 'b') and (b = 'a') and (c = 'i121') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b,c from t2 where (a2 >= 'b') and (b = 'a') and (c = 'i121' or c = 'i121') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select a1,a2,b from t2 where (a1 > 'a') and (a2 > 'a') and (b = 'c') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
@@ -1770,50 +1788,50 @@ select a1,a2,b from t2 where (a1 > 'a') and (a2 > 'a') and (b = 'c') group by a1
 a1	a2	b
 explain select distinct a1,a2,b from t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using index for group-by
 explain select distinct a1,a2,b from t1 where (a2 >= 'b') and (b = 'a');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain extended select distinct a1,a2,b,c from t1 where (a2 >= 'b') and (b = 'a') and (c = 'i121');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	SIMPLE	t1	range	idx_t1_1	idx_t1_1	163	NULL	1001	99.90	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	501	100.00	Using where; Using index for group-by
 Warnings:
-Note	1003	/* select#1 */ select distinct `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c` from `test`.`t1` where ((`test`.`t1`.`c` = 'i121') and (`test`.`t1`.`b` = 'a') and (`test`.`t1`.`a2` >= 'b'))
+Note	1003	select distinct `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c` from `test`.`t1` where `test`.`t1`.`b` = 'a' and `test`.`t1`.`c` = 'i121' and `test`.`t1`.`a2` >= 'b'
 explain select distinct a1,a2,b from t1 where (a1 > 'a') and (a2 > 'a') and (b = 'c');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select distinct b from t1 where (a2 >= 'b') and (b = 'a');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	idx_t1_1,idx_t1_2	idx_t1_2	147	NULL	1000	Using where; Using index
+1	SIMPLE	t1	index	NULL	idx_t1_2	147	NULL	1000	Using where; Using index
 explain select distinct a1 from t1 where a1 in ('a', 'd') and a2 = 'b';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	252	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using where; Using index for group-by
 explain select distinct a1 from t1 where a1 in ('a', 'd') and a2 = 'e';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	252	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using where; Using index for group-by
 explain select distinct a1,a2,b from t2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using index for group-by
 explain select distinct a1,a2,b from t2 where (a2 >= 'b') and (b = 'a');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain extended select distinct a1,a2,b,c from t2 where (a2 >= 'b') and (b = 'a') and (c = 'i121');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	SIMPLE	t2	range	idx_t2_1	idx_t2_1	163	NULL	1001	99.90	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	501	100.00	Using where; Using index for group-by
 Warnings:
-Note	1003	/* select#1 */ select distinct `test`.`t2`.`a1` AS `a1`,`test`.`t2`.`a2` AS `a2`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c` from `test`.`t2` where ((`test`.`t2`.`c` = 'i121') and (`test`.`t2`.`b` = 'a') and (`test`.`t2`.`a2` >= 'b'))
+Note	1003	select distinct `test`.`t2`.`a1` AS `a1`,`test`.`t2`.`a2` AS `a2`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c` from `test`.`t2` where `test`.`t2`.`b` = 'a' and `test`.`t2`.`c` = 'i121' and `test`.`t2`.`a2` >= 'b'
 explain select distinct a1,a2,b from t2 where (a1 > 'a') and (a2 > 'a') and (b = 'c');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain select distinct b from t2 where (a2 >= 'b') and (b = 'a');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_1,idx_t2_2	idx_t2_2	146	NULL	1000	Using where; Using index
+1	SIMPLE	t2	index	NULL	idx_t2_2	146	NULL	1000	Using where; Using index
 explain select distinct a1 from t2 where a1 in ('a', 'd') and a2 = 'b';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	129	NULL	252	Using where; Using index for group-by
+1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	129	NULL	126	Using where; Using index for group-by
 explain select distinct a1 from t2 where a1 in ('a', 'd') and a2 = 'e';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	129	NULL	252	Using where; Using index for group-by
+1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	129	NULL	126	Using where; Using index for group-by
 select distinct a1,a2,b from t1;
 a1	a2	b
 a	a	a
@@ -1942,40 +1960,40 @@ c	e
 d	e
 explain select distinct a1,a2,b from t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using index for group-by
 explain select distinct a1,a2,b from t1 where (a2 >= 'b') and (b = 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select distinct a1,a2,b,c from t1 where (a2 >= 'b') and (b = 'a') and (c = 'i121') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	251	Using where; Using index for group-by
 explain select distinct a1,a2,b from t1 where (a1 > 'a') and (a2 > 'a') and (b = 'c') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select distinct b from t1 where (a2 >= 'b') and (b = 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by; Using temporary; Using filesort
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by; Using temporary; Using filesort
 explain select distinct a1 from t1 where a1 in ('a', 'd') and a2 = 'b' group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	252	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using where; Using index for group-by
 explain select distinct a1 from t1 where a1 in ('a', 'd') and a2 = 'e' group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	252	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using where; Using index for group-by
 explain select distinct a1,a2,b from t2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using index for group-by
 explain select distinct a1,a2,b from t2 where (a2 >= 'b') and (b = 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain select distinct a1,a2,b,c from t2 where (a2 >= 'b') and (b = 'a') and (c = 'i121') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	#	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	idx_t2_1	163	NULL	#	Using where; Using index for group-by
 explain select distinct a1,a2,b from t2 where (a1 > 'a') and (a2 > 'a') and (b = 'c') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by
 explain select distinct b from t2 where (a2 >= 'b') and (b = 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_1,idx_t2_2	idx_t2_1	146	NULL	#	Using where; Using index for group-by; Using temporary; Using filesort
+1	SIMPLE	t2	range	NULL	idx_t2_1	146	NULL	#	Using where; Using index for group-by; Using temporary; Using filesort
 explain select distinct a1 from t2 where a1 in ('a', 'd') and a2 = 'b' group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	129	NULL	#	Using where; Using index for group-by
@@ -2064,23 +2082,23 @@ select distinct a1 from t2 where a1 in ('a', 'd') and a2 = 'e' group by a1;
 a1
 explain select count(distinct a1,a2,b) from t1 where (a2 >= 'b') and (b = 'a');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by (scanning)
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select count(distinct a1,a2,b,c) from t1 where (a2 >= 'b') and (b = 'a') and (c = 'i121');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1	idx_t1_1	163	NULL	1001	Using where; Using index for group-by (scanning)
+1	SIMPLE	t1	range	NULL	idx_t1_1	163	NULL	501	Using where; Using index for group-by (scanning)
 explain extended select count(distinct a1,a2,b) from t1 where (a1 > 'a') and (a2 > 'a') and (b = 'c');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	100.00	Using where; Using index for group-by (scanning)
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	100.00	Using where; Using index for group-by
 Warnings:
-Note	1003	/* select#1 */ select count(distinct `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`) AS `count(distinct a1,a2,b)` from `test`.`t1` where ((`test`.`t1`.`b` = 'c') and (`test`.`t1`.`a1` > 'a') and (`test`.`t1`.`a2` > 'a'))
+Note	1003	select count(distinct `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`) AS `count(distinct a1,a2,b)` from `test`.`t1` where `test`.`t1`.`b` = 'c' and `test`.`t1`.`a1` > 'a' and `test`.`t1`.`a2` > 'a'
 explain select count(distinct b) from t1 where (a2 >= 'b') and (b = 'a');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	idx_t1_1,idx_t1_2	idx_t1_2	147	NULL	1000	Using where; Using index
+1	SIMPLE	t1	index	NULL	idx_t1_2	147	NULL	1000	Using where; Using index
 explain extended select 98 + count(distinct a1,a2,b) from t1 where (a1 > 'a') and (a2 > 'a');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	100.00	Using where; Using index for group-by (scanning)
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	100.00	Using where; Using index for group-by
 Warnings:
-Note	1003	/* select#1 */ select (98 + count(distinct `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`)) AS `98 + count(distinct a1,a2,b)` from `test`.`t1` where ((`test`.`t1`.`a1` > 'a') and (`test`.`t1`.`a2` > 'a'))
+Note	1003	select 98 + count(distinct `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`) AS `98 + count(distinct a1,a2,b)` from `test`.`t1` where `test`.`t1`.`a1` > 'a' and `test`.`t1`.`a2` > 'a'
 select count(distinct a1,a2,b) from t1 where (a2 >= 'b') and (b = 'a');
 count(distinct a1,a2,b)
 4
@@ -2098,19 +2116,19 @@ select 98 + count(distinct a1,a2,b) from t1 where (a1 > 'a') and (a2 > 'a');
 104
 explain select a1,a2,b, concat(min(c), max(c)) from t1 where a1 < 'd' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select concat(a1,min(c)),b from t1 where a1 < 'd' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select concat(a1,min(c)),b,max(c) from t1 where a1 < 'd' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select concat(a1,a2),b,min(c),max(c) from t1 where a1 < 'd' group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using where; Using index for group-by
 explain select concat(ord(min(b)),ord(max(b))),min(b),max(b) from t1 group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_1,idx_t1_2	idx_t1_1	147	NULL	251	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	147	NULL	126	Using index for group-by
 select a1,a2,b, concat(min(c), max(c)) from t1 where a1 < 'd' group by a1,a2,b;
 a1	a2	b	concat(min(c), max(c))
 a	a	a	a111d111
@@ -2179,85 +2197,83 @@ concat(ord(min(b)),ord(max(b)))	min(b)	max(b)
 9798	a	b
 explain select a1,a2,b,d,min(c),max(c) from t1 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	idx_t1_1,idx_t1_2	idx_t1_2	147	NULL	1000	NULL
+1	SIMPLE	t1	index	NULL	idx_t1_2	147	NULL	1000	
 explain select a1,a2,b,d from t1 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	idx_t1_1,idx_t1_2	idx_t1_2	147	NULL	1000	NULL
+1	SIMPLE	t1	index	NULL	idx_t1_2	147	NULL	1000	
 explain extended select a1,a2,min(b),max(b) from t1
 where (a1 = 'b' or a1 = 'd' or a1 = 'a' or a1 = 'c') and (a2 > 'a') and (c > 'a111') group by a1,a2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	index	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	1000	100.00	Using where; Using index
 Warnings:
-Note	1003	/* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,min(`test`.`t1`.`b`) AS `min(b)`,max(`test`.`t1`.`b`) AS `max(b)` from `test`.`t1` where (((`test`.`t1`.`a1` = 'b') or (`test`.`t1`.`a1` = 'd') or (`test`.`t1`.`a1` = 'a') or (`test`.`t1`.`a1` = 'c')) and (`test`.`t1`.`a2` > 'a') and (`test`.`t1`.`c` > 'a111')) group by `test`.`t1`.`a1`,`test`.`t1`.`a2`
+Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,min(`test`.`t1`.`b`) AS `min(b)`,max(`test`.`t1`.`b`) AS `max(b)` from `test`.`t1` where (`test`.`t1`.`a1` = 'b' or `test`.`t1`.`a1` = 'd' or `test`.`t1`.`a1` = 'a' or `test`.`t1`.`a1` = 'c') and `test`.`t1`.`a2` > 'a' and `test`.`t1`.`c` > 'a111' group by `test`.`t1`.`a1`,`test`.`t1`.`a2`
 explain extended select a1,a2,b,min(c),max(c) from t1
 where (a1 = 'b' or a1 = 'd' or a1 = 'a' or a1 = 'c') and (a2 > 'a') and (d > 'xy2') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	index	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_2	147	NULL	1000	100.00	Using where
 Warnings:
-Note	1003	/* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t1`.`b` AS `b`,min(`test`.`t1`.`c`) AS `min(c)`,max(`test`.`t1`.`c`) AS `max(c)` from `test`.`t1` where (((`test`.`t1`.`a1` = 'b') or (`test`.`t1`.`a1` = 'd') or (`test`.`t1`.`a1` = 'a') or (`test`.`t1`.`a1` = 'c')) and (`test`.`t1`.`a2` > 'a') and (`test`.`t1`.`d` > 'xy2')) group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`
+Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t1`.`b` AS `b`,min(`test`.`t1`.`c`) AS `min(c)`,max(`test`.`t1`.`c`) AS `max(c)` from `test`.`t1` where (`test`.`t1`.`a1` = 'b' or `test`.`t1`.`a1` = 'd' or `test`.`t1`.`a1` = 'a' or `test`.`t1`.`a1` = 'c') and `test`.`t1`.`a2` > 'a' and `test`.`t1`.`d` > 'xy2' group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`
 explain extended select a1,a2,b,c from t1
 where (a1 = 'b' or a1 = 'd' or a1 = 'a' or a1 = 'c') and (a2 > 'a') and (d > 'xy2') group by a1,a2,b,c;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	index	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	1000	100.00	Using where
 Warnings:
-Note	1003	/* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c` from `test`.`t1` where (((`test`.`t1`.`a1` = 'b') or (`test`.`t1`.`a1` = 'd') or (`test`.`t1`.`a1` = 'a') or (`test`.`t1`.`a1` = 'c')) and (`test`.`t1`.`a2` > 'a') and (`test`.`t1`.`d` > 'xy2')) group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`,`test`.`t1`.`c`
+Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c` from `test`.`t1` where (`test`.`t1`.`a1` = 'b' or `test`.`t1`.`a1` = 'd' or `test`.`t1`.`a1` = 'a' or `test`.`t1`.`a1` = 'c') and `test`.`t1`.`a2` > 'a' and `test`.`t1`.`d` > 'xy2' group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`,`test`.`t1`.`c`
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'b') or (b < 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_0	65	NULL	1000	Using where
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b < 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_0	65	NULL	1000	Using where
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b <= 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_0	65	NULL	1000	Using where
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b <= 'b' and b >= 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_0	65	NULL	1000	Using where
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain extended select a1,a2,b from t1 where (a1 = 'b' or a1 = 'd' or a1 = 'a' or a1 = 'c') and (a2 > 'a') and (c > 'a111') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	index	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	163	NULL	1000	100.00	Using where; Using index
 Warnings:
-Note	1003	/* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t1`.`b` AS `b` from `test`.`t1` where (((`test`.`t1`.`a1` = 'b') or (`test`.`t1`.`a1` = 'd') or (`test`.`t1`.`a1` = 'a') or (`test`.`t1`.`a1` = 'c')) and (`test`.`t1`.`a2` > 'a') and (`test`.`t1`.`c` > 'a111')) group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`
+Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t1`.`b` AS `b` from `test`.`t1` where (`test`.`t1`.`a1` = 'b' or `test`.`t1`.`a1` = 'd' or `test`.`t1`.`a1` = 'a' or `test`.`t1`.`a1` = 'c') and `test`.`t1`.`a2` > 'a' and `test`.`t1`.`c` > 'a111' group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`
 explain select a1,a2,min(b),c from t2 where (a2 = 'a') and (c = 'a111') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_0	65	NULL	1000	Using where
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 select a1,a2,min(b),c from t2 where (a2 = 'a') and (c = 'a111') group by a1;
 a1	a2	min(b)	c
 a	a	a	a111
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'b') or (b = 'a') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_0	65	NULL	1000	Using where
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,min(c),max(c) from t2
 where (c > 'a000') and (c <= 'd999') and (c like '_8__') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	1000	Using where; Using index
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1, a2, b, c, min(d), max(d) from t1 group by a1,a2,b,c;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	idx_t1_1	idx_t1_1	163	NULL	1000	NULL
+1	SIMPLE	t1	index	NULL	idx_t1_1	163	NULL	1000	
 explain select a1,a2,count(a2) from t1 group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	idx_t1_1,idx_t1_2	idx_t1_2	147	NULL	1000	Using index
+1	SIMPLE	t1	index	NULL	idx_t1_2	147	NULL	1000	Using index
 explain extended select a1,a2,count(a2) from t1 where (a1 > 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	index	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_2	147	NULL	1000	100.00	Using where; Using index
 Warnings:
-Note	1003	/* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,count(`test`.`t1`.`a2`) AS `count(a2)` from `test`.`t1` where (`test`.`t1`.`a1` > 'a') group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`
+Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,count(`test`.`t1`.`a2`) AS `count(a2)` from `test`.`t1` where `test`.`t1`.`a1` > 'a' group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`
 explain extended select sum(ord(a1)) from t1 where (a1 > 'a') group by a1,a2,b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	index	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_2	147	NULL	1000	100.00	Using where; Using index
 Warnings:
-Note	1003	/* select#1 */ select sum(ord(`test`.`t1`.`a1`)) AS `sum(ord(a1))` from `test`.`t1` where (`test`.`t1`.`a1` > 'a') group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`
-set optimizer_switch = 'multi_range_groupby=off';
+Note	1003	select sum(ord(`test`.`t1`.`a1`)) AS `sum(ord(a1))` from `test`.`t1` where `test`.`t1`.`a1` > 'a' group by `test`.`t1`.`a1`,`test`.`t1`.`a2`,`test`.`t1`.`b`
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'a' or b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_0	65	NULL	1000	Using where
-set optimizer_switch = 'default';
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select a1,a2,b,max(c),min(c) from t2 where (a2 = 'a') and (b = 'a' or b = 'b') group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	idx_t2_0,idx_t2_1,idx_t2_2	idx_t2_1	163	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t2	index	NULL	idx_t2_1	163	NULL	1000	Using where; Using index
 explain select distinct(a1) from t1 where ord(a2) = 98;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_0	65	NULL	1000	Using where
+1	SIMPLE	t1	index	NULL	idx_t1_2	147	NULL	1000	Using where; Using index
 select distinct(a1) from t1 where ord(a2) = 98;
 a1
 a
@@ -2266,7 +2282,7 @@ c
 d
 explain select a1 from t1 where a2 = 'b' group by a1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	130	NULL	63	Using where; Using index for group-by
 select a1 from t1 where a2 = 'b' group by a1;
 a1
 a
@@ -2275,7 +2291,7 @@ c
 d
 explain select distinct a1 from t1 where a2 = 'b';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx_t1_0,idx_t1_1,idx_t1_2	idx_t1_1	130	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	idx_t1_1	130	NULL	63	Using where; Using index for group-by
 select distinct a1 from t1 where a2 = 'b';
 a1
 a
@@ -2385,7 +2401,7 @@ INSERT INTO t1 (a) VALUES
 ('SOUTH EAST'), ('SOUTH WEST'), ('WESTERN');
 EXPLAIN SELECT DISTINCT a,a FROM t1 ORDER BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	PRIMARY	PRIMARY	66	NULL	1001	Using index for group-by
+1	SIMPLE	t1	index	NULL	PRIMARY	66	NULL	1000	Using index
 SELECT DISTINCT a,a FROM t1 ORDER BY a;
 a	a
 	
@@ -2477,7 +2493,7 @@ Table	Op	Msg_type	Msg_text
 test.t1	analyze	status	OK
 EXPLAIN SELECT max(b), a FROM t1 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	5	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	a	5	NULL	251	Using index for group-by
 FLUSH STATUS;
 SELECT max(b), a FROM t1 GROUP BY a;
 max(b)	a
@@ -2489,15 +2505,17 @@ SHOW STATUS LIKE 'handler_read__e%';
 Variable_name	Value
 Handler_read_key	8
 Handler_read_next	0
+Handler_read_retry	0
 EXPLAIN SELECT max(b), a FROM t1 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	5	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	a	5	NULL	251	Using index for group-by
 FLUSH STATUS;
 CREATE TABLE t2 engine=RocksDB SELECT max(b), a FROM t1 GROUP BY a;
 SHOW STATUS LIKE 'handler_read__e%';
 Variable_name	Value
 Handler_read_key	8
 Handler_read_next	0
+Handler_read_retry	0
 FLUSH STATUS;
 SELECT * FROM (SELECT max(b), a FROM t1 GROUP BY a) b;
 max(b)	a
@@ -2509,6 +2527,7 @@ SHOW STATUS LIKE 'handler_read__e%';
 Variable_name	Value
 Handler_read_key	8
 Handler_read_next	0
+Handler_read_retry	0
 FLUSH STATUS;
 (SELECT max(b), a FROM t1 GROUP BY a) UNION 
 (SELECT max(b), a FROM t1 GROUP BY a);
@@ -2521,50 +2540,52 @@ SHOW STATUS LIKE 'handler_read__e%';
 Variable_name	Value
 Handler_read_key	16
 Handler_read_next	0
+Handler_read_retry	0
 EXPLAIN (SELECT max(b), a FROM t1 GROUP BY a) UNION 
 (SELECT max(b), a FROM t1 GROUP BY a);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	range	a	a	5	NULL	501	Using index for group-by
-2	UNION	t1	range	a	a	5	NULL	501	Using index for group-by
-NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+1	PRIMARY	t1	range	NULL	a	5	NULL	251	Using index for group-by
+2	UNION	t1	range	NULL	a	5	NULL	251	Using index for group-by
+NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL	
 EXPLAIN SELECT (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2) x
 FROM t1 AS t1_outer;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1_outer	index	NULL	a	10	NULL	1000	Using index
-2	SUBQUERY	t1	range	a	a	5	NULL	501	Using index for group-by
+2	SUBQUERY	t1	range	NULL	a	5	NULL	251	Using index for group-by
 EXPLAIN SELECT 1 FROM t1 AS t1_outer WHERE EXISTS 
 (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1_outer	index	NULL	a	10	NULL	1000	Using index
-2	SUBQUERY	t1	range	a	a	5	NULL	501	Using index for group-by
+2	SUBQUERY	t1	index	NULL	a	10	NULL	1000	Using index
 EXPLAIN SELECT 1 FROM t1 AS t1_outer WHERE 
 (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2) > 12;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
-2	SUBQUERY	t1	range	a	a	5	NULL	501	Using index for group-by
+1	PRIMARY	t1_outer	index	NULL	a	10	NULL	1000	Using index
+2	SUBQUERY	t1	range	NULL	a	5	NULL	251	Using index for group-by
 EXPLAIN SELECT 1 FROM t1 AS t1_outer WHERE 
 a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1_outer	index	NULL	a	10	NULL	1000	Using where; Using index
-2	SUBQUERY	t1	range	a	a	5	NULL	501	Using index for group-by
+1	PRIMARY	<subquery2>	ALL	distinct_key	NULL	NULL	NULL	251	
+1	PRIMARY	t1_outer	ref	a	a	5	<subquery2>.max(b)	4	Using index
+2	MATERIALIZED	t1	range	NULL	a	5	NULL	251	Using index for group-by
 EXPLAIN SELECT 1 FROM t1 AS t1_outer GROUP BY a HAVING 
 a > (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1_outer	range	a	a	5	NULL	501	Using index for group-by
-2	SUBQUERY	t1	range	a	a	5	NULL	501	Using index for group-by
+1	PRIMARY	t1_outer	range	NULL	a	5	NULL	251	Using index for group-by
+2	SUBQUERY	t1	range	NULL	a	5	NULL	251	Using index for group-by
 EXPLAIN SELECT 1 FROM t1 AS t1_outer1 JOIN t1 AS t1_outer2 
 ON t1_outer1.a = (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2) 
 AND t1_outer1.b = t1_outer2.b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1_outer2	index	NULL	a	10	NULL	1000	Using where; Using index
-1	PRIMARY	t1_outer1	ref	a	a	10	const,test.t1_outer2.b	1	Using where; Using index
-2	SUBQUERY	t1	range	a	a	5	NULL	501	Using index for group-by
+1	PRIMARY	t1_outer1	ref	a	a	5	const	4	Using where; Using index
+1	PRIMARY	t1_outer2	index	NULL	a	10	NULL	1000	Using where; Using index; Using join buffer (flat, BNL join)
+2	SUBQUERY	t1	range	NULL	a	5	NULL	251	Using index for group-by
 EXPLAIN SELECT (SELECT (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2) x
 FROM t1 AS t1_outer) x2 FROM t1 AS t1_outer2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1_outer2	index	NULL	a	10	NULL	1000	Using index
 2	SUBQUERY	t1_outer	index	NULL	a	10	NULL	1000	Using index
-3	SUBQUERY	t1	range	a	a	5	NULL	501	Using index for group-by
+3	SUBQUERY	t1	range	NULL	a	5	NULL	251	Using index for group-by
 CREATE TABLE t3 LIKE t1;
 FLUSH STATUS;
 INSERT INTO t3 SELECT a,MAX(b) FROM t1 GROUP BY a;
@@ -2572,6 +2593,7 @@ SHOW STATUS LIKE 'handler_read__e%';
 Variable_name	Value
 Handler_read_key	8
 Handler_read_next	0
+Handler_read_retry	0
 DELETE FROM t3;
 FLUSH STATUS;
 INSERT INTO t3 SELECT 1, (SELECT MAX(b) FROM t1 GROUP BY a HAVING a < 2) 
@@ -2580,12 +2602,14 @@ SHOW STATUS LIKE 'handler_read__e%';
 Variable_name	Value
 Handler_read_key	8
 Handler_read_next	0
+Handler_read_retry	0
 FLUSH STATUS;
 DELETE FROM t3 WHERE (SELECT MAX(b) FROM t1 GROUP BY a HAVING a < 2) > 10000;
 SHOW STATUS LIKE 'handler_read__e%';
 Variable_name	Value
 Handler_read_key	8
 Handler_read_next	0
+Handler_read_retry	0
 FLUSH STATUS;
 DELETE FROM t3 WHERE (SELECT (SELECT MAX(b) FROM t1 GROUP BY a HAVING a < 2) x 
 FROM t1) > 10000;
@@ -2594,6 +2618,7 @@ SHOW STATUS LIKE 'handler_read__e%';
 Variable_name	Value
 Handler_read_key	8
 Handler_read_next	1
+Handler_read_retry	0
 DROP TABLE t1,t2,t3;
 CREATE TABLE t1 (a int, INDEX idx(a)) engine=RocksDB;
 INSERT INTO t1 VALUES
@@ -2604,7 +2629,7 @@ Table	Op	Msg_type	Msg_text
 test.t1	analyze	status	OK
 EXPLAIN SELECT DISTINCT(a) FROM t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx	idx	5	NULL	1001	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx	5	NULL	501	Using index for group-by
 SELECT DISTINCT(a) FROM t1;
 a
 1
@@ -2612,7 +2637,7 @@ a
 4
 EXPLAIN SELECT SQL_BIG_RESULT DISTINCT(a) FROM t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	idx	idx	5	NULL	1001	Using index for group-by
+1	SIMPLE	t1	range	NULL	idx	5	NULL	501	Using index for group-by
 SELECT SQL_BIG_RESULT DISTINCT(a) FROM t1;
 a
 1
@@ -2640,7 +2665,7 @@ CREATE INDEX break_it ON t1 (a, b);
 EXPLAIN
 SELECT a, MIN(b), MAX(b) FROM t1 GROUP BY a ORDER BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	break_it	break_it	10	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	break_it	10	NULL	251	Using index for group-by
 SELECT a, MIN(b), MAX(b) FROM t1 GROUP BY a ORDER BY a;
 a	MIN(b)	MAX(b)
 1	1	3
@@ -2650,7 +2675,7 @@ a	MIN(b)	MAX(b)
 EXPLAIN
 SELECT a, MIN(b), MAX(b) FROM t1 GROUP BY a ORDER BY a DESC;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	break_it	break_it	10	NULL	501	Using index for group-by; Using temporary; Using filesort
+1	SIMPLE	t1	range	NULL	break_it	10	NULL	251	Using index for group-by; Using temporary; Using filesort
 SELECT a, MIN(b), MAX(b) FROM t1 GROUP BY a ORDER BY a DESC;
 a	MIN(b)	MAX(b)
 4	1	3
@@ -2660,7 +2685,7 @@ a	MIN(b)	MAX(b)
 EXPLAIN
 SELECT a, MIN(b), MAX(b), AVG(b) FROM t1 GROUP BY a ORDER BY a DESC;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	break_it	break_it	10	NULL	1000	Using index
+1	SIMPLE	t1	index	NULL	break_it	10	NULL	1000	Using index
 SELECT a, MIN(b), MAX(b), AVG(b) FROM t1 GROUP BY a ORDER BY a DESC;
 a	MIN(b)	MAX(b)	AVG(b)
 4	1	3	2.0000
@@ -2743,9 +2768,9 @@ a	b
 3	13
 explain extended select sql_buffer_result a, max(b)+1 from t1 where a = 0 group by a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	SIMPLE	t1	range	PRIMARY,index	PRIMARY	4	NULL	1	100.00	Using where; Using index for group-by; Using temporary
+1	SIMPLE	t1	ref	PRIMARY,index	PRIMARY	4	const	15	100.00	Using index; Using temporary
 Warnings:
-Note	1003	/* select#1 */ select sql_buffer_result `test`.`t1`.`a` AS `a`,(max(`test`.`t1`.`b`) + 1) AS `max(b)+1` from `test`.`t1` where (`test`.`t1`.`a` = 0) group by `test`.`t1`.`a`
+Note	1003	select sql_buffer_result `test`.`t1`.`a` AS `a`,max(`test`.`t1`.`b`) + 1 AS `max(b)+1` from `test`.`t1` where `test`.`t1`.`a` = 0 group by `test`.`t1`.`a`
 drop table t1;
 CREATE TABLE t1 (a int, b int, c int, d int,
 KEY foo (c,d,a,b), KEY bar (c,a,b,d)) engine=RocksDB;
@@ -2758,7 +2783,7 @@ Table	Op	Msg_type	Msg_text
 test.t1	analyze	status	OK
 EXPLAIN SELECT DISTINCT c FROM t1 WHERE d=4;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	foo,bar	foo	10	NULL	126	Using where; Using index for group-by
+1	SIMPLE	t1	range	NULL	foo	10	NULL	63	Using where; Using index for group-by
 SELECT DISTINCT c FROM t1 WHERE d=4;
 c
 1
@@ -2780,7 +2805,7 @@ test.t	analyze	status	OK
 EXPLAIN
 SELECT a, MIN(b) FROM t WHERE b <> 0 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t	range	a	a	10	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t	range	NULL	a	10	NULL	251	Using where; Using index for group-by
 #should return 1 row
 SELECT a, MIN(b) FROM t WHERE b <> 0 GROUP BY a;
 a	MIN(b)
@@ -2790,7 +2815,7 @@ a	MIN(b)
 EXPLAIN
 SELECT a, MAX(b) FROM t WHERE b <> 1 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t	range	a	a	10	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t	range	NULL	a	10	NULL	251	Using where; Using index for group-by
 #should return 1 row
 SELECT a, MAX(b) FROM t WHERE b <> 1 GROUP BY a;
 a	MAX(b)
@@ -2801,7 +2826,7 @@ INSERT INTO t SELECT a, 2 FROM t;
 EXPLAIN
 SELECT a, MAX(b) FROM t WHERE b > 0 AND b < 2 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t	range	a	a	10	NULL	501	Using where; Using index for group-by
+1	SIMPLE	t	range	NULL	a	10	NULL	251	Using where; Using index for group-by
 #should return 1 row
 SELECT a, MAX(b) FROM t WHERE b > 0 AND b < 2 GROUP BY a;
 a	MAX(b)
@@ -2915,7 +2940,7 @@ NULL
 EXPLAIN
 SELECT MIN( a ) FROM t1 WHERE a = (SELECT a FROM t1 WHERE a < 0);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-x	x	x	x	x	x	x	x	x	Impossible WHERE noticed after reading const tables
+x	x	x	x	x	x	x	x	x	Using where; Using index
 x	x	x	x	x	x	x	x	x	Using where; Using index
 SELECT MIN( a ) FROM t1 WHERE a = (SELECT a FROM t1 WHERE a < 0);
 MIN( a )
@@ -2990,7 +3015,7 @@ NULL
 EXPLAIN
 SELECT MIN( a ) FROM t1 WHERE a = (SELECT a FROM t1 WHERE a < 0);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-x	x	x	x	x	x	x	x	x	Impossible WHERE noticed after reading const tables
+x	x	x	x	x	x	x	x	x	Using where; Using index
 x	x	x	x	x	x	x	x	x	Using where; Using index
 SELECT MIN( a ) FROM t1 WHERE a = (SELECT a FROM t1 WHERE a < 0);
 MIN( a )
@@ -3072,7 +3097,7 @@ NULL
 EXPLAIN
 SELECT MIN( a ) FROM t1 WHERE a = (SELECT a FROM t1 WHERE a < 0);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-x	x	x	x	x	x	x	x	x	Impossible WHERE noticed after reading const tables
+x	x	x	x	x	x	x	x	x	Using where; Using index
 x	x	x	x	x	x	x	x	x	Using where; Using index
 SELECT MIN( a ) FROM t1 WHERE a = (SELECT a FROM t1 WHERE a < 0);
 MIN( a )
@@ -3119,45 +3144,45 @@ Table	Op	Msg_type	Msg_text
 test.t2	analyze	status	OK
 EXPLAIN SELECT COUNT(DISTINCT a) FROM t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	5	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	a	5	NULL	251	Using index for group-by
 SELECT COUNT(DISTINCT a) FROM t1;
 COUNT(DISTINCT a)
 2
 EXPLAIN SELECT COUNT(DISTINCT a,b) FROM t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	10	NULL	1001	Using index for group-by (scanning)
+1	SIMPLE	t1	range	NULL	a	10	NULL	501	Using index for group-by
 SELECT COUNT(DISTINCT a,b) FROM t1;
 COUNT(DISTINCT a,b)
 16
 EXPLAIN SELECT COUNT(DISTINCT b,a) FROM t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	10	NULL	1001	Using index for group-by (scanning)
+1	SIMPLE	t1	range	NULL	a	10	NULL	501	Using index for group-by
 SELECT COUNT(DISTINCT b,a) FROM t1;
 COUNT(DISTINCT b,a)
 16
 EXPLAIN SELECT COUNT(DISTINCT b) FROM t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	a	a	10	NULL	1000	Using index
+1	SIMPLE	t1	index	NULL	a	10	NULL	1000	Using index
 SELECT COUNT(DISTINCT b) FROM t1;
 COUNT(DISTINCT b)
 8
 EXPLAIN SELECT COUNT(DISTINCT a) FROM t1 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	5	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	a	5	NULL	251	Using index for group-by
 SELECT COUNT(DISTINCT a) FROM t1 GROUP BY a;
 COUNT(DISTINCT a)
 1
 1
 EXPLAIN SELECT COUNT(DISTINCT b) FROM t1 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	10	NULL	1001	Using index for group-by (scanning)
+1	SIMPLE	t1	range	NULL	a	10	NULL	501	Using index for group-by
 SELECT COUNT(DISTINCT b) FROM t1 GROUP BY a;
 COUNT(DISTINCT b)
 8
 8
 EXPLAIN SELECT COUNT(DISTINCT a) FROM t1 GROUP BY b;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	a	a	10	NULL	1000	Using index; Using filesort
+1	SIMPLE	t1	index	NULL	a	10	NULL	1000	Using index; Using filesort
 SELECT COUNT(DISTINCT a) FROM t1 GROUP BY b;
 COUNT(DISTINCT a)
 2
@@ -3170,7 +3195,7 @@ COUNT(DISTINCT a)
 2
 EXPLAIN SELECT DISTINCT COUNT(DISTINCT a) FROM t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	a	a	10	NULL	1000	Using index
+1	SIMPLE	t1	index	NULL	a	10	NULL	1000	Using index
 SELECT DISTINCT COUNT(DISTINCT a) FROM t1;
 COUNT(DISTINCT a)
 2
@@ -3188,76 +3213,76 @@ COUNT(DISTINCT a)
 2
 EXPLAIN SELECT COUNT(DISTINCT a) FROM t1 HAVING COUNT(DISTINCT c) < 10;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	1000	NULL
+1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	1000	
 SELECT COUNT(DISTINCT a) FROM t1 HAVING COUNT(DISTINCT c) < 10;
 COUNT(DISTINCT a)
 2
 EXPLAIN SELECT 1 FROM t1 HAVING COUNT(DISTINCT a) < 10;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	5	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	a	5	NULL	251	Using index for group-by
 SELECT 1 FROM t1 HAVING COUNT(DISTINCT a) < 10;
 1
 1
 EXPLAIN SELECT 1 FROM t1 GROUP BY a HAVING COUNT(DISTINCT b) > 1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	10	NULL	1001	Using index for group-by (scanning)
+1	SIMPLE	t1	range	NULL	a	10	NULL	501	Using index for group-by
 SELECT 1 FROM t1 GROUP BY a HAVING COUNT(DISTINCT b) > 1;
 1
 1
 1
 EXPLAIN SELECT COUNT(DISTINCT t1_1.a) FROM t1 t1_1, t1 t1_2 GROUP BY t1_1.a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1_1	index	a	a	10	NULL	1000	Using index; Using temporary; Using filesort
-1	SIMPLE	t1_2	index	NULL	a	10	NULL	1000	Using index; Using join buffer (Block Nested Loop)
+1	SIMPLE	t1_1	index	NULL	a	10	NULL	1000	Using index; Using temporary; Using filesort
+1	SIMPLE	t1_2	index	NULL	a	10	NULL	1000	Using index; Using join buffer (flat, BNL join)
 SELECT COUNT(DISTINCT t1_1.a) FROM t1 t1_1, t1 t1_2 GROUP BY t1_1.a;
 COUNT(DISTINCT t1_1.a)
 1
 1
 EXPLAIN SELECT COUNT(DISTINCT a), 12 FROM t1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	5	NULL	501	Using index for group-by
+1	SIMPLE	t1	range	NULL	a	5	NULL	251	Using index for group-by
 SELECT COUNT(DISTINCT a), 12 FROM t1;
 COUNT(DISTINCT a)	12
 2	12
 EXPLAIN SELECT COUNT(DISTINCT a, b, c) FROM t2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	15	NULL	1001	Using index for group-by (scanning)
+1	SIMPLE	t2	range	NULL	a	15	NULL	501	Using index for group-by
 SELECT COUNT(DISTINCT a, b, c) FROM t2;
 COUNT(DISTINCT a, b, c)
 16
 EXPLAIN SELECT COUNT(DISTINCT a), SUM(DISTINCT a), AVG(DISTINCT a) FROM t2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	5	NULL	251	Using index for group-by
+1	SIMPLE	t2	range	NULL	a	5	NULL	126	Using index for group-by
 SELECT COUNT(DISTINCT a), SUM(DISTINCT a), AVG(DISTINCT a) FROM t2;
 COUNT(DISTINCT a)	SUM(DISTINCT a)	AVG(DISTINCT a)
 2	3	1.5000
 EXPLAIN SELECT COUNT(DISTINCT a), SUM(DISTINCT a), AVG(DISTINCT f) FROM t2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	1000	NULL
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	1000	
 SELECT COUNT(DISTINCT a), SUM(DISTINCT a), AVG(DISTINCT f) FROM t2;
 COUNT(DISTINCT a)	SUM(DISTINCT a)	AVG(DISTINCT f)
 2	3	1.0000
 EXPLAIN SELECT COUNT(DISTINCT a, b), COUNT(DISTINCT b, a) FROM t2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	10	NULL	501	Using index for group-by (scanning)
+1	SIMPLE	t2	range	NULL	a	10	NULL	251	Using index for group-by
 SELECT COUNT(DISTINCT a, b), COUNT(DISTINCT b, a) FROM t2;
 COUNT(DISTINCT a, b)	COUNT(DISTINCT b, a)
 16	16
 EXPLAIN SELECT COUNT(DISTINCT a, b), COUNT(DISTINCT b, f) FROM t2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	1000	NULL
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	1000	
 SELECT COUNT(DISTINCT a, b), COUNT(DISTINCT b, f) FROM t2;
 COUNT(DISTINCT a, b)	COUNT(DISTINCT b, f)
 16	8
 EXPLAIN SELECT COUNT(DISTINCT a, b), COUNT(DISTINCT b, d) FROM t2;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	1000	NULL
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	1000	
 SELECT COUNT(DISTINCT a, b), COUNT(DISTINCT b, d) FROM t2;
 COUNT(DISTINCT a, b)	COUNT(DISTINCT b, d)
 16	8
 EXPLAIN SELECT a, c, COUNT(DISTINCT c, a, b) FROM t2 GROUP BY a, b, c;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	15	NULL	1001	Using index for group-by (scanning)
+1	SIMPLE	t2	range	NULL	a	15	NULL	501	Using index for group-by
 SELECT a, c, COUNT(DISTINCT c, a, b) FROM t2 GROUP BY a, b, c;
 a	c	COUNT(DISTINCT c, a, b)
 1	1	1
@@ -3279,7 +3304,7 @@ a	c	COUNT(DISTINCT c, a, b)
 EXPLAIN SELECT COUNT(DISTINCT c, a, b) FROM t2
 WHERE a > 5 AND b BETWEEN 10 AND 20 GROUP BY a, b, c;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	15	NULL	1001	Using where; Using index for group-by (scanning)
+1	SIMPLE	t2	range	a	a	15	NULL	501	Using where; Using index for group-by
 SELECT COUNT(DISTINCT c, a, b) FROM t2
 WHERE a > 5 AND b BETWEEN 10 AND 20 GROUP BY a, b, c;
 COUNT(DISTINCT c, a, b)
@@ -3292,28 +3317,28 @@ GROUP BY b;
 COUNT(DISTINCT b)	SUM(DISTINCT b)
 EXPLAIN SELECT a, COUNT(DISTINCT b), SUM(DISTINCT b) FROM t2 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	10	NULL	501	Using index for group-by (scanning)
+1	SIMPLE	t2	range	NULL	a	10	NULL	251	Using index for group-by
 SELECT a, COUNT(DISTINCT b), SUM(DISTINCT b) FROM t2 GROUP BY a;
 a	COUNT(DISTINCT b)	SUM(DISTINCT b)
 1	8	36
 2	8	36
 EXPLAIN SELECT COUNT(DISTINCT b), SUM(DISTINCT b) FROM t2 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	10	NULL	501	Using index for group-by (scanning)
+1	SIMPLE	t2	range	NULL	a	10	NULL	251	Using index for group-by
 SELECT COUNT(DISTINCT b), SUM(DISTINCT b) FROM t2 GROUP BY a;
 COUNT(DISTINCT b)	SUM(DISTINCT b)
 8	36
 8	36
 EXPLAIN SELECT COUNT(DISTINCT a, b) FROM t2 WHERE c = 13 AND d = 42;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	ALL	a	NULL	NULL	NULL	1000	Using where
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	1000	Using where
 SELECT COUNT(DISTINCT a, b) FROM t2 WHERE c = 13 AND d = 42;
 COUNT(DISTINCT a, b)
 0
 EXPLAIN SELECT a, COUNT(DISTINCT a), SUM(DISTINCT a) FROM t2
 WHERE b = 13 AND c = 42 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	15	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t2	range	NULL	a	15	NULL	126	Using where; Using index for group-by
 SELECT a, COUNT(DISTINCT a), SUM(DISTINCT a) FROM t2
 WHERE b = 13 AND c = 42 GROUP BY a;
 a	COUNT(DISTINCT a)	SUM(DISTINCT a)
@@ -3327,14 +3352,14 @@ COUNT(DISTINCT a, b)	SUM(DISTINCT a)
 0	NULL
 EXPLAIN SELECT SUM(DISTINCT a), MAX(b) FROM t2 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	a	a	15	NULL	1000	Using index
+1	SIMPLE	t2	index	NULL	a	15	NULL	1000	Using index
 SELECT SUM(DISTINCT a), MAX(b) FROM t2 GROUP BY a;
 SUM(DISTINCT a)	MAX(b)
 1	8
 2	8
 EXPLAIN SELECT 42 * (a + c + COUNT(DISTINCT c, a, b)) FROM t2 GROUP BY a, b, c;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	range	a	a	15	NULL	1001	Using index for group-by (scanning)
+1	SIMPLE	t2	range	NULL	a	15	NULL	501	Using index for group-by
 SELECT 42 * (a + c + COUNT(DISTINCT c, a, b)) FROM t2 GROUP BY a, b, c;
 42 * (a + c + COUNT(DISTINCT c, a, b))
 126
@@ -3355,7 +3380,7 @@ SELECT 42 * (a + c + COUNT(DISTINCT c, a, b)) FROM t2 GROUP BY a, b, c;
 168
 EXPLAIN SELECT (SUM(DISTINCT a) + MAX(b)) FROM t2 GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t2	index	a	a	15	NULL	1000	Using index
+1	SIMPLE	t2	index	NULL	a	15	NULL	1000	Using index
 SELECT (SUM(DISTINCT a) + MAX(b)) FROM t2 GROUP BY a;
 (SUM(DISTINCT a) + MAX(b))
 9
@@ -3383,7 +3408,7 @@ f1	COUNT(DISTINCT f2)
 3	4
 explain SELECT f1, COUNT(DISTINCT f2) FROM t1 GROUP BY f1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	PRIMARY	PRIMARY	5	NULL	1000	Using index
+1	SIMPLE	t1	index	NULL	PRIMARY	5	NULL	1000	Using index
 drop table t1;
 # End of test#50539.
 #
@@ -3395,9 +3420,6 @@ INSERT INTO t VALUES (1,1), (2,2), (3,3), (4,4), (1,0), (3,2), (4,5);
 ANALYZE TABLE t;
 Table	Op	Msg_type	Msg_text
 test.t	analyze	status	OK
-set optimizer_trace_max_mem_size=1048576;
-set @@session.optimizer_trace='enabled=on';
-set end_markers_in_json=on;
 ANALYZE TABLE t;
 Table	Op	Msg_type	Msg_text
 test.t	analyze	status	OK
@@ -3409,11 +3431,7 @@ a	SUM(DISTINCT a)	MIN(b)
 4	4	4
 EXPLAIN SELECT a, SUM(DISTINCT a), MIN(b) FROM t GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t	index	a	a	10	NULL	1000	Using index
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
-OK
-1
+1	SIMPLE	t	index	NULL	a	10	NULL	1000	Using index
 SELECT a, SUM(DISTINCT a), MAX(b) FROM t GROUP BY a;
 a	SUM(DISTINCT a)	MAX(b)
 1	1	1
@@ -3422,11 +3440,7 @@ a	SUM(DISTINCT a)	MAX(b)
 4	4	5
 EXPLAIN SELECT a, SUM(DISTINCT a), MAX(b) FROM t GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t	index	a	a	10	NULL	1000	Using index
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
-OK
-1
+1	SIMPLE	t	index	NULL	a	10	NULL	1000	Using index
 SELECT a, MAX(b) FROM t GROUP BY a HAVING SUM(DISTINCT a);
 a	MAX(b)
 1	1
@@ -3435,21 +3449,13 @@ a	MAX(b)
 4	5
 EXPLAIN SELECT a, MAX(b) FROM t GROUP BY a HAVING SUM(DISTINCT a);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t	index	a	a	10	NULL	1000	Using index
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
-OK
-1
+1	SIMPLE	t	index	NULL	a	10	NULL	1000	Using index
 SELECT SUM(DISTINCT a), MIN(b), MAX(b) FROM t;
 SUM(DISTINCT a)	MIN(b)	MAX(b)
 10	0	5
 EXPLAIN SELECT SUM(DISTINCT a), MIN(b), MAX(b) FROM t;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t	index	a	a	10	NULL	1000	Using index
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
-OK
-1
+1	SIMPLE	t	index	NULL	a	10	NULL	1000	Using index
 SELECT a, SUM(DISTINCT a), MIN(b), MAX(b) FROM t GROUP BY a;
 a	SUM(DISTINCT a)	MIN(b)	MAX(b)
 1	1	0	1
@@ -3458,14 +3464,7 @@ a	SUM(DISTINCT a)	MIN(b)	MAX(b)
 4	4	4	5
 EXPLAIN SELECT a, SUM(DISTINCT a), MIN(b), MAX(b) FROM t GROUP BY a;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t	index	a	a	10	NULL	1000	Using index
-SELECT TRACE RLIKE 'have_both_agg_distinct_and_min_max' AS OK
-FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
-OK
-1
-SET optimizer_trace_max_mem_size=DEFAULT;
-SET optimizer_trace=DEFAULT;
-SET end_markers_in_json=DEFAULT;
+1	SIMPLE	t	index	NULL	a	10	NULL	1000	Using index
 DROP TABLE t;
 #
 # Bug#18109609: LOOSE INDEX SCAN IS NOT USED WHEN IT SHOULD
@@ -3484,7 +3483,7 @@ Table	Op	Msg_type	Msg_text
 test.t1	analyze	status	OK
 EXPLAIN SELECT MAX(c2), c1 FROM t1 WHERE c1 = 4 GROUP BY c1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	c1	c1	5	NULL	251	Using where; Using index for group-by
+1	SIMPLE	t1	ref	c1	c1	5	const	1000	Using index
 FLUSH STATUS;
 SELECT MAX(c2), c1 FROM t1 WHERE c1 = 4 GROUP BY c1;
 MAX(c2)	c1
@@ -3492,12 +3491,14 @@ MAX(c2)	c1
 SHOW SESSION STATUS LIKE 'Handler_read%';
 Variable_name	Value
 Handler_read_first	0
-Handler_read_key	3
-Handler_read_last	1
-Handler_read_next	0
+Handler_read_key	1
+Handler_read_last	0
+Handler_read_next	20
 Handler_read_prev	0
+Handler_read_retry	0
 Handler_read_rnd	0
+Handler_read_rnd_deleted	0
 Handler_read_rnd_next	0
 DROP TABLE t1;
 # End of test for Bug#18109609
-set global debug="-d,force_group_by";
+set global debug_dbug=@debug_tmp;
diff --git a/storage/rocksdb/mysql-test/rocksdb/r/kill.result b/storage/rocksdb/mysql-test/rocksdb/r/kill.result
new file mode 100644
index 00000000000..19614e1fd95
--- /dev/null
+++ b/storage/rocksdb/mysql-test/rocksdb/r/kill.result
@@ -0,0 +1,6 @@
+CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=RocksDB;
+LOCK TABLE t1 WRITE;
+SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+FLUSH TABLES;
+ERROR HY000: MyRocks supports only READ COMMITTED and REPEATABLE READ isolation levels. Please change from current isolation level SERIALIZABLE
+DROP TABLE t1;
diff --git a/storage/rocksdb/mysql-test/rocksdb/t/group_min_max.test b/storage/rocksdb/mysql-test/rocksdb/t/group_min_max.test
index a9c44a71edd..eb66bd0e972 100644
--- a/storage/rocksdb/mysql-test/rocksdb/t/group_min_max.test
+++ b/storage/rocksdb/mysql-test/rocksdb/t/group_min_max.test
@@ -1,8 +1,9 @@
 --source include/have_debug.inc
-set global debug="+d,force_group_by";
+set @debug_tmp= @@debug_dbug;
+set global debug_dbug="+d,force_group_by";
 
 let $engine=RocksDB;
 --source include/group_min_max.inc
 
-set global debug="-d,force_group_by";
+set global debug_dbug=@debug_tmp;
 
diff --git a/storage/rocksdb/mysql-test/rocksdb/t/kill.test b/storage/rocksdb/mysql-test/rocksdb/t/kill.test
new file mode 100644
index 00000000000..a2809d969d5
--- /dev/null
+++ b/storage/rocksdb/mysql-test/rocksdb/t/kill.test
@@ -0,0 +1,9 @@
+--source include/have_binlog_format_row.inc
+--source include/have_rocksdb.inc
+
+CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=RocksDB;
+LOCK TABLE t1 WRITE;
+SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+--error ER_ISOLATION_MODE_NOT_SUPPORTED
+FLUSH TABLES;
+DROP TABLE t1;
diff --git a/storage/rocksdb/mysql-test/rocksdb/t/rocksdb_concurrent_delete.inc b/storage/rocksdb/mysql-test/rocksdb/t/rocksdb_concurrent_delete.inc
index 55f466a4d31..5336c77ee83 100644
--- a/storage/rocksdb/mysql-test/rocksdb/t/rocksdb_concurrent_delete.inc
+++ b/storage/rocksdb/mysql-test/rocksdb/t/rocksdb_concurrent_delete.inc
@@ -1,4 +1,7 @@
---source include/have_rocksdb.inc
+# MariaDB: including the below too many times causes really long argv list
+# in win_main()'s argument which blows up some limit on Windows.
+# Comment it out:
+#--source include/have_rocksdb.inc
 --source include/have_debug_sync.inc
 
 --source include/count_sessions.inc
diff --git a/unittest/mysys/my_atomic-t.c b/unittest/mysys/my_atomic-t.c
index 4796cc424d8..52bced7ce3b 100644
--- a/unittest/mysys/my_atomic-t.c
+++ b/unittest/mysys/my_atomic-t.c
@@ -90,10 +90,10 @@ pthread_handler_t test_atomic_cas(void *arg)
     y= my_atomic_load32(&bad);
     x= (x*m+0x87654321) & INT_MAX32;
     do {
-      ok= my_atomic_cas32(&bad, &y, (uint32)y+x);
+      ok= my_atomic_cas32((int32*) &bad, &y, y+x);
     } while (!ok) ;
     do {
-      ok= my_atomic_cas32(&bad, &y, y-x);
+      ok= my_atomic_cas32((int32*) &bad, &y, y-x);
     } while (!ok) ;
   }
   return 0;