08 Mar '23
revision-id: 001dc26012b69757cd35f1a59e4768e577f8ec5c (mariadb-10.11.1-109-g001dc26)
parent(s): cce24adeefdbd0262d9df9410c5cd4928bcf71e2
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-03-08 12:05:28 -0800
message:
Another attempt to rebase MDEV-7487 against 11.0.1
---
sql/item_subselect.cc | 4 ----
sql/sql_select.cc | 3 ++-
2 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc
index 320cc11..9e6c205 100644
--- a/sql/item_subselect.cc
+++ b/sql/item_subselect.cc
@@ -2953,13 +2953,9 @@ bool Item_exists_subselect::select_prepare_to_be_in()
if (!optimizer &&
(thd->lex->sql_command == SQLCOM_SELECT ||
thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
-<<<<<<< c8ab3596380cd05a7e29350b1da0d32ee025a70c
- thd->lex->sql_command == SQLCOM_DELETE_MULTI) &&
-=======
thd->lex->sql_command == SQLCOM_DELETE_MULTI ||
thd->lex->sql_command == SQLCOM_UPDATE ||
thd->lex->sql_command == SQLCOM_DELETE) &&
->>>>>>> Fixes of MDEV-30538 and MDEV-30586 for 10.4 adjusted for 11.0.
!unit->first_select()->is_part_of_union() &&
optimizer_flag(thd, OPTIMIZER_SWITCH_EXISTS_TO_IN) &&
(is_top_level_item() ||
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 52de1fd..86b09dc 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -32592,7 +32592,8 @@ bool Sql_cmd_dml::execute_inner(THD *thd)
if (unlikely(thd->is_error()))
goto err;
- join->exec();
+ if (join->exec())
+ goto err;
if (thd->lex->describe & DESCRIBE_EXTENDED)
{
1
0
08 Mar '23
revision-id: 40938e077f130e7d6139d296fd8dfbf29ce096ca (mariadb-10.11.1-109-g40938e0)
parent(s): 7fca91e5e2267a995896e162a13b8ba7d1b08a22
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-03-07 17:18:45 -0800
message:
An attempt to rebase MDEV-7487 against 11.0.1
---
mysql-test/main/delete.result | 6 +-
mysql-test/main/delete_single_to_multi.result | 286 +++++++-------
mysql-test/main/derived.test | 10 -
mysql-test/main/derived_1.result | 19 +
mysql-test/main/derived_cond_pushdown.result | 3 +
mysql-test/main/log_state.result | 2 +-
mysql-test/main/multi_update.result | 18 -
.../main/myisam_explain_non_select_all.result | 28 +-
mysql-test/main/opt_trace.result | 4 +-
mysql-test/main/subselect.result | 12 +
mysql-test/main/subselect.test | 6 +
mysql-test/main/subselect_no_exists_to_in.result | 12 +
mysql-test/main/subselect_no_mat.result | 12 +
mysql-test/main/subselect_no_opts.result | 12 +
mysql-test/main/subselect_no_scache.result | 12 +
mysql-test/main/subselect_no_semijoin.result | 12 +
mysql-test/main/update_single_to_multi.result | 418 +++++++++++----------
mysql-test/main/update_use_source.result | 17 +-
sql/sql_delete.cc | 4 -
sql/sql_update.cc | 4 -
20 files changed, 495 insertions(+), 402 deletions(-)
diff --git a/mysql-test/main/delete.result b/mysql-test/main/delete.result
index dd62e09..c4bf335 100644
--- a/mysql-test/main/delete.result
+++ b/mysql-test/main/delete.result
@@ -583,8 +583,7 @@ explain delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
select *from t1;
@@ -600,8 +599,7 @@ where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3
order by c2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3
order by c2;
diff --git a/mysql-test/main/delete_single_to_multi.result b/mysql-test/main/delete_single_to_multi.result
index 9bd5e93..6c13edc 100644
--- a/mysql-test/main/delete_single_to_multi.result
+++ b/mysql-test/main/delete_single_to_multi.result
@@ -26,6 +26,7 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.072146724,
"nested_loop": [
{
"table": {
@@ -36,7 +37,9 @@ EXPLAIN
"key_length": "26",
"used_key_parts": ["n_name"],
"ref": ["const"],
+ "loops": 1,
"rows": 1,
+ "cost": 0.002024411,
"filtered": 100,
"index_condition": "nation.n_name = 'PERU'"
}
@@ -50,7 +53,9 @@ EXPLAIN
"key_length": "5",
"used_key_parts": ["c_nationkey"],
"ref": ["dbt3_s001.nation.n_nationkey"],
+ "loops": 1,
"rows": 11,
+ "cost": 0.014384434,
"filtered": 100
}
},
@@ -71,7 +76,9 @@ EXPLAIN
"rows": 108,
"selectivity_pct": 7.2
},
+ "loops": 11.66666667,
"rows": 11,
+ "cost": 0.055737879,
"filtered": 7.199999809,
"attached_condition": "orders.o_orderDATE between '1992-01-01' and '1992-06-30'"
}
@@ -115,6 +122,7 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.072146724,
"nested_loop": [
{
"table": {
@@ -125,7 +133,9 @@ EXPLAIN
"key_length": "26",
"used_key_parts": ["n_name"],
"ref": ["const"],
+ "loops": 1,
"rows": 1,
+ "cost": 0.002024411,
"filtered": 100,
"index_condition": "nation.n_name = 'PERU'"
}
@@ -139,7 +149,9 @@ EXPLAIN
"key_length": "5",
"used_key_parts": ["c_nationkey"],
"ref": ["dbt3_s001.nation.n_nationkey"],
+ "loops": 1,
"rows": 11,
+ "cost": 0.014384434,
"filtered": 100
}
},
@@ -160,7 +172,9 @@ EXPLAIN
"rows": 108,
"selectivity_pct": 7.2
},
+ "loops": 11.66666667,
"rows": 11,
+ "cost": 0.055737879,
"filtered": 7.199999809,
"attached_condition": "orders.o_orderDATE between '1992-01-01' and '1992-06-30'"
}
@@ -200,7 +214,7 @@ s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where (ps_partkey, ps_suppkey) in
@@ -230,7 +244,7 @@ s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
delete from partsupp where (ps_partkey, ps_suppkey) in
@@ -267,7 +281,7 @@ where s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where ps_partkey in (select p_partkey from part
@@ -297,7 +311,7 @@ where s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
delete from partsupp where ps_partkey in (select p_partkey from part
@@ -344,11 +358,11 @@ where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 11
-1 PRIMARY orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (7%) Using where; Using rowid filter
-1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey 4 dbt3_s001.orders.o_orderkey 17 Using where
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
+1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 Using where
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
-1 PRIMARY supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
+1 PRIMARY orders eq_ref|filter PRIMARY,i_o_orderdate,i_o_custkey PRIMARY|i_o_orderdate 4|4 dbt3_s001.lineitem.l_orderkey 1 (7%) Using where; Using rowid filter
+1 PRIMARY customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
select l_orderkey, l_linenumber, l_tax from lineitem where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -401,11 +415,11 @@ where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 11
-1 PRIMARY orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (7%) Using where; Using rowid filter
-1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey 4 dbt3_s001.orders.o_orderkey 17 Using where
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
+1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 Using where
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
-1 PRIMARY supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
+1 PRIMARY orders eq_ref|filter PRIMARY,i_o_orderdate,i_o_custkey PRIMARY|i_o_orderdate 4|4 dbt3_s001.lineitem.l_orderkey 1 (7%) Using where; Using rowid filter
+1 PRIMARY customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
delete from lineitem where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -466,8 +480,8 @@ and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150 Using where
-1 PRIMARY nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
+1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150
+1 PRIMARY nation ALL PRIMARY,i_n_regionkey NULL NULL NULL 25 Using where; Using join buffer (flat, BNL join)
1 PRIMARY orders ref|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (9%) Using where; FirstMatch(nation); Using rowid filter
explain format=json
select c_name, c_acctbal from customer where c_nationkey in (select n_nationkey from nation
@@ -479,29 +493,35 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.439864068,
"nested_loop": [
{
"table": {
"table_name": "customer",
"access_type": "ALL",
"possible_keys": ["PRIMARY", "i_c_nationkey"],
+ "loops": 1,
"rows": 150,
- "filtered": 100,
- "attached_condition": "customer.c_nationkey is not null"
+ "cost": 0.03493875,
+ "filtered": 100
}
},
{
- "table": {
- "table_name": "nation",
- "access_type": "eq_ref",
- "possible_keys": ["PRIMARY", "i_n_regionkey"],
- "key": "PRIMARY",
- "key_length": "4",
- "used_key_parts": ["n_nationkey"],
- "ref": ["dbt3_s001.customer.c_nationkey"],
- "rows": 1,
- "filtered": 40,
- "attached_condition": "nation.n_regionkey in (1,2)"
+ "block-nl-join": {
+ "table": {
+ "table_name": "nation",
+ "access_type": "ALL",
+ "possible_keys": ["PRIMARY", "i_n_regionkey"],
+ "loops": 150,
+ "rows": 25,
+ "cost": 0.162374625,
+ "filtered": 1.600000024,
+ "attached_condition": "nation.n_regionkey in (1,2)"
+ },
+ "buffer_type": "flat",
+ "buffer_size": "6Kb",
+ "join_type": "BNL",
+ "attached_condition": "nation.n_nationkey = customer.c_nationkey"
}
},
{
@@ -521,8 +541,10 @@ EXPLAIN
"rows": 140,
"selectivity_pct": 9.333333333
},
+ "loops": 60,
"rows": 11,
- "filtered": 9.333333015,
+ "cost": 0.242550693,
+ "filtered": 8.571428299,
"attached_condition": "orders.o_orderDATE between '1992-10-09' and '1993-06-08'",
"first_match": "nation"
}
@@ -581,8 +603,8 @@ and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150 Using where
-1 PRIMARY nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
+1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150
+1 PRIMARY nation ALL PRIMARY,i_n_regionkey NULL NULL NULL 25 Using where
1 PRIMARY orders ref|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (9%) Using where; FirstMatch(nation); Using rowid filter
explain format=json
delete from customer where c_nationkey in (select n_nationkey from nation
@@ -594,29 +616,29 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.439864068,
"nested_loop": [
{
"table": {
"table_name": "customer",
"access_type": "ALL",
"possible_keys": ["PRIMARY", "i_c_nationkey"],
+ "loops": 1,
"rows": 150,
- "filtered": 100,
- "attached_condition": "customer.c_nationkey is not null"
+ "cost": 0.03493875,
+ "filtered": 100
}
},
{
"table": {
"table_name": "nation",
- "access_type": "eq_ref",
+ "access_type": "ALL",
"possible_keys": ["PRIMARY", "i_n_regionkey"],
- "key": "PRIMARY",
- "key_length": "4",
- "used_key_parts": ["n_nationkey"],
- "ref": ["dbt3_s001.customer.c_nationkey"],
- "rows": 1,
- "filtered": 40,
- "attached_condition": "nation.n_regionkey in (1,2)"
+ "loops": 150,
+ "rows": 25,
+ "cost": 0.162374625,
+ "filtered": 1.600000024,
+ "attached_condition": "nation.n_nationkey = customer.c_nationkey and nation.n_regionkey in (1,2)"
}
},
{
@@ -636,8 +658,10 @@ EXPLAIN
"rows": 140,
"selectivity_pct": 9.333333333
},
+ "loops": 60,
"rows": 11,
- "filtered": 9.333333015,
+ "cost": 0.242550693,
+ "filtered": 8.571428299,
"attached_condition": "orders.o_orderDATE between '1992-10-09' and '1993-06-08'",
"first_match": "nation"
}
@@ -759,9 +783,8 @@ explain
select c_name, c_acctbal from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 28
-1 PRIMARY customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1
-2 MATERIALIZED orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where
+1 PRIMARY orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where; Start temporary
+1 PRIMARY customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1 End temporary
explain format=json
select c_name, c_acctbal from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
@@ -769,50 +792,42 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.114174674,
"nested_loop": [
{
- "table": {
- "table_name": "<subquery2>",
- "access_type": "ALL",
- "possible_keys": ["distinct_key"],
- "rows": 28,
- "filtered": 100,
- "materialized": {
- "unique": 1,
- "query_block": {
- "select_id": 2,
- "nested_loop": [
- {
- "table": {
- "table_name": "orders",
- "access_type": "range",
- "possible_keys": ["i_o_orderdate", "i_o_custkey"],
- "key": "i_o_orderdate",
- "key_length": "4",
- "used_key_parts": ["o_orderDATE"],
- "rows": 28,
- "filtered": 100,
- "index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'",
- "attached_condition": "orders.o_custkey is not null"
- }
- }
- ]
+ "duplicates_removal": [
+ {
+ "table": {
+ "table_name": "orders",
+ "access_type": "range",
+ "possible_keys": ["i_o_orderdate", "i_o_custkey"],
+ "key": "i_o_orderdate",
+ "key_length": "4",
+ "used_key_parts": ["o_orderDATE"],
+ "loops": 1,
+ "rows": 28,
+ "cost": 0.035889016,
+ "filtered": 100,
+ "index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'",
+ "attached_condition": "orders.o_custkey is not null"
+ }
+ },
+ {
+ "table": {
+ "table_name": "customer",
+ "access_type": "eq_ref",
+ "possible_keys": ["PRIMARY"],
+ "key": "PRIMARY",
+ "key_length": "4",
+ "used_key_parts": ["c_custkey"],
+ "ref": ["dbt3_s001.orders.o_custkey"],
+ "loops": 28,
+ "rows": 1,
+ "cost": 0.048617528,
+ "filtered": 3.571428537
}
}
- }
- },
- {
- "table": {
- "table_name": "customer",
- "access_type": "eq_ref",
- "possible_keys": ["PRIMARY"],
- "key": "PRIMARY",
- "key_length": "4",
- "used_key_parts": ["c_custkey"],
- "ref": ["dbt3_s001.orders.o_custkey"],
- "rows": 1,
- "filtered": 100
- }
+ ]
}
]
}
@@ -851,9 +866,8 @@ explain
delete from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 28
-1 PRIMARY customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1
-2 MATERIALIZED orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where
+1 PRIMARY orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where; Start temporary
+1 PRIMARY customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1 End temporary
explain format=json
delete from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
@@ -861,50 +875,42 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.083497902,
"nested_loop": [
{
- "table": {
- "table_name": "<subquery2>",
- "access_type": "ALL",
- "possible_keys": ["distinct_key"],
- "rows": 28,
- "filtered": 100,
- "materialized": {
- "unique": 1,
- "query_block": {
- "select_id": 2,
- "nested_loop": [
- {
- "table": {
- "table_name": "orders",
- "access_type": "range",
- "possible_keys": ["i_o_orderdate", "i_o_custkey"],
- "key": "i_o_orderdate",
- "key_length": "4",
- "used_key_parts": ["o_orderDATE"],
- "rows": 28,
- "filtered": 100,
- "index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'",
- "attached_condition": "orders.o_custkey is not null"
- }
- }
- ]
+ "duplicates_removal": [
+ {
+ "table": {
+ "table_name": "orders",
+ "access_type": "range",
+ "possible_keys": ["i_o_orderdate", "i_o_custkey"],
+ "key": "i_o_orderdate",
+ "key_length": "4",
+ "used_key_parts": ["o_orderDATE"],
+ "loops": 1,
+ "rows": 28,
+ "cost": 0.035889016,
+ "filtered": 100,
+ "index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'",
+ "attached_condition": "orders.o_custkey is not null"
+ }
+ },
+ {
+ "table": {
+ "table_name": "customer",
+ "access_type": "eq_ref",
+ "possible_keys": ["PRIMARY"],
+ "key": "PRIMARY",
+ "key_length": "4",
+ "used_key_parts": ["c_custkey"],
+ "ref": ["dbt3_s001.orders.o_custkey"],
+ "loops": 28,
+ "rows": 1,
+ "cost": 0.017940756,
+ "filtered": 3.571428537
}
}
- }
- },
- {
- "table": {
- "table_name": "customer",
- "access_type": "eq_ref",
- "possible_keys": ["PRIMARY"],
- "key": "PRIMARY",
- "key_length": "4",
- "used_key_parts": ["c_custkey"],
- "ref": ["dbt3_s001.orders.o_custkey"],
- "rows": 1,
- "filtered": 100
- }
+ ]
}
]
}
@@ -1125,18 +1131,22 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.085533248,
"nested_loop": [
{
"table": {
"table_name": "<subquery2>",
"access_type": "ALL",
"possible_keys": ["distinct_key"],
+ "loops": 1,
"rows": 28,
+ "cost": 0.03691572,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
+ "cost": 0.053826401,
"having_condition": "count(orders.o_custkey) > 1",
"temporary_table": {
"nested_loop": [
@@ -1148,7 +1158,9 @@ EXPLAIN
"key": "i_o_orderdate",
"key_length": "4",
"used_key_parts": ["o_orderDATE"],
+ "loops": 1,
"rows": 28,
+ "cost": 0.035889016,
"filtered": 100,
"index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'"
}
@@ -1168,7 +1180,9 @@ EXPLAIN
"key_length": "4",
"used_key_parts": ["c_custkey"],
"ref": ["<subquery2>.o_custkey"],
+ "loops": 28,
"rows": 1,
+ "cost": 0.048617528,
"filtered": 100
}
}
@@ -1204,18 +1218,22 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.054856476,
"nested_loop": [
{
"table": {
"table_name": "<subquery2>",
"access_type": "ALL",
"possible_keys": ["distinct_key"],
+ "loops": 1,
"rows": 28,
+ "cost": 0.03691572,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
+ "cost": 0.053826401,
"having_condition": "count(orders.o_custkey) > 1",
"temporary_table": {
"nested_loop": [
@@ -1227,7 +1245,9 @@ EXPLAIN
"key": "i_o_orderdate",
"key_length": "4",
"used_key_parts": ["o_orderDATE"],
+ "loops": 1,
"rows": 28,
+ "cost": 0.035889016,
"filtered": 100,
"index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'"
}
@@ -1247,7 +1267,9 @@ EXPLAIN
"key_length": "4",
"used_key_parts": ["c_custkey"],
"ref": ["<subquery2>.o_custkey"],
+ "loops": 28,
"rows": 1,
+ "cost": 0.017940756,
"filtered": 100
}
}
@@ -1279,7 +1301,7 @@ where o_orderDATE between '1992-01-09' and '1993-03-08'
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL PRIMARY NULL NULL NULL 150
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 dbt3_s001.customer.c_custkey 1
-2 MATERIALIZED orders range i_o_orderdate i_o_orderdate 4 NULL 242 Using index condition; Using temporary
+2 MATERIALIZED orders ALL i_o_orderdate NULL NULL NULL 1500 Using where; Using temporary
select c_name, c_acctbal from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1993-03-08'
group by o_custkey having count(o_custkey) > 5);
@@ -1304,7 +1326,7 @@ where o_orderDATE between '1992-01-09' and '1993-03-08'
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL PRIMARY NULL NULL NULL 150
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 dbt3_s001.customer.c_custkey 1
-2 MATERIALIZED orders range i_o_orderdate i_o_orderdate 4 NULL 242 Using index condition; Using temporary
+2 MATERIALIZED orders ALL i_o_orderdate NULL NULL NULL 1500 Using where; Using temporary
delete from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1993-03-08'
group by o_custkey having count(o_custkey) > 5);
@@ -2329,9 +2351,9 @@ o_custkey in (select c_custkey from customer
where c_nationkey in (1,2));
select o_orderkey, o_totalprice from t;
o_orderkey o_totalprice
+1221 117397.16
324 26868.85
1856 189361.42
-1221 117397.16
4903 34363.63
5607 24660.06
delete from orders where o_orderDATE between '1992-01-01' and '1992-06-30' and
@@ -2367,9 +2389,9 @@ o_custkey in (select c_custkey from customer
where c_nationkey in (1,2));
select o_orderkey, o_totalprice from t;
o_orderkey o_totalprice
-1856 189361.42
-324 26868.85
1221 117397.16
+324 26868.85
+1856 189361.42
4903 34363.63
5607 24660.06
delete from orders where o_orderDATE between '1992-01-01' and '1992-06-30' and
diff --git a/mysql-test/main/derived.test b/mysql-test/main/derived.test
index 586d0d5..a7ecfa0 100644
--- a/mysql-test/main/derived.test
+++ b/mysql-test/main/derived.test
@@ -1132,13 +1132,10 @@ select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a fr
analyze select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except ALL (select t3.a from t3 order by b))q where t1.a=q.a;
-<<<<<<< 21505331eaaf9b4e9ab6b393da821ecd87975be0
select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except ALL (select t3.a from t3 order by b))q where t1.a=q.a;
drop table t1,t2,t3;
-=======
->>>>>>> MDEV-7487 Semi-join optimization for single-table update/delete statements
--echo #
--echo # MDEV-16549: Server crashes in Item_field::fix_fields on query with
--echo # view and subquery, Assertion `context' failed, Assertion `field' failed
@@ -1157,7 +1154,6 @@ DROP TABLE t1;
--echo #
--echo # End of 10.3 tests
--echo #
-<<<<<<< 21505331eaaf9b4e9ab6b393da821ecd87975be0
--echo #
--echo # Test of "Derived tables and union can now create distinct keys"
@@ -1249,8 +1245,6 @@ drop table t1;
--echo #
--echo # End of 11.0 tests
--echo #
-=======
->>>>>>> MDEV-7487 Semi-join optimization for single-table update/delete statements
--echo #
--echo # MDEV-28883: single/multi-table UPDATE/DELETE whose WHERE condition
@@ -1458,8 +1452,4 @@ deallocate prepare stmt;
drop table t1,t2,t3;
-<<<<<<< 21505331eaaf9b4e9ab6b393da821ecd87975be0
--echo # End of MariaDB 11.1 tests
-=======
---echo # End of MariaDB 11.0 tests
->>>>>>> MDEV-7487 Semi-join optimization for single-table update/delete statements
diff --git a/mysql-test/main/derived_1.result b/mysql-test/main/derived_1.result
new file mode 100644
index 0000000..a61f5d5
--- /dev/null
+++ b/mysql-test/main/derived_1.result
@@ -0,0 +1,19 @@
+#
+# MDEV-28883: single/multi-table UPDATE/DELETE whose WHERE condition
+# contains subquery from mergeable derived table
+# that uses the updated/deleted table
+#
+create table t1 (pk int, a int);
+insert into t1 values (1,3), (2, 7), (3,1), (4,9);
+create table t2 (pk int, a int);
+insert into t2 values (1,3), (2, 7), (3,1), (4,9);
+create table t3 (a int);
+insert into t3 VALUES (0),(1);
+explain delete from t1 using t1,t3
+where t1.a=t3.a and
+t1.a = ( select * from (select a from t2) dt where dt.a > 7);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
+drop table t1,t2,t3;
diff --git a/mysql-test/main/derived_cond_pushdown.result b/mysql-test/main/derived_cond_pushdown.result
index 6c9501d..fd3530b 100644
--- a/mysql-test/main/derived_cond_pushdown.result
+++ b/mysql-test/main/derived_cond_pushdown.result
@@ -13134,6 +13134,7 @@ EXPLAIN
"materialized": {
"query_block": {
"select_id": 3,
+ "cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f2",
"temporary_table": {
@@ -13142,7 +13143,9 @@ EXPLAIN
"table": {
"table_name": "t1",
"access_type": "ALL",
+ "loops": 1,
"rows": 2,
+ "cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f2 < 2"
}
diff --git a/mysql-test/main/log_state.result b/mysql-test/main/log_state.result
index 1b1c737..18c8da7 100644
--- a/mysql-test/main/log_state.result
+++ b/mysql-test/main/log_state.result
@@ -243,7 +243,7 @@ rows_examined sql_text
4 UPDATE t1 SET a=a+sleep(.02) WHERE a>2
8 UPDATE t1 SET a=a+sleep(.02) ORDER BY a DESC
1 UPDATE t2 set b=b+sleep(.02) limit 1
-10 UPDATE t1 SET a=a+sleep(.02) WHERE a in (SELECT b from t2)
+6 UPDATE t1 SET a=a+sleep(.02) WHERE a in (SELECT b from t2)
6 DELETE FROM t1 WHERE a=a+sleep(.02) ORDER BY a LIMIT 2
disconnect con2;
connection default;
diff --git a/mysql-test/main/multi_update.result b/mysql-test/main/multi_update.result
index af254be..2364ee7 100644
--- a/mysql-test/main/multi_update.result
+++ b/mysql-test/main/multi_update.result
@@ -1302,42 +1302,24 @@ t1.c1 > 1 and
exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL idx NULL NULL NULL 8 Using where
-<<<<<<< 3f0764f42cf801a4410f32a4ee7b2b4f4ce7e3fb
1 PRIMARY t2 range idx idx 5 NULL 3 Using index condition; Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
1 PRIMARY t3 ref idx idx 5 test.t1.c2 3
-=======
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-1 PRIMARY t3 ref idx idx 5 test.t1.c2 3
-2 MATERIALIZED t2 range idx idx 5 NULL 3 Using index condition; Using where
->>>>>>> Fixes of MDEV-30538 and MDEV-30586 for 10.4 adjusted for 11.0.
explain delete from t1 using t1,t3
where t1.c2 = t3.c2 and
t1.c1 > 1 and
exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL idx NULL NULL NULL 8 Using where
-<<<<<<< 3f0764f42cf801a4410f32a4ee7b2b4f4ce7e3fb
1 PRIMARY t2 range idx idx 5 NULL 3 Using where; FirstMatch(t1)
1 PRIMARY t3 ref idx idx 5 test.t1.c2 3 Using index
-=======
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-1 PRIMARY t3 ref idx idx 5 test.t1.c2 3 Using index
-2 MATERIALIZED t2 range idx idx 5 NULL 3 Using where
->>>>>>> Fixes of MDEV-30538 and MDEV-30586 for 10.4 adjusted for 11.0.
explain update t1,t3 set t1.c1 = t1.c1+10
where t1.c2 = t3.c2 and
t1.c1 > 1 and
exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL idx NULL NULL NULL 8 Using where
-<<<<<<< 3f0764f42cf801a4410f32a4ee7b2b4f4ce7e3fb
1 PRIMARY t2 range idx idx 5 NULL 3 Using where; FirstMatch(t1)
1 PRIMARY t3 ref idx idx 5 test.t1.c2 3 Using index
-=======
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-1 PRIMARY t3 ref idx idx 5 test.t1.c2 3 Using index
-2 MATERIALIZED t2 range idx idx 5 NULL 3 Using where
->>>>>>> Fixes of MDEV-30538 and MDEV-30586 for 10.4 adjusted for 11.0.
create table t as select * from t1;
select * from t1,t3
where t1.c2 = t3.c2 and
diff --git a/mysql-test/main/myisam_explain_non_select_all.result b/mysql-test/main/myisam_explain_non_select_all.result
index c8462bd..2f56847 100644
--- a/mysql-test/main/myisam_explain_non_select_all.result
+++ b/mysql-test/main/myisam_explain_non_select_all.result
@@ -218,16 +218,14 @@ INSERT INTO t2 VALUES (1), (2), (3);
#
EXPLAIN UPDATE t1 SET a = 10 WHERE 1 IN (SELECT 1 FROM t2 WHERE t2.b < 3);
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+1 PRIMARY t2 ALL NULL NULL NULL NULL 3 Using where; FirstMatch
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
-2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 Using where
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE 1 IN (SELECT 1 FROM t2 WHERE t2.b < 3);
id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
+1 PRIMARY t2 ALL NULL NULL NULL NULL 3 33.33 Using where; FirstMatch
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
-2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -248,9 +246,9 @@ Handler_read_key 4
Handler_read_rnd_next 5
# Status of testing query execution:
Variable_name Value
-Handler_read_key 5
+Handler_read_key 4
Handler_read_rnd 3
-Handler_read_rnd_next 12
+Handler_read_rnd_next 9
Handler_update 3
DROP TABLE t1, t2;
@@ -904,17 +902,13 @@ INSERT INTO t2 VALUES (1), (2), (3), (1000);
EXPLAIN UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3
-s
+1 PRIMARY t2 ALL NULL NULL NULL NULL 4 Using where; FirstMatch(t1)
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
-2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00
-s
+1 PRIMARY t2 ALL NULL NULL NULL NULL 4 100.00 Using where; FirstMatch(t1)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -935,8 +929,8 @@ Handler_read_key 4
Handler_read_rnd_next 9
# Status of testing query execution:
Variable_name Value
-Handler_read_key 7
-Handler_read_rnd_next 8
+Handler_read_key 4
+Handler_read_rnd_next 10
Handler_update 3
DROP TABLE t1, t2;
@@ -2830,16 +2824,14 @@ INSERT INTO t2 VALUES (1), (2), (3);
EXPLAIN UPDATE t1 SET a = 10 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
-1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2 FirstMatch(t1)
-s
+1 PRIMARY <derived3> eq_ref distinct_key distinct_key 5 test.t1.a 1
3 DERIVED t2 ALL NULL NULL NULL NULL 3 Using filesort
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
-1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2 100.00 FirstMatch(t1)
-s
+1 PRIMARY <derived3> eq_ref distinct_key distinct_key 5 test.t1.a 1 100.00
3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
# Status of EXPLAIN EXTENDED query
Variable_name Value
diff --git a/mysql-test/main/opt_trace.result b/mysql-test/main/opt_trace.result
index 1e32802..d5213d4 100644
--- a/mysql-test/main/opt_trace.result
+++ b/mysql-test/main/opt_trace.result
@@ -4495,7 +4495,7 @@ explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3 {
"rowid_filters": [
{
"key": "a",
- "build_cost": 0.174715752,
+ "build_cost": 0.001129926,
"rows": 3
}
]
@@ -4570,7 +4570,7 @@ explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3 {
"rowid_filters": [
{
"key": "a",
- "build_cost": 0.174715752,
+ "build_cost": 0.001129926,
"rows": 3
}
]
diff --git a/mysql-test/main/subselect.result b/mysql-test/main/subselect.result
index ce156f1..3a246e2 100644
--- a/mysql-test/main/subselect.result
+++ b/mysql-test/main/subselect.result
@@ -634,6 +634,18 @@ a b
22 11
2 12
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
+select * from t11;
+a b
+0 10
+1 11
+select * from t12;
+a b
+33 10
+22 11
+delete from t11;
+delete from t12;
+insert into t11 values (0, 10),(1, 11),(2, 12);
+insert into t12 values (33, 10),(22, 11),(2, 12);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
diff --git a/mysql-test/main/subselect.test b/mysql-test/main/subselect.test
index 8fefa39..578dbe8 100644
--- a/mysql-test/main/subselect.test
+++ b/mysql-test/main/subselect.test
@@ -387,6 +387,12 @@ insert into t2 values (1, 21),(2, 12),(3, 23);
select * from t11;
select * from t12;
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
+select * from t11;
+select * from t12;
+delete from t11;
+delete from t12;
+insert into t11 values (0, 10),(1, 11),(2, 12);
+insert into t12 values (33, 10),(22, 11),(2, 12);
-- error ER_SUBQUERY_NO_1_ROW
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
diff --git a/mysql-test/main/subselect_no_exists_to_in.result b/mysql-test/main/subselect_no_exists_to_in.result
index 37a503d..3be4f07 100644
--- a/mysql-test/main/subselect_no_exists_to_in.result
+++ b/mysql-test/main/subselect_no_exists_to_in.result
@@ -638,6 +638,18 @@ a b
22 11
2 12
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
+select * from t11;
+a b
+0 10
+1 11
+select * from t12;
+a b
+33 10
+22 11
+delete from t11;
+delete from t12;
+insert into t11 values (0, 10),(1, 11),(2, 12);
+insert into t12 values (33, 10),(22, 11),(2, 12);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
diff --git a/mysql-test/main/subselect_no_mat.result b/mysql-test/main/subselect_no_mat.result
index 32a6358..58124a9 100644
--- a/mysql-test/main/subselect_no_mat.result
+++ b/mysql-test/main/subselect_no_mat.result
@@ -641,6 +641,18 @@ a b
22 11
2 12
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
+select * from t11;
+a b
+0 10
+1 11
+select * from t12;
+a b
+33 10
+22 11
+delete from t11;
+delete from t12;
+insert into t11 values (0, 10),(1, 11),(2, 12);
+insert into t12 values (33, 10),(22, 11),(2, 12);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
diff --git a/mysql-test/main/subselect_no_opts.result b/mysql-test/main/subselect_no_opts.result
index d75c421..10153b0 100644
--- a/mysql-test/main/subselect_no_opts.result
+++ b/mysql-test/main/subselect_no_opts.result
@@ -637,6 +637,18 @@ a b
22 11
2 12
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
+select * from t11;
+a b
+0 10
+1 11
+select * from t12;
+a b
+33 10
+22 11
+delete from t11;
+delete from t12;
+insert into t11 values (0, 10),(1, 11),(2, 12);
+insert into t12 values (33, 10),(22, 11),(2, 12);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
diff --git a/mysql-test/main/subselect_no_scache.result b/mysql-test/main/subselect_no_scache.result
index 336936d..cd31b9b 100644
--- a/mysql-test/main/subselect_no_scache.result
+++ b/mysql-test/main/subselect_no_scache.result
@@ -640,6 +640,18 @@ a b
22 11
2 12
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
+select * from t11;
+a b
+0 10
+1 11
+select * from t12;
+a b
+33 10
+22 11
+delete from t11;
+delete from t12;
+insert into t11 values (0, 10),(1, 11),(2, 12);
+insert into t12 values (33, 10),(22, 11),(2, 12);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
diff --git a/mysql-test/main/subselect_no_semijoin.result b/mysql-test/main/subselect_no_semijoin.result
index c34dfe6..eba0f55 100644
--- a/mysql-test/main/subselect_no_semijoin.result
+++ b/mysql-test/main/subselect_no_semijoin.result
@@ -637,6 +637,18 @@ a b
22 11
2 12
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
+select * from t11;
+a b
+0 10
+1 11
+select * from t12;
+a b
+33 10
+22 11
+delete from t11;
+delete from t12;
+insert into t11 values (0, 10),(1, 11),(2, 12);
+insert into t12 values (33, 10),(22, 11),(2, 12);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
diff --git a/mysql-test/main/update_single_to_multi.result b/mysql-test/main/update_single_to_multi.result
index d020fd1..be97047 100644
--- a/mysql-test/main/update_single_to_multi.result
+++ b/mysql-test/main/update_single_to_multi.result
@@ -26,6 +26,7 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.072146724,
"nested_loop": [
{
"table": {
@@ -36,7 +37,9 @@ EXPLAIN
"key_length": "26",
"used_key_parts": ["n_name"],
"ref": ["const"],
+ "loops": 1,
"rows": 1,
+ "cost": 0.002024411,
"filtered": 100,
"index_condition": "nation.n_name = 'PERU'"
}
@@ -50,7 +53,9 @@ EXPLAIN
"key_length": "5",
"used_key_parts": ["c_nationkey"],
"ref": ["dbt3_s001.nation.n_nationkey"],
+ "loops": 1,
"rows": 11,
+ "cost": 0.014384434,
"filtered": 100
}
},
@@ -71,7 +76,9 @@ EXPLAIN
"rows": 108,
"selectivity_pct": 7.2
},
+ "loops": 11.66666667,
"rows": 11,
+ "cost": 0.055737879,
"filtered": 7.199999809,
"attached_condition": "orders.o_orderDATE between '1992-01-01' and '1992-06-30'"
}
@@ -110,6 +117,7 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.072146724,
"nested_loop": [
{
"table": {
@@ -120,7 +128,9 @@ EXPLAIN
"key_length": "26",
"used_key_parts": ["n_name"],
"ref": ["const"],
+ "loops": 1,
"rows": 1,
+ "cost": 0.002024411,
"filtered": 100,
"index_condition": "nation.n_name = 'PERU'"
}
@@ -134,7 +144,9 @@ EXPLAIN
"key_length": "5",
"used_key_parts": ["c_nationkey"],
"ref": ["dbt3_s001.nation.n_nationkey"],
+ "loops": 1,
"rows": 11,
+ "cost": 0.014384434,
"filtered": 100
}
},
@@ -155,7 +167,9 @@ EXPLAIN
"rows": 108,
"selectivity_pct": 7.2
},
+ "loops": 11.66666667,
"rows": 11,
+ "cost": 0.055737879,
"filtered": 7.199999809,
"attached_condition": "orders.o_orderDATE between '1992-01-01' and '1992-06-30'"
}
@@ -205,7 +219,7 @@ s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where (ps_partkey, ps_suppkey) in
@@ -229,7 +243,7 @@ s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
update partsupp set ps_supplycost = ps_supplycost+2 where (ps_partkey, ps_suppkey) in
@@ -276,7 +290,7 @@ where s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where ps_partkey in (select p_partkey from part
@@ -300,7 +314,7 @@ where s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
update partsupp set ps_supplycost = ps_supplycost+10 where ps_partkey in (select p_partkey from part
@@ -357,11 +371,11 @@ where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 11
-1 PRIMARY orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (7%) Using where; Using rowid filter
-1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey 4 dbt3_s001.orders.o_orderkey 17 Using where
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
+1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 Using where
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
-1 PRIMARY supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
+1 PRIMARY orders eq_ref|filter PRIMARY,i_o_orderdate,i_o_custkey PRIMARY|i_o_orderdate 4|4 dbt3_s001.lineitem.l_orderkey 1 (7%) Using where; Using rowid filter
+1 PRIMARY customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
select l_orderkey, l_linenumber, l_tax from lineitem where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -377,10 +391,10 @@ where p_retailprice between 901 and 1000 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
l_orderkey l_linenumber l_tax
+4996 1 0.01
933 1 0.04
2500 2 0.02
2500 4 0.02
-4996 1 0.01
explain
update lineitem set l_tax = (l_tax*100+1)/100 where l_orderkey in (select o_orderkey from orders
where o_custkey in
@@ -399,11 +413,11 @@ where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 11
-1 PRIMARY orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (7%) Using where; Using rowid filter
-1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey 4 dbt3_s001.orders.o_orderkey 17 Using where
+1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1
+1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 Using where
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
-1 PRIMARY supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
+1 PRIMARY orders eq_ref|filter PRIMARY,i_o_orderdate,i_o_custkey PRIMARY|i_o_orderdate 4|4 dbt3_s001.lineitem.l_orderkey 1 (7%) Using where; Using rowid filter
+1 PRIMARY customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
update lineitem set l_tax = (l_tax*100+1)/100 where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -433,10 +447,10 @@ where p_retailprice between 901 and 1000 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
l_orderkey l_linenumber l_tax
+4996 1 0.02
933 1 0.05
2500 2 0.03
2500 4 0.03
-4996 1 0.02
update lineitem set l_tax = (l_tax*100-1)/100 where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -466,10 +480,10 @@ where p_retailprice between 901 and 1000 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
l_orderkey l_linenumber l_tax
+4996 1 0.01
933 1 0.04
2500 2 0.02
2500 4 0.02
-4996 1 0.01
# FirstMatch
# ==========
set optimizer_switch='materialization=off';
@@ -480,8 +494,8 @@ and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150 Using where
-1 PRIMARY nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
+1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150
+1 PRIMARY nation ALL PRIMARY,i_n_regionkey NULL NULL NULL 25 Using where; Using join buffer (flat, BNL join)
1 PRIMARY orders ref|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (9%) Using where; FirstMatch(nation); Using rowid filter
explain format=json
select c_name, c_acctbal from customer where c_nationkey in (select n_nationkey from nation
@@ -493,29 +507,35 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.439864068,
"nested_loop": [
{
"table": {
"table_name": "customer",
"access_type": "ALL",
"possible_keys": ["PRIMARY", "i_c_nationkey"],
+ "loops": 1,
"rows": 150,
- "filtered": 100,
- "attached_condition": "customer.c_nationkey is not null"
+ "cost": 0.03493875,
+ "filtered": 100
}
},
{
- "table": {
- "table_name": "nation",
- "access_type": "eq_ref",
- "possible_keys": ["PRIMARY", "i_n_regionkey"],
- "key": "PRIMARY",
- "key_length": "4",
- "used_key_parts": ["n_nationkey"],
- "ref": ["dbt3_s001.customer.c_nationkey"],
- "rows": 1,
- "filtered": 40,
- "attached_condition": "nation.n_regionkey in (1,2)"
+ "block-nl-join": {
+ "table": {
+ "table_name": "nation",
+ "access_type": "ALL",
+ "possible_keys": ["PRIMARY", "i_n_regionkey"],
+ "loops": 150,
+ "rows": 25,
+ "cost": 0.162374625,
+ "filtered": 1.600000024,
+ "attached_condition": "nation.n_regionkey in (1,2)"
+ },
+ "buffer_type": "flat",
+ "buffer_size": "6Kb",
+ "join_type": "BNL",
+ "attached_condition": "nation.n_nationkey = customer.c_nationkey"
}
},
{
@@ -535,8 +555,10 @@ EXPLAIN
"rows": 140,
"selectivity_pct": 9.333333333
},
+ "loops": 60,
"rows": 11,
- "filtered": 9.333333015,
+ "cost": 0.242550693,
+ "filtered": 8.571428299,
"attached_condition": "orders.o_orderDATE between '1992-10-09' and '1993-06-08'",
"first_match": "nation"
}
@@ -550,38 +572,38 @@ and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
c_name c_acctbal
-Customer#000000007 9561.95
-Customer#000000008 6819.74
+Customer#000000059 3458.6
+Customer#000000106 3288.42
Customer#000000017 6.34
-Customer#000000019 8914.71
+Customer#000000047 274.58
+Customer#000000092 1182.91
+Customer#000000101 7470.96
Customer#000000022 591.98
-Customer#000000025 7133.7
-Customer#000000028 1007.18
-Customer#000000037 -917.75
Customer#000000040 1335.3
-Customer#000000047 274.58
-Customer#000000059 3458.6
-Customer#000000061 1536.24
Customer#000000064 -646.64
-Customer#000000067 8166.59
-Customer#000000077 1738.87
-Customer#000000082 9468.34
+Customer#000000122 7865.46
+Customer#000000028 1007.18
+Customer#000000037 -917.75
Customer#000000091 4643.14
-Customer#000000092 1182.91
+Customer#000000115 7508.92
+Customer#000000067 8166.59
Customer#000000094 5500.11
-Customer#000000097 2164.48
-Customer#000000101 7470.96
Customer#000000103 2757.45
-Customer#000000106 3288.42
-Customer#000000115 7508.92
-Customer#000000121 6428.32
-Customer#000000122 7865.46
-Customer#000000124 1842.49
-Customer#000000127 9280.71
Customer#000000130 5073.58
-Customer#000000133 2314.67
Customer#000000139 7897.78
Customer#000000142 2209.81
+Customer#000000025 7133.7
+Customer#000000008 6819.74
+Customer#000000061 1536.24
+Customer#000000077 1738.87
+Customer#000000097 2164.48
+Customer#000000121 6428.32
+Customer#000000133 2314.67
+Customer#000000007 9561.95
+Customer#000000019 8914.71
+Customer#000000082 9468.34
+Customer#000000124 1842.49
+Customer#000000127 9280.71
explain
update customer set c_acctbal = c_acctbal+10 where c_nationkey in (select n_nationkey from nation
where n_regionkey in (1,2))
@@ -589,8 +611,8 @@ and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150 Using where
-1 PRIMARY nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
+1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150
+1 PRIMARY nation ALL PRIMARY,i_n_regionkey NULL NULL NULL 25 Using where
1 PRIMARY orders ref|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (9%) Using where; FirstMatch(nation); Using rowid filter
explain format=json
update customer set c_acctbal = c_acctbal+10 where c_nationkey in (select n_nationkey from nation
@@ -602,29 +624,29 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.439864068,
"nested_loop": [
{
"table": {
"table_name": "customer",
"access_type": "ALL",
"possible_keys": ["PRIMARY", "i_c_nationkey"],
+ "loops": 1,
"rows": 150,
- "filtered": 100,
- "attached_condition": "customer.c_nationkey is not null"
+ "cost": 0.03493875,
+ "filtered": 100
}
},
{
"table": {
"table_name": "nation",
- "access_type": "eq_ref",
+ "access_type": "ALL",
"possible_keys": ["PRIMARY", "i_n_regionkey"],
- "key": "PRIMARY",
- "key_length": "4",
- "used_key_parts": ["n_nationkey"],
- "ref": ["dbt3_s001.customer.c_nationkey"],
- "rows": 1,
- "filtered": 40,
- "attached_condition": "nation.n_regionkey in (1,2)"
+ "loops": 150,
+ "rows": 25,
+ "cost": 0.162374625,
+ "filtered": 1.600000024,
+ "attached_condition": "nation.n_nationkey = customer.c_nationkey and nation.n_regionkey in (1,2)"
}
},
{
@@ -644,8 +666,10 @@ EXPLAIN
"rows": 140,
"selectivity_pct": 9.333333333
},
+ "loops": 60,
"rows": 11,
- "filtered": 9.333333015,
+ "cost": 0.242550693,
+ "filtered": 8.571428299,
"attached_condition": "orders.o_orderDATE between '1992-10-09' and '1993-06-08'",
"first_match": "nation"
}
@@ -664,38 +688,38 @@ and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
c_name c_acctbal
-Customer#000000007 9571.95
-Customer#000000008 6829.74
+Customer#000000059 3468.6
+Customer#000000106 3298.42
Customer#000000017 16.34
-Customer#000000019 8924.71
+Customer#000000047 284.58
+Customer#000000092 1192.91
+Customer#000000101 7480.96
Customer#000000022 601.98
-Customer#000000025 7143.7
-Customer#000000028 1017.18
-Customer#000000037 -907.75
Customer#000000040 1345.3
-Customer#000000047 284.58
-Customer#000000059 3468.6
-Customer#000000061 1546.24
Customer#000000064 -636.64
-Customer#000000067 8176.59
-Customer#000000077 1748.87
-Customer#000000082 9478.34
+Customer#000000122 7875.46
+Customer#000000028 1017.18
+Customer#000000037 -907.75
Customer#000000091 4653.14
-Customer#000000092 1192.91
+Customer#000000115 7518.92
+Customer#000000067 8176.59
Customer#000000094 5510.11
-Customer#000000097 2174.48
-Customer#000000101 7480.96
Customer#000000103 2767.45
-Customer#000000106 3298.42
-Customer#000000115 7518.92
-Customer#000000121 6438.32
-Customer#000000122 7875.46
-Customer#000000124 1852.49
-Customer#000000127 9290.71
Customer#000000130 5083.58
-Customer#000000133 2324.67
Customer#000000139 7907.78
Customer#000000142 2219.81
+Customer#000000025 7143.7
+Customer#000000008 6829.74
+Customer#000000061 1546.24
+Customer#000000077 1748.87
+Customer#000000097 2174.48
+Customer#000000121 6438.32
+Customer#000000133 2324.67
+Customer#000000007 9571.95
+Customer#000000019 8924.71
+Customer#000000082 9478.34
+Customer#000000124 1852.49
+Customer#000000127 9290.71
update customer set c_acctbal = c_acctbal-10 where c_nationkey in (select n_nationkey from nation
where n_regionkey in (1,2))
and
@@ -707,38 +731,38 @@ and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
c_name c_acctbal
-Customer#000000007 9561.95
-Customer#000000008 6819.74
+Customer#000000059 3458.6
+Customer#000000106 3288.42
Customer#000000017 6.34
-Customer#000000019 8914.71
+Customer#000000047 274.58
+Customer#000000092 1182.91
+Customer#000000101 7470.96
Customer#000000022 591.98
-Customer#000000025 7133.7
-Customer#000000028 1007.18
-Customer#000000037 -917.75
Customer#000000040 1335.3
-Customer#000000047 274.58
-Customer#000000059 3458.6
-Customer#000000061 1536.24
Customer#000000064 -646.64
-Customer#000000067 8166.59
-Customer#000000077 1738.87
-Customer#000000082 9468.34
+Customer#000000122 7865.46
+Customer#000000028 1007.18
+Customer#000000037 -917.75
Customer#000000091 4643.14
-Customer#000000092 1182.91
+Customer#000000115 7508.92
+Customer#000000067 8166.59
Customer#000000094 5500.11
-Customer#000000097 2164.48
-Customer#000000101 7470.96
Customer#000000103 2757.45
-Customer#000000106 3288.42
-Customer#000000115 7508.92
-Customer#000000121 6428.32
-Customer#000000122 7865.46
-Customer#000000124 1842.49
-Customer#000000127 9280.71
Customer#000000130 5073.58
-Customer#000000133 2314.67
Customer#000000139 7897.78
Customer#000000142 2209.81
+Customer#000000025 7133.7
+Customer#000000008 6819.74
+Customer#000000061 1536.24
+Customer#000000077 1738.87
+Customer#000000097 2164.48
+Customer#000000121 6428.32
+Customer#000000133 2314.67
+Customer#000000007 9561.95
+Customer#000000019 8914.71
+Customer#000000082 9468.34
+Customer#000000124 1842.49
+Customer#000000127 9280.71
set optimizer_switch='materialization=default';
explain
select c_name, c_acctbal from customer where c_nationkey in (select n_nationkey from nation where n_name='PERU')
@@ -805,9 +829,8 @@ explain
select c_name, c_acctbal from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 28
-1 PRIMARY customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1
-2 MATERIALIZED orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where
+1 PRIMARY orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where; Start temporary
+1 PRIMARY customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1 End temporary
explain format=json
select c_name, c_acctbal from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
@@ -815,50 +838,42 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.114174674,
"nested_loop": [
{
- "table": {
- "table_name": "<subquery2>",
- "access_type": "ALL",
- "possible_keys": ["distinct_key"],
- "rows": 28,
- "filtered": 100,
- "materialized": {
- "unique": 1,
- "query_block": {
- "select_id": 2,
- "nested_loop": [
- {
- "table": {
- "table_name": "orders",
- "access_type": "range",
- "possible_keys": ["i_o_orderdate", "i_o_custkey"],
- "key": "i_o_orderdate",
- "key_length": "4",
- "used_key_parts": ["o_orderDATE"],
- "rows": 28,
- "filtered": 100,
- "index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'",
- "attached_condition": "orders.o_custkey is not null"
- }
- }
- ]
+ "duplicates_removal": [
+ {
+ "table": {
+ "table_name": "orders",
+ "access_type": "range",
+ "possible_keys": ["i_o_orderdate", "i_o_custkey"],
+ "key": "i_o_orderdate",
+ "key_length": "4",
+ "used_key_parts": ["o_orderDATE"],
+ "loops": 1,
+ "rows": 28,
+ "cost": 0.035889016,
+ "filtered": 100,
+ "index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'",
+ "attached_condition": "orders.o_custkey is not null"
+ }
+ },
+ {
+ "table": {
+ "table_name": "customer",
+ "access_type": "eq_ref",
+ "possible_keys": ["PRIMARY"],
+ "key": "PRIMARY",
+ "key_length": "4",
+ "used_key_parts": ["c_custkey"],
+ "ref": ["dbt3_s001.orders.o_custkey"],
+ "loops": 28,
+ "rows": 1,
+ "cost": 0.048617528,
+ "filtered": 3.571428537
}
}
- }
- },
- {
- "table": {
- "table_name": "customer",
- "access_type": "eq_ref",
- "possible_keys": ["PRIMARY"],
- "key": "PRIMARY",
- "key_length": "4",
- "used_key_parts": ["c_custkey"],
- "ref": ["dbt3_s001.orders.o_custkey"],
- "rows": 1,
- "filtered": 100
- }
+ ]
}
]
}
@@ -894,9 +909,8 @@ explain
update customer set c_acctbal = c_acctbal+5 where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 28
-1 PRIMARY customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1
-2 MATERIALIZED orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where
+1 PRIMARY orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where; Start temporary
+1 PRIMARY customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1 End temporary
explain format=json
update customer set c_acctbal = c_acctbal+5 where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
@@ -904,50 +918,42 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.114174674,
"nested_loop": [
{
- "table": {
- "table_name": "<subquery2>",
- "access_type": "ALL",
- "possible_keys": ["distinct_key"],
- "rows": 28,
- "filtered": 100,
- "materialized": {
- "unique": 1,
- "query_block": {
- "select_id": 2,
- "nested_loop": [
- {
- "table": {
- "table_name": "orders",
- "access_type": "range",
- "possible_keys": ["i_o_orderdate", "i_o_custkey"],
- "key": "i_o_orderdate",
- "key_length": "4",
- "used_key_parts": ["o_orderDATE"],
- "rows": 28,
- "filtered": 100,
- "index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'",
- "attached_condition": "orders.o_custkey is not null"
- }
- }
- ]
+ "duplicates_removal": [
+ {
+ "table": {
+ "table_name": "orders",
+ "access_type": "range",
+ "possible_keys": ["i_o_orderdate", "i_o_custkey"],
+ "key": "i_o_orderdate",
+ "key_length": "4",
+ "used_key_parts": ["o_orderDATE"],
+ "loops": 1,
+ "rows": 28,
+ "cost": 0.035889016,
+ "filtered": 100,
+ "index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'",
+ "attached_condition": "orders.o_custkey is not null"
+ }
+ },
+ {
+ "table": {
+ "table_name": "customer",
+ "access_type": "eq_ref",
+ "possible_keys": ["PRIMARY"],
+ "key": "PRIMARY",
+ "key_length": "4",
+ "used_key_parts": ["c_custkey"],
+ "ref": ["dbt3_s001.orders.o_custkey"],
+ "loops": 28,
+ "rows": 1,
+ "cost": 0.048617528,
+ "filtered": 3.571428537
}
}
- }
- },
- {
- "table": {
- "table_name": "customer",
- "access_type": "eq_ref",
- "possible_keys": ["PRIMARY"],
- "key": "PRIMARY",
- "key_length": "4",
- "used_key_parts": ["c_custkey"],
- "ref": ["dbt3_s001.orders.o_custkey"],
- "rows": 1,
- "filtered": 100
- }
+ ]
}
]
}
@@ -1256,18 +1262,22 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.085533248,
"nested_loop": [
{
"table": {
"table_name": "<subquery2>",
"access_type": "ALL",
"possible_keys": ["distinct_key"],
+ "loops": 1,
"rows": 28,
+ "cost": 0.03691572,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
+ "cost": 0.053826401,
"having_condition": "count(orders.o_custkey) > 1",
"temporary_table": {
"nested_loop": [
@@ -1279,7 +1289,9 @@ EXPLAIN
"key": "i_o_orderdate",
"key_length": "4",
"used_key_parts": ["o_orderDATE"],
+ "loops": 1,
"rows": 28,
+ "cost": 0.035889016,
"filtered": 100,
"index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'"
}
@@ -1299,7 +1311,9 @@ EXPLAIN
"key_length": "4",
"used_key_parts": ["c_custkey"],
"ref": ["<subquery2>.o_custkey"],
+ "loops": 28,
"rows": 1,
+ "cost": 0.048617528,
"filtered": 100
}
}
@@ -1331,18 +1345,22 @@ EXPLAIN
{
"query_block": {
"select_id": 1,
+ "cost": 0.085533248,
"nested_loop": [
{
"table": {
"table_name": "<subquery2>",
"access_type": "ALL",
"possible_keys": ["distinct_key"],
+ "loops": 1,
"rows": 28,
+ "cost": 0.03691572,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
+ "cost": 0.053826401,
"having_condition": "count(orders.o_custkey) > 1",
"temporary_table": {
"nested_loop": [
@@ -1354,7 +1372,9 @@ EXPLAIN
"key": "i_o_orderdate",
"key_length": "4",
"used_key_parts": ["o_orderDATE"],
+ "loops": 1,
"rows": 28,
+ "cost": 0.035889016,
"filtered": 100,
"index_condition": "orders.o_orderDATE between '1992-01-09' and '1992-03-08'"
}
@@ -1374,7 +1394,9 @@ EXPLAIN
"key_length": "4",
"used_key_parts": ["c_custkey"],
"ref": ["<subquery2>.o_custkey"],
+ "loops": 28,
"rows": 1,
+ "cost": 0.048617528,
"filtered": 100
}
}
@@ -1412,7 +1434,7 @@ where o_orderDATE between '1992-01-09' and '1993-03-08'
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL PRIMARY NULL NULL NULL 150
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 dbt3_s001.customer.c_custkey 1
-2 MATERIALIZED orders range i_o_orderdate i_o_orderdate 4 NULL 242 Using index condition; Using temporary
+2 MATERIALIZED orders ALL i_o_orderdate NULL NULL NULL 1500 Using where; Using temporary
select c_name, c_acctbal from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1993-03-08'
group by o_custkey having count(o_custkey) > 5);
@@ -1433,7 +1455,7 @@ where o_orderDATE between '1992-01-09' and '1993-03-08'
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL PRIMARY NULL NULL NULL 150
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 dbt3_s001.customer.c_custkey 1
-2 MATERIALIZED orders range i_o_orderdate i_o_orderdate 4 NULL 242 Using index condition; Using temporary
+2 MATERIALIZED orders ALL i_o_orderdate NULL NULL NULL 1500 Using where; Using temporary
update customer set c_acctbal = c_acctbal-1 where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1993-03-08'
group by o_custkey having count(o_custkey) > 5);
diff --git a/mysql-test/main/update_use_source.result b/mysql-test/main/update_use_source.result
index 994b38a..99d7242 100644
--- a/mysql-test/main/update_use_source.result
+++ b/mysql-test/main/update_use_source.result
@@ -76,8 +76,7 @@ rollback;
explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
start transaction;
update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
@@ -318,8 +317,7 @@ rollback;
explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 2 Using index condition
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 2 Using where; Using index
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c2 8 Using index; FirstMatch(t1)
start transaction;
update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
@@ -559,9 +557,8 @@ rollback;
#
explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 2 Using index condition
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 2 Using where; Using index
+1 PRIMARY a index t1_c2 t1_c2 10 NULL 8 Using where; Using index; LooseScan
+1 PRIMARY t1 ref t1_c2 t1_c2 5 test.a.c2 1
start transaction;
update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
@@ -802,9 +799,8 @@ rollback;
#
explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 2 Using index condition
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 2 Using where; Using index
+1 PRIMARY a index t1_c2 t1_c2 10 NULL 8 Using where; Using index; LooseScan
+1 PRIMARY t1 ref t1_c2 t1_c2 5 test.a.c2 1
start transaction;
update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
@@ -1199,7 +1195,6 @@ create table t1 (c1 integer) engine=InnoDb;
create table t2 (c1 integer) engine=InnoDb;
create view v1 as select t1.c1 as "t1c1" ,t2.c1 as "t2c1" from t1,t2 where t1.c1=t2.c1;
update v1 set t1c1=2 order by 1;
-ERROR 42S22: Unknown column '1' in 'order clause'
update v1 set t1c1=2 limit 1;
drop table t1;
drop table t2;
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 609f5f0..6339d9d 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -351,10 +351,6 @@ bool Sql_cmd_delete::delete_from_single_table(THD *thd)
query_plan.using_filesort= FALSE;
THD_STAGE_INFO(thd, stage_init_update);
-<<<<<<< 21505331eaaf9b4e9ab6b393da821ecd87975be0
- // create_explain_query(thd->lex, thd->mem_root);
-=======
->>>>>>> MDEV-7487 Semi-join optimization for single-table update/delete statements
const bool delete_history= table_list->vers_conditions.delete_history;
DBUG_ASSERT(!(delete_history && table_list->period_conditions.is_set()));
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index 3838f27..b1a5875 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -385,10 +385,6 @@ bool Sql_cmd_update::update_single_table(THD *thd)
DBUG_ENTER("Sql_cmd_update::update_single_table");
THD_STAGE_INFO(thd, stage_init_update);
-<<<<<<< 21505331eaaf9b4e9ab6b393da821ecd87975be0
- // create_explain_query(thd->lex, thd->mem_root);
-=======
->>>>>>> MDEV-7487 Semi-join optimization for single-table update/delete statements
thd->table_map_for_update= 0;
1
0
[Commits] b1f98b4: MDEV-30539 EXPLAIN EXTENDED: no message with query for DML statements
by IgorBabaev 03 Mar '23
by IgorBabaev 03 Mar '23
03 Mar '23
revision-id: b1f98b4fa7719054e6259370c75c21d94300f8ae (mariadb-10.4.27-113-gb1f98b4)
parent(s): ccec9b1de95a66b7597bc30e0a60bd61866f225d
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-03-02 20:08:52 -0800
message:
MDEV-30539 EXPLAIN EXTENDED: no message with query for DML statements
Preliminary patch.
---
mysql-test/main/explain_non_select.result | 4 +
mysql-test/main/multi_update.result | 2 +-
mysql-test/main/multi_update.test | 2 +-
.../main/myisam_explain_non_select_all.result | 138 +++++++++
mysql-test/main/opt_trace.result | 2 +-
mysql-test/main/ps.result | 4 +
sql/sql_delete.cc | 2 +
sql/sql_explain.cc | 19 +-
sql/sql_explain.h | 2 +-
sql/sql_insert.cc | 6 +-
sql/sql_lex.cc | 2 +
sql/sql_lex.h | 6 +
sql/sql_parse.cc | 20 +-
sql/sql_select.cc | 322 ++++++++++++++++++---
sql/sql_update.cc | 10 +-
sql/table.h | 2 +
16 files changed, 485 insertions(+), 58 deletions(-)
diff --git a/mysql-test/main/explain_non_select.result b/mysql-test/main/explain_non_select.result
index 111b4c8..d60f10f 100644
--- a/mysql-test/main/explain_non_select.result
+++ b/mysql-test/main/explain_non_select.result
@@ -157,9 +157,13 @@ id select_type table partitions type possible_keys key key_len ref rows Extra
explain extended update t2 set b=3 where a in (3,4);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`b` = 3 where `test`.`t2`.`a` in (3,4)
explain extended delete from t2 where a in (3,4);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`a` in (3,4)
drop table t1,t2;
#
# Check the special case where partition pruning removed all partitions
diff --git a/mysql-test/main/multi_update.result b/mysql-test/main/multi_update.result
index d6cf9ba..222c592 100644
--- a/mysql-test/main/multi_update.result
+++ b/mysql-test/main/multi_update.result
@@ -1253,7 +1253,7 @@ EXPLAIN
DROP TABLES t1, t2;
# End of 10.3 tests
#
-# MDEV-28538: multi-table UPDATE/DELETE with possible exists-to-in
+# MDEV-30538: multi-table UPDATE/DELETE with possible exists-to-in
#
create table t1 (c1 int, c2 int, c3 int, index idx(c2));
insert into t1 values
diff --git a/mysql-test/main/multi_update.test b/mysql-test/main/multi_update.test
index 48e6250..329394e 100644
--- a/mysql-test/main/multi_update.test
+++ b/mysql-test/main/multi_update.test
@@ -1134,7 +1134,7 @@ DROP TABLES t1, t2;
--echo # End of 10.3 tests
--echo #
---echo # MDEV-28538: multi-table UPDATE/DELETE with possible exists-to-in
+--echo # MDEV-30538: multi-table UPDATE/DELETE with possible exists-to-in
--echo #
create table t1 (c1 int, c2 int, c3 int, index idx(c2));
diff --git a/mysql-test/main/myisam_explain_non_select_all.result b/mysql-test/main/myisam_explain_non_select_all.result
index 7f24cb4..75b9e6f 100644
--- a/mysql-test/main/myisam_explain_non_select_all.result
+++ b/mysql-test/main/myisam_explain_non_select_all.result
@@ -17,6 +17,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a < 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`a` = 10 where `test`.`t1`.`a` < 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -60,6 +62,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a < 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` < 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -103,6 +107,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 USING t1 WHERE a = 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` using `test`.`t1` where `test`.`t1`.`a` = 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -150,6 +156,8 @@ EXPLAIN EXTENDED UPDATE t1, t2 SET t1.a = 10 WHERE t1.a = 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t2` set `test`.`t1`.`a` = 10 where `test`.`t1`.`a` = 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -200,6 +208,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t11 ALL NULL NULL NULL NULL 3 100.00 Using where
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` `t11`,(/* select#2 */ select `test`.`t2`.`b` AS `b` from `test`.`t2`) `t12` set `test`.`t11`.`a` = 10 where `test`.`t11`.`a` = 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -248,6 +258,8 @@ EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE 1 IN (SELECT 1 FROM t2 WHERE
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`a` = 10 where <in_optimizer>(1,<exists>(/* select#2 */ select 1 from `test`.`t2` where `test`.`t2`.`b` < 3 and 1))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -300,6 +312,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`a` = 10 where <in_optimizer>(`test`.`t1`.`a`,<exists>(/* select#2 */ select `test`.`t2`.`b` from `test`.`t2` where `test`.`t1`.`a` < 3 and <cache>(`test`.`t1`.`a`) = `test`.`t2`.`b`))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -353,6 +366,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t2`,`test`.`t2` set `test`.`t1`.`a` = 10 where `test`.`t2`.`b` < 3
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -405,6 +420,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t11 ALL NULL NULL NULL NULL 3 100.00
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` `t11`,(/* select#2 */ select `test`.`t2`.`b` AS `b` from `test`.`t2`) `t12` set `test`.`t11`.`a` = `test`.`t11`.`a` + 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -457,6 +474,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00
1 PRIMARY t11 ALL NULL NULL NULL NULL 3 100.00
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` `t11`,(/* select#2 */ select 1 AS `1`) `t12` set `test`.`t11`.`a` = `test`.`t11`.`a` + 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -511,6 +530,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t11 ALL NULL NULL NULL NULL 3 100.00 Using where
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` `t11`,(/* select#2 */ select `test`.`t2`.`b` AS `b` from `test`.`t2`) `t12` set `test`.`t11`.`a` = 10 where `test`.`t11`.`a` > 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -555,6 +576,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a > 1 LIMIT 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` > 1 limit 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -598,6 +621,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+Warnings:
+Note 1003 delete from `test`.`t1` where 0
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -638,6 +663,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 USING t1 WHERE 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+Warnings:
+Note 1003 delete from `test`.`t1` using `test`.`t1` where 0
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -678,6 +705,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a = 3;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range a a 5 NULL 1 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` = 3
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 5
@@ -719,6 +748,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a < 3;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range a a 5 NULL 1 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` < 3
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 5
@@ -758,6 +789,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` > 0 order by `test`.`t1`.`a`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -797,6 +830,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 3 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` > 0 order by `test`.`t1`.`a`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -840,6 +875,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE (@a:= a) ORDER BY a LIMIT 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 index NULL PRIMARY 4 NULL 1 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where @a:=`test`.`t1`.`a` order by `test`.`t1`.`a` limit 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -884,6 +921,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 ORDER BY a ASC, b ASC LIMIT 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 100.00 Using filesort
+Warnings:
+Note 1003 delete from `test`.`t1` order by `test`.`t1`.`a`,`test`.`t1`.`b` limit 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 7
@@ -941,6 +980,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
1 SIMPLE t2 ref PRIMARY PRIMARY 4 test.t1.a1 1 100.00
1 SIMPLE t3 eq_ref PRIMARY PRIMARY 8 test.t2.b2,test.t1.b1 1 100.00
+Warnings:
+Note 1003 delete from `test`.`t1`,`test`.`t2`,`test`.`t3` using `test`.`t1`,`test`.`t2`,`test`.`t3` where `test`.`t2`.`a2` = `test`.`t1`.`a1` and `test`.`t3`.`a3` = `test`.`t2`.`b2` and `test`.`t3`.`b3` = `test`.`t1`.`b1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 13
@@ -993,6 +1034,8 @@ EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`a` = 10 where <in_optimizer>(`test`.`t1`.`a`,<exists>(/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where <cache>(`test`.`t1`.`a`) = `test`.`t2`.`a`))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1043,6 +1086,8 @@ EXPLAIN EXTENDED DELETE FROM t1 WHERE a1 IN (SELECT a2 FROM t2 WHERE a2 > 2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 5 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ delete from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a1`,<exists>(/* select#2 */ select `test`.`t2`.`a2` from `test`.`t2` where `test`.`t2`.`a2` > 2 and <cache>(`test`.`t1`.`a1`) = `test`.`t2`.`a2`))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1088,6 +1133,8 @@ EXPLAIN EXTENDED DELETE FROM t1 WHERE a1 IN (SELECT a2 FROM t2 WHERE a2 > 2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 5 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ delete from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a1`,<exists>(/* select#2 */ select `test`.`t2`.`a2` from `test`.`t2` where `test`.`t2`.`a2` > 2 and <cache>(`test`.`t1`.`a1`) = `test`.`t2`.`a2`))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1132,6 +1179,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET i = 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`i` = 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -1175,6 +1224,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL 5 NULL Deleting all rows
+Warnings:
+Note 1003 delete from `test`.`t1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -1221,6 +1272,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 15 NULL 5 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1267,6 +1320,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED INSERT INTO t2 SELECT * FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 insert into `test`.`t2` select `test`.`t1`.`i` AS `i` from `test`.`t1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1311,6 +1366,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED REPLACE INTO t2 SELECT * FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 replaceinto `test`.`t2` select `test`.`t1`.`i` AS `i` from `test`.`t1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1351,6 +1408,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED INSERT INTO t1 SET i = 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 INSERT t1 ALL NULL NULL NULL NULL NULL 100.00 NULL
+Warnings:
+Note 1003 insert into `test`.`t1`(i) values (10)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -1374,6 +1433,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED REPLACE INTO t1 SET i = 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 INSERT t1 ALL NULL NULL NULL NULL NULL 100.00 NULL
+Warnings:
+Note 1003 replaceinto `test`.`t1`(i) values (10)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -1402,6 +1463,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 5 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`i` > 10 and `test`.`t1`.`i` <= 18 order by `test`.`t1`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1447,6 +1510,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`i` > 10 and `test`.`t1`.`i` <= 18 order by `test`.`t1`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1500,6 +1565,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1554,6 +1621,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 15 NULL 5 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1603,6 +1672,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1657,6 +1728,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1712,6 +1785,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE key1 < 13 or key2 < 14 ORDER BY key1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index_merge key1,key2 key1,key2 5,5 NULL 7 100.00 Using sort_union(key1,key2); Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`key1` < 13 or `test`.`t2`.`key2` < 14 order by `test`.`t2`.`key1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -1765,6 +1840,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE i > 10 AND i <= 18 ORDER BY i DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 range PRIMARY PRIMARY 4 NULL 5 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1812,6 +1889,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 ORDER BY a, b DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` order by `test`.`t2`.`a`,`test`.`t2`.`b` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -1866,6 +1945,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 ORDER BY a DESC, b DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 6 NULL 5 100.00
+Warnings:
+Note 1003 delete from `test`.`t2` order by `test`.`t2`.`a` desc,`test`.`t2`.`b` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -1915,6 +1996,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET a = 10 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 range PRIMARY PRIMARY 4 NULL 5 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`a` = 10 where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1963,6 +2046,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET a = 10 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`a` = 10 where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -2017,6 +2102,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET d = 10 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`d` = 10 where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -2072,6 +2159,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET d = 10 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 15 NULL 5 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`d` = 10 where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -2122,6 +2211,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET d = 10 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`d` = 10 where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -2176,6 +2267,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET d = 10 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`d` = 10 where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -2231,6 +2324,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET i = 123 WHERE key1 < 13 or key2 < 14 ORDER BY key1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index_merge key1,key2 key1,key2 5,5 NULL 7 100.00 Using sort_union(key1,key2); Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`i` = 123 where `test`.`t2`.`key1` < 13 or `test`.`t2`.`key2` < 14 order by `test`.`t2`.`key1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2284,6 +2379,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET a = 10 WHERE i > 10 AND i <= 18 ORDER BY i DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 range PRIMARY PRIMARY 4 NULL 5 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`a` = 10 where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -2332,6 +2429,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET c = 10 ORDER BY a, b DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`c` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`b` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2387,6 +2486,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET c = 10 ORDER BY a DESC, b DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 6 NULL 5 100.00 Using buffer
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`c` = 10 order by `test`.`t2`.`a` desc,`test`.`t2`.`b` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2439,6 +2540,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET c2 = 0 WHERE c1_idx = 'y' ORDER BY pk DESC LIMIT 2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range c1_idx c1_idx 2 NULL 2 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`c2` = 0 where `test`.`t1`.`c1_idx` = 'y' order by `test`.`t1`.`pk` desc limit 2
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2485,6 +2588,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE c1_idx = 'y' ORDER BY pk DESC LIMIT 2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range c1_idx c1_idx 2 NULL 2 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`c1_idx` = 'y' order by `test`.`t1`.`pk` desc limit 2
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2534,6 +2639,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a=a+10 WHERE a > 34;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 2 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`a` = `test`.`t1`.`a` + 10 where `test`.`t1`.`a` > 34
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -2581,6 +2688,8 @@ EXPLAIN EXTENDED UPDATE t1 LEFT JOIN t2 ON t1.c1 = t2.c1 SET t2.c2 = 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 system NULL NULL NULL NULL 0 0.00 Const row not found
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t2` set NULL = 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 7
@@ -2624,6 +2733,8 @@ EXPLAIN EXTENDED UPDATE t1 LEFT JOIN t2 ON t1.c1 = t2.c1 SET t2.c2 = 10 W
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 system NULL NULL NULL NULL 0 0.00 Const row not found
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t2` set NULL = 10 where `test`.`t1`.`c3` = 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 7
@@ -2676,6 +2787,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
2 DEPENDENT SUBQUERY t2 ALL IDX NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.f1' of SELECT #2 was resolved in SELECT #1
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`f2` = (/* select#2 */ select max(`test`.`t2`.`f4`) from `test`.`t2` where `test`.`t2`.`f3` = `test`.`t1`.`f1`)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 7
@@ -2747,6 +2859,8 @@ EXPLAIN EXTENDED UPDATE v1 SET a = 1 WHERE a > 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t11 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t12 ALL NULL NULL NULL NULL 2 100.00
+Warnings:
+Note 1003 update `test`.`t1` `t11`,`test`.`t1` `t12` set `test`.`t11`.`a` = 1 where `test`.`t11`.`a` > 0
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -2792,6 +2906,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
1 SIMPLE t11 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t12 ALL NULL NULL NULL NULL 2 100.00
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t1` `t11`,`test`.`t1` `t12` set `test`.`t11`.`a` = 1 where `test`.`t11`.`a` = `test`.`t1`.`a`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -2841,6 +2957,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM v1 WHERE a < 4;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 3 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ delete from `test`.`t1` where `test`.`t1`.`a` < 4
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -2892,6 +3010,8 @@ EXPLAIN EXTENDED DELETE v1 FROM t2, v1 WHERE t2.x = v1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using where
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t2.x 1 100.00
+Warnings:
+Note 1003 delete from `test`.`t1` using `test`.`t2`,`test`.`t1` where `test`.`t1`.`a` = `test`.`t2`.`x`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2943,6 +3063,8 @@ EXPLAIN EXTENDED DELETE v1 FROM t2, v1 WHERE t2.x = v1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using where
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t2.x 1 100.00
+Warnings:
+Note 1003 delete from `test`.`t1` using `test`.`t2`,`test`.`t1` where `test`.`t1`.`a` = `test`.`t2`.`x`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2987,6 +3109,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED INSERT INTO v1 VALUES (10);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 INSERT t1 ALL NULL NULL NULL NULL NULL 100.00 NULL
+Warnings:
+Note 1003 insert into `test`.`t1`(x) values (10)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -3027,6 +3151,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED INSERT INTO v1 SELECT * FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 system NULL NULL NULL NULL 0 0.00 Const row not found
+Warnings:
+Note 1003 insert into `test`.`t2`(x) /* select#1 */ select NULL AS `a` from `test`.`t1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -3084,6 +3210,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY <derived3> index_subquery key0 key0 5 func 2 100.00
3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`a` = 10 where <in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in (temporary) on key0)))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -3140,6 +3268,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00
2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 3 100.00
3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1`,`test`.`t2`,(/* select#3 */ select `test`.`t2`.`b` AS `b` from `test`.`t2` order by `test`.`t2`.`b` limit 2,2) `x` set `test`.`t1`.`a` = 10 where 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -3198,6 +3328,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
3 MATERIALIZED <derived4> ALL NULL NULL NULL NULL 3 100.00
4 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
2 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1`,(/* select#2 */ select `test`.`t2`.`b` AS `b` from `test`.`t2`) `y`,(/* select#4 */ select `test`.`t2`.`b` AS `b` from `test`.`t2` order by `test`.`t2`.`b` limit 2,2) `x` set `test`.`t1`.`a` = 10 where 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -3248,6 +3380,8 @@ JOIN t1 AS a12 ON a12.c1 = a11.c1
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 0 100.00
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t3` set `test`.`t3`.`c3` = (/* select#2 */ select count(NULL) from `test`.`t1` `a11` straight_join `test`.`t2` `a21` join `test`.`t1` `a12` where 0)
DROP TABLE t1, t2, t3;
#73
CREATE TABLE t1 (id INT);
@@ -3276,6 +3410,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a=a+1 WHERE a>10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 1 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`a` = `test`.`t1`.`a` + 1 where `test`.`t1`.`a` > 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -3315,6 +3451,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a=a+1 WHERE a>10 ORDER BY a+20;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 1 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`a` = `test`.`t1`.`a` + 1 where `test`.`t1`.`a` > 10 order by `test`.`t1`.`a` + 20
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
diff --git a/mysql-test/main/opt_trace.result b/mysql-test/main/opt_trace.result
index a343d59..44ce91c 100644
--- a/mysql-test/main/opt_trace.result
+++ b/mysql-test/main/opt_trace.result
@@ -3766,7 +3766,7 @@ explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3 {
"select_id": 1,
"steps": [
{
- "expanded_query": "select NULL AS `NULL` from t0 join t1 where t0.a = t1.a and t1.a < 3"
+ "expanded_query": "delete from t0,t1 using t0,t1 where t0.a = t1.a and t1.a < 3"
}
]
}
diff --git a/mysql-test/main/ps.result b/mysql-test/main/ps.result
index 673b18c..408b1ec 100644
--- a/mysql-test/main/ps.result
+++ b/mysql-test/main/ps.result
@@ -5566,11 +5566,15 @@ EXPLAIN EXTENDED UPDATE t3 SET c3 = ( SELECT COUNT(d1.c1) FROM ( SELECT a11.c1 F
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 0 100.00
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t3` set `test`.`t3`.`c3` = (/* select#2 */ select count(NULL) from `test`.`t1` `a11` straight_join `test`.`t2` `a21` join `test`.`t1` `a12` where 0)
PREPARE stmt FROM "EXPLAIN EXTENDED UPDATE t3 SET c3 = ( SELECT COUNT(d1.c1) FROM ( SELECT a11.c1 FROM t1 AS a11 STRAIGHT_JOIN t2 AS a21 ON a21.c2 = a11.c1 JOIN t1 AS a12 ON a12.c1 = a11.c1 ) d1 )";
EXECUTE stmt;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 0 100.00
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t3` set `test`.`t3`.`c3` = (/* select#2 */ select count(NULL) from `test`.`t1` `a11` straight_join `test`.`t2` `a21` join `test`.`t1` `a12` where 0)
DEALLOCATE PREPARE stmt;
DROP TABLE t1, t2, t3;
#
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 1b3fa30..776dfff 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -1084,6 +1084,8 @@ int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list,
DBUG_RETURN(TRUE);
select_lex->fix_prepare_information(thd, conds, &fake_conds);
+ if (!thd->lex->upd_del_where)
+ thd->lex->upd_del_where= *conds;
DBUG_RETURN(FALSE);
}
diff --git a/sql/sql_explain.cc b/sql/sql_explain.cc
index 70e3009..6b76db8 100644
--- a/sql/sql_explain.cc
+++ b/sql/sql_explain.cc
@@ -161,7 +161,7 @@ void Explain_query::query_plan_ready()
Send EXPLAIN output to the client.
*/
-int Explain_query::send_explain(THD *thd)
+int Explain_query::send_explain(THD *thd, bool extended)
{
select_result *result;
LEX *lex= thd->lex;
@@ -174,8 +174,22 @@ int Explain_query::send_explain(THD *thd)
if (thd->lex->explain_json)
print_explain_json(result, thd->lex->analyze_stmt);
else
+ {
res= print_explain(result, lex->describe, thd->lex->analyze_stmt);
-
+ if (extended)
+ {
+ char buff[1024];
+ String str(buff,(uint32) sizeof(buff), system_charset_info);
+ str.length(0);
+ /*
+ The warnings system requires input in utf8, @see
+ mysqld_show_warnings().
+ */
+ lex->unit.print(&str, QT_EXPLAIN_EXTENDED);
+ push_warning(thd, Sql_condition::WARN_LEVEL_NOTE,
+ ER_YES, str.c_ptr_safe());
+ }
+ }
if (res)
result->abort_result_set();
else
@@ -185,6 +199,7 @@ int Explain_query::send_explain(THD *thd)
}
+
/*
The main entry point to print EXPLAIN of the entire query
*/
diff --git a/sql/sql_explain.h b/sql/sql_explain.h
index 7b5042b..3add404 100644
--- a/sql/sql_explain.h
+++ b/sql/sql_explain.h
@@ -474,7 +474,7 @@ class Explain_query : public Sql_alloc
bool is_analyze);
/* Send tabular EXPLAIN to the client */
- int send_explain(THD *thd);
+ int send_explain(THD *thd, bool extended);
/* Return tabular EXPLAIN output as a text string */
bool print_explain_str(THD *thd, String *out_str, bool is_analyze);
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index f31c9ea..9a76061 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -827,7 +827,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
save_insert_query_plan(thd, table_list);
if (thd->lex->describe)
{
- retval= thd->lex->explain->send_explain(thd);
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ retval= thd->lex->explain->send_explain(thd, extended);
goto abort;
}
@@ -1250,7 +1251,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
goto abort;
if (thd->lex->analyze_stmt)
{
- retval= thd->lex->explain->send_explain(thd);
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ retval= thd->lex->explain->send_explain(thd, extended);
goto abort;
}
DBUG_PRINT("info", ("touched: %llu copied: %llu updated: %llu deleted: %llu",
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index e14d9f7..e7689a9 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -780,6 +780,8 @@ void LEX::start(THD *thd_arg)
frame_bottom_bound= NULL;
win_spec= NULL;
+ upd_del_where= NULL;
+
vers_conditions.empty();
period_conditions.empty();
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index 769b1750..6170637 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -1417,6 +1417,10 @@ class st_select_lex: public st_select_lex_node
}
bool setup_ref_array(THD *thd, uint order_group_num);
void print(THD *thd, String *str, enum_query_type query_type);
+ void print_item_list(THD *thd, String *str, enum_query_type query_type);
+ void print_set_clause(THD *thd, String *str, enum_query_type query_type);
+ void print_on_duplicate_key_clause(THD *thd, String *str,
+ enum_query_type query_type);
static void print_order(String *str,
ORDER *order,
enum_query_type query_type);
@@ -3493,6 +3497,8 @@ struct LEX: public Query_tables_list
Window_frame_bound *frame_bottom_bound;
Window_spec *win_spec;
+ Item *upd_del_where;
+
/* System Versioning */
vers_select_conds_t vers_conditions;
vers_select_conds_t period_conditions;
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 1f1962a..c495ae2 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -4744,7 +4744,10 @@ mysql_execute_command(THD *thd)
}
if (!res && (explain || lex->analyze_stmt))
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
/* revert changes for SP */
MYSQL_INSERT_SELECT_DONE(res, (ulong) thd->get_row_count_func());
@@ -4813,7 +4816,10 @@ mysql_execute_command(THD *thd)
if (thd->lex->analyze_stmt || thd->lex->describe)
{
if (!res)
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
}
delete sel_result;
@@ -4875,7 +4881,10 @@ mysql_execute_command(THD *thd)
else
{
if (lex->describe || lex->analyze_stmt)
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
}
multi_delete_error:
delete result;
@@ -6463,7 +6472,10 @@ static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
thd->protocol= save_protocol;
}
if (!res)
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
}
}
}
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 2c12d1c..f38a7e3 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -305,6 +305,9 @@ static Item **get_sargable_cond(JOIN *join, TABLE *table);
bool is_eq_cond_injected_for_split_opt(Item_func_eq *eq_item);
+void print_list_item(String *str, List_item *list,
+ enum_query_type query_type);
+
#ifndef DBUG_OFF
/*
@@ -27981,6 +27984,162 @@ void TABLE_LIST::print(THD *thd, table_map eliminated_tables, String *str,
}
}
+enum explainable_cmd_type
+{
+ SELECT_CMD, INSERT_CMD, REPLACE_CMD, UPDATE_CMD, DELETE_CMD, NO_CMD
+};
+
+static
+const char * const explainable_cmd_name []=
+{
+ "select ",
+ "insert ",
+ "replace ",
+ "update ",
+ "delete ",
+};
+
+static
+char const *get_explainable_cmd_name(enum explainable_cmd_type cmd)
+{
+ return explainable_cmd_name[cmd];
+}
+
+static
+enum explainable_cmd_type get_explainable_cmd_type(THD *thd)
+{
+ switch (thd->lex->sql_command) {
+ case SQLCOM_SELECT:
+ return SELECT_CMD;
+ case SQLCOM_INSERT:
+ case SQLCOM_INSERT_SELECT:
+ return INSERT_CMD;
+ case SQLCOM_REPLACE:
+ case SQLCOM_REPLACE_SELECT:
+ return REPLACE_CMD;
+ case SQLCOM_UPDATE:
+ case SQLCOM_UPDATE_MULTI:
+ return UPDATE_CMD;
+ case SQLCOM_DELETE:
+ case SQLCOM_DELETE_MULTI:
+ return DELETE_CMD;
+ default:
+ return SELECT_CMD;
+ }
+}
+
+
+void TABLE_LIST::print_leaf_tables(THD *thd, String *str,
+ enum_query_type query_type)
+{
+ if (merge_underlying_list)
+ {
+ for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
+ tbl->print_leaf_tables(thd, str, query_type);
+ }
+ else
+ print(thd, 0, str, query_type);
+}
+
+
+void st_select_lex::print_item_list(THD *thd, String *str,
+ enum_query_type query_type)
+{
+ bool first= 1;
+ /*
+ outer_select() can not be used here because it is for name resolution
+ and will return NULL at any end of name resolution chain (view/derived)
+ */
+ bool top_level= (get_master()->get_master() == 0);
+ List_iterator_fast<Item> it(item_list);
+ Item *item;
+ while ((item= it++))
+ {
+ if (first)
+ first= 0;
+ else
+ str->append(',');
+
+ if ((is_subquery_function() && item->is_autogenerated_name) ||
+ !item->name.str)
+ {
+ /*
+ Do not print auto-generated aliases in subqueries. It has no purpose
+ in a view definition or other contexts where the query is printed.
+ */
+ item->print(str, query_type);
+ }
+ else
+ {
+ /*
+ Do not print illegal names (if it is not top level SELECT).
+ Top level view checked (and correct name are assigned),
+ other cases of top level SELECT are not important, because
+ it is not "table field".
+ */
+ if (top_level ||
+ !item->is_autogenerated_name ||
+ !check_column_name(item->name.str))
+ item->print_item_w_name(str, query_type);
+ else
+ item->print(str, query_type);
+ }
+ }
+}
+
+
+void st_select_lex::print_set_clause(THD *thd, String *str,
+ enum_query_type query_type)
+{
+ bool first= 1;
+ /*
+ outer_select() can not be used here because it is for name resolution
+ and will return NULL at any end of name resolution chain (view/derived)
+ */
+ List_iterator_fast<Item> it(item_list);
+ List_iterator_fast<Item> vt(thd->lex->value_list);
+ Item *item;
+ Item *val;
+ while ((item= it++, val= vt++ ))
+ {
+ if (first)
+ {
+ str->append(STRING_WITH_LEN(" set "));
+ first= 0;
+ }
+ else
+ str->append(',');
+
+ item->print(str, query_type);
+ str->append(STRING_WITH_LEN(" = "));
+ val->print(str, query_type);
+ }
+}
+
+
+void st_select_lex::print_on_duplicate_key_clause(THD *thd, String *str,
+ enum_query_type query_type)
+{
+ bool first= 1;
+ List_iterator_fast<Item> it(thd->lex->update_list);
+ List_iterator_fast<Item> vt(thd->lex->value_list);
+ Item *item;
+ Item *val;
+ while ((item= it++, val= vt++ ))
+ {
+ if (first)
+ {
+ str->append(STRING_WITH_LEN(" on duplicate key update "));
+ first= 0;
+ }
+ else
+ str->append(',');
+
+ item->print(str, query_type);
+ str->append(STRING_WITH_LEN(" = "));
+ val->print(str, query_type);
+ }
+}
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
{
@@ -27998,6 +28157,61 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
return;
}
+ bool top_level= (get_master()->get_master() == 0);
+ enum explainable_cmd_type sel_type= SELECT_CMD;
+ if (top_level)
+ sel_type= get_explainable_cmd_type(thd);
+
+ if (sel_type == INSERT_CMD || sel_type == REPLACE_CMD)
+ {
+ str->append(STRING_WITH_LEN(get_explainable_cmd_name(sel_type)));
+ str->append(STRING_WITH_LEN("into "));
+ TABLE_LIST *tbl= thd->lex->query_tables;
+ while (tbl->merge_underlying_list)
+ tbl= tbl->merge_underlying_list;
+ tbl->print(thd, 0, str, query_type);
+ if (thd->lex->field_list.elements)
+ {
+ str->append ('(');
+ List_iterator_fast<Item> it(thd->lex->field_list);
+ Item *item;
+ bool first= true;
+ while ((item= it++))
+ {
+ if (first)
+ first= false;
+ else
+ str->append(',');
+ str->append(item->name);
+ }
+ str->append(')');
+ }
+
+ str->append(' ');
+
+ if (thd->lex->sql_command == SQLCOM_INSERT ||
+ thd->lex->sql_command == SQLCOM_REPLACE)
+ {
+ str->append(STRING_WITH_LEN("values "));
+ bool is_first_elem= true;
+ List_iterator_fast<List_item> li(thd->lex->many_values);
+ List_item *list;
+
+ while ((list= li++))
+ {
+ if (is_first_elem)
+ is_first_elem= false;
+ else
+ str->append(',');
+
+ print_list_item(str, list, query_type);
+ }
+ if (thd->lex->update_list.elements)
+ print_on_duplicate_key_clause(thd, str, query_type);
+ return;
+ }
+ }
+
if ((query_type & QT_SHOW_SELECT_NUMBER) &&
thd->lex->all_selects_list &&
thd->lex->all_selects_list->link_next &&
@@ -28021,7 +28235,10 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
str->append(" */ ");
}
- str->append(STRING_WITH_LEN("select "));
+ if (sel_type == SELECT_CMD ||
+ sel_type == INSERT_CMD ||
+ sel_type == REPLACE_CMD)
+ str->append(STRING_WITH_LEN("select "));
if (join && join->cleaned)
{
@@ -28067,56 +28284,59 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
}
//Item List
- bool first= 1;
+ if (sel_type == SELECT_CMD ||
+ sel_type == INSERT_CMD ||
+ sel_type == REPLACE_CMD)
+ print_item_list(thd, str, query_type);
/*
- outer_select() can not be used here because it is for name resolution
- and will return NULL at any end of name resolution chain (view/derived)
+ from clause
+ TODO: support USING/FORCE/IGNORE index
*/
- bool top_level= (get_master()->get_master() == 0);
- List_iterator_fast<Item> it(item_list);
- Item *item;
- while ((item= it++))
+ if (table_list.elements)
{
- if (first)
- first= 0;
- else
- str->append(',');
-
- if ((is_subquery_function() && item->is_autogenerated_name) ||
- !item->name.str)
+ if (sel_type == SELECT_CMD ||
+ sel_type == INSERT_CMD ||
+ sel_type == REPLACE_CMD)
{
- /*
- Do not print auto-generated aliases in subqueries. It has no purpose
- in a view definition or other contexts where the query is printed.
- */
- item->print(str, query_type);
+ str->append(STRING_WITH_LEN(" from "));
+ /* go through join tree */
+ print_join(thd, join? join->eliminated_tables: 0, str, &top_join_list,
+ query_type);
}
- else
+ if (sel_type == UPDATE_CMD || sel_type == DELETE_CMD)
+ str->append(STRING_WITH_LEN(get_explainable_cmd_name(sel_type)));
+ if (sel_type == DELETE_CMD)
{
- /*
- Do not print illegal names (if it is not top level SELECT).
- Top level view checked (and correct name are assigned),
- other cases of top level SELECT are not important, because
- it is not "table field".
- */
- if (top_level ||
- !item->is_autogenerated_name ||
- !check_column_name(item->name.str))
- item->print_item_w_name(str, query_type);
- else
- item->print(str, query_type);
+ str->append(STRING_WITH_LEN(" from "));
+ bool first= true;
+ for (TABLE_LIST *target_tbl= thd->lex->auxiliary_table_list.first;
+ target_tbl;
+ target_tbl= target_tbl->next_local)
+ {
+ if (first)
+ first= false;
+ else
+ str->append(',');
+ target_tbl->correspondent_table->print_leaf_tables(thd, str,
+ query_type);
+ }
+ if (!first)
+ str->append(STRING_WITH_LEN(" using "));
+ }
+ if (sel_type == UPDATE_CMD || sel_type == DELETE_CMD)
+ {
+ bool first= true;
+ List_iterator_fast<TABLE_LIST> li(leaf_tables);
+ TABLE_LIST *tbl;
+ while ((tbl= li++))
+ {
+ if (first)
+ first= false;
+ else
+ str->append(',');
+ tbl->print(thd, 0, str, query_type);
+ }
}
- }
-
- /*
- from clause
- TODO: support USING/FORCE/IGNORE index
- */
- if (table_list.elements)
- {
- str->append(STRING_WITH_LEN(" from "));
- /* go through join tree */
- print_join(thd, join? join->eliminated_tables: 0, str, &top_join_list, query_type);
}
else if (where)
{
@@ -28127,10 +28347,15 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
str->append(STRING_WITH_LEN(" from DUAL "));
}
+ if (sel_type == UPDATE_CMD)
+ print_set_clause(thd, str, query_type);
+
// Where
Item *cur_where= where;
if (join)
cur_where= join->conds;
+ else if (sel_type == UPDATE_CMD || sel_type == DELETE_CMD)
+ cur_where= thd->lex->upd_del_where;
if (cur_where || cond_value != Item::COND_UNDEF)
{
str->append(STRING_WITH_LEN(" where "));
@@ -28187,6 +28412,15 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
else if (lock_type == TL_WRITE)
str->append(" for update");
+ if ((sel_type == INSERT_CMD || sel_type == REPLACE_CMD) &&
+ thd->lex->update_list.elements)
+ print_on_duplicate_key_clause(thd, str, query_type);
+
+ // returning clause
+ if (sel_type == DELETE_CMD && !item_list.elements)
+ {
+ print_item_list(thd, str, query_type);
+ }
// PROCEDURE unsupported here
}
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index a298071..834fa61 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -1371,7 +1371,8 @@ int mysql_update(THD *thd,
goto err;
emit_explain_and_leave:
- int err2= thd->lex->explain->send_explain(thd);
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ int err2= thd->lex->explain->send_explain(thd, extended);
delete select;
free_underlaid_joins(thd, select_lex);
@@ -1445,6 +1446,8 @@ bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list,
select_lex->fix_prepare_information(thd, conds, &fake_conds);
+ if (!thd->lex->upd_del_where)
+ thd->lex->upd_del_where= *conds;
DBUG_RETURN(FALSE);
}
@@ -1974,7 +1977,10 @@ bool mysql_multi_update(THD *thd, TABLE_LIST *table_list, List<Item> *fields,
else
{
if (thd->lex->describe || thd->lex->analyze_stmt)
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
}
thd->abort_on_warning= 0;
DBUG_RETURN(res);
diff --git a/sql/table.h b/sql/table.h
index c4b0d78..6f7f4e6 100644
--- a/sql/table.h
+++ b/sql/table.h
@@ -2708,6 +2708,8 @@ struct TABLE_LIST
}
void print(THD *thd, table_map eliminated_tables, String *str,
enum_query_type query_type);
+ void print_leaf_tables(THD *thd, String *str,
+ enum_query_type query_type);
bool check_single_table(TABLE_LIST **table, table_map map,
TABLE_LIST *view);
bool set_insert_values(MEM_ROOT *mem_root);
1
0
[Commits] c8613d7: MDEV-30706 Different results of selects from view and CTE with same definition
by IgorBabaev 02 Mar '23
by IgorBabaev 02 Mar '23
02 Mar '23
revision-id: c8613d7ddf380eb9eb9a2e070e55dbe7ebaf37cf (mariadb-10.4.28-31-gc8613d7)
parent(s): 476b24d084e7e717310155bb986eb086d3c1e1a6
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-03-01 22:49:27 -0800
message:
MDEV-30706 Different results of selects from view and CTE with same definition
MDEV-30668 Set function aggregated in outer select used in view definition
This patch fixes two bugs concerning views whose specifications contain
subqueries with set functions aggregated in outer selects.
Due to the first bug those such views that have implicit grouping were
considered as mergeable. This led to wrong result sets for selects from
these views.
Due to the second bug the aggregation select was determined incorrectly and
this led to bogus error messages.
The patch added several test cases for these two bugs and for four other
duplicate bugs.
The patch also enables view-protocol for many other test cases.
Approved by Oleksandr Byelkin <sanja(a)mariadb.com>
---
mysql-test/main/derived_view.result | 482 +++++++++++++++++++++++
mysql-test/main/derived_view.test | 297 ++++++++++++++
mysql-test/main/func_group.result | 9 +-
mysql-test/main/func_group.test | 11 +-
mysql-test/main/group_by.result | 10 +-
mysql-test/main/group_by.test | 23 +-
mysql-test/main/opt_trace.result | 12 +-
mysql-test/main/subselect.result | 262 ++++++++----
mysql-test/main/subselect.test | 215 +++++-----
mysql-test/main/subselect4.result | 4 +-
mysql-test/main/subselect_no_exists_to_in.result | 262 ++++++++----
mysql-test/main/subselect_no_mat.result | 262 ++++++++----
mysql-test/main/subselect_no_opts.result | 262 ++++++++----
mysql-test/main/subselect_no_scache.result | 262 ++++++++----
mysql-test/main/subselect_no_semijoin.result | 262 ++++++++----
sql/item.cc | 16 +-
sql/item_sum.cc | 3 +-
sql/sql_base.cc | 3 +-
sql/sql_derived.cc | 18 -
sql/sql_lex.cc | 43 +-
sql/sql_lex.h | 3 +-
sql/table.cc | 16 +-
sql/table.h | 7 +-
23 files changed, 2021 insertions(+), 723 deletions(-)
diff --git a/mysql-test/main/derived_view.result b/mysql-test/main/derived_view.result
index 3df1acc..ab32e47 100644
--- a/mysql-test/main/derived_view.result
+++ b/mysql-test/main/derived_view.result
@@ -3691,3 +3691,485 @@ drop procedure sp2;
drop view v, v2;
drop table t1,t2;
# End of 10.2 tests
+#
+# MDEV-30706: view defined as select with implicit grouping and
+# a set function used in a subquery
+#
+CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
+INSERT INTO t1 VALUES (1,1), (2,2);
+CREATE TABLE t2 (a INT PRIMARY KEY, b INT);
+INSERT INTO t2 VALUES (1,1), (3,3);
+CREATE TABLE t3 (a INT PRIMARY KEY, b INT);
+INSERT INTO t3 VALUES (2,2), (4,4), (7,7);
+CREATE TABLE t4 (a INT PRIMARY KEY, b INT);
+INSERT INTO t4 VALUES (2,2), (5,5), (7,7);
+CREATE VIEW v AS SELECT
+(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
+FROM t3, t4
+WHERE t3.a = t4.b;
+SELECT
+(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
+FROM t3, t4
+WHERE t3.a = t4.b;
+m
+9
+SELECT * FROM v;
+m
+9
+WITH cte AS ( SELECT
+(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
+FROM t3, t4
+WHERE t3.a = t4.b ) SELECT * FROM cte;
+m
+9
+EXPLAIN SELECT
+(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
+FROM t3, t4
+WHERE t3.a = t4.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t4 ALL NULL NULL NULL NULL 3 Using where
+1 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t4.b 1 Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using temporary; Using filesort
+2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.t2.b 1 Using index
+EXPLAIN SELECT * FROM v;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
+2 SUBQUERY t4 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t3 eq_ref PRIMARY PRIMARY 4 test.t4.b 1 Using index
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using temporary; Using filesort
+3 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.t2.b 1 Using index
+EXPLAIN WITH cte AS ( SELECT
+(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
+FROM t3, t4
+WHERE t3.a = t4.b ) SELECT * FROM cte;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
+2 DERIVED t4 ALL NULL NULL NULL NULL 3 Using where
+2 DERIVED t3 eq_ref PRIMARY PRIMARY 4 test.t4.b 1 Using index
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using temporary; Using filesort
+3 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.t2.b 1 Using index
+PREPARE stmt FROM "SELECT
+(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
+FROM t3, t4
+WHERE t3.a = t4.b";
+execute stmt;
+m
+9
+execute stmt;
+m
+9
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "SELECT * FROM v";
+execute stmt;
+m
+9
+execute stmt;
+m
+9
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "WITH cte AS ( SELECT
+(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
+FROM t3, t4
+WHERE t3.a = t4.b ) SELECT * FROM cte";
+execute stmt;
+m
+9
+execute stmt;
+m
+9
+DEALLOCATE PREPARE stmt;
+DROP VIEW v;
+DROP TABLE t1,t2,t3,t4;
+#
+# MDEV-29224: view defined as select with implicit grouping and
+# a set function used in a subquery
+#
+CREATE TABLE t1 (f1 INT);
+INSERT INTO t1 VALUES (1),(2);
+CREATE TABLE t2 (f2 int);
+INSERT INTO t2 VALUES (3);
+CREATE VIEW v AS SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
+SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
+( SELECT MAX(f1) FROM t2 )
+2
+SELECT * FROM v;
+( SELECT MAX(f1) FROM t2 )
+2
+WITH cte AS ( SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1 ) SELECT * FROM cte;
+( SELECT MAX(f1) FROM t2 )
+2
+EXPLAIN SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 2
+2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
+EXPLAIN SELECT * FROM v;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2
+2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
+3 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
+EXPLAIN WITH cte AS ( SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1 ) SELECT * FROM cte;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2
+2 DERIVED t1 ALL NULL NULL NULL NULL 2
+3 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
+PREPARE stmt FROM "SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1";
+execute stmt;
+( SELECT MAX(f1) FROM t2 )
+2
+execute stmt;
+( SELECT MAX(f1) FROM t2 )
+2
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "SELECT * FROM v";
+execute stmt;
+( SELECT MAX(f1) FROM t2 )
+2
+execute stmt;
+( SELECT MAX(f1) FROM t2 )
+2
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "WITH cte AS ( SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1 ) SELECT * FROM cte";
+execute stmt;
+( SELECT MAX(f1) FROM t2 )
+2
+execute stmt;
+( SELECT MAX(f1) FROM t2 )
+2
+DEALLOCATE PREPARE stmt;
+DROP VIEW v;
+DROP TABLE t1,t2;
+#
+# MDEV-28573: view defined as select with implicit grouping and
+# a set function used in a subquery
+#
+CREATE TABLE t1 (a INTEGER, b INTEGER);
+CREATE TABLE t2 (c INTEGER);
+INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
+INSERT INTO t2 VALUES (1), (2);
+CREATE VIEW v1 AS SELECT (SELECT COUNT(b) FROM t2) FROM t1;
+CREATE VIEW v2 AS SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1;
+SELECT (SELECT COUNT(b) FROM t2) FROM t1;
+ERROR 21000: Subquery returns more than 1 row
+SELECT * FROM v1;
+ERROR 21000: Subquery returns more than 1 row
+WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2) FROM t1 ) SELECT * FROM cte;
+ERROR 21000: Subquery returns more than 1 row
+SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+SELECT * FROM v2;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1 ) SELECT * FROM cte;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+EXPLAIN SELECT (SELECT COUNT(b) FROM t2) FROM t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2
+EXPLAIN SELECT * FROM v1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
+2 SUBQUERY t1 ALL NULL NULL NULL NULL 3
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2
+EXPLAIN WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2) FROM t1 ) SELECT * FROM cte;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
+2 DERIVED t1 ALL NULL NULL NULL NULL 3
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2
+PREPARE stmt FROM "SELECT (SELECT COUNT(b) FROM t2) FROM t1";
+execute stmt;
+ERROR 21000: Subquery returns more than 1 row
+execute stmt;
+ERROR 21000: Subquery returns more than 1 row
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "SELECT * FROM v1";
+execute stmt;
+ERROR 21000: Subquery returns more than 1 row
+execute stmt;
+ERROR 21000: Subquery returns more than 1 row
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2) FROM t1 ) SELECT * FROM cte";
+execute stmt;
+ERROR 21000: Subquery returns more than 1 row
+execute stmt;
+ERROR 21000: Subquery returns more than 1 row
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1";
+execute stmt;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+execute stmt;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "SELECT * FROM v2";
+execute stmt;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+execute stmt;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1 ) SELECT * FROM cte";
+execute stmt;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+execute stmt;
+(SELECT COUNT(b) FROM t2 WHERE c > 1)
+3
+DEALLOCATE PREPARE stmt;
+DROP VIEW v1,v2;
+DROP TABLE t1,t2;
+#
+# MDEV-28570: VIEW with WHERE containing subquery
+# with set function aggregated in query
+#
+CREATE TABLE t1 (a int, b int);
+CREATE TABLE t2 (c int, d int);
+INSERT INTO t1 VALUES
+(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
+INSERT INTO t2 VALUES
+(2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
+CREATE VIEW v AS SELECT a FROM t1 GROUP BY a
+HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
+SELECT a FROM t1 GROUP BY a
+HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
+a
+2
+4
+SELECT * FROM v;
+a
+2
+4
+WITH cte AS ( SELECT a FROM t1 GROUP BY a
+HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20) ) SELECT * FROM cte;
+a
+2
+4
+EXPLAIN SELECT a FROM t1 GROUP BY a
+HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 7 Using temporary; Using filesort
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 6
+EXPLAIN SELECT * FROM v;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 7
+2 DERIVED t1 ALL NULL NULL NULL NULL 7 Using temporary; Using filesort
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 6
+EXPLAIN WITH cte AS ( SELECT a FROM t1 GROUP BY a
+HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20) ) SELECT * FROM cte;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 7
+2 DERIVED t1 ALL NULL NULL NULL NULL 7 Using temporary; Using filesort
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 6
+PREPARE stmt FROM "SELECT a FROM t1 GROUP BY a
+HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20)";
+execute stmt;
+a
+2
+4
+execute stmt;
+a
+2
+4
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "SELECT * FROM v";
+execute stmt;
+a
+2
+4
+execute stmt;
+a
+2
+4
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "WITH cte AS ( SELECT a FROM t1 GROUP BY a
+HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20) ) SELECT * FROM cte";
+execute stmt;
+a
+2
+4
+execute stmt;
+a
+2
+4
+DEALLOCATE PREPARE stmt;
+DROP VIEW v;
+DROP TABLE t1,t2;
+#
+# MDEV-28571: VIEW with select list containing subquery
+# with set function aggregated in query
+#
+CREATE TABLE t1 (a int, b int);
+CREATE TABLE t2 (m int, n int);
+INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
+INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
+CREATE VIEW v AS SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
+FROM t1
+GROUP BY a;
+SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
+FROM t1
+GROUP BY a;
+c
+2
+3
+1,1
+SELECT * FROM v;
+c
+2
+3
+1,1
+WITH cte AS ( SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
+FROM t1
+GROUP BY a ) SELECT * FROM cte;
+c
+2
+3
+1,1
+EXPLAIN SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
+FROM t1
+GROUP BY a;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+EXPLAIN SELECT * FROM v;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 6
+2 DERIVED t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+EXPLAIN WITH cte AS ( SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
+FROM t1
+GROUP BY a ) SELECT * FROM cte;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY <derived2> ALL NULL NULL NULL NULL 6
+2 DERIVED t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+PREPARE stmt FROM "SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
+FROM t1
+GROUP BY a";
+execute stmt;
+c
+2
+3
+1,1
+execute stmt;
+c
+2
+3
+1,1
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "SELECT * FROM v";
+execute stmt;
+c
+2
+3
+1,1
+execute stmt;
+c
+2
+3
+1,1
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "WITH cte AS ( SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
+FROM t1
+GROUP BY a ) SELECT * FROM cte";
+execute stmt;
+c
+2
+3
+1,1
+execute stmt;
+c
+2
+3
+1,1
+DEALLOCATE PREPARE stmt;
+DROP VIEW v;
+DROP TABLE t1,t2;
+#
+# MDEV-30668: VIEW with WHERE containing nested subquery
+# with set function aggregated in outer subquery
+#
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+create table t2 (b int);
+insert into t2 values (2), (1), (4), (7);
+create table t3 (a int, b int);
+insert into t3 values (2,10), (7,30), (2,30), (1,10), (7,40);
+create view v as select * from t1
+where t1.a in (select t3.a from t3 group by t3.a
+having t3.a > any (select t2.b from t2
+where t2.b*10 < sum(t3.b)));
+select * from t1
+where t1.a in (select t3.a from t3 group by t3.a
+having t3.a > any (select t2.b from t2
+where t2.b*10 < sum(t3.b)));
+a
+7
+select * from v;
+a
+7
+with cte as ( select * from t1
+where t1.a in (select t3.a from t3 group by t3.a
+having t3.a > any (select t2.b from t2
+where t2.b*10 < sum(t3.b))) ) select * from cte;
+a
+7
+explain select * from t1
+where t1.a in (select t3.a from t3 group by t3.a
+having t3.a > any (select t2.b from t2
+where t2.b*10 < sum(t3.b)));
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 test.t1.a 1
+2 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using temporary
+3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
+explain select * from v;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 test.t1.a 1
+3 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using temporary
+4 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
+explain with cte as ( select * from t1
+where t1.a in (select t3.a from t3 group by t3.a
+having t3.a > any (select t2.b from t2
+where t2.b*10 < sum(t3.b))) ) select * from cte;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 test.t1.a 1
+3 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using temporary
+4 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
+prepare stmt from "select * from t1
+where t1.a in (select t3.a from t3 group by t3.a
+having t3.a > any (select t2.b from t2
+where t2.b*10 < sum(t3.b)))";
+execute stmt;
+a
+7
+execute stmt;
+a
+7
+deallocate prepare stmt;
+prepare stmt from "select * from v";
+execute stmt;
+a
+7
+execute stmt;
+a
+7
+deallocate prepare stmt;
+prepare stmt from "with cte as ( select * from t1
+where t1.a in (select t3.a from t3 group by t3.a
+having t3.a > any (select t2.b from t2
+where t2.b*10 < sum(t3.b))) ) select * from cte";
+execute stmt;
+a
+7
+execute stmt;
+a
+7
+deallocate prepare stmt;
+drop view v;
+drop table t1,t2,t3;
+# End of 10.4 tests
diff --git a/mysql-test/main/derived_view.test b/mysql-test/main/derived_view.test
index caccc7d..5422fbc 100644
--- a/mysql-test/main/derived_view.test
+++ b/mysql-test/main/derived_view.test
@@ -2455,3 +2455,300 @@ drop view v, v2;
drop table t1,t2;
--echo # End of 10.2 tests
+
+--echo #
+--echo # MDEV-30706: view defined as select with implicit grouping and
+--echo # a set function used in a subquery
+--echo #
+
+CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
+INSERT INTO t1 VALUES (1,1), (2,2);
+CREATE TABLE t2 (a INT PRIMARY KEY, b INT);
+INSERT INTO t2 VALUES (1,1), (3,3);
+CREATE TABLE t3 (a INT PRIMARY KEY, b INT);
+INSERT INTO t3 VALUES (2,2), (4,4), (7,7);
+CREATE TABLE t4 (a INT PRIMARY KEY, b INT);
+INSERT INTO t4 VALUES (2,2), (5,5), (7,7);
+
+let $q=
+SELECT
+ (SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
+FROM t3, t4
+ WHERE t3.a = t4.b;
+
+eval CREATE VIEW v AS $q;
+
+eval $q;
+SELECT * FROM v;
+eval WITH cte AS ( $q ) SELECT * FROM cte;
+
+eval EXPLAIN $q;
+EXPLAIN SELECT * FROM v;
+eval EXPLAIN WITH cte AS ( $q ) SELECT * FROM cte;
+
+eval PREPARE stmt FROM "$q";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "SELECT * FROM v";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "WITH cte AS ( $q ) SELECT * FROM cte";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+DROP VIEW v;
+DROP TABLE t1,t2,t3,t4;
+
+--echo #
+--echo # MDEV-29224: view defined as select with implicit grouping and
+--echo # a set function used in a subquery
+--echo #
+
+CREATE TABLE t1 (f1 INT);
+INSERT INTO t1 VALUES (1),(2);
+CREATE TABLE t2 (f2 int);
+INSERT INTO t2 VALUES (3);
+
+let $q=
+SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
+
+eval CREATE VIEW v AS $q;
+
+eval $q;
+SELECT * FROM v;
+eval WITH cte AS ( $q ) SELECT * FROM cte;
+
+eval EXPLAIN $q;
+EXPLAIN SELECT * FROM v;
+eval EXPLAIN WITH cte AS ( $q ) SELECT * FROM cte;
+
+eval PREPARE stmt FROM "$q";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "SELECT * FROM v";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "WITH cte AS ( $q ) SELECT * FROM cte";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+DROP VIEW v;
+DROP TABLE t1,t2;
+
+--echo #
+--echo # MDEV-28573: view defined as select with implicit grouping and
+--echo # a set function used in a subquery
+--echo #
+
+CREATE TABLE t1 (a INTEGER, b INTEGER);
+CREATE TABLE t2 (c INTEGER);
+INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
+INSERT INTO t2 VALUES (1), (2);
+
+let $q1=
+SELECT (SELECT COUNT(b) FROM t2) FROM t1;
+let $q2=
+SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1;
+
+eval CREATE VIEW v1 AS $q1;
+eval CREATE VIEW v2 AS $q2;
+
+--error ER_SUBQUERY_NO_1_ROW
+eval $q1;
+--error ER_SUBQUERY_NO_1_ROW
+SELECT * FROM v1;
+--error ER_SUBQUERY_NO_1_ROW
+eval WITH cte AS ( $q1 ) SELECT * FROM cte;
+eval $q2;
+SELECT * FROM v2;
+eval WITH cte AS ( $q2 ) SELECT * FROM cte;
+
+eval EXPLAIN $q1;
+EXPLAIN SELECT * FROM v1;
+eval EXPLAIN WITH cte AS ( $q1 ) SELECT * FROM cte;
+
+eval PREPARE stmt FROM "$q1";
+--error ER_SUBQUERY_NO_1_ROW
+execute stmt;
+--error ER_SUBQUERY_NO_1_ROW
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "SELECT * FROM v1";
+--error ER_SUBQUERY_NO_1_ROW
+execute stmt;
+--error ER_SUBQUERY_NO_1_ROW
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "WITH cte AS ( $q1 ) SELECT * FROM cte";
+--error ER_SUBQUERY_NO_1_ROW
+execute stmt;
+--error ER_SUBQUERY_NO_1_ROW
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "$q2";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "SELECT * FROM v2";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "WITH cte AS ( $q2 ) SELECT * FROM cte";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+DROP VIEW v1,v2;
+DROP TABLE t1,t2;
+
+--echo #
+--echo # MDEV-28570: VIEW with WHERE containing subquery
+--echo # with set function aggregated in query
+--echo #
+
+CREATE TABLE t1 (a int, b int);
+CREATE TABLE t2 (c int, d int);
+
+INSERT INTO t1 VALUES
+ (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
+INSERT INTO t2 VALUES
+ (2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
+
+let $q=
+SELECT a FROM t1 GROUP BY a
+ HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
+
+eval CREATE VIEW v AS $q;
+
+eval $q;
+SELECT * FROM v;
+eval WITH cte AS ( $q ) SELECT * FROM cte;
+
+eval EXPLAIN $q;
+EXPLAIN SELECT * FROM v;
+eval EXPLAIN WITH cte AS ( $q ) SELECT * FROM cte;
+
+eval PREPARE stmt FROM "$q";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "SELECT * FROM v";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "WITH cte AS ( $q ) SELECT * FROM cte";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+DROP VIEW v;
+DROP TABLE t1,t2;
+
+--echo #
+--echo # MDEV-28571: VIEW with select list containing subquery
+--echo # with set function aggregated in query
+--echo #
+
+CREATE TABLE t1 (a int, b int);
+CREATE TABLE t2 (m int, n int);
+INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
+INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
+
+let $q=
+SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
+FROM t1
+GROUP BY a;
+
+eval CREATE VIEW v AS $q;
+
+eval $q;
+SELECT * FROM v;
+eval WITH cte AS ( $q ) SELECT * FROM cte;
+
+eval EXPLAIN $q;
+EXPLAIN SELECT * FROM v;
+eval EXPLAIN WITH cte AS ( $q ) SELECT * FROM cte;
+
+eval PREPARE stmt FROM "$q";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "SELECT * FROM v";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+eval PREPARE stmt FROM "WITH cte AS ( $q ) SELECT * FROM cte";
+execute stmt;
+execute stmt;
+DEALLOCATE PREPARE stmt;
+
+DROP VIEW v;
+DROP TABLE t1,t2;
+
+--echo #
+--echo # MDEV-30668: VIEW with WHERE containing nested subquery
+--echo # with set function aggregated in outer subquery
+--echo #
+
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+
+create table t2 (b int);
+insert into t2 values (2), (1), (4), (7);
+
+create table t3 (a int, b int);
+insert into t3 values (2,10), (7,30), (2,30), (1,10), (7,40);
+
+let $q=
+select * from t1
+ where t1.a in (select t3.a from t3 group by t3.a
+ having t3.a > any (select t2.b from t2
+ where t2.b*10 < sum(t3.b)));
+eval create view v as $q;
+
+eval $q;
+eval select * from v;
+eval with cte as ( $q ) select * from cte;
+
+eval explain $q;
+eval explain select * from v;
+eval explain with cte as ( $q ) select * from cte;
+
+eval prepare stmt from "$q";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+eval prepare stmt from "select * from v";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+eval prepare stmt from "with cte as ( $q ) select * from cte";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+drop view v;
+drop table t1,t2,t3;
+
+--echo # End of 10.4 tests
diff --git a/mysql-test/main/func_group.result b/mysql-test/main/func_group.result
index a0ee121..f6fcc61 100644
--- a/mysql-test/main/func_group.result
+++ b/mysql-test/main/func_group.result
@@ -1443,16 +1443,11 @@ FROM derived1 AS X
WHERE
X.int_nokey < 61
GROUP BY pk
-LIMIT 1)
+LIMIT 1) AS m
FROM D AS X
WHERE X.int_key < 13
GROUP BY int_nokey LIMIT 1;
-(SELECT COUNT( int_nokey )
-FROM derived1 AS X
-WHERE
-X.int_nokey < 61
-GROUP BY pk
-LIMIT 1)
+m
1
DROP TABLE derived1;
DROP TABLE D;
diff --git a/mysql-test/main/func_group.test b/mysql-test/main/func_group.test
index e5ae33f..446154e 100644
--- a/mysql-test/main/func_group.test
+++ b/mysql-test/main/func_group.test
@@ -579,8 +579,6 @@ DROP TABLE t1;
#
# Bug #16792 query with subselect, join, and group not returning proper values
#
-#enable after fix MDEV-28573
---disable_view_protocol
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1,1),(1,2),(2,3);
@@ -591,7 +589,6 @@ SELECT AVG(2), BIT_AND(2), BIT_OR(2), BIT_XOR(2), COUNT(*), COUNT(12),
COUNT(DISTINCT 12), MIN(2),MAX(2),STD(2), VARIANCE(2),SUM(2),
GROUP_CONCAT(2),GROUP_CONCAT(DISTINCT 2);
DROP TABLE t1;
---enable_view_protocol
# End of 4.1 tests
@@ -623,13 +620,10 @@ drop table t1, t2, t3;
#
# BUG#3190, WL#1639: Standard Deviation STDDEV - 2 different calculations
#
-#enable after fix MDEV-28573
---disable_view_protocol
CREATE TABLE t1 (id int(11),value1 float(10,2));
INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00), (2,13.00);
select id, stddev_pop(value1), var_pop(value1), stddev_samp(value1), var_samp(value1) from t1 group by id;
DROP TABLE t1;
---enable_view_protocol
#
# BUG#8464 decimal AVG returns incorrect result
@@ -966,22 +960,19 @@ INSERT INTO D VALUES
(83,45,4,repeat(' X', 42)),
(105,53,12,NULL);
-#enable after fix MDEV-27871
---disable_view_protocol
SELECT
(SELECT COUNT( int_nokey )
FROM derived1 AS X
WHERE
X.int_nokey < 61
GROUP BY pk
- LIMIT 1)
+ LIMIT 1) AS m
FROM D AS X
WHERE X.int_key < 13
GROUP BY int_nokey LIMIT 1;
DROP TABLE derived1;
DROP TABLE D;
---enable_view_protocol
#
# Bug #39656: Behaviour different for agg functions with & without where -
diff --git a/mysql-test/main/group_by.result b/mysql-test/main/group_by.result
index 06138b3..07629a2 100644
--- a/mysql-test/main/group_by.result
+++ b/mysql-test/main/group_by.result
@@ -1027,8 +1027,9 @@ FROM t1 AS t1_outer GROUP BY t1_outer.b;
21
21
SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1)
+AS m
FROM t1 AS t1_outer;
-(SELECT SUM(t1_inner.a) FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1)
+m
3
3
3
@@ -1268,12 +1269,9 @@ a
select avg (
(select
(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
-from t1 as outr order by outr.a limit 1))
+from t1 as outr order by outr.a limit 1)) as m
from t1 as most_outer;
-avg (
-(select
-(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
-from t1 as outr order by outr.a limit 1))
+m
29.0000
select avg (
(select (
diff --git a/mysql-test/main/group_by.test b/mysql-test/main/group_by.test
index 95518c3..9bbe1e0 100644
--- a/mysql-test/main/group_by.test
+++ b/mysql-test/main/group_by.test
@@ -774,11 +774,9 @@ SELECT 1 FROM t1 as t1_outer GROUP BY a
SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner LIMIT 1)
FROM t1 AS t1_outer GROUP BY t1_outer.b;
-#enable after fix MDEV-27871
---disable_view_protocol
SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1)
+AS m
FROM t1 AS t1_outer;
---enable_view_protocol
--error ER_WRONG_FIELD_WITH_GROUP
SELECT (SELECT SUM(t1_outer.a) FROM t1 AS t1_inner LIMIT 1)
@@ -854,6 +852,7 @@ DROP TABLE t1;
--echo #
--echo # Bug#27219: Aggregate functions in ORDER BY.
--echo #
+
SET @save_sql_mode=@@sql_mode;
SET @@sql_mode='ONLY_FULL_GROUP_BY';
@@ -875,6 +874,8 @@ SELECT 1 FROM t1 ORDER BY SUM(a) + 1;
--error 1140
SELECT 1 FROM t1 ORDER BY SUM(a), b;
+--disable_service_connection
+
--error 1140
SELECT a FROM t1 ORDER BY COUNT(b);
@@ -887,9 +888,6 @@ SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2 ORDER BY t2.a);
--error 1140
SELECT t1.a FROM t1 ORDER BY (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
-#enable after fix MDEV-28570
---disable_view_protocol
-
--error 1140
SELECT t1.a FROM t1
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
@@ -927,7 +925,7 @@ SELECT 1 FROM t1 GROUP BY t1.a
SELECT 1 FROM t1 GROUP BY t1.a
HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);
---enable_view_protocol
+--enable_service_connection
# Both SUMs are aggregated in the subquery, no mixture:
SELECT t1.a FROM t1
@@ -952,18 +950,17 @@ SELECT t1.a, SUM(t1.b) FROM t1
ORDER BY SUM(t2.b + t1.a) LIMIT 1)
GROUP BY t1.a;
-#enable after fix MDEV-28570, MDEV-28571
---disable_view_protocol
-
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
select avg (
(select
(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
- from t1 as outr order by outr.a limit 1))
+ from t1 as outr order by outr.a limit 1)) as m
from t1 as most_outer;
+--disable_service_connection
+
--error 1140
select avg (
(select (
@@ -971,7 +968,7 @@ select avg (
from t1 as outr order by count(outr.a) limit 1)) as tt
from t1 as most_outer;
---enable_view_protocol
+--enable_service_connection
select (select sum(outr.a + t1.a) from t1 limit 1) as tt from t1 as outr order by outr.a;
@@ -1369,7 +1366,7 @@ DROP TABLE t1;
--echo # Bug#11765254 (58200): Assertion failed: param.sort_length when grouping
--echo # by functions
--echo #
-#createing view adds one new warning
+#creating view adds one new warning
--disable_view_protocol
SET BIG_TABLES=1;
diff --git a/mysql-test/main/opt_trace.result b/mysql-test/main/opt_trace.result
index a97d007..a343d59 100644
--- a/mysql-test/main/opt_trace.result
+++ b/mysql-test/main/opt_trace.result
@@ -4130,7 +4130,7 @@ explain select * from (select rand() from t1)q {
"derived": {
"table": "q",
"select_id": 2,
- "algorithm": "merged"
+ "algorithm": "materialized"
}
},
{
@@ -4144,7 +4144,7 @@ explain select * from (select rand() from t1)q {
}
},
{
- "expanded_query": "/* select#1 */ select rand() AS `rand()` from (/* select#2 */ select rand() AS `rand()` from t1) q"
+ "expanded_query": "/* select#1 */ select q.`rand()` AS `rand()` from (/* select#2 */ select rand() AS `rand()` from t1) q"
}
]
}
@@ -4154,14 +4154,6 @@ explain select * from (select rand() from t1)q {
"select_id": 1,
"steps": [
{
- "derived": {
- "table": "q",
- "select_id": 2,
- "algorithm": "materialized",
- "cause": "Random function in the select"
- }
- },
- {
"join_optimization": {
"select_id": 2,
"steps": [
diff --git a/mysql-test/main/subselect.result b/mysql-test/main/subselect.result
index c57cae7..d2ae340 100644
--- a/mysql-test/main/subselect.result
+++ b/mysql-test/main/subselect.result
@@ -118,27 +118,27 @@ ROW(1,2,3) > (SELECT 1,2,1)
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
ROW(1,2,3) = (SELECT 1,2,NULL)
NULL
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a') AS m;
+m
1
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b') AS m;
+m
0
-SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
-(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
-SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
-(SELECT 'b',2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'b'
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
-(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a') AS m;
+m
1
-SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
-(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: 'c'
@@ -228,19 +228,26 @@ a
2
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
a
-select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
-b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
+b m
8 7.5000
8 4.5000
9 7.5000
-explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
+explain extended
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
+Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `m` from `test`.`t4`
select * from t3 where exists (select * from t2 where t2.b=t3.a);
a
7
@@ -307,21 +314,34 @@ select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 w
b ma
7 12
create table t5 (a int);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (5);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (2);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
-explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
+explain extended
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
@@ -330,7 +350,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
Warnings:
Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
+Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `m`,`test`.`t2`.`a` AS `a` from `test`.`t2`
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
ERROR 21000: Subquery returns more than 1 row
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
@@ -486,8 +506,11 @@ SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING t
mot topic date pseudo
joce 40143 2002-10-22 joce
joce 43506 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 1
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
@@ -505,8 +528,11 @@ joce 40143 2002-10-22 joce
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
mot topic date pseudo
joce 40143 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 0
drop table t1,t2;
@@ -881,6 +907,25 @@ NULL
select 1.5 > ANY (SELECT * from t1);
1.5 > ANY (SELECT * from t1)
NULL
+update t1 set a=NULL where a=2.5;
+select 1.5 IN (SELECT * from t1);
+1.5 IN (SELECT * from t1)
+1
+select 3.5 IN (SELECT * from t1);
+3.5 IN (SELECT * from t1)
+1
+select 10.5 IN (SELECT * from t1);
+10.5 IN (SELECT * from t1)
+NULL
+select 1.5 > ALL (SELECT * from t1);
+1.5 > ALL (SELECT * from t1)
+0
+select 10.5 > ALL (SELECT * from t1);
+10.5 > ALL (SELECT * from t1)
+NULL
+select 1.5 > ANY (SELECT * from t1);
+1.5 > ANY (SELECT * from t1)
+NULL
select 10.5 > ANY (SELECT * from t1);
10.5 > ANY (SELECT * from t1)
1
@@ -891,6 +936,20 @@ Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1249 Select 2 was reduced during optimization
Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
select (select a+1) from t1;
(select a+1)
2.5
@@ -1532,8 +1591,8 @@ create table t3 (a int, b int);
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
insert into t3 values (3,3), (2,2), (1,1);
-select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
-a (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1)
+select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) as m from t3;
+a m
3 1
2 2
1 2
@@ -1728,8 +1787,8 @@ CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(1
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
-select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
-dbid name (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')
+select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') as m FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') as m from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
+dbid name m
-1 Valid 1
-1 Valid 2 1
-1 Should Not Return 0
@@ -3784,9 +3843,10 @@ SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2
1
1
-SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+SELECT
+(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+m
2
1
1
@@ -3796,9 +3856,9 @@ COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
1 1
1 1
SELECT COUNT(DISTINCT t1.b),
-(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+COUNT(DISTINCT t1.b) m
2 2
1 1
1 1
@@ -3822,16 +3882,10 @@ SELECT (
SELECT COUNT(DISTINCT t1.b)
)
)
-FROM t1 GROUP BY t1.a LIMIT 1)
+FROM t1 GROUP BY t1.a LIMIT 1) AS m
FROM t1 t2
GROUP BY t2.a;
-(
-SELECT (
-SELECT (
-SELECT COUNT(DISTINCT t1.b)
-)
-)
-FROM t1 GROUP BY t1.a LIMIT 1)
+m
2
2
2
@@ -6425,11 +6479,10 @@ CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (10,7), (0,7);
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+WHERE t.a != 0 AND t2.a != 0) AS m
FROM (SELECT * FROM t3) AS t
GROUP BY 2;
-SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+SUM(DISTINCT b) m
7 NULL
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
@@ -6562,66 +6615,93 @@ CREATE TABLE t3 (f3a int default 1, f3b int default 2);
INSERT INTO t3 VALUES (1,1),(2,2);
set @old_optimizer_switch = @@session.optimizer_switch;
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
set @@session.optimizer_switch=@old_optimizer_switch;
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2 AS m;
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
select (null, null) = (null, null);
(null, null) = (null, null)
@@ -6667,8 +6747,10 @@ INSERT INTO t2 VALUES (1);
CREATE TABLE t3 ( c INT );
INSERT INTO t3 VALUES (4),(5);
SET optimizer_switch='subquery_cache=off';
-SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
-( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
+SELECT
+( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) AS m
+FROM t1;
+m
1
NULL
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
@@ -6885,7 +6967,9 @@ CREATE TABLE t3 (c INT);
INSERT INTO t3 VALUES (8),(3);
set @@expensive_subquery_limit= 0;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6895,9 +6979,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
@@ -6923,7 +7009,9 @@ Handler_read_rnd_deleted 0
Handler_read_rnd_next 22
set @@expensive_subquery_limit= default;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6933,9 +7021,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
diff --git a/mysql-test/main/subselect.test b/mysql-test/main/subselect.test
index 187dc80..77da710 100644
--- a/mysql-test/main/subselect.test
+++ b/mysql-test/main/subselect.test
@@ -74,13 +74,13 @@ SELECT ROW(1,2,3) > (SELECT 1,2,1);
#enable after fix MDEV-28585
--disable_view_protocol
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
-SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
-SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
-SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
--enable_view_protocol
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a') AS m;
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b') AS m;
+SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b') AS m;
+SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a') AS m;
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a') AS m;
+SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a') AS m;
-- error ER_OPERAND_COLUMNS
SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
@@ -118,11 +118,15 @@ set optimizer_switch=@tmp_optimizer_switch;
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1);
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
-#enable afte fix MDEV-27871
---disable_view_protocol
-select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
-explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
---enable_view_protocol
+select
+ b,
+ (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
+explain extended
+select
+ b,
+ (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
select * from t3 where exists (select * from t2 where t2.b=t3.a);
select * from t3 where not exists (select * from t2 where t2.b=t3.a);
select * from t3 where a in (select b from t2);
@@ -155,16 +159,25 @@ delete from t2 where a=2 and b=10;
select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
create table t5 (a int);
-#enable afte fix MDEV-27871
---disable_view_protocol
-
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
+select
+ (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+ a
+from t2;
insert into t5 values (5);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
+select
+ (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+ a
+from t2;
insert into t5 values (2);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
---enable_view_protocol
+select
+ (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+ a
+from t2;
+explain extended
+select
+ (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+ a
+from t2;
-- error ER_SUBQUERY_NO_1_ROW
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
@@ -269,19 +282,19 @@ SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING t
SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
-#enable after fix MDEV-27871
---disable_view_protocol
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
---enable_view_protocol
+SELECT
+ *,
+ topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) AS m
+FROM t2;
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
-#enable after fix MDEV-27871
---disable_view_protocol
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
---enable_view_protocol
+SELECT
+ *,
+ topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) AS m
+FROM t2;
drop table t1,t2;
#forumconthardwarefr7
@@ -523,11 +536,20 @@ select 10.5 IN (SELECT * from t1);
select 1.5 > ALL (SELECT * from t1);
select 10.5 > ALL (SELECT * from t1);
select 1.5 > ANY (SELECT * from t1);
+update t1 set a=NULL where a=2.5;
+select 1.5 IN (SELECT * from t1);
+select 3.5 IN (SELECT * from t1);
+select 10.5 IN (SELECT * from t1);
+select 1.5 > ALL (SELECT * from t1);
+select 10.5 > ALL (SELECT * from t1);
+select 1.5 > ANY (SELECT * from t1);
select 10.5 > ANY (SELECT * from t1);
+--enable_view_protocol
+explain extended select (select a+1) from t1;
+explain extended select (select a+1) from t1;
explain extended select (select a+1) from t1;
select (select a+1) from t1;
drop table t1;
---enable_view_protocol
#
# Null with keys
@@ -947,10 +969,7 @@ create table t3 (a int, b int);
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
insert into t3 values (3,3), (2,2), (1,1);
-#enable after fix MDEV-27871
---disable_view_protocol
-select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
---enable_view_protocol
+select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) as m from t3;
drop table t1,t2,t3;
#
@@ -1068,10 +1087,7 @@ CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(1
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
-#enable after fix MDEV-27871
---disable_view_protocol
-select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
---enable_view_protocol
+select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') as m FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') as m from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
SELECT dbid, name FROM t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND ((date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')) AND t4.task_id = taskid;
drop table t1,t2,t3,t4;
@@ -2390,9 +2406,6 @@ SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
SELECT * FROM t2;
SELECT * FROM t3;
-#enable after fix MDEV-28570
---disable_view_protocol
-
SELECT a FROM t1 GROUP BY a
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
SELECT a FROM t1 GROUP BY a
@@ -2457,8 +2470,6 @@ SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
HAVING t2.c+sum > 20);
---enable_view_protocol
-
DROP TABLE t1,t2,t3;
@@ -2689,19 +2700,17 @@ DROP TABLE t1;
# Bug#21540 Subqueries with no from and aggregate functions return
# wrong results
-#enable after fix MDEV-27871, MDEV-28573
---disable_view_protocol
-
CREATE TABLE t1 (a INT, b INT);
CREATE TABLE t2 (a INT);
INSERT INTO t2 values (1);
INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
-SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+SELECT
+ (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
SELECT COUNT(DISTINCT t1.b),
- (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+ (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
SELECT (
SELECT (
@@ -2715,11 +2724,10 @@ SELECT (
SELECT COUNT(DISTINCT t1.b)
)
)
- FROM t1 GROUP BY t1.a LIMIT 1)
+ FROM t1 GROUP BY t1.a LIMIT 1) AS m
FROM t1 t2
GROUP BY t2.a;
DROP TABLE t1,t2;
---enable_view_protocol
#
# Bug#21727 Correlated subquery that requires filesort:
@@ -2954,8 +2962,6 @@ DROP TABLE t1,t2;
# Bug#27229 GROUP_CONCAT in subselect with COUNT() as an argument
#
-#enable after fix MDEV-28571
---disable_view_protocol
CREATE TABLE t1 (a int, b int);
CREATE TABLE t2 (m int, n int);
INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
@@ -2970,7 +2976,6 @@ SELECT COUNT(*) c, a,
FROM t1 GROUP BY a;
DROP table t1,t2;
---enable_view_protocol
#
# Bug#27321 Wrong subquery result in a grouping select
@@ -3001,14 +3006,11 @@ SELECT tt.a,
FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
FROM t1 as tt GROUP BY tt.a;
-#enable after fix MDEV-28571
---disable_view_protocol
SELECT tt.a, MAX(
(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
LIMIT 1)
FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
FROM t1 as tt GROUP BY tt.a;
---enable_view_protocol
DROP TABLE t1;
@@ -3163,8 +3165,6 @@ CREATE TABLE t2 (x INTEGER);
INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
INSERT INTO t2 VALUES (1), (2);
-#enable after fix MDEV-28573
---disable_view_protocol
# wasn't failing, but should
--error ER_SUBQUERY_NO_1_ROW
SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
@@ -3174,7 +3174,6 @@ SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
---enable_view_protocol
DROP TABLE t1,t2;
@@ -3189,8 +3188,6 @@ GROUP BY a1.a;
DROP TABLE t1;
#test cases from 29297
-#enable after fix MDEV-28573
---disable_view_protocol
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT);
INSERT INTO t1 VALUES (1),(2);
@@ -3200,7 +3197,6 @@ SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
DROP TABLE t1,t2;
---enable_view_protocol
#
# Bug#31884 Assertion + crash in subquery in the SELECT clause.
@@ -5402,14 +5398,11 @@ INSERT INTO t2 VALUES (10,7,0), (0,7,0);
CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (10,7), (0,7);
-#enable after fix MDEV-27871
---disable_view_protocol
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
- WHERE t.a != 0 AND t2.a != 0)
+ WHERE t.a != 0 AND t2.a != 0) AS m
FROM (SELECT * FROM t3) AS t
GROUP BY 2;
---enable_view_protocol
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
@@ -5567,29 +5560,53 @@ INSERT INTO t3 VALUES (1,1),(2,2);
set @old_optimizer_switch = @@session.optimizer_switch;
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
-#enable after fix MDEV-27871
---disable_view_protocol
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
+SELECT
+ (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+ FROM t2;
+SELECT
+ (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+SELECT
+ (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
set @@session.optimizer_switch=@old_optimizer_switch;
# check different IN with default switches
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
---enable_view_protocol
+SELECT
+ (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+SELECT
+ (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2 AS m;
+SELECT
+ (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+SELECT
+ (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
# other row operation with NULL single row subquery also should work
select (null, null) = (null, null);
@@ -5629,10 +5646,9 @@ INSERT INTO t3 VALUES (4),(5);
SET optimizer_switch='subquery_cache=off';
-#enable after fix MDEV-27871
---disable_view_protocol
-SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
---enable_view_protocol
+SELECT
+ ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) AS m
+FROM t1;
# This query just for example, it should return the same as above (1 and NULL)
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
@@ -5821,15 +5837,17 @@ INSERT INTO t3 VALUES (8),(3);
set @@expensive_subquery_limit= 0;
-#enable after fix MDEV-27871
---disable_view_protocol
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+ (SELECT MIN(b) FROM t1, t2
+ WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+ (SELECT MIN(b) FROM t1, t2
+ WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
show status like "subquery_cache%";
@@ -5838,17 +5856,20 @@ show status like '%Handler_read%';
set @@expensive_subquery_limit= default;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+ (SELECT MIN(b) FROM t1, t2
+ WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+ (SELECT MIN(b) FROM t1, t2
+ WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
show status like "subquery_cache%";
show status like '%Handler_read%';
---enable_view_protocol
drop table t1, t2, t3;
@@ -6102,22 +6123,16 @@ INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 (f2 int);
INSERT INTO t2 VALUES (3);
-#enable after fix MDEV-29224
---disable_view_protocol
SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
---enable_view_protocol
INSERT INTO t2 VALUES (4);
-#enable after fix MDEV-28573
---disable_view_protocol
--error ER_SUBQUERY_NO_1_ROW
SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
--error ER_SUBQUERY_NO_1_ROW
SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
---enable_view_protocol
drop view v1;
drop table t1,t2;
diff --git a/mysql-test/main/subselect4.result b/mysql-test/main/subselect4.result
index 262adc5..259337c 100644
--- a/mysql-test/main/subselect4.result
+++ b/mysql-test/main/subselect4.result
@@ -20,8 +20,8 @@ WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
ORDER BY count(*);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index NULL a 5 NULL 2 Using index
-2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
-3 DEPENDENT SUBQUERY t3 system NULL NULL NULL NULL 0 Const row not found
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
+3 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
# should not crash the next statement
SELECT 1 FROM t1
WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
diff --git a/mysql-test/main/subselect_no_exists_to_in.result b/mysql-test/main/subselect_no_exists_to_in.result
index 8746f72..f508cb2 100644
--- a/mysql-test/main/subselect_no_exists_to_in.result
+++ b/mysql-test/main/subselect_no_exists_to_in.result
@@ -122,27 +122,27 @@ ROW(1,2,3) > (SELECT 1,2,1)
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
ROW(1,2,3) = (SELECT 1,2,NULL)
NULL
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a') AS m;
+m
1
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b') AS m;
+m
0
-SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
-(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
-SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
-(SELECT 'b',2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'b'
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
-(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a') AS m;
+m
1
-SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
-(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: 'c'
@@ -232,19 +232,26 @@ a
2
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
a
-select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
-b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
+b m
8 7.5000
8 4.5000
9 7.5000
-explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
+explain extended
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
+Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `m` from `test`.`t4`
select * from t3 where exists (select * from t2 where t2.b=t3.a);
a
7
@@ -311,21 +318,34 @@ select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 w
b ma
7 12
create table t5 (a int);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (5);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (2);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
-explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
+explain extended
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
@@ -334,7 +354,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
Warnings:
Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
+Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `m`,`test`.`t2`.`a` AS `a` from `test`.`t2`
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
ERROR 21000: Subquery returns more than 1 row
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
@@ -490,8 +510,11 @@ SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING t
mot topic date pseudo
joce 40143 2002-10-22 joce
joce 43506 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 1
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
@@ -509,8 +532,11 @@ joce 40143 2002-10-22 joce
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
mot topic date pseudo
joce 40143 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 0
drop table t1,t2;
@@ -885,6 +911,25 @@ NULL
select 1.5 > ANY (SELECT * from t1);
1.5 > ANY (SELECT * from t1)
NULL
+update t1 set a=NULL where a=2.5;
+select 1.5 IN (SELECT * from t1);
+1.5 IN (SELECT * from t1)
+1
+select 3.5 IN (SELECT * from t1);
+3.5 IN (SELECT * from t1)
+1
+select 10.5 IN (SELECT * from t1);
+10.5 IN (SELECT * from t1)
+NULL
+select 1.5 > ALL (SELECT * from t1);
+1.5 > ALL (SELECT * from t1)
+0
+select 10.5 > ALL (SELECT * from t1);
+10.5 > ALL (SELECT * from t1)
+NULL
+select 1.5 > ANY (SELECT * from t1);
+1.5 > ANY (SELECT * from t1)
+NULL
select 10.5 > ANY (SELECT * from t1);
10.5 > ANY (SELECT * from t1)
1
@@ -895,6 +940,20 @@ Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1249 Select 2 was reduced during optimization
Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
select (select a+1) from t1;
(select a+1)
2.5
@@ -1536,8 +1595,8 @@ create table t3 (a int, b int);
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
insert into t3 values (3,3), (2,2), (1,1);
-select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
-a (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1)
+select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) as m from t3;
+a m
3 1
2 2
1 2
@@ -1732,8 +1791,8 @@ CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(1
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
-select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
-dbid name (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')
+select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') as m FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') as m from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
+dbid name m
-1 Valid 1
-1 Valid 2 1
-1 Should Not Return 0
@@ -3787,9 +3846,10 @@ SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2
1
1
-SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+SELECT
+(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+m
2
1
1
@@ -3799,9 +3859,9 @@ COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
1 1
1 1
SELECT COUNT(DISTINCT t1.b),
-(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+COUNT(DISTINCT t1.b) m
2 2
1 1
1 1
@@ -3825,16 +3885,10 @@ SELECT (
SELECT COUNT(DISTINCT t1.b)
)
)
-FROM t1 GROUP BY t1.a LIMIT 1)
+FROM t1 GROUP BY t1.a LIMIT 1) AS m
FROM t1 t2
GROUP BY t2.a;
-(
-SELECT (
-SELECT (
-SELECT COUNT(DISTINCT t1.b)
-)
-)
-FROM t1 GROUP BY t1.a LIMIT 1)
+m
2
2
2
@@ -6425,11 +6479,10 @@ CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (10,7), (0,7);
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+WHERE t.a != 0 AND t2.a != 0) AS m
FROM (SELECT * FROM t3) AS t
GROUP BY 2;
-SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+SUM(DISTINCT b) m
7 NULL
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
@@ -6562,66 +6615,93 @@ CREATE TABLE t3 (f3a int default 1, f3b int default 2);
INSERT INTO t3 VALUES (1,1),(2,2);
set @old_optimizer_switch = @@session.optimizer_switch;
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
set @@session.optimizer_switch=@old_optimizer_switch;
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2 AS m;
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
select (null, null) = (null, null);
(null, null) = (null, null)
@@ -6667,8 +6747,10 @@ INSERT INTO t2 VALUES (1);
CREATE TABLE t3 ( c INT );
INSERT INTO t3 VALUES (4),(5);
SET optimizer_switch='subquery_cache=off';
-SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
-( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
+SELECT
+( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) AS m
+FROM t1;
+m
1
NULL
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
@@ -6885,7 +6967,9 @@ CREATE TABLE t3 (c INT);
INSERT INTO t3 VALUES (8),(3);
set @@expensive_subquery_limit= 0;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6895,9 +6979,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
@@ -6923,7 +7009,9 @@ Handler_read_rnd_deleted 0
Handler_read_rnd_next 22
set @@expensive_subquery_limit= default;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6933,9 +7021,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
diff --git a/mysql-test/main/subselect_no_mat.result b/mysql-test/main/subselect_no_mat.result
index f0d788d..02cb92f 100644
--- a/mysql-test/main/subselect_no_mat.result
+++ b/mysql-test/main/subselect_no_mat.result
@@ -125,27 +125,27 @@ ROW(1,2,3) > (SELECT 1,2,1)
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
ROW(1,2,3) = (SELECT 1,2,NULL)
NULL
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a') AS m;
+m
1
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b') AS m;
+m
0
-SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
-(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
-SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
-(SELECT 'b',2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'b'
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
-(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a') AS m;
+m
1
-SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
-(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: 'c'
@@ -235,19 +235,26 @@ a
2
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
a
-select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
-b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
+b m
8 7.5000
8 4.5000
9 7.5000
-explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
+explain extended
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
+Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `m` from `test`.`t4`
select * from t3 where exists (select * from t2 where t2.b=t3.a);
a
7
@@ -314,21 +321,34 @@ select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 w
b ma
7 12
create table t5 (a int);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (5);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (2);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
-explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
+explain extended
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
@@ -337,7 +357,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
Warnings:
Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
+Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `m`,`test`.`t2`.`a` AS `a` from `test`.`t2`
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
ERROR 21000: Subquery returns more than 1 row
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
@@ -493,8 +513,11 @@ SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING t
mot topic date pseudo
joce 40143 2002-10-22 joce
joce 43506 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 1
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
@@ -512,8 +535,11 @@ joce 40143 2002-10-22 joce
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
mot topic date pseudo
joce 40143 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 0
drop table t1,t2;
@@ -888,6 +914,25 @@ NULL
select 1.5 > ANY (SELECT * from t1);
1.5 > ANY (SELECT * from t1)
NULL
+update t1 set a=NULL where a=2.5;
+select 1.5 IN (SELECT * from t1);
+1.5 IN (SELECT * from t1)
+1
+select 3.5 IN (SELECT * from t1);
+3.5 IN (SELECT * from t1)
+1
+select 10.5 IN (SELECT * from t1);
+10.5 IN (SELECT * from t1)
+NULL
+select 1.5 > ALL (SELECT * from t1);
+1.5 > ALL (SELECT * from t1)
+0
+select 10.5 > ALL (SELECT * from t1);
+10.5 > ALL (SELECT * from t1)
+NULL
+select 1.5 > ANY (SELECT * from t1);
+1.5 > ANY (SELECT * from t1)
+NULL
select 10.5 > ANY (SELECT * from t1);
10.5 > ANY (SELECT * from t1)
1
@@ -898,6 +943,20 @@ Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1249 Select 2 was reduced during optimization
Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
select (select a+1) from t1;
(select a+1)
2.5
@@ -1539,8 +1598,8 @@ create table t3 (a int, b int);
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
insert into t3 values (3,3), (2,2), (1,1);
-select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
-a (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1)
+select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) as m from t3;
+a m
3 1
2 2
1 2
@@ -1735,8 +1794,8 @@ CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(1
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
-select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
-dbid name (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')
+select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') as m FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') as m from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
+dbid name m
-1 Valid 1
-1 Valid 2 1
-1 Should Not Return 0
@@ -3787,9 +3846,10 @@ SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2
1
1
-SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+SELECT
+(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+m
2
1
1
@@ -3799,9 +3859,9 @@ COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
1 1
1 1
SELECT COUNT(DISTINCT t1.b),
-(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+COUNT(DISTINCT t1.b) m
2 2
1 1
1 1
@@ -3825,16 +3885,10 @@ SELECT (
SELECT COUNT(DISTINCT t1.b)
)
)
-FROM t1 GROUP BY t1.a LIMIT 1)
+FROM t1 GROUP BY t1.a LIMIT 1) AS m
FROM t1 t2
GROUP BY t2.a;
-(
-SELECT (
-SELECT (
-SELECT COUNT(DISTINCT t1.b)
-)
-)
-FROM t1 GROUP BY t1.a LIMIT 1)
+m
2
2
2
@@ -6420,11 +6474,10 @@ CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (10,7), (0,7);
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+WHERE t.a != 0 AND t2.a != 0) AS m
FROM (SELECT * FROM t3) AS t
GROUP BY 2;
-SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+SUM(DISTINCT b) m
7 NULL
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
@@ -6557,66 +6610,93 @@ CREATE TABLE t3 (f3a int default 1, f3b int default 2);
INSERT INTO t3 VALUES (1,1),(2,2);
set @old_optimizer_switch = @@session.optimizer_switch;
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
set @@session.optimizer_switch=@old_optimizer_switch;
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2 AS m;
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
select (null, null) = (null, null);
(null, null) = (null, null)
@@ -6662,8 +6742,10 @@ INSERT INTO t2 VALUES (1);
CREATE TABLE t3 ( c INT );
INSERT INTO t3 VALUES (4),(5);
SET optimizer_switch='subquery_cache=off';
-SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
-( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
+SELECT
+( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) AS m
+FROM t1;
+m
1
NULL
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
@@ -6879,7 +6961,9 @@ CREATE TABLE t3 (c INT);
INSERT INTO t3 VALUES (8),(3);
set @@expensive_subquery_limit= 0;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6889,9 +6973,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
@@ -6917,7 +7003,9 @@ Handler_read_rnd_deleted 0
Handler_read_rnd_next 22
set @@expensive_subquery_limit= default;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6927,9 +7015,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
diff --git a/mysql-test/main/subselect_no_opts.result b/mysql-test/main/subselect_no_opts.result
index 91e82ca..72b148b 100644
--- a/mysql-test/main/subselect_no_opts.result
+++ b/mysql-test/main/subselect_no_opts.result
@@ -121,27 +121,27 @@ ROW(1,2,3) > (SELECT 1,2,1)
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
ROW(1,2,3) = (SELECT 1,2,NULL)
NULL
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a') AS m;
+m
1
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b') AS m;
+m
0
-SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
-(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
-SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
-(SELECT 'b',2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'b'
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
-(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a') AS m;
+m
1
-SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
-(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: 'c'
@@ -231,19 +231,26 @@ a
2
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
a
-select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
-b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
+b m
8 7.5000
8 4.5000
9 7.5000
-explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
+explain extended
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,(/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
+Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,(/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`) AS `m` from `test`.`t4`
select * from t3 where exists (select * from t2 where t2.b=t3.a);
a
7
@@ -310,21 +317,34 @@ select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 w
b ma
7 12
create table t5 (a int);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (5);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (2);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
-explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
+explain extended
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
@@ -333,7 +353,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
Warnings:
Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select (/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
+Note 1003 /* select#1 */ select (/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`) AS `m`,`test`.`t2`.`a` AS `a` from `test`.`t2`
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
ERROR 21000: Subquery returns more than 1 row
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
@@ -489,8 +509,11 @@ SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING t
mot topic date pseudo
joce 40143 2002-10-22 joce
joce 43506 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 1
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
@@ -508,8 +531,11 @@ joce 40143 2002-10-22 joce
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
mot topic date pseudo
joce 40143 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 0
drop table t1,t2;
@@ -884,6 +910,25 @@ NULL
select 1.5 > ANY (SELECT * from t1);
1.5 > ANY (SELECT * from t1)
NULL
+update t1 set a=NULL where a=2.5;
+select 1.5 IN (SELECT * from t1);
+1.5 IN (SELECT * from t1)
+1
+select 3.5 IN (SELECT * from t1);
+3.5 IN (SELECT * from t1)
+1
+select 10.5 IN (SELECT * from t1);
+10.5 IN (SELECT * from t1)
+NULL
+select 1.5 > ALL (SELECT * from t1);
+1.5 > ALL (SELECT * from t1)
+0
+select 10.5 > ALL (SELECT * from t1);
+10.5 > ALL (SELECT * from t1)
+NULL
+select 1.5 > ANY (SELECT * from t1);
+1.5 > ANY (SELECT * from t1)
+NULL
select 10.5 > ANY (SELECT * from t1);
10.5 > ANY (SELECT * from t1)
1
@@ -894,6 +939,20 @@ Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1249 Select 2 was reduced during optimization
Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
select (select a+1) from t1;
(select a+1)
2.5
@@ -1535,8 +1594,8 @@ create table t3 (a int, b int);
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
insert into t3 values (3,3), (2,2), (1,1);
-select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
-a (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1)
+select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) as m from t3;
+a m
3 1
2 2
1 2
@@ -1731,8 +1790,8 @@ CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(1
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
-select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
-dbid name (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')
+select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') as m FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') as m from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
+dbid name m
-1 Valid 1
-1 Valid 2 1
-1 Should Not Return 0
@@ -3783,9 +3842,10 @@ SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2
1
1
-SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+SELECT
+(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+m
2
1
1
@@ -3795,9 +3855,9 @@ COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
1 1
1 1
SELECT COUNT(DISTINCT t1.b),
-(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+COUNT(DISTINCT t1.b) m
2 2
1 1
1 1
@@ -3821,16 +3881,10 @@ SELECT (
SELECT COUNT(DISTINCT t1.b)
)
)
-FROM t1 GROUP BY t1.a LIMIT 1)
+FROM t1 GROUP BY t1.a LIMIT 1) AS m
FROM t1 t2
GROUP BY t2.a;
-(
-SELECT (
-SELECT (
-SELECT COUNT(DISTINCT t1.b)
-)
-)
-FROM t1 GROUP BY t1.a LIMIT 1)
+m
2
2
2
@@ -6416,11 +6470,10 @@ CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (10,7), (0,7);
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+WHERE t.a != 0 AND t2.a != 0) AS m
FROM (SELECT * FROM t3) AS t
GROUP BY 2;
-SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+SUM(DISTINCT b) m
7 NULL
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
@@ -6553,66 +6606,93 @@ CREATE TABLE t3 (f3a int default 1, f3b int default 2);
INSERT INTO t3 VALUES (1,1),(2,2);
set @old_optimizer_switch = @@session.optimizer_switch;
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
set @@session.optimizer_switch=@old_optimizer_switch;
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2 AS m;
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
select (null, null) = (null, null);
(null, null) = (null, null)
@@ -6658,8 +6738,10 @@ INSERT INTO t2 VALUES (1);
CREATE TABLE t3 ( c INT );
INSERT INTO t3 VALUES (4),(5);
SET optimizer_switch='subquery_cache=off';
-SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
-( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
+SELECT
+( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) AS m
+FROM t1;
+m
1
NULL
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
@@ -6876,7 +6958,9 @@ CREATE TABLE t3 (c INT);
INSERT INTO t3 VALUES (8),(3);
set @@expensive_subquery_limit= 0;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6886,9 +6970,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
@@ -6914,7 +7000,9 @@ Handler_read_rnd_deleted 0
Handler_read_rnd_next 22
set @@expensive_subquery_limit= default;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6924,9 +7012,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
diff --git a/mysql-test/main/subselect_no_scache.result b/mysql-test/main/subselect_no_scache.result
index 1cc69ef..4717eb9 100644
--- a/mysql-test/main/subselect_no_scache.result
+++ b/mysql-test/main/subselect_no_scache.result
@@ -124,27 +124,27 @@ ROW(1,2,3) > (SELECT 1,2,1)
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
ROW(1,2,3) = (SELECT 1,2,NULL)
NULL
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a') AS m;
+m
1
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b') AS m;
+m
0
-SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
-(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
-SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
-(SELECT 'b',2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'b'
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
-(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a') AS m;
+m
1
-SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
-(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: 'c'
@@ -234,19 +234,26 @@ a
2
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
a
-select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
-b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
+b m
8 7.5000
8 4.5000
9 7.5000
-explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
+explain extended
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,(/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
+Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,(/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`) AS `m` from `test`.`t4`
select * from t3 where exists (select * from t2 where t2.b=t3.a);
a
7
@@ -313,21 +320,34 @@ select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 w
b ma
7 12
create table t5 (a int);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (5);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (2);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
-explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
+explain extended
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
@@ -336,7 +356,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
Warnings:
Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select (/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
+Note 1003 /* select#1 */ select (/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`) AS `m`,`test`.`t2`.`a` AS `a` from `test`.`t2`
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
ERROR 21000: Subquery returns more than 1 row
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
@@ -492,8 +512,11 @@ SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING t
mot topic date pseudo
joce 40143 2002-10-22 joce
joce 43506 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 1
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
@@ -511,8 +534,11 @@ joce 40143 2002-10-22 joce
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
mot topic date pseudo
joce 40143 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 0
drop table t1,t2;
@@ -887,6 +913,25 @@ NULL
select 1.5 > ANY (SELECT * from t1);
1.5 > ANY (SELECT * from t1)
NULL
+update t1 set a=NULL where a=2.5;
+select 1.5 IN (SELECT * from t1);
+1.5 IN (SELECT * from t1)
+1
+select 3.5 IN (SELECT * from t1);
+3.5 IN (SELECT * from t1)
+1
+select 10.5 IN (SELECT * from t1);
+10.5 IN (SELECT * from t1)
+NULL
+select 1.5 > ALL (SELECT * from t1);
+1.5 > ALL (SELECT * from t1)
+0
+select 10.5 > ALL (SELECT * from t1);
+10.5 > ALL (SELECT * from t1)
+NULL
+select 1.5 > ANY (SELECT * from t1);
+1.5 > ANY (SELECT * from t1)
+NULL
select 10.5 > ANY (SELECT * from t1);
10.5 > ANY (SELECT * from t1)
1
@@ -897,6 +942,20 @@ Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1249 Select 2 was reduced during optimization
Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
select (select a+1) from t1;
(select a+1)
2.5
@@ -1538,8 +1597,8 @@ create table t3 (a int, b int);
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
insert into t3 values (3,3), (2,2), (1,1);
-select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
-a (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1)
+select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) as m from t3;
+a m
3 1
2 2
1 2
@@ -1734,8 +1793,8 @@ CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(1
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
-select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
-dbid name (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')
+select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') as m FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') as m from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
+dbid name m
-1 Valid 1
-1 Valid 2 1
-1 Should Not Return 0
@@ -3790,9 +3849,10 @@ SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2
1
1
-SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+SELECT
+(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+m
2
1
1
@@ -3802,9 +3862,9 @@ COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
1 1
1 1
SELECT COUNT(DISTINCT t1.b),
-(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+COUNT(DISTINCT t1.b) m
2 2
1 1
1 1
@@ -3828,16 +3888,10 @@ SELECT (
SELECT COUNT(DISTINCT t1.b)
)
)
-FROM t1 GROUP BY t1.a LIMIT 1)
+FROM t1 GROUP BY t1.a LIMIT 1) AS m
FROM t1 t2
GROUP BY t2.a;
-(
-SELECT (
-SELECT (
-SELECT COUNT(DISTINCT t1.b)
-)
-)
-FROM t1 GROUP BY t1.a LIMIT 1)
+m
2
2
2
@@ -6431,11 +6485,10 @@ CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (10,7), (0,7);
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+WHERE t.a != 0 AND t2.a != 0) AS m
FROM (SELECT * FROM t3) AS t
GROUP BY 2;
-SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+SUM(DISTINCT b) m
7 NULL
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
@@ -6568,66 +6621,93 @@ CREATE TABLE t3 (f3a int default 1, f3b int default 2);
INSERT INTO t3 VALUES (1,1),(2,2);
set @old_optimizer_switch = @@session.optimizer_switch;
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
set @@session.optimizer_switch=@old_optimizer_switch;
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2 AS m;
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
select (null, null) = (null, null);
(null, null) = (null, null)
@@ -6673,8 +6753,10 @@ INSERT INTO t2 VALUES (1);
CREATE TABLE t3 ( c INT );
INSERT INTO t3 VALUES (4),(5);
SET optimizer_switch='subquery_cache=off';
-SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
-( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
+SELECT
+( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) AS m
+FROM t1;
+m
1
NULL
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
@@ -6891,7 +6973,9 @@ CREATE TABLE t3 (c INT);
INSERT INTO t3 VALUES (8),(3);
set @@expensive_subquery_limit= 0;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6901,9 +6985,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
@@ -6929,7 +7015,9 @@ Handler_read_rnd_deleted 0
Handler_read_rnd_next 58
set @@expensive_subquery_limit= default;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6939,9 +7027,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
diff --git a/mysql-test/main/subselect_no_semijoin.result b/mysql-test/main/subselect_no_semijoin.result
index 7dbee3f..9450cf4 100644
--- a/mysql-test/main/subselect_no_semijoin.result
+++ b/mysql-test/main/subselect_no_semijoin.result
@@ -121,27 +121,27 @@ ROW(1,2,3) > (SELECT 1,2,1)
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
ROW(1,2,3) = (SELECT 1,2,NULL)
NULL
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a') AS m;
+m
1
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
-(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b') AS m;
+m
0
-SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
-(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
+SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
-SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
-(SELECT 'b',2,'a') = ROW(1.5,2,'a')
+SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'b'
-SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
-(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
+SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a') AS m;
+m
1
-SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
-(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
+SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a') AS m;
+m
0
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: 'c'
@@ -231,19 +231,26 @@ a
2
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
a
-select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
-b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
+b m
8 7.5000
8 4.5000
9 7.5000
-explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
+explain extended
+select
+b,
+(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) as m
+from t4;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
+Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `m` from `test`.`t4`
select * from t3 where exists (select * from t2 where t2.b=t3.a);
a
7
@@ -310,21 +317,34 @@ select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 w
b ma
7 12
create table t5 (a int);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (5);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
insert into t5 values (2);
-select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
-(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
+m a
NULL 1
2 2
-explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
+explain extended
+select
+(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) as m,
+a
+from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
@@ -333,7 +353,7 @@ NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
Warnings:
Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
-Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
+Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `m`,`test`.`t2`.`a` AS `a` from `test`.`t2`
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
ERROR 21000: Subquery returns more than 1 row
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
@@ -489,8 +509,11 @@ SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING t
mot topic date pseudo
joce 40143 2002-10-22 joce
joce 43506 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 1
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
@@ -508,8 +531,11 @@ joce 40143 2002-10-22 joce
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
mot topic date pseudo
joce 40143 2002-10-22 joce
-SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
-mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
+SELECT
+*,
+topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) AS m
+FROM t2;
+mot topic date pseudo m
joce 40143 2002-10-22 joce 1
joce 43506 2002-10-22 joce 0
drop table t1,t2;
@@ -884,6 +910,25 @@ NULL
select 1.5 > ANY (SELECT * from t1);
1.5 > ANY (SELECT * from t1)
NULL
+update t1 set a=NULL where a=2.5;
+select 1.5 IN (SELECT * from t1);
+1.5 IN (SELECT * from t1)
+1
+select 3.5 IN (SELECT * from t1);
+3.5 IN (SELECT * from t1)
+1
+select 10.5 IN (SELECT * from t1);
+10.5 IN (SELECT * from t1)
+NULL
+select 1.5 > ALL (SELECT * from t1);
+1.5 > ALL (SELECT * from t1)
+0
+select 10.5 > ALL (SELECT * from t1);
+10.5 > ALL (SELECT * from t1)
+NULL
+select 1.5 > ANY (SELECT * from t1);
+1.5 > ANY (SELECT * from t1)
+NULL
select 10.5 > ANY (SELECT * from t1);
10.5 > ANY (SELECT * from t1)
1
@@ -894,6 +939,20 @@ Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1249 Select 2 was reduced during optimization
Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
+explain extended select (select a+1) from t1;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1249 Select 2 was reduced during optimization
+Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
select (select a+1) from t1;
(select a+1)
2.5
@@ -1535,8 +1594,8 @@ create table t3 (a int, b int);
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
insert into t3 values (3,3), (2,2), (1,1);
-select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
-a (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1)
+select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) as m from t3;
+a m
3 1
2 2
1 2
@@ -1731,8 +1790,8 @@ CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(1
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
-select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
-dbid name (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')
+select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') as m FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') as m from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
+dbid name m
-1 Valid 1
-1 Valid 2 1
-1 Should Not Return 0
@@ -3783,9 +3842,10 @@ SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2
1
1
-SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+SELECT
+(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
+m
2
1
1
@@ -3795,9 +3855,9 @@ COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
1 1
1 1
SELECT COUNT(DISTINCT t1.b),
-(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) AS m
FROM t1 GROUP BY t1.a;
-COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
+COUNT(DISTINCT t1.b) m
2 2
1 1
1 1
@@ -3821,16 +3881,10 @@ SELECT (
SELECT COUNT(DISTINCT t1.b)
)
)
-FROM t1 GROUP BY t1.a LIMIT 1)
+FROM t1 GROUP BY t1.a LIMIT 1) AS m
FROM t1 t2
GROUP BY t2.a;
-(
-SELECT (
-SELECT (
-SELECT COUNT(DISTINCT t1.b)
-)
-)
-FROM t1 GROUP BY t1.a LIMIT 1)
+m
2
2
2
@@ -6416,11 +6470,10 @@ CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (10,7), (0,7);
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+WHERE t.a != 0 AND t2.a != 0) AS m
FROM (SELECT * FROM t3) AS t
GROUP BY 2;
-SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
-WHERE t.a != 0 AND t2.a != 0)
+SUM(DISTINCT b) m
7 NULL
SELECT SUM(DISTINCT b),
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
@@ -6553,66 +6606,93 @@ CREATE TABLE t3 (f3a int default 1, f3b int default 2);
INSERT INTO t3 VALUES (1,1),(2,2);
set @old_optimizer_switch = @@session.optimizer_switch;
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
set @@session.optimizer_switch=@old_optimizer_switch;
-SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
-(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
-SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
+SELECT
+(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2 AS m;
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
NULL
NULL
-SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
-(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
+SELECT
+(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m
+FROM t2;
+m
NULL
NULL
-SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
-(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
+SELECT
+(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) AS m;
+m
NULL
select (null, null) = (null, null);
(null, null) = (null, null)
@@ -6658,8 +6738,10 @@ INSERT INTO t2 VALUES (1);
CREATE TABLE t3 ( c INT );
INSERT INTO t3 VALUES (4),(5);
SET optimizer_switch='subquery_cache=off';
-SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
-( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
+SELECT
+( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) AS m
+FROM t1;
+m
1
NULL
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
@@ -6876,7 +6958,9 @@ CREATE TABLE t3 (c INT);
INSERT INTO t3 VALUES (8),(3);
set @@expensive_subquery_limit= 0;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6886,9 +6970,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
@@ -6914,7 +7000,9 @@ Handler_read_rnd_deleted 0
Handler_read_rnd_next 22
set @@expensive_subquery_limit= default;
EXPLAIN
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
@@ -6924,9 +7012,11 @@ id select_type table type possible_keys key key_len ref rows Extra
2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
flush status;
-SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+SELECT
+(SELECT MIN(b) FROM t1, t2
+WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) AS m
FROM t2 alias1, t1 alias2, t1 alias3;
-(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
+m
NULL
NULL
NULL
diff --git a/sql/item.cc b/sql/item.cc
index ca66974..be7780c 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -5655,7 +5655,8 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
max_arg_level for the function if it's needed.
*/
if (thd->lex->in_sum_func &&
- thd->lex == context->select_lex->parent_lex &&
+ last_checked_context->select_lex->parent_lex ==
+ context->select_lex->parent_lex &&
thd->lex->in_sum_func->nest_level >= select->nest_level)
{
Item::Type ref_type= (*reference)->type();
@@ -5681,7 +5682,8 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
(Item_ident*) (*reference) :
0), false);
if (thd->lex->in_sum_func &&
- thd->lex == context->select_lex->parent_lex &&
+ last_checked_context->select_lex->parent_lex ==
+ context->select_lex->parent_lex &&
thd->lex->in_sum_func->nest_level >= select->nest_level)
{
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
@@ -6017,7 +6019,6 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
if (!thd->lex->current_select->no_wrap_view_item &&
thd->lex->in_sum_func &&
- thd->lex == select->parent_lex &&
thd->lex->in_sum_func->nest_level ==
select->nest_level)
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
@@ -8051,7 +8052,8 @@ bool Item_ref::fix_fields(THD *thd, Item **reference)
max_arg_level for the function if it's needed.
*/
if (thd->lex->in_sum_func &&
- thd->lex == context->select_lex->parent_lex &&
+ last_checked_context->select_lex->parent_lex ==
+ context->select_lex->parent_lex &&
thd->lex->in_sum_func->nest_level >=
last_checked_context->select_lex->nest_level)
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
@@ -8075,7 +8077,8 @@ bool Item_ref::fix_fields(THD *thd, Item **reference)
max_arg_level for the function if it's needed.
*/
if (thd->lex->in_sum_func &&
- thd->lex == context->select_lex->parent_lex &&
+ last_checked_context->select_lex->parent_lex ==
+ context->select_lex->parent_lex &&
thd->lex->in_sum_func->nest_level >=
last_checked_context->select_lex->nest_level)
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
@@ -8090,7 +8093,8 @@ bool Item_ref::fix_fields(THD *thd, Item **reference)
1. outer reference (will be fixed later by the fix_inner_refs function);
2. an unnamed reference inside an aggregate function.
*/
- if (!((*ref)->type() == REF_ITEM &&
+ if (!set_properties_only &&
+ !((*ref)->type() == REF_ITEM &&
((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
(((*ref)->with_sum_func() && name.str &&
!(current_sel->get_linkage() != GLOBAL_OPTIONS_TYPE &&
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index f80b89b..9a0f08e 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -408,7 +408,8 @@ bool Item_sum::register_sum_func(THD *thd, Item **ref)
sl= sl->master_unit()->outer_select() )
sl->master_unit()->item->get_with_sum_func_cache()->set_with_sum_func();
}
- thd->lex->current_select->mark_as_dependent(thd, aggr_sel, NULL);
+ if (aggr_sel)
+ thd->lex->current_select->mark_as_dependent(thd, aggr_sel, NULL);
if ((thd->lex->describe & DESCRIBE_EXTENDED) && aggr_sel)
{
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index d1ee15f..8201fd8 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -7994,9 +7994,8 @@ bool setup_tables(THD *thd, Name_resolution_context *context,
table_list;
table_list= table_list->next_local)
{
- if (table_list->merge_underlying_list)
+ if (table_list->is_merged_derived() && table_list->merge_underlying_list)
{
- DBUG_ASSERT(table_list->is_merged_derived());
Query_arena *arena, backup;
arena= thd->activate_stmt_arena_if_needed(&backup);
bool res;
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc
index 4dcc8a6..2e23273 100644
--- a/sql/sql_derived.cc
+++ b/sql/sql_derived.cc
@@ -351,24 +351,6 @@ bool mysql_derived_merge(THD *thd, LEX *lex, TABLE_LIST *derived)
DBUG_RETURN(FALSE);
}
- if (dt_select->uncacheable & UNCACHEABLE_RAND)
- {
- /* There is random function => fall back to materialization. */
- cause= "Random function in the select";
- if (unlikely(thd->trace_started()))
- {
- OPT_TRACE_VIEWS_TRANSFORM(thd, trace_wrapper, trace_derived,
- derived->is_derived() ? "derived" : "view",
- derived->alias.str ? derived->alias.str : "<NULL>",
- derived->get_unit()->first_select()->select_number,
- "materialized");
- trace_derived.add("cause", cause);
- }
- derived->change_refs_to_fields();
- derived->set_materialized_derived();
- DBUG_RETURN(FALSE);
- }
-
if (derived->dt_handler)
{
derived->change_refs_to_fields();
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index 24958b5..4bd65ea 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -3283,40 +3283,45 @@ LEX::LEX()
}
+bool LEX::can_be_merged()
+{
+ return unit.can_be_merged();
+}
+
+
/*
- Check whether the merging algorithm can be used on this VIEW
+ Check whether the merging algorithm can be used for this unit
SYNOPSIS
- LEX::can_be_merged()
+ st_select_lex_unit::can_be_merged()
DESCRIPTION
- We can apply merge algorithm if it is single SELECT view with
- subqueries only in WHERE clause (we do not count SELECTs of underlying
- views, and second level subqueries) and we have not grpouping, ordering,
- HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
- several underlying tables.
+ We can apply merge algorithm for a unit if it is single SELECT with
+ subqueries only in WHERE clauses or in ON conditions or in select list
+ (we do not count SELECTs of underlying views/derived tables/CTEs and
+ second level subqueries) and we have no grouping, ordering, HAVING
+ clause, aggregate functions, DISTINCT clause, LIMIT clause.
RETURN
FALSE - only temporary table algorithm can be used
TRUE - merge algorithm can be used
*/
-bool LEX::can_be_merged()
+bool st_select_lex_unit::can_be_merged()
{
// TODO: do not forget implement case when select_lex.table_list.elements==0
/* find non VIEW subqueries/unions */
- bool selects_allow_merge= (first_select_lex()->next_select() == 0 &&
- !(first_select_lex()->uncacheable &
+ bool selects_allow_merge= (first_select()->next_select() == 0 &&
+ !(first_select()->uncacheable &
UNCACHEABLE_RAND));
if (selects_allow_merge)
{
- for (SELECT_LEX_UNIT *tmp_unit= first_select_lex()->first_inner_unit();
+ for (SELECT_LEX_UNIT *tmp_unit= first_select()->first_inner_unit();
tmp_unit;
tmp_unit= tmp_unit->next_unit())
{
- if (tmp_unit->first_select()->parent_lex == this &&
- (tmp_unit->item != 0 &&
+ if ((tmp_unit->item != 0 &&
(tmp_unit->item->place() != IN_WHERE &&
tmp_unit->item->place() != IN_ON &&
tmp_unit->item->place() != SELECT_LIST)))
@@ -3328,12 +3333,12 @@ bool LEX::can_be_merged()
}
return (selects_allow_merge &&
- first_select_lex()->group_list.elements == 0 &&
- first_select_lex()->having == 0 &&
- first_select_lex()->with_sum_func == 0 &&
- first_select_lex()->table_list.elements >= 1 &&
- !(first_select_lex()->options & SELECT_DISTINCT) &&
- first_select_lex()->select_limit == 0);
+ first_select()->group_list.elements == 0 &&
+ first_select()->having == 0 &&
+ first_select()->with_sum_func == 0 &&
+ first_select()->table_list.elements >= 1 &&
+ !(first_select()->options & SELECT_DISTINCT) &&
+ first_select()->select_limit == 0);
}
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index 3545a4e..1cc6569 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -1003,6 +1003,8 @@ class st_select_lex_unit: public st_select_lex_node {
bool set_lock_to_the_last_select(Lex_select_lock l);
+ bool can_be_merged();
+
friend class st_select_lex;
};
@@ -1132,7 +1134,6 @@ class st_select_lex: public st_select_lex_node
st_select_lex.
*/
uint curr_tvc_name;
-
/*
Needed to correctly generate 'PRIMARY' or 'SIMPLE' for select_type column
of EXPLAIN
diff --git a/sql/table.cc b/sql/table.cc
index 141b2e4..1299553 100644
--- a/sql/table.cc
+++ b/sql/table.cc
@@ -6484,6 +6484,9 @@ bool TABLE_LIST::prepare_security(THD *thd)
#ifndef DBUG_OFF
void TABLE_LIST::set_check_merged()
{
+ if (is_view())
+ return;
+
DBUG_ASSERT(derived);
/*
It is not simple to check all, but at least this should be checked:
@@ -6811,9 +6814,8 @@ void Field_iterator_table_ref::set_field_iterator()
table_ref->alias.str));
}
/* This is a merge view, so use field_translation. */
- else if (table_ref->field_translation)
+ else if (table_ref->is_merged_derived() && table_ref->field_translation)
{
- DBUG_ASSERT(table_ref->is_merged_derived());
field_it= &view_field_it;
DBUG_PRINT("info", ("field_it for '%s' is Field_iterator_view",
table_ref->alias.str));
@@ -9263,15 +9265,15 @@ bool TABLE_LIST::init_derived(THD *thd, bool init_view)
set_derived();
}
- if (!is_view() &&
+ if (is_view() ||
!derived_table_optimization_done(this))
{
/* A subquery might be forced to be materialized due to a side-effect. */
- if (!is_materialized_derived() && first_select->is_mergeable() &&
- optimizer_flag(thd, OPTIMIZER_SWITCH_DERIVED_MERGE) &&
+ if (!is_materialized_derived() && unit->can_be_merged() &&
+ (optimizer_flag(thd, OPTIMIZER_SWITCH_DERIVED_MERGE) || is_view()) &&
!thd->lex->can_not_use_merged() &&
- !(thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
- thd->lex->sql_command == SQLCOM_DELETE_MULTI) &&
+ !((thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
+ thd->lex->sql_command == SQLCOM_DELETE_MULTI) && !is_view()) &&
!is_recursive_with_table())
set_merged_derived();
else
diff --git a/sql/table.h b/sql/table.h
index 3adcc88..c4b0d78 100644
--- a/sql/table.h
+++ b/sql/table.h
@@ -2863,8 +2863,7 @@ struct TABLE_LIST
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
(alias.str ? alias.str : "<NULL>"),
get_unit()));
- derived_type= ((derived_type & DTYPE_MASK) |
- DTYPE_TABLE | DTYPE_MERGE);
+ derived_type= (derived_type & DTYPE_MASK) | DTYPE_MERGE;
set_check_merged();
DBUG_VOID_RETURN;
}
@@ -2878,9 +2877,7 @@ struct TABLE_LIST
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
(alias.str ? alias.str : "<NULL>"),
get_unit()));
- derived= get_unit();
- derived_type= ((derived_type & (derived ? DTYPE_MASK : DTYPE_VIEW)) |
- DTYPE_TABLE | DTYPE_MATERIALIZE);
+ derived_type= (derived_type & DTYPE_MASK) | DTYPE_MATERIALIZE;
set_check_materialized();
DBUG_VOID_RETURN;
}
1
0
[Commits] 841e887: MDEV-28603 Invalid view when its definition uses TVC as single-value subquery
by IgorBabaev 27 Feb '23
by IgorBabaev 27 Feb '23
27 Feb '23
revision-id: 841e8877ccb8ef5d692a22b4447383a360557326 (mariadb-10.4.27-107-g841e887)
parent(s): 839c7fcf38ef17f9627f63ebec3e82d814912973
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-02-27 10:51:22 -0800
message:
MDEV-28603 Invalid view when its definition uses TVC as single-value subquery
Subselect_single_value_engine cannot handle table value constructor used as
subquery. That's why any table value constructor TVC used as subquery is
converted into a select over derived table whose specification is TVC.
Currently the names of the columns of the derived table DT are taken from
the first element of TVC and if the k-th component of the element happens
to be a subquery the text representation of this subquery serves as the
name of the k-th column of the derived table. References of all columns of
the derived table DT compose the select list of the result of the conversion.
If a definition of a view contained a table value constructor used as a
subquery and the view was registered after this conversion had been
applied we could register an invalid view definition if the first element
of TVC contained a subquery as its component: the name of this component
was taken from the original subquery, while the name of the corresponding
column of the derived table was taken from the text representation of the
subquery produced by the function SELECT_LEX::print() and these names were
usually differ from each other.
To avoid registration of such invalid views the function SELECT_LEX::print()
now prints the original TVC instead of the select in which this TVC has
been wrapped. Now the specification of registered view looks like as if no
conversions from TVC to selects were done.
Approved by Oleksandr Byelkin <sanja(a)mariadb.com>
---
mysql-test/main/table_value_constr.result | 119 ++++++++++++++++++++++++++++++
mysql-test/main/table_value_constr.test | 74 +++++++++++++++++++
sql/mysqld.h | 2 +
sql/sql_lex.cc | 1 +
sql/sql_lex.h | 3 +-
sql/sql_select.cc | 6 ++
sql/sql_show.cc | 3 +-
sql/sql_tvc.cc | 1 +
sql/sql_view.cc | 6 +-
9 files changed, 211 insertions(+), 4 deletions(-)
diff --git a/mysql-test/main/table_value_constr.result b/mysql-test/main/table_value_constr.result
index c8e6363..bd0aac5 100644
--- a/mysql-test/main/table_value_constr.result
+++ b/mysql-test/main/table_value_constr.result
@@ -3133,5 +3133,124 @@ INSERT INTO t1 (VALUES (IGNORE) UNION VALUES (IGNORE));
ERROR HY000: 'ignore' is not allowed in this context
DROP TABLE t1;
#
+# MDEV-28603: VIEW with table value constructor used as single-value
+# subquery contains subquery as its first element
+#
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+create table t2 (b int);
+insert into t2 values (1), (2);
+create view v as select (values ((select * from t1 where a > 5))) as m from t2;
+select (values ((select * from t1 where a > 5))) as m from t2;
+m
+7
+7
+select * from v;
+m
+7
+7
+with cte as ( select (values ((select * from t1 where a > 5))) as m from t2 ) select * from cte;
+m
+7
+7
+explain select (values ((select * from t1 where a > 5))) as m from t2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t2 ALL NULL NULL NULL NULL 2
+4 SUBQUERY <derived2> ALL NULL NULL NULL NULL 2
+2 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
+3 SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
+explain select * from v;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t2 ALL NULL NULL NULL NULL 2
+5 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2
+3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
+4 SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
+explain with cte as ( select (values ((select * from t1 where a > 5))) as m from t2 ) select * from cte;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t2 ALL NULL NULL NULL NULL 2
+5 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2
+3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
+4 SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
+prepare stmt from "select (values ((select * from t1 where a > 5))) as m from t2";
+execute stmt;
+m
+7
+7
+execute stmt;
+m
+7
+7
+deallocate prepare stmt;
+prepare stmt from "select * from v";
+execute stmt;
+m
+7
+7
+execute stmt;
+m
+7
+7
+deallocate prepare stmt;
+prepare stmt from "with cte as ( select (values ((select * from t1 where a > 5))) as m from t2 ) select * from cte";
+execute stmt;
+m
+7
+7
+execute stmt;
+m
+7
+7
+deallocate prepare stmt;
+show create view v;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+drop view v;
+prepare stmt from "create view v as select (values ((select * from t1 where a > 5))) as m from t2";
+execute stmt;
+show create view v;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+select * from v;
+m
+7
+7
+drop view v;
+execute stmt;
+show create view v;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+select * from v;
+m
+7
+7
+deallocate prepare stmt;
+prepare stmt from "show create view v";
+execute stmt;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+execute stmt;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+deallocate prepare stmt;
+drop view v;
+create view v as select (values ((select * from t1 where a > 5
+union
+select * from t1 where a > 7))) as m from t2;
+select (values ((select * from t1 where a > 5
+union
+select * from t1 where a > 7))) as m from t2;
+m
+7
+7
+select * from v;
+m
+7
+7
+show create view v;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5 union select `t1`.`a` from `t1` where `t1`.`a` > 7))) AS `m` from `t2` latin1 latin1_swedish_ci
+drop view v;
+drop table t1,t2;
+#
# End of 10.4 tests
#
diff --git a/mysql-test/main/table_value_constr.test b/mysql-test/main/table_value_constr.test
index 19af93e..1d58cd0 100644
--- a/mysql-test/main/table_value_constr.test
+++ b/mysql-test/main/table_value_constr.test
@@ -1737,5 +1737,79 @@ INSERT INTO t1 (VALUES (IGNORE) UNION VALUES (IGNORE));
DROP TABLE t1;
--echo #
+--echo # MDEV-28603: VIEW with table value constructor used as single-value
+--echo # subquery contains subquery as its first element
+--echo #
+
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+create table t2 (b int);
+insert into t2 values (1), (2);
+
+let $q=
+select (values ((select * from t1 where a > 5))) as m from t2;
+
+eval create view v as $q;
+
+eval $q;
+eval select * from v;
+eval with cte as ( $q ) select * from cte;
+
+eval explain $q;
+eval explain select * from v;
+eval explain with cte as ( $q ) select * from cte;
+
+eval prepare stmt from "$q";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+eval prepare stmt from "select * from v";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+eval prepare stmt from "with cte as ( $q ) select * from cte";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+show create view v;
+
+drop view v;
+
+eval prepare stmt from "create view v as $q";
+execute stmt;
+show create view v;
+select * from v;
+drop view v;
+execute stmt;
+show create view v;
+select * from v;
+deallocate prepare stmt;
+
+prepare stmt from "show create view v";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+drop view v;
+
+let $q=
+select (values ((select * from t1 where a > 5
+ union
+ select * from t1 where a > 7))) as m from t2;
+
+eval create view v as $q;
+
+eval $q;
+eval select * from v;
+
+show create view v;
+
+drop view v;
+drop table t1,t2;
+
+--echo #
--echo # End of 10.4 tests
--echo #
diff --git a/sql/mysqld.h b/sql/mysqld.h
index 60afb0b..f521ea2 100644
--- a/sql/mysqld.h
+++ b/sql/mysqld.h
@@ -755,6 +755,8 @@ enum enum_query_type
// it evaluates to. Should be used for error messages, so that they
// don't reveal values.
QT_NO_DATA_EXPANSION= (1 << 9),
+ // Remove wrappers added for TVC when creating or showing view
+ QT_NO_WRAPPERS_FOR_TVC_IN_VIEW= (1 << 11),
};
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index 24958b5..62c7556 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -2487,6 +2487,7 @@ void st_select_lex::init_select()
curr_tvc_name= 0;
in_tvc= false;
versioned_tables= 0;
+ is_tvc_wrapper= false;
}
/*
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index 3545a4e..8f25426 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -1132,7 +1132,8 @@ class st_select_lex: public st_select_lex_node
st_select_lex.
*/
uint curr_tvc_name;
-
+ /* true <=> select has been created a TVC wrapper */
+ bool is_tvc_wrapper;
/*
Needed to correctly generate 'PRIMARY' or 'SIMPLE' for select_type column
of EXPLAIN
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index f83bf32..2c12d1c 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -27992,6 +27992,12 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
return;
}
+ if (is_tvc_wrapper && (query_type & QT_NO_WRAPPERS_FOR_TVC_IN_VIEW))
+ {
+ first_inner_unit()->first_select()->print(thd, str, query_type);
+ return;
+ }
+
if ((query_type & QT_SHOW_SELECT_NUMBER) &&
thd->lex->all_selects_list &&
thd->lex->all_selects_list->link_next &&
diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index 9fee8ce..1ad20ac 100644
--- a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -2707,7 +2707,8 @@ static int show_create_view(THD *thd, TABLE_LIST *table, String *buff)
a different syntax, like when ANSI_QUOTES is defined.
*/
table->view->unit.print(buff, enum_query_type(QT_VIEW_INTERNAL |
- QT_ITEM_ORIGINAL_FUNC_NULLIF));
+ QT_ITEM_ORIGINAL_FUNC_NULLIF |
+ QT_NO_WRAPPERS_FOR_TVC_IN_VIEW));
if (table->with_check != VIEW_CHECK_NONE)
{
diff --git a/sql/sql_tvc.cc b/sql/sql_tvc.cc
index d8e6770..71377d3 100644
--- a/sql/sql_tvc.cc
+++ b/sql/sql_tvc.cc
@@ -673,6 +673,7 @@ st_select_lex *wrap_tvc(THD *thd, st_select_lex *tvc_sl,
wrapper_sl->parent_lex= lex; /* Used in init_query. */
wrapper_sl->init_query();
wrapper_sl->init_select();
+ wrapper_sl->is_tvc_wrapper= true;
wrapper_sl->nest_level= tvc_sl->nest_level;
wrapper_sl->parsing_place= tvc_sl->parsing_place;
diff --git a/sql/sql_view.cc b/sql/sql_view.cc
index 1efbc68..54debf9 100644
--- a/sql/sql_view.cc
+++ b/sql/sql_view.cc
@@ -956,10 +956,12 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view,
thd->variables.sql_mode&= ~MODE_ANSI_QUOTES;
lex->unit.print(&view_query, enum_query_type(QT_VIEW_INTERNAL |
- QT_ITEM_ORIGINAL_FUNC_NULLIF));
+ QT_ITEM_ORIGINAL_FUNC_NULLIF |
+ QT_NO_WRAPPERS_FOR_TVC_IN_VIEW));
lex->unit.print(&is_query, enum_query_type(QT_TO_SYSTEM_CHARSET |
QT_WITHOUT_INTRODUCERS |
- QT_ITEM_ORIGINAL_FUNC_NULLIF));
+ QT_ITEM_ORIGINAL_FUNC_NULLIF |
+ QT_NO_WRAPPERS_FOR_TVC_IN_VIEW));
thd->variables.sql_mode|= sql_mode;
}
1
0
[Commits] c282faa: MDEV-28603 Invalid view when its definition uses TVC as single-value subquery
by IgorBabaev 27 Feb '23
by IgorBabaev 27 Feb '23
27 Feb '23
revision-id: c282faaf400e5fe5324cee816641ddd1223ed662 (mariadb-10.4.27-107-gc282faa)
parent(s): 839c7fcf38ef17f9627f63ebec3e82d814912973
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-02-26 22:26:39 -0800
message:
MDEV-28603 Invalid view when its definition uses TVC as single-value subquery
Subselect_single_value_engine cannot handle table value constructor used as
subquery. That's why any table value constructor TVC used as subquery is
converted into a select over derived table whose specification is TVC.
Currently the names of the columns of the derived table DT are taken from
the first element of TVC and if the k-th component of the element happens
to be a subquery the text representation of this subquery serves as the
name of the k-th column of the derived table. References of all columns of
the derived table DT compose the select list of the result of the conversion.
If a definition of a view contained a table value constructor used as a
subquery and the view was registered after this conversion had been
applied we could register an invalid view definition if the first element
of TVC contained a subquery as its component: the name of this component
was taken from the original subquery, while the name of the corresponding
column of the derived table was taken from the text representation of the
subquery produced by the function SELECT_LEX::print() and these names were
usually differ from each other.
To avoid registration of such invalid views the function SELECT_LEX::print()
now prints the original TVC instead of the select in which this TVC has
been wrapped. Now the specification of registered view looks like as if no
conversions from TVC to selects were done.
Approved by Oleksandr Byelkin <sanja(a)mariadb.com>
---
mysql-test/main/table_value_constr.result | 119 ++++++++++++++++++++++++++++++
mysql-test/main/table_value_constr.test | 74 +++++++++++++++++++
sql/sql_lex.cc | 1 +
sql/sql_lex.h | 3 +-
sql/sql_select.cc | 9 +++
sql/sql_tvc.cc | 1 +
6 files changed, 206 insertions(+), 1 deletion(-)
diff --git a/mysql-test/main/table_value_constr.result b/mysql-test/main/table_value_constr.result
index c8e6363..bd0aac5 100644
--- a/mysql-test/main/table_value_constr.result
+++ b/mysql-test/main/table_value_constr.result
@@ -3133,5 +3133,124 @@ INSERT INTO t1 (VALUES (IGNORE) UNION VALUES (IGNORE));
ERROR HY000: 'ignore' is not allowed in this context
DROP TABLE t1;
#
+# MDEV-28603: VIEW with table value constructor used as single-value
+# subquery contains subquery as its first element
+#
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+create table t2 (b int);
+insert into t2 values (1), (2);
+create view v as select (values ((select * from t1 where a > 5))) as m from t2;
+select (values ((select * from t1 where a > 5))) as m from t2;
+m
+7
+7
+select * from v;
+m
+7
+7
+with cte as ( select (values ((select * from t1 where a > 5))) as m from t2 ) select * from cte;
+m
+7
+7
+explain select (values ((select * from t1 where a > 5))) as m from t2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t2 ALL NULL NULL NULL NULL 2
+4 SUBQUERY <derived2> ALL NULL NULL NULL NULL 2
+2 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
+3 SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
+explain select * from v;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t2 ALL NULL NULL NULL NULL 2
+5 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2
+3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
+4 SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
+explain with cte as ( select (values ((select * from t1 where a > 5))) as m from t2 ) select * from cte;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t2 ALL NULL NULL NULL NULL 2
+5 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2
+3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
+4 SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
+prepare stmt from "select (values ((select * from t1 where a > 5))) as m from t2";
+execute stmt;
+m
+7
+7
+execute stmt;
+m
+7
+7
+deallocate prepare stmt;
+prepare stmt from "select * from v";
+execute stmt;
+m
+7
+7
+execute stmt;
+m
+7
+7
+deallocate prepare stmt;
+prepare stmt from "with cte as ( select (values ((select * from t1 where a > 5))) as m from t2 ) select * from cte";
+execute stmt;
+m
+7
+7
+execute stmt;
+m
+7
+7
+deallocate prepare stmt;
+show create view v;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+drop view v;
+prepare stmt from "create view v as select (values ((select * from t1 where a > 5))) as m from t2";
+execute stmt;
+show create view v;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+select * from v;
+m
+7
+7
+drop view v;
+execute stmt;
+show create view v;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+select * from v;
+m
+7
+7
+deallocate prepare stmt;
+prepare stmt from "show create view v";
+execute stmt;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+execute stmt;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5))) AS `m` from `t2` latin1 latin1_swedish_ci
+deallocate prepare stmt;
+drop view v;
+create view v as select (values ((select * from t1 where a > 5
+union
+select * from t1 where a > 7))) as m from t2;
+select (values ((select * from t1 where a > 5
+union
+select * from t1 where a > 7))) as m from t2;
+m
+7
+7
+select * from v;
+m
+7
+7
+show create view v;
+View Create View character_set_client collation_connection
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select (values ((select `t1`.`a` from `t1` where `t1`.`a` > 5 union select `t1`.`a` from `t1` where `t1`.`a` > 7))) AS `m` from `t2` latin1 latin1_swedish_ci
+drop view v;
+drop table t1,t2;
+#
# End of 10.4 tests
#
diff --git a/mysql-test/main/table_value_constr.test b/mysql-test/main/table_value_constr.test
index 19af93e..1d58cd0 100644
--- a/mysql-test/main/table_value_constr.test
+++ b/mysql-test/main/table_value_constr.test
@@ -1737,5 +1737,79 @@ INSERT INTO t1 (VALUES (IGNORE) UNION VALUES (IGNORE));
DROP TABLE t1;
--echo #
+--echo # MDEV-28603: VIEW with table value constructor used as single-value
+--echo # subquery contains subquery as its first element
+--echo #
+
+create table t1 (a int);
+insert into t1 values (3), (7), (1);
+create table t2 (b int);
+insert into t2 values (1), (2);
+
+let $q=
+select (values ((select * from t1 where a > 5))) as m from t2;
+
+eval create view v as $q;
+
+eval $q;
+eval select * from v;
+eval with cte as ( $q ) select * from cte;
+
+eval explain $q;
+eval explain select * from v;
+eval explain with cte as ( $q ) select * from cte;
+
+eval prepare stmt from "$q";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+eval prepare stmt from "select * from v";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+eval prepare stmt from "with cte as ( $q ) select * from cte";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+show create view v;
+
+drop view v;
+
+eval prepare stmt from "create view v as $q";
+execute stmt;
+show create view v;
+select * from v;
+drop view v;
+execute stmt;
+show create view v;
+select * from v;
+deallocate prepare stmt;
+
+prepare stmt from "show create view v";
+execute stmt;
+execute stmt;
+deallocate prepare stmt;
+
+drop view v;
+
+let $q=
+select (values ((select * from t1 where a > 5
+ union
+ select * from t1 where a > 7))) as m from t2;
+
+eval create view v as $q;
+
+eval $q;
+eval select * from v;
+
+show create view v;
+
+drop view v;
+drop table t1,t2;
+
+--echo #
--echo # End of 10.4 tests
--echo #
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index 24958b5..62c7556 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -2487,6 +2487,7 @@ void st_select_lex::init_select()
curr_tvc_name= 0;
in_tvc= false;
versioned_tables= 0;
+ is_tvc_wrapper= false;
}
/*
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index 3545a4e..8f25426 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -1132,7 +1132,8 @@ class st_select_lex: public st_select_lex_node
st_select_lex.
*/
uint curr_tvc_name;
-
+ /* true <=> select has been created a TVC wrapper */
+ bool is_tvc_wrapper;
/*
Needed to correctly generate 'PRIMARY' or 'SIMPLE' for select_type column
of EXPLAIN
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index f83bf32..6f85853 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -27992,6 +27992,15 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
return;
}
+ if (is_tvc_wrapper &&
+ (thd->lex->sql_command == SQLCOM_CREATE_VIEW ||
+ (thd->lex->sql_command == SQLCOM_SHOW_CREATE &&
+ thd->lex->query_tables->view)))
+ {
+ first_inner_unit()->first_select()->print(thd, str, query_type);
+ return;
+ }
+
if ((query_type & QT_SHOW_SELECT_NUMBER) &&
thd->lex->all_selects_list &&
thd->lex->all_selects_list->link_next &&
diff --git a/sql/sql_tvc.cc b/sql/sql_tvc.cc
index d8e6770..71377d3 100644
--- a/sql/sql_tvc.cc
+++ b/sql/sql_tvc.cc
@@ -673,6 +673,7 @@ st_select_lex *wrap_tvc(THD *thd, st_select_lex *tvc_sl,
wrapper_sl->parent_lex= lex; /* Used in init_query. */
wrapper_sl->init_query();
wrapper_sl->init_select();
+ wrapper_sl->is_tvc_wrapper= true;
wrapper_sl->nest_level= tvc_sl->nest_level;
wrapper_sl->parsing_place= tvc_sl->parsing_place;
1
0
[Commits] 662f8e95: MDEV-30539 EXPLAIN EXTENDED: no message with query for DML statements
by IgorBabaev 27 Feb '23
by IgorBabaev 27 Feb '23
27 Feb '23
revision-id: 662f8e95612f7970edae819c221806d5a606d420 (mariadb-10.4.27-72-g662f8e95)
parent(s): bef20b5f36d21a2e7a03d283e158e66a64a16754
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-02-26 17:10:58 -0800
message:
MDEV-30539 EXPLAIN EXTENDED: no message with query for DML statements
Preliminary patch.
---
mysql-test/main/explain_non_select.result | 4 +
mysql-test/main/multi_update.result | 2 +-
mysql-test/main/multi_update.test | 2 +-
.../main/myisam_explain_non_select_all.result | 138 +++++++++
mysql-test/main/opt_trace.result | 2 +-
mysql-test/main/ps.result | 4 +
sql/sql_delete.cc | 2 +
sql/sql_explain.cc | 19 +-
sql/sql_explain.h | 2 +-
sql/sql_insert.cc | 6 +-
sql/sql_lex.cc | 2 +
sql/sql_lex.h | 6 +
sql/sql_parse.cc | 20 +-
sql/sql_select.cc | 322 ++++++++++++++++++---
sql/sql_update.cc | 10 +-
sql/table.h | 2 +
16 files changed, 485 insertions(+), 58 deletions(-)
diff --git a/mysql-test/main/explain_non_select.result b/mysql-test/main/explain_non_select.result
index 111b4c8..d60f10f 100644
--- a/mysql-test/main/explain_non_select.result
+++ b/mysql-test/main/explain_non_select.result
@@ -157,9 +157,13 @@ id select_type table partitions type possible_keys key key_len ref rows Extra
explain extended update t2 set b=3 where a in (3,4);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`b` = 3 where `test`.`t2`.`a` in (3,4)
explain extended delete from t2 where a in (3,4);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`a` in (3,4)
drop table t1,t2;
#
# Check the special case where partition pruning removed all partitions
diff --git a/mysql-test/main/multi_update.result b/mysql-test/main/multi_update.result
index d6cf9ba..222c592 100644
--- a/mysql-test/main/multi_update.result
+++ b/mysql-test/main/multi_update.result
@@ -1253,7 +1253,7 @@ EXPLAIN
DROP TABLES t1, t2;
# End of 10.3 tests
#
-# MDEV-28538: multi-table UPDATE/DELETE with possible exists-to-in
+# MDEV-30538: multi-table UPDATE/DELETE with possible exists-to-in
#
create table t1 (c1 int, c2 int, c3 int, index idx(c2));
insert into t1 values
diff --git a/mysql-test/main/multi_update.test b/mysql-test/main/multi_update.test
index 48e6250..329394e 100644
--- a/mysql-test/main/multi_update.test
+++ b/mysql-test/main/multi_update.test
@@ -1134,7 +1134,7 @@ DROP TABLES t1, t2;
--echo # End of 10.3 tests
--echo #
---echo # MDEV-28538: multi-table UPDATE/DELETE with possible exists-to-in
+--echo # MDEV-30538: multi-table UPDATE/DELETE with possible exists-to-in
--echo #
create table t1 (c1 int, c2 int, c3 int, index idx(c2));
diff --git a/mysql-test/main/myisam_explain_non_select_all.result b/mysql-test/main/myisam_explain_non_select_all.result
index 7f24cb4..75b9e6f 100644
--- a/mysql-test/main/myisam_explain_non_select_all.result
+++ b/mysql-test/main/myisam_explain_non_select_all.result
@@ -17,6 +17,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a < 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`a` = 10 where `test`.`t1`.`a` < 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -60,6 +62,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a < 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` < 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -103,6 +107,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 USING t1 WHERE a = 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` using `test`.`t1` where `test`.`t1`.`a` = 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -150,6 +156,8 @@ EXPLAIN EXTENDED UPDATE t1, t2 SET t1.a = 10 WHERE t1.a = 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t2` set `test`.`t1`.`a` = 10 where `test`.`t1`.`a` = 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -200,6 +208,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t11 ALL NULL NULL NULL NULL 3 100.00 Using where
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` `t11`,(/* select#2 */ select `test`.`t2`.`b` AS `b` from `test`.`t2`) `t12` set `test`.`t11`.`a` = 10 where `test`.`t11`.`a` = 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -248,6 +258,8 @@ EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE 1 IN (SELECT 1 FROM t2 WHERE
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`a` = 10 where <in_optimizer>(1,<exists>(/* select#2 */ select 1 from `test`.`t2` where `test`.`t2`.`b` < 3 and 1))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -300,6 +312,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`a` = 10 where <in_optimizer>(`test`.`t1`.`a`,<exists>(/* select#2 */ select `test`.`t2`.`b` from `test`.`t2` where `test`.`t1`.`a` < 3 and <cache>(`test`.`t1`.`a`) = `test`.`t2`.`b`))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -353,6 +366,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t2`,`test`.`t2` set `test`.`t1`.`a` = 10 where `test`.`t2`.`b` < 3
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -405,6 +420,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t11 ALL NULL NULL NULL NULL 3 100.00
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` `t11`,(/* select#2 */ select `test`.`t2`.`b` AS `b` from `test`.`t2`) `t12` set `test`.`t11`.`a` = `test`.`t11`.`a` + 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -457,6 +474,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00
1 PRIMARY t11 ALL NULL NULL NULL NULL 3 100.00
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` `t11`,(/* select#2 */ select 1 AS `1`) `t12` set `test`.`t11`.`a` = `test`.`t11`.`a` + 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -511,6 +530,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t11 ALL NULL NULL NULL NULL 3 100.00 Using where
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` `t11`,(/* select#2 */ select `test`.`t2`.`b` AS `b` from `test`.`t2`) `t12` set `test`.`t11`.`a` = 10 where `test`.`t11`.`a` > 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -555,6 +576,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a > 1 LIMIT 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` > 1 limit 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -598,6 +621,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+Warnings:
+Note 1003 delete from `test`.`t1` where 0
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -638,6 +663,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 USING t1 WHERE 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+Warnings:
+Note 1003 delete from `test`.`t1` using `test`.`t1` where 0
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -678,6 +705,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a = 3;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range a a 5 NULL 1 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` = 3
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 5
@@ -719,6 +748,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a < 3;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range a a 5 NULL 1 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` < 3
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 5
@@ -758,6 +789,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` > 0 order by `test`.`t1`.`a`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -797,6 +830,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 3 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`a` > 0 order by `test`.`t1`.`a`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -840,6 +875,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE (@a:= a) ORDER BY a LIMIT 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 index NULL PRIMARY 4 NULL 1 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where @a:=`test`.`t1`.`a` order by `test`.`t1`.`a` limit 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -884,6 +921,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 ORDER BY a ASC, b ASC LIMIT 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 100.00 Using filesort
+Warnings:
+Note 1003 delete from `test`.`t1` order by `test`.`t1`.`a`,`test`.`t1`.`b` limit 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 7
@@ -941,6 +980,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
1 SIMPLE t2 ref PRIMARY PRIMARY 4 test.t1.a1 1 100.00
1 SIMPLE t3 eq_ref PRIMARY PRIMARY 8 test.t2.b2,test.t1.b1 1 100.00
+Warnings:
+Note 1003 delete from `test`.`t1`,`test`.`t2`,`test`.`t3` using `test`.`t1`,`test`.`t2`,`test`.`t3` where `test`.`t2`.`a2` = `test`.`t1`.`a1` and `test`.`t3`.`a3` = `test`.`t2`.`b2` and `test`.`t3`.`b3` = `test`.`t1`.`b1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 13
@@ -993,6 +1034,8 @@ EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`a` = 10 where <in_optimizer>(`test`.`t1`.`a`,<exists>(/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where <cache>(`test`.`t1`.`a`) = `test`.`t2`.`a`))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1043,6 +1086,8 @@ EXPLAIN EXTENDED DELETE FROM t1 WHERE a1 IN (SELECT a2 FROM t2 WHERE a2 > 2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 5 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ delete from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a1`,<exists>(/* select#2 */ select `test`.`t2`.`a2` from `test`.`t2` where `test`.`t2`.`a2` > 2 and <cache>(`test`.`t1`.`a1`) = `test`.`t2`.`a2`))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1088,6 +1133,8 @@ EXPLAIN EXTENDED DELETE FROM t1 WHERE a1 IN (SELECT a2 FROM t2 WHERE a2 > 2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 5 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ delete from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a1`,<exists>(/* select#2 */ select `test`.`t2`.`a2` from `test`.`t2` where `test`.`t2`.`a2` > 2 and <cache>(`test`.`t1`.`a1`) = `test`.`t2`.`a2`))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1132,6 +1179,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET i = 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`i` = 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -1175,6 +1224,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL 5 NULL Deleting all rows
+Warnings:
+Note 1003 delete from `test`.`t1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -1221,6 +1272,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 15 NULL 5 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1267,6 +1320,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED INSERT INTO t2 SELECT * FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 insert into `test`.`t2` select `test`.`t1`.`i` AS `i` from `test`.`t1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1311,6 +1366,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED REPLACE INTO t2 SELECT * FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 replaceinto `test`.`t2` select `test`.`t1`.`i` AS `i` from `test`.`t1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1351,6 +1408,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED INSERT INTO t1 SET i = 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 INSERT t1 ALL NULL NULL NULL NULL NULL 100.00 NULL
+Warnings:
+Note 1003 insert into `test`.`t1`(i) values (10)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -1374,6 +1433,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED REPLACE INTO t1 SET i = 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 INSERT t1 ALL NULL NULL NULL NULL NULL 100.00 NULL
+Warnings:
+Note 1003 replaceinto `test`.`t1`(i) values (10)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -1402,6 +1463,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 5 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`i` > 10 and `test`.`t1`.`i` <= 18 order by `test`.`t1`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1447,6 +1510,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`i` > 10 and `test`.`t1`.`i` <= 18 order by `test`.`t1`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1500,6 +1565,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1554,6 +1621,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 15 NULL 5 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1603,6 +1672,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1657,6 +1728,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -1712,6 +1785,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE key1 < 13 or key2 < 14 ORDER BY key1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index_merge key1,key2 key1,key2 5,5 NULL 7 100.00 Using sort_union(key1,key2); Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`key1` < 13 or `test`.`t2`.`key2` < 14 order by `test`.`t2`.`key1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -1765,6 +1840,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 WHERE i > 10 AND i <= 18 ORDER BY i DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 range PRIMARY PRIMARY 4 NULL 5 100.00 Using where
+Warnings:
+Note 1003 delete from `test`.`t2` where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1812,6 +1889,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 ORDER BY a, b DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using filesort
+Warnings:
+Note 1003 delete from `test`.`t2` order by `test`.`t2`.`a`,`test`.`t2`.`b` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -1866,6 +1945,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t2 ORDER BY a DESC, b DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 6 NULL 5 100.00
+Warnings:
+Note 1003 delete from `test`.`t2` order by `test`.`t2`.`a` desc,`test`.`t2`.`b` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -1915,6 +1996,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET a = 10 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 range PRIMARY PRIMARY 4 NULL 5 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`a` = 10 where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -1963,6 +2046,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET a = 10 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`a` = 10 where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -2017,6 +2102,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET d = 10 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`d` = 10 where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -2072,6 +2159,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET d = 10 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 15 NULL 5 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`d` = 10 where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -2122,6 +2211,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET d = 10 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`d` = 10 where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -2176,6 +2267,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET d = 10 WHERE b = 10 ORDER BY a, c LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`d` = 10 where `test`.`t2`.`b` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`c` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 8
@@ -2231,6 +2324,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET i = 123 WHERE key1 < 13 or key2 < 14 ORDER BY key1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index_merge key1,key2 key1,key2 5,5 NULL 7 100.00 Using sort_union(key1,key2); Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`i` = 123 where `test`.`t2`.`key1` < 13 or `test`.`t2`.`key2` < 14 order by `test`.`t2`.`key1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2284,6 +2379,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET a = 10 WHERE i > 10 AND i <= 18 ORDER BY i DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 range PRIMARY PRIMARY 4 NULL 5 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`a` = 10 where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -2332,6 +2429,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET c = 10 ORDER BY a, b DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using filesort
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`c` = 10 order by `test`.`t2`.`a`,`test`.`t2`.`b` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2387,6 +2486,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET c = 10 ORDER BY a DESC, b DESC LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 index NULL a 6 NULL 5 100.00 Using buffer
+Warnings:
+Note 1003 update `test`.`t2` set `test`.`t2`.`c` = 10 order by `test`.`t2`.`a` desc,`test`.`t2`.`b` desc limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2439,6 +2540,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET c2 = 0 WHERE c1_idx = 'y' ORDER BY pk DESC LIMIT 2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range c1_idx c1_idx 2 NULL 2 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`c2` = 0 where `test`.`t1`.`c1_idx` = 'y' order by `test`.`t1`.`pk` desc limit 2
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2485,6 +2588,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE c1_idx = 'y' ORDER BY pk DESC LIMIT 2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range c1_idx c1_idx 2 NULL 2 100.00 Using where; Using filesort
+Warnings:
+Note 1003 delete from `test`.`t1` where `test`.`t1`.`c1_idx` = 'y' order by `test`.`t1`.`pk` desc limit 2
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2534,6 +2639,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a=a+10 WHERE a > 34;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 2 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`a` = `test`.`t1`.`a` + 10 where `test`.`t1`.`a` > 34
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -2581,6 +2688,8 @@ EXPLAIN EXTENDED UPDATE t1 LEFT JOIN t2 ON t1.c1 = t2.c1 SET t2.c2 = 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 system NULL NULL NULL NULL 0 0.00 Const row not found
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t2` set NULL = 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 7
@@ -2624,6 +2733,8 @@ EXPLAIN EXTENDED UPDATE t1 LEFT JOIN t2 ON t1.c1 = t2.c1 SET t2.c2 = 10 W
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 system NULL NULL NULL NULL 0 0.00 Const row not found
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t2` set NULL = 10 where `test`.`t1`.`c3` = 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 7
@@ -2676,6 +2787,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
2 DEPENDENT SUBQUERY t2 ALL IDX NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.f1' of SELECT #2 was resolved in SELECT #1
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`f2` = (/* select#2 */ select max(`test`.`t2`.`f4`) from `test`.`t2` where `test`.`t2`.`f3` = `test`.`t1`.`f1`)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 7
@@ -2747,6 +2859,8 @@ EXPLAIN EXTENDED UPDATE v1 SET a = 1 WHERE a > 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t11 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t12 ALL NULL NULL NULL NULL 2 100.00
+Warnings:
+Note 1003 update `test`.`t1` `t11`,`test`.`t1` `t12` set `test`.`t11`.`a` = 1 where `test`.`t11`.`a` > 0
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -2792,6 +2906,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
1 SIMPLE t11 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t12 ALL NULL NULL NULL NULL 2 100.00
+Warnings:
+Note 1003 update `test`.`t1`,`test`.`t1` `t11`,`test`.`t1` `t12` set `test`.`t11`.`a` = 1 where `test`.`t11`.`a` = `test`.`t1`.`a`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -2841,6 +2957,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM v1 WHERE a < 4;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 3 100.00 Using where
+Warnings:
+Note 1003 /* select#1 */ delete from `test`.`t1` where `test`.`t1`.`a` < 4
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -2892,6 +3010,8 @@ EXPLAIN EXTENDED DELETE v1 FROM t2, v1 WHERE t2.x = v1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using where
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t2.x 1 100.00
+Warnings:
+Note 1003 delete from `test`.`t1` using `test`.`t2`,`test`.`t1` where `test`.`t1`.`a` = `test`.`t2`.`x`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2943,6 +3063,8 @@ EXPLAIN EXTENDED DELETE v1 FROM t2, v1 WHERE t2.x = v1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using where
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t2.x 1 100.00
+Warnings:
+Note 1003 delete from `test`.`t1` using `test`.`t2`,`test`.`t1` where `test`.`t1`.`a` = `test`.`t2`.`x`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 6
@@ -2987,6 +3109,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED INSERT INTO v1 VALUES (10);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 INSERT t1 ALL NULL NULL NULL NULL NULL 100.00 NULL
+Warnings:
+Note 1003 insert into `test`.`t1`(x) values (10)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 2
@@ -3027,6 +3151,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED INSERT INTO v1 SELECT * FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 system NULL NULL NULL NULL 0 0.00 Const row not found
+Warnings:
+Note 1003 insert into `test`.`t2`(x) /* select#1 */ select NULL AS `a` from `test`.`t1`
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -3084,6 +3210,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY <derived3> index_subquery key0 key0 5 func 2 100.00
3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1` set `test`.`t1`.`a` = 10 where <in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in (temporary) on key0)))
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -3140,6 +3268,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00
2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 3 100.00
3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1`,`test`.`t2`,(/* select#3 */ select `test`.`t2`.`b` AS `b` from `test`.`t2` order by `test`.`t2`.`b` limit 2,2) `x` set `test`.`t1`.`a` = 10 where 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -3198,6 +3328,8 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
3 MATERIALIZED <derived4> ALL NULL NULL NULL NULL 3 100.00
4 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
2 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t1`,(/* select#2 */ select `test`.`t2`.`b` AS `b` from `test`.`t2`) `y`,(/* select#4 */ select `test`.`t2`.`b` AS `b` from `test`.`t2` order by `test`.`t2`.`b` limit 2,2) `x` set `test`.`t1`.`a` = 10 where 1
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -3248,6 +3380,8 @@ JOIN t1 AS a12 ON a12.c1 = a11.c1
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 0 100.00
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t3` set `test`.`t3`.`c3` = (/* select#2 */ select count(NULL) from `test`.`t1` `a11` straight_join `test`.`t2` `a21` join `test`.`t1` `a12` where 0)
DROP TABLE t1, t2, t3;
#73
CREATE TABLE t1 (id INT);
@@ -3276,6 +3410,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a=a+1 WHERE a>10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 1 100.00 Using where; Using buffer
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`a` = `test`.`t1`.`a` + 1 where `test`.`t1`.`a` > 10
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
@@ -3315,6 +3451,8 @@ FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a=a+1 WHERE a>10 ORDER BY a+20;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 1 100.00 Using where; Using filesort
+Warnings:
+Note 1003 update `test`.`t1` set `test`.`t1`.`a` = `test`.`t1`.`a` + 1 where `test`.`t1`.`a` > 10 order by `test`.`t1`.`a` + 20
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 3
diff --git a/mysql-test/main/opt_trace.result b/mysql-test/main/opt_trace.result
index 2eef0da..862183c 100644
--- a/mysql-test/main/opt_trace.result
+++ b/mysql-test/main/opt_trace.result
@@ -3771,7 +3771,7 @@ explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3 {
"select_id": 1,
"steps": [
{
- "expanded_query": "select NULL AS `NULL` from t0 join t1 where t0.a = t1.a and t1.a < 3"
+ "expanded_query": "delete from t0,t1 using t0,t1 where t0.a = t1.a and t1.a < 3"
}
]
}
diff --git a/mysql-test/main/ps.result b/mysql-test/main/ps.result
index 673b18c..408b1ec 100644
--- a/mysql-test/main/ps.result
+++ b/mysql-test/main/ps.result
@@ -5566,11 +5566,15 @@ EXPLAIN EXTENDED UPDATE t3 SET c3 = ( SELECT COUNT(d1.c1) FROM ( SELECT a11.c1 F
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 0 100.00
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t3` set `test`.`t3`.`c3` = (/* select#2 */ select count(NULL) from `test`.`t1` `a11` straight_join `test`.`t2` `a21` join `test`.`t1` `a12` where 0)
PREPARE stmt FROM "EXPLAIN EXTENDED UPDATE t3 SET c3 = ( SELECT COUNT(d1.c1) FROM ( SELECT a11.c1 FROM t1 AS a11 STRAIGHT_JOIN t2 AS a21 ON a21.c2 = a11.c1 JOIN t1 AS a12 ON a12.c1 = a11.c1 ) d1 )";
EXECUTE stmt;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 0 100.00
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+Warnings:
+Note 1003 /* select#1 */ update `test`.`t3` set `test`.`t3`.`c3` = (/* select#2 */ select count(NULL) from `test`.`t1` `a11` straight_join `test`.`t2` `a21` join `test`.`t1` `a12` where 0)
DEALLOCATE PREPARE stmt;
DROP TABLE t1, t2, t3;
#
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 1b3fa30..776dfff 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -1084,6 +1084,8 @@ int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list,
DBUG_RETURN(TRUE);
select_lex->fix_prepare_information(thd, conds, &fake_conds);
+ if (!thd->lex->upd_del_where)
+ thd->lex->upd_del_where= *conds;
DBUG_RETURN(FALSE);
}
diff --git a/sql/sql_explain.cc b/sql/sql_explain.cc
index 70e3009..6b76db8 100644
--- a/sql/sql_explain.cc
+++ b/sql/sql_explain.cc
@@ -161,7 +161,7 @@ void Explain_query::query_plan_ready()
Send EXPLAIN output to the client.
*/
-int Explain_query::send_explain(THD *thd)
+int Explain_query::send_explain(THD *thd, bool extended)
{
select_result *result;
LEX *lex= thd->lex;
@@ -174,8 +174,22 @@ int Explain_query::send_explain(THD *thd)
if (thd->lex->explain_json)
print_explain_json(result, thd->lex->analyze_stmt);
else
+ {
res= print_explain(result, lex->describe, thd->lex->analyze_stmt);
-
+ if (extended)
+ {
+ char buff[1024];
+ String str(buff,(uint32) sizeof(buff), system_charset_info);
+ str.length(0);
+ /*
+ The warnings system requires input in utf8, @see
+ mysqld_show_warnings().
+ */
+ lex->unit.print(&str, QT_EXPLAIN_EXTENDED);
+ push_warning(thd, Sql_condition::WARN_LEVEL_NOTE,
+ ER_YES, str.c_ptr_safe());
+ }
+ }
if (res)
result->abort_result_set();
else
@@ -185,6 +199,7 @@ int Explain_query::send_explain(THD *thd)
}
+
/*
The main entry point to print EXPLAIN of the entire query
*/
diff --git a/sql/sql_explain.h b/sql/sql_explain.h
index 6575511..88ac5a1 100644
--- a/sql/sql_explain.h
+++ b/sql/sql_explain.h
@@ -474,7 +474,7 @@ class Explain_query : public Sql_alloc
bool is_analyze);
/* Send tabular EXPLAIN to the client */
- int send_explain(THD *thd);
+ int send_explain(THD *thd, bool extended);
/* Return tabular EXPLAIN output as a text string */
bool print_explain_str(THD *thd, String *out_str, bool is_analyze);
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index f31c9ea..9a76061 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -827,7 +827,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
save_insert_query_plan(thd, table_list);
if (thd->lex->describe)
{
- retval= thd->lex->explain->send_explain(thd);
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ retval= thd->lex->explain->send_explain(thd, extended);
goto abort;
}
@@ -1250,7 +1251,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
goto abort;
if (thd->lex->analyze_stmt)
{
- retval= thd->lex->explain->send_explain(thd);
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ retval= thd->lex->explain->send_explain(thd, extended);
goto abort;
}
DBUG_PRINT("info", ("touched: %llu copied: %llu updated: %llu deleted: %llu",
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index ac570be..3876fc8 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -782,6 +782,8 @@ void LEX::start(THD *thd_arg)
frame_bottom_bound= NULL;
win_spec= NULL;
+ upd_del_where= NULL;
+
vers_conditions.empty();
period_conditions.empty();
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index aae994c..aa33efd 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -1412,6 +1412,10 @@ class st_select_lex: public st_select_lex_node
}
bool setup_ref_array(THD *thd, uint order_group_num);
void print(THD *thd, String *str, enum_query_type query_type);
+ void print_item_list(THD *thd, String *str, enum_query_type query_type);
+ void print_set_clause(THD *thd, String *str, enum_query_type query_type);
+ void print_on_duplicate_key_clause(THD *thd, String *str,
+ enum_query_type query_type);
static void print_order(String *str,
ORDER *order,
enum_query_type query_type);
@@ -3494,6 +3498,8 @@ struct LEX: public Query_tables_list
Window_frame_bound *frame_bottom_bound;
Window_spec *win_spec;
+ Item *upd_del_where;
+
/* System Versioning */
vers_select_conds_t vers_conditions;
vers_select_conds_t period_conditions;
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index bb0b3eb..2820f2c 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -4744,7 +4744,10 @@ mysql_execute_command(THD *thd)
}
if (!res && (explain || lex->analyze_stmt))
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
/* revert changes for SP */
MYSQL_INSERT_SELECT_DONE(res, (ulong) thd->get_row_count_func());
@@ -4813,7 +4816,10 @@ mysql_execute_command(THD *thd)
if (thd->lex->analyze_stmt || thd->lex->describe)
{
if (!res)
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
}
delete sel_result;
@@ -4875,7 +4881,10 @@ mysql_execute_command(THD *thd)
else
{
if (lex->describe || lex->analyze_stmt)
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
}
multi_delete_error:
delete result;
@@ -6463,7 +6472,10 @@ static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
thd->protocol= save_protocol;
}
if (!res)
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
}
}
}
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index eb54484..8aea6f5 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -303,6 +303,9 @@ static Item **get_sargable_cond(JOIN *join, TABLE *table);
bool is_eq_cond_injected_for_split_opt(Item_func_eq *eq_item);
+void print_list_item(String *str, List_item *list,
+ enum_query_type query_type);
+
#ifndef DBUG_OFF
/*
@@ -27833,6 +27836,162 @@ void TABLE_LIST::print(THD *thd, table_map eliminated_tables, String *str,
}
}
+enum explainable_cmd_type
+{
+ SELECT_CMD, INSERT_CMD, REPLACE_CMD, UPDATE_CMD, DELETE_CMD, NO_CMD
+};
+
+static
+const char * const explainable_cmd_name []=
+{
+ "select ",
+ "insert ",
+ "replace ",
+ "update ",
+ "delete ",
+};
+
+static
+char const *get_explainable_cmd_name(enum explainable_cmd_type cmd)
+{
+ return explainable_cmd_name[cmd];
+}
+
+static
+enum explainable_cmd_type get_explainable_cmd_type(THD *thd)
+{
+ switch (thd->lex->sql_command) {
+ case SQLCOM_SELECT:
+ return SELECT_CMD;
+ case SQLCOM_INSERT:
+ case SQLCOM_INSERT_SELECT:
+ return INSERT_CMD;
+ case SQLCOM_REPLACE:
+ case SQLCOM_REPLACE_SELECT:
+ return REPLACE_CMD;
+ case SQLCOM_UPDATE:
+ case SQLCOM_UPDATE_MULTI:
+ return UPDATE_CMD;
+ case SQLCOM_DELETE:
+ case SQLCOM_DELETE_MULTI:
+ return DELETE_CMD;
+ default:
+ return SELECT_CMD;
+ }
+}
+
+
+void TABLE_LIST::print_leaf_tables(THD *thd, String *str,
+ enum_query_type query_type)
+{
+ if (merge_underlying_list)
+ {
+ for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
+ tbl->print_leaf_tables(thd, str, query_type);
+ }
+ else
+ print(thd, 0, str, query_type);
+}
+
+
+void st_select_lex::print_item_list(THD *thd, String *str,
+ enum_query_type query_type)
+{
+ bool first= 1;
+ /*
+ outer_select() can not be used here because it is for name resolution
+ and will return NULL at any end of name resolution chain (view/derived)
+ */
+ bool top_level= (get_master()->get_master() == 0);
+ List_iterator_fast<Item> it(item_list);
+ Item *item;
+ while ((item= it++))
+ {
+ if (first)
+ first= 0;
+ else
+ str->append(',');
+
+ if ((is_subquery_function() && item->is_autogenerated_name) ||
+ !item->name.str)
+ {
+ /*
+ Do not print auto-generated aliases in subqueries. It has no purpose
+ in a view definition or other contexts where the query is printed.
+ */
+ item->print(str, query_type);
+ }
+ else
+ {
+ /*
+ Do not print illegal names (if it is not top level SELECT).
+ Top level view checked (and correct name are assigned),
+ other cases of top level SELECT are not important, because
+ it is not "table field".
+ */
+ if (top_level ||
+ !item->is_autogenerated_name ||
+ !check_column_name(item->name.str))
+ item->print_item_w_name(str, query_type);
+ else
+ item->print(str, query_type);
+ }
+ }
+}
+
+
+void st_select_lex::print_set_clause(THD *thd, String *str,
+ enum_query_type query_type)
+{
+ bool first= 1;
+ /*
+ outer_select() can not be used here because it is for name resolution
+ and will return NULL at any end of name resolution chain (view/derived)
+ */
+ List_iterator_fast<Item> it(item_list);
+ List_iterator_fast<Item> vt(thd->lex->value_list);
+ Item *item;
+ Item *val;
+ while ((item= it++, val= vt++ ))
+ {
+ if (first)
+ {
+ str->append(STRING_WITH_LEN(" set "));
+ first= 0;
+ }
+ else
+ str->append(',');
+
+ item->print(str, query_type);
+ str->append(STRING_WITH_LEN(" = "));
+ val->print(str, query_type);
+ }
+}
+
+
+void st_select_lex::print_on_duplicate_key_clause(THD *thd, String *str,
+ enum_query_type query_type)
+{
+ bool first= 1;
+ List_iterator_fast<Item> it(thd->lex->update_list);
+ List_iterator_fast<Item> vt(thd->lex->value_list);
+ Item *item;
+ Item *val;
+ while ((item= it++, val= vt++ ))
+ {
+ if (first)
+ {
+ str->append(STRING_WITH_LEN(" on duplicate key update "));
+ first= 0;
+ }
+ else
+ str->append(',');
+
+ item->print(str, query_type);
+ str->append(STRING_WITH_LEN(" = "));
+ val->print(str, query_type);
+ }
+}
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
{
@@ -27844,6 +28003,61 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
return;
}
+ bool top_level= (get_master()->get_master() == 0);
+ enum explainable_cmd_type sel_type= SELECT_CMD;
+ if (top_level)
+ sel_type= get_explainable_cmd_type(thd);
+
+ if (sel_type == INSERT_CMD || sel_type == REPLACE_CMD)
+ {
+ str->append(STRING_WITH_LEN(get_explainable_cmd_name(sel_type)));
+ str->append(STRING_WITH_LEN("into "));
+ TABLE_LIST *tbl= thd->lex->query_tables;
+ while (tbl->merge_underlying_list)
+ tbl= tbl->merge_underlying_list;
+ tbl->print(thd, 0, str, query_type);
+ if (thd->lex->field_list.elements)
+ {
+ str->append ('(');
+ List_iterator_fast<Item> it(thd->lex->field_list);
+ Item *item;
+ bool first= true;
+ while ((item= it++))
+ {
+ if (first)
+ first= false;
+ else
+ str->append(',');
+ str->append(item->name);
+ }
+ str->append(')');
+ }
+
+ str->append(' ');
+
+ if (thd->lex->sql_command == SQLCOM_INSERT ||
+ thd->lex->sql_command == SQLCOM_REPLACE)
+ {
+ str->append(STRING_WITH_LEN("values "));
+ bool is_first_elem= true;
+ List_iterator_fast<List_item> li(thd->lex->many_values);
+ List_item *list;
+
+ while ((list= li++))
+ {
+ if (is_first_elem)
+ is_first_elem= false;
+ else
+ str->append(',');
+
+ print_list_item(str, list, query_type);
+ }
+ if (thd->lex->update_list.elements)
+ print_on_duplicate_key_clause(thd, str, query_type);
+ return;
+ }
+ }
+
if ((query_type & QT_SHOW_SELECT_NUMBER) &&
thd->lex->all_selects_list &&
thd->lex->all_selects_list->link_next &&
@@ -27867,7 +28081,10 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
str->append(" */ ");
}
- str->append(STRING_WITH_LEN("select "));
+ if (sel_type == SELECT_CMD ||
+ sel_type == INSERT_CMD ||
+ sel_type == REPLACE_CMD)
+ str->append(STRING_WITH_LEN("select "));
if (join && join->cleaned)
{
@@ -27913,56 +28130,59 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
}
//Item List
- bool first= 1;
+ if (sel_type == SELECT_CMD ||
+ sel_type == INSERT_CMD ||
+ sel_type == REPLACE_CMD)
+ print_item_list(thd, str, query_type);
/*
- outer_select() can not be used here because it is for name resolution
- and will return NULL at any end of name resolution chain (view/derived)
+ from clause
+ TODO: support USING/FORCE/IGNORE index
*/
- bool top_level= (get_master()->get_master() == 0);
- List_iterator_fast<Item> it(item_list);
- Item *item;
- while ((item= it++))
+ if (table_list.elements)
{
- if (first)
- first= 0;
- else
- str->append(',');
-
- if ((is_subquery_function() && item->is_autogenerated_name) ||
- !item->name.str)
+ if (sel_type == SELECT_CMD ||
+ sel_type == INSERT_CMD ||
+ sel_type == REPLACE_CMD)
{
- /*
- Do not print auto-generated aliases in subqueries. It has no purpose
- in a view definition or other contexts where the query is printed.
- */
- item->print(str, query_type);
+ str->append(STRING_WITH_LEN(" from "));
+ /* go through join tree */
+ print_join(thd, join? join->eliminated_tables: 0, str, &top_join_list,
+ query_type);
}
- else
+ if (sel_type == UPDATE_CMD || sel_type == DELETE_CMD)
+ str->append(STRING_WITH_LEN(get_explainable_cmd_name(sel_type)));
+ if (sel_type == DELETE_CMD)
{
- /*
- Do not print illegal names (if it is not top level SELECT).
- Top level view checked (and correct name are assigned),
- other cases of top level SELECT are not important, because
- it is not "table field".
- */
- if (top_level ||
- !item->is_autogenerated_name ||
- !check_column_name(item->name.str))
- item->print_item_w_name(str, query_type);
- else
- item->print(str, query_type);
+ str->append(STRING_WITH_LEN(" from "));
+ bool first= true;
+ for (TABLE_LIST *target_tbl= thd->lex->auxiliary_table_list.first;
+ target_tbl;
+ target_tbl= target_tbl->next_local)
+ {
+ if (first)
+ first= false;
+ else
+ str->append(',');
+ target_tbl->correspondent_table->print_leaf_tables(thd, str,
+ query_type);
+ }
+ if (!first)
+ str->append(STRING_WITH_LEN(" using "));
+ }
+ if (sel_type == UPDATE_CMD || sel_type == DELETE_CMD)
+ {
+ bool first= true;
+ List_iterator_fast<TABLE_LIST> li(leaf_tables);
+ TABLE_LIST *tbl;
+ while ((tbl= li++))
+ {
+ if (first)
+ first= false;
+ else
+ str->append(',');
+ tbl->print(thd, 0, str, query_type);
+ }
}
- }
-
- /*
- from clause
- TODO: support USING/FORCE/IGNORE index
- */
- if (table_list.elements)
- {
- str->append(STRING_WITH_LEN(" from "));
- /* go through join tree */
- print_join(thd, join? join->eliminated_tables: 0, str, &top_join_list, query_type);
}
else if (where)
{
@@ -27973,10 +28193,15 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
str->append(STRING_WITH_LEN(" from DUAL "));
}
+ if (sel_type == UPDATE_CMD)
+ print_set_clause(thd, str, query_type);
+
// Where
Item *cur_where= where;
if (join)
cur_where= join->conds;
+ else if (sel_type == UPDATE_CMD || sel_type == DELETE_CMD)
+ cur_where= thd->lex->upd_del_where;
if (cur_where || cond_value != Item::COND_UNDEF)
{
str->append(STRING_WITH_LEN(" where "));
@@ -28033,6 +28258,15 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
else if (lock_type == TL_WRITE)
str->append(" for update");
+ if ((sel_type == INSERT_CMD || sel_type == REPLACE_CMD) &&
+ thd->lex->update_list.elements)
+ print_on_duplicate_key_clause(thd, str, query_type);
+
+ // returning clause
+ if (sel_type == DELETE_CMD && !item_list.elements)
+ {
+ print_item_list(thd, str, query_type);
+ }
// PROCEDURE unsupported here
}
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index a298071..834fa61 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -1371,7 +1371,8 @@ int mysql_update(THD *thd,
goto err;
emit_explain_and_leave:
- int err2= thd->lex->explain->send_explain(thd);
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ int err2= thd->lex->explain->send_explain(thd, extended);
delete select;
free_underlaid_joins(thd, select_lex);
@@ -1445,6 +1446,8 @@ bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list,
select_lex->fix_prepare_information(thd, conds, &fake_conds);
+ if (!thd->lex->upd_del_where)
+ thd->lex->upd_del_where= *conds;
DBUG_RETURN(FALSE);
}
@@ -1974,7 +1977,10 @@ bool mysql_multi_update(THD *thd, TABLE_LIST *table_list, List<Item> *fields,
else
{
if (thd->lex->describe || thd->lex->analyze_stmt)
- res= thd->lex->explain->send_explain(thd);
+ {
+ bool extended= thd->lex->describe & DESCRIBE_EXTENDED;
+ res= thd->lex->explain->send_explain(thd, extended);
+ }
}
thd->abort_on_warning= 0;
DBUG_RETURN(res);
diff --git a/sql/table.h b/sql/table.h
index 799675a..34c941c 100644
--- a/sql/table.h
+++ b/sql/table.h
@@ -2709,6 +2709,8 @@ struct TABLE_LIST
}
void print(THD *thd, table_map eliminated_tables, String *str,
enum_query_type query_type);
+ void print_leaf_tables(THD *thd, String *str,
+ enum_query_type query_type);
bool check_single_table(TABLE_LIST **table, table_map map,
TABLE_LIST *view);
bool set_insert_values(MEM_ROOT *mem_root);
1
0
revision-id: b673b6ae245de404fb1b49b90ffc58823efb3f14 (mariadb-10.11.1-188-gb673b6a)
parent(s): b3480cd20440a6944871bd7743584cb10521898b
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-02-15 06:52:12 -0800
message:
An attempt to rebase against bb-11.0
---
mysql-test/include/update_use_source.inc | 155 +-----------
mysql-test/include/update_use_source_cases.inc | 130 +---------
mysql-test/main/delete.result | 6 +-
mysql-test/main/delete_use_source_engines.result | 281 ++++++++-------------
mysql-test/main/derived.result | 26 +-
mysql-test/main/derived_cond_pushdown.result | 27 +-
mysql-test/main/log_state.result | 2 +-
mysql-test/main/multi_update.result | 9 +-
.../main/myisam_explain_non_select_all.result | 24 +-
mysql-test/main/opt_trace.result | 4 +-
mysql-test/main/subselect.result | 2 +-
mysql-test/main/subselect.test | 2 +-
mysql-test/main/subselect_no_exists_to_in.result | 2 +-
mysql-test/main/subselect_no_mat.result | 2 +-
mysql-test/main/subselect_no_opts.result | 2 +-
mysql-test/main/subselect_no_scache.result | 2 +-
mysql-test/main/subselect_no_semijoin.result | 2 +-
mysql-test/main/update_use_source.test | 55 +---
18 files changed, 154 insertions(+), 579 deletions(-)
diff --git a/mysql-test/include/update_use_source.inc b/mysql-test/include/update_use_source.inc
index 196f141..03bf168 100644
--- a/mysql-test/include/update_use_source.inc
+++ b/mysql-test/include/update_use_source.inc
@@ -28,162 +28,11 @@ select * from t1;
create table tmp as select * from t1;
---echo # Test without any index
---source include/update_use_source_cases.inc
+# --echo # Test without any index
+# --source include/update_use_source_cases.inc
--echo # Test with an index on updated columns
create index t1_c2 on t1 (c2,c1);
analyze table t1;
--source include/update_use_source_cases.inc
---echo # Test with an index on updated columns
-create index t1_c3 on t1 (c3);
-analyze table t1;
---source include/update_use_source_cases.inc
-
---echo # Test with a primary key on updated columns
-drop index t1_c3 on t1;
-alter table t1 add primary key (c3);
-analyze table t1;
---source include/update_use_source_cases.inc
-
---echo # Update with error "Subquery returns more than 1 row"
---error ER_SUBQUERY_NO_1_ROW
-update t1 set c2=(select c2 from t1);
---sorted_result
-select c1,c2,c3 from t1;
-
---echo # Update with error "Subquery returns more than 1 row"
---echo # and order by
---error ER_SUBQUERY_NO_1_ROW
-update t1 set c2=(select c2 from t1) order by c3;
---sorted_result
-select c1,c2,c3 from t1;
-
--- echo # Duplicate value on update a primary key
---error ER_DUP_ENTRY
-
-update t1 set c3=0
- where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
--- echo # Duplicate value on update a primary key with ignore
---enable_info ONCE
-update ignore t1 set c3=0
- where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
--- echo # Duplicate value on update a primary key and limit
---error ER_DUP_ENTRY
-update t1 set c3=0
- where exists (select 'X' from t1 a where a.c2 = t1.c2)
- and c2 >= 3 limit 2;
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
--- echo # Duplicate value on update a primary key with ignore
--- echo # and limit
---enable_info ONCE
-update ignore t1 set c3=0
- where exists (select 'X' from t1 a where a.c2 = t1.c2)
- and c2 >= 3 limit 2;
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo # Update no rows found
---enable_info ONCE
-update t1 set c1=10
- where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1 + 10);
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo # Update no rows changed
-drop trigger trg_t1;
---enable_info ONCE
-update t1 set c1=c1
- where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1);
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Check call of after trigger
---echo #
-
-delimiter /;
-create or replace trigger trg_t2 after update on t1 for each row
-begin
- declare msg varchar(100);
- if (new.c3 = 5) then
- set msg=concat('in after update trigger on ',new.c3);
- SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
- end if;
-end;
-/
-delimiter ;/
---error 1644
-
-update t1 set c1=2
- where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1);
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Check update with order by and after trigger
---echo #
-
---error 1644
-update t1 set c1=2
- where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1)
- order by t1.c2, t1.c1;
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
-drop view v1;
-
---echo #
---echo # Check update on view with check option
---echo #
-
-create view v1 as select * from t1 where c2=2 with check option;
-
--- error 1369
-update v1 set c2=3 where c1=1;
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
--- error 1369
-update v1 set c2=(select max(c3) from v1) where c1=1;
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
-update v1 set c2=(select min(va.c3) from v1 va), c1=0 where c1=1;
---sorted_result
-select c1,c2,c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
-drop table tmp;
-drop view v1;
-drop table t1;
diff --git a/mysql-test/include/update_use_source_cases.inc b/mysql-test/include/update_use_source_cases.inc
index 55b45a3..6e8e5c0 100644
--- a/mysql-test/include/update_use_source_cases.inc
+++ b/mysql-test/include/update_use_source_cases.inc
@@ -1,31 +1,3 @@
---echo #
---echo # Update with value from subquery on the same table
---echo #
-
-let $q=update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
-eval explain $q;
---enable_info ONCE
-eval $q;
-select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Update with EXISTS subquery over the updated table
---echo # in WHERE + possibly sargable condition
---echo #
-
-let $q=update t1 set c1=10
- where c1 <2
- and exists (select 'X' from t1 a where a.c1 = t1.c1);
-eval explain $q;
---enable_info ONCE
-eval $q;
-select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-truncate table t1;
-insert into t1 select * from tmp;
--echo #
--echo # Update with EXISTS subquery over the updated table
@@ -38,7 +10,7 @@ eval explain $q;
--enable_info ONCE
eval $q;
select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
truncate table t1;
insert into t1 select * from tmp;
@@ -57,103 +29,3 @@ select concat(old_c1,'->',c1),c3,
truncate table t1;
insert into t1 select * from tmp;
---echo #
---echo # Update with a reference to view in subquery
---echo #
-
-let $q=update t1 set c1=c1 +(select max(a.c2) from v1 a
- where a.c1 = t1.c1);
-eval explain $q;
---enable_info ONCE
-eval $q;
-select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Update view
---echo #
-
-let $q=update v1 set c1=c1 + (select max(a.c2) from t1 a
- where a.c1 = v1.c1) +10 where c3 > 3;
-eval explain $q;
---enable_info ONCE
-eval $q;
-select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Update view with reference to the same view in subquery
---echo #
-
-let $q=update v1 set c1=c1 + 1
- where c1 <2
- and exists (select 'X' from v1 a where a.c1 = v1.c1);
-eval explain $q;
---enable_info ONCE
-eval $q;
-select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Update view with EXISTS and reference to the same view in subquery
---echo #
-
-let $q=update v1
- set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
- where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
-eval explain $q;
---enable_info ONCE
-eval $q;
-select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Update with IN predicand over the updated table in WHERE
---echo #
-
-let $q=update t1 set c3=c3+10
- where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
-eval explain $q;
---enable_info ONCE
-eval $q;
---sorted_result
-select c3 from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Update with a limit
---echo #
-
-let $q=update t1
- set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
-eval explain $q;
---enable_info ONCE
-eval $q;
-select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-truncate table t1;
-insert into t1 select * from tmp;
-
---echo #
---echo # Update with a limit and an order by
---echo #
-
-let $q=update t1
- set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
- order by c3 desc limit 2;
-#eval explain $q;
---enable_info ONCE
-eval $q;
-select concat(old_c1,'->',c1),c3,
- case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-truncate table t1;
-insert into t1 select * from tmp;
diff --git a/mysql-test/main/delete.result b/mysql-test/main/delete.result
index 98d092c..3e3030a 100644
--- a/mysql-test/main/delete.result
+++ b/mysql-test/main/delete.result
@@ -583,8 +583,7 @@ explain delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
select *from t1;
@@ -600,8 +599,7 @@ where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3
order by c2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
delete from t1
where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3
order by c2;
diff --git a/mysql-test/main/delete_use_source_engines.result b/mysql-test/main/delete_use_source_engines.result
index b038b77..32d291d 100644
--- a/mysql-test/main/delete_use_source_engines.result
+++ b/mysql-test/main/delete_use_source_engines.result
@@ -72,18 +72,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+1 PRIMARY a ALL NULL NULL NULL NULL 32 12.00 100.00 5.56 Using where; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -130,13 +127,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -731,22 +726,21 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # 31.00 25.00 3.23 Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
select * from t1;
c1 c2 c3
@@ -843,18 +837,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 Using where; Using index; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 Using where; Using index; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 32.00 100.00 100.00 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 11.67 100.00 5.71 Using where; Using index; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -900,14 +891,12 @@ test.t1 analyze status OK
create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
+1 PRIMARY a ref t1_c2 t1_c2 5 const 8 Using index; FirstMatch(t1)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+1 PRIMARY a ref t1_c2 t1_c2 5 const 8 Using index; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -1038,7 +1027,7 @@ id select_type table type possible_keys key key_len ref rows Extra
analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 30.00 100.00 13.33 Using where
-1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 100.00 100.00 Using index; FirstMatch(t1)
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 18.75 100.00 Using index; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 2 2
@@ -1502,8 +1491,7 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ref t1_c2 t1_c2 10 const,const # Using index
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using index; FirstMatch(t1)
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
@@ -1564,11 +1552,11 @@ create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3
explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1
explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1
delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
affected rows: 4
select * from t1;
@@ -1615,18 +1603,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+1 PRIMARY a ALL NULL NULL NULL NULL 32 12.33 100.00 5.41 Using where; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -1673,13 +1658,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -1810,7 +1793,7 @@ id select_type table type possible_keys key key_len ref rows Extra
analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY a ALL NULL NULL NULL NULL 32 1.00 100.00 100.00 Using where; Start temporary
-1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 100.00 100.00 Using where; End temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 3.12 100.00 Using where; End temporary
select * from t1;
c1 c2 c3
1 2 2
@@ -2274,22 +2257,21 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # Using where
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # 5.00 25.00 20.00 Using where
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 # 1.00 100.00 100.00
select * from t1;
c1 c2 c3
@@ -2475,18 +2457,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+1 PRIMARY a ALL NULL NULL NULL NULL 32 12.67 100.00 5.26 Using where; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -2533,13 +2512,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -3134,22 +3111,21 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # 5.00 25.00 20.00 Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
select * from t1;
c1 c2 c3
@@ -3246,18 +3222,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 Using where; Using index; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 Using where; Using index; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 32.00 100.00 100.00 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 11.67 100.00 5.71 Using where; Using index; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -3304,13 +3277,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+1 PRIMARY a ref t1_c2 t1_c2 5 const 8 Using index; FirstMatch(t1)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+1 PRIMARY a ref t1_c2 t1_c2 5 const 8 Using index; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -3353,21 +3324,18 @@ and c2 >= 3 order by c2;
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
and c2 >= 3 order by c2;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where; Using filesort
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 21 Using where; Using index
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 21 Using index condition
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c2 5 Using index; FirstMatch(t1)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
and c2 >= 3 order by c2;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 21 Using index condition
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 21 Using where; Using index
+1 PRIMARY a index t1_c2 t1_c2 10 NULL 32 Using where; Using index; LooseScan
+1 PRIMARY t1 ref t1_c2 t1_c2 5 test.a.c2 5
analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
and c2 >= 3 order by c2;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 21 20.00 100.00 100.00 Using index condition
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 21 20.00 100.00 100.00 Using where; Using index
+1 PRIMARY a index t1_c2 t1_c2 10 NULL 32 16.00 20.00 25.00 Using where; Using index; LooseScan
+1 PRIMARY t1 ref t1_c2 t1_c2 5 test.a.c2 5 5.00 4.76 100.00
select * from t1;
c1 c2 c3
1 1 1
@@ -3441,7 +3409,7 @@ id select_type table type possible_keys key key_len ref rows Extra
analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 30.00 100.00 13.33 Using where
-1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 100.00 100.00 Using index; FirstMatch(t1)
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 18.75 100.00 Using index; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 2 2
@@ -3905,8 +3873,7 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ref t1_c2 t1_c2 10 const,const # Using index
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using index; FirstMatch(t1)
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
@@ -4018,18 +3985,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+1 PRIMARY a ALL NULL NULL NULL NULL 32 11.67 100.00 5.71 Using where; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -4076,13 +4040,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -4213,7 +4175,7 @@ id select_type table type possible_keys key key_len ref rows Extra
analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY a ALL NULL NULL NULL NULL 32 4.00 100.00 100.00 Using where; Start temporary
-1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 100.00 25.00 Using where; End temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 3.12 25.00 Using where; End temporary
select * from t1;
c1 c2 c3
1 2 2
@@ -4677,22 +4639,21 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # Using where
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # 1.00 25.00 100.00 Using where
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 # 1.00 100.00 100.00
select * from t1;
c1 c2 c3
@@ -4878,18 +4839,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+1 PRIMARY a ALL NULL NULL NULL NULL 32 12.67 100.00 5.26 Using where; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -4936,13 +4894,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -5537,22 +5493,21 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # 5.00 25.00 20.00 Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
select * from t1;
c1 c2 c3
@@ -5649,18 +5604,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 Using where; Using index; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 Using where; Using index; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a index NULL t1_c2 10 NULL 32 32.00 100.00 100.00 Using index
+1 PRIMARY a index NULL t1_c2 10 NULL 32 11.67 100.00 5.71 Using where; Using index; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -5707,13 +5659,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+1 PRIMARY a ref t1_c2 t1_c2 5 const 8 Using index; FirstMatch(t1)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+1 PRIMARY a ref t1_c2 t1_c2 5 const 8 Using index; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -5757,20 +5707,17 @@ explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
and c2 >= 3 order by c2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where; Using filesort
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 20 Using where; Using index
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c2 5 Using index; FirstMatch(t1)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
and c2 >= 3 order by c2;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 20 Using index condition
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 20 Using where; Using index
+1 PRIMARY a index t1_c2 t1_c2 10 NULL 32 Using where; Using index; LooseScan
+1 PRIMARY t1 ref t1_c2 t1_c2 5 test.a.c2 5
analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
and c2 >= 3 order by c2;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 20 20.00 100.00 100.00 Using index condition
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 20 20.00 100.00 100.00 Using where; Using index
+1 PRIMARY a index t1_c2 t1_c2 10 NULL 32 16.00 20.00 25.00 Using where; Using index; LooseScan
+1 PRIMARY t1 ref t1_c2 t1_c2 5 test.a.c2 5 5.00 5.00 100.00
select * from t1;
c1 c2 c3
1 1 1
@@ -5844,7 +5791,7 @@ id select_type table type possible_keys key key_len ref rows Extra
analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 27.00 100.00 14.81 Using where
-1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 100.00 100.00 Using index; FirstMatch(t1)
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 18.75 100.00 Using index; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 2 2
@@ -6308,8 +6255,7 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ref t1_c2 t1_c2 10 const,const # Using index
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using index; FirstMatch(t1)
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
@@ -6421,18 +6367,15 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+1 PRIMARY a ALL NULL NULL NULL NULL 32 13.67 100.00 4.88 Using where; FirstMatch(t1)
select * from t1;
c1 c2 c3
1 3 3
@@ -6479,13 +6422,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -6616,7 +6557,7 @@ id select_type table type possible_keys key key_len ref rows Extra
analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY a ALL NULL NULL NULL NULL 32 4.00 100.00 100.00 Using where; Start temporary
-1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 100.00 25.00 Using where; End temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 3.12 25.00 Using where; End temporary
select * from t1;
c1 c2 c3
1 2 2
@@ -7080,22 +7021,21 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # Using where
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # 6.00 25.00 16.67 Using where
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 # 1.00 100.00 100.00
select * from t1;
c1 c2 c3
@@ -7281,13 +7221,11 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
affected rows: 2
select * from t1;
@@ -7336,13 +7274,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -7930,15 +7866,14 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # Using where
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
@@ -8038,13 +7973,11 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
affected rows: 2
select * from t1;
@@ -8092,13 +8025,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ALL t1_c2 NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ALL t1_c2 NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -8683,8 +8614,7 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ref t1_c2 t1_c2 10 const,const #
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # FirstMatch(t1)
2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
@@ -8793,13 +8723,11 @@ create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1
explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
affected rows: 2
select * from t1;
@@ -8848,13 +8776,11 @@ create table tmp as select * from t1 where exists (select 'X' from t1 a where a.
explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
affected rows: 8
select * from t1;
@@ -9442,15 +9368,14 @@ and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
-3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # Using where
2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
diff --git a/mysql-test/main/derived.result b/mysql-test/main/derived.result
index 495b2d1..f0d5608 100644
--- a/mysql-test/main/derived.result
+++ b/mysql-test/main/derived.result
@@ -1325,23 +1325,6 @@ a a
3 3
4 4
6 6
-analyze select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except ALL (select t3.a from t3 order by b))q where t1.a=q.a;
-id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00 Using where
-1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 1 1.17 100.00 100.00
-2 DERIVED t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
-3 UNION t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
-4 EXCEPT t3 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
-NULL UNIT RESULT <unit2,3,4> ALL NULL NULL NULL NULL NULL 7.00 NULL NULL
-select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except ALL (select t3.a from t3 order by b))q where t1.a=q.a;
-a a
-3 3
-3 3
-4 4
-4 4
-5 5
-6 6
-6 6
drop table t1,t2,t3;
#
# MDEV-16549: Server crashes in Item_field::fix_fields on query with
@@ -1360,7 +1343,6 @@ DROP TABLE t1;
# End of 10.3 tests
#
#
-<<<<<<< 25e52f7da156b5e8ab6a784d305125011fa3de44
# Test of "Derived tables and union can now create distinct keys"
#
create table t1 (a int);
@@ -1477,9 +1459,6 @@ a
2
drop table t1;
#
-# End of 11.0 tests
-#
-=======
# MDEV-28883: single/multi-table UPDATE/DELETE whose WHERE condition
# contains subquery from mergeable derived table
# that uses the updated/deleted table
@@ -1811,5 +1790,6 @@ pk a
1 3
deallocate prepare stmt;
drop table t1,t2,t3;
-# End of MariaDB 11.0 tests
->>>>>>> MDEV-28965 Assertion failure when preparing UPDATE with derived table in WHERE
+#
+# End of 11.0 tests
+#
diff --git a/mysql-test/main/derived_cond_pushdown.result b/mysql-test/main/derived_cond_pushdown.result
index 1aa6ea4..92bfa69 100644
--- a/mysql-test/main/derived_cond_pushdown.result
+++ b/mysql-test/main/derived_cond_pushdown.result
@@ -13135,17 +13135,22 @@ EXPLAIN
"query_block": {
"select_id": 3,
"cost": "COST_REPLACED",
- "nested_loop": [
- {
- "table": {
- "table_name": "t1",
- "access_type": "ALL",
- "loops": 1,
- "rows": 2,
- "cost": "COST_REPLACED",
- "filtered": 100,
- "attached_condition": "t1.f2 < 2"
- }
+ "filesort": {
+ "sort_key": "t1.f2",
+ "temporary_table": {
+ "nested_loop": [
+ {
+ "table": {
+ "table_name": "t1",
+ "access_type": "ALL",
+ "loops": 1,
+ "rows": 2,
+ "cost": "COST_REPLACED",
+ "filtered": 100,
+ "attached_condition": "t1.f2 < 2"
+ }
+ }
+ ]
}
}
}
diff --git a/mysql-test/main/log_state.result b/mysql-test/main/log_state.result
index 1b1c737..18c8da7 100644
--- a/mysql-test/main/log_state.result
+++ b/mysql-test/main/log_state.result
@@ -243,7 +243,7 @@ rows_examined sql_text
4 UPDATE t1 SET a=a+sleep(.02) WHERE a>2
8 UPDATE t1 SET a=a+sleep(.02) ORDER BY a DESC
1 UPDATE t2 set b=b+sleep(.02) limit 1
-10 UPDATE t1 SET a=a+sleep(.02) WHERE a in (SELECT b from t2)
+6 UPDATE t1 SET a=a+sleep(.02) WHERE a in (SELECT b from t2)
6 DELETE FROM t1 WHERE a=a+sleep(.02) ORDER BY a LIMIT 2
disconnect con2;
connection default;
diff --git a/mysql-test/main/multi_update.result b/mysql-test/main/multi_update.result
index de36182..2364ee7 100644
--- a/mysql-test/main/multi_update.result
+++ b/mysql-test/main/multi_update.result
@@ -1302,27 +1302,24 @@ t1.c1 > 1 and
exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL idx NULL NULL NULL 8 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+1 PRIMARY t2 range idx idx 5 NULL 3 Using index condition; Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
1 PRIMARY t3 ref idx idx 5 test.t1.c2 3
-2 MATERIALIZED t2 range idx idx 5 NULL 3 Using index condition; Using where
explain delete from t1 using t1,t3
where t1.c2 = t3.c2 and
t1.c1 > 1 and
exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL idx NULL NULL NULL 8 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+1 PRIMARY t2 range idx idx 5 NULL 3 Using where; FirstMatch(t1)
1 PRIMARY t3 ref idx idx 5 test.t1.c2 3 Using index
-2 MATERIALIZED t2 range idx idx 5 NULL 3 Using where
explain update t1,t3 set t1.c1 = t1.c1+10
where t1.c2 = t3.c2 and
t1.c1 > 1 and
exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL idx NULL NULL NULL 8 Using where
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+1 PRIMARY t2 range idx idx 5 NULL 3 Using where; FirstMatch(t1)
1 PRIMARY t3 ref idx idx 5 test.t1.c2 3 Using index
-2 MATERIALIZED t2 range idx idx 5 NULL 3 Using where
create table t as select * from t1;
select * from t1,t3
where t1.c2 = t3.c2 and
diff --git a/mysql-test/main/myisam_explain_non_select_all.result b/mysql-test/main/myisam_explain_non_select_all.result
index 3edbf29..2f56847 100644
--- a/mysql-test/main/myisam_explain_non_select_all.result
+++ b/mysql-test/main/myisam_explain_non_select_all.result
@@ -218,16 +218,14 @@ INSERT INTO t2 VALUES (1), (2), (3);
#
EXPLAIN UPDATE t1 SET a = 10 WHERE 1 IN (SELECT 1 FROM t2 WHERE t2.b < 3);
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+1 PRIMARY t2 ALL NULL NULL NULL NULL 3 Using where; FirstMatch
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
-2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 Using where
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE 1 IN (SELECT 1 FROM t2 WHERE t2.b < 3);
id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
+1 PRIMARY t2 ALL NULL NULL NULL NULL 3 33.33 Using where; FirstMatch
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
-2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -248,9 +246,9 @@ Handler_read_key 4
Handler_read_rnd_next 5
# Status of testing query execution:
Variable_name Value
-Handler_read_key 5
+Handler_read_key 4
Handler_read_rnd 3
-Handler_read_rnd_next 12
+Handler_read_rnd_next 9
Handler_update 3
DROP TABLE t1, t2;
@@ -904,15 +902,13 @@ INSERT INTO t2 VALUES (1), (2), (3), (1000);
EXPLAIN UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3
+1 PRIMARY t2 ALL NULL NULL NULL NULL 4 Using where; FirstMatch(t1)
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
-1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
-2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00
+1 PRIMARY t2 ALL NULL NULL NULL NULL 4 100.00 Using where; FirstMatch(t1)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@@ -933,8 +929,8 @@ Handler_read_key 4
Handler_read_rnd_next 9
# Status of testing query execution:
Variable_name Value
-Handler_read_key 7
-Handler_read_rnd_next 8
+Handler_read_key 4
+Handler_read_rnd_next 10
Handler_update 3
DROP TABLE t1, t2;
@@ -2828,14 +2824,14 @@ INSERT INTO t2 VALUES (1), (2), (3);
EXPLAIN UPDATE t1 SET a = 10 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
-1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2 FirstMatch(t1)
+1 PRIMARY <derived3> eq_ref distinct_key distinct_key 5 test.t1.a 1
3 DERIVED t2 ALL NULL NULL NULL NULL 3 Using filesort
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
-1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2 100.00 FirstMatch(t1)
+1 PRIMARY <derived3> eq_ref distinct_key distinct_key 5 test.t1.a 1 100.00
3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
# Status of EXPLAIN EXTENDED query
Variable_name Value
diff --git a/mysql-test/main/opt_trace.result b/mysql-test/main/opt_trace.result
index bc56809..97d7531 100644
--- a/mysql-test/main/opt_trace.result
+++ b/mysql-test/main/opt_trace.result
@@ -4495,7 +4495,7 @@ explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3 {
"rowid_filters": [
{
"key": "a",
- "build_cost": 0.174715752,
+ "build_cost": 2.057372e-5,
"rows": 3
}
]
@@ -4570,7 +4570,7 @@ explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3 {
"rowid_filters": [
{
"key": "a",
- "build_cost": 0.174715752,
+ "build_cost": 2.057372e-5,
"rows": 3
}
]
diff --git a/mysql-test/main/subselect.result b/mysql-test/main/subselect.result
index ce156f1..f6bbba4 100644
--- a/mysql-test/main/subselect.result
+++ b/mysql-test/main/subselect.result
@@ -633,9 +633,9 @@ a b
33 10
22 11
2 12
-delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
+delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
select * from t11;
a b
diff --git a/mysql-test/main/subselect.test b/mysql-test/main/subselect.test
index 8fefa39..21c475e 100644
--- a/mysql-test/main/subselect.test
+++ b/mysql-test/main/subselect.test
@@ -386,9 +386,9 @@ insert into t12 values (33, 10),(22, 11),(2, 12);
insert into t2 values (1, 21),(2, 12),(3, 23);
select * from t11;
select * from t12;
-delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
-- error ER_SUBQUERY_NO_1_ROW
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
+delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
select * from t11;
select * from t12;
diff --git a/mysql-test/main/subselect_no_exists_to_in.result b/mysql-test/main/subselect_no_exists_to_in.result
index 37a503d..45344a9 100644
--- a/mysql-test/main/subselect_no_exists_to_in.result
+++ b/mysql-test/main/subselect_no_exists_to_in.result
@@ -637,9 +637,9 @@ a b
33 10
22 11
2 12
-delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
+delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
select * from t11;
a b
diff --git a/mysql-test/main/subselect_no_mat.result b/mysql-test/main/subselect_no_mat.result
index 32a6358..9061847 100644
--- a/mysql-test/main/subselect_no_mat.result
+++ b/mysql-test/main/subselect_no_mat.result
@@ -640,9 +640,9 @@ a b
33 10
22 11
2 12
-delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
+delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
select * from t11;
a b
diff --git a/mysql-test/main/subselect_no_opts.result b/mysql-test/main/subselect_no_opts.result
index d75c421..48fb77a 100644
--- a/mysql-test/main/subselect_no_opts.result
+++ b/mysql-test/main/subselect_no_opts.result
@@ -636,9 +636,9 @@ a b
33 10
22 11
2 12
-delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
+delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
select * from t11;
a b
diff --git a/mysql-test/main/subselect_no_scache.result b/mysql-test/main/subselect_no_scache.result
index 336936d..59c3308 100644
--- a/mysql-test/main/subselect_no_scache.result
+++ b/mysql-test/main/subselect_no_scache.result
@@ -639,9 +639,9 @@ a b
33 10
22 11
2 12
-delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
+delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
select * from t11;
a b
diff --git a/mysql-test/main/subselect_no_semijoin.result b/mysql-test/main/subselect_no_semijoin.result
index c34dfe6..c723833 100644
--- a/mysql-test/main/subselect_no_semijoin.result
+++ b/mysql-test/main/subselect_no_semijoin.result
@@ -636,9 +636,9 @@ a b
33 10
22 11
2 12
-delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
ERROR 21000: Subquery returns more than 1 row
+delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
select * from t11;
a b
diff --git a/mysql-test/main/update_use_source.test b/mysql-test/main/update_use_source.test
index 65dfa8b..54b077d 100644
--- a/mysql-test/main/update_use_source.test
+++ b/mysql-test/main/update_use_source.test
@@ -4,59 +4,12 @@
set @save_default_engine=@@default_storage_engine;
-set global innodb_stats_persistent=1;
-set default_storage_engine=InnoDB;
---source include/update_use_source.inc
---source include/update_use_source_ext.inc
+# set global innodb_stats_persistent=1;
+# set default_storage_engine=InnoDB;
+# --source include/update_use_source.inc
+# --source include/update_use_source_ext.inc
set default_storage_engine=Aria;
--source include/update_use_source.inc
---source include/update_use_source_ext.inc
-
-set default_storage_engine=MyISAM;
---source include/update_use_source.inc
---source include/update_use_source_ext.inc
-
-set default_storage_engine=MEMORY;
---source include/update_use_source.inc
-
-set @@default_storage_engine=@save_default_engine;
-
---echo #
---echo # Test with MyISAM
---echo #
-
-create table t1 (old_c1 integer,
- old_c2 integer,
- c1 integer,
- c2 integer,
- c3 integer) engine=MyISAM;
-insert t1 (c1,c2,c3) select 0,seq,seq%10 from seq_1_to_500;
-insert t1 (c1,c2,c3) select 1,seq,seq%10 from seq_1_to_400;
-insert t1 (c1,c2,c3) select 2,seq,seq%10 from seq_1_to_300;
-insert t1 (c1,c2,c3) select 3,seq,seq%10 from seq_1_to_200;
-create index t1_idx1 on t1(c3);
-analyze table t1;
-
-update t1 set c1=2 where exists (select 'x' from t1);
-select count(*) from t1 where c1=2;
-update t1 set c1=3 where c3 in (select c3 from t1 b where t1.c3=b.c1);
-select count(*) from t1 where c1=3;
-drop table t1;
-
-
---echo #
---echo # Test error on multi_update conversion on view
---echo # with order by or limit
---echo #
-create table t1 (c1 integer) engine=InnoDb;
-create table t2 (c1 integer) engine=InnoDb;
-create view v1 as select t1.c1 as "t1c1" ,t2.c1 as "t2c1"
- from t1,t2 where t1.c1=t2.c1;
-# 'order by 1' should be considered as in 'select * from v1 order 1'
-update v1 set t1c1=2 order by 1;
-update v1 set t1c1=2 limit 1;
drop table t1;
-drop table t2;
-drop view v1;
1
0
[Commits] 540e637: MDEV-29390: Improve coverage for UPDATE and DELETE statements in MTR test suites
by IgorBabaev 14 Feb '23
by IgorBabaev 14 Feb '23
14 Feb '23
revision-id: 540e6379351e390245cf6877e9532340dc0313be (mariadb-10.11.1-59-g540e637)
parent(s): efc4614112e8f252ac83320e0ad7035836b8a383
author: Lena Startseva
committer: Igor Babaev
timestamp: 2023-02-14 13:54:34 -0800
message:
MDEV-29390: Improve coverage for UPDATE and DELETE statements in MTR test suites
Created tests for "delete" based on update_use_source.test
For the update_use_source.test tests, data recovery in the table has been changed
from a rollback transaction to a complete delete and re-insert of the data with
optimize table. Cases are now being checked on three engines.
Added tests for update/delete with LooseScan and DuplicateWeedout optimization strategies
Added tests for engine MEMORY on delete and update
Added tests for multi-update with JSON_TABLE
Added tests for multi-update and multi-delete for engine Connect
---
mysql-test/include/delete_use_source.inc | 87 +
mysql-test/include/delete_use_source_cases.inc | 289 +
.../include/delete_use_source_cases_memory.inc | 293 +
mysql-test/include/delete_use_source_memory.inc | 27 +
mysql-test/include/update_use_source.inc | 293 +-
mysql-test/include/update_use_source_cases.inc | 159 +
mysql-test/include/update_use_source_ext.inc | 59 +
mysql-test/main/delete_single_to_multi.result | 623 ++
mysql-test/main/delete_single_to_multi.test | 224 +
mysql-test/main/delete_use_source_engines.result | 9499 ++++++++++++++++++++
mysql-test/main/delete_use_source_engines.test | 22 +
mysql-test/main/update_single_to_multi.result | 539 ++
mysql-test/main/update_single_to_multi.test | 238 +
mysql-test/main/update_use_source.result | 5582 +++++++++++-
mysql-test/main/update_use_source.test | 222 +-
mysql-test/suite/json/r/json_table.result | 101 +
mysql-test/suite/json/t/json_table.test | 94 +
storage/connect/mysql-test/connect/r/upd.result | 160 +
storage/connect/mysql-test/connect/t/upd.test | 96 +
19 files changed, 17838 insertions(+), 769 deletions(-)
diff --git a/mysql-test/include/delete_use_source.inc b/mysql-test/include/delete_use_source.inc
new file mode 100644
index 0000000..3c43207
--- /dev/null
+++ b/mysql-test/include/delete_use_source.inc
@@ -0,0 +1,87 @@
+let $engine = `select @@default_storage_engine`;
+
+create table t1 (c1 integer, c2 integer, c3 integer);
+
+insert into t1(c1,c2,c3)
+ values (1,1,1),(1,2,2),(1,3,3),
+ (2,1,4),(2,2,5),(2,3,6),
+ (2,4,7),(2,5,8);
+insert into t1 select c1+10,c2,c3+10 from t1;
+insert into t1 select c1+20,c2+1,c3+20 from t1;
+analyze table t1 persistent for all;
+
+create view v1 as select * from t1 where c2=2;
+
+--echo Test without any index
+--source include/delete_use_source_cases.inc
+
+--echo Test with an index
+create index t1_c2 on t1 (c2,c1);
+--source include/delete_use_source_cases.inc
+
+--echo Test with a primary key
+drop index t1_c2 on t1;
+alter table t1 add primary key (c3);
+--source include/delete_use_source_cases.inc
+
+drop view v1;
+drop table t1;
+
+--echo #
+--echo # Test on dynamic columns (blob)
+--echo #
+create table assets (
+ item_name varchar(32) primary key, -- A common attribute for all items
+ dynamic_cols blob -- Dynamic columns will be stored here
+);
+
+INSERT INTO assets
+ VALUES ('MariaDB T-shirt',
+ COLUMN_CREATE('color', 'blue', 'size', 'XL'));
+INSERT INTO assets
+ VALUES ('Thinkpad Laptop',
+ COLUMN_CREATE('color', 'black', 'price', 500));
+INSERT INTO assets
+ VALUES ('Fridge',
+ COLUMN_CREATE('color', 'white', 'warranty', '5 years'));
+INSERT INTO assets
+ VALUES ('Microwave',
+ COLUMN_CREATE('warranty', '3 years'));
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+ FROM assets ORDER BY item_name;
+UPDATE assets SET dynamic_cols=COLUMN_DELETE(dynamic_cols, 'color')
+ WHERE item_name='Fridge';
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+ FROM assets ORDER BY item_name;
+DELETE FROM assets
+ WHERE item_name in
+ (select b.item_name from assets b
+ where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+ FROM assets ORDER BY item_name;
+DELETE FROM assets WHERE item_name='Microwave';
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+ FROM assets ORDER BY item_name;
+drop table assets ;
+
+
+--echo #
+--echo # Test on fulltext columns
+--echo #
+CREATE TABLE ft2(copy TEXT,FULLTEXT(copy));
+INSERT INTO ft2(copy) VALUES
+ ('MySQL vs MariaDB database'),
+ ('Oracle vs MariaDB database'),
+ ('PostgreSQL vs MariaDB database'),
+ ('MariaDB overview'),
+ ('Foreign keys'),
+ ('Primary keys'),
+ ('Indexes'),
+ ('Transactions'),
+ ('Triggers');
+
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+DELETE FROM ft2 WHERE MATCH(copy) AGAINST('database');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+drop table ft2;
+
diff --git a/mysql-test/include/delete_use_source_cases.inc b/mysql-test/include/delete_use_source_cases.inc
new file mode 100644
index 0000000..35ecf4e
--- /dev/null
+++ b/mysql-test/include/delete_use_source_cases.inc
@@ -0,0 +1,289 @@
+--echo #
+--echo # Delete with value from subquery on the same table
+--echo #
+let $c = c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with EXISTS subquery over the updated table
+--echo # in WHERE + possibly sargable condition
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+eval analyze $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with EXISTS subquery over the updated table
+--echo # in WHERE + non-sargable condition
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with order by
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = exists (select 'X' from t1 a where a.c2 = t1.c2)
+ and c2 >= 3 order by c2;
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+eval analyze $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with IN predicand over the updated table in WHERE
+--echo #
+let $c = c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with a limit - can be deleted
+--echo #
+let $c = c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+eval analyze $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with a limit and an order by
+--echo #
+
+let $c = c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete: 2 execution of PS
+--echo #
+
+prepare create_tmp_stmt from
+ "create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+ "delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+--sorted_result
+select * from t1;
+
+prepare insert_tmp_stmt from
+ "insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+--sorted_result
+select * from t1;
+
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+--sorted_result
+select * from t1;
+
+drop table tmp;
+
+--echo #
+--echo # Delete in stored procedure
+--echo #
+
+delimiter //;
+create procedure sp()
+begin
+ delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+end
+//
+delimiter ;//
+
+create table tmp as select * from t1
+ where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+ where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+ where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+CALL sp;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+
+--echo #
+--echo # Delete in stored function
+--echo #
+delimiter //;
+create function f1(IN a INT) returns int
+begin
+ delete from t1 where c3 < a order by c3 limit 1;
+ return 1;
+end;//
+delimiter ;//
+
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+ order by c3 limit 1;
+select f1(@a);
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+ order by c3 limit 1;
+select f1(@a);
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+
+--echo #
+--echo # Delete in trigger
+--echo #
+
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+ UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+ DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+--enable_info ONCE
+DELETE FROM t1 WHERE c2>=3;
+
+--sorted_result
+select * from t1;
+--sorted_result
+SELECT * FROM t2;
+SELECT * FROM cnt;
+
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+
+--echo #
+--echo # Delete with a reference to view in subquery
+--echo #
+let $c = t1.c2 in ( select max(a.c2) from v1 a
+ where a.c1 = t1.c1);
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+eval analyze $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete from view
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = v1.c1 in
+ (select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+eval create table tmp as select * from v1 where $c;
+let $q = delete from v1 where $c;
+eval explain select * from v1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete from view using reference
+--echo # to the same view in subquery
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+ and c1 = 2
+ and exists (select 'X' from v1 a where a.c1 = v1.c1);
+eval create table tmp as select * from v1 where $c;
+let $q = delete from v1 where $c;
+--replace_column 9 #
+eval explain select * from v1 where $c;
+--replace_column 9 #
+eval explain $q;
+--replace_column 9 #
+eval analyze $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
diff --git a/mysql-test/include/delete_use_source_cases_memory.inc b/mysql-test/include/delete_use_source_cases_memory.inc
new file mode 100644
index 0000000..a246774
--- /dev/null
+++ b/mysql-test/include/delete_use_source_cases_memory.inc
@@ -0,0 +1,293 @@
+--echo #
+--echo # Delete with value from subquery on the same table
+--echo #
+let $c = c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with EXISTS subquery over the updated table
+--echo # in WHERE + possibly sargable condition
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with EXISTS subquery over the updated table
+--echo # in WHERE + non-sargable condition
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with order by
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = exists (select 'X' from t1 a where a.c2 = t1.c2)
+ and c2 >= 3 order by c2;
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with IN predicand over the updated table in WHERE
+--echo #
+let $c = c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with a limit - can be deleted
+--echo #
+let $c = c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete with a limit and an order by
+--echo #
+
+let $c = c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete: 2 execution of PS
+--echo #
+
+prepare create_tmp_stmt from
+ "create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+ "delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+--sorted_result
+select * from t1;
+
+prepare insert_tmp_stmt from
+ "insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+--sorted_result
+select * from t1;
+
+drop table tmp;
+
+--echo #
+--echo # Delete in stored procedure
+--echo #
+
+delimiter //;
+create procedure sp()
+begin
+ delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+end
+//
+delimiter ;//
+
+create table tmp as select * from t1
+ where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+ where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+ where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 1;
+CALL sp;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+
+--echo #
+--echo # Delete in stored function
+--echo #
+delimiter //;
+create function f1(IN a INT) returns int
+begin
+ delete from t1 where c3 < a order by c3 limit 1;
+ return 1;
+end;//
+delimiter ;//
+
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+ order by c3 limit 1;
+select f1(@a);
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+ order by c3 limit 1;
+select f1(@a);
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+
+--echo #
+--echo # Delete in trigger
+--echo #
+
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+ UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+ DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+--enable_info ONCE
+DELETE FROM t1 WHERE c2>=3;
+
+--sorted_result
+select * from t1;
+--sorted_result
+SELECT * FROM t2;
+SELECT * FROM cnt;
+
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+
+--echo #
+--echo Delete with a reference to view in subquery
+--echo #
+let $c = t1.c2 in ( select max(a.c2) from v1 a
+ where a.c1 = t1.c1);
+eval create table tmp as select * from t1 where $c;
+let $q = delete from t1 where $c;
+eval explain select * from t1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete from view
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = v1.c1 in
+ (select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+eval create table tmp as select * from v1 where $c;
+let $q = delete from v1 where $c;
+eval explain select * from v1 where $c;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
+--echo #
+--echo # Delete from view using reference
+--echo # to the same view in subquery
+--echo #
+
+analyze table t1 persistent for all;
+
+let $c = v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+ and c1 = 2
+ and exists (select 'X' from v1 a where a.c1 = v1.c1);
+eval create table tmp as select * from v1 where $c;
+let $q = delete from v1 where $c;
+--replace_column 9 #
+eval explain select * from v1 where $c;
+--replace_column 9 #
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select * from t1;
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+
diff --git a/mysql-test/include/delete_use_source_memory.inc b/mysql-test/include/delete_use_source_memory.inc
new file mode 100644
index 0000000..8e4640b
--- /dev/null
+++ b/mysql-test/include/delete_use_source_memory.inc
@@ -0,0 +1,27 @@
+create table t1 (c1 integer, c2 integer, c3 integer);
+
+insert into t1(c1,c2,c3)
+ values (1,1,1),(1,2,2),(1,3,3),
+ (2,1,4),(2,2,5),(2,3,6),
+ (2,4,7),(2,5,8);
+insert into t1 select c1+10,c2,c3+10 from t1;
+insert into t1 select c1+20,c2+1,c3+20 from t1;
+analyze table t1 persistent for all;
+
+create view v1 as select * from t1 where c2=2;
+
+--echo Test without any index
+--source include/delete_use_source_cases_memory.inc
+
+--echo Test with an index
+create index t1_c2 on t1 (c2,c1);
+--source include/delete_use_source_cases_memory.inc
+
+--echo Test with a primary key
+drop index t1_c2 on t1;
+alter table t1 add primary key (c3);
+--source include/delete_use_source_cases_memory.inc
+
+drop view v1;
+drop table t1;
+
diff --git a/mysql-test/include/update_use_source.inc b/mysql-test/include/update_use_source.inc
index 3c48770..196f141 100644
--- a/mysql-test/include/update_use_source.inc
+++ b/mysql-test/include/update_use_source.inc
@@ -1,150 +1,189 @@
# Include to test update with same table as source and target
---echo #
---echo # Update a with value from subquery on the same table, no search clause. ALL access
---echo #
-
-#Enable view protocol after fix MDEV-29207
---disable_view_protocol
-start transaction;
+create table t1 (old_c1 integer,
+ old_c2 integer,
+ c1 integer,
+ c2 integer,
+ c3 integer);
+insert into t1 select c1+10,c2,c3+10, NULL, NULL from t1;
+insert into t1 select c1+20,c2+1,c3+20, NULL, NULL from t1;
+analyze table t1 persistent for all;
+
+create view v1 as select * from t1 where c2=2;
+delimiter /;
+create trigger trg_t1 before update on t1 for each row
+begin
+ set new.old_c1=old.c1;
+ set new.old_c2=old.c2;
+end;
+/
+delimiter ;/
+
+insert into t1(c1,c2,c3)
+ values (1,1,1), (1,2,2), (1,3,3),
+ (2,1,4), (2,2,5), (2,3,6),
+ (2,4,7), (2,5,8);
+analyze table t1;
+select * from t1;
+
+create table tmp as select * from t1;
+
+--echo # Test without any index
+--source include/update_use_source_cases.inc
+
+--echo # Test with an index on updated columns
+create index t1_c2 on t1 (c2,c1);
+analyze table t1;
+--source include/update_use_source_cases.inc
+
+--echo # Test with an index on updated columns
+create index t1_c3 on t1 (c3);
+analyze table t1;
+--source include/update_use_source_cases.inc
+
+--echo # Test with a primary key on updated columns
+drop index t1_c3 on t1;
+alter table t1 add primary key (c3);
+analyze table t1;
+--source include/update_use_source_cases.inc
+
+--echo # Update with error "Subquery returns more than 1 row"
+--error ER_SUBQUERY_NO_1_ROW
+update t1 set c2=(select c2 from t1);
+--sorted_result
+select c1,c2,c3 from t1;
+
+--echo # Update with error "Subquery returns more than 1 row"
+--echo # and order by
+--error ER_SUBQUERY_NO_1_ROW
+update t1 set c2=(select c2 from t1) order by c3;
+--sorted_result
+select c1,c2,c3 from t1;
+
+-- echo # Duplicate value on update a primary key
+--error ER_DUP_ENTRY
+
+update t1 set c3=0
+ where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+-- echo # Duplicate value on update a primary key with ignore
--enable_info ONCE
-update t1
- set c1=(select a.c3
- from t1 a
- where a.c3 = t1.c3);
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
-rollback;
-
---echo #
---echo # Update with search clause on the same table
---echo #
-
-start transaction;
+update ignore t1 set c3=0
+ where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+-- echo # Duplicate value on update a primary key and limit
+--error ER_DUP_ENTRY
+update t1 set c3=0
+ where exists (select 'X' from t1 a where a.c2 = t1.c2)
+ and c2 >= 3 limit 2;
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+-- echo # Duplicate value on update a primary key with ignore
+-- echo # and limit
--enable_info ONCE
-update t1
- set c1=10
- where c1 <2
- and exists (select 'X'
- from t1 a
- where a.c1 = t1.c1);
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
-rollback;
-
---echo #
---echo # Update via RANGE or INDEX access if an index or a primary key exists
---echo #
-
-explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
-start transaction;
+update ignore t1 set c3=0
+ where exists (select 'X' from t1 a where a.c2 = t1.c2)
+ and c2 >= 3 limit 2;
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo # Update no rows found
--enable_info ONCE
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-rollback;
-
---echo #
---echo # Update with order by
---echo #
-
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 order by c2;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-rollback;
-
---echo #
---echo Update using a view in subquery
---echo #
-
-start transaction;
+update t1 set c1=10
+ where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1 + 10);
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo # Update no rows changed
+drop trigger trg_t1;
--enable_info ONCE
-update t1
- set c1=c1 +(select max(a.c2)
- from v1 a
- where a.c1 = t1.c1) ;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-rollback;
+update t1 set c1=c1
+ where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1);
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
--echo #
---echo # Update throw a view
+--echo # Check call of after trigger
--echo #
-start transaction;
---enable_info ONCE
-update v1
- set c1=c1 + (select max(a.c2)
- from t1 a
- where a.c1 = v1.c1) +10
-where c3 > 3;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-rollback;
-
---echo #
---echo # Update through a view and using the view in subquery
---echo #
+delimiter /;
+create or replace trigger trg_t2 after update on t1 for each row
+begin
+ declare msg varchar(100);
+ if (new.c3 = 5) then
+ set msg=concat('in after update trigger on ',new.c3);
+ SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
+ end if;
+end;
+/
+delimiter ;/
+--error 1644
-start transaction;
---enable_info ONCE
-update v1
- set c1=c1 + 1
- where c1 <2
- and exists (select 'X'
- from v1 a
- where a.c1 = v1.c1);
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-rollback;
+update t1 set c1=2
+ where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1);
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
--echo #
---echo # Update through a view and using the view in subquery
+--echo # Check update with order by and after trigger
--echo #
-start transaction;
---enable_info ONCE
-update v1
- set c1=(select max(a.c1)+10
- from v1 a
- where a.c1 = v1.c1)
- where c1 <10
- and exists (select 'X'
- from v1 a
- where a.c2 = v1.c2);
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
-rollback;
+--error 1644
+update t1 set c1=2
+ where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1)
+ order by t1.c2, t1.c1;
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+drop view v1;
--echo #
---echo # Update of the index or primary key (c3)
+--echo # Check update on view with check option
--echo #
-start transaction;
-explain update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
---enable_info ONCE
-update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
-select c3 from t1;
-rollback;
+create view v1 as select * from t1 where c2=2 with check option;
---echo #
---echo # update with a limit
---echo #
+-- error 1369
+update v1 set c2=3 where c1=1;
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
-start transaction;
---enable_info ONCE
-update t1
- set c1=(select a.c3
- from t1 a
- where a.c3 = t1.c3)
- limit 2;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
-rollback;
+-- error 1369
+update v1 set c2=(select max(c3) from v1) where c1=1;
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
---echo #
---echo # update with a limit and an order by
---echo #
+update v1 set c2=(select min(va.c3) from v1 va), c1=0 where c1=1;
+--sorted_result
+select c1,c2,c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
-start transaction;
---enable_info ONCE
-update t1
- set c1=(select a.c3
- from t1 a
- where a.c3 = t1.c3)
- order by c3 desc limit 2;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
-rollback;
---enable_view_protocol
+drop table tmp;
+drop view v1;
+drop table t1;
diff --git a/mysql-test/include/update_use_source_cases.inc b/mysql-test/include/update_use_source_cases.inc
new file mode 100644
index 0000000..55b45a3
--- /dev/null
+++ b/mysql-test/include/update_use_source_cases.inc
@@ -0,0 +1,159 @@
+--echo #
+--echo # Update with value from subquery on the same table
+--echo #
+
+let $q=update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update with EXISTS subquery over the updated table
+--echo # in WHERE + possibly sargable condition
+--echo #
+
+let $q=update t1 set c1=10
+ where c1 <2
+ and exists (select 'X' from t1 a where a.c1 = t1.c1);
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update with EXISTS subquery over the updated table
+--echo # in WHERE + non-sargable condition
+--echo #
+
+let $q=update t1 set c1=c1+10
+ where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update with order by
+--echo #
+
+let $q=update t1 set c1=c1+10
+ where exists (select 'X' from t1 a where a.c2 = t1.c2)
+ and c2 >= 3 order by c2;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update with a reference to view in subquery
+--echo #
+
+let $q=update t1 set c1=c1 +(select max(a.c2) from v1 a
+ where a.c1 = t1.c1);
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update view
+--echo #
+
+let $q=update v1 set c1=c1 + (select max(a.c2) from t1 a
+ where a.c1 = v1.c1) +10 where c3 > 3;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update view with reference to the same view in subquery
+--echo #
+
+let $q=update v1 set c1=c1 + 1
+ where c1 <2
+ and exists (select 'X' from v1 a where a.c1 = v1.c1);
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update view with EXISTS and reference to the same view in subquery
+--echo #
+
+let $q=update v1
+ set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+ where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update with IN predicand over the updated table in WHERE
+--echo #
+
+let $q=update t1 set c3=c3+10
+ where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+eval explain $q;
+--enable_info ONCE
+eval $q;
+--sorted_result
+select c3 from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update with a limit
+--echo #
+
+let $q=update t1
+ set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
+
+--echo #
+--echo # Update with a limit and an order by
+--echo #
+
+let $q=update t1
+ set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+ order by c3 desc limit 2;
+#eval explain $q;
+--enable_info ONCE
+eval $q;
+select concat(old_c1,'->',c1),c3,
+ case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+truncate table t1;
+insert into t1 select * from tmp;
diff --git a/mysql-test/include/update_use_source_ext.inc b/mysql-test/include/update_use_source_ext.inc
new file mode 100644
index 0000000..004f7a9
--- /dev/null
+++ b/mysql-test/include/update_use_source_ext.inc
@@ -0,0 +1,59 @@
+--echo #
+--echo # Test on dynamic columns (blob)
+--echo #
+
+create table assets (
+ item_name varchar(32) primary key, -- A common attribute for all items
+ dynamic_cols blob -- Dynamic columns will be stored here
+);
+INSERT INTO assets VALUES ('MariaDB T-shirt',
+ COLUMN_CREATE('color', 'blue', 'size', 'XL'));
+INSERT INTO assets VALUES ('Thinkpad Laptop',
+ COLUMN_CREATE('color', 'black', 'price', 500));
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+ FROM assets;
+UPDATE assets
+ SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '3 years')
+ WHERE item_name='Thinkpad Laptop';
+SELECT item_name,
+ COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+ FROM assets;
+UPDATE assets
+ SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '4 years')
+ WHERE item_name in
+ (select b.item_name from assets b
+ where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
+SELECT item_name,
+ COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+ FROM assets;
+
+UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty',
+ (select COLUMN_GET(b.dynamic_cols, 'color' as char)
+ from assets b
+ where assets.item_name = item_name));
+SELECT item_name,
+ COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+ FROM assets;
+drop table assets;
+
+--echo #
+--echo # Test on fulltext columns
+--echo #
+CREATE TABLE ft2(copy TEXT,FULLTEXT(copy));
+INSERT INTO ft2(copy) VALUES
+ ('MySQL vs MariaDB database'),
+ ('Oracle vs MariaDB database'),
+ ('PostgreSQL vs MariaDB database'),
+ ('MariaDB overview'),
+ ('Foreign keys'),
+ ('Primary keys'),
+ ('Indexes'),
+ ('Transactions'),
+ ('Triggers');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+
+update ft2 set copy = (select max(concat('mykeyword ',substr(b.copy,1,5)))
+ from ft2 b WHERE MATCH(b.copy) AGAINST('database'))
+ where MATCH(copy) AGAINST('keys');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('mykeyword');
+drop table ft2;
diff --git a/mysql-test/main/delete_single_to_multi.result b/mysql-test/main/delete_single_to_multi.result
index 9bd5e93..b49f07e 100644
--- a/mysql-test/main/delete_single_to_multi.result
+++ b/mysql-test/main/delete_single_to_multi.result
@@ -2390,4 +2390,627 @@ o_orderkey o_totalprice
4903 34363.63
5607 24660.06
drop table t;
+# LooseScan
+# =========
+create index i_l_sup_part on lineitem(l_suppkey, l_partkey);
+create index i_ps_sup_part on partsupp(ps_suppkey, ps_partkey);
+analyze table lineitem;
+Table Op Msg_type Msg_text
+dbt3_s001.lineitem analyze status Engine-independent statistics collected
+dbt3_s001.lineitem analyze status Table is already up to date
+analyze table partsupp;
+Table Op Msg_type Msg_text
+dbt3_s001.partsupp analyze status Engine-independent statistics collected
+dbt3_s001.partsupp analyze status Table is already up to date
+explain
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY partsupp index PRIMARY,i_ps_partkey,i_ps_suppkey,i_ps_sup_part i_ps_sup_part 8 NULL 700 Using where; Using index; LooseScan
+1 PRIMARY lineitem ref i_l_suppkey,i_l_sup_part i_l_suppkey 5 dbt3_s001.partsupp.ps_suppkey 600 Using index
+explain format=json
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+EXPLAIN
+{
+ "query_block": {
+ "select_id": 1,
+ "nested_loop": [
+ {
+ "table": {
+ "table_name": "partsupp",
+ "access_type": "index",
+ "possible_keys": [
+ "PRIMARY",
+ "i_ps_partkey",
+ "i_ps_suppkey",
+ "i_ps_sup_part"
+ ],
+ "key": "i_ps_sup_part",
+ "key_length": "8",
+ "used_key_parts": ["ps_suppkey", "ps_partkey"],
+ "rows": 700,
+ "filtered": 1.428571463,
+ "attached_condition": "partsupp.ps_partkey in (1,2,3)",
+ "using_index": true,
+ "loose_scan": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey", "i_l_sup_part"],
+ "key": "i_l_suppkey",
+ "key_length": "5",
+ "used_key_parts": ["l_suppkey"],
+ "ref": ["dbt3_s001.partsupp.ps_suppkey"],
+ "rows": 600,
+ "filtered": 100,
+ "using_index": true
+ }
+ }
+ ]
+ }
+}
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+5373
+create table t as
+select * from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+explain
+delete from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY partsupp index PRIMARY,i_ps_partkey,i_ps_suppkey,i_ps_sup_part i_ps_sup_part 8 NULL 700 Using where; Using index; LooseScan
+1 PRIMARY lineitem ref i_l_suppkey,i_l_sup_part i_l_suppkey 5 dbt3_s001.partsupp.ps_suppkey 600
+explain format=json
+delete from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+EXPLAIN
+{
+ "query_block": {
+ "select_id": 1,
+ "nested_loop": [
+ {
+ "table": {
+ "table_name": "partsupp",
+ "access_type": "index",
+ "possible_keys": [
+ "PRIMARY",
+ "i_ps_partkey",
+ "i_ps_suppkey",
+ "i_ps_sup_part"
+ ],
+ "key": "i_ps_sup_part",
+ "key_length": "8",
+ "used_key_parts": ["ps_suppkey", "ps_partkey"],
+ "rows": 700,
+ "filtered": 1.428571463,
+ "attached_condition": "partsupp.ps_partkey in (1,2,3)",
+ "using_index": true,
+ "loose_scan": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey", "i_l_sup_part"],
+ "key": "i_l_suppkey",
+ "key_length": "5",
+ "used_key_parts": ["l_suppkey"],
+ "ref": ["dbt3_s001.partsupp.ps_suppkey"],
+ "rows": 600,
+ "filtered": 100
+ }
+ }
+ ]
+ }
+}
+delete from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+0
+insert into lineitem select * from t;
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+5373
+drop table t;
+# LooseScan PS
+# ============
+prepare stmt from "
+delete from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+";
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+5373
+create table t as
+select * from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+execute stmt;
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+0
+insert into lineitem select * from t;
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+5373
+create table r as
+select * from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+execute stmt;
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+0
+insert into lineitem select * from r;
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+5373
+drop tables r, t;
+deallocate prepare stmt;
+# LooseScan SP
+# ============
+create procedure p()
+delete from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+5373
+create table t as
+select * from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+call p();
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+0
+insert into lineitem select * from t;
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+5373
+create table r as
+select * from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+call p();
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+0
+insert into lineitem select * from r;
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp
+where ps_partkey in (1,2,3));
+count(*)
+5373
+drop tables r, t;
+drop procedure p;
+drop index i_l_sup_part on lineitem;
+drop index i_ps_sup_part on partsupp;
+# DuplicateWeedout
+# ================
+set @tmp_optimizer_switch= @@optimizer_switch;
+set optimizer_switch='materialization=off';
+analyze table lineitem;
+Table Op Msg_type Msg_text
+dbt3_s001.lineitem analyze status Engine-independent statistics collected
+dbt3_s001.lineitem analyze status Table is already up to date
+analyze table orders;
+Table Op Msg_type Msg_text
+dbt3_s001.orders analyze status Engine-independent statistics collected
+dbt3_s001.orders analyze status OK
+explain
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY supplier range PRIMARY PRIMARY 4 NULL 1 Using where; Using index; Start temporary
+1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 70 Using where
+1 PRIMARY lineitem ref i_l_suppkey_partkey,i_l_partkey i_l_partkey 5 dbt3_s001.partsupp.ps_partkey 30 Using index
+1 PRIMARY lineitem ref i_l_suppkey_partkey,i_l_partkey i_l_partkey 5 dbt3_s001.partsupp.ps_partkey 30 Using index; End temporary
+explain format=json
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+EXPLAIN
+{
+ "query_block": {
+ "select_id": 1,
+ "nested_loop": [
+ {
+ "duplicates_removal": [
+ {
+ "table": {
+ "table_name": "supplier",
+ "access_type": "range",
+ "possible_keys": ["PRIMARY"],
+ "key": "PRIMARY",
+ "key_length": "4",
+ "used_key_parts": ["s_suppkey"],
+ "rows": 1,
+ "filtered": 100,
+ "attached_condition": "supplier.s_suppkey < 2",
+ "using_index": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "partsupp",
+ "access_type": "ref",
+ "possible_keys": ["PRIMARY", "i_ps_partkey", "i_ps_suppkey"],
+ "key": "i_ps_suppkey",
+ "key_length": "4",
+ "used_key_parts": ["ps_suppkey"],
+ "ref": ["dbt3_s001.supplier.s_suppkey"],
+ "rows": 70,
+ "filtered": 100,
+ "attached_condition": "partsupp.ps_partkey is not null"
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey_partkey", "i_l_partkey"],
+ "key": "i_l_partkey",
+ "key_length": "5",
+ "used_key_parts": ["l_partkey"],
+ "ref": ["dbt3_s001.partsupp.ps_partkey"],
+ "rows": 30,
+ "filtered": 100,
+ "using_index": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey_partkey", "i_l_partkey"],
+ "key": "i_l_partkey",
+ "key_length": "5",
+ "used_key_parts": ["l_partkey"],
+ "ref": ["dbt3_s001.partsupp.ps_partkey"],
+ "rows": 30,
+ "filtered": 100,
+ "using_index": true
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+create table t as
+select * from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+explain
+delete from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY supplier range PRIMARY PRIMARY 4 NULL 1 Using where; Using index; Start temporary
+1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 70 Using where
+1 PRIMARY lineitem ref i_l_suppkey_partkey,i_l_partkey i_l_partkey 5 dbt3_s001.partsupp.ps_partkey 30
+1 PRIMARY lineitem ref i_l_suppkey_partkey,i_l_partkey i_l_partkey 5 dbt3_s001.partsupp.ps_partkey 30 Using index; End temporary
+explain format=json
+delete from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+EXPLAIN
+{
+ "query_block": {
+ "select_id": 1,
+ "nested_loop": [
+ {
+ "duplicates_removal": [
+ {
+ "table": {
+ "table_name": "supplier",
+ "access_type": "range",
+ "possible_keys": ["PRIMARY"],
+ "key": "PRIMARY",
+ "key_length": "4",
+ "used_key_parts": ["s_suppkey"],
+ "rows": 1,
+ "filtered": 100,
+ "attached_condition": "supplier.s_suppkey < 2",
+ "using_index": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "partsupp",
+ "access_type": "ref",
+ "possible_keys": ["PRIMARY", "i_ps_partkey", "i_ps_suppkey"],
+ "key": "i_ps_suppkey",
+ "key_length": "4",
+ "used_key_parts": ["ps_suppkey"],
+ "ref": ["dbt3_s001.supplier.s_suppkey"],
+ "rows": 70,
+ "filtered": 100,
+ "attached_condition": "partsupp.ps_partkey is not null"
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey_partkey", "i_l_partkey"],
+ "key": "i_l_partkey",
+ "key_length": "5",
+ "used_key_parts": ["l_partkey"],
+ "ref": ["dbt3_s001.partsupp.ps_partkey"],
+ "rows": 30,
+ "filtered": 100
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey_partkey", "i_l_partkey"],
+ "key": "i_l_partkey",
+ "key_length": "5",
+ "used_key_parts": ["l_partkey"],
+ "ref": ["dbt3_s001.partsupp.ps_partkey"],
+ "rows": 30,
+ "filtered": 100,
+ "using_index": true
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
+delete from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+0
+insert into lineitem select * from t;
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+drop table t;
+# DuplicateWeedout PS
+# ===================
+prepare stmt from "
+delete from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+";
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+create table t as
+select * from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+execute stmt;
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+0
+insert into lineitem select * from t;
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+create table r as
+select * from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+execute stmt;
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+0
+insert into lineitem select * from r;
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+drop tables r, t;
+deallocate prepare stmt;
+# DuplicateWeedout SP
+# ===================
+create procedure p()
+delete from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+create table t as
+select * from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+call p();
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+0
+insert into lineitem select * from t;
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+create table r as
+select * from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+call p();
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+0
+insert into lineitem select * from r;
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+drop tables r, t;
+drop procedure p;
+set @@optimizer_switch=@tmp_optimizer_switch;
DROP DATABASE dbt3_s001;
diff --git a/mysql-test/main/delete_single_to_multi.test b/mysql-test/main/delete_single_to_multi.test
index 2c49128..3a2a683 100644
--- a/mysql-test/main/delete_single_to_multi.test
+++ b/mysql-test/main/delete_single_to_multi.test
@@ -798,4 +798,228 @@ eval
select o_orderkey, o_totalprice from orders where $c11;
drop table t;
+--echo # LooseScan
+--echo # =========
+
+create index i_l_sup_part on lineitem(l_suppkey, l_partkey);
+create index i_ps_sup_part on partsupp(ps_suppkey, ps_partkey);
+
+analyze table lineitem;
+analyze table partsupp;
+
+let $c12 = l_suppkey in
+ (select ps_suppkey from partsupp
+ where ps_partkey in (1,2,3));
+
+eval
+explain
+select count(*) from lineitem where $c12;
+eval
+explain format=json
+select count(*) from lineitem where $c12;
+eval
+select count(*) from lineitem where $c12;
+eval
+create table t as
+select * from lineitem where $c12;
+
+eval
+explain
+delete from lineitem where $c12;
+eval
+explain format=json
+delete from lineitem where $c12;
+eval
+delete from lineitem where $c12;
+eval
+select count(*) from lineitem where $c12;
+
+insert into lineitem select * from t;
+eval
+select count(*) from lineitem where $c12;
+
+drop table t;
+
+--echo # LooseScan PS
+--echo # ============
+
+eval
+prepare stmt from "
+delete from lineitem where $c12;
+";
+
+eval
+select count(*) from lineitem where $c12;
+eval
+create table t as
+select * from lineitem where $c12;
+execute stmt;
+eval
+select count(*) from lineitem where $c12;
+insert into lineitem select * from t;
+eval
+select count(*) from lineitem where $c12;
+
+eval
+create table r as
+select * from lineitem where $c12;
+execute stmt;
+eval
+select count(*) from lineitem where $c12;
+insert into lineitem select * from r;
+eval
+select count(*) from lineitem where $c12;
+
+drop tables r, t;
+deallocate prepare stmt;
+
+--echo # LooseScan SP
+--echo # ============
+
+eval
+create procedure p()
+delete from lineitem where $c12;
+
+eval
+select count(*) from lineitem where $c12;
+eval
+create table t as
+select * from lineitem where $c12;
+call p();
+eval
+select count(*) from lineitem where $c12;
+insert into lineitem select * from t;
+eval
+select count(*) from lineitem where $c12;
+
+eval
+create table r as
+select * from lineitem where $c12;
+call p();
+eval
+select count(*) from lineitem where $c12;
+insert into lineitem select * from r;
+eval
+select count(*) from lineitem where $c12;
+
+drop tables r, t;
+drop procedure p;
+
+drop index i_l_sup_part on lineitem;
+drop index i_ps_sup_part on partsupp;
+
+--echo # DuplicateWeedout
+--echo # ================
+
+set @tmp_optimizer_switch= @@optimizer_switch;
+set optimizer_switch='materialization=off';
+
+analyze table lineitem;
+analyze table orders;
+
+let $c13 = l_partkey in (
+ select ps_partkey
+ from partsupp join lineitem on ps_partkey=l_partkey
+ where ps_suppkey in (
+ select s_suppkey from supplier where s_suppkey < 2
+ )
+);
+
+eval
+explain
+select count(*) from lineitem where $c13;
+eval
+explain format=json
+select count(*) from lineitem where $c13;
+eval
+select count(*) from lineitem where $c13;
+eval
+create table t as
+select * from lineitem where $c13;
+
+eval
+explain
+delete from lineitem where $c13;
+eval
+explain format=json
+delete from lineitem where $c13;
+eval
+delete from lineitem where $c13;
+eval
+select count(*) from lineitem where $c13;
+
+insert into lineitem select * from t;
+eval
+select count(*) from lineitem where $c13;
+
+drop table t;
+
+--echo # DuplicateWeedout PS
+--echo # ===================
+
+eval
+prepare stmt from "
+delete from lineitem where $c13;
+";
+
+eval
+select count(*) from lineitem where $c13;
+eval
+create table t as
+select * from lineitem where $c13;
+execute stmt;
+eval
+select count(*) from lineitem where $c13;
+insert into lineitem select * from t;
+eval
+select count(*) from lineitem where $c13;
+
+eval
+create table r as
+select * from lineitem where $c13;
+execute stmt;
+eval
+select count(*) from lineitem where $c13;
+insert into lineitem select * from r;
+eval
+select count(*) from lineitem where $c13;
+
+drop tables r, t;
+deallocate prepare stmt;
+
+--echo # DuplicateWeedout SP
+--echo # ===================
+
+eval
+create procedure p()
+delete from lineitem where $c13;
+
+eval
+select count(*) from lineitem where $c13;
+eval
+create table t as
+select * from lineitem where $c13;
+call p();
+eval
+select count(*) from lineitem where $c13;
+insert into lineitem select * from t;
+eval
+select count(*) from lineitem where $c13;
+
+eval
+create table r as
+select * from lineitem where $c13;
+call p();
+eval
+select count(*) from lineitem where $c13;
+insert into lineitem select * from r;
+eval
+select count(*) from lineitem where $c13;
+
+drop tables r, t;
+drop procedure p;
+
+set @@optimizer_switch=@tmp_optimizer_switch;
+
+
DROP DATABASE dbt3_s001;
diff --git a/mysql-test/main/delete_use_source_engines.result b/mysql-test/main/delete_use_source_engines.result
new file mode 100644
index 0000000..b038b77
--- /dev/null
+++ b/mysql-test/main/delete_use_source_engines.result
@@ -0,0 +1,9499 @@
+set @save_default_engine=@@default_storage_engine;
+set global innodb_stats_persistent=1;
+set default_storage_engine=InnoDB;
+create table t1 (c1 integer, c2 integer, c3 integer);
+insert into t1(c1,c2,c3)
+values (1,1,1),(1,2,2),(1,3,3),
+(2,1,4),(2,2,5),(2,3,6),
+(2,4,7),(2,5,8);
+insert into t1 select c1+10,c2,c3+10 from t1;
+insert into t1 select c1+20,c2+1,c3+20 from t1;
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create view v1 as select * from t1 where c2=2;
+Test without any index
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 62.50 62.50 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 62.50 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 30.00 100.00 13.33 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 24.25 100.00 1.03 Using where; FirstMatch(t1)
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using temporary; Using filesort
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 32.00 25.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+Test with an index
+create index t1_c2 on t1 (c2,c1);
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 32.00 100.00 100.00 Using index
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 28 Using where; Using index
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 28 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 28 Using where; Using index
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 28 20.00 100.00 100.00 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 28 20.00 100.00 100.00 Using where; Using index
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c3 1 Using index; FirstMatch(t1)
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c3 1 Using index; FirstMatch(t1)
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using index; FirstMatch(t1)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using index; FirstMatch(t1)
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 30.00 100.00 13.33 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 100.00 100.00 Using index; FirstMatch(t1)
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using where; FirstMatch(t1)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a index_subquery t1_c2 t1_c2 5 func 5 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 1.00 100.00 100.00 Using index
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 5 Using index
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 5 Using index
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ref t1_c2 t1_c2 10 const,const # Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
+3 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func # Using where; Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # 1.00 100.00 100.00 Using where
+3 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func # 1.00 100.00 100.00 Using where; Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+Test with a primary key
+drop index t1_c2 on t1;
+alter table t1 add primary key (c3);
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 62.50 62.50 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 62.50 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c1 1 Using where; End temporary
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c1 1 Using where; End temporary
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 Using where; End temporary
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 Using where; End temporary
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 1.00 100.00 100.00 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 100.00 100.00 Using where; End temporary
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 1
+1 PRIMARY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using where
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL PRIMARY 4 NULL 1 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 32.00 25.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 # 1.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+drop view v1;
+drop table t1;
+#
+# Test on dynamic columns (blob)
+#
+create table assets (
+item_name varchar(32) primary key, -- A common attribute for all items
+dynamic_cols blob -- Dynamic columns will be stored here
+);
+INSERT INTO assets
+VALUES ('MariaDB T-shirt',
+COLUMN_CREATE('color', 'blue', 'size', 'XL'));
+INSERT INTO assets
+VALUES ('Thinkpad Laptop',
+COLUMN_CREATE('color', 'black', 'price', 500));
+INSERT INTO assets
+VALUES ('Fridge',
+COLUMN_CREATE('color', 'white', 'warranty', '5 years'));
+INSERT INTO assets
+VALUES ('Microwave',
+COLUMN_CREATE('warranty', '3 years'));
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge white
+MariaDB T-shirt blue
+Microwave NULL
+Thinkpad Laptop black
+UPDATE assets SET dynamic_cols=COLUMN_DELETE(dynamic_cols, 'color')
+WHERE item_name='Fridge';
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+Microwave NULL
+Thinkpad Laptop black
+DELETE FROM assets
+WHERE item_name in
+(select b.item_name from assets b
+where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+Microwave NULL
+DELETE FROM assets WHERE item_name='Microwave';
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+drop table assets ;
+#
+# Test on fulltext columns
+#
+CREATE TABLE ft2(copy TEXT,FULLTEXT(copy));
+INSERT INTO ft2(copy) VALUES
+('MySQL vs MariaDB database'),
+('Oracle vs MariaDB database'),
+('PostgreSQL vs MariaDB database'),
+('MariaDB overview'),
+('Foreign keys'),
+('Primary keys'),
+('Indexes'),
+('Transactions'),
+('Triggers');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+copy
+MySQL vs MariaDB database
+Oracle vs MariaDB database
+PostgreSQL vs MariaDB database
+DELETE FROM ft2 WHERE MATCH(copy) AGAINST('database');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+copy
+drop table ft2;
+set default_storage_engine=Aria;
+create table t1 (c1 integer, c2 integer, c3 integer);
+insert into t1(c1,c2,c3)
+values (1,1,1),(1,2,2),(1,3,3),
+(2,1,4),(2,2,5),(2,3,6),
+(2,4,7),(2,5,8);
+insert into t1 select c1+10,c2,c3+10 from t1;
+insert into t1 select c1+20,c2+1,c3+20 from t1;
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create view v1 as select * from t1 where c2=2;
+Test without any index
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 62.50 62.50 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 62.50 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 21.00 100.00 14.29 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 22.67 100.00 1.47 Using where; FirstMatch(t1)
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using temporary; Using filesort
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 32.00 25.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status Table is already up to date
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+Test with an index
+create index t1_c2 on t1 (c2,c1);
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 32.00 100.00 100.00 Using index
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 21 Using where; Using index
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 21 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 21 Using where; Using index
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 21 20.00 100.00 100.00 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 21 20.00 100.00 100.00 Using where; Using index
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c3 1 Using index; FirstMatch(t1)
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c3 1 Using index; FirstMatch(t1)
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using index; FirstMatch(t1)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using index; FirstMatch(t1)
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 30.00 100.00 13.33 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 100.00 100.00 Using index; FirstMatch(t1)
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using where; FirstMatch(t1)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a index_subquery t1_c2 t1_c2 5 func 5 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 1.00 100.00 100.00 Using index
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8 Using where
+2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 5 Using index
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8 Using where
+2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 5 Using index
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status Table is already up to date
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ref t1_c2 t1_c2 10 const,const # Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
+3 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func # Using where; Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # 1.00 100.00 100.00 Using where
+3 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func # 1.00 100.00 100.00 Using where; Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+Test with a primary key
+drop index t1_c2 on t1;
+alter table t1 add primary key (c3);
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 62.50 62.50 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 62.50 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c1 1 Using where; End temporary
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c1 1 Using where; End temporary
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 Using where; End temporary
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 Using where; End temporary
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 4.00 100.00 100.00 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 100.00 25.00 Using where; End temporary
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 1
+1 PRIMARY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using where
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL PRIMARY 4 NULL 1 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 32.00 25.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status Table is already up to date
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 # 1.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+drop view v1;
+drop table t1;
+#
+# Test on dynamic columns (blob)
+#
+create table assets (
+item_name varchar(32) primary key, -- A common attribute for all items
+dynamic_cols blob -- Dynamic columns will be stored here
+);
+INSERT INTO assets
+VALUES ('MariaDB T-shirt',
+COLUMN_CREATE('color', 'blue', 'size', 'XL'));
+INSERT INTO assets
+VALUES ('Thinkpad Laptop',
+COLUMN_CREATE('color', 'black', 'price', 500));
+INSERT INTO assets
+VALUES ('Fridge',
+COLUMN_CREATE('color', 'white', 'warranty', '5 years'));
+INSERT INTO assets
+VALUES ('Microwave',
+COLUMN_CREATE('warranty', '3 years'));
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge white
+MariaDB T-shirt blue
+Microwave NULL
+Thinkpad Laptop black
+UPDATE assets SET dynamic_cols=COLUMN_DELETE(dynamic_cols, 'color')
+WHERE item_name='Fridge';
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+Microwave NULL
+Thinkpad Laptop black
+DELETE FROM assets
+WHERE item_name in
+(select b.item_name from assets b
+where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+Microwave NULL
+DELETE FROM assets WHERE item_name='Microwave';
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+drop table assets ;
+#
+# Test on fulltext columns
+#
+CREATE TABLE ft2(copy TEXT,FULLTEXT(copy));
+INSERT INTO ft2(copy) VALUES
+('MySQL vs MariaDB database'),
+('Oracle vs MariaDB database'),
+('PostgreSQL vs MariaDB database'),
+('MariaDB overview'),
+('Foreign keys'),
+('Primary keys'),
+('Indexes'),
+('Transactions'),
+('Triggers');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+copy
+MySQL vs MariaDB database
+Oracle vs MariaDB database
+PostgreSQL vs MariaDB database
+DELETE FROM ft2 WHERE MATCH(copy) AGAINST('database');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+copy
+drop table ft2;
+set default_storage_engine=MyISAM;
+create table t1 (c1 integer, c2 integer, c3 integer);
+insert into t1(c1,c2,c3)
+values (1,1,1),(1,2,2),(1,3,3),
+(2,1,4),(2,2,5),(2,3,6),
+(2,4,7),(2,5,8);
+insert into t1 select c1+10,c2,c3+10 from t1;
+insert into t1 select c1+20,c2+1,c3+20 from t1;
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create view v1 as select * from t1 where c2=2;
+Test without any index
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 62.50 62.50 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 62.50 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 25.00 100.00 12.00 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 22.67 100.00 1.47 Using where; FirstMatch(t1)
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using temporary; Using filesort
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 32.00 25.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status Table is already up to date
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+Test with an index
+create index t1_c2 on t1 (c2,c1);
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 Using index
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a index NULL t1_c2 10 NULL 32 32.00 100.00 100.00 Using index
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ref t1_c2 t1_c2 5 const 8 Using index
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 20 Using where; Using index
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 20 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 20 Using where; Using index
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 20 20.00 100.00 100.00 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 20 20.00 100.00 100.00 Using where; Using index
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c3 1 Using index; FirstMatch(t1)
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c3 1 Using index; FirstMatch(t1)
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using index; FirstMatch(t1)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using index; FirstMatch(t1)
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 27.00 100.00 14.81 Using where
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 0.25 100.00 100.00 Using index; FirstMatch(t1)
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY a ref t1_c2 t1_c2 5 test.t1.c1 5 Using where; FirstMatch(t1)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a index_subquery t1_c2 t1_c2 5 func 5 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 1.00 100.00 100.00 Using index
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8 Using where
+2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 5 Using index
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 8 Using where
+2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 5 Using index
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status Table is already up to date
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ref t1_c2 t1_c2 10 const,const # Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
+3 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func # Using where; Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # 1.00 100.00 100.00 Using where
+3 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func # 1.00 100.00 100.00 Using where; Using index
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # 32.00 100.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+Test with a primary key
+drop index t1_c2 on t1;
+alter table t1 add primary key (c3);
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 9.38 9.38 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 0.67 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+analyze delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 62.50 62.50 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1.00 100.00 100.00
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 32.00 62.50 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c1 1 Using where; End temporary
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c1 1 Using where; End temporary
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 Using where; End temporary
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 Using where; End temporary
+analyze delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 4.00 100.00 100.00 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 1.00 100.00 25.00 Using where; End temporary
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 1
+1 PRIMARY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using where
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL PRIMARY 4 NULL 1 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+# Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+analyze delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 32.00 100.00 25.00 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 32.00 25.00 3.12 Using where
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status Table is already up to date
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
+analyze delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # 32.00 3.91 3.12 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # 32.00 25.00 25.00 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 # 1.00 100.00 100.00
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+drop view v1;
+drop table t1;
+#
+# Test on dynamic columns (blob)
+#
+create table assets (
+item_name varchar(32) primary key, -- A common attribute for all items
+dynamic_cols blob -- Dynamic columns will be stored here
+);
+INSERT INTO assets
+VALUES ('MariaDB T-shirt',
+COLUMN_CREATE('color', 'blue', 'size', 'XL'));
+INSERT INTO assets
+VALUES ('Thinkpad Laptop',
+COLUMN_CREATE('color', 'black', 'price', 500));
+INSERT INTO assets
+VALUES ('Fridge',
+COLUMN_CREATE('color', 'white', 'warranty', '5 years'));
+INSERT INTO assets
+VALUES ('Microwave',
+COLUMN_CREATE('warranty', '3 years'));
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge white
+MariaDB T-shirt blue
+Microwave NULL
+Thinkpad Laptop black
+UPDATE assets SET dynamic_cols=COLUMN_DELETE(dynamic_cols, 'color')
+WHERE item_name='Fridge';
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+Microwave NULL
+Thinkpad Laptop black
+DELETE FROM assets
+WHERE item_name in
+(select b.item_name from assets b
+where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+Microwave NULL
+DELETE FROM assets WHERE item_name='Microwave';
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets ORDER BY item_name;
+item_name color
+Fridge NULL
+MariaDB T-shirt blue
+drop table assets ;
+#
+# Test on fulltext columns
+#
+CREATE TABLE ft2(copy TEXT,FULLTEXT(copy));
+INSERT INTO ft2(copy) VALUES
+('MySQL vs MariaDB database'),
+('Oracle vs MariaDB database'),
+('PostgreSQL vs MariaDB database'),
+('MariaDB overview'),
+('Foreign keys'),
+('Primary keys'),
+('Indexes'),
+('Transactions'),
+('Triggers');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+copy
+MySQL vs MariaDB database
+Oracle vs MariaDB database
+PostgreSQL vs MariaDB database
+DELETE FROM ft2 WHERE MATCH(copy) AGAINST('database');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+copy
+drop table ft2;
+set default_storage_engine=MEMORY;
+create table t1 (c1 integer, c2 integer, c3 integer);
+insert into t1(c1,c2,c3)
+values (1,1,1),(1,2,2),(1,3,3),
+(2,1,4),(2,2,5),(2,3,6),
+(2,4,7),(2,5,8);
+insert into t1 select c1+10,c2,c3+10 from t1;
+insert into t1 select c1+20,c2+1,c3+20 from t1;
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create view v1 as select * from t1 where c2=2;
+Test without any index
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+affected rows: 2
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1)
+delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using temporary; Using filesort
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+Test with an index
+create index t1_c2 on t1 (c2,c1);
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Operation failed
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+affected rows: 2
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Operation failed
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Operation failed
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 32
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c3 2 FirstMatch(t1)
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c3 2 FirstMatch(t1)
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL t1_c2 NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY a ALL t1_c2 NULL NULL NULL 32 Using where; FirstMatch(t1)
+delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using temporary; Using filesort
+1 PRIMARY a ALL t1_c2 NULL NULL NULL 32 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a ALL t1_c2 NULL NULL NULL 32 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 2
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 2
+delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Operation failed
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL t1_c2 NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a ALL t1_c2 NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Operation failed
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ref t1_c2 t1_c2 10 const,const #
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 10 const,const # Using where
+3 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func # Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL # Using where
+delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+Test with a primary key
+drop index t1_c2 on t1;
+alter table t1 add primary key (c3);
+#
+# Delete with value from subquery on the same table
+#
+create table tmp as select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+explain select * from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1
+explain delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1
+delete from t1 where c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 4
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+explain select * from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+delete from t1 where c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
+affected rows: 2
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 4 7
+2 5 8
+21 2 21
+21 4 23
+22 2 24
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 4 33
+32 2 34
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with order by
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+explain select * from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+explain delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 32
+delete from t1 where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with IN predicand over the updated table in WHERE
+#
+create table tmp as select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+explain select * from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c1 1 Using where; End temporary
+explain delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c1 1 Using where; End temporary
+delete from t1 where c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 3 3
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit - can be deleted
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 Using where; End temporary
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY a ALL NULL NULL NULL NULL 32 Using where; Start temporary
+1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.a.c2 1 Using where; End temporary
+delete from t1 where c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete with a limit and an order by
+#
+create table tmp as select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+explain select * from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 32 Using filesort
+1 PRIMARY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using where
+explain delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where; Using filesort
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using where
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete: 2 execution of PS
+#
+prepare create_tmp_stmt from
+"create table tmp as select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+prepare delete_t1_stmt from
+"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=5;
+execute create_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 6 38
+prepare insert_tmp_stmt from
+"insert into tmp(c1,c2,c3) select * from t1
+ where c2=(select a.c3 from t1 a where a.c3 = ?)";
+set @a:=2;
+execute insert_tmp_stmt using @a;
+execute delete_t1_stmt using @a;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+2 1 4
+2 3 6
+2 4 7
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+drop table tmp;
+#
+# Delete in stored procedure
+#
+create procedure sp()
+begin
+delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+end
+//
+create table tmp as select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+insert into tmp(c1,c2,c3) select * from t1
+where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 1;
+CALL sp;
+select * from t1;
+c1 c2 c3
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop procedure sp;
+drop table tmp;
+#
+# Delete in stored function
+#
+create function f1(IN a INT) returns int
+begin
+delete from t1 where c3 < a order by c3 limit 1;
+return 1;
+end;//
+set @a:=7;
+create table tmp as select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
+order by c3 limit 1;
+select f1(@a);
+f1(@a)
+1
+select * from t1;
+c1 c2 c3
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop function f1;
+drop table tmp;
+#
+# Delete in trigger
+#
+create table t2 (c1 integer);
+insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
+CREATE TABLE cnt(del integer);
+INSERT INTO cnt VALUES(0);
+CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
+UPDATE cnt SET del=del+1;
+CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
+DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
+CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
+DELETE FROM t1 WHERE c2>=3;
+affected rows: 20
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+11 1 11
+11 2 12
+12 1 14
+12 2 15
+2 1 4
+2 2 5
+21 2 21
+22 2 24
+31 2 31
+32 2 34
+SELECT * FROM t2;
+c1
+SELECT * FROM cnt;
+del
+20
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+DROP TRIGGER tr1;
+DROP TRIGGER tr2;
+drop table t2, cnt, tmp;
+#
+Delete with a reference to view in subquery
+#
+create table tmp as select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+explain select * from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+explain delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 32 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 32 Using where
+delete from t1 where t1.c2 in ( select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+select * from t1;
+c1 c2 c3
+1 1 1
+1 3 3
+11 1 11
+11 3 13
+12 1 14
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 3 22
+21 4 23
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 3 32
+31 4 33
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+explain select * from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+explain delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 32 Using where
+delete from v1 where v1.c1 in
+(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
+affected rows: 0
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+#
+# Delete from view using reference
+# to the same view in subquery
+#
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create table tmp as select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func #
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
+explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL # Using where
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 #
+delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
+and c1 = 2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+select * from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+11 1 11
+11 2 12
+11 3 13
+12 1 14
+12 2 15
+12 3 16
+12 4 17
+12 5 18
+2 1 4
+2 3 6
+2 4 7
+2 5 8
+21 2 21
+21 3 22
+21 4 23
+22 2 24
+22 3 25
+22 4 26
+22 5 27
+22 6 28
+31 2 31
+31 3 32
+31 4 33
+32 2 34
+32 3 35
+32 4 36
+32 5 37
+32 6 38
+insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
+drop table tmp;
+drop view v1;
+drop table t1;
+set @@default_storage_engine=@save_default_engine;
+#
+# End of 11.0 tests
+#
diff --git a/mysql-test/main/delete_use_source_engines.test b/mysql-test/main/delete_use_source_engines.test
new file mode 100644
index 0000000..4938523
--- /dev/null
+++ b/mysql-test/main/delete_use_source_engines.test
@@ -0,0 +1,22 @@
+--source include/have_innodb.inc
+
+set @save_default_engine=@@default_storage_engine;
+
+set global innodb_stats_persistent=1;
+set default_storage_engine=InnoDB;
+--source include/delete_use_source.inc
+
+set default_storage_engine=Aria;
+--source include/delete_use_source.inc
+
+set default_storage_engine=MyISAM;
+--source include/delete_use_source.inc
+
+set default_storage_engine=MEMORY;
+--source include/delete_use_source_memory.inc
+
+set @@default_storage_engine=@save_default_engine;
+
+--echo #
+--echo # End of 11.0 tests
+--echo #
diff --git a/mysql-test/main/update_single_to_multi.result b/mysql-test/main/update_single_to_multi.result
index d020fd1..bc988b5 100644
--- a/mysql-test/main/update_single_to_multi.result
+++ b/mysql-test/main/update_single_to_multi.result
@@ -2351,4 +2351,543 @@ o_orderkey o_totalprice
3139 40975.96
4903 34363.63
5607 24660.06
+# LooseScan
+# =========
+set @tmp_optimizer_switch= @@optimizer_switch;
+set optimizer_switch='materialization=off';
+create index i_l_sup_part on lineitem(l_suppkey, l_partkey);
+create index i_ps_sup_part on partsupp(ps_suppkey, ps_partkey);
+analyze table lineitem;
+Table Op Msg_type Msg_text
+dbt3_s001.lineitem analyze status Engine-independent statistics collected
+dbt3_s001.lineitem analyze status Table is already up to date
+analyze table partsupp;
+Table Op Msg_type Msg_text
+dbt3_s001.partsupp analyze status Engine-independent statistics collected
+dbt3_s001.partsupp analyze status Table is already up to date
+explain
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY partsupp index PRIMARY,i_ps_partkey,i_ps_suppkey,i_ps_sup_part i_ps_sup_part 8 NULL 700 Using where; Using index; LooseScan
+1 PRIMARY lineitem ref i_l_suppkey,i_l_sup_part i_l_suppkey 5 dbt3_s001.partsupp.ps_suppkey 600 Using index
+explain format=json
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+EXPLAIN
+{
+ "query_block": {
+ "select_id": 1,
+ "nested_loop": [
+ {
+ "table": {
+ "table_name": "partsupp",
+ "access_type": "index",
+ "possible_keys": [
+ "PRIMARY",
+ "i_ps_partkey",
+ "i_ps_suppkey",
+ "i_ps_sup_part"
+ ],
+ "key": "i_ps_sup_part",
+ "key_length": "8",
+ "used_key_parts": ["ps_suppkey", "ps_partkey"],
+ "rows": 700,
+ "filtered": 1.428571463,
+ "attached_condition": "partsupp.ps_partkey in (1,2,3)",
+ "using_index": true,
+ "loose_scan": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey", "i_l_sup_part"],
+ "key": "i_l_suppkey",
+ "key_length": "5",
+ "used_key_parts": ["l_suppkey"],
+ "ref": ["dbt3_s001.partsupp.ps_suppkey"],
+ "rows": 600,
+ "filtered": 100,
+ "using_index": true
+ }
+ }
+ ]
+ }
+}
+select count(*) from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+count(*)
+5373
+select 5373 as count, 136458704.22 as old_sum;
+count old_sum
+5373 136458704.22
+explain
+update lineitem set l_extendedprice=l_extendedprice+10 where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY partsupp index PRIMARY,i_ps_partkey,i_ps_suppkey,i_ps_sup_part i_ps_sup_part 8 NULL 700 Using where; Using index; LooseScan
+1 PRIMARY lineitem ref i_l_suppkey,i_l_sup_part i_l_suppkey 5 dbt3_s001.partsupp.ps_suppkey 600
+explain format=json
+update lineitem set l_extendedprice=l_extendedprice+10 where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+EXPLAIN
+{
+ "query_block": {
+ "select_id": 1,
+ "nested_loop": [
+ {
+ "table": {
+ "table_name": "partsupp",
+ "access_type": "index",
+ "possible_keys": [
+ "PRIMARY",
+ "i_ps_partkey",
+ "i_ps_suppkey",
+ "i_ps_sup_part"
+ ],
+ "key": "i_ps_sup_part",
+ "key_length": "8",
+ "used_key_parts": ["ps_suppkey", "ps_partkey"],
+ "rows": 700,
+ "filtered": 1.428571463,
+ "attached_condition": "partsupp.ps_partkey in (1,2,3)",
+ "using_index": true,
+ "loose_scan": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey", "i_l_sup_part"],
+ "key": "i_l_suppkey",
+ "key_length": "5",
+ "used_key_parts": ["l_suppkey"],
+ "ref": ["dbt3_s001.partsupp.ps_suppkey"],
+ "rows": 600,
+ "filtered": 100
+ }
+ }
+ ]
+ }
+}
+update lineitem set l_extendedprice=l_extendedprice+10 where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+select ROUND(sum(l_extendedprice),2), 5373 as count,
+(136458704.22+10*5373) as 'old_sum+10*count'
+ from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+ROUND(sum(l_extendedprice),2) count old_sum+10*count
+136512434.22 5373 136512434.22
+update lineitem set l_extendedprice=l_extendedprice-10 where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+select ROUND(sum(l_extendedprice),2), 5373 as count,
+136458704.22 as old_sum from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+ROUND(sum(l_extendedprice),2) count old_sum
+136458704.22 5373 136458704.22
+# LooseScan PS
+# ============
+prepare stmt from "
+update lineitem set l_extendedprice=l_extendedprice+? where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+";
+select 5373 as count, 136458704.22 as old_sum;
+count old_sum
+5373 136458704.22
+set @a1=20;
+execute stmt using @a1;
+select ROUND(sum(l_extendedprice),2), 5373 as count,
+(136458704.22+20*5373) as 'old_sum+20*count'
+ from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+ROUND(sum(l_extendedprice),2) count old_sum+20*count
+136566164.22 5373 136566164.22
+set @a2=10;
+execute stmt using @a2;
+select ROUND(sum(l_extendedprice),2), 5373 as count,
+(136458704.22+30*5373) as 'old_sum+30*count'
+ from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+ROUND(sum(l_extendedprice),2) count old_sum+30*count
+136619894.22 5373 136619894.22
+execute stmt using -(@a1+@a2);
+select ROUND(sum(l_extendedprice),2), 5373 as count,
+136458704.22 as old_sum from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+ROUND(sum(l_extendedprice),2) count old_sum
+136458704.22 5373 136458704.22
+deallocate prepare stmt;
+# LooseScan SP
+# ============
+create procedure p(d int)
+update lineitem set l_extendedprice=l_extendedprice+d where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+select 5373 as count, 136458704.22 as old_sum;
+count old_sum
+5373 136458704.22
+call p(10);
+select ROUND(sum(l_extendedprice),2), 5373 as count,
+(136458704.22+10*5373) as 'old_sum+10*count'
+ from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+ROUND(sum(l_extendedprice),2) count old_sum+10*count
+136512434.22 5373 136512434.22
+call p(20);
+select ROUND(sum(l_extendedprice),2), 5373 as count,
+(136458704.22+30*5373) as 'old_sum+30*count'
+ from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+ROUND(sum(l_extendedprice),2) count old_sum+30*count
+136619894.22 5373 136619894.22
+call p(-(10+20));
+select ROUND(sum(l_extendedprice),2), 5373 as count,
+136458704.22 as old_sum from lineitem where l_suppkey in
+(select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+ROUND(sum(l_extendedprice),2) count old_sum
+136458704.22 5373 136458704.22
+drop procedure p;
+set @@optimizer_switch=@tmp_optimizer_switch;
+drop index i_l_sup_part on lineitem;
+drop index i_ps_sup_part on partsupp;
+# DuplicateWeedout
+# ================
+set @tmp_optimizer_switch= @@optimizer_switch;
+set optimizer_switch='materialization=off';
+analyze table lineitem;
+Table Op Msg_type Msg_text
+dbt3_s001.lineitem analyze status Engine-independent statistics collected
+dbt3_s001.lineitem analyze status Table is already up to date
+analyze table orders;
+Table Op Msg_type Msg_text
+dbt3_s001.orders analyze status Engine-independent statistics collected
+dbt3_s001.orders analyze status OK
+explain
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY supplier range PRIMARY PRIMARY 4 NULL 1 Using where; Using index; Start temporary
+1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 70 Using where
+1 PRIMARY lineitem ref i_l_suppkey_partkey,i_l_partkey i_l_partkey 5 dbt3_s001.partsupp.ps_partkey 30 Using index
+1 PRIMARY lineitem ref i_l_suppkey_partkey,i_l_partkey i_l_partkey 5 dbt3_s001.partsupp.ps_partkey 30 Using index; End temporary
+explain format=json
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+EXPLAIN
+{
+ "query_block": {
+ "select_id": 1,
+ "nested_loop": [
+ {
+ "duplicates_removal": [
+ {
+ "table": {
+ "table_name": "supplier",
+ "access_type": "range",
+ "possible_keys": ["PRIMARY"],
+ "key": "PRIMARY",
+ "key_length": "4",
+ "used_key_parts": ["s_suppkey"],
+ "rows": 1,
+ "filtered": 100,
+ "attached_condition": "supplier.s_suppkey < 2",
+ "using_index": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "partsupp",
+ "access_type": "ref",
+ "possible_keys": ["PRIMARY", "i_ps_partkey", "i_ps_suppkey"],
+ "key": "i_ps_suppkey",
+ "key_length": "4",
+ "used_key_parts": ["ps_suppkey"],
+ "ref": ["dbt3_s001.supplier.s_suppkey"],
+ "rows": 70,
+ "filtered": 100,
+ "attached_condition": "partsupp.ps_partkey is not null"
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey_partkey", "i_l_partkey"],
+ "key": "i_l_partkey",
+ "key_length": "5",
+ "used_key_parts": ["l_partkey"],
+ "ref": ["dbt3_s001.partsupp.ps_partkey"],
+ "rows": 30,
+ "filtered": 100,
+ "using_index": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey_partkey", "i_l_partkey"],
+ "key": "i_l_partkey",
+ "key_length": "5",
+ "used_key_parts": ["l_partkey"],
+ "ref": ["dbt3_s001.partsupp.ps_partkey"],
+ "rows": 30,
+ "filtered": 100,
+ "using_index": true
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
+select count(*) from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+count(*)
+2126
+select 2126 as count, 53473218.20 as old_sum;
+count old_sum
+2126 53473218.20
+explain
+update lineitem set l_extendedprice=l_extendedprice+10 where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY supplier range PRIMARY PRIMARY 4 NULL 1 Using where; Using index; Start temporary
+1 PRIMARY partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 70 Using where
+1 PRIMARY lineitem ref i_l_suppkey_partkey,i_l_partkey i_l_partkey 5 dbt3_s001.partsupp.ps_partkey 30
+1 PRIMARY lineitem ref i_l_suppkey_partkey,i_l_partkey i_l_partkey 5 dbt3_s001.partsupp.ps_partkey 30 Using index; End temporary
+explain format=json
+update lineitem set l_extendedprice=l_extendedprice+10 where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+EXPLAIN
+{
+ "query_block": {
+ "select_id": 1,
+ "nested_loop": [
+ {
+ "duplicates_removal": [
+ {
+ "table": {
+ "table_name": "supplier",
+ "access_type": "range",
+ "possible_keys": ["PRIMARY"],
+ "key": "PRIMARY",
+ "key_length": "4",
+ "used_key_parts": ["s_suppkey"],
+ "rows": 1,
+ "filtered": 100,
+ "attached_condition": "supplier.s_suppkey < 2",
+ "using_index": true
+ }
+ },
+ {
+ "table": {
+ "table_name": "partsupp",
+ "access_type": "ref",
+ "possible_keys": ["PRIMARY", "i_ps_partkey", "i_ps_suppkey"],
+ "key": "i_ps_suppkey",
+ "key_length": "4",
+ "used_key_parts": ["ps_suppkey"],
+ "ref": ["dbt3_s001.supplier.s_suppkey"],
+ "rows": 70,
+ "filtered": 100,
+ "attached_condition": "partsupp.ps_partkey is not null"
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey_partkey", "i_l_partkey"],
+ "key": "i_l_partkey",
+ "key_length": "5",
+ "used_key_parts": ["l_partkey"],
+ "ref": ["dbt3_s001.partsupp.ps_partkey"],
+ "rows": 30,
+ "filtered": 100
+ }
+ },
+ {
+ "table": {
+ "table_name": "lineitem",
+ "access_type": "ref",
+ "possible_keys": ["i_l_suppkey_partkey", "i_l_partkey"],
+ "key": "i_l_partkey",
+ "key_length": "5",
+ "used_key_parts": ["l_partkey"],
+ "ref": ["dbt3_s001.partsupp.ps_partkey"],
+ "rows": 30,
+ "filtered": 100,
+ "using_index": true
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
+update lineitem set l_extendedprice=l_extendedprice+10 where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+select ROUND(sum(l_extendedprice),2), 2126 as count,
+(53473218.20+10*2126) as 'old_sum+10*count'
+ from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+ROUND(sum(l_extendedprice),2) count old_sum+10*count
+53494478.20 2126 53494478.20
+update lineitem set l_extendedprice=l_extendedprice-10 where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+select ROUND(sum(l_extendedprice),2), 2126 as count,
+53473218.20 as old_sum from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+ROUND(sum(l_extendedprice),2) count old_sum
+53473218.20 2126 53473218.20
+# DuplicateWeedout PS
+# ===================
+prepare stmt from "
+update lineitem set l_extendedprice=l_extendedprice+? where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+";
+select 2126 as count, 53473218.20 as old_sum;
+count old_sum
+2126 53473218.20
+set @a1=20;
+execute stmt using @a1;
+select ROUND(sum(l_extendedprice),2), 2126 as count,
+(53473218.20+20*2126) as 'old_sum+20*count'
+ from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+ROUND(sum(l_extendedprice),2) count old_sum+20*count
+53515738.20 2126 53515738.20
+set @a2=10;
+execute stmt using @a2;
+select ROUND(sum(l_extendedprice),2), 2126 as count,
+(53473218.20+30*2126) as 'old_sum+30*count'
+ from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+ROUND(sum(l_extendedprice),2) count old_sum+30*count
+53536998.20 2126 53536998.20
+execute stmt using -(@a1+@a2);
+select ROUND(sum(l_extendedprice),2), 2126 as count,
+53473218.20 as old_sum from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+ROUND(sum(l_extendedprice),2) count old_sum
+53473218.20 2126 53473218.20
+deallocate prepare stmt;
+# DuplicateWeedout SP
+# ===================
+create procedure p(d int)
+update lineitem set l_extendedprice=l_extendedprice+d where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+select 2126 as count, 53473218.20 as old_sum;
+count old_sum
+2126 53473218.20
+call p(10);
+select ROUND(sum(l_extendedprice),2), 2126 as count,
+(53473218.20+10*2126) as 'old_sum+10*count'
+ from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+ROUND(sum(l_extendedprice),2) count old_sum+10*count
+53494478.20 2126 53494478.20
+call p(20);
+select ROUND(sum(l_extendedprice),2), 2126 as count,
+(53473218.20+30*2126) as 'old_sum+30*count'
+ from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+ROUND(sum(l_extendedprice),2) count old_sum+30*count
+53536998.20 2126 53536998.20
+call p(-(10+20));
+select ROUND(sum(l_extendedprice),2), 2126 as count,
+53473218.20 as old_sum from lineitem where l_partkey in (
+select ps_partkey
+from partsupp join lineitem on ps_partkey=l_partkey
+where ps_suppkey in (
+select s_suppkey from supplier where s_suppkey < 2
+)
+);
+ROUND(sum(l_extendedprice),2) count old_sum
+53473218.20 2126 53473218.20
+drop procedure p;
+set @@optimizer_switch=@tmp_optimizer_switch;
DROP DATABASE dbt3_s001;
diff --git a/mysql-test/main/update_single_to_multi.test b/mysql-test/main/update_single_to_multi.test
index bf89a6c..53c01bd 100644
--- a/mysql-test/main/update_single_to_multi.test
+++ b/mysql-test/main/update_single_to_multi.test
@@ -548,4 +548,242 @@ update orders set o_totalprice = o_totalprice+50 where $c11;
eval
select o_orderkey, o_totalprice from orders where $c11;
+--echo # LooseScan
+--echo # =========
+
+set @tmp_optimizer_switch= @@optimizer_switch;
+set optimizer_switch='materialization=off';
+
+create index i_l_sup_part on lineitem(l_suppkey, l_partkey);
+create index i_ps_sup_part on partsupp(ps_suppkey, ps_partkey);
+
+analyze table lineitem;
+analyze table partsupp;
+
+let $c12 = l_suppkey in
+ (select ps_suppkey from partsupp where ps_partkey in (1,2,3));
+
+eval
+explain
+select count(*) from lineitem where $c12;
+eval
+explain format=json
+select count(*) from lineitem where $c12;
+eval
+select count(*) from lineitem where $c12;
+let $l_count =
+ query_get_value('select count(*) as a from lineitem where $c12;', a, 1);
+let $l_old_sum =
+ query_get_value('select ROUND(sum(l_extendedprice),2) as a
+ from lineitem where $c12;', a, 1
+ );
+eval select $l_count as count, $l_old_sum as old_sum;
+
+eval
+explain
+update lineitem set l_extendedprice=l_extendedprice+10 where $c12;
+eval
+explain format=json
+update lineitem set l_extendedprice=l_extendedprice+10 where $c12;
+eval
+update lineitem set l_extendedprice=l_extendedprice+10 where $c12;
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+10*$l_count) as 'old_sum+10*count'
+ from lineitem where $c12;
+
+eval
+update lineitem set l_extendedprice=l_extendedprice-10 where $c12;
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ $l_old_sum as old_sum from lineitem where $c12;
+
+
+--echo # LooseScan PS
+--echo # ============
+
+eval
+prepare stmt from "
+update lineitem set l_extendedprice=l_extendedprice+? where $c12;
+";
+
+let $l_count = query_get_value('select count(*) as a
+ from lineitem where $c12;', a, 1 );
+let $l_old_sum = query_get_value('select ROUND(sum(l_extendedprice),2) as a
+ from lineitem where $c12;', a, 1 );
+eval select $l_count as count, $l_old_sum as old_sum;
+eval
+set @a1=20;
+execute stmt using @a1;
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+20*$l_count) as 'old_sum+20*count'
+ from lineitem where $c12;
+set @a2=10;
+execute stmt using @a2;
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+30*$l_count) as 'old_sum+30*count'
+ from lineitem where $c12;
+execute stmt using -(@a1+@a2);
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ $l_old_sum as old_sum from lineitem where $c12;
+
+deallocate prepare stmt;
+
+--echo # LooseScan SP
+--echo # ============
+
+eval
+create procedure p(d int)
+update lineitem set l_extendedprice=l_extendedprice+d where $c12;
+
+let $l_count = query_get_value('select count(*) as a
+ from lineitem where $c12;', a, 1 );
+let $l_old_sum = query_get_value('select ROUND(sum(l_extendedprice),2) as a
+ from lineitem where $c12;', a, 1 );
+eval select $l_count as count, $l_old_sum as old_sum;
+eval
+call p(10);
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+10*$l_count) as 'old_sum+10*count'
+ from lineitem where $c12;
+call p(20);
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+30*$l_count) as 'old_sum+30*count'
+ from lineitem where $c12;
+call p(-(10+20));
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ $l_old_sum as old_sum from lineitem where $c12;
+
+drop procedure p;
+set @@optimizer_switch=@tmp_optimizer_switch;
+
+drop index i_l_sup_part on lineitem;
+drop index i_ps_sup_part on partsupp;
+
+
+--echo # DuplicateWeedout
+--echo # ================
+
+set @tmp_optimizer_switch= @@optimizer_switch;
+set optimizer_switch='materialization=off';
+
+analyze table lineitem;
+analyze table orders;
+
+let $c13 = l_partkey in (
+ select ps_partkey
+ from partsupp join lineitem on ps_partkey=l_partkey
+ where ps_suppkey in (
+ select s_suppkey from supplier where s_suppkey < 2
+ )
+);
+
+eval
+explain
+select count(*) from lineitem where $c13;
+eval
+explain format=json
+select count(*) from lineitem where $c13;
+eval
+select count(*) from lineitem where $c13;
+
+let $l_count = query_get_value('select count(*) as a
+ from lineitem where $c13;', a, 1 );
+let $l_old_sum = query_get_value('select ROUND(sum(l_extendedprice),2) as a
+ from lineitem where $c13;', a, 1 );
+eval select $l_count as count, $l_old_sum as old_sum;
+
+eval
+explain
+update lineitem set l_extendedprice=l_extendedprice+10 where $c13;
+eval
+explain format=json
+update lineitem set l_extendedprice=l_extendedprice+10 where $c13;
+eval
+update lineitem set l_extendedprice=l_extendedprice+10 where $c13;
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+10*$l_count) as 'old_sum+10*count'
+ from lineitem where $c13;
+
+eval
+update lineitem set l_extendedprice=l_extendedprice-10 where $c13;
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ $l_old_sum as old_sum from lineitem where $c13;
+
+
+--echo # DuplicateWeedout PS
+--echo # ===================
+
+eval
+prepare stmt from "
+update lineitem set l_extendedprice=l_extendedprice+? where $c13;
+";
+
+let $l_count =
+ query_get_value('select count(*) as a
+ from lineitem where $c13;', a, 1 );
+let $l_old_sum =
+ query_get_value('select ROUND(sum(l_extendedprice),2) as a
+ from lineitem where $c13;', a, 1);
+eval select $l_count as count, $l_old_sum as old_sum;
+eval
+set @a1=20;
+execute stmt using @a1;
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+20*$l_count) as 'old_sum+20*count'
+ from lineitem where $c13;
+set @a2=10;
+execute stmt using @a2;
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+30*$l_count) as 'old_sum+30*count'
+ from lineitem where $c13;
+execute stmt using -(@a1+@a2);
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ $l_old_sum as old_sum from lineitem where $c13;
+
+deallocate prepare stmt;
+
+--echo # DuplicateWeedout SP
+--echo # ===================
+
+eval
+create procedure p(d int)
+update lineitem set l_extendedprice=l_extendedprice+d where $c13;
+
+let $l_count = query_get_value('select count(*) as a
+ from lineitem where $c13;', a, 1 );
+let $l_old_sum = query_get_value('select ROUND(sum(l_extendedprice),2) as a
+ from lineitem where $c13;', a, 1 );
+eval select $l_count as count, $l_old_sum as old_sum;
+eval
+call p(10);
+
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+10*$l_count) as 'old_sum+10*count'
+ from lineitem where $c13;
+call p(20);
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ ($l_old_sum+30*$l_count) as 'old_sum+30*count'
+ from lineitem where $c13;
+call p(-(10+20));
+eval
+select ROUND(sum(l_extendedprice),2), $l_count as count,
+ $l_old_sum as old_sum from lineitem where $c13;
+
+drop procedure p;
+set @@optimizer_switch=@tmp_optimizer_switch;
+
DROP DATABASE dbt3_s001;
diff --git a/mysql-test/main/update_use_source.result b/mysql-test/main/update_use_source.result
index e4ba0e9..1e9c839 100644
--- a/mysql-test/main/update_use_source.result
+++ b/mysql-test/main/update_use_source.result
@@ -1,4 +1,17 @@
-create table t1 (old_c1 integer, old_c2 integer,c1 integer, c2 integer, c3 integer) engine=InnoDB STATS_PERSISTENT=0;
+set @save_default_engine=@@default_storage_engine;
+set global innodb_stats_persistent=1;
+set default_storage_engine=InnoDB;
+create table t1 (old_c1 integer,
+old_c2 integer,
+c1 integer,
+c2 integer,
+c3 integer);
+insert into t1 select c1+10,c2,c3+10, NULL, NULL from t1;
+insert into t1 select c1+20,c2+1,c3+20, NULL, NULL from t1;
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
create view v1 as select * from t1 where c2=2;
create trigger trg_t1 before update on t1 for each row
begin
@@ -6,15 +19,14 @@ set new.old_c1=old.c1;
set new.old_c2=old.c2;
end;
/
-insert into t1(c1,c2,c3) values (1,1,1);
-insert into t1(c1,c2,c3) values (1,2,2);
-insert into t1(c1,c2,c3) values (1,3,3);
-insert into t1(c1,c2,c3) values (2,1,4);
-insert into t1(c1,c2,c3) values (2,2,5);
-insert into t1(c1,c2,c3) values (2,3,6);
-insert into t1(c1,c2,c3) values (2,4,7);
-insert into t1(c1,c2,c3) values (2,5,8);
-commit;
+insert into t1(c1,c2,c3)
+values (1,1,1), (1,2,2), (1,3,3),
+(2,1,4), (2,2,5), (2,3,6),
+(2,4,7), (2,5,8);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
select * from t1;
old_c1 old_c2 c1 c2 c3
NULL NULL 1 1 1
@@ -25,18 +37,20 @@ NULL NULL 2 2 5
NULL NULL 2 3 6
NULL NULL 2 4 7
NULL NULL 2 5 8
-Test without any index
+create table tmp as select * from t1;
+# Test without any index
#
-# Update a with value from subquery on the same table, no search clause. ALL access
+# Update with value from subquery on the same table
#
-start transaction;
-update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3);
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->1 1
1->2 2 *
@@ -46,20 +60,26 @@ concat(old_c1,'->',c1) c3 Changed
2->6 6 *
2->7 7 *
2->8 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update with search clause on the same table
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
#
-start transaction;
-update t1
-set c1=10
+explain update t1 set c1=10
where c1 <2
-and exists (select 'X'
- from t1 a
-where a.c1 = t1.c1);
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
affected rows: 3
info: Rows matched: 3 Changed: 3 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->10 1 *
1->10 2 *
@@ -69,20 +89,24 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update via RANGE or INDEX access if an index or a primary key exists
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
#
-explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
2 MATERIALIZED a ALL NULL NULL NULL NULL 8
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
info: Rows matched: 4 Changed: 4 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -92,13 +116,25 @@ NULL 5
2->12 6 *
2->12 7 *
2->12 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
# Update with order by
#
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 order by c2;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -108,18 +144,22 @@ NULL 5
2->12 6 *
2->12 7 *
2->12 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-Update using a view in subquery
+# Update with a reference to view in subquery
#
-start transaction;
-update t1
-set c1=c1 +(select max(a.c2)
-from v1 a
-where a.c1 = t1.c1) ;
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->3 1 *
1->3 2 *
@@ -129,19 +169,22 @@ concat(old_c1,'->',c1) c3 Changed
2->4 6 *
2->4 7 *
2->4 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update throw a view
+# Update view
#
-start transaction;
-update v1
-set c1=c1 + (select max(a.c2)
-from t1 a
-where a.c1 = v1.c1) +10
-where c3 > 3;
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
affected rows: 1
info: Rows matched: 1 Changed: 1 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -151,20 +194,24 @@ NULL 4
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update through a view and using the view in subquery
+# Update view with reference to the same view in subquery
#
-start transaction;
-update v1
-set c1=c1 + 1
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + 1
where c1 <2
-and exists (select 'X'
- from v1 a
-where a.c1 = v1.c1);
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
affected rows: 1
info: Rows matched: 1 Changed: 1 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
1->2 2 *
@@ -174,22 +221,25 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update through a view and using the view in subquery
+# Update view with EXISTS and reference to the same view in subquery
#
-start transaction;
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
update v1
-set c1=(select max(a.c1)+10
-from v1 a
-where a.c1 = v1.c1)
-where c1 <10
-and exists (select 'X'
- from v1 a
-where a.c2 = v1.c2);
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
1->11 2 *
@@ -199,16 +249,18 @@ NULL 4
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update of the index or primary key (c3)
+# Update with IN predicand over the updated table in WHERE
#
-start transaction;
-explain update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 8
1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
-update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
select c3 from t1;
@@ -221,19 +273,22 @@ c3
16
17
18
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# update with a limit
+# Update with a limit
#
-start transaction;
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3)
-limit 2;
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->1 1
1->2 2 *
@@ -243,19 +298,18 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# update with a limit and an order by
+# Update with a limit and an order by
#
-start transaction;
update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3)
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -265,20 +319,26 @@ NULL 5
NULL 6
2->7 7 *
2->8 8 *
-rollback;
-Test with an index on updated columns
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with an index on updated columns
create index t1_c2 on t1 (c2,c1);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
#
-# Update a with value from subquery on the same table, no search clause. ALL access
+# Update with value from subquery on the same table
#
-start transaction;
-update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3);
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->1 1
1->2 2 *
@@ -288,20 +348,26 @@ concat(old_c1,'->',c1) c3 Changed
2->6 6 *
2->7 7 *
2->8 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update with search clause on the same table
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
#
-start transaction;
-update t1
-set c1=10
+explain update t1 set c1=10
where c1 <2
-and exists (select 'X'
- from t1 a
-where a.c1 = t1.c1);
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
affected rows: 3
info: Rows matched: 3 Changed: 3 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->10 1 *
1->10 2 *
@@ -311,20 +377,24 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update via RANGE or INDEX access if an index or a primary key exists
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
#
-explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 2 Using index condition
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 2 Using where; Using index
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
info: Rows matched: 4 Changed: 4 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -334,13 +404,25 @@ NULL 5
2->12 6 *
2->12 7 *
2->12 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
# Update with order by
#
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 order by c2;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -350,18 +432,22 @@ NULL 5
2->12 6 *
2->12 7 *
2->12 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-Update using a view in subquery
+# Update with a reference to view in subquery
#
-start transaction;
-update t1
-set c1=c1 +(select max(a.c2)
-from v1 a
-where a.c1 = t1.c1) ;
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->3 1 *
1->3 2 *
@@ -371,19 +457,22 @@ concat(old_c1,'->',c1) c3 Changed
2->4 6 *
2->4 7 *
2->4 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update throw a view
+# Update view
#
-start transaction;
-update v1
-set c1=c1 + (select max(a.c2)
-from t1 a
-where a.c1 = v1.c1) +10
-where c3 > 3;
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
affected rows: 1
info: Rows matched: 1 Changed: 1 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -393,20 +482,24 @@ NULL 4
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update through a view and using the view in subquery
+# Update view with reference to the same view in subquery
#
-start transaction;
-update v1
-set c1=c1 + 1
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
where c1 <2
-and exists (select 'X'
- from v1 a
-where a.c1 = v1.c1);
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
affected rows: 1
info: Rows matched: 1 Changed: 1 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
1->2 2 *
@@ -416,22 +509,25 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update through a view and using the view in subquery
+# Update view with EXISTS and reference to the same view in subquery
#
-start transaction;
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
update v1
-set c1=(select max(a.c1)+10
-from v1 a
-where a.c1 = v1.c1)
-where c1 <10
-and exists (select 'X'
- from v1 a
-where a.c2 = v1.c2);
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
1->11 2 *
@@ -441,16 +537,18 @@ NULL 4
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update of the index or primary key (c3)
+# Update with IN predicand over the updated table in WHERE
#
-start transaction;
-explain update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
-update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
select c3 from t1;
@@ -463,19 +561,22 @@ c3
16
17
18
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# update with a limit
+# Update with a limit
#
-start transaction;
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3)
-limit 2;
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->1 1
1->2 2 *
@@ -485,19 +586,18 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# update with a limit and an order by
+# Update with a limit and an order by
#
-start transaction;
update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3)
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -507,20 +607,26 @@ NULL 5
NULL 6
2->7 7 *
2->8 8 *
-rollback;
-Test with an index on updated columns
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with an index on updated columns
create index t1_c3 on t1 (c3);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
#
-# Update a with value from subquery on the same table, no search clause. ALL access
+# Update with value from subquery on the same table
#
-start transaction;
-update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3);
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ref t1_c3 t1_c3 5 test.t1.c3 1 Using index
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->1 1
1->2 2 *
@@ -530,20 +636,26 @@ concat(old_c1,'->',c1) c3 Changed
2->6 6 *
2->7 7 *
2->8 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update with search clause on the same table
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
#
-start transaction;
-update t1
-set c1=10
+explain update t1 set c1=10
where c1 <2
-and exists (select 'X'
- from t1 a
-where a.c1 = t1.c1);
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
affected rows: 3
info: Rows matched: 3 Changed: 3 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->10 1 *
1->10 2 *
@@ -553,20 +665,24 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update via RANGE or INDEX access if an index or a primary key exists
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
#
-explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 2 Using index condition
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 2 Using where; Using index
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
info: Rows matched: 4 Changed: 4 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -576,13 +692,25 @@ NULL 5
2->12 6 *
2->12 7 *
2->12 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
# Update with order by
#
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 order by c2;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -592,18 +720,22 @@ NULL 5
2->12 6 *
2->12 7 *
2->12 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-Update using a view in subquery
+# Update with a reference to view in subquery
#
-start transaction;
-update t1
-set c1=c1 +(select max(a.c2)
-from v1 a
-where a.c1 = t1.c1) ;
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->3 1 *
1->3 2 *
@@ -613,19 +745,22 @@ concat(old_c1,'->',c1) c3 Changed
2->4 6 *
2->4 7 *
2->4 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update throw a view
+# Update view
#
-start transaction;
-update v1
-set c1=c1 + (select max(a.c2)
-from t1 a
-where a.c1 = v1.c1) +10
-where c3 > 3;
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2,t1_c3 t1_c2 5 const 2 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
affected rows: 1
info: Rows matched: 1 Changed: 1 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -635,20 +770,24 @@ NULL 4
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update through a view and using the view in subquery
+# Update view with reference to the same view in subquery
#
-start transaction;
-update v1
-set c1=c1 + 1
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
where c1 <2
-and exists (select 'X'
- from v1 a
-where a.c1 = v1.c1);
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
affected rows: 1
info: Rows matched: 1 Changed: 1 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
1->2 2 *
@@ -658,22 +797,25 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update through a view and using the view in subquery
+# Update view with EXISTS and reference to the same view in subquery
#
-start transaction;
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
update v1
-set c1=(select max(a.c1)+10
-from v1 a
-where a.c1 = v1.c1)
-where c1 <10
-and exists (select 'X'
- from v1 a
-where a.c2 = v1.c2);
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
1->11 2 *
@@ -683,16 +825,18 @@ NULL 4
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update of the index or primary key (c3)
+# Update with IN predicand over the updated table in WHERE
#
-start transaction;
-explain update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
-update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
select c3 from t1;
@@ -705,19 +849,22 @@ c3
16
17
18
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# update with a limit
+# Update with a limit
#
-start transaction;
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ref t1_c3 t1_c3 5 test.t1.c3 1 Using index
update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3)
-limit 2;
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->1 1
1->2 2 *
@@ -727,19 +874,18 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# update with a limit and an order by
+# Update with a limit and an order by
#
-start transaction;
update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3)
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -749,21 +895,27 @@ NULL 5
NULL 6
2->7 7 *
2->8 8 *
-rollback;
-Test with a primary key on updated columns
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with a primary key on updated columns
drop index t1_c3 on t1;
alter table t1 add primary key (c3);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
#
-# Update a with value from subquery on the same table, no search clause. ALL access
+# Update with value from subquery on the same table
#
-start transaction;
-update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3);
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->1 1
1->2 2 *
@@ -773,20 +925,26 @@ concat(old_c1,'->',c1) c3 Changed
2->6 6 *
2->7 7 *
2->8 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update with search clause on the same table
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
#
-start transaction;
-update t1
-set c1=10
+explain update t1 set c1=10
where c1 <2
-and exists (select 'X'
- from t1 a
-where a.c1 = t1.c1);
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
affected rows: 3
info: Rows matched: 3 Changed: 3 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->10 1 *
1->10 2 *
@@ -796,20 +954,24 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update via RANGE or INDEX access if an index or a primary key exists
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
#
-explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
id select_type table type possible_keys key key_len ref rows Extra
-1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 2 Using index condition
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
-2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 2 Using where; Using index
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
info: Rows matched: 4 Changed: 4 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -819,13 +981,25 @@ NULL 5
2->12 6 *
2->12 7 *
2->12 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
# Update with order by
#
-start transaction;
-update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 order by c2;
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -835,18 +1009,22 @@ NULL 5
2->12 6 *
2->12 7 *
2->12 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-Update using a view in subquery
+# Update with a reference to view in subquery
#
-start transaction;
-update t1
-set c1=c1 +(select max(a.c2)
-from v1 a
-where a.c1 = t1.c1) ;
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->3 1 *
1->3 2 *
@@ -856,19 +1034,22 @@ concat(old_c1,'->',c1) c3 Changed
2->4 6 *
2->4 7 *
2->4 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update throw a view
+# Update view
#
-start transaction;
-update v1
-set c1=c1 + (select max(a.c2)
-from t1 a
-where a.c1 = v1.c1) +10
-where c3 > 3;
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range PRIMARY,t1_c2 PRIMARY 4 NULL 5 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
affected rows: 1
info: Rows matched: 1 Changed: 1 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -878,20 +1059,24 @@ NULL 4
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update through a view and using the view in subquery
+# Update view with reference to the same view in subquery
#
-start transaction;
-update v1
-set c1=c1 + 1
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
where c1 <2
-and exists (select 'X'
- from v1 a
-where a.c1 = v1.c1);
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
affected rows: 1
info: Rows matched: 1 Changed: 1 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
1->2 2 *
@@ -901,22 +1086,25 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update through a view and using the view in subquery
+# Update view with EXISTS and reference to the same view in subquery
#
-start transaction;
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
update v1
-set c1=(select max(a.c1)+10
-from v1 a
-where a.c1 = v1.c1)
-where c1 <10
-and exists (select 'X'
- from v1 a
-where a.c2 = v1.c2);
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
1->11 2 *
@@ -926,41 +1114,46 @@ NULL 4
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# Update of the index or primary key (c3)
+# Update with IN predicand over the updated table in WHERE
#
-start transaction;
-explain update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
-update t1 set c3=c3+10 where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
affected rows: 8
info: Rows matched: 8 Changed: 8 Warnings: 0
select c3 from t1;
c3
11
-14
12
-15
13
+14
+15
16
17
18
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# update with a limit
+# Update with a limit
#
-start transaction;
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3)
-limit 2;
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
1->1 1
1->2 2 *
@@ -970,19 +1163,18 @@ NULL 5
NULL 6
NULL 7
NULL 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
#
-# update with a limit and an order by
+# Update with a limit and an order by
#
-start transaction;
update t1
-set c1=(select a.c3
-from t1 a
-where a.c3 = t1.c3)
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-select concat(old_c1,'->',c1),c3, case when c1 != old_c1 then '*' else ' ' end "Changed" from t1 ;
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
concat(old_c1,'->',c1) c3 Changed
NULL 1
NULL 2
@@ -992,56 +1184,139 @@ NULL 5
NULL 6
2->7 7 *
2->8 8 *
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
# Update with error "Subquery returns more than 1 row"
update t1 set c2=(select c2 from t1);
ERROR 21000: Subquery returns more than 1 row
-# Update with error "Subquery returns more than 1 row" and order by
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+# Update with error "Subquery returns more than 1 row"
+# and order by
update t1 set c2=(select c2 from t1) order by c3;
ERROR 21000: Subquery returns more than 1 row
-Duplicate value on update a primary key
-start transaction;
-update t1 set c3=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+# Duplicate value on update a primary key
+update t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
ERROR 23000: Duplicate entry '0' for key 'PRIMARY'
-rollback;
-Duplicate value on update a primary key with ignore
-start transaction;
-update ignore t1 set c3=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key with ignore
+update ignore t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
info: Rows matched: 4 Changed: 4 Warnings: 0
-rollback;
-Duplicate value on update a primary key and limit
-start transaction;
-update t1 set c3=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 limit 2;
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key and limit
+update t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 limit 2;
ERROR 23000: Duplicate entry '0' for key 'PRIMARY'
-rollback;
-Duplicate value on update a primary key with ignore and limit
-start transaction;
-update ignore t1 set c3=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 limit 2;
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key with ignore
+# and limit
+update ignore t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 limit 2;
affected rows: 2
info: Rows matched: 2 Changed: 2 Warnings: 0
-rollback;
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
# Update no rows found
-update t1
-set c1=10
-where c1 <2
-and exists (select 'X'
- from t1 a
-where a.c1 = t1.c1 + 10);
+update t1 set c1=10
+where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1 + 10);
affected rows: 0
info: Rows matched: 0 Changed: 0 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
# Update no rows changed
drop trigger trg_t1;
-start transaction;
-update t1
-set c1=c1
-where c1 <2
-and exists (select 'X'
- from t1 a
-where a.c1 = t1.c1);
+update t1 set c1=c1
+where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1);
affected rows: 0
info: Rows matched: 3 Changed: 0 Warnings: 0
-rollback;
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
#
# Check call of after trigger
#
@@ -1054,61 +1329,88 @@ SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
end if;
end;
/
-update t1 set c1=2 where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1);
+update t1 set c1=2
+where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1);
ERROR 45000: in after update trigger on 5
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
#
# Check update with order by and after trigger
#
-update t1 set c1=2 where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1) order by t1.c2;
+update t1 set c1=2
+where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1)
+order by t1.c2, t1.c1;
ERROR 45000: in after update trigger on 5
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
drop view v1;
#
# Check update on view with check option
#
create view v1 as select * from t1 where c2=2 with check option;
-start transaction;
update v1 set c2=3 where c1=1;
ERROR 44000: CHECK OPTION failed `test`.`v1`
-rollback;
-start transaction;
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
update v1 set c2=(select max(c3) from v1) where c1=1;
ERROR 44000: CHECK OPTION failed `test`.`v1`
-rollback;
-start transaction;
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
update v1 set c2=(select min(va.c3) from v1 va), c1=0 where c1=1;
-rollback;
-drop view v1;
-drop table t1;
-#
-# Test with a temporary table
-#
-create temporary table t1 (c1 integer, c2 integer, c3 integer) engine=InnoDb;
-insert into t1(c1,c2,c3) values (1,1,1);
-insert into t1(c1,c2,c3) values (1,2,2);
-insert into t1(c1,c2,c3) values (1,3,3);
-insert into t1(c1,c2,c3) values (2,1,4);
-insert into t1(c1,c2,c3) values (2,2,5);
-insert into t1(c1,c2,c3) values (2,3,6);
-insert into t1(c1,c2,c3) values (2,4,7);
-insert into t1(c1,c2,c3) values (2,5,8);
-start transaction;
-update t1
-set c1=(select a.c2
-from t1 a
-where a.c3 = t1.c3) limit 3;
-affected rows: 2
-info: Rows matched: 3 Changed: 2 Warnings: 0
-select * from t1 ;
+select c1,c2,c3 from t1;
c1 c2 c3
+0 2 2
1 1 1
-2 2 2
-3 3 3
+1 3 3
2 1 4
2 2 5
2 3 6
2 4 7
2 5 8
-rollback;
+truncate table t1;
+insert into t1 select * from tmp;
+drop table tmp;
+drop view v1;
drop table t1;
#
# Test on dynamic columns (blob)
@@ -1117,37 +1419,50 @@ create table assets (
item_name varchar(32) primary key, -- A common attribute for all items
dynamic_cols blob -- Dynamic columns will be stored here
);
-INSERT INTO assets VALUES ('MariaDB T-shirt', COLUMN_CREATE('color', 'blue', 'size', 'XL'));
-INSERT INTO assets VALUES ('Thinkpad Laptop', COLUMN_CREATE('color', 'black', 'price', 500));
-SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color FROM assets;
+INSERT INTO assets VALUES ('MariaDB T-shirt',
+COLUMN_CREATE('color', 'blue', 'size', 'XL'));
+INSERT INTO assets VALUES ('Thinkpad Laptop',
+COLUMN_CREATE('color', 'black', 'price', 500));
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets;
item_name color
MariaDB T-shirt blue
Thinkpad Laptop black
-UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '3 years') WHERE item_name='Thinkpad Laptop';
-SELECT item_name, COLUMN_GET(dynamic_cols, 'warranty' as char) AS color FROM assets;
+UPDATE assets
+SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '3 years')
+WHERE item_name='Thinkpad Laptop';
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
item_name color
MariaDB T-shirt NULL
Thinkpad Laptop 3 years
-UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '4 years')
-WHERE item_name in (select b.item_name
-from assets b
+UPDATE assets
+SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '4 years')
+WHERE item_name in
+(select b.item_name from assets b
where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
-SELECT item_name, COLUMN_GET(dynamic_cols, 'warranty' as char) AS color FROM assets;
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
item_name color
MariaDB T-shirt NULL
Thinkpad Laptop 4 years
-UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', (select COLUMN_GET(b.dynamic_cols, 'color' as char)
+UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty',
+(select COLUMN_GET(b.dynamic_cols, 'color' as char)
from assets b
where assets.item_name = item_name));
-SELECT item_name, COLUMN_GET(dynamic_cols, 'warranty' as char) AS color FROM assets;
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
item_name color
MariaDB T-shirt blue
Thinkpad Laptop black
-drop table assets ;
+drop table assets;
#
# Test on fulltext columns
#
-CREATE TABLE ft2(copy TEXT,FULLTEXT(copy)) ENGINE=MyISAM;
+CREATE TABLE ft2(copy TEXT,FULLTEXT(copy));
INSERT INTO ft2(copy) VALUES
('MySQL vs MariaDB database'),
('Oracle vs MariaDB database'),
@@ -1163,17 +1478,4404 @@ copy
MySQL vs MariaDB database
Oracle vs MariaDB database
PostgreSQL vs MariaDB database
-update ft2 set copy = (select max(concat('mykeyword ',substr(b.copy,1,5))) from ft2 b WHERE MATCH(b.copy) AGAINST('database'))
+update ft2 set copy = (select max(concat('mykeyword ',substr(b.copy,1,5)))
+from ft2 b WHERE MATCH(b.copy) AGAINST('database'))
where MATCH(copy) AGAINST('keys');
SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('mykeyword');
copy
mykeyword Postg
mykeyword Postg
drop table ft2;
+set default_storage_engine=Aria;
+create table t1 (old_c1 integer,
+old_c2 integer,
+c1 integer,
+c2 integer,
+c3 integer);
+insert into t1 select c1+10,c2,c3+10, NULL, NULL from t1;
+insert into t1 select c1+20,c2+1,c3+20, NULL, NULL from t1;
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status Table is already up to date
+create view v1 as select * from t1 where c2=2;
+create trigger trg_t1 before update on t1 for each row
+begin
+set new.old_c1=old.c1;
+set new.old_c2=old.c2;
+end;
+/
+insert into t1(c1,c2,c3)
+values (1,1,1), (1,2,2), (1,3,3),
+(2,1,4), (2,2,5), (2,3,6),
+(2,4,7), (2,5,8);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+select * from t1;
+old_c1 old_c2 c1 c2 c3
+NULL NULL 1 1 1
+NULL NULL 1 2 2
+NULL NULL 1 3 3
+NULL NULL 2 1 4
+NULL NULL 2 2 5
+NULL NULL 2 3 6
+NULL NULL 2 4 7
+NULL NULL 2 5 8
+create table tmp as select * from t1;
+# Test without any index
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with an index on updated columns
+create index t1_c2 on t1 (c2,c1);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 5 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 5 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 5 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 5 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with an index on updated columns
+create index t1_c3 on t1 (c3);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ref t1_c3 t1_c3 5 test.t1.c3 1 Using index
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 5 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 5 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 5 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 5 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2,t1_c3 t1_c2 5 const 2 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ref t1_c3 t1_c3 5 test.t1.c3 1 Using index
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with a primary key on updated columns
+drop index t1_c3 on t1;
+alter table t1 add primary key (c3);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 5 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 5 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 5 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 5 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref PRIMARY,t1_c2 t1_c2 5 const 2 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Update with error "Subquery returns more than 1 row"
+update t1 set c2=(select c2 from t1);
+ERROR 21000: Subquery returns more than 1 row
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+# Update with error "Subquery returns more than 1 row"
+# and order by
+update t1 set c2=(select c2 from t1) order by c3;
+ERROR 21000: Subquery returns more than 1 row
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+# Duplicate value on update a primary key
+update t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+ERROR 23000: Duplicate entry '0' for key 'PRIMARY'
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key with ignore
+update ignore t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key and limit
+update t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 limit 2;
+ERROR 23000: Duplicate entry '0' for key 'PRIMARY'
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key with ignore
+# and limit
+update ignore t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Update no rows found
+update t1 set c1=10
+where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1 + 10);
+affected rows: 0
+info: Rows matched: 0 Changed: 0 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Update no rows changed
+drop trigger trg_t1;
+update t1 set c1=c1
+where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 0
+info: Rows matched: 3 Changed: 0 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Check call of after trigger
+#
+create or replace trigger trg_t2 after update on t1 for each row
+begin
+declare msg varchar(100);
+if (new.c3 = 5) then
+set msg=concat('in after update trigger on ',new.c3);
+SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
+end if;
+end;
+/
+update t1 set c1=2
+where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1);
+ERROR 45000: in after update trigger on 5
+select c1,c2,c3 from t1;
+c1 c2 c3
+2 1 1
+2 1 4
+2 2 2
+2 2 5
+2 3 3
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Check update with order by and after trigger
+#
+update t1 set c1=2
+where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1)
+order by t1.c2, t1.c1;
+ERROR 45000: in after update trigger on 5
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 3 3
+2 1 1
+2 1 4
+2 2 2
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+drop view v1;
+#
+# Check update on view with check option
+#
+create view v1 as select * from t1 where c2=2 with check option;
+update v1 set c2=3 where c1=1;
+ERROR 44000: CHECK OPTION failed `test`.`v1`
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+update v1 set c2=(select max(c3) from v1) where c1=1;
+ERROR 44000: CHECK OPTION failed `test`.`v1`
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+update v1 set c2=(select min(va.c3) from v1 va), c1=0 where c1=1;
+select c1,c2,c3 from t1;
+c1 c2 c3
+0 2 2
+1 1 1
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+drop table tmp;
+drop view v1;
+drop table t1;
+#
+# Test on dynamic columns (blob)
+#
+create table assets (
+item_name varchar(32) primary key, -- A common attribute for all items
+dynamic_cols blob -- Dynamic columns will be stored here
+);
+INSERT INTO assets VALUES ('MariaDB T-shirt',
+COLUMN_CREATE('color', 'blue', 'size', 'XL'));
+INSERT INTO assets VALUES ('Thinkpad Laptop',
+COLUMN_CREATE('color', 'black', 'price', 500));
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets;
+item_name color
+MariaDB T-shirt blue
+Thinkpad Laptop black
+UPDATE assets
+SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '3 years')
+WHERE item_name='Thinkpad Laptop';
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
+item_name color
+MariaDB T-shirt NULL
+Thinkpad Laptop 3 years
+UPDATE assets
+SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '4 years')
+WHERE item_name in
+(select b.item_name from assets b
+where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
+item_name color
+MariaDB T-shirt NULL
+Thinkpad Laptop 4 years
+UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty',
+(select COLUMN_GET(b.dynamic_cols, 'color' as char)
+from assets b
+where assets.item_name = item_name));
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
+item_name color
+MariaDB T-shirt blue
+Thinkpad Laptop black
+drop table assets;
+#
+# Test on fulltext columns
+#
+CREATE TABLE ft2(copy TEXT,FULLTEXT(copy));
+INSERT INTO ft2(copy) VALUES
+('MySQL vs MariaDB database'),
+('Oracle vs MariaDB database'),
+('PostgreSQL vs MariaDB database'),
+('MariaDB overview'),
+('Foreign keys'),
+('Primary keys'),
+('Indexes'),
+('Transactions'),
+('Triggers');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+copy
+MySQL vs MariaDB database
+Oracle vs MariaDB database
+PostgreSQL vs MariaDB database
+update ft2 set copy = (select max(concat('mykeyword ',substr(b.copy,1,5)))
+from ft2 b WHERE MATCH(b.copy) AGAINST('database'))
+where MATCH(copy) AGAINST('keys');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('mykeyword');
+copy
+mykeyword Postg
+mykeyword Postg
+drop table ft2;
+set default_storage_engine=MyISAM;
+create table t1 (old_c1 integer,
+old_c2 integer,
+c1 integer,
+c2 integer,
+c3 integer);
+insert into t1 select c1+10,c2,c3+10, NULL, NULL from t1;
+insert into t1 select c1+20,c2+1,c3+20, NULL, NULL from t1;
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status Table is already up to date
+create view v1 as select * from t1 where c2=2;
+create trigger trg_t1 before update on t1 for each row
+begin
+set new.old_c1=old.c1;
+set new.old_c2=old.c2;
+end;
+/
+insert into t1(c1,c2,c3)
+values (1,1,1), (1,2,2), (1,3,3),
+(2,1,4), (2,2,5), (2,3,6),
+(2,4,7), (2,5,8);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+select * from t1;
+old_c1 old_c2 c1 c2 c3
+NULL NULL 1 1 1
+NULL NULL 1 2 2
+NULL NULL 1 3 3
+NULL NULL 2 1 4
+NULL NULL 2 2 5
+NULL NULL 2 3 6
+NULL NULL 2 4 7
+NULL NULL 2 5 8
+create table tmp as select * from t1;
+# Test without any index
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with an index on updated columns
+create index t1_c2 on t1 (c2,c1);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 4 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 4 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with an index on updated columns
+create index t1_c3 on t1 (c3);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ref t1_c3 t1_c3 5 test.t1.c3 1 Using index
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 4 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 4 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref t1_c2,t1_c3 t1_c2 5 const 2 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ref t1_c3 t1_c3 5 test.t1.c3 1 Using index
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with a primary key on updated columns
+drop index t1_c3 on t1;
+alter table t1 add primary key (c3);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a index NULL t1_c2 10 NULL 8 Using index
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 4 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 4 Using index condition
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a range t1_c2 t1_c2 5 NULL 4 Using where; Using index
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref PRIMARY,t1_c2 t1_c2 5 const 2 Using where
+2 DEPENDENT SUBQUERY a index NULL t1_c2 10 NULL 8 Using where; Using index
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 1 Using index condition; Using where
+2 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 range t1_c2 t1_c2 10 NULL 2 Using index condition
+3 MATERIALIZED t1 ref t1_c2 t1_c2 5 const 2 Using index
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 1 Using index
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 1 Using index; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1 Using index
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Update with error "Subquery returns more than 1 row"
+update t1 set c2=(select c2 from t1);
+ERROR 21000: Subquery returns more than 1 row
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+# Update with error "Subquery returns more than 1 row"
+# and order by
+update t1 set c2=(select c2 from t1) order by c3;
+ERROR 21000: Subquery returns more than 1 row
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+# Duplicate value on update a primary key
+update t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+ERROR 23000: Duplicate entry '0' for key 'PRIMARY'
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key with ignore
+update ignore t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key and limit
+update t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 limit 2;
+ERROR 23000: Duplicate entry '0' for key 'PRIMARY'
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key with ignore
+# and limit
+update ignore t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Update no rows found
+update t1 set c1=10
+where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1 + 10);
+affected rows: 0
+info: Rows matched: 0 Changed: 0 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Update no rows changed
+drop trigger trg_t1;
+update t1 set c1=c1
+where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 0
+info: Rows matched: 3 Changed: 0 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Check call of after trigger
+#
+create or replace trigger trg_t2 after update on t1 for each row
+begin
+declare msg varchar(100);
+if (new.c3 = 5) then
+set msg=concat('in after update trigger on ',new.c3);
+SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
+end if;
+end;
+/
+update t1 set c1=2
+where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1);
+ERROR 45000: in after update trigger on 5
+select c1,c2,c3 from t1;
+c1 c2 c3
+2 1 1
+2 1 4
+2 2 2
+2 2 5
+2 3 3
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Check update with order by and after trigger
+#
+update t1 set c1=2
+where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1)
+order by t1.c2, t1.c1;
+ERROR 45000: in after update trigger on 5
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 3 3
+2 1 1
+2 1 4
+2 2 2
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+drop view v1;
+#
+# Check update on view with check option
+#
+create view v1 as select * from t1 where c2=2 with check option;
+update v1 set c2=3 where c1=1;
+ERROR 44000: CHECK OPTION failed `test`.`v1`
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+update v1 set c2=(select max(c3) from v1) where c1=1;
+ERROR 44000: CHECK OPTION failed `test`.`v1`
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+update v1 set c2=(select min(va.c3) from v1 va), c1=0 where c1=1;
+select c1,c2,c3 from t1;
+c1 c2 c3
+0 2 2
+1 1 1
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+drop table tmp;
+drop view v1;
+drop table t1;
+#
+# Test on dynamic columns (blob)
+#
+create table assets (
+item_name varchar(32) primary key, -- A common attribute for all items
+dynamic_cols blob -- Dynamic columns will be stored here
+);
+INSERT INTO assets VALUES ('MariaDB T-shirt',
+COLUMN_CREATE('color', 'blue', 'size', 'XL'));
+INSERT INTO assets VALUES ('Thinkpad Laptop',
+COLUMN_CREATE('color', 'black', 'price', 500));
+SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color
+FROM assets;
+item_name color
+MariaDB T-shirt blue
+Thinkpad Laptop black
+UPDATE assets
+SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '3 years')
+WHERE item_name='Thinkpad Laptop';
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
+item_name color
+MariaDB T-shirt NULL
+Thinkpad Laptop 3 years
+UPDATE assets
+SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '4 years')
+WHERE item_name in
+(select b.item_name from assets b
+where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
+item_name color
+MariaDB T-shirt NULL
+Thinkpad Laptop 4 years
+UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty',
+(select COLUMN_GET(b.dynamic_cols, 'color' as char)
+from assets b
+where assets.item_name = item_name));
+SELECT item_name,
+COLUMN_GET(dynamic_cols, 'warranty' as char) AS color
+FROM assets;
+item_name color
+MariaDB T-shirt blue
+Thinkpad Laptop black
+drop table assets;
+#
+# Test on fulltext columns
+#
+CREATE TABLE ft2(copy TEXT,FULLTEXT(copy));
+INSERT INTO ft2(copy) VALUES
+('MySQL vs MariaDB database'),
+('Oracle vs MariaDB database'),
+('PostgreSQL vs MariaDB database'),
+('MariaDB overview'),
+('Foreign keys'),
+('Primary keys'),
+('Indexes'),
+('Transactions'),
+('Triggers');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
+copy
+MySQL vs MariaDB database
+Oracle vs MariaDB database
+PostgreSQL vs MariaDB database
+update ft2 set copy = (select max(concat('mykeyword ',substr(b.copy,1,5)))
+from ft2 b WHERE MATCH(b.copy) AGAINST('database'))
+where MATCH(copy) AGAINST('keys');
+SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('mykeyword');
+copy
+mykeyword Postg
+mykeyword Postg
+drop table ft2;
+set default_storage_engine=MEMORY;
+create table t1 (old_c1 integer,
+old_c2 integer,
+c1 integer,
+c2 integer,
+c3 integer);
+insert into t1 select c1+10,c2,c3+10, NULL, NULL from t1;
+insert into t1 select c1+20,c2+1,c3+20, NULL, NULL from t1;
+analyze table t1 persistent for all;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+create view v1 as select * from t1 where c2=2;
+create trigger trg_t1 before update on t1 for each row
+begin
+set new.old_c1=old.c1;
+set new.old_c2=old.c2;
+end;
+/
+insert into t1(c1,c2,c3)
+values (1,1,1), (1,2,2), (1,3,3),
+(2,1,4), (2,2,5), (2,3,6),
+(2,4,7), (2,5,8);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze note The storage engine for the table doesn't support analyze
+select * from t1;
+old_c1 old_c2 c1 c2 c3
+NULL NULL 1 1 1
+NULL NULL 1 2 2
+NULL NULL 1 3 3
+NULL NULL 2 1 4
+NULL NULL 2 2 5
+NULL NULL 2 3 6
+NULL NULL 2 4 7
+NULL NULL 2 5 8
+create table tmp as select * from t1;
+# Test without any index
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+3 MATERIALIZED t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 8 Using where
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+1 PRIMARY a ALL NULL NULL NULL NULL 8 Using where; FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with an index on updated columns
+create index t1_c2 on t1 (c2,c1);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Operation failed
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 2
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func 2 Using where
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+3 MATERIALIZED t1 ALL t1_c2 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 2
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 2 FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with an index on updated columns
+create index t1_c3 on t1 (c3);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Operation failed
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ref t1_c3 t1_c3 5 test.t1.c3 2
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 2
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2,t1_c3 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func 2 Using where
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+3 MATERIALIZED t1 ALL t1_c2 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 2
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 2 FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a ref t1_c3 t1_c3 5 test.t1.c3 2
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Test with a primary key on updated columns
+drop index t1_c3 on t1;
+alter table t1 add primary key (c3);
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status Operation failed
+#
+# Update with value from subquery on the same table
+#
+explain update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1
+update t1 set c1=(select a.c3 from t1 a where a.c3 = t1.c3);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+1->3 3 *
+2->4 4 *
+2->5 5 *
+2->6 6 *
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + possibly sargable condition
+#
+explain update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 8
+update t1 set c1=10
+where c1 <2
+and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 3
+info: Rows matched: 3 Changed: 3 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->10 1 *
+1->10 2 *
+1->10 3 *
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with EXISTS subquery over the updated table
+# in WHERE + non-sargable condition
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with order by
+#
+explain update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where; Using filesort
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
+2 MATERIALIZED a ALL t1_c2 NULL NULL NULL 8
+update t1 set c1=c1+10
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 order by c2;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+1->11 3 *
+NULL 4
+NULL 5
+2->12 6 *
+2->12 7 *
+2->12 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a reference to view in subquery
+#
+explain update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 2
+update t1 set c1=c1 +(select max(a.c2) from v1 a
+where a.c1 = t1.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->3 1 *
+1->3 2 *
+1->3 3 *
+2->4 4 *
+2->4 5 *
+2->4 6 *
+2->4 7 *
+2->4 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view
+#
+explain update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL PRIMARY,t1_c2 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY a ALL NULL NULL NULL NULL 8 Using where
+update v1 set c1=c1 + (select max(a.c2) from t1 a
+where a.c1 = v1.c1) +10 where c3 > 3;
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+2->17 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with reference to the same view in subquery
+#
+explain update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,func 2 Using where
+update v1 set c1=c1 + 1
+where c1 <2
+and exists (select 'X' from v1 a where a.c1 = v1.c1);
+affected rows: 1
+info: Rows matched: 1 Changed: 1 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update view with EXISTS and reference to the same view in subquery
+#
+explain update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+3 MATERIALIZED t1 ALL t1_c2 NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t1 ref t1_c2 t1_c2 10 const,test.t1.c1 2
+update v1
+set c1=(select max(a.c1)+10 from v1 a where a.c1 = v1.c1)
+where c1 <10 and exists (select 'X' from v1 a where a.c2 = v1.c2);
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+1->11 2 *
+NULL 3
+NULL 4
+2->12 5 *
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with IN predicand over the updated table in WHERE
+#
+explain update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL t1_c2 NULL NULL NULL 8 Using where
+1 PRIMARY a ref t1_c2 t1_c2 10 test.t1.c2,test.t1.c1 2 FirstMatch(t1)
+update t1 set c3=c3+10
+where c2 in (select distinct a.c2 from t1 a where t1.c1=a.c1);
+affected rows: 8
+info: Rows matched: 8 Changed: 8 Warnings: 0
+select c3 from t1;
+c3
+11
+12
+13
+14
+15
+16
+17
+18
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit
+#
+explain update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY a eq_ref PRIMARY PRIMARY 4 test.t1.c3 1
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3) limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+1->1 1
+1->2 2 *
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+NULL 7
+NULL 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Update with a limit and an order by
+#
+update t1
+set c1=(select a.c3 from t1 a where a.c3 = t1.c3)
+order by c3 desc limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select concat(old_c1,'->',c1),c3,
+case when c1 != old_c1 then '*' else ' ' end "Changed" from t1;
+concat(old_c1,'->',c1) c3 Changed
+NULL 1
+NULL 2
+NULL 3
+NULL 4
+NULL 5
+NULL 6
+2->7 7 *
+2->8 8 *
+truncate table t1;
+insert into t1 select * from tmp;
+# Update with error "Subquery returns more than 1 row"
+update t1 set c2=(select c2 from t1);
+ERROR 21000: Subquery returns more than 1 row
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+# Update with error "Subquery returns more than 1 row"
+# and order by
+update t1 set c2=(select c2 from t1) order by c3;
+ERROR 21000: Subquery returns more than 1 row
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+# Duplicate value on update a primary key
+update t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+ERROR 23000: Duplicate entry '0' for key 'PRIMARY'
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key with ignore
+update ignore t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
+affected rows: 4
+info: Rows matched: 4 Changed: 4 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key and limit
+update t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 limit 2;
+ERROR 23000: Duplicate entry '0' for key 'PRIMARY'
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Duplicate value on update a primary key with ignore
+# and limit
+update ignore t1 set c3=0
+where exists (select 'X' from t1 a where a.c2 = t1.c2)
+and c2 >= 3 limit 2;
+affected rows: 2
+info: Rows matched: 2 Changed: 2 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 0
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Update no rows found
+update t1 set c1=10
+where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1 + 10);
+affected rows: 0
+info: Rows matched: 0 Changed: 0 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+# Update no rows changed
+drop trigger trg_t1;
+update t1 set c1=c1
+where c1 <2 and exists (select 'X' from t1 a where a.c1 = t1.c1);
+affected rows: 0
+info: Rows matched: 3 Changed: 0 Warnings: 0
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Check call of after trigger
+#
+create or replace trigger trg_t2 after update on t1 for each row
+begin
+declare msg varchar(100);
+if (new.c3 = 5) then
+set msg=concat('in after update trigger on ',new.c3);
+SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
+end if;
+end;
+/
+update t1 set c1=2
+where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1);
+ERROR 45000: in after update trigger on 5
+select c1,c2,c3 from t1;
+c1 c2 c3
+2 1 1
+2 1 4
+2 2 2
+2 2 5
+2 3 3
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+#
+# Check update with order by and after trigger
+#
+update t1 set c1=2
+where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1)
+order by t1.c2, t1.c1;
+ERROR 45000: in after update trigger on 5
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 3 3
+2 1 1
+2 1 4
+2 2 2
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+drop view v1;
+#
+# Check update on view with check option
+#
+create view v1 as select * from t1 where c2=2 with check option;
+update v1 set c2=3 where c1=1;
+ERROR 44000: CHECK OPTION failed `test`.`v1`
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+update v1 set c2=(select max(c3) from v1) where c1=1;
+ERROR 44000: CHECK OPTION failed `test`.`v1`
+select c1,c2,c3 from t1;
+c1 c2 c3
+1 1 1
+1 2 2
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+update v1 set c2=(select min(va.c3) from v1 va), c1=0 where c1=1;
+select c1,c2,c3 from t1;
+c1 c2 c3
+0 2 2
+1 1 1
+1 3 3
+2 1 4
+2 2 5
+2 3 6
+2 4 7
+2 5 8
+truncate table t1;
+insert into t1 select * from tmp;
+drop table tmp;
+drop view v1;
+drop table t1;
+set @@default_storage_engine=@save_default_engine;
#
# Test with MyISAM
#
-create table t1 (old_c1 integer, old_c2 integer,c1 integer, c2 integer, c3 integer) engine=MyISAM;
+create table t1 (old_c1 integer,
+old_c2 integer,
+c1 integer,
+c2 integer,
+c3 integer) engine=MyISAM;
insert t1 (c1,c2,c3) select 0,seq,seq%10 from seq_1_to_500;
insert t1 (c1,c2,c3) select 1,seq,seq%10 from seq_1_to_400;
insert t1 (c1,c2,c3) select 2,seq,seq%10 from seq_1_to_300;
@@ -1193,11 +5895,13 @@ count(*)
140
drop table t1;
#
-# Test error on multi_update conversion on view with order by or limit
+# Test error on multi_update conversion on view
+# with order by or limit
#
create table t1 (c1 integer) engine=InnoDb;
create table t2 (c1 integer) engine=InnoDb;
-create view v1 as select t1.c1 as "t1c1" ,t2.c1 as "t2c1" from t1,t2 where t1.c1=t2.c1;
+create view v1 as select t1.c1 as "t1c1" ,t2.c1 as "t2c1"
+ from t1,t2 where t1.c1=t2.c1;
update v1 set t1c1=2 order by 1;
update v1 set t1c1=2 limit 1;
drop table t1;
diff --git a/mysql-test/main/update_use_source.test b/mysql-test/main/update_use_source.test
index 3c32a25..65dfa8b 100644
--- a/mysql-test/main/update_use_source.test
+++ b/mysql-test/main/update_use_source.test
@@ -2,221 +2,35 @@
--source include/have_innodb.inc
--source include/no_valgrind_without_big.inc
-create table t1 (old_c1 integer, old_c2 integer,c1 integer, c2 integer, c3 integer) engine=InnoDB STATS_PERSISTENT=0;
-create view v1 as select * from t1 where c2=2;
-delimiter /;
-create trigger trg_t1 before update on t1 for each row
-begin
- set new.old_c1=old.c1;
- set new.old_c2=old.c2;
-end;
-/
-delimiter ;/
+set @save_default_engine=@@default_storage_engine;
-insert into t1(c1,c2,c3) values (1,1,1);
-insert into t1(c1,c2,c3) values (1,2,2);
-insert into t1(c1,c2,c3) values (1,3,3);
-insert into t1(c1,c2,c3) values (2,1,4);
-insert into t1(c1,c2,c3) values (2,2,5);
-insert into t1(c1,c2,c3) values (2,3,6);
-insert into t1(c1,c2,c3) values (2,4,7);
-insert into t1(c1,c2,c3) values (2,5,8);
-
-commit;
-select * from t1;
-
---echo Test without any index
+set global innodb_stats_persistent=1;
+set default_storage_engine=InnoDB;
--source include/update_use_source.inc
+--source include/update_use_source_ext.inc
---echo Test with an index on updated columns
-create index t1_c2 on t1 (c2,c1);
+set default_storage_engine=Aria;
--source include/update_use_source.inc
+--source include/update_use_source_ext.inc
---echo Test with an index on updated columns
-create index t1_c3 on t1 (c3);
+set default_storage_engine=MyISAM;
--source include/update_use_source.inc
+--source include/update_use_source_ext.inc
---echo Test with a primary key on updated columns
-drop index t1_c3 on t1;
-alter table t1 add primary key (c3);
+set default_storage_engine=MEMORY;
--source include/update_use_source.inc
---echo # Update with error "Subquery returns more than 1 row"
---error ER_SUBQUERY_NO_1_ROW
-update t1 set c2=(select c2 from t1);
-
---echo # Update with error "Subquery returns more than 1 row" and order by
---error ER_SUBQUERY_NO_1_ROW
-update t1 set c2=(select c2 from t1) order by c3;
-
--- echo Duplicate value on update a primary key
-start transaction;
---error ER_DUP_ENTRY
-update t1 set c3=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
-rollback;
-
--- echo Duplicate value on update a primary key with ignore
-start transaction;
---enable_info ONCE
-update ignore t1 set c3=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
-rollback;
-
--- echo Duplicate value on update a primary key and limit
-start transaction;
---error ER_DUP_ENTRY
-update t1 set c3=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 limit 2;
-rollback;
-
--- echo Duplicate value on update a primary key with ignore and limit
-start transaction;
---enable_info ONCE
-update ignore t1 set c3=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3 limit 2;
-rollback;
-
---echo # Update no rows found
---enable_info ONCE
-update t1
- set c1=10
- where c1 <2
- and exists (select 'X'
- from t1 a
- where a.c1 = t1.c1 + 10);
-
---echo # Update no rows changed
-drop trigger trg_t1;
-start transaction;
---enable_info ONCE
-update t1
- set c1=c1
- where c1 <2
- and exists (select 'X'
- from t1 a
- where a.c1 = t1.c1);
-rollback;
-
---echo #
---echo # Check call of after trigger
---echo #
-
-delimiter /;
-create or replace trigger trg_t2 after update on t1 for each row
-begin
- declare msg varchar(100);
- if (new.c3 = 5) then
- set msg=concat('in after update trigger on ',new.c3);
- SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
- end if;
-end;
-/
-delimiter ;/
---error 1644
-update t1 set c1=2 where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1);
-
---echo #
---echo # Check update with order by and after trigger
---echo #
-
---error 1644
-update t1 set c1=2 where c3 in (select distinct a.c3 from t1 a where a.c1=t1.c1) order by t1.c2;
-
-drop view v1;
---echo #
---echo # Check update on view with check option
---echo #
-
-create view v1 as select * from t1 where c2=2 with check option;
-
-start transaction;
--- error 1369
-update v1 set c2=3 where c1=1;
-rollback;
-
-start transaction;
--- error 1369
-update v1 set c2=(select max(c3) from v1) where c1=1;
-rollback;
-
-start transaction;
-update v1 set c2=(select min(va.c3) from v1 va), c1=0 where c1=1;
-rollback;
-
-drop view v1;
-drop table t1;
-
---echo #
---echo # Test with a temporary table
---echo #
-
-create temporary table t1 (c1 integer, c2 integer, c3 integer) engine=InnoDb;
-insert into t1(c1,c2,c3) values (1,1,1);
-insert into t1(c1,c2,c3) values (1,2,2);
-insert into t1(c1,c2,c3) values (1,3,3);
-insert into t1(c1,c2,c3) values (2,1,4);
-insert into t1(c1,c2,c3) values (2,2,5);
-insert into t1(c1,c2,c3) values (2,3,6);
-insert into t1(c1,c2,c3) values (2,4,7);
-insert into t1(c1,c2,c3) values (2,5,8);
-
-start transaction;
---enable_info ONCE
-update t1
- set c1=(select a.c2
- from t1 a
- where a.c3 = t1.c3) limit 3;
-select * from t1 ;
-rollback;
-drop table t1;
-
---echo #
---echo # Test on dynamic columns (blob)
---echo #
-
-create table assets (
- item_name varchar(32) primary key, -- A common attribute for all items
- dynamic_cols blob -- Dynamic columns will be stored here
-);
-INSERT INTO assets VALUES ('MariaDB T-shirt', COLUMN_CREATE('color', 'blue', 'size', 'XL'));
-INSERT INTO assets VALUES ('Thinkpad Laptop', COLUMN_CREATE('color', 'black', 'price', 500));
-SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color FROM assets;
-UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '3 years') WHERE item_name='Thinkpad Laptop';
-SELECT item_name, COLUMN_GET(dynamic_cols, 'warranty' as char) AS color FROM assets;
-UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '4 years')
- WHERE item_name in (select b.item_name
- from assets b
- where COLUMN_GET(b.dynamic_cols, 'color' as char) ='black');
-SELECT item_name, COLUMN_GET(dynamic_cols, 'warranty' as char) AS color FROM assets;
-
-UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', (select COLUMN_GET(b.dynamic_cols, 'color' as char)
- from assets b
- where assets.item_name = item_name));
-SELECT item_name, COLUMN_GET(dynamic_cols, 'warranty' as char) AS color FROM assets;
-drop table assets ;
-
---echo #
---echo # Test on fulltext columns
---echo #
-CREATE TABLE ft2(copy TEXT,FULLTEXT(copy)) ENGINE=MyISAM;
-INSERT INTO ft2(copy) VALUES
- ('MySQL vs MariaDB database'),
- ('Oracle vs MariaDB database'),
- ('PostgreSQL vs MariaDB database'),
- ('MariaDB overview'),
- ('Foreign keys'),
- ('Primary keys'),
- ('Indexes'),
- ('Transactions'),
- ('Triggers');
-SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('database');
-update ft2 set copy = (select max(concat('mykeyword ',substr(b.copy,1,5))) from ft2 b WHERE MATCH(b.copy) AGAINST('database'))
- where MATCH(copy) AGAINST('keys');
-SELECT * FROM ft2 WHERE MATCH(copy) AGAINST('mykeyword');
-drop table ft2;
+set @@default_storage_engine=@save_default_engine;
--echo #
--echo # Test with MyISAM
--echo #
-create table t1 (old_c1 integer, old_c2 integer,c1 integer, c2 integer, c3 integer) engine=MyISAM;
+create table t1 (old_c1 integer,
+ old_c2 integer,
+ c1 integer,
+ c2 integer,
+ c3 integer) engine=MyISAM;
insert t1 (c1,c2,c3) select 0,seq,seq%10 from seq_1_to_500;
insert t1 (c1,c2,c3) select 1,seq,seq%10 from seq_1_to_400;
insert t1 (c1,c2,c3) select 2,seq,seq%10 from seq_1_to_300;
@@ -232,12 +46,14 @@ drop table t1;
--echo #
---echo # Test error on multi_update conversion on view with order by or limit
+--echo # Test error on multi_update conversion on view
+--echo # with order by or limit
--echo #
create table t1 (c1 integer) engine=InnoDb;
create table t2 (c1 integer) engine=InnoDb;
-create view v1 as select t1.c1 as "t1c1" ,t2.c1 as "t2c1" from t1,t2 where t1.c1=t2.c1;
+create view v1 as select t1.c1 as "t1c1" ,t2.c1 as "t2c1"
+ from t1,t2 where t1.c1=t2.c1;
# 'order by 1' should be considered as in 'select * from v1 order 1'
update v1 set t1c1=2 order by 1;
update v1 set t1c1=2 limit 1;
diff --git a/mysql-test/suite/json/r/json_table.result b/mysql-test/suite/json/r/json_table.result
index 4495735..059a2fe 100644
--- a/mysql-test/suite/json/r/json_table.result
+++ b/mysql-test/suite/json/r/json_table.result
@@ -1143,3 +1143,104 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
#
# End of 10.9 tests
#
+#
+# MDEV-29390: Improve coverage for UPDATE and DELETE statements in MTR test suites
+#
+# Multi-update with JSON_TABLE
+create table t1 ( name varchar(10),
+color varchar(10),
+price decimal(8,2),
+instock BOOLEAN);
+insert into t1 values ("Laptop", "black", 20000, 1);
+insert into t1 values ("Jacket", "brown", 5000, 1);
+insert into t1 values ("Jeans", "blue", 5000, 1);
+select * from t1;
+name color price instock
+Laptop black 20000.00 1
+Jacket brown 5000.00 1
+Jeans blue 5000.00 1
+set @json='
+[
+ {"name":"Laptop", "color":"black", "price":"1000", "ordered":"3"},
+ {"name":"Jeans", "color":"blue", "ordered":"0"},
+ {"name":"Phone", "color":"red", "ordered":"0"}
+]';
+select * from json_table(@json, '$[*]'
+columns(
+name varchar(10) path '$.name',
+color varchar(10) path '$.color',
+price decimal(8,2) path '$.price',
+ordered boolean path '$.ordered' )
+) as jt;
+name color price ordered
+Laptop black 1000.00 3
+Jeans blue NULL 0
+Phone red NULL 0
+explain update t1, JSON_TABLE(@json,'$[*]'
+COLUMNS (
+name varchar(10) path '$.name',
+color varchar(10) path '$.color',
+price decimal(8,2) path '$.price',
+ordered boolean path '$.ordered'
+ )) AS jt1
+SET t1.instock=0 where t1.name=jt1.name and jt1.ordered=3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 3
+1 SIMPLE jt1 ALL NULL NULL NULL NULL 40 Table function: json_table; Using where
+update t1, JSON_TABLE(@json,'$[*]'
+COLUMNS (
+name varchar(10) path '$.name',
+color varchar(10) path '$.color',
+price decimal(8,2) path '$.price',
+ordered boolean path '$.ordered'
+ )) AS jt1
+SET t1.instock=0 where t1.name=jt1.name and jt1.ordered=2;
+select * from t1;
+name color price instock
+Laptop black 20000.00 1
+Jacket brown 5000.00 1
+Jeans blue 5000.00 1
+explain update t1
+SET t1.instock=2 where t1.name in (
+select jt1.name from json_table(@json, '$[*]'
+columns(
+name varchar(10) path '$.name',
+color varchar(10) path '$.color',
+price decimal(8,2) path '$.price',
+ordered boolean path '$.ordered' )
+) as jt1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3
+1 PRIMARY jt1 ALL NULL NULL NULL NULL 40 Table function: json_table; Using where; FirstMatch(t1)
+update t1
+SET t1.instock=2 where t1.name in (
+select jt1.name from json_table(@json, '$[*]'
+columns(
+name varchar(10) path '$.name',
+color varchar(10) path '$.color',
+price decimal(8,2) path '$.price',
+ordered boolean path '$.ordered' )
+) as jt1);
+select * from t1;
+name color price instock
+Laptop black 20000.00 2
+Jacket brown 5000.00 1
+Jeans blue 5000.00 2
+update t1, JSON_TABLE(@json,'$[*]'
+COLUMNS (
+name varchar(10) path '$.name',
+color varchar(10) path '$.color',
+price decimal(8,2) path '$.price',
+ordered boolean path '$.ordered'
+ )) AS jt1
+SET t1.instock=0, jt1.ordered=1 where t1.name=jt1.name;
+ERROR HY000: The target table jt1 of the UPDATE is not updatable
+select * from t1;
+name color price instock
+Laptop black 20000.00 2
+Jacket brown 5000.00 1
+Jeans blue 5000.00 2
+drop table t1;
+#
+# End of 11.0 tests
+#
diff --git a/mysql-test/suite/json/t/json_table.test b/mysql-test/suite/json/t/json_table.test
index 05db8f6..8612ab9 100644
--- a/mysql-test/suite/json/t/json_table.test
+++ b/mysql-test/suite/json/t/json_table.test
@@ -995,3 +995,97 @@ COLUMNS
--echo #
--echo # End of 10.9 tests
--echo #
+
+--echo #
+--echo # MDEV-29390: Improve coverage for UPDATE and DELETE statements in MTR test suites
+--echo #
+
+--echo # Multi-update with JSON_TABLE
+
+create table t1 ( name varchar(10),
+ color varchar(10),
+ price decimal(8,2),
+ instock BOOLEAN);
+
+insert into t1 values ("Laptop", "black", 20000, 1);
+insert into t1 values ("Jacket", "brown", 5000, 1);
+insert into t1 values ("Jeans", "blue", 5000, 1);
+
+select * from t1;
+
+set @json='
+[
+ {"name":"Laptop", "color":"black", "price":"1000", "ordered":"3"},
+ {"name":"Jeans", "color":"blue", "ordered":"0"},
+ {"name":"Phone", "color":"red", "ordered":"0"}
+]';
+
+select * from json_table(@json, '$[*]'
+ columns(
+ name varchar(10) path '$.name',
+ color varchar(10) path '$.color',
+ price decimal(8,2) path '$.price',
+ ordered boolean path '$.ordered' )
+) as jt;
+
+explain update t1, JSON_TABLE(@json,'$[*]'
+ COLUMNS (
+ name varchar(10) path '$.name',
+ color varchar(10) path '$.color',
+ price decimal(8,2) path '$.price',
+ ordered boolean path '$.ordered'
+ )) AS jt1
+ SET t1.instock=0 where t1.name=jt1.name and jt1.ordered=3;
+
+update t1, JSON_TABLE(@json,'$[*]'
+ COLUMNS (
+ name varchar(10) path '$.name',
+ color varchar(10) path '$.color',
+ price decimal(8,2) path '$.price',
+ ordered boolean path '$.ordered'
+ )) AS jt1
+ SET t1.instock=0 where t1.name=jt1.name and jt1.ordered=2;
+
+select * from t1;
+
+explain update t1
+ SET t1.instock=2 where t1.name in (
+ select jt1.name from json_table(@json, '$[*]'
+ columns(
+ name varchar(10) path '$.name',
+ color varchar(10) path '$.color',
+ price decimal(8,2) path '$.price',
+ ordered boolean path '$.ordered' )
+ ) as jt1);
+
+
+update t1
+ SET t1.instock=2 where t1.name in (
+ select jt1.name from json_table(@json, '$[*]'
+ columns(
+ name varchar(10) path '$.name',
+ color varchar(10) path '$.color',
+ price decimal(8,2) path '$.price',
+ ordered boolean path '$.ordered' )
+ ) as jt1);
+
+select * from t1;
+
+
+-- error ER_NON_UPDATABLE_TABLE
+update t1, JSON_TABLE(@json,'$[*]'
+ COLUMNS (
+ name varchar(10) path '$.name',
+ color varchar(10) path '$.color',
+ price decimal(8,2) path '$.price',
+ ordered boolean path '$.ordered'
+ )) AS jt1
+ SET t1.instock=0, jt1.ordered=1 where t1.name=jt1.name;
+
+select * from t1;
+
+drop table t1;
+
+--echo #
+--echo # End of 11.0 tests
+--echo #
diff --git a/storage/connect/mysql-test/connect/r/upd.result b/storage/connect/mysql-test/connect/r/upd.result
index 8faf0089..7e3e880 100644
--- a/storage/connect/mysql-test/connect/r/upd.result
+++ b/storage/connect/mysql-test/connect/r/upd.result
@@ -1627,3 +1627,163 @@ serialno name sex title manager department secretary salary
DROP PROCEDURE test.tst_up;
DROP TABLE employee;
SET sql_mode = DEFAULT;
+#
+# End of 10.10 tests
+#
+SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
+CREATE TABLE t1
+(
+name char(12) not null,
+city char(11) not null,
+birth date not null date_format='DD/MM/YYYY',
+hired date not null date_format='DD/MM/YYYY' flag=36
+) ENGINE=CONNECT TABLE_TYPE=FIX FILE_NAME='boys.txt' ENDING=1;
+CREATE TABLE t2 (
+_id INT(2) NOT NULL,
+name_first CHAR(9) NOT NULL JPATH='$.name.first',
+name_aka CHAR(4) DEFAULT NULL JPATH='$.name.aka',
+name_last CHAR(10) NOT NULL JPATH='$.name.last',
+title CHAR(12) DEFAULT NULL,
+birth CHAR(20) DEFAULT NULL,
+death CHAR(20) DEFAULT NULL,
+contribs CHAR(50) NOT NULL JPATH='$.contribs',
+awards_award CHAR(42) DEFAULT NULL JPATH='$.awards.award',
+awards_year CHAR(4) DEFAULT NULL JPATH='$.awards.year',
+awards_by CHAR(38) DEFAULT NULL JPATH='$.awards.by'
+) ENGINE=CONNECT TABLE_TYPE=JSON FILE_NAME='bios.json';
+# Multi-update
+select t1.name, t1.city from t1;
+name city
+John Boston
+Henry Boston
+George San Jose
+Sam Chicago
+James Dallas
+Bill Boston
+select t2._id, t2.name_first, t2.name_aka, t2.name_last from t2;
+_id name_first name_aka name_last
+1 John NULL Backus
+2 John NULL McCarthy
+3 Grace NULL Hopper
+4 Kristen NULL Nygaard
+5 Ole-Johan NULL Dahl
+6 Guido NULL van Rossum
+7 Dennis NULL Ritchie
+8 Yukihiro Matz Matsumoto
+9 James NULL Gosling
+10 Martin NULL Odersky
+select t1.name, t2.name_last, t2.name_aka, t1.city from t1, t2 where t1.name=t2.name_first and t1.birth
+BETWEEN '1992-01-01' and '1995-01-01';
+name name_last name_aka city
+James Gosling NULL Dallas
+explain update t1, t2
+set t1.city='Washington', t2.name_aka='von' where t1.name=t2.name_first and t1.birth
+BETWEEN '1992-01-01' and '1995-01-01';
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 10 Using where
+update t1, t2
+set t1.city='Washington', t2.name_aka='von' where t1.name=t2.name_first and t1.birth
+BETWEEN '1992-01-01' and '1995-01-01';
+select t1.name, t2.name_last, t2.name_aka, t1.city from t1, t2 where t1.name=t2.name_first and t1.birth
+BETWEEN '1992-01-01' and '1995-01-01';
+name name_last name_aka city
+James Gosling von Washington
+# Conversion to multi-update
+select t1.name, t1.city from t1;
+name city
+John Boston
+Henry Boston
+George San Jose
+Sam Chicago
+James Washington
+Bill Boston
+explain update t1
+set t1.city='New York' where t1.name in (select t2.name_first from t2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 6
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 9 func 1 Using where
+2 MATERIALIZED t2 ALL NULL NULL NULL NULL 10
+update t1
+set t1.city='New York' where t1.name in (select t2.name_first from t2);
+select t1.name, t1.city from t1;
+name city
+John New York
+Henry Boston
+George San Jose
+Sam Chicago
+James New York
+Bill Boston
+select t1.name, t1.city from t1 where t1.name in (select a.name from t1 as a where a.birth
+BETWEEN '1981-01-01' and '1982-01-01');
+name city
+George San Jose
+explain update t1 set t1.city='Los Angeles' where t1.name in (select a.name from t1 as a where a.birth
+BETWEEN '1981-01-01' and '1982-01-01');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 6
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 6 Using where
+update t1 set t1.city='Los Angeles' where t1.name in (select a.name from t1 as a where a.birth
+BETWEEN '1981-01-01' and '1982-01-01');
+select t1.name, t1.city from t1 where t1.name in (select a.name from t1 as a where a.birth
+BETWEEN '1981-01-01' and '1982-01-01');
+name city
+George Los Angeles
+# Multi-delete
+explain delete t1.*, t2.* from t1, t2 where t1.name=t2.name_first and t1.birth
+BETWEEN '1992-01-01' and '1995-01-01';
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 10 Using where
+delete t1.*, t2.* from t1, t2 where t1.name=t2.name_first and t1.birth
+BETWEEN '1992-01-01' and '1995-01-01';
+select t1.name, t1.city from t1;
+name city
+John New York
+Henry Boston
+George Los Angeles
+Sam Chicago
+Bill Boston
+select t2._id, t2.name_first, t2.name_aka, t2.name_last from t2;
+_id name_first name_aka name_last
+1 John NULL Backus
+2 John NULL McCarthy
+3 Grace NULL Hopper
+4 Kristen NULL Nygaard
+5 Ole-Johan NULL Dahl
+6 Guido NULL van Rossum
+7 Dennis NULL Ritchie
+8 Yukihiro Matz Matsumoto
+10 Martin NULL Odersky
+# Conversion to multi-delete
+explain delete from t1 where t1.name in (select t2.name_first from t2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 5
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 9 func 1 Using where
+2 MATERIALIZED t2 ALL NULL NULL NULL NULL 9
+delete from t1 where t1.name in (select t2.name_first from t2);
+select t1.name, t1.city from t1;
+name city
+Henry Boston
+George Los Angeles
+Sam Chicago
+Bill Boston
+explain delete from t1 where t1.name in (select a.name from t1 as a where a.birth
+BETWEEN '1981-01-01' and '1982-01-01');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 4
+1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 func 1
+2 MATERIALIZED a ALL NULL NULL NULL NULL 4 Using where
+delete from t1 where t1.name in (select a.name from t1 as a where a.birth
+BETWEEN '1981-01-01' and '1982-01-01');
+select t1.name, t1.city from t1;
+name city
+Henry Boston
+Sam Chicago
+Bill Boston
+drop tables t1, t2;
+SET sql_mode = DEFAULT;
+#
+# End of 11.0 tests
+#
diff --git a/storage/connect/mysql-test/connect/t/upd.test b/storage/connect/mysql-test/connect/t/upd.test
index 28b566b..0372a9a 100644
--- a/storage/connect/mysql-test/connect/t/upd.test
+++ b/storage/connect/mysql-test/connect/t/upd.test
@@ -155,3 +155,99 @@ DROP TABLE employee;
SET sql_mode = DEFAULT;
--remove_file $MYSQLD_DATADIR/test/employee.dat
+
+--echo #
+--echo # End of 10.10 tests
+--echo #
+
+--copy_file $MTR_SUITE_DIR/std_data/boys.txt $MYSQLD_DATADIR/test/boys.txt
+--copy_file $MTR_SUITE_DIR/std_data/bios.json $MYSQLD_DATADIR/test/bios.json
+
+SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
+
+CREATE TABLE t1
+(
+ name char(12) not null,
+ city char(11) not null,
+ birth date not null date_format='DD/MM/YYYY',
+ hired date not null date_format='DD/MM/YYYY' flag=36
+) ENGINE=CONNECT TABLE_TYPE=FIX FILE_NAME='boys.txt' ENDING=1;
+
+CREATE TABLE t2 (
+ _id INT(2) NOT NULL,
+ name_first CHAR(9) NOT NULL JPATH='$.name.first',
+ name_aka CHAR(4) DEFAULT NULL JPATH='$.name.aka',
+ name_last CHAR(10) NOT NULL JPATH='$.name.last',
+ title CHAR(12) DEFAULT NULL,
+ birth CHAR(20) DEFAULT NULL,
+ death CHAR(20) DEFAULT NULL,
+ contribs CHAR(50) NOT NULL JPATH='$.contribs',
+ awards_award CHAR(42) DEFAULT NULL JPATH='$.awards.award',
+ awards_year CHAR(4) DEFAULT NULL JPATH='$.awards.year',
+ awards_by CHAR(38) DEFAULT NULL JPATH='$.awards.by'
+) ENGINE=CONNECT TABLE_TYPE=JSON FILE_NAME='bios.json';
+
+--echo # Multi-update
+
+select t1.name, t1.city from t1;
+select t2._id, t2.name_first, t2.name_aka, t2.name_last from t2;
+
+let $c1 = where t1.name=t2.name_first and t1.birth
+ BETWEEN '1992-01-01' and '1995-01-01';
+
+eval select t1.name, t2.name_last, t2.name_aka, t1.city from t1, t2 $c1;
+eval explain update t1, t2
+ set t1.city='Washington', t2.name_aka='von' $c1;
+eval update t1, t2
+ set t1.city='Washington', t2.name_aka='von' $c1;
+eval select t1.name, t2.name_last, t2.name_aka, t1.city from t1, t2 $c1;
+
+--echo # Conversion to multi-update
+
+let $c2 = where t1.name in (select t2.name_first from t2);
+
+select t1.name, t1.city from t1;
+eval explain update t1
+ set t1.city='New York' $c2;
+eval update t1
+ set t1.city='New York' $c2;
+select t1.name, t1.city from t1;
+
+let $c3 = where t1.name in (select a.name from t1 as a where a.birth
+ BETWEEN '1981-01-01' and '1982-01-01');
+
+eval select t1.name, t1.city from t1 $c3;
+eval explain update t1 set t1.city='Los Angeles' $c3;
+eval update t1 set t1.city='Los Angeles' $c3;
+eval select t1.name, t1.city from t1 $c3;
+
+--echo # Multi-delete
+
+eval explain delete t1.*, t2.* from t1, t2 $c1;
+eval delete t1.*, t2.* from t1, t2 $c1;
+
+select t1.name, t1.city from t1;
+select t2._id, t2.name_first, t2.name_aka, t2.name_last from t2;
+
+--echo # Conversion to multi-delete
+
+eval explain delete from t1 $c2;
+eval delete from t1 $c2;
+
+select t1.name, t1.city from t1;
+
+eval explain delete from t1 $c3;
+eval delete from t1 $c3;
+
+select t1.name, t1.city from t1;
+
+drop tables t1, t2;
+
+SET sql_mode = DEFAULT;
+
+--remove_file $MYSQLD_DATADIR/test/boys.txt
+--remove_file $MYSQLD_DATADIR/test/bios.json
+
+--echo #
+--echo # End of 11.0 tests
+--echo #
1
0
revision-id: efc4614112e8f252ac83320e0ad7035836b8a383 (mariadb-10.11.1-58-gefc4614)
parent(s): 883675b8ce366166a245464e2e664c26055f9371
author: Igor Babaev
committer: Igor Babaev
timestamp: 2023-02-09 22:42:29 -0800
message:
Adjusted test results after rebase
---
mysql-test/main/delete_single_to_multi.result | 42 ++++++++-----------------
mysql-test/main/update_single_to_multi.result | 44 +++++++++------------------
2 files changed, 27 insertions(+), 59 deletions(-)
diff --git a/mysql-test/main/delete_single_to_multi.result b/mysql-test/main/delete_single_to_multi.result
index fe27f69..9bd5e93 100644
--- a/mysql-test/main/delete_single_to_multi.result
+++ b/mysql-test/main/delete_single_to_multi.result
@@ -344,11 +344,11 @@ where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
-1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 Using where
+1 PRIMARY customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 11
+1 PRIMARY orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (7%) Using where; Using rowid filter
+1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey 4 dbt3_s001.orders.o_orderkey 17 Using where
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
-1 PRIMARY orders eq_ref|filter PRIMARY,i_o_orderdate,i_o_custkey PRIMARY|i_o_orderdate 4|4 dbt3_s001.lineitem.l_orderkey 1 (7%) Using where; Using rowid filter
-1 PRIMARY customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
+1 PRIMARY supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
select l_orderkey, l_linenumber, l_tax from lineitem where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -401,11 +401,11 @@ where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
-1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 Using where
+1 PRIMARY customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 11
+1 PRIMARY orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (7%) Using where; Using rowid filter
+1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey 4 dbt3_s001.orders.o_orderkey 17 Using where
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
-1 PRIMARY orders eq_ref|filter PRIMARY,i_o_orderdate,i_o_custkey PRIMARY|i_o_orderdate 4|4 dbt3_s001.lineitem.l_orderkey 1 (7%) Using where; Using rowid filter
-1 PRIMARY customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
+1 PRIMARY supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
delete from lineitem where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -467,7 +467,7 @@ c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150 Using where
-1 PRIMARY nation eq_ref|filter PRIMARY,i_n_regionkey PRIMARY|i_n_regionkey 4|5 dbt3_s001.customer.c_nationkey 1 (40%) Using where; Using rowid filter
+1 PRIMARY nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
1 PRIMARY orders ref|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (9%) Using where; FirstMatch(nation); Using rowid filter
explain format=json
select c_name, c_acctbal from customer where c_nationkey in (select n_nationkey from nation
@@ -499,14 +499,6 @@ EXPLAIN
"key_length": "4",
"used_key_parts": ["n_nationkey"],
"ref": ["dbt3_s001.customer.c_nationkey"],
- "rowid_filter": {
- "range": {
- "key": "i_n_regionkey",
- "used_key_parts": ["n_regionkey"]
- },
- "rows": 10,
- "selectivity_pct": 40
- },
"rows": 1,
"filtered": 40,
"attached_condition": "nation.n_regionkey in (1,2)"
@@ -590,7 +582,7 @@ c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150 Using where
-1 PRIMARY nation eq_ref|filter PRIMARY,i_n_regionkey PRIMARY|i_n_regionkey 4|5 dbt3_s001.customer.c_nationkey 1 (40%) Using where; Using rowid filter
+1 PRIMARY nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
1 PRIMARY orders ref|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (9%) Using where; FirstMatch(nation); Using rowid filter
explain format=json
delete from customer where c_nationkey in (select n_nationkey from nation
@@ -622,14 +614,6 @@ EXPLAIN
"key_length": "4",
"used_key_parts": ["n_nationkey"],
"ref": ["dbt3_s001.customer.c_nationkey"],
- "rowid_filter": {
- "range": {
- "key": "i_n_regionkey",
- "used_key_parts": ["n_regionkey"]
- },
- "rows": 10,
- "selectivity_pct": 40
- },
"rows": 1,
"filtered": 40,
"attached_condition": "nation.n_regionkey in (1,2)"
@@ -2183,7 +2167,7 @@ delete from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08') returning c_name;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL NULL NULL NULL NULL 141 Using where
-2 DEPENDENT SUBQUERY orders index_subquery|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 func 175 (2%) Using where; Using rowid filter
+2 DEPENDENT SUBQUERY orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where
delete from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08') returning c_name;
c_name
@@ -2296,7 +2280,7 @@ delete from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08') returning c_name;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL NULL NULL NULL NULL 141 Using where
-2 DEPENDENT SUBQUERY orders index_subquery|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 func 175 (2%) Using where; Using rowid filter
+2 DEPENDENT SUBQUERY orders range i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 28 Using index condition; Using where
delete from customer where c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08') returning c_name;
c_name
@@ -2338,7 +2322,7 @@ where c_nationkey in (1,2))
order by o_totalprice limit 500;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY orders range i_o_orderdate i_o_orderdate 4 NULL 108 Using where; Using filesort
-2 DEPENDENT SUBQUERY customer unique_subquery|filter PRIMARY,i_c_nationkey PRIMARY|i_c_nationkey 4|5 func 1 (10%) Using where; Using rowid filter
+2 DEPENDENT SUBQUERY customer unique_subquery PRIMARY,i_c_nationkey PRIMARY 4 func 1 Using where
create table t as
select * from orders where o_orderDATE between '1992-01-01' and '1992-06-30' and
o_custkey in (select c_custkey from customer
diff --git a/mysql-test/main/update_single_to_multi.result b/mysql-test/main/update_single_to_multi.result
index cc28a0f..d020fd1 100644
--- a/mysql-test/main/update_single_to_multi.result
+++ b/mysql-test/main/update_single_to_multi.result
@@ -357,11 +357,11 @@ where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
-1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 Using where
+1 PRIMARY customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 11
+1 PRIMARY orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (7%) Using where; Using rowid filter
+1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey 4 dbt3_s001.orders.o_orderkey 17 Using where
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
-1 PRIMARY orders eq_ref|filter PRIMARY,i_o_orderdate,i_o_custkey PRIMARY|i_o_orderdate 4|4 dbt3_s001.lineitem.l_orderkey 1 (7%) Using where; Using rowid filter
-1 PRIMARY customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
+1 PRIMARY supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
select l_orderkey, l_linenumber, l_tax from lineitem where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -377,10 +377,10 @@ where p_retailprice between 901 and 1000 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
l_orderkey l_linenumber l_tax
-4996 1 0.01
933 1 0.04
2500 2 0.02
2500 4 0.02
+4996 1 0.01
explain
update lineitem set l_tax = (l_tax*100+1)/100 where l_orderkey in (select o_orderkey from orders
where o_custkey in
@@ -399,11 +399,11 @@ where n_name='PERU'));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
1 PRIMARY nation ref PRIMARY,i_n_name i_n_name 26 const 1 Using index condition
-1 PRIMARY supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 2
-1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 Using where
+1 PRIMARY customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 11
+1 PRIMARY orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (7%) Using where; Using rowid filter
+1 PRIMARY lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey 4 dbt3_s001.orders.o_orderkey 17 Using where
1 PRIMARY part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
-1 PRIMARY orders eq_ref|filter PRIMARY,i_o_orderdate,i_o_custkey PRIMARY|i_o_orderdate 4|4 dbt3_s001.lineitem.l_orderkey 1 (7%) Using where; Using rowid filter
-1 PRIMARY customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
+1 PRIMARY supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
update lineitem set l_tax = (l_tax*100+1)/100 where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -433,10 +433,10 @@ where p_retailprice between 901 and 1000 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
l_orderkey l_linenumber l_tax
-4996 1 0.02
933 1 0.05
2500 2 0.03
2500 4 0.03
+4996 1 0.02
update lineitem set l_tax = (l_tax*100-1)/100 where l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
@@ -466,10 +466,10 @@ where p_retailprice between 901 and 1000 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
l_orderkey l_linenumber l_tax
-4996 1 0.01
933 1 0.04
2500 2 0.02
2500 4 0.02
+4996 1 0.01
# FirstMatch
# ==========
set optimizer_switch='materialization=off';
@@ -481,7 +481,7 @@ c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150 Using where
-1 PRIMARY nation eq_ref|filter PRIMARY,i_n_regionkey PRIMARY|i_n_regionkey 4|5 dbt3_s001.customer.c_nationkey 1 (40%) Using where; Using rowid filter
+1 PRIMARY nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
1 PRIMARY orders ref|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (9%) Using where; FirstMatch(nation); Using rowid filter
explain format=json
select c_name, c_acctbal from customer where c_nationkey in (select n_nationkey from nation
@@ -513,14 +513,6 @@ EXPLAIN
"key_length": "4",
"used_key_parts": ["n_nationkey"],
"ref": ["dbt3_s001.customer.c_nationkey"],
- "rowid_filter": {
- "range": {
- "key": "i_n_regionkey",
- "used_key_parts": ["n_regionkey"]
- },
- "rows": 10,
- "selectivity_pct": 40
- },
"rows": 1,
"filtered": 40,
"attached_condition": "nation.n_regionkey in (1,2)"
@@ -598,7 +590,7 @@ c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer ALL PRIMARY,i_c_nationkey NULL NULL NULL 150 Using where
-1 PRIMARY nation eq_ref|filter PRIMARY,i_n_regionkey PRIMARY|i_n_regionkey 4|5 dbt3_s001.customer.c_nationkey 1 (40%) Using where; Using rowid filter
+1 PRIMARY nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
1 PRIMARY orders ref|filter i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 11 (9%) Using where; FirstMatch(nation); Using rowid filter
explain format=json
update customer set c_acctbal = c_acctbal+10 where c_nationkey in (select n_nationkey from nation
@@ -630,14 +622,6 @@ EXPLAIN
"key_length": "4",
"used_key_parts": ["n_nationkey"],
"ref": ["dbt3_s001.customer.c_nationkey"],
- "rowid_filter": {
- "range": {
- "key": "i_n_regionkey",
- "used_key_parts": ["n_regionkey"]
- },
- "rows": 10,
- "selectivity_pct": 40
- },
"rows": 1,
"filtered": 40,
"attached_condition": "nation.n_regionkey in (1,2)"
@@ -2296,7 +2280,7 @@ where c_nationkey in (1,2))
order by o_totalprice limit 500;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY orders range i_o_orderdate i_o_orderdate 4 NULL 108 Using where; Using filesort
-2 DEPENDENT SUBQUERY customer unique_subquery|filter PRIMARY,i_c_nationkey PRIMARY|i_c_nationkey 4|5 func 1 (10%) Using where; Using rowid filter
+2 DEPENDENT SUBQUERY customer unique_subquery PRIMARY,i_c_nationkey PRIMARY 4 func 1 Using where
update orders set o_totalprice = o_totalprice-50 where o_orderDATE between '1992-01-01' and '1992-06-30' and
o_custkey in (select c_custkey from customer
where c_nationkey in (1,2))
1
0