mirror of
				https://github.com/MariaDB/server.git
				synced 2025-11-03 14:33:32 +03:00 
			
		
		
		
	s/TRUNCATE ... TO/DELETE HISTORY FROM ... BEFORE/
This commit is contained in:
		
				
					committed by
					
						
						Aleksey Midenkov
					
				
			
			
				
	
			
			
			
						parent
						
							ee68d019d1
						
					
				
				
					commit
					617e108fb6
				
			@@ -1,5 +1,5 @@
 | 
				
			|||||||
create table t (a int);
 | 
					create table t (a int);
 | 
				
			||||||
truncate t to system_time now();
 | 
					delete history from t before system_time now();
 | 
				
			||||||
ERROR HY000: System versioning required: t
 | 
					ERROR HY000: System versioning required: t
 | 
				
			||||||
create or replace table t (a int) with system versioning;
 | 
					create or replace table t (a int) with system versioning;
 | 
				
			||||||
insert into t values (1);
 | 
					insert into t values (1);
 | 
				
			||||||
@@ -7,7 +7,7 @@ update t set a=2;
 | 
				
			|||||||
set @test = 'correct';
 | 
					set @test = 'correct';
 | 
				
			||||||
create trigger trg_before before delete on t for each row set @test = 'incorrect';
 | 
					create trigger trg_before before delete on t for each row set @test = 'incorrect';
 | 
				
			||||||
create trigger trg_after after delete on t for each row set @test = 'incorrect';
 | 
					create trigger trg_after after delete on t for each row set @test = 'incorrect';
 | 
				
			||||||
truncate t to system_time now(6);
 | 
					delete history from t before system_time now(6);
 | 
				
			||||||
select @test from t;
 | 
					select @test from t;
 | 
				
			||||||
@test
 | 
					@test
 | 
				
			||||||
correct
 | 
					correct
 | 
				
			||||||
@@ -23,17 +23,20 @@ a
 | 
				
			|||||||
22
 | 
					22
 | 
				
			||||||
1
 | 
					1
 | 
				
			||||||
2
 | 
					2
 | 
				
			||||||
truncate t to system_time timestamp @ts1;
 | 
					delete history from t before system_time timestamp @ts1;
 | 
				
			||||||
select * from t for system_time all;
 | 
					select * from t for system_time all;
 | 
				
			||||||
a
 | 
					a
 | 
				
			||||||
11
 | 
					11
 | 
				
			||||||
22
 | 
					22
 | 
				
			||||||
 | 
					1
 | 
				
			||||||
2
 | 
					2
 | 
				
			||||||
truncate table t to system_time timestamp now(6);
 | 
					delete history from t before system_time timestamp now(6);
 | 
				
			||||||
select * from t for system_time all;
 | 
					select * from t for system_time all;
 | 
				
			||||||
a
 | 
					a
 | 
				
			||||||
11
 | 
					11
 | 
				
			||||||
22
 | 
					22
 | 
				
			||||||
 | 
					1
 | 
				
			||||||
 | 
					2
 | 
				
			||||||
### Issue #399, truncate partitioned table is now unimplemented
 | 
					### Issue #399, truncate partitioned table is now unimplemented
 | 
				
			||||||
create or replace table t (a int)
 | 
					create or replace table t (a int)
 | 
				
			||||||
with system versioning
 | 
					with system versioning
 | 
				
			||||||
@@ -41,18 +44,18 @@ engine myisam
 | 
				
			|||||||
partition by system_time (
 | 
					partition by system_time (
 | 
				
			||||||
partition p0 history,
 | 
					partition p0 history,
 | 
				
			||||||
partition pn current);
 | 
					partition pn current);
 | 
				
			||||||
truncate table t to system_time current_timestamp;
 | 
					delete history from t before system_time current_timestamp;
 | 
				
			||||||
ERROR 42000: The used command is not allowed with this MariaDB version
 | 
					ERROR 42000: The used command is not allowed with this MariaDB version
 | 
				
			||||||
create or replace table t (i int) with system versioning;
 | 
					create or replace table t (i int) with system versioning;
 | 
				
			||||||
truncate t to system_time now();
 | 
					delete history from t before system_time now();
 | 
				
			||||||
