Skip to content

Commit 8be6d25

Browse files
MDEV-35206: Assertion failure in JOIN::dbug_verify_sj_inner_tables
A nested select query is crashing in when optimizer_join_limit_pref_ratio=10 and optimizer_search_depth=1 due to an assertion failure in JOIN::dbug_verify_sj_inner_tables(). In sql_select.cc#choose_plan(), there are 2 back-2-back calls to greedy_search(). The first one is invoked to build a join plan that can short-cut ORDER BY...LIMIT, while the second invocation to not consider short-cut. The greedy_search() should start with a value of join->cur_sj_inner_tables set to 0. However, the first greedy_search() call left the value of join->cur_sj_inner_tables to "6". This caused the assert to fail in dbug_verify_sj_inner_tables() as soon as the second greedy_search() started, where in it was expecting a value of 0. Similar problem is noticed with cur_embedding_map in the case of nested joins, and nested_join counter. introduced init_join_plan_search_state() which is called from start of greedy_search() and optimize_straight join that does all the needed initialization by : 1. setting 0's to cur_sj_inner_tables, and cur_embedding_map 2. invoking reset_nj_counters().
1 parent 4918fc5 commit 8be6d25

File tree

4 files changed

+204
-7
lines changed

4 files changed

+204
-7
lines changed

mysql-test/main/join_nested.result

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,4 +2062,67 @@ LEFT JOIN (t1 a LEFT JOIN t1 b ON t1.i = b.i) ON c.i = t1.i);
20622062
1
20632063
1
20642064
DROP TABLE t1;
2065+
#
2066+
# MDEV-35206: Assertion in JOIN::dbug_verify_sj_inner_tables
2067+
#
2068+
SET @save_optimizer_join_limit_pref_ratio= @@optimizer_join_limit_pref_ratio;
2069+
SET @save_optimizer_search_depth= @@optimizer_search_depth;
2070+
CREATE TABLE t1 (c1 VARCHAR(64) DEFAULT NULL, c2 VARCHAR(8) DEFAULT NULL);
2071+
INSERT INTO t1 (c1) values ('one');
2072+
INSERT INTO t1 (c2) values ('2');
2073+
SET optimizer_join_limit_pref_ratio=10;
2074+
SET optimizer_search_depth=1;
2075+
SELECT
2076+
c1
2077+
FROM
2078+
t1
2079+
WHERE
2080+
c2 IN (SELECT c2
2081+
FROM t1
2082+
WHERE c1 IN (SELECT c1
2083+
FROM t1
2084+
WHERE c1 IN (NULL)
2085+
)
2086+
)
2087+
ORDER BY c1 LIMIT 1;
2088+
c1
2089+
DROP TABLE t1;
2090+
#
2091+
# similar issue with join::cur_embedding_map
2092+
#
2093+
CREATE TABLE t10 (a int, b int, index(b));
2094+
INSERT INTO t10 SELECT seq, seq FROM seq_1_to_10;
2095+
CREATE TABLE t11(a int, b int);
2096+
CREATE TABLE t12(a int, b int, index(b));
2097+
INSERT INTO t11 select seq, seq FROM seq_1_to_20;
2098+
INSERT INTO t12 select seq, seq FROM seq_1_to_40;
2099+
CREATE TABLE t13(a int, b int);
2100+
CREATE TABLE t14(a int, b int, index(b));
2101+
INSERT INTO t13 select seq, seq FROM seq_1_to_20;
2102+
INSERT INTO t14 select seq, seq FROM seq_1_to_40;
2103+
ANALYZE TABLE t10, t11, t12;
2104+
Table Op Msg_type Msg_text
2105+
test.t10 analyze status Engine-independent statistics collected
2106+
test.t10 analyze status Table is already up to date
2107+
test.t11 analyze status Engine-independent statistics collected
2108+
test.t11 analyze status OK
2109+
test.t12 analyze status Engine-independent statistics collected
2110+
test.t12 analyze status Table is already up to date
2111+
EXPLAIN SELECT *
2112+
FROM
2113+
t10 LEFT JOIN
2114+
(
2115+
t11 JOIN t12 ON t11.b=t12.b
2116+
left join (t13 join t14 on t13.b=t14.b) on t13.a=t11.a
2117+
) ON t10.a=t11.a
2118+
ORDER BY t10.b LIMIT 1;
2119+
id select_type table type possible_keys key key_len ref rows Extra
2120+
1 SIMPLE t10 ALL NULL NULL NULL NULL 10 Using temporary; Using filesort
2121+
1 SIMPLE t11 ALL NULL NULL NULL NULL 20 Using where
2122+
1 SIMPLE t12 ref b b 5 test.t11.b 1
2123+
1 SIMPLE t13 ALL NULL NULL NULL NULL 20 Using where
2124+
1 SIMPLE t14 ref b b 5 test.t13.b 1
2125+
DROP TABLE t10, t11, t12, t13, t14;
2126+
SET optimizer_join_limit_pref_ratio= @save_optimizer_join_limit_pref_ratio;
2127+
SET optimizer_search_depth= @save_optimizer_search_depth;
20652128
# end of 10.11 tests

