Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
SQL: SELECT from archive table [closes #127]
  • Loading branch information
midenok committed Sep 28, 2017
1 parent c5801dd commit 5e42511
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
8 changes: 8 additions & 0 deletions mysql-test/suite/versioning/r/vtmd.result
Expand Up @@ -86,7 +86,15 @@ ERROR HY000: VTMD error: `test.t0_vtmd` exists and not empty!
drop table t0_vtmd;
create table t0 (y int) with system versioning;
create or replace table t0 (x int) with system versioning;
insert into t0 values (1);
set @t0= now(6);
alter table t0 add column (y int);
select * from t0 for system_time as of @t0;
x
1
select * from t0;
x y
1 NULL
call check_vtmd('t0_vtmd');
@start > 0 and @start < @inf
1
Expand Down
4 changes: 4 additions & 0 deletions mysql-test/suite/versioning/t/vtmd.test
Expand Up @@ -86,7 +86,11 @@ create table t0 (y int) with system versioning;
create or replace table t0 (x int) with system versioning;

# alter
insert into t0 values (1);
set @t0= now(6);
alter table t0 add column (y int);
select * from t0 for system_time as of @t0;
select * from t0;
call check_vtmd('t0_vtmd');

call drop_archives('t0_vtmd');
Expand Down
16 changes: 16 additions & 0 deletions sql/sql_parse.cc
Expand Up @@ -113,6 +113,7 @@

#include "wsrep_mysqld.h"
#include "wsrep_thd.h"
#include "vtmd.h"

static void wsrep_mysql_parse(THD *thd, char *rawbuf, uint length,
Parser_state *parser_state,
Expand Down Expand Up @@ -6373,6 +6374,21 @@ static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
if (check_dependencies_in_with_clauses(lex->with_clauses_list))
return 1;

if (thd->variables.vers_alter_history == VERS_ALTER_HISTORY_SURVIVE)
{
for (TABLE_LIST *table= all_tables; table; table= table->next_local)
{
if (table->vers_conditions)
{
VTMD_exists vtmd(*table);
if (vtmd.check_exists(thd))
return 1;
if (vtmd.exists && vtmd.setup_select(thd))
return 1;
}
}
}

if (!(res= open_and_lock_tables(thd, all_tables, TRUE, 0)))
{
if (lex->describe)
Expand Down
13 changes: 8 additions & 5 deletions sql/sql_show.cc
Expand Up @@ -1280,12 +1280,15 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list)
if (vtmd.find_archive_name(thd, archive_name))
goto exit;

archive.init_one_table(table_list->db, table_list->db_length, archive_name.ptr(),
archive_name.length(), archive_name.ptr(), TL_READ);
if (archive_name.length() > 0)
{
archive.init_one_table(table_list->db, table_list->db_length, archive_name.ptr(),
archive_name.length(), archive_name.ptr(), TL_READ);

archive.alias= table_list->table_name;
archive.vers_force_alias= true;
table_list= &archive;
archive.alias= table_list->table_name;
archive.vers_force_alias= true;
table_list= &archive;
}
}

if (mysqld_show_create_get_fields(thd, table_list, &field_list, &buffer))
Expand Down
21 changes: 19 additions & 2 deletions sql/vtmd.cc
Expand Up @@ -7,6 +7,8 @@
#include "table_cache.h" // tdc_remove_table()
#include "key.h"
#include "sql_show.h"
#include "sql_parse.h"
#include "sql_lex.h"

LString VERS_VTMD_TEMPLATE(C_STRING_WITH_LEN("vtmd_template"));

Expand Down Expand Up @@ -546,8 +548,6 @@ VTMD_table::find_archive_name(THD *thd, String &out)
if (select->skip_record(thd) > 0)
{
vtmd.table->field[FLD_ARCHIVE_NAME]->val_str(&out);
if (out.length() == 0) // Handle AS OF NOW or RENAME TABLE case
out.set(about.table_name, about.table_name_length, system_charset_info);
break;
}
}
Expand Down Expand Up @@ -644,3 +644,20 @@ VTMD_table::get_archive_tables(THD *thd, const char *db, size_t db_length,

return false;
}

bool VTMD_table::setup_select(THD* thd)
{
SString archive_name;
if (find_archive_name(thd, archive_name))
return true;

if (archive_name.length() == 0)
return false;

about.table_name= (char *) thd->memdup(archive_name.c_ptr_safe(), archive_name.length() + 1);
about.table_name_length= archive_name.length();
DBUG_ASSERT(!about.mdl_request.ticket);
about.mdl_request.init(MDL_key::TABLE, about.db, about.table_name,
about.mdl_request.type, about.mdl_request.duration);
return false;
}
2 changes: 1 addition & 1 deletion sql/vtmd.h
Expand Up @@ -52,7 +52,7 @@ class VTMD_table

protected:
TABLE_LIST vtmd;
const TABLE_LIST &about;
TABLE_LIST &about;
SString_t vtmd_name;

private:
Expand Down

0 comments on commit 5e42511

Please sign in to comment.