From 2e7891080667c59ac80f788eef4d59d447595772 Mon Sep 17 00:00:00 2001
From: Igor Babaev <igor@askmonty.org>
Date: Wed, 2 Jun 2021 08:40:30 -0700
Subject: [PATCH 01/12] MDEV-25635 Assertion failure when pushing from HAVING
 into WHERE of view

This bug could manifest itself after pushing a where condition over a
mergeable derived table / view / CTE DT into a grouping view / derived
table / CTE V whose item list contained set functions with constant
arguments such as MIN(2), SUM(1) etc. In such cases the field references
used in the condition pushed into the view V that correspond set functions
are wrapped into Item_direct_view_ref wrappers. Due to a wrong implementation
of the virtual method const_item() for the class Item_direct_view_ref the
wrapped set functions with constant arguments could be erroneously taken
for constant items. This could lead to a wrong result set returned by the
main select query in 10.2. In 10.4 where a possibility of pushing condition
from HAVING into WHERE had been added this could cause a crash.

Approved by Sergey Petrunya <sergey.petrunya@mariadb.com>
---
 mysql-test/r/derived_cond_pushdown.result | 39 +++++++++++++++++++++++
 mysql-test/t/derived_cond_pushdown.test   | 25 +++++++++++++++
 sql/item.h                                |  5 ++-
 3 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/mysql-test/r/derived_cond_pushdown.result b/mysql-test/r/derived_cond_pushdown.result
index 25237aa11a9..28532ae88a4 100644
--- a/mysql-test/r/derived_cond_pushdown.result
+++ b/mysql-test/r/derived_cond_pushdown.result
@@ -10634,4 +10634,43 @@ m
 7
 drop view v1;
 drop table t1;
+#
+# MDEV-25635: pushdown into grouping view using aggregate functions
+#             with constant arguments via a mergeable derived table
+#
+create table t1 (a int);
+insert into t1 values (3), (7), (1), (3), (7), (7), (3);
+create view v1 as select a, sum(1) as f, sum(1) as g from t1 group by a;
+select * from v1;
+a	f	g
+1	1	1
+3	3	3
+7	3	3
+select * from (select * from v1) as dt where a=f and a=g;
+a	f	g
+1	1	1
+3	3	3
+explain extended select * from (select * from v1) as dt where a=f and a=g;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
+3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using temporary; Using filesort
+Warnings:
+Note	1003	select `v1`.`a` AS `a`,`v1`.`f` AS `f`,`v1`.`g` AS `g` from `test`.`v1` where `v1`.`a` = `v1`.`f` and `v1`.`a` = `v1`.`g`
+create view v2 as select a, min(1) as f, min(1) as g from t1 group by a;
+select * from v2;
+a	f	g
+1	1	1
+3	1	1
+7	1	1
+select * from (select * from v2) as dt where a=f and a=g;
+a	f	g
+1	1	1
+explain extended select * from (select * from v2) as dt where a=f and a=g;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
+3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using temporary; Using filesort
+Warnings:
+Note	1003	select `v2`.`a` AS `a`,`v2`.`f` AS `f`,`v2`.`g` AS `g` from `test`.`v2` where `v2`.`f` = `v2`.`a` and `v2`.`g` = `v2`.`a`
+drop view v1,v2;
+drop table t1;
 # End of 10.2 tests
diff --git a/mysql-test/t/derived_cond_pushdown.test b/mysql-test/t/derived_cond_pushdown.test
index 31b49047bf1..58f38ac1e5a 100644
--- a/mysql-test/t/derived_cond_pushdown.test
+++ b/mysql-test/t/derived_cond_pushdown.test
@@ -2212,4 +2212,29 @@ select * from v1 where m > 0;
 drop view v1;
 drop table t1;
 
+--echo #
+--echo # MDEV-25635: pushdown into grouping view using aggregate functions
+--echo #             with constant arguments via a mergeable derived table
+--echo #
+
+create table t1 (a int);
+insert into t1 values (3), (7), (1), (3), (7), (7), (3);
+
+create view v1 as select a, sum(1) as f, sum(1) as g from t1 group by a;
+select * from v1;
+let $q1=
+select * from (select * from v1) as dt where a=f and a=g;
+eval $q1;
+eval explain extended $q1;
+
+create view v2 as select a, min(1) as f, min(1) as g from t1 group by a;
+select * from v2;
+let $q2=
+select * from (select * from v2) as dt where a=f and a=g;
+eval $q2;
+eval explain extended $q2;
+
+drop view v1,v2;
+drop table t1;
+
 --echo # End of 10.2 tests
diff --git a/sql/item.h b/sql/item.h
index c94709c733e..76be66d2a7c 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -4952,7 +4952,10 @@ public:
   table_map used_tables() const;
   void update_used_tables();
   table_map not_null_tables() const;
