From 412dd1e1f3a890a771b64283b7f34bc65a8a791f Mon Sep 17 00:00:00 2001 From: Aleksey Midenkov Date: Tue, 20 Dec 2016 18:52:45 +0000 Subject: [PATCH] SQL: FOR SYSTEM_TIME support in VIEW expression [fixes #99] --- mysql-test/suite/versioning/r/view.result | 25 +++++++++++++ mysql-test/suite/versioning/t/view.test | 25 +++++++++++++ sql/sql_select.cc | 43 ++++------------------- sql/table.h | 6 ---- 4 files changed, 56 insertions(+), 43 deletions(-) create mode 100644 mysql-test/suite/versioning/r/view.result create mode 100644 mysql-test/suite/versioning/t/view.test diff --git a/mysql-test/suite/versioning/r/view.result b/mysql-test/suite/versioning/r/view.result new file mode 100644 index 00000000000..b9538698ec1 --- /dev/null +++ b/mysql-test/suite/versioning/r/view.result @@ -0,0 +1,25 @@ +create table t1 (x int) with system versioning engine innodb; +insert into t1 values (1); +select now(6) into @t1; +update t1 set x= 2; +select now(6) into @t2; +delete from t1; +set @vt1= concat("create view vt1 as select * from t1 for system_time as of timestamp '", @t1, "'"); +prepare stmt from @vt1; +execute stmt; +drop prepare stmt; +set @vt2= concat("create view vt2 as select * from t1 for system_time as of timestamp '", @t2, "'"); +prepare stmt from @vt2; +execute stmt; +drop prepare stmt; +select * from vt1; +x +1 +select * from vt2; +x +2 +select * from t1; +x +drop view vt1; +drop view vt2; +drop table t1; diff --git a/mysql-test/suite/versioning/t/view.test b/mysql-test/suite/versioning/t/view.test new file mode 100644 index 00000000000..6c04255bad1 --- /dev/null +++ b/mysql-test/suite/versioning/t/view.test @@ -0,0 +1,25 @@ +-- source include/have_innodb.inc + +create table t1 (x int) with system versioning engine innodb; +insert into t1 values (1); + +select now(6) into @t1; +update t1 set x= 2; + +select now(6) into @t2; +delete from t1; + +set @vt1= concat("create view vt1 as select * from t1 for system_time as of timestamp '", @t1, "'"); +prepare stmt from @vt1; execute stmt; drop prepare stmt; + +set @vt2= concat("create view vt2 as select * from t1 for system_time as of timestamp '", @t2, "'"); +prepare stmt from @vt2; execute stmt; drop prepare stmt; + +select * from vt1; +select * from vt2; +select * from t1; + +drop view vt1; +drop view vt2; +drop table t1; + diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 1aef44397b2..f72456a9414 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -899,8 +899,6 @@ vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, SELECT_LEX *s *dst_cond= cond1; else thd->change_item_tree(dst_cond, cond1); - - table->vers_moved_to_where= true; } } // if (... table->table->versioned()) } // for (table= tables; ...) @@ -25036,39 +25034,6 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result) DBUG_RETURN(res || thd->is_error()); } -void TABLE_LIST::print_system_versioning(THD *thd, table_map eliminated_tables, - String *str, enum_query_type query_type) -{ - if (vers_moved_to_where) - return; - - DBUG_ASSERT(select_lex); - // system versioning - if (select_lex->vers_conditions.type != FOR_SYSTEM_TIME_UNSPECIFIED) - { - switch (select_lex->vers_conditions.type) - { - case FOR_SYSTEM_TIME_AS_OF: - str->append(STRING_WITH_LEN(" for system_time as of ")); - select_lex->vers_conditions.start->print(str, query_type); - break; - case FOR_SYSTEM_TIME_FROM_TO: - str->append(STRING_WITH_LEN(" for system_time from timestamp ")); - select_lex->vers_conditions.start->print(str, query_type); - str->append(STRING_WITH_LEN(" to ")); - select_lex->vers_conditions.end->print(str, query_type); - break; - case FOR_SYSTEM_TIME_BETWEEN: - str->append(STRING_WITH_LEN(" for system_time between timestamp ")); - select_lex->vers_conditions.start->print(str, query_type); - str->append(STRING_WITH_LEN(" and ")); - select_lex->vers_conditions.end->print(str, query_type); - break; - default: - DBUG_ASSERT(0); - } - } -} static void print_table_array(THD *thd, table_map eliminated_tables, @@ -25404,8 +25369,6 @@ void TABLE_LIST::print(THD *thd, table_map eliminated_tables, String *str, append_identifier(thd, str, t_alias, strlen(t_alias)); } - print_system_versioning(thd, eliminated_tables, str, query_type); - if (index_hints) { List_iterator it(*index_hints); @@ -25565,6 +25528,12 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type) str->append(having_value != Item::COND_FALSE ? "1" : "0"); } + if (vers_conditions.type != FOR_SYSTEM_TIME_UNSPECIFIED) + { + // versioning conditions must be already unwrapped to WHERE clause + str->append(STRING_WITH_LEN(" for system_time all ")); + } + if (order_list.elements) { str->append(STRING_WITH_LEN(" order by ")); diff --git a/sql/table.h b/sql/table.h index fc7e639f162..dbcbee290f8 100644 --- a/sql/table.h +++ b/sql/table.h @@ -2334,9 +2334,6 @@ struct TABLE_LIST TABLE_LIST *first_leaf_for_name_resolution(); TABLE_LIST *last_leaf_for_name_resolution(); - /* System Versioning */ - bool vers_moved_to_where; - /** @brief Find the bottom in the chain of embedded table VIEWs. @@ -2535,9 +2532,6 @@ struct TABLE_LIST void check_pushable_cond_for_table(Item *cond); Item *build_pushable_cond_for_table(THD *thd, Item *cond); - void print_system_versioning(THD *thd, table_map eliminated_tables, - String *str, enum_query_type query_type); - private: bool prep_check_option(THD *thd, uint8 check_opt_type); bool prep_where(THD *thd, Item **conds, bool no_where_clause);