Skip to content

Commit 77fca51

Browse files
committed
Bug#35889583: Server fails when we execute set of queries Bug#35996409: Failure in Query_expression::is_set_operation() Bug#36404149: Failing the server
Problem is that we attempt to inspect a query expression object that has been abandoned due to condition elimination. The problem is with equality operations that are transformed into semi-join conditions, where we store the left and right parts of the equality in sj_inner_exprs and sj_outer_exprs. The fix is to remove the references in the semi-join expression arrays when removing the corresponding equality condition. A dive within all join nests of the query block is necessary, due to the fact that we may have seen multiple transformations that add join nests so far. Also had to add some code that was developed for the fix of bug#35710378. This code was not originally needed for 8.0, but without it there would be several tests using semi-join that would change plans. Change-Id: Ida1bc4c1d586733330b8bd63a3f1f51ac094b96f
1 parent 0fa789a commit 77fca51

13 files changed

+126
-65
lines changed

mysql-test/r/group_by_fd_no_prot.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ id select_type table partitions type possible_keys key key_len ref rows filtered
22212221
2 DEPENDENT SUBQUERY t3 NULL index NULL b 15 NULL 1 100.00 Using where; Not exists; Using index; Using join buffer (hash join)
22222222
Warnings:
22232223
Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
2224-
Note 1003 /* select#1 */ select (/* select#2 */ select 2 from `test`.`t2` anti join (`test`.`t3`) on((1 = 1)) where true) AS `col` from `test`.`t1` group by `test`.`t1`.`a`
2224+
Note 1003 /* select#1 */ select (/* select#2 */ select 2 from `test`.`t2` anti join (`test`.`t3`) on(true) where true) AS `col` from `test`.`t1` group by `test`.`t1`.`a`
22252225
explain select (select 2 from t2 where exists(select 1 from t3 where t1.b)) as col from t1 group by t1.a;
22262226
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
22272227
explain select (select 2 from t2 where not exists(select 1 from t3 where t1.b)) as col from t1 group by t1.a;

mysql-test/r/group_by_fd_ps_prot.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ id select_type table partitions type possible_keys key key_len ref rows filtered
15771577
2 DEPENDENT SUBQUERY t3 NULL index NULL b 15 NULL 1 100.00 Using where; Not exists; Using index; Using join buffer (hash join)
15781578
Warnings:
15791579
Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
1580-
Note 1003 /* select#1 */ select (/* select#2 */ select 2 from `test`.`t2` anti join (`test`.`t3`) on((1 = 1)) where true) AS `col` from `test`.`t1` group by `test`.`t1`.`a`
1580+
Note 1003 /* select#1 */ select (/* select#2 */ select 2 from `test`.`t2` anti join (`test`.`t3`) on(true) where true) AS `col` from `test`.`t1` group by `test`.`t1`.`a`
15811581
explain select (select 2 from t2 where exists(select 1 from t3 where t1.b)) as col from t1 group by t1.a;
15821582
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
15831583
explain select (select 2 from t2 where not exists(select 1 from t3 where t1.b)) as col from t1 group by t1.a;

mysql-test/r/subquery_sj_all.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7774,7 +7774,7 @@ WHERE 1 IN(SELECT 1
77747774
FROM t4)));
77757775
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
77767776
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
7777-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
7777+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
77787778
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
77797779
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 9 100.00 Using join buffer (hash join)
77807780
2 MATERIALIZED t4 NULL ALL NULL NULL NULL NULL 9 100.00 Using join buffer (hash join)

mysql-test/r/subquery_sj_all_bka.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7776,7 +7776,7 @@ WHERE 1 IN(SELECT 1
77767776
FROM t4)));
77777777
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
77787778
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
7779-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
7779+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
77807780
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
77817781
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 9 100.00 Using join buffer (hash join)
77827782
2 MATERIALIZED t4 NULL ALL NULL NULL NULL NULL 9 100.00 Using join buffer (hash join)