mysql-test/main/join_nested.test

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
--disable_warnings
33
DROP TABLE IF EXISTS t0,t1,t2,t3,t4,t5,t6,t7,t8,t9;
44
--enable_warnings
5+
--source include/have_sequence.inc
56

67
SET @save_optimizer_switch=@@optimizer_switch;
78
SET optimizer_switch=ifnull(@optimizer_switch_for_join_nested_test,'outer_join_with_cache=off');
@@ -1470,4 +1471,69 @@ SELECT 1 FROM t1 WHERE i IN
14701471
LEFT JOIN (t1 a LEFT JOIN t1 b ON t1.i = b.i) ON c.i = t1.i);
14711472

14721473
DROP TABLE t1;
1474+
1475+
--echo #
1476+
--echo # MDEV-35206: Assertion in JOIN::dbug_verify_sj_inner_tables
1477+
--echo #
1478+
1479+
SET @save_optimizer_join_limit_pref_ratio= @@optimizer_join_limit_pref_ratio;
1480+
SET @save_optimizer_search_depth= @@optimizer_search_depth;
1481+
1482+
CREATE TABLE t1 (c1 VARCHAR(64) DEFAULT NULL, c2 VARCHAR(8) DEFAULT NULL);
1483+
INSERT INTO t1 (c1) values ('one');
1484+
INSERT INTO t1 (c2) values ('2');
1485+
1486+
SET optimizer_join_limit_pref_ratio=10;
1487+
SET optimizer_search_depth=1;
1488+
1489+
SELECT
1490+
c1
1491+
FROM
1492+
t1
1493+
WHERE
1494+
c2 IN (SELECT c2
1495+
FROM t1
1496+
WHERE c1 IN (SELECT c1
1497+
FROM t1
1498+
WHERE c1 IN (NULL)
1499+
)
1500+
)
1501+
ORDER BY c1 LIMIT 1;
1502+
1503+
DROP TABLE t1;
1504+
1505+
--echo #
1506+
--echo # similar issue with join::cur_embedding_map
1507+
--echo #
1508+
CREATE TABLE t10 (a int, b int, index(b));
1509+
INSERT INTO t10 SELECT seq, seq FROM seq_1_to_10;
1510+
1511+
CREATE TABLE t11(a int, b int);
1512+
CREATE TABLE t12(a int, b int, index(b));
1513+
1514+
INSERT INTO t11 select seq, seq FROM seq_1_to_20;
1515+
INSERT INTO t12 select seq, seq FROM seq_1_to_40;
1516+
1517+
CREATE TABLE t13(a int, b int);
1518+
CREATE TABLE t14(a int, b int, index(b));
1519+
1520+
INSERT INTO t13 select seq, seq FROM seq_1_to_20;
1521+
INSERT INTO t14 select seq, seq FROM seq_1_to_40;
1522+
1523+
ANALYZE TABLE t10, t11, t12;
1524+
1525+
EXPLAIN SELECT *
1526+
FROM
1527+
t10 LEFT JOIN
1528+
(
1529+
t11 JOIN t12 ON t11.b=t12.b
1530+
left join (t13 join t14 on t13.b=t14.b) on t13.a=t11.a
1531+
) ON t10.a=t11.a
1532+
ORDER BY t10.b LIMIT 1;
1533+
1534+
DROP TABLE t10, t11, t12, t13, t14;
1535+
1536+
SET optimizer_join_limit_pref_ratio= @save_optimizer_join_limit_pref_ratio;
1537+
SET optimizer_search_depth= @save_optimizer_search_depth;
1538+
14731539
--echo # end of 10.11 tests

