From df0e1817c716a4b2c48d2f83d55f4d2549168843 Mon Sep 17 00:00:00 2001 From: Aleksey Midenkov Date: Fri, 26 Jan 2018 12:46:14 +0300 Subject: [PATCH] Vers SQL: partition rotation by INTERVAL fix Update partition stats on ha_partition::write_row() --- .../suite/versioning/r/partition.result | 37 ++++++++++++++----- mysql-test/suite/versioning/t/partition.test | 25 ++++++++++--- sql/ha_partition.cc | 12 +++--- sql/partition_info.h | 5 ++- 4 files changed, 56 insertions(+), 23 deletions(-) diff --git a/mysql-test/suite/versioning/r/partition.result b/mysql-test/suite/versioning/r/partition.result index 2b275538fb9..e572eb2e785 100644 --- a/mysql-test/suite/versioning/r/partition.result +++ b/mysql-test/suite/versioning/r/partition.result @@ -268,31 +268,50 @@ partition p0 history, partition p1 history, partition pn current); ERROR HY000: Wrong parameters for partitioned `t1`: wrong value for 'INTERVAL' +### ha_partition::update_row() check create or replace table t1 (x int) with system versioning partition by system_time interval 1 second ( partition p0 history, partition p1 history, partition pn current); -insert into t1 values (1), (2), (3); +insert into t1 values (1), (2), (3), (4); select * from t1 partition (pn); x 1 2 3 -delete from t1; -select * from t1 partition (p0); -x -1 -2 -3 -insert into t1 values (4); +4 +delete from t1 where x < 3; delete from t1; Warnings: Note 4114 Versioned table `test`.`t1`: switching from partition `p0` to `p1` +select * from t1 partition (p0) order by x; +x +1 +2 +select * from t1 partition (p1) order by x; +x +3 +4 +### ha_partition::write_row() check +create or replace table t1 (x int) +with system versioning +partition by system_time interval 1 second ( +partition p0 history, +partition p1 history, +partition pn current); +insert into t1 values (1); +update t1 set x= 2; +update t1 set x= 3; +Warnings: +Note 4114 Versioned table `test`.`t1`: switching from partition `p0` to `p1` +select * from t1 partition (p0); +x +1 select * from t1 partition (p1); x -4 +2 ## Subpartitions create or replace table t1 (x int) with system versioning diff --git a/mysql-test/suite/versioning/t/partition.test b/mysql-test/suite/versioning/t/partition.test index 1dea9f7c69c..d16bb4abc5c 100644 --- a/mysql-test/suite/versioning/t/partition.test +++ b/mysql-test/suite/versioning/t/partition.test @@ -122,7 +122,6 @@ alter table t1 add partition (partition px history); --echo ## INSERT, UPDATE, DELETE - create or replace table t1 (x int) with system versioning partition by system_time ( @@ -241,6 +240,7 @@ partition by system_time interval 0 second ( partition p1 history, partition pn current); +--echo ### ha_partition::update_row() check create or replace table t1 (x int) with system versioning partition by system_time interval 1 second ( @@ -248,14 +248,29 @@ partition by system_time interval 1 second ( partition p1 history, partition pn current); -insert into t1 values (1), (2), (3); +insert into t1 values (1), (2), (3), (4); select * from t1 partition (pn); -delete from t1; -select * from t1 partition (p0); +delete from t1 where x < 3; --sleep 2 -insert into t1 values (4); delete from t1; + +select * from t1 partition (p0) order by x; +select * from t1 partition (p1) order by x; + +--echo ### ha_partition::write_row() check +create or replace table t1 (x int) +with system versioning +partition by system_time interval 1 second ( + partition p0 history, + partition p1 history, + partition pn current); + +insert into t1 values (1); +update t1 set x= 2; +sleep 2; +update t1 set x= 3; +select * from t1 partition (p0); select * from t1 partition (p1); --echo ## Subpartitions diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc index a0b4abf77fb..b990baf49fb 100644 --- a/sql/ha_partition.cc +++ b/sql/ha_partition.cc @@ -4269,6 +4269,10 @@ int ha_partition::write_row(uchar * buf) if (have_auto_increment && !table->s->next_number_keypart) set_auto_increment_if_higher(table->next_number_field); reenable_binlog(thd); + + if (m_part_info->part_type == VERSIONING_PARTITION) + m_part_info->vers_update_stats(thd, part_id); + exit: thd->variables.sql_mode= saved_sql_mode; table->auto_increment_field_not_null= saved_auto_inc_field_not_null; @@ -4378,13 +4382,7 @@ int ha_partition::update_row(const uchar *old_data, const uchar *new_data) goto exit; if (m_part_info->part_type == VERSIONING_PARTITION) - { - uint sub_factor= m_part_info->num_subparts ? m_part_info->num_subparts : 1; - DBUG_ASSERT(m_tot_parts == m_part_info->num_parts * sub_factor); - uint lpart_id= new_part_id / sub_factor; - // lpart_id is HISTORY partition because new_part_id != old_part_id - m_part_info->vers_update_stats(thd, lpart_id); - } + m_part_info->vers_update_stats(thd, new_part_id); tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */ error= m_file[old_part_id]->ha_delete_row(old_data); diff --git a/sql/partition_info.h b/sql/partition_info.h index 26955682dda..f2d185cdfaf 100644 --- a/sql/partition_info.h +++ b/sql/partition_info.h @@ -521,8 +521,9 @@ public: void vers_update_stats(THD *thd, uint part_id) { DBUG_ASSERT(vers_info && vers_info->initialized()); - if (part_id < vers_info->now_part->id) - vers_update_stats(thd, get_partition(part_id)); + uint lpart_id= num_subparts ? part_id / num_subparts : part_id; + if (lpart_id < vers_info->now_part->id) + vers_update_stats(thd, get_partition(lpart_id)); } bool vers_update_range_constants(THD *thd) {