summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorkevg <claprix@yandex.ru>2016-12-23 17:05:57 +0300
committerAleksey Midenkov <midenok@gmail.com>2017-05-05 20:36:24 +0300
commitc9e4ac4b7221ab58097ce16e91eb1b885f2a3485 (patch)
tree383f3101b7dd19810ccfca6a4ba1227adebfbd3d /sql
parent4c37011582b4581bb122293ba30a6956b573483c (diff)
downloadmariadb-git-c9e4ac4b7221ab58097ce16e91eb1b885f2a3485.tar.gz
0.6: truncate history feature [closes #96]
Diffstat (limited to 'sql')
-rw-r--r--sql/handler.cc2
-rw-r--r--sql/handler.h2
-rw-r--r--sql/share/errmsg-utf8.txt3
-rw-r--r--sql/sql_delete.cc58
-rw-r--r--sql/sql_select.cc4
-rw-r--r--sql/sql_select.h3
-rw-r--r--sql/sql_truncate.cc9
-rw-r--r--sql/sql_yacc.yy3
8 files changed, 61 insertions, 23 deletions
diff --git a/sql/handler.cc b/sql/handler.cc
index 0712d3e5798..98f558f50cc 100644
--- a/sql/handler.cc
+++ b/sql/handler.cc
@@ -5659,7 +5659,7 @@ bool ha_show_status(THD *thd, handlerton *db_type, enum ha_stat_type stat)
1 Row needs to be logged
*/
-inline bool handler::check_table_binlog_row_based(bool binlog_row)
+bool handler::check_table_binlog_row_based(bool binlog_row)
{
if (unlikely((table->in_use->variables.sql_log_bin_off)))
return 0; /* Called by partitioning engine */
diff --git a/sql/handler.h b/sql/handler.h
index 5bcacf1f50b..56ee1addd6d 100644
--- a/sql/handler.h
+++ b/sql/handler.h
@@ -4079,7 +4079,7 @@ protected:
virtual int delete_table(const char *name);
public:
- inline bool check_table_binlog_row_based(bool binlog_row);
+ bool check_table_binlog_row_based(bool binlog_row);
private:
/* Cache result to avoid extra calls */
inline void mark_trx_read_write()
diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt
index c25b8b655cd..daca9b4ce52 100644
--- a/sql/share/errmsg-utf8.txt
+++ b/sql/share/errmsg-utf8.txt
@@ -7522,3 +7522,6 @@ WARN_VERS_PARAMETERS
WARN_VERS_PART_ROTATION
eng "Switching from partition %`s to %`s"
+
+ER_VERS_NOT_ALLOWED
+ eng "%`s is not allowed for versioned table"
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 775d0c0d023..5d62b54b297 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -215,19 +215,13 @@ void Update_plan::save_explain_data_intern(MEM_ROOT *mem_root,
inline
int TABLE::delete_row()
{
- int error;
- if (!versioned_by_sql())
- error= file->ha_delete_row(record[0]);
- else
- {
- store_record(this, record[1]);
- Field *sys_trx_end= vers_end_field();
- sys_trx_end->set_time();
- error= file->ha_update_row(record[1], record[0]);
- }
- return error;
-}
+ if (!versioned_by_sql() || !vers_end_field()->is_max())
+ return file->ha_delete_row(record[0]);
+ store_record(this, record[1]);
+ vers_end_field()->set_time();
+ return file->ha_update_row(record[1], record[0]);
+}
/**
Implement DELETE SQL word.
@@ -269,6 +263,34 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
if (open_and_lock_tables(thd, table_list, TRUE, 0))
DBUG_RETURN(TRUE);
+ bool truncate_history=
+ select_lex->vers_conditions.type != FOR_SYSTEM_TIME_UNSPECIFIED;
+ if (truncate_history)
+ {
+ TABLE *table= table_list->table;
+ DBUG_ASSERT(table);
+
+ if (table->versioned_by_engine() &&
+ table->file->check_table_binlog_row_based(1))
+ {
+ my_error(ER_VERS_NOT_ALLOWED, MYF(0),
+ "TRUNCATE FOR SYSTEM_TIME with row-based replication");
+ DBUG_RETURN(TRUE);
+ }
+
+ DBUG_ASSERT(!conds);
+ if (vers_setup_select(thd, table_list, &conds, select_lex))
+ DBUG_RETURN(TRUE);
+
+ // trx_sees() in InnoDB reads sys_trx_start
+ if (!table->versioned_by_sql() &&
+ (select_lex->vers_conditions.type == FOR_SYSTEM_TIME_BETWEEN ||
+ select_lex->vers_conditions.type == FOR_SYSTEM_TIME_FROM_TO))
+ {
+ bitmap_set_bit(table->read_set, table->vers_start_field()->field_index);
+ }
+ }
+
if (mysql_handle_list_of_derived(thd->lex, table_list, DT_MERGE_FOR_INSERT))
DBUG_RETURN(TRUE);
if (mysql_handle_list_of_derived(thd->lex, table_list, DT_PREPARE))
@@ -577,9 +599,13 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
while (!(error=info.read_record(&info)) && !thd->killed &&
! thd->is_error())
{
- if (table->versioned() && !table->vers_end_field()->is_max())
+ if (table->versioned())
{
- continue;
+ bool row_is_alive= table->vers_end_field()->is_max();
+ if (truncate_history && row_is_alive)
+ continue;
+ if (!truncate_history && !row_is_alive)
+ continue;
}
explain->tracker.on_record_read();
@@ -589,7 +615,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
if (!select || select->skip_record(thd) > 0)
{
explain->tracker.on_record_after_where();
- if (table->triggers &&
+ if (!truncate_history && table->triggers &&
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
TRG_ACTION_BEFORE, FALSE))
{
@@ -607,7 +633,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
if (!error)
{
deleted++;
- if (table->triggers &&
+ if (!truncate_history && table->triggers &&
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
TRG_ACTION_AFTER, FALSE))
{
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 4dc67267b74..6655f71e10a 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -667,8 +667,8 @@ setup_without_group(THD *thd, Ref_ptr_array ref_pointer_array,
DBUG_RETURN(res);
}
-static int
-vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, SELECT_LEX *slex)
+int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
+ SELECT_LEX *slex)
{
DBUG_ENTER("vers_setup_select");
#define newx new (thd->mem_root)
diff --git a/sql/sql_select.h b/sql/sql_select.h
index 77cf73d785f..aa238c9b741 100644
--- a/sql/sql_select.h
+++ b/sql/sql_select.h
@@ -2301,4 +2301,7 @@ int create_sort_index(THD *thd, JOIN *join, JOIN_TAB *tab, Filesort *fsort);
JOIN_TAB *first_explain_order_tab(JOIN* join);
JOIN_TAB *next_explain_order_tab(JOIN* join, JOIN_TAB* tab);
+int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
+ SELECT_LEX *slex);
+
#endif /* SQL_SELECT_INCLUDED */
diff --git a/sql/sql_truncate.cc b/sql/sql_truncate.cc
index daa295d768e..4faad5b4711 100644
--- a/sql/sql_truncate.cc
+++ b/sql/sql_truncate.cc
@@ -26,7 +26,8 @@
#include "sql_truncate.h"
#include "wsrep_mysqld.h"
#include "sql_show.h" //append_identifier()
-
+#include "sql_select.h"
+#include "sql_delete.h"
/**
Append a list of field names to a string.
@@ -480,7 +481,6 @@ bool Sql_cmd_truncate_table::truncate_table(THD *thd, TABLE_LIST *table_ref)
DBUG_RETURN(error);
}
-
/**
Execute a TRUNCATE statement at runtime.
@@ -495,6 +495,11 @@ bool Sql_cmd_truncate_table::execute(THD *thd)
TABLE_LIST *first_table= thd->lex->select_lex.table_list.first;
DBUG_ENTER("Sql_cmd_truncate_table::execute");
+ bool truncate_history= thd->lex->current_select->vers_conditions.type !=
+ FOR_SYSTEM_TIME_UNSPECIFIED;
+ if (truncate_history)
+ DBUG_RETURN(mysql_delete(thd, first_table, NULL, NULL, -1, 0, NULL));
+
if (check_one_table_access(thd, DROP_ACL, first_table))
DBUG_RETURN(res);
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index f591566d91a..4c781a6afd1 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -12998,8 +12998,9 @@ truncate:
lex->select_lex.init_order();
YYPS->m_lock_type= TL_WRITE;
YYPS->m_mdl_type= MDL_EXCLUSIVE;
+ Select->vers_conditions.empty();
}
- table_name opt_lock_wait_timeout
+ table_name opt_for_system_time_clause opt_lock_wait_timeout
{
LEX* lex= thd->lex;
DBUG_ASSERT(!lex->m_sql_cmd);