mysql-test/main/join_nested_jcl6.result

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,69 @@ LEFT JOIN (t1 a LEFT JOIN t1 b ON t1.i = b.i) ON c.i = t1.i);
20712071
1
20722072
1
20732073
DROP TABLE t1;
2074+
#
2075+
# MDEV-35206: Assertion in JOIN::dbug_verify_sj_inner_tables
2076+
#
2077+
SET @save_optimizer_join_limit_pref_ratio= @@optimizer_join_limit_pref_ratio;
2078+
SET @save_optimizer_search_depth= @@optimizer_search_depth;
2079+
CREATE TABLE t1 (c1 VARCHAR(64) DEFAULT NULL, c2 VARCHAR(8) DEFAULT NULL);
2080+
INSERT INTO t1 (c1) values ('one');
2081+
INSERT INTO t1 (c2) values ('2');
2082+
SET optimizer_join_limit_pref_ratio=10;
2083+
SET optimizer_search_depth=1;
2084+
SELECT
2085+
c1
2086+
FROM
2087+
t1
2088+
WHERE
2089+
c2 IN (SELECT c2
2090+
FROM t1
2091+
WHERE c1 IN (SELECT c1
2092+
FROM t1
2093+
WHERE c1 IN (NULL)
2094+
)
2095+
)
2096+
ORDER BY c1 LIMIT 1;
2097+
c1
2098+
DROP TABLE t1;
2099+
#
2100+
# similar issue with join::cur_embedding_map
2101+
#
2102+
CREATE TABLE t10 (a int, b int, index(b));
2103+
INSERT INTO t10 SELECT seq, seq FROM seq_1_to_10;
2104+
CREATE TABLE t11(a int, b int);
2105+
CREATE TABLE t12(a int, b int, index(b));
2106+
INSERT INTO t11 select seq, seq FROM seq_1_to_20;
2107+
INSERT INTO t12 select seq, seq FROM seq_1_to_40;
2108+
CREATE TABLE t13(a int, b int);
2109+
CREATE TABLE t14(a int, b int, index(b));
2110+
INSERT INTO t13 select seq, seq FROM seq_1_to_20;
2111+
INSERT INTO t14 select seq, seq FROM seq_1_to_40;
2112+
ANALYZE TABLE t10, t11, t12;
2113+
Table Op Msg_type Msg_text
2114+
test.t10 analyze status Engine-independent statistics collected
2115+
test.t10 analyze status Table is already up to date
2116+
test.t11 analyze status Engine-independent statistics collected
2117+
test.t11 analyze status OK
2118+
test.t12 analyze status Engine-independent statistics collected
2119+
test.t12 analyze status Table is already up to date
2120+
EXPLAIN SELECT *
2121+
FROM
2122+
t10 LEFT JOIN
2123+
(
2124+
t11 JOIN t12 ON t11.b=t12.b
2125+
left join (t13 join t14 on t13.b=t14.b) on t13.a=t11.a
2126+
) ON t10.a=t11.a
2127+
ORDER BY t10.b LIMIT 1;
2128+
id select_type table type possible_keys key key_len ref rows Extra
2129+
1 SIMPLE t10 ALL NULL NULL NULL NULL 10 Using temporary; Using filesort
2130+
1 SIMPLE t11 hash_ALL NULL #hash#$hj 5 test.t10.a 20 Using where; Using join buffer (flat, BNLH join)
2131+
1 SIMPLE t12 ref b b 5 test.t11.b 1 Using join buffer (incremental, BKA join); Key-ordered Rowid-ordered scan
2132+
1 SIMPLE t13 hash_ALL NULL #hash#$hj 5 test.t10.a 20 Using where; Using join buffer (incremental, BNLH join)
2133+
1 SIMPLE t14 ref b b 5 test.t13.b 1 Using join buffer (incremental, BKA join); Key-ordered Rowid-ordered scan
2134+
DROP TABLE t10, t11, t12, t13, t14;
2135+
SET optimizer_join_limit_pref_ratio= @save_optimizer_join_limit_pref_ratio;
2136+
SET optimizer_search_depth= @save_optimizer_search_depth;
20742137
# end of 10.11 tests
20752138
CREATE TABLE t5 (a int, b int, c int, PRIMARY KEY(a), KEY b_i (b));
20762139
CREATE TABLE t6 (a int, b int, c int, PRIMARY KEY(a), KEY b_i (b));