-  bool const_item() const { return used_tables() == 0; }
+  bool const_item() const
+  {
+    return (*ref)->const_item() && (null_ref_table == NO_NULL_TABLE);
+  }
   TABLE *get_null_ref_table() const { return null_ref_table; }
   bool walk(Item_processor processor, bool walk_subquery, void *arg)
   { 

From 5c896472b6cb315fc54091a342ec37a7c4b5421d Mon Sep 17 00:00:00 2001
From: Sergei Golubchik <serg@mariadb.org>
Date: Wed, 2 Jun 2021 23:10:21 +0200
Subject: [PATCH 02/12] MDEV-25672 table alias from previous statement
 interferes later commands

only perform the "correct table name" check for *new* generated columns,
but not for already existing ones - they're guaranteed to be valid
---
 mysql-test/suite/vcol/r/vcol_syntax.result | 11 ++++++++++-
 mysql-test/suite/vcol/t/vcol_syntax.test   | 17 +++++++++++++----
 sql/item.h                                 |  2 +-
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/mysql-test/suite/vcol/r/vcol_syntax.result b/mysql-test/suite/vcol/r/vcol_syntax.result
index c8983f34c93..0063f38ea36 100644
--- a/mysql-test/suite/vcol/r/vcol_syntax.result
+++ b/mysql-test/suite/vcol/r/vcol_syntax.result
@@ -1,4 +1,3 @@
-drop table if exists t1;
 set @OLD_SQL_MODE=@@SESSION.SQL_MODE;
 create table t1 (a int, b int generated always  as (a+1));
 show create table t1;
@@ -88,3 +87,13 @@ create table t1 (x int, y int default test2.t1.x);
 ERROR 42S22: Unknown column '`test2`.`t1`.`x`' in 'DEFAULT'
 create table t1 (x int, check (test2.t1.x > 0));
 ERROR 42S22: Unknown column '`test2`.`t1`.`x`' in 'CHECK'
+#
+# MDEV-25672 table alias from previous statement interferes later commands
+#
+create table t1 (a int, v_a int generated always as (a));
+update t1 as x set a = 1;
+alter table t1 force;
+drop table t1;
+#
+# End of 10.2 tests
+#
diff --git a/mysql-test/suite/vcol/t/vcol_syntax.test b/mysql-test/suite/vcol/t/vcol_syntax.test
index f425b52ab79..3c8a50a7f36 100644
--- a/mysql-test/suite/vcol/t/vcol_syntax.test
+++ b/mysql-test/suite/vcol/t/vcol_syntax.test
@@ -1,10 +1,6 @@
 #
 # test syntax
 #
---disable_warnings
-drop table if exists t1;
---enable_warnings
-
 set @OLD_SQL_MODE=@@SESSION.SQL_MODE;
 create table t1 (a int, b int generated always  as (a+1));
 show create table t1;
@@ -72,3 +68,16 @@ create table t1 (x int, y int check (y > test2.t1.x));
 create table t1 (x int, y int default test2.t1.x);
 --error ER_BAD_FIELD_ERROR
 create table t1 (x int, check (test2.t1.x > 0));
+
+--echo #
+--echo # MDEV-25672 table alias from previous statement interferes later commands
+--echo #
+create table t1 (a int, v_a int generated always as (a));
+update t1 as x set a = 1;
+alter table t1 force;
+drop table t1;
+
+
+--echo #
+--echo # End of 10.2 tests
+--echo #
diff --git a/sql/item.h b/sql/item.h
index 76be66d2a7c..cc1914a7ad4 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -2841,7 +2841,7 @@ public:
   bool check_table_name_processor(void *arg)
   {
     Check_table_name_prm &p= *(Check_table_name_prm *) arg;
-    if (p.table_name.length && table_name)
+    if (!field && p.table_name.length && table_name)
     {
       DBUG_ASSERT(p.db.length);
       if ((db_name &&

From 7eed97ed9fe2b0c1e69167576be12759dffcd926 Mon Sep 17 00:00:00 2001
From: Anel Husakovic <anel@mariadb.org>
Date: Wed, 26 May 2021 14:15:26 +0200
Subject: [PATCH 03/12] MDEV-25777: JAVA_INCLUDE_PATH and JAVA_INCLUDE_PATH2
 not found with out of source configuration and Ninja generator

- As solution `PLUGIN_CONNECT=NO` use early check to disable plugin:
  Solution suggested by wlad@mariadb.com
- `JNI_FOUND` is a internal result variable and should be set with
cached library and header variables (like `JAVA_INCLUDE_PATH`) defined.
  * Note: wrapper cmake/FindJNI.cmake runs first time and cmake native Find<module> returns only cached variable, like `JAVA_INCLUDE_PATH`, results variable are not cached).

Reviewed by: serg@mariadb.com
---
 cmake/FindJNI.cmake            | 2 +-
 storage/connect/CMakeLists.txt | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/cmake/FindJNI.cmake b/cmake/FindJNI.cmake
index 12305d7c86d..b2c6f849c87 100644
--- a/cmake/FindJNI.cmake
+++ b/cmake/FindJNI.cmake
@@ -1,4 +1,4 @@
-if(JAVA_AWT_LIBRARY)
+if(JAVA_AWT_LIBRARY AND JAVA_INCLUDE_PATH)
   set(JNI_FOUND TRUE)
   return()
 endif()
diff --git a/storage/connect/CMakeLists.txt b/storage/connect/CMakeLists.txt
index e9533e8f3e5..d3632b689fe 100644
--- a/storage/connect/CMakeLists.txt
+++ b/storage/connect/CMakeLists.txt
@@ -13,6 +13,10 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335 USA
 
+IF(WITHOUT_DYNAMIC_PLUGINS OR WITH_NONE OR ("${PLUGIN_CONNECT}" STREQUAL "NO"))
+  RETURN()
+ENDIF()
+
 SET(CONNECT_PLUGIN_STATIC  "connect")
 SET(CONNECT_PLUGIN_DYNAMIC "connect")
 

From ddddfc33c7825609a25ce9531183a0b0fb97f206 Mon Sep 17 00:00:00 2001
From: Anel Husakovic <anel@mariadb.org>
Date: Fri, 4 Jun 2021 14:57:11 +0200
Subject: [PATCH 04/12] Fix mtr tests with file_key_managment extension for
 Windows

Commit b5615eff0d00 introduced comment in result file during shutdown.
In case of Windows for the tests involving `file_key_managment.so` as plugin-load-add the tests will be overwritten with .dll extension.
The same happens with environment variable `$FILE_KEY_MANAGMENT_SO`.
So the patch is removing the extension to be extension agnostic.

Reviewed by: wlad@mariadb.com
---
 .../encrypted_master_switch_to_unencrypted.test      |  2 +-
 .../suite/encryption/r/innodb-bad-key-change2.result | 12 ++++++------
 .../suite/encryption/r/innodb-bad-key-change4.result |  6 +++---
 .../encryption/r/innodb-encryption-disable.result    |  4 ++--
 .../encryption/r/innodb-remove-encryption.result     |  2 +-
 .../suite/encryption/t/innodb-bad-key-change2.test   | 12 ++++++------
 .../suite/encryption/t/innodb-bad-key-change3.test   |  6 +++---
 .../suite/encryption/t/innodb-bad-key-change4.test   |  6 +++---
 .../encryption/t/innodb-encryption-disable.test      |  4 ++--
 .../suite/encryption/t/innodb-remove-encryption.test |  2 +-
 mysql-test/suite/maria/encrypt-wrong-key.test        |  6 +++---
 11 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/mysql-test/suite/binlog_encryption/encrypted_master_switch_to_unencrypted.test b/mysql-test/suite/binlog_encryption/encrypted_master_switch_to_unencrypted.test
index eec72d64066..1e1b0cbd353 100644
--- a/mysql-test/suite/binlog_encryption/encrypted_master_switch_to_unencrypted.test
+++ b/mysql-test/suite/binlog_encryption/encrypted_master_switch_to_unencrypted.test
@@ -65,7 +65,7 @@ INSERT INTO table1_no_encryption SELECT NULL,NOW(),b FROM table1_no_encryption;
 --echo # Part 2: restart master, now with binlog encryption
 --echo #####################################################
 
---let $rpl_server_parameters= --encrypt-binlog=1 --plugin-load-add=$FILE_KEY_MANAGEMENT_SO --file-key-management --loose-file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys.txt
+--let $rpl_server_parameters= --encrypt-binlog=1 --plugin-load-add=file_key_management --file-key-management --loose-file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys.txt
 
 --let $rpl_server_number= 1
 --source restart_server.inc
diff --git a/mysql-test/suite/encryption/r/innodb-bad-key-change2.result b/mysql-test/suite/encryption/r/innodb-bad-key-change2.result
index 543c3bc29b2..af1028f1331 100644
--- a/mysql-test/suite/encryption/r/innodb-bad-key-change2.result
+++ b/mysql-test/suite/encryption/r/innodb-bad-key-change2.result
@@ -7,12 +7,12 @@ call mtr.add_suppression("InnoDB: Table `test`\\.`t1` is corrupted");
 call mtr.add_suppression("InnoDB: Cannot delete tablespace .* because it is not found in the tablespace memory cache");
 call mtr.add_suppression("InnoDB: ALTER TABLE `test`\\.`t1` DISCARD TABLESPACE failed to find tablespace");
 call mtr.add_suppression("\\[ERROR\\] InnoDB: Cannot decrypt \\[page id: space=");
-# restart: --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
+# restart: --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
 SET GLOBAL innodb_file_per_table = ON;
 CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB
 ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
-# restart: --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys3.txt
+# restart: --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys3.txt
 SELECT * FROM t1;
 ERROR 42S02: Table 'test.t1' doesn't exist in engine
 SHOW WARNINGS;
@@ -35,11 +35,11 @@ test.t1	check	Error	Table 'test.t1' doesn't exist in engine
 test.t1	check	status	Operation failed
 SHOW WARNINGS;
 Level	Code	Message
-# restart: --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
+# restart: --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
 FLUSH TABLES t1 FOR EXPORT;
 backup: t1
 UNLOCK TABLES;
-# restart: --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys3.txt
+# restart: --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys3.txt
 ALTER TABLE t1 DISCARD TABLESPACE;
 ERROR 42S02: Table 'test.t1' doesn't exist in engine
 DROP TABLE t1;
@@ -47,7 +47,7 @@ CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB
 ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 ALTER TABLE t1 DISCARD TABLESPACE;
 restore: t1 .ibd and .cfg files
-# restart: --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
+# restart: --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
 ALTER TABLE t1 DISCARD TABLESPACE;
 Warnings:
 Warning	1814	Tablespace has been discarded for table `t1`
@@ -61,7 +61,7 @@ t1	CREATE TABLE `t1` (
   `f` varchar(8) DEFAULT NULL,
   PRIMARY KEY (`pk`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 `ENCRYPTED`=YES `ENCRYPTION_KEY_ID`=4
-# restart: --innodb-encrypt-tables --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys3.txt
+# restart: --innodb-encrypt-tables --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys3.txt
 RENAME TABLE t1 TO t1new;
 ERROR HY000: Error on rename of './test/t1' to './test/t1new' (errno: 155 "The table does not exist in the storage engine")
 ALTER TABLE t1 RENAME TO t1new;
diff --git a/mysql-test/suite/encryption/r/innodb-bad-key-change4.result b/mysql-test/suite/encryption/r/innodb-bad-key-change4.result
index e37ee8eb8cd..ad218457068 100644
--- a/mysql-test/suite/encryption/r/innodb-bad-key-change4.result
+++ b/mysql-test/suite/encryption/r/innodb-bad-key-change4.result
@@ -4,12 +4,12 @@ call mtr.add_suppression("failed to read or decrypt \\[page id: space=[1-9][0-9]
 call mtr.add_suppression("Couldn't load plugins from 'file_key_management");
 call mtr.add_suppression("InnoDB: Table `test`\\.`t1` is corrupted");
 call mtr.add_suppression("\\[ERROR\\] InnoDB: Cannot decrypt \\[page id: space=");
-# restart: --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
+# restart: --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
 SET GLOBAL innodb_file_per_table = ON;
 CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB
 ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
-# restart: --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys3.txt
+# restart: --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys3.txt
 OPTIMIZE TABLE t1;
 Table	Op	Msg_type	Msg_text
 test.t1	optimize	Error	Table 'test.t1' doesn't exist in engine
@@ -22,5 +22,5 @@ test.t1	check	Error	Table 'test.t1' doesn't exist in engine
 test.t1	check	status	Operation failed
 SHOW WARNINGS;
 Level	Code	Message
-# restart: --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
+# restart: --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
 DROP TABLE t1;
diff --git a/mysql-test/suite/encryption/r/innodb-encryption-disable.result b/mysql-test/suite/encryption/r/innodb-encryption-disable.result
index e49a6b759e9..bb4f02b9c39 100644
--- a/mysql-test/suite/encryption/r/innodb-encryption-disable.result
+++ b/mysql-test/suite/encryption/r/innodb-encryption-disable.result
@@ -4,7 +4,7 @@ call mtr.add_suppression("failed to read or decrypt \\[page id: space=[1-9][0-9]
 call mtr.add_suppression("InnoDB: Encrypted page \\[page id: space=[1-9][0-9]*, page number=3\\] in file .*test.t[15].ibd looks corrupted; key_version=1");
 call mtr.add_suppression("InnoDB: Table `test`\\.`t[15]` is corrupted");
 call mtr.add_suppression("Couldn't load plugins from 'file_key_management");
-# restart: --innodb-encrypt-tables=ON --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
+# restart: --innodb-encrypt-tables=ON --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
 create table t5 (
 `intcol1` int(32) DEFAULT NULL,
 `intcol2` int(32) DEFAULT NULL,
@@ -27,6 +27,6 @@ select * from t1;
 ERROR 42S02: Table 'test.t1' doesn't exist in engine
 select * from t5;
 ERROR 42S02: Table 'test.t5' doesn't exist in engine
-# restart: --innodb-encrypt-tables=ON --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
+# restart: --innodb-encrypt-tables=ON --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=MYSQL_TEST_DIR/std_data/keys2.txt
 drop table t1;
 drop table t5;
diff --git a/mysql-test/suite/encryption/r/innodb-remove-encryption.result b/mysql-test/suite/encryption/r/innodb-remove-encryption.result
index 08b31cb568d..9bce59dbbea 100644
--- a/mysql-test/suite/encryption/r/innodb-remove-encryption.result
+++ b/mysql-test/suite/encryption/r/innodb-remove-encryption.result
@@ -6,7 +6,7 @@ flush tables;
 create table t1(a int not null primary key, b char(200)) engine=innodb;
 
 # Restart server with encryption
-# restart: --plugin-load-add=file_key_management.so --loose-file-key-management --loose-file-key-management-filename=MYSQL_TEST_DIR/std_data/keys.txt --file-key-management-encryption-algorithm=aes_cbc --innodb-encrypt-tables=ON --innodb-encryption-threads=4 --innodb-tablespaces-encryption --innodb-encryption-rotate-key-age=15
+# restart: --plugin-load-add=file_key_management --loose-file-key-management --loose-file-key-management-filename=MYSQL_TEST_DIR/std_data/keys.txt --file-key-management-encryption-algorithm=aes_cbc --innodb-encrypt-tables=ON --innodb-encryption-threads=4 --innodb-tablespaces-encryption --innodb-encryption-rotate-key-age=15
 # Wait until encryption threads have encrypted all tablespaces
 SELECT NAME FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION = 0;
 NAME
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-change2.test b/mysql-test/suite/encryption/t/innodb-bad-key-change2.test
index bdbf2327e5d..19399b1e891 100644
--- a/mysql-test/suite/encryption/t/innodb-bad-key-change2.test
+++ b/mysql-test/suite/encryption/t/innodb-bad-key-change2.test
@@ -20,7 +20,7 @@ call mtr.add_suppression("InnoDB: ALTER TABLE `test`\\.`t1` DISCARD TABLESPACE f
 # for innodb_checksum_algorithm=full_crc32 only
 call mtr.add_suppression("\\[ERROR\\] InnoDB: Cannot decrypt \\[page id: space=");
 
---let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--let $restart_parameters=--plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
 --source include/restart_mysqld.inc
 
 SET GLOBAL innodb_file_per_table = ON;
@@ -29,7 +29,7 @@ CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB
 ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
 
---let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
+--let $restart_parameters=--plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
 --source include/restart_mysqld.inc
 
 --error ER_NO_SUCH_TABLE_IN_ENGINE
@@ -48,7 +48,7 @@ CHECK TABLE t1;
 --replace_regex /key_id [1-9][0-9]*/\1 /
 SHOW WARNINGS;
 
---let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--let $restart_parameters=--plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
 --source include/restart_mysqld.inc
 
 let MYSQLD_DATADIR =`SELECT @@datadir`;
@@ -60,7 +60,7 @@ ib_backup_tablespaces("test", "t1");
 EOF
 UNLOCK TABLES;
 
---let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
+--let $restart_parameters=--plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
 --source include/restart_mysqld.inc
 
 --error ER_NO_SUCH_TABLE_IN_ENGINE
@@ -78,7 +78,7 @@ ib_discard_tablespaces("test", "t1");
 ib_restore_tablespaces("test", "t1");
 EOF
 
---let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--let $restart_parameters=--plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
 --source include/restart_mysqld.inc
 
 ALTER TABLE t1 DISCARD TABLESPACE;
@@ -92,7 +92,7 @@ EOF
 ALTER TABLE t1 IMPORT TABLESPACE;
 SHOW CREATE TABLE t1;
 
---let $restart_parameters= --innodb-encrypt-tables --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
+--let $restart_parameters= --innodb-encrypt-tables --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
 --source include/restart_mysqld.inc
 
 --error ER_ERROR_ON_RENAME
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-change3.test b/mysql-test/suite/encryption/t/innodb-bad-key-change3.test
index dbd04748143..9c2918f3118 100644
--- a/mysql-test/suite/encryption/t/innodb-bad-key-change3.test
+++ b/mysql-test/suite/encryption/t/innodb-bad-key-change3.test
@@ -25,7 +25,7 @@ call mtr.add_suppression("InnoDB: Cannot calculate statistics for table .* becau
 4;770A8A65DA156D24EE2A093277530143
 EOF
 
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
+--exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
 --enable_reconnect
 --source include/wait_until_connected_again.inc
 
@@ -62,7 +62,7 @@ ib_discard_tablespaces("test", "t1");
 ib_restore_tablespaces("test", "t1");
 EOF
 
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
+--exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
 --enable_reconnect
 --source include/wait_until_connected_again.inc
 --source include/restart_mysqld.inc
@@ -89,7 +89,7 @@ SELECT * FROM t1;
 4;770A8A65DA156D24EE2A093277530143
 EOF
 
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
+--exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
 --enable_reconnect
 --source include/wait_until_connected_again.inc
 DROP TABLE t1;
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-change4.test b/mysql-test/suite/encryption/t/innodb-bad-key-change4.test
index b341fc81d39..58517f14978 100644
--- a/mysql-test/suite/encryption/t/innodb-bad-key-change4.test
+++ b/mysql-test/suite/encryption/t/innodb-bad-key-change4.test
@@ -16,7 +16,7 @@ call mtr.add_suppression("InnoDB: Table `test`\\.`t1` is corrupted");
 # for innodb_checksum_algorithm=full_crc32 only
 call mtr.add_suppression("\\[ERROR\\] InnoDB: Cannot decrypt \\[page id: space=");
 
---let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--let $restart_parameters=--plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
 --source include/restart_mysqld.inc
 
 SET GLOBAL innodb_file_per_table = ON;
@@ -25,7 +25,7 @@ CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB
 ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
 
---let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
+--let $restart_parameters=--plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
 --source include/restart_mysqld.inc
 
 --replace_regex /key_id [1-9][0-9]*/\1 /
@@ -38,7 +38,7 @@ CHECK TABLE t1;
 --replace_regex /key_id [1-9][0-9]*/\1 /
 SHOW WARNINGS;
 
---let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--let $restart_parameters=--plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
 --source include/restart_mysqld.inc
 
 DROP TABLE t1;
diff --git a/mysql-test/suite/encryption/t/innodb-encryption-disable.test b/mysql-test/suite/encryption/t/innodb-encryption-disable.test
index 4d0aa04bc56..2097a4ad184 100644
--- a/mysql-test/suite/encryption/t/innodb-encryption-disable.test
+++ b/mysql-test/suite/encryption/t/innodb-encryption-disable.test
@@ -16,7 +16,7 @@ call mtr.add_suppression("InnoDB: Table `test`\\.`t[15]` is corrupted");
 # Suppression for builds where file_key_management plugin is linked statically
 call mtr.add_suppression("Couldn't load plugins from 'file_key_management");
 
---let $restart_parameters=--innodb-encrypt-tables=ON --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--let $restart_parameters=--innodb-encrypt-tables=ON --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
 --source include/restart_mysqld.inc
 
 create table t5 (
@@ -48,7 +48,7 @@ select * from t1;
 --error ER_NO_SUCH_TABLE_IN_ENGINE
 select * from t5;
 
---let $restart_parameters=--innodb-encrypt-tables=ON --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--let $restart_parameters=--innodb-encrypt-tables=ON --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
 --source include/restart_mysqld.inc
 
 drop table t1;
diff --git a/mysql-test/suite/encryption/t/innodb-remove-encryption.test b/mysql-test/suite/encryption/t/innodb-remove-encryption.test
index 24e00a00a02..3d719dbef74 100644
--- a/mysql-test/suite/encryption/t/innodb-remove-encryption.test
+++ b/mysql-test/suite/encryption/t/innodb-remove-encryption.test
@@ -18,7 +18,7 @@ create table t1(a int not null primary key, b char(200)) engine=innodb;
 
 --echo
 --echo # Restart server with encryption
--- let $restart_parameters=--plugin-load-add=$FILE_KEY_MANAGEMENT_SO --loose-file-key-management --loose-file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys.txt --file-key-management-encryption-algorithm=aes_cbc --innodb-encrypt-tables=ON --innodb-encryption-threads=4 --innodb-tablespaces-encryption --innodb-encryption-rotate-key-age=15
+-- let $restart_parameters=--plugin-load-add=file_key_management --loose-file-key-management --loose-file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys.txt --file-key-management-encryption-algorithm=aes_cbc --innodb-encrypt-tables=ON --innodb-encryption-threads=4 --innodb-tablespaces-encryption --innodb-encryption-rotate-key-age=15
 -- source include/restart_mysqld.inc
 
 --echo # Wait until encryption threads have encrypted all tablespaces
diff --git a/mysql-test/suite/maria/encrypt-wrong-key.test b/mysql-test/suite/maria/encrypt-wrong-key.test
index 2afa785dd0f..ca65e1018d0 100644
--- a/mysql-test/suite/maria/encrypt-wrong-key.test
+++ b/mysql-test/suite/maria/encrypt-wrong-key.test
@@ -17,7 +17,7 @@ call mtr.add_suppression("Failed to decrypt");
 1;770A8A65DA156D24EE2A093277530142
 EOF
 
---exec echo "restart:--aria-encrypt-tables=1 --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
+--exec echo "restart:--aria-encrypt-tables=1 --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
 --enable_reconnect
 --source include/wait_until_connected_again.inc
 
@@ -32,7 +32,7 @@ INSERT INTO t1 VALUES (1);
 2;770A8A65DA156D24EE2A093277530143
 EOF
 
---exec echo "restart:--aria-encrypt-tables=1 --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
+--exec echo "restart:--aria-encrypt-tables=1 --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
 --enable_reconnect
 --source include/wait_until_connected_again.inc
 
@@ -44,7 +44,7 @@ INSERT INTO t1 VALUES (2);
 --shutdown_server
 --source include/wait_until_disconnected.inc
 
---exec echo "restart:--aria-encrypt-tables=1 --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
+--exec echo "restart:--aria-encrypt-tables=1 --plugin-load-add=file_key_management --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
 --enable_reconnect
 --source include/wait_until_connected_again.inc
 

From b1b4d67bcda32472f5b9c46465bff9db86904a00 Mon Sep 17 00:00:00 2001
From: Vladislav Vaintroub <wlad@mariadb.com>
Date: Fri, 4 Jun 2021 15:00:34 +0200
Subject: [PATCH 05/12] MDEV-21373 DBUG compilation - bad synchronization in
 ha_heap::external_lock()

ha_heap::external_lock contains some consistency checks for the table,#
in a debug compilation.

This code suffers from lack of synchronization, in a rare case
where mysql_lock_tables() fail, and unlock is forced, even if lock was
not previously taken.

To workaround, require EXTRA_DEBUG compile definition in order to activate
the consistency checks.The code still might be useful in some cases - but
the audience are developers looking for errors in single-threaded scenarios,
rather than multiuser stress-tests.
---
 storage/heap/ha_heap.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/storage/heap/ha_heap.cc b/storage/heap/ha_heap.cc
index 5af68f098a4..8a8e1f74e47 100644
--- a/storage/heap/ha_heap.cc
+++ b/storage/heap/ha_heap.cc
@@ -423,7 +423,7 @@ int ha_heap::reset_auto_increment(ulonglong value)
 
 int ha_heap::external_lock(THD *thd, int lock_type)
 {
-#ifndef DBUG_OFF
+#if !defined(DBUG_OFF) && defined(EXTRA_DEBUG)
   if (lock_type == F_UNLCK && file->s->changed && heap_check_heap(file, 0))
     return HA_ERR_CRASHED;
 #endif

From cebc435592043a83d5f430aec8bdaa79cd7c1d44 Mon Sep 17 00:00:00 2001
From: Vladislav Vaintroub <wlad@mariadb.com>
Date: Sat, 5 Jun 2021 16:57:10 +0200
Subject: [PATCH 06/12] MDEV-25859 - HeidiSQL 11.3

---
 win/packaging/heidisql.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/win/packaging/heidisql.cmake b/win/packaging/heidisql.cmake
index 6de033eacb2..c57282ce90a 100644
--- a/win/packaging/heidisql.cmake
+++ b/win/packaging/heidisql.cmake
@@ -1,4 +1,4 @@
-SET(HEIDISQL_BASE_NAME "HeidiSQL_11.2_32_Portable")
+SET(HEIDISQL_BASE_NAME "HeidiSQL_11.3_32_Portable")
 SET(HEIDISQL_ZIP "${HEIDISQL_BASE_NAME}.zip")
 SET(HEIDISQL_URL "http://www.heidisql.com/downloads/releases/${HEIDISQL_ZIP}")
 SET(HEIDISQL_DOWNLOAD_DIR ${THIRD_PARTY_DOWNLOAD_LOCATION}/${HEIDISQL_BASE_NAME})

From 9f9a925c399b9d960f095be0886f56f51396eb04 Mon Sep 17 00:00:00 2001
From: Vladislav Vaintroub <wlad@mariadb.com>
Date: Sun, 6 Jun 2021 08:46:59 +0200
Subject: [PATCH 07/12] MDEV-23815 Windows : mysql_upgrade_wizard fails, if
 service name has spaces

The fix is to quote service name parameter, when it is passed to
mysql_upgrade_service subprocess.
---
 win/upgrade_wizard/upgradeDlg.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/win/upgrade_wizard/upgradeDlg.cpp b/win/upgrade_wizard/upgradeDlg.cpp
index d996c0ebe5d..d0dd6a3fa75 100644
--- a/win/upgrade_wizard/upgradeDlg.cpp
+++ b/win/upgrade_wizard/upgradeDlg.cpp
@@ -367,7 +367,10 @@ void CUpgradeDlg::UpgradeOneService(const string& servicename)
     ErrorExit("Stdout SetHandleInformation"); 
 
   string commandline("mysql_upgrade_service.exe --service=");
+  commandline += "\"";
   commandline += servicename;
+  commandline += "\"";
+
   si.cb = sizeof(si);
   si.hStdInput= GetStdHandle(STD_INPUT_HANDLE);
   si.hStdOutput= hPipeWrite;
@@ -397,7 +400,7 @@ void CUpgradeDlg::UpgradeOneService(const string& servicename)
   else
   {
 	/* 
-	  Creating a process with CREATE_BREAKAWAY_FROM_JOB, reset this flag
+	  Creating a process with CREATE_BREAKAWAY_FROM_JOB failed, reset this flag
 	  and retry.
 	*/
     if (!CreateProcess(NULL, (LPSTR)commandline.c_str(), NULL, NULL, TRUE,

From 3c922d6defcfa6be819fe33794abcf507bb70c25 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Otto=20Kek=C3=A4l=C3=A4inen?= <otto@kekalainen.net>
Date: Fri, 28 May 2021 22:04:17 -0700
Subject: [PATCH 08/12] Revert "CONNECT: move jar files to /usr/share and
 include them in DEBs"

This partially reverts commit d7321893d8c50071632a102e17a7869da9cb03a5.

The *.jar files are not being built and all Debian builds are failing
as dh_install stops on missing files. To build them we would need to also
add new Java build dependencies.

In a stable release (10.2->10.5) we shouldn't add new files and certainly
not any new build dependencies, so reverting commit.

Also, the files are located in a different path, and already included
in the mariadb-test-data package:

  /usr/share/mysql/mysql-test/plugin/connect/connect/std_data/JavaWrappers.jar
  /usr/share/mysql/mysql-test/plugin/connect/connect/std_data/JdbcMariaDB.jar
  /usr/share/mysql/mysql-test/plugin/connect/connect/std_data/Mongo2.jar
  /usr/share/mysql/mysql-test/plugin/connect/connect/std_data/Mongo3.jar

This change needs to be redesigned and applies only on 10.6 or newer.
---
 debian/mariadb-plugin-connect.install | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/debian/mariadb-plugin-connect.install b/debian/mariadb-plugin-connect.install
index 7b5a5f0633e..8a7aee412df 100644
--- a/debian/mariadb-plugin-connect.install
+++ b/debian/mariadb-plugin-connect.install
@@ -1,6 +1,2 @@
 etc/mysql/conf.d/connect.cnf
 usr/lib/mysql/plugin/ha_connect.so
-usr/share/mysql/Mongo2.jar
-usr/share/mysql/Mongo3.jar
-usr/share/mysql/JavaWrappers.jar
-usr/share/mysql/JdbcInterface.jar

From 8149e4d0a139b901c8902b5b9fae371cef47275f Mon Sep 17 00:00:00 2001
From: Igor Babaev <igor@askmonty.org>
Date: Mon, 7 Jun 2021 15:08:18 -0700
Subject: [PATCH 09/12] MDEV-25682 Explain shows an execution plan different
 from actually executed

If a select query contained an ORDER BY clause that followed a LIMIT clause
or an ORDER BY clause or ORDER BY with LIMIT the EXPLAIN output for the
query showed an execution plan different from that was actually executed.

Approved by Roman Nozdrin <roman.nozdrin@mariadb.com>
---
 mysql-test/main/order_by.result | 25 +++++++++++++++++++++++++
 mysql-test/main/order_by.test   | 16 ++++++++++++++++
 sql/item_subselect.cc           |  2 ++
 sql/sql_select.cc               |  2 +-
 4 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/mysql-test/main/order_by.result b/mysql-test/main/order_by.result
index f4e88d6e6e3..c8f63f881cc 100644
--- a/mysql-test/main/order_by.result
+++ b/mysql-test/main/order_by.result
@@ -3460,6 +3460,31 @@ SET max_length_for_sort_data=@save_max_length_for_sort_data;
 SET max_sort_length= @save_max_sort_length;
 SET sql_select_limit= @save_sql_select_limit;
 DROP TABLE t1;
+#
+# MDEV-25682: EXPLAIN for SELECT with ORDER BY after [ORDER BY] LIMIT
+#
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+explain (select a from t1 limit 2) order by a desc;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	
+NULL	UNIT RESULT	<unit1>	ALL	NULL	NULL	NULL	NULL	NULL	Using filesort
+(select a from t1 limit 2) order by a desc;
+a
+7
+3
+create table t2 (a int, b int);
+insert into t2 values (3,70), (7,10), (1,40), (4,30);
+explain (select b,a from t2 order by a limit 3) order by b desc;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	Using filesort
+NULL	UNIT RESULT	<unit1>	ALL	NULL	NULL	NULL	NULL	NULL	Using filesort
+(select b,a from t2 order by a limit 3) order by b desc;
+b	a
+70	3
+40	1
+30	4
+drop table t1,t2;
 # End of 10.2 tests
 #
 # MDEV-16214: Incorrect plan taken by the optimizer , uses INDEX instead of ref access with ORDER BY
diff --git a/mysql-test/main/order_by.test b/mysql-test/main/order_by.test
index 74884144a98..08d5982b220 100644
--- a/mysql-test/main/order_by.test
+++ b/mysql-test/main/order_by.test
@@ -2293,6 +2293,22 @@ SET max_sort_length= @save_max_sort_length;
 SET sql_select_limit= @save_sql_select_limit;
 DROP TABLE t1;
 
+--echo #
+--echo # MDEV-25682: EXPLAIN for SELECT with ORDER BY after [ORDER BY] LIMIT
+--echo #
+
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+explain (select a from t1 limit 2) order by a desc;
+(select a from t1 limit 2) order by a desc;
+
+create table t2 (a int, b int);
+insert into t2 values (3,70), (7,10), (1,40), (4,30);
+explain (select b,a from t2 order by a limit 3) order by b desc;
+(select b,a from t2 order by a limit 3) order by b desc;
+
+drop table t1,t2;
+
 --echo # End of 10.2 tests
 
 --echo #
diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc
index 9f43561151d..352d80da92c 100644
--- a/sql/item_subselect.cc
+++ b/sql/item_subselect.cc
@@ -274,6 +274,8 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref)
         res= TRUE;
         goto end;
       }
+      if (sl ==  unit->first_select() && !sl->next_select())
+        unit->fake_select_lex= 0;
     }
   }
   
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 6c090ea5352..7f4c6d24b8d 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -26370,7 +26370,7 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
     sl->options|= SELECT_DESCRIBE;
   }
 
-  if (unit->is_unit_op())
+  if (unit->is_unit_op() || unit->fake_select_lex)
   {
     if (unit->union_needs_tmp_table() && unit->fake_select_lex)
     {

From bb28bffc3ed179a9912aced2b873e43999111887 Mon Sep 17 00:00:00 2001
From: Igor Babaev <igor@askmonty.org>
Date: Mon, 7 Jun 2021 19:51:57 -0700
Subject: [PATCH 10/12] Ported the test case for MDEV-25682 from 10.2

No fix for this bug is needed starting from version 10.4.
---
 mysql-test/main/order_by.result | 25 +++++++++++++++++++++++++
 mysql-test/main/order_by.test   | 16 ++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/mysql-test/main/order_by.result b/mysql-test/main/order_by.result
index 826daf0542f..129bd8928f2 100644
--- a/mysql-test/main/order_by.result
+++ b/mysql-test/main/order_by.result
@@ -3536,6 +3536,31 @@ SET max_length_for_sort_data=@save_max_length_for_sort_data;
 SET max_sort_length= @save_max_sort_length;
 SET sql_select_limit= @save_sql_select_limit;
 DROP TABLE t1;
+#
+# MDEV-25682: EXPLAIN for SELECT with ORDER BY after [ORDER BY] LIMIT
+#
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+explain (select a from t1 limit 2) order by a desc;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	Using filesort
+2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	
+(select a from t1 limit 2) order by a desc;
+a
+7
+3
+create table t2 (a int, b int);
+insert into t2 values (3,70), (7,10), (1,40), (4,30);
+explain (select b,a from t2 order by a limit 3) order by b desc;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	3	Using filesort
+2	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	4	Using filesort
+(select b,a from t2 order by a limit 3) order by b desc;
+b	a
+70	3
+40	1
+30	4
+drop table t1,t2;
 # End of 10.2 tests
 #
 # MDEV-16214: Incorrect plan taken by the optimizer , uses INDEX instead of ref access with ORDER BY
diff --git a/mysql-test/main/order_by.test b/mysql-test/main/order_by.test
index 1bf353fd69d..0bf0311a642 100644
--- a/mysql-test/main/order_by.test
+++ b/mysql-test/main/order_by.test
@@ -2294,6 +2294,22 @@ SET max_sort_length= @save_max_sort_length;
 SET sql_select_limit= @save_sql_select_limit;
 DROP TABLE t1;
 
+--echo #
+--echo # MDEV-25682: EXPLAIN for SELECT with ORDER BY after [ORDER BY] LIMIT
+--echo #
+
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+explain (select a from t1 limit 2) order by a desc;
+(select a from t1 limit 2) order by a desc;
+
+create table t2 (a int, b int);
+insert into t2 values (3,70), (7,10), (1,40), (4,30);
+explain (select b,a from t2 order by a limit 3) order by b desc;
+(select b,a from t2 order by a limit 3) order by b desc;
+
+drop table t1,t2;
+
 --echo # End of 10.2 tests
 
 --echo #

From dfa2d0bc13362b949b1b1699955583f74e7db90a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= <marko.makela@mariadb.com>
Date: Mon, 7 Jun 2021 17:46:59 +0300
Subject: [PATCH 11/12] MDEV-25869 Change buffer entries are lost on InnoDB
 restart

buf_read_ibuf_merge_pages(): If space->size is 0, invoke
fil_space_get_size() to determine the size of the tablespace
by reading the header page. Only after that proceed to delete
any entries that are beyond the end of the tablespace.
Otherwise, we could be deleting valid entries that actually
need to be applied.

This fixes a regression that had been introduced in
commit b80df9eba23b4eb9694e770a41135127c6dbc5df (MDEV-21069),
which aimed to avoid crashes during DROP TABLE of corrupted tables.
---
 storage/innobase/buf/buf0rea.cc | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/storage/innobase/buf/buf0rea.cc b/storage/innobase/buf/buf0rea.cc
index ad583e577c4..6b68e9f8fa5 100644
--- a/storage/innobase/buf/buf0rea.cc
+++ b/storage/innobase/buf/buf0rea.cc
@@ -1,7 +1,7 @@
 /*****************************************************************************
 
 Copyright (c) 1995, 2017, Oracle and/or its affiliates. All Rights Reserved.
-Copyright (c) 2015, 2020, MariaDB Corporation.
+Copyright (c) 2015, 2021, 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
@@ -806,13 +806,18 @@ tablespace_deleted:
 			continue;
 		}
 
-		if (UNIV_UNLIKELY(page_nos[i] >= space->size)) {
+		ulint size = space->size;
+		if (!size) {
+			size = fil_space_get_size(space->id);
+		}
+
+		if (UNIV_UNLIKELY(page_nos[i] >= size)) {
 			do {
 				ibuf_delete_recs(page_id_t(space_ids[i],
 							   page_nos[i]));
 			} while (++i < n_stored
 				 && space_ids[i - 1] == space_ids[i]
-				 && page_nos[i] >= space->size);
+				 && page_nos[i] >= size);
 			i--;
 next:
 			fil_space_release(space);

From b8d38c5e39d526e006f4e8e8977ac4c6166bc4c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= <marko.makela@mariadb.com>
Date: Tue, 8 Jun 2021 15:03:50 +0300
Subject: [PATCH 12/12] dict_index_build_internal_clust(): Catch MDEV-20131 on
 CREATE TABLE

---
 storage/innobase/dict/dict0dict.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/storage/innobase/dict/dict0dict.cc b/storage/innobase/dict/dict0dict.cc
index 59851acdaab..1ffeb8d1e16 100644
--- a/storage/innobase/dict/dict0dict.cc
+++ b/storage/innobase/dict/dict0dict.cc
@@ -2318,8 +2318,8 @@ dict_index_build_internal_clust(
 	ulint		i;
 	ibool*		indexed;
 
-	ut_ad(dict_index_is_clust(index));
-	ut_ad(!dict_index_is_ibuf(index));
+	ut_ad(index->is_primary());
+	ut_ad(!index->has_virtual());
 
 	ut_ad(mutex_own(&dict_sys.mutex));