create or replace view v as select * from t;
 | 
					create or replace view v as select * from t;
 | 
				
			||||||
truncate v to system_time now();
 | 
					delete history from v before system_time now();
 | 
				
			||||||
ERROR HY000: TRUNCATE table_name TO doesn't work with VIEWs
 | 
					ERROR HY000: TRUNCATE table_name TO doesn't work with VIEWs
 | 
				
			||||||
create or replace table t (i int);
 | 
					create or replace table t (i int);
 | 
				
			||||||
truncate t to system_time now();
 | 
					delete history from t before system_time now();
 | 
				
			||||||
ERROR HY000: System versioning required: t
 | 
					ERROR HY000: System versioning required: t
 | 
				
			||||||
create or replace view v as select * from t;
 | 
					create or replace view v as select * from t;
 | 
				
			||||||
truncate v to system_time now();
 | 
					delete history from v before system_time now();
 | 
				
			||||||
ERROR HY000: TRUNCATE table_name TO doesn't work with VIEWs
 | 
					ERROR HY000: TRUNCATE table_name TO doesn't work with VIEWs
 | 
				
			||||||
drop table t;
 | 
					drop table t;
 | 
				
			||||||
drop view v;
 | 
					drop view v;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,7 @@ connection user1;
 | 
				
			|||||||
show grants;
 | 
					show grants;
 | 
				
			||||||
Grants for mysqltest_1@localhost
 | 
					Grants for mysqltest_1@localhost
 | 
				
			||||||
GRANT USAGE ON *.* TO 'mysqltest_1'@'localhost'
 | 
					GRANT USAGE ON *.* TO 'mysqltest_1'@'localhost'
 | 
				
			||||||
truncate mysqltest.t to system_time now();
 | 
					delete history from mysqltest.t before system_time now();
 | 
				
			||||||
ERROR 42000: DELETE VERSIONING ROWS command denied to user 'mysqltest_1'@'localhost' for table 't'
 | 
					ERROR 42000: DELETE VERSIONING ROWS command denied to user 'mysqltest_1'@'localhost' for table 't'
 | 
				
			||||||
connection root;
 | 
					connection root;
 | 
				
			||||||
grant delete history on mysqltest.* to mysqltest_1@localhost;
 | 
					grant delete history on mysqltest.* to mysqltest_1@localhost;
 | 
				
			||||||
@@ -21,7 +21,7 @@ Grants for mysqltest_1@localhost
 | 
				
			|||||||
GRANT USAGE ON *.* TO 'mysqltest_1'@'localhost'
 | 
					GRANT USAGE ON *.* TO 'mysqltest_1'@'localhost'
 | 
				
			||||||
GRANT DELETE VERSIONING ROWS ON `mysqltest`.* TO 'mysqltest_1'@'localhost'
 | 
					GRANT DELETE VERSIONING ROWS ON `mysqltest`.* TO 'mysqltest_1'@'localhost'
 | 
				
			||||||
GRANT DELETE VERSIONING ROWS ON `mysqltest`.`t` TO 'mysqltest_1'@'localhost'
 | 
					GRANT DELETE VERSIONING ROWS ON `mysqltest`.`t` TO 'mysqltest_1'@'localhost'
 | 
				
			||||||
truncate mysqltest.t to system_time now();
 | 
					delete history from mysqltest.t before system_time now();
 | 
				
			||||||
connection root;
 | 
					connection root;
 | 
				
			||||||
grant all on *.* to mysqltest_1@localhost;
 | 
					grant all on *.* to mysqltest_1@localhost;
 | 
				
			||||||
show grants for mysqltest_1@localhost;
 | 
					show grants for mysqltest_1@localhost;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
create table t (a int);
 | 
					create table t (a int);
 | 
				
			||||||
--error ER_VERSIONING_REQUIRED
 | 
					--error ER_VERSIONING_REQUIRED
 | 
				
			||||||
truncate t to system_time now();
 | 
					delete history from t before system_time now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# TRUNCATE is not DELETE and trigger must not be called.
 | 
					# TRUNCATE is not DELETE and trigger must not be called.
 | 
				
			||||||
