Skip to content

Commit 6789f2c

Browse files
committed
MDEV-18304 sql_safe_updates does not work with OR clauses
not every index-using plan sets bits in table->quick_keys. QUICK_ROR_INTERSECT_SELECT, for example, doesn't. Use the fact that select->quick is set instead. Also allow EXPLAIN to work.
1 parent dc680d2 commit 6789f2c

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
#
2+
# MDEV-14429 sql_safe_updates in my.cnf not work
3+
#
14
select @@sql_safe_updates;
25
@@sql_safe_updates
36
1
7+
#
8+
# MDEV-18304 sql_safe_updates does not work with OR clauses
9+
#
10+
create table t1 (a int, b int, primary key (a), key (b));
11+
update t1 set b=2 where a=1 or b=2;
12+
ERROR HY000: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
13+
explain update t1 set b=2 where a=1 or b=2;
14+
id select_type table type possible_keys key key_len ref rows Extra
15+
1 SIMPLE t1 ALL NULL NULL NULL NULL 1 Using where
16+
delete from t1 where a=1 or b=2;
17+
ERROR HY000: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
18+
explain delete from t1 where a=1 or b=2;
19+
id select_type table type possible_keys key key_len ref rows Extra
20+
1 SIMPLE t1 ALL NULL NULL NULL NULL 1 Using where
21+
insert into t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8);
22+
update t1 set b=2 where a=1 or b=2;
23+
delete from t1 where a=1 or b=2;
24+
drop table t1;
25+
#
26+
# End of 10.3 tests
27+
#
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
#
2-
# MDEV-14429 sql_safe_updates in my.cnf not work
3-
#
1+
--echo #
2+
--echo # MDEV-14429 sql_safe_updates in my.cnf not work
3+
--echo #
44
select @@sql_safe_updates;
5+
6+
--echo #
7+
--echo # MDEV-18304 sql_safe_updates does not work with OR clauses
8+
--echo #
9+
create table t1 (a int, b int, primary key (a), key (b));
10+
--error ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE
11+
update t1 set b=2 where a=1 or b=2;
12+
explain update t1 set b=2 where a=1 or b=2;
13+
--error ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE
14+
delete from t1 where a=1 or b=2;
15+
explain delete from t1 where a=1 or b=2;
16+
insert into t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8);
17+
update t1 set b=2 where a=1 or b=2;
18+
delete from t1 where a=1 or b=2;
19+
drop table t1;
20+
21+
--echo #
22+
--echo # End of 10.3 tests
23+
--echo #

sql/sql_delete.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
371371
DBUG_RETURN(TRUE);
372372

373373
const_cond= (!conds || conds->const_item());
374-
safe_update= MY_TEST(thd->variables.option_bits & OPTION_SAFE_UPDATES);
374+
safe_update= (thd->variables.option_bits & OPTION_SAFE_UPDATES) &&
375+
!thd->lex->describe;
375376
if (safe_update && const_cond)
376377
{
377378
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
@@ -497,7 +498,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
497498
}
498499

499500
/* If running in safe sql mode, don't allow updates without keys */
500-
if (table->quick_keys.is_clear_all())
501+
if (!select || !select->quick)
501502
{
502503
thd->set_status_no_index_used();
503504
if (safe_update && !using_limit)

sql/sql_update.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ int mysql_update(THD *thd,
314314
ha_rows *found_return, ha_rows *updated_return)
315315
{
316316
boolusing_limit= limit != HA_POS_ERROR;
317-
bool safe_update= thd->variables.option_bits & OPTION_SAFE_UPDATES;
317+
bool safe_update= (thd->variables.option_bits & OPTION_SAFE_UPDATES)
318+
&& !thd->lex->describe;
318319
bool used_key_is_modified= FALSE, transactional_table;
319320
bool will_batch= FALSE;
320321
boolcan_compare_record;
@@ -517,7 +518,7 @@ int mysql_update(THD *thd,
517518
}
518519

519520
/* If running in safe sql mode, don't allow updates without keys */
520-
if (table->quick_keys.is_clear_all())
521+
if (!select || !select->quick)
521522
{
522523
thd->set_status_no_index_used();
523524
if (safe_update && !using_limit)

0 commit comments

Comments
 (0)