mysql-test/r/subquery_sj_all_bka_nobnl.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7774,7 +7774,7 @@ WHERE 1 IN(SELECT 1
77747774
FROM t4)));
77757775
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
77767776
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
7777-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
7777+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
77787778
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
77797779
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
77807780
2 MATERIALIZED t4 NULL ALL NULL NULL NULL NULL 9 100.00 NULL

mysql-test/r/subquery_sj_mat.result

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7977,7 +7977,7 @@ WHERE 1 IN(SELECT 1
79777977
FROM t3));
79787978
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
79797979
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 2 100.00 NULL
7980-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
7980+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
79817981
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 2 100.00 NULL
79827982
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 2 100.00 Using join buffer (hash join)
79837983
Warnings:
@@ -8016,7 +8016,7 @@ WHERE 1 IN(SELECT 1
80168016
FROM t4)));
80178017
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
80188018
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
8019-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
8019+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
80208020
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
80218021
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 9 100.00 Using join buffer (hash join)
80228022
2 MATERIALIZED t4 NULL ALL NULL NULL NULL NULL 9 100.00 Using join buffer (hash join)
@@ -13188,9 +13188,8 @@ Table Op Msg_type Msg_text
1318813188
test.t1 analyze status OK
1318913189
EXPLAIN SELECT 1 FROM t1 WHERE EXISTS(SELECT 1) IN (SELECT 1 FROM t1);
1319013190
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
13191-
1 PRIMARY t1 NULL ALL NULL NULL NULL NULL 1 100.00 NULL
13192-
1 PRIMARY <subquery3> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
13193-
3 MATERIALIZED t1 NULL ALL NULL NULL NULL NULL 1 100.00 NULL
13191+
1 PRIMARY t1 NULL ALL NULL NULL NULL NULL 1 100.00 Start temporary
13192+
1 PRIMARY t1 NULL ALL NULL NULL NULL NULL 1 100.00 End temporary; Using join buffer (hash join)
1319413193
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
1319513194
Warnings:
1319613195
Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` semi join (`test`.`t1`) where true

mysql-test/r/subquery_sj_mat_bka.result

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7978,7 +7978,7 @@ WHERE 1 IN(SELECT 1
79787978
FROM t3));
79797979
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
79807980
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 2 100.00 NULL
7981-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
7981+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
79827982
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 2 100.00 NULL
79837983
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 2 100.00 Using join buffer (hash join)
79847984
Warnings:
@@ -8017,7 +8017,7 @@ WHERE 1 IN(SELECT 1
80178017
FROM t4)));
80188018
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
80198019
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
8020-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
8020+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
80218021
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
80228022
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 9 100.00 Using join buffer (hash join)
80238023
2 MATERIALIZED t4 NULL ALL NULL NULL NULL NULL 9 100.00 Using join buffer (hash join)
@@ -13189,9 +13189,8 @@ Table Op Msg_type Msg_text
1318913189
test.t1 analyze status OK
1319013190
EXPLAIN SELECT 1 FROM t1 WHERE EXISTS(SELECT 1) IN (SELECT 1 FROM t1);
1319113191
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
13192-
1 PRIMARY t1 NULL ALL NULL NULL NULL NULL 1 100.00 NULL
13193-
1 PRIMARY <subquery3> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
13194-
3 MATERIALIZED t1 NULL ALL NULL NULL NULL NULL 1 100.00 NULL
13192+
1 PRIMARY t1 NULL ALL NULL NULL NULL NULL 1 100.00 Start temporary
13193+
1 PRIMARY t1 NULL ALL NULL NULL NULL NULL 1 100.00 End temporary; Using join buffer (hash join)
1319513194
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
1319613195
Warnings:
1319713196
Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` semi join (`test`.`t1`) where true