sql/sql_select.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ static
342342
void compute_part_of_sort_key_for_equals(JOIN *join, TABLE *table,
343343
Item_field *item_field,
344344
key_map *col_keys);
345+
static void init_join_plan_search_state(JOIN *join);
345346

346347
#ifndef DBUG_OFF
347348

@@ -9396,11 +9397,9 @@ choose_plan(JOIN *join, table_map join_tables)
93969397
DBUG_ENTER("choose_plan");
93979398

93989399
join->limit_optimization_mode= false;
9399-
join->cur_embedding_map= 0;
94009400
join->extra_heuristic_pruning= false;
94019401
join->prune_level= join->thd->variables.optimizer_prune_level;
94029402

9403-
reset_nj_counters(join, join->join_list);
94049403
qsort_cmp2 jtab_sort_func;
94059404

94069405
if (join->emb_sjm_nest)
@@ -9440,11 +9439,6 @@ choose_plan(JOIN *join, table_map join_tables)
94409439
{
94419440
choose_initial_table_order(join);
94429441
}
9443-
/*
9444-
Note: constant tables are already in the join prefix. We don't
9445-
put them into the cur_sj_inner_tables, though.
9446-
*/
9447-
join->cur_sj_inner_tables= 0;
94489442

94499443
if (straight_join)
94509444
{
@@ -9781,6 +9775,8 @@ optimize_straight_join(JOIN *join, table_map remaining_tables)
97819775
POSITION loose_scan_pos;
97829776
THD *thd= join->thd;
97839777

9778+
init_join_plan_search_state(join);
9779+
97849780
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
97859781
{
97869782
POSITION *position= join->positions + idx;
@@ -9926,6 +9922,8 @@ greedy_search(JOIN *join,
99269922
uint n_tables __attribute__((unused));
99279923
DBUG_ENTER("greedy_search");
99289924

9925+
init_join_plan_search_state(join);
9926+
99299927
/* number of tables that remain to be optimized */
99309928
usable_tables= (join->emb_sjm_nest ?
99319929
(join->emb_sjm_nest->sj_inner_tables &
@@ -18993,6 +18991,7 @@ static bool check_interleaving_with_nj(JOIN_TAB *next_tab)
1899318991
if (!next_emb->sj_on_expr)
1899418992
{
1899518993
next_emb->nested_join->counter++;
18994+
DBUG_ASSERT(next_emb->nested_join->counter <= next_emb->nested_join->n_tables);
1899618995
if (next_emb->nested_join->counter == 1)
1899718996
{
1899818997
/*
@@ -33073,6 +33072,12 @@ bool JOIN::transform_all_conds_and_on_exprs_in_join_list(
3307333072
return false;
3307433073
}
3307533074

33075+
static void init_join_plan_search_state(JOIN *join)
33076+
{
33077+
join->cur_sj_inner_tables= 0;
33078+
join->cur_embedding_map= 0;
33079+
reset_nj_counters(join, join->join_list);
33080+
}
3307633081

3307733082
/**
3307833083
@} (end of group Query_Optimizer)

0 commit comments

Comments
 (0)