create or replace table t (a int) with system versioning;
 | 
					create or replace table t (a int) with system versioning;
 | 
				
			||||||
@@ -11,7 +11,7 @@ update t set a=2;
 | 
				
			|||||||
set @test = 'correct';
 | 
					set @test = 'correct';
 | 
				
			||||||
create trigger trg_before before delete on t for each row set @test = 'incorrect';
 | 
					create trigger trg_before before delete on t for each row set @test = 'incorrect';
 | 
				
			||||||
create trigger trg_after after delete on t for each row set @test = 'incorrect';
 | 
					create trigger trg_after after delete on t for each row set @test = 'incorrect';
 | 
				
			||||||
truncate t to system_time now(6);
 | 
					delete history from t before system_time now(6);
 | 
				
			||||||
select @test from t;
 | 
					select @test from t;
 | 
				
			||||||
drop table t;
 | 
					drop table t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -23,9 +23,9 @@ set @ts1=now(6);
 | 
				
			|||||||
--real_sleep 0.01
 | 
					--real_sleep 0.01
 | 
				
			||||||
update t set a=22 where a=2;
 | 
					update t set a=22 where a=2;
 | 
				
			||||||
select * from t for system_time all;
 | 
					select * from t for system_time all;
 | 
				
			||||||
truncate t to system_time timestamp @ts1;
 | 
					delete history from t before system_time timestamp @ts1;
 | 
				
			||||||
select * from t for system_time all;
 | 
					select * from t for system_time all;
 | 
				
			||||||
truncate table t to system_time timestamp now(6);
 | 
					delete history from t before system_time timestamp now(6);
 | 
				
			||||||
select * from t for system_time all;
 | 
					select * from t for system_time all;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
--echo ### Issue #399, truncate partitioned table is now unimplemented
 | 
					--echo ### Issue #399, truncate partitioned table is now unimplemented
 | 
				
			||||||
@@ -38,20 +38,20 @@ partition by system_time (
 | 
				
			|||||||
    partition pn current);
 | 
					    partition pn current);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
--error ER_NOT_ALLOWED_COMMAND
 | 
					--error ER_NOT_ALLOWED_COMMAND
 | 
				
			||||||
truncate table t to system_time current_timestamp;
 | 
					delete history from t before system_time current_timestamp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
create or replace table t (i int) with system versioning;
 | 
					create or replace table t (i int) with system versioning;
 | 
				
			||||||
truncate t to system_time now();
 | 
					delete history from t before system_time now();
 | 
				
			||||||
create or replace view v as select * from t;
 | 
					create or replace view v as select * from t;
 | 
				
			||||||
--error ER_VERS_TRUNCATE_TO_VIEW
 | 
					--error ER_VERS_TRUNCATE_TO_VIEW
 | 
				
			||||||
truncate v to system_time now();
 | 
					delete history from v before system_time now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
create or replace table t (i int);
 | 
					create or replace table t (i int);
 | 
				
			||||||
--error ER_VERSIONING_REQUIRED
 | 
					--error ER_VERSIONING_REQUIRED
 | 
				
			||||||
truncate t to system_time now();
 | 
					delete history from t before system_time now();
 | 
				
			||||||
create or replace view v as select * from t;
 | 
					create or replace view v as select * from t;
 | 
				
			||||||
--error ER_VERS_TRUNCATE_TO_VIEW
 | 
					--error ER_VERS_TRUNCATE_TO_VIEW
 | 
				
			||||||
truncate v to system_time now();
 | 
					delete history from v before system_time now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
drop table t;
 | 
					drop table t;
 | 
				
			||||||
drop view v;
 | 
					drop view v;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,7 +23,7 @@ create table mysqltest.t (a int) with system versioning;
 | 
				
			|||||||
connection user1;
 | 
					connection user1;
 | 
				
			||||||
show grants;
 | 
					show grants;
 | 
				
			||||||
--error ER_TABLEACCESS_DENIED_ERROR
 | 
					--error ER_TABLEACCESS_DENIED_ERROR
 | 
				
			||||||
truncate mysqltest.t to system_time now();
 | 
					delete history from mysqltest.t before system_time now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
connection root;
 | 
					connection root;
 | 
				
			||||||
