From 8c9c30201630169e35844c9739eeb1b9b112305f Mon Sep 17 00:00:00 2001
From: Eugene Kosov <claprix@yandex.ru>
Date: Wed, 6 Dec 2017 15:54:00 +0300
Subject: [PATCH] SQL: fix implicit sys fields for implicit engine of
 partitioned table [#366]

---
 .../suite/versioning/r/partition.result       |  6 +++++
 mysql-test/suite/versioning/t/partition.test  |  7 ++++++
 sql/handler.cc                                | 24 ++++++++++++++++---
 sql/sql_table.cc                              | 16 ++++++++-----
 4 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/mysql-test/suite/versioning/r/partition.result b/mysql-test/suite/versioning/r/partition.result
index 2bca840d4cc..629004840c1 100644
--- a/mysql-test/suite/versioning/r/partition.result
+++ b/mysql-test/suite/versioning/r/partition.result
@@ -287,4 +287,10 @@ x
 select * from t1 partition (p1sp1) for system_time all;
 x
 2
+create or replace table t1 (a bigint)
+with system versioning
+partition by range (a)
+(partition p0 values less than (20) engine innodb,
+partition p1 values less than maxvalue engine innodb);
+insert into t1 values (1);
 drop table t1;
diff --git a/mysql-test/suite/versioning/t/partition.test b/mysql-test/suite/versioning/t/partition.test
index 12143647770..e6c1812e2ae 100644
--- a/mysql-test/suite/versioning/t/partition.test
+++ b/mysql-test/suite/versioning/t/partition.test
@@ -229,6 +229,13 @@ select * from t1 partition (p0sp1) for system_time all;
 select * from t1 partition (p1sp0) for system_time all;
 select * from t1 partition (p1sp1) for system_time all;
 
+create or replace table t1 (a bigint)
+with system versioning
+partition by range (a)
+(partition p0 values less than (20) engine innodb,
+ partition p1 values less than maxvalue engine innodb);
+insert into t1 values (1);
+
 drop table t1;
 
 -- source suite/versioning/common_finish.inc
diff --git a/sql/handler.cc b/sql/handler.cc
index 3b936362d4b..a38113e8262 100644
--- a/sql/handler.cc
+++ b/sql/handler.cc
@@ -6872,7 +6872,25 @@ bool Vers_parse_info::check_and_fix_implicit(
     }
   }
 
-  bool integer_fields= create_info->db_type->flags & HTON_NATIVE_SYS_VERSIONING;
+  bool integer_fields= ha_check_storage_engine_flag(create_info->db_type,
+                                                    HTON_NATIVE_SYS_VERSIONING);
+
+#ifdef WITH_PARTITION_STORAGE_ENGINE
+  if (partition_info *info= thd->work_part_info)
+  {
+    if (!(create_info->used_fields & HA_CREATE_USED_ENGINE) &&
+        info->partitions.elements)
+    {
+      partition_element *element=
+          static_cast<partition_element *>(info->partitions.elem(0));
+      handlerton *hton= element->engine_type;
+      if (hton && ha_check_storage_engine_flag(hton, HTON_NATIVE_SYS_VERSIONING))
+      {
+        integer_fields= true;
+      }
+    }
+  }
+#endif
 
   if (fix_implicit(thd, alter_info, integer_fields))
     return true;
@@ -6947,8 +6965,8 @@ bool Vers_parse_info::check_and_fix_alter(THD *thd, Alter_info *alter_info,
                                           TABLE *table)
 {
   TABLE_SHARE *share= table->s;
-  bool integer_fields=
-      create_info->db_type->flags & HTON_NATIVE_SYS_VERSIONING;
+  bool integer_fields= ha_check_storage_engine_flag(create_info->db_type,
+                                                    HTON_NATIVE_SYS_VERSIONING);
   const char *table_name= share->table_name.str;
 
   if (!need_check() && !share->versioned)
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index bbb27228e2f..24b9fa619ca 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -8854,13 +8854,17 @@ bool mysql_alter_table(THD *thd, const char *new_db, const char *new_name,
 
   if (versioned)
   {
-    if (create_info->db_type &&
-      table->s->db_type() != create_info->db_type && (
-      table->file->native_versioned() ||
-      create_info->db_type->flags & HTON_NATIVE_SYS_VERSIONING))
+    if (handlerton *hton1= create_info->db_type)
     {
-      my_error(ER_VERS_ALTER_ENGINE_PROHIBITED, MYF(0), table_list->db, table_list->table_name);
-      DBUG_RETURN(true);
+      handlerton *hton2= table->file->ht;
+      if (hton1 != hton2 &&
+          (ha_check_storage_engine_flag(hton1, HTON_NATIVE_SYS_VERSIONING) ||
+           ha_check_storage_engine_flag(hton2, HTON_NATIVE_SYS_VERSIONING)))
+      {
+        my_error(ER_VERS_ALTER_ENGINE_PROHIBITED, MYF(0), table_list->db,
+                 table_list->table_name);
+        DBUG_RETURN(true);
+      }
     }
     bool vers_data_mod= alter_info->data_modifying();
     if (thd->variables.vers_alter_history == VERS_ALTER_HISTORY_SURVIVE)
-- 
2.30.9