mysql-test/r/subquery_sj_mat_bka_nobnl.result

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7977,7 +7977,7 @@ WHERE 1 IN(SELECT 1
79777977
FROM t3));
79787978
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
79797979
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 2 100.00 NULL
7980-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
7980+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
79817981
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 2 100.00 NULL
79827982
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 2 100.00 NULL
79837983
Warnings:
@@ -8016,7 +8016,7 @@ WHERE 1 IN(SELECT 1
80168016
FROM t4)));
80178017
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
80188018
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
8019-
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
8019+
1 SIMPLE <subquery2> NULL const <auto_distinct_key> <auto_distinct_key> 8 const 1 100.00 NULL
80208020
2 MATERIALIZED t2 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
80218021
2 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
80228022
2 MATERIALIZED t4 NULL ALL NULL NULL NULL NULL 9 100.00 NULL
@@ -13185,8 +13185,7 @@ test.t1 analyze status OK
1318513185
EXPLAIN SELECT 1 FROM t1 WHERE EXISTS(SELECT 1) IN (SELECT 1 FROM t1);
1318613186
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1318713187
1 PRIMARY t1 NULL ALL NULL NULL NULL NULL 1 100.00 NULL
13188-
1 PRIMARY <subquery3> NULL const <auto_distinct_key> <auto_distinct_key> 4 const 1 100.00 NULL
13189-
3 MATERIALIZED t1 NULL ALL NULL NULL NULL NULL 1 100.00 NULL
13188+
1 PRIMARY t1 NULL ALL NULL NULL NULL NULL 1 100.00 Start temporary; End temporary
1319013189
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
1319113190
Warnings:
1319213191
Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` semi join (`test`.`t1`) where true

sql/item.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,7 @@ class Item : public Parse_tree_node {
27372737
Query_block *const m_root;
27382738

27392739
friend class Item;
2740+
friend class Item_func_eq;
27402741
friend class Item_sum;
27412742
friend class Item_subselect;
27422743
friend class Item_ref;

sql/item_cmpfunc.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,22 @@ void Item_in_optimizer::update_used_tables() {
24372437
}
24382438
}
24392439

2440+
bool Item_func_eq::clean_up_after_removal(uchar *arg) {
2441+
Cleanup_after_removal_context *const ctx =
2442+
pointer_cast<Cleanup_after_removal_context *>(arg);
2443+
2444+
if (ctx->is_stopped(this)) return false;
2445+
2446+
if (reference_count() > 1) {
2447+
(void)decrement_ref_count();
2448+
ctx->stop_at(this);
2449+
}
2450+
2451+
ctx->get_root()->prune_sj_exprs(this, nullptr);
2452+
2453+
return false;
2454+
}
2455+
24402456
longlong Item_func_eq::val_int() {
24412457
assert(fixed == 1);
24422458
int value = cmp.compare();

sql/item_cmpfunc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ class Item_func_eq final : public Item_eq_base {
10571057
Item *negated_item() override;
10581058
bool equality_substitution_analyzer(uchar **) override { return true; }
10591059
Item *equality_substitution_transformer(uchar *arg) override;
1060+
bool clean_up_after_removal(uchar *arg) override;
10601061
bool gc_subst_analyzer(uchar **) override { return true; }
10611062

10621063
float get_filtering_effect(THD *thd, table_map filter_for_table,

sql/sql_lex.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,8 @@ class Query_block : public Query_term {
17181718
*/
17191719
bool accept(Select_lex_visitor *visitor);
17201720

1721+
void prune_sj_exprs(Item_func_eq *item, mem_root_deque<Table_ref *> *nest);
1722+
17211723
/**
17221724
Cleanup this subtree (this Query_block and all nested Query_blockes and
17231725
Query_expressions).
@@ -2208,7 +2210,7 @@ class Query_block : public Query_term {
22082210
/// Build semijoin condition for th query block
22092211
bool build_sj_cond(THD *thd, NESTED_JOIN *nested_join,
22102212
Query_block *subq_query_block, table_map outer_tables_map,
2211-
Item **sj_cond);
2213+
Item **sj_cond, bool *simple_const);
22122214
bool decorrelate_condition(Semijoin_decorrelation &sj_decor,
22132215
Table_ref *join_nest);
22142216

0 commit comments

Comments
 (0)