grant delete history on mysqltest.* to mysqltest_1@localhost;
 | 
					grant delete history on mysqltest.* to mysqltest_1@localhost;
 | 
				
			||||||
@@ -31,7 +31,7 @@ grant delete history on mysqltest.t to mysqltest_1@localhost;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
connection user1;
 | 
					connection user1;
 | 
				
			||||||
show grants;
 | 
					show grants;
 | 
				
			||||||
truncate mysqltest.t to system_time now();
 | 
					delete history from mysqltest.t before system_time now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
connection root;
 | 
					connection root;
 | 
				
			||||||
grant all on *.* to mysqltest_1@localhost;
 | 
					grant all on *.* to mysqltest_1@localhost;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9409,10 +9409,18 @@ bool update_precheck(THD *thd, TABLE_LIST *tables)
 | 
				
			|||||||
bool delete_precheck(THD *thd, TABLE_LIST *tables)
 | 
					bool delete_precheck(THD *thd, TABLE_LIST *tables)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  DBUG_ENTER("delete_precheck");
 | 
					  DBUG_ENTER("delete_precheck");
 | 
				
			||||||
 | 
					  if (tables->vers_conditions)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    if (check_one_table_access(thd, DELETE_HISTORY_ACL, tables))
 | 
				
			||||||
 | 
					      DBUG_RETURN(TRUE);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
    if (check_one_table_access(thd, DELETE_ACL, tables))
 | 
					    if (check_one_table_access(thd, DELETE_ACL, tables))
 | 
				
			||||||
      DBUG_RETURN(TRUE);
 | 
					      DBUG_RETURN(TRUE);
 | 
				
			||||||
    /* Set privilege for the WHERE clause */
 | 
					    /* Set privilege for the WHERE clause */
 | 
				
			||||||
    tables->grant.want_privilege=(SELECT_ACL & ~tables->grant.privilege);
 | 
					    tables->grant.want_privilege=(SELECT_ACL & ~tables->grant.privilege);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  DBUG_RETURN(FALSE);
 | 
					  DBUG_RETURN(FALSE);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -496,13 +496,6 @@ bool Sql_cmd_truncate_table::execute(THD *thd)
 | 
				
			|||||||
  TABLE_LIST *table= thd->lex->select_lex.table_list.first;
 | 
					  TABLE_LIST *table= thd->lex->select_lex.table_list.first;
 | 
				
			||||||
  DBUG_ENTER("Sql_cmd_truncate_table::execute");
 | 
					  DBUG_ENTER("Sql_cmd_truncate_table::execute");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (table->vers_conditions)
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
    if (check_one_table_access(thd, DELETE_HISTORY_ACL, table))
 | 
					 | 
				
			||||||
      DBUG_RETURN(res);
 | 
					 | 
				
			||||||
    DBUG_RETURN(mysql_delete(thd, table, NULL, NULL, -1, 0, NULL));
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (check_one_table_access(thd, DROP_ACL, table))
 | 
					  if (check_one_table_access(thd, DROP_ACL, table))
 | 
				
			||||||
    DBUG_RETURN(res);
 | 
					    DBUG_RETURN(res);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -892,10 +892,10 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
 | 
				
			|||||||
%parse-param { THD *thd }
 | 
					%parse-param { THD *thd }
 | 
				
			||||||
%lex-param { THD *thd }
 | 
					%lex-param { THD *thd }
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
  Currently there are 122 shift/reduce conflicts.
 | 
					  Currently there are 123 shift/reduce conflicts.
 | 
				
			||||||
  We should not introduce new conflicts any more.
 | 
					  We should not introduce new conflicts any more.
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
%expect 122
 | 
					%expect 123
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
   Comments for TOKENS.
 | 
					   Comments for TOKENS.
 | 
				
			||||||
@@ -13404,10 +13404,20 @@ delete:
 | 
				
			|||||||
            lex->ignore= 0;
 | 
					            lex->ignore= 0;
 | 
				
			||||||
            lex->select_lex.init_order();
 | 
					            lex->select_lex.init_order();
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          opt_delete_options single_multi
 | 
					          delete_part2
 | 
				
			||||||
          ;
 | 
					          ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
