Commit 412dd1e1 authored by Aleksey Midenkov's avatar Aleksey Midenkov

SQL: FOR SYSTEM_TIME support in VIEW expression [fixes #99]

parent 2157c6bf
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;
-- 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;
...@@ -899,8 +899,6 @@ vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, SELECT_LEX *s ...@@ -899,8 +899,6 @@ vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, SELECT_LEX *s
*dst_cond= cond1; *dst_cond= cond1;
else else
thd->change_item_tree(dst_cond, cond1); thd->change_item_tree(dst_cond, cond1);
table->vers_moved_to_where= true;
} }
} // if (... table->table->versioned()) } // if (... table->table->versioned())
} // for (table= tables; ...) } // for (table= tables; ...)
...@@ -25036,39 +25034,6 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result) ...@@ -25036,39 +25034,6 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
DBUG_RETURN(res || thd->is_error()); 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, static void print_table_array(THD *thd,
table_map eliminated_tables, table_map eliminated_tables,
...@@ -25404,8 +25369,6 @@ void TABLE_LIST::print(THD *thd, table_map eliminated_tables, String *str, ...@@ -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)); append_identifier(thd, str, t_alias, strlen(t_alias));
} }
print_system_versioning(thd, eliminated_tables, str, query_type);
if (index_hints) if (index_hints)
{ {
List_iterator<Index_hint> it(*index_hints); List_iterator<Index_hint> it(*index_hints);
...@@ -25565,6 +25528,12 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type) ...@@ -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"); 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) if (order_list.elements)
{ {
str->append(STRING_WITH_LEN(" order by ")); str->append(STRING_WITH_LEN(" order by "));
......
...@@ -2334,9 +2334,6 @@ struct TABLE_LIST ...@@ -2334,9 +2334,6 @@ struct TABLE_LIST
TABLE_LIST *first_leaf_for_name_resolution(); TABLE_LIST *first_leaf_for_name_resolution();
TABLE_LIST *last_leaf_for_name_resolution(); TABLE_LIST *last_leaf_for_name_resolution();
/* System Versioning */
bool vers_moved_to_where;
/** /**
@brief @brief
Find the bottom in the chain of embedded table VIEWs. Find the bottom in the chain of embedded table VIEWs.
...@@ -2535,9 +2532,6 @@ struct TABLE_LIST ...@@ -2535,9 +2532,6 @@ struct TABLE_LIST
void check_pushable_cond_for_table(Item *cond); void check_pushable_cond_for_table(Item *cond);
Item *build_pushable_cond_for_table(THD *thd, 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: private:
bool prep_check_option(THD *thd, uint8 check_opt_type); bool prep_check_option(THD *thd, uint8 check_opt_type);
bool prep_where(THD *thd, Item **conds, bool no_where_clause); bool prep_where(THD *thd, Item **conds, bool no_where_clause);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment