Commit ac5eb977 authored by Eugene Kosov's avatar Eugene Kosov Committed by Aleksey Midenkov

SQL: Versioned SHOW CREATE TABLE [closes #125]

parent 448374a2
......@@ -202,6 +202,7 @@ No A B C D
11 1 1 1 1
12 1 1 1 1
drop table t;
drop table t_vtmd;
drop procedure verify_vtq;
drop procedure innodb_verify_vtq;
drop function default_engine;
......
create or replace procedure drop_archives (in vtmd_name varchar(64))
begin
declare archive_name varchar(64);
declare cur_done bool default false;
declare cur cursor for
select cur_tmp.archive_name from cur_tmp;
declare continue handler for not found set cur_done = true;
set @tmp= concat('
create or replace temporary table
cur_tmp as
select vtmd.archive_name from ', vtmd_name, ' as vtmd
for system_time all
where vtmd.archive_name is not null
group by vtmd.archive_name');
prepare stmt from @tmp; execute stmt; drop prepare stmt;
open cur;
fetch_loop: loop
fetch cur into archive_name;
if cur_done then
leave fetch_loop;
end if;
set @tmp= concat('drop table ', archive_name);
prepare stmt from @tmp; execute stmt; drop prepare stmt;
end loop;
drop table cur_tmp;
end~~
create procedure test_01(in engine varchar(64))
begin
set @tmp = concat('create table t (a int) with system versioning engine ', engine);
prepare stmt from @tmp; execute stmt; drop prepare stmt;
set @tm1 = now(6);
alter table t add column b int;
set @tm2 = now(6);
alter table t add column c int;
show create table t for system_time as of timestamp @tm1;
show create table t for system_time as of timestamp @tm2;
show create table t for system_time as of now;
show create table t for system_time as of timestamp now(6);
show create table t;
set @tm3 = now(6);
rename table t to tt;
show create table tt for system_time as of timestamp @tm3;
set @tm4 = now(6);
alter table tt add column d int;
show create table tt for system_time as of timestamp @tm3;
show create table tt for system_time as of timestamp @tm4;
show create table tt;
drop table tt;
call drop_archives('tt_vtmd');
drop table tt_vtmd;
end~~
create table t (a int) with system versioning;
show create table t for system_time as of now;
ERROR 42S02: Table 'test.t_vtmd' doesn't exist
set versioning_ddl_survival=on;
create or replace table t (a int) with system versioning;
show create table t for system_time between timestamp @tm1 and timestamp @tm1;
ERROR HY000: Wrong parameters for `FOR SYSTEM_TIME`: only AS OF allowed here
show create table t for system_time from timestamp @tm1 to timestamp @tm1;
ERROR HY000: Wrong parameters for `FOR SYSTEM_TIME`: only AS OF allowed here
show create table t for system_time before timestamp @tm1;
ERROR HY000: Wrong parameters for `FOR SYSTEM_TIME`: only AS OF allowed here
show create table t for system_time as of timestamp '01-01-1990';
ERROR HY000: VTMD error: failed to query VTMD table
show create table t for system_time as of timestamp '01-01-2020';
ERROR HY000: VTMD error: failed to query VTMD table
drop table t;
call drop_archives('t_vtmd');
drop table t_vtmd;
call test_01('myisam');
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
tt CREATE TABLE `tt` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
tt CREATE TABLE `tt` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
tt CREATE TABLE `tt` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
tt CREATE TABLE `tt` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
call test_01('innodb');
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
tt CREATE TABLE `tt` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
tt CREATE TABLE `tt` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
tt CREATE TABLE `tt` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
Table Create Table
tt CREATE TABLE `tt` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` bigint(20) unsigned GENERATED ALWAYS AS ROW START,
`sys_trx_end` bigint(20) unsigned GENERATED ALWAYS AS ROW END,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
drop procedure test_01;
drop procedure drop_archives;
......@@ -118,5 +118,6 @@ show create table mysql.vtmd_template;
call verify_vtq;
drop table t;
drop table t_vtmd;
-- source suite/versioning/common_finish.inc
delimiter ~~;
create or replace procedure drop_archives (in vtmd_name varchar(64))
begin
declare archive_name varchar(64);
declare cur_done bool default false;
declare cur cursor for
select cur_tmp.archive_name from cur_tmp;
declare continue handler for not found set cur_done = true;
set @tmp= concat('
create or replace temporary table
cur_tmp as
select vtmd.archive_name from ', vtmd_name, ' as vtmd
for system_time all
where vtmd.archive_name is not null
group by vtmd.archive_name');
prepare stmt from @tmp; execute stmt; drop prepare stmt;
open cur;
fetch_loop: loop
fetch cur into archive_name;
if cur_done then
leave fetch_loop;
end if;
set @tmp= concat('drop table ', archive_name);
prepare stmt from @tmp; execute stmt; drop prepare stmt;
end loop;
drop table cur_tmp;
end~~
delimiter ;~~
delimiter ~~;
create procedure test_01(in engine varchar(64))
begin
set @tmp = concat('create table t (a int) with system versioning engine ', engine);
prepare stmt from @tmp; execute stmt; drop prepare stmt;
set @tm1 = now(6);
alter table t add column b int;
set @tm2 = now(6);
alter table t add column c int;
show create table t for system_time as of timestamp @tm1;
show create table t for system_time as of timestamp @tm2;
show create table t for system_time as of now;
show create table t for system_time as of timestamp now(6);
show create table t;
set @tm3 = now(6);
rename table t to tt;
show create table tt for system_time as of timestamp @tm3;
set @tm4 = now(6);
alter table tt add column d int;
show create table tt for system_time as of timestamp @tm3;
show create table tt for system_time as of timestamp @tm4;
show create table tt;
drop table tt;
call drop_archives('tt_vtmd');
drop table tt_vtmd;
end~~
delimiter ;~~
create table t (a int) with system versioning;
--error ER_NO_SUCH_TABLE
show create table t for system_time as of now;
set versioning_ddl_survival=on;
create or replace table t (a int) with system versioning;
--error ER_VERS_WRONG_PARAMS
show create table t for system_time between timestamp @tm1 and timestamp @tm1;
--error ER_VERS_WRONG_PARAMS
show create table t for system_time from timestamp @tm1 to timestamp @tm1;
--error ER_VERS_WRONG_PARAMS
show create table t for system_time before timestamp @tm1;
--error ER_VERS_VTMD_ERROR
show create table t for system_time as of timestamp '01-01-1990';
--error ER_VERS_VTMD_ERROR
show create table t for system_time as of timestamp '01-01-2020';
drop table t;
call drop_archives('t_vtmd');
drop table t_vtmd;
call test_01('myisam');
call test_01('innodb');
drop procedure test_01;
drop procedure drop_archives;
......@@ -62,6 +62,8 @@
#ifdef WITH_PARTITION_STORAGE_ENGINE
#include "ha_partition.h"
#endif
#include "vtmd.h"
#include "transaction.h"
enum enum_i_s_events_fields
{
......@@ -1267,6 +1269,24 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list)
*/
MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
TABLE_LIST tl;
bool versioned_query=
table_list->vers_conditions.type != FOR_SYSTEM_TIME_UNSPECIFIED;
String archive_name;
if (versioned_query)
{
DBUG_ASSERT(table_list->vers_conditions.type == FOR_SYSTEM_TIME_AS_OF);
VTMD_table vtmd(*table_list);
if (vtmd.find_archive_name(thd, archive_name))
goto exit;
tl.init_one_table(table_list->db, table_list->db_length, archive_name.ptr(),
archive_name.length(), archive_name.ptr(), TL_READ);
tl.alias= table_list->table_name;
tl.vers_force_alias= true;
table_list= &tl;
}
if (mysqld_show_create_get_fields(thd, table_list, &field_list, &buffer))
goto exit;
......@@ -1281,7 +1301,9 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list)
protocol->store(table_list->view_name.str, system_charset_info);
else
{
if (table_list->schema_table)
if (versioned_query)
protocol->store(tl.alias, system_charset_info);
else if (table_list->schema_table)
protocol->store(table_list->schema_table->table_name,
system_charset_info);
else
......@@ -1309,6 +1331,13 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list)
my_eof(thd);
exit:
if (versioned_query)
{
/* If commit fails, we should be able to reset the OK status. */
thd->get_stmt_da()->set_overwrite_status(true);
trans_commit_stmt(thd);
thd->get_stmt_da()->set_overwrite_status(false);
}
close_thread_tables(thd);
/* Release any metadata locks taken during SHOW CREATE. */
thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
......@@ -2026,7 +2055,7 @@ int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
alias= table_list->schema_table->table_name;
else
{
if (lower_case_table_names == 2)
if (lower_case_table_names == 2 || table_list->vers_force_alias)
alias= table->alias.c_ptr();
else
{
......
......@@ -1905,7 +1905,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
parse_vcol_expr vcol_opt_specifier vcol_opt_attribute
vcol_opt_attribute_list vcol_attribute
opt_serial_attribute opt_serial_attribute_list serial_attribute
explainable_command opt_lock_wait_timeout
explainable_command opt_lock_wait_timeout asrow_attribute
END_OF_INPUT
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
......@@ -13330,13 +13330,21 @@ show_param:
Lex->set_command(SQLCOM_SHOW_CREATE_DB, $3);
Lex->name= $4;
}
| CREATE TABLE_SYM table_ident
| CREATE TABLE_SYM table_ident opt_for_system_time_clause
{
LEX *lex= Lex;
lex->sql_command = SQLCOM_SHOW_CREATE;
if (!lex->select_lex.add_table_to_list(thd, $3, NULL,0))
MYSQL_YYABORT;
lex->create_info.storage_media= HA_SM_DEFAULT;
if (lex->vers_conditions.type != FOR_SYSTEM_TIME_UNSPECIFIED &&
lex->vers_conditions.type != FOR_SYSTEM_TIME_AS_OF) {
my_yyabort_error((ER_VERS_WRONG_PARAMS, MYF(0), "FOR SYSTEM_TIME",
"only AS OF allowed here"));
}
if ($4)
Lex->last_table()->vers_conditions= Lex->vers_conditions;
}
| CREATE VIEW_SYM table_ident
{
......
......@@ -1890,7 +1890,7 @@ struct vers_select_conds_t
{
return type != b;
}
operator bool()
operator bool() const
{
return type != FOR_SYSTEM_TIME_UNSPECIFIED;
}
......@@ -2356,6 +2356,7 @@ struct TABLE_LIST
/* System Versioning */
vers_select_conds_t vers_conditions;
bool vers_vtmd_name(String &out) const;
bool vers_force_alias;
/**
@brief
......
......@@ -3,6 +3,7 @@
#include "sql_class.h"
#include "sql_handler.h" // mysql_ha_rm_tables()
#include "sql_table.h"
#include "sql_select.h"
#include "table_cache.h" // tdc_remove_table()
#include "key.h"
......@@ -493,3 +494,81 @@ void VTMD_table::archive_name(
table_name, now.year, now.month, now.day, now.hour, now.minute,
now.second, now.second_part);
}
bool VTMD_table::find_archive_name(THD *thd, String &out)
{
String vtmd_name;
if (about.vers_vtmd_name(vtmd_name))
return true;
READ_RECORD info;
int error= 0;
SQL_SELECT *select= NULL;
COND *conds= NULL;
List<TABLE_LIST> dummy;
SELECT_LEX &select_lex= thd->lex->select_lex;
TABLE_LIST tl;
tl.init_one_table(about.db, about.db_length, vtmd_name.ptr(),
vtmd_name.length(), vtmd_name.ptr(), TL_READ);
Open_tables_backup open_tables_backup;
if (!(vtmd= open_log_table(thd, &tl, &open_tables_backup)))
{
my_error(ER_VERS_VTMD_ERROR, MYF(0), "failed to open VTMD table");
return true;
}
Name_resolution_context &ctx= thd->lex->select_lex.context;
TABLE_LIST *table_list= ctx.table_list;
TABLE_LIST *first_name_resolution_table= ctx.first_name_resolution_table;
table_map map = tl.table->map;
ctx.table_list= &tl;
ctx.first_name_resolution_table= &tl;
tl.table->map= 1;
tl.vers_conditions= about.vers_conditions;
if ((error= vers_setup_select(thd, &tl, &conds, &select_lex)) ||
(error= setup_conds(thd, &tl, dummy, &conds)))
{
my_error(ER_VERS_VTMD_ERROR, MYF(0),
"failed to setup conditions for querying VTMD table");
goto err;
}
select= make_select(tl.table, 0, 0, conds, NULL, 0, &error);
if (error)
goto loc_err;
if ((error=
init_read_record(&info, thd, tl.table, select, NULL, 1, 1, false)))
goto loc_err;
while (!(error= info.read_record(&info)) && !thd->killed && !thd->is_error())
{
if (select->skip_record(thd) > 0)
{
tl.table->field[FLD_ARCHIVE_NAME]->val_str(&out);
if (out.length() == 0)
{
// Handle AS OF NOW or just RENAMEd case
out.set(about.table_name, about.table_name_length,
system_charset_info);
}
break;
}
}
loc_err:
if (error)
my_error(ER_VERS_VTMD_ERROR, MYF(0), "failed to query VTMD table");
end_read_record(&info);
err:
delete select;
ctx.table_list= table_list;
ctx.first_name_resolution_table= first_name_resolution_table;
tl.table->map= map;
close_log_table(thd, &open_tables_backup);
return error ? true : false;
}
......@@ -86,6 +86,7 @@ class VTMD_table
archive_name(thd, about.table_name, new_name, new_name_size);
}
bool find_archive_name(THD *thd, String &out);
};
class VTMD_exists : public VTMD_table
......
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