single_multi:
 | 
					delete_part2:
 | 
				
			||||||
 | 
					          opt_delete_options single_multi {}
 | 
				
			||||||
 | 
					        | HISTORY_SYM delete_single_table
 | 
				
			||||||
 | 
					          BEFORE_SYM SYSTEM_TIME_SYM opt_trans_or_timestamp simple_expr
 | 
				
			||||||
 | 
					          {
 | 
				
			||||||
 | 
					            Lex->vers_conditions.init(SYSTEM_TIME_BEFORE, $5, $6);
 | 
				
			||||||
 | 
					            Lex->last_table()->vers_conditions= Lex->vers_conditions;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					delete_single_table:
 | 
				
			||||||
          FROM table_ident opt_use_partition
 | 
					          FROM table_ident opt_use_partition
 | 
				
			||||||
          {
 | 
					          {
 | 
				
			||||||
            if (!Select->add_table_to_list(thd, $2, NULL, TL_OPTION_UPDATING,
 | 
					            if (!Select->add_table_to_list(thd, $2, NULL, TL_OPTION_UPDATING,
 | 
				
			||||||
@@ -13419,8 +13429,13 @@ single_multi:
 | 
				
			|||||||
            YYPS->m_lock_type= TL_READ_DEFAULT;
 | 
					            YYPS->m_lock_type= TL_READ_DEFAULT;
 | 
				
			||||||
            YYPS->m_mdl_type= MDL_SHARED_READ;
 | 
					            YYPS->m_mdl_type= MDL_SHARED_READ;
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          opt_where_clause opt_order_clause
 | 
					        ;
 | 
				
			||||||
          delete_limit_clause {}
 | 
					
 | 
				
			||||||
 | 
					single_multi:
 | 
				
			||||||
 | 
					          delete_single_table
 | 
				
			||||||
 | 
					          opt_where_clause
 | 
				
			||||||
 | 
					          opt_order_clause
 | 
				
			||||||
 | 
					          delete_limit_clause
 | 
				
			||||||
          opt_select_expressions {}
 | 
					          opt_select_expressions {}
 | 
				
			||||||
        | table_wild_list
 | 
					        | table_wild_list
 | 
				
			||||||
          {
 | 
					          {
 | 
				
			||||||
@@ -13501,15 +13516,6 @@ opt_delete_option:
 | 
				
			|||||||
        | IGNORE_SYM   { Lex->ignore= 1; }
 | 
					        | IGNORE_SYM   { Lex->ignore= 1; }
 | 
				
			||||||
        ;
 | 
					        ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
truncate_end:
 | 
					 | 
				
			||||||
          opt_lock_wait_timeout
 | 
					 | 
				
			||||||
          | TO_SYM SYSTEM_TIME_SYM opt_trans_or_timestamp simple_expr
 | 
					 | 
				
			||||||
          {
 | 
					 | 
				
			||||||
            Lex->vers_conditions.init(SYSTEM_TIME_BEFORE, $3, $4);
 | 
					 | 
				
			||||||
            Lex->last_table()->vers_conditions= Lex->vers_conditions;
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
        ;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
truncate:
 | 
					truncate:
 | 
				
			||||||
          TRUNCATE_SYM
 | 
					          TRUNCATE_SYM
 | 
				
			||||||
          {
 | 
					          {
 | 
				
			||||||
@@ -13522,7 +13528,7 @@ truncate:
 | 
				
			|||||||
            YYPS->m_lock_type= TL_WRITE;
 | 
					            YYPS->m_lock_type= TL_WRITE;
 | 
				
			||||||
            YYPS->m_mdl_type= MDL_EXCLUSIVE;
 | 
					            YYPS->m_mdl_type= MDL_EXCLUSIVE;
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          opt_table_sym table_name truncate_end
 | 
					          opt_table_sym table_name opt_lock_wait_timeout
 | 
				
			||||||
          {
 | 
					          {
 | 
				
			||||||
            LEX* lex= thd->lex;
 | 
					            LEX* lex= thd->lex;
 | 
				
			||||||
            DBUG_ASSERT(!lex->m_sql_cmd);
 | 
					            DBUG_ASSERT(!lex->m_sql_cmd);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user