summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergei Golubchik <serg@mariadb.org>2021-10-12 18:34:51 +0200
committerSergei Golubchik <serg@mariadb.org>2022-03-12 19:13:17 +0100
commit6789f2cfabb6a92b17ce822921cb522098ef3ff7 (patch)
tree104a4ebfb3a2bb6665614c4eb8e64ffc4a25e8ca
parentdc680d2119d3cc4ad642e5c1f11f38ab072b3032 (diff)
downloadmariadb-git-6789f2cfabb6a92b17ce822921cb522098ef3ff7.tar.gz
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.
-rw-r--r--mysql-test/main/sql_safe_updates.result24
-rw-r--r--mysql-test/main/sql_safe_updates.test25
-rw-r--r--sql/sql_delete.cc5
-rw-r--r--sql/sql_update.cc5
4 files changed, 52 insertions, 7 deletions
diff --git a/mysql-test/main/sql_safe_updates.result b/mysql-test/main/sql_safe_updates.result
index 356cd36bad9..f2944e60489 100644
--- a/mysql-test/main/sql_safe_updates.result
+++ b/mysql-test/main/sql_safe_updates.result
@@ -1,3 +1,27 @@
+#
+# MDEV-14429 sql_safe_updates in my.cnf not work
+#
select @@sql_safe_updates;
@@sql_safe_updates
1
+#
+# MDEV-18304 sql_safe_updates does not work with OR clauses
+#
+create table t1 (a int, b int, primary key (a), key (b));
+update t1 set b=2 where a=1 or b=2;
+ERROR HY000: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
+explain update t1 set b=2 where a=1 or b=2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 1 Using where
+delete from t1 where a=1 or b=2;
+ERROR HY000: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
+explain delete from t1 where a=1 or b=2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 1 Using where
+insert into t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8);
+update t1 set b=2 where a=1 or b=2;
+delete from t1 where a=1 or b=2;
+drop table t1;
+#
+# End of 10.3 tests
+#
diff --git a/mysql-test/main/sql_safe_updates.test b/mysql-test/main/sql_safe_updates.test
index 18decd0ff2c..25fe4a15ca2 100644
--- a/mysql-test/main/sql_safe_updates.test
+++ b/mysql-test/main/sql_safe_updates.test
@@ -1,4 +1,23 @@
-#
-# MDEV-14429 sql_safe_updates in my.cnf not work
-#
+--echo #
+--echo # MDEV-14429 sql_safe_updates in my.cnf not work
+--echo #
select @@sql_safe_updates;
+
+--echo #
+--echo # MDEV-18304 sql_safe_updates does not work with OR clauses
+--echo #
+create table t1 (a int, b int, primary key (a), key (b));
+--error ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE
+update t1 set b=2 where a=1 or b=2;
+explain update t1 set b=2 where a=1 or b=2;
+--error ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE
+delete from t1 where a=1 or b=2;
+explain delete from t1 where a=1 or b=2;
+insert into t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8);
+update t1 set b=2 where a=1 or b=2;
+delete from t1 where a=1 or b=2;
+drop table t1;
+
+--echo #
+--echo # End of 10.3 tests
+--echo #
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index f6c05da9445..a0d8feb61e8 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -371,7 +371,8 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
DBUG_RETURN(TRUE);
const_cond= (!conds || conds->const_item());
- safe_update= MY_TEST(thd->variables.option_bits & OPTION_SAFE_UPDATES);
+ safe_update= (thd->variables.option_bits & OPTION_SAFE_UPDATES) &&
+ !thd->lex->describe;
if (safe_update && const_cond)
{
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
@@ -497,7 +498,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
}
/* If running in safe sql mode, don't allow updates without keys */
- if (table->quick_keys.is_clear_all())
+ if (!select || !select->quick)
{
thd->set_status_no_index_used();
if (safe_update && !using_limit)
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index 6065d03402f..1e997b75c7d 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -314,7 +314,8 @@ int mysql_update(THD *thd,
ha_rows *found_return, ha_rows *updated_return)
{
bool using_limit= limit != HA_POS_ERROR;
- bool safe_update= thd->variables.option_bits & OPTION_SAFE_UPDATES;
+ bool safe_update= (thd->variables.option_bits & OPTION_SAFE_UPDATES)
+ && !thd->lex->describe;
bool used_key_is_modified= FALSE, transactional_table;
bool will_batch= FALSE;
bool can_compare_record;
@@ -517,7 +518,7 @@ int mysql_update(THD *thd,
}
/* If running in safe sql mode, don't allow updates without keys */
- if (table->quick_keys.is_clear_all())
+ if (!select || !select->quick)
{
thd->set_status_no_index_used();
if (safe_update && !using_limit)