Skip to content

Commit 96b62b5

Browse files
committed
Fixed bug mdev-11161.
The flag TABLE_LIST::fill_me must be reset to false at the prepare phase for any materialized derived table used in the executed query. Otherwise if the optimizer decides to generate a key for such a table it is generated only for the first execution of the query.
1 parent 10aee66 commit 96b62b5

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

mysql-test/r/derived_view.result

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,5 +2496,88 @@ DROP TABLE t1,t2;
24962496
#
24972497
# end of 5.3 tests
24982498
#
2499+
#
2500+
# Bug mdev-11161: The second execution of prepared statement
2501+
# does not use generated key for materialized
2502+
# derived table / view
2503+
# (actually this is a 5.3 bug.)
2504+
#
2505+
create table t1 (
2506+
mat_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
2507+
matintnum CHAR(6) NOT NULL,
2508+
test MEDIUMINT UNSIGNED NULL
2509+
);
2510+
create table t2 (
2511+
mat_id MEDIUMINT UNSIGNED NOT NULL,
2512+
pla_id MEDIUMINT UNSIGNED NOT NULL
2513+
);
2514+
insert into t1 values
2515+
(NULL, 'a', 1), (NULL, 'b', 2), (NULL, 'c', 3), (NULL, 'd', 4),
2516+
(NULL, 'e', 5), (NULL, 'f', 6), (NULL, 'g', 7), (NULL, 'h', 8),
2517+
(NULL, 'i', 9);
2518+
insert into t2 values
2519+
(1, 100), (1, 101), (1, 102), (2, 100), (2, 103), (2, 104),
2520+
(3, 101), (3, 102), (3, 105);
2521+
explain
2522+
SELECT STRAIGHT_JOIN d.pla_id, m2.mat_id
2523+
FROM t1 m2
2524+
INNER JOIN
2525+
(SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum
2526+
FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id
2527+
GROUP BY mp.pla_id) d
2528+
ON d.matintnum=m2.matintnum;
2529+
id select_type table type possible_keys key key_len ref rows Extra
2530+
1 PRIMARY m2 ALL NULL NULL NULL NULL 9
2531+
1 PRIMARY <derived2> ref key0 key0 7 test.m2.matintnum 2
2532+
2 DERIVED mp ALL NULL NULL NULL NULL 9 Using temporary; Using filesort
2533+
2 DERIVED m1 eq_ref PRIMARY PRIMARY 3 test.mp.mat_id 1
2534+
prepare stmt1 from
2535+
"SELECT STRAIGHT_JOIN d.pla_id, m2.mat_id
2536+
FROM t1 m2
2537+
INNER JOIN
2538+
(SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum
2539+
FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id
2540+
GROUP BY mp.pla_id) d
2541+
ON d.matintnum=m2.matintnum";
2542+
flush status;
2543+
execute stmt1;
2544+
pla_id mat_id
2545+
102 1
2546+
101 1
2547+
100 1
2548+
104 2
2549+
103 2
2550+
105 3
2551+
show status like '%Handler_read%';
2552+
Variable_name Value
2553+
Handler_read_first 0
2554+
Handler_read_key 21
2555+
Handler_read_last 0
2556+
Handler_read_next 6
2557+
Handler_read_prev 0
2558+
Handler_read_rnd 6
2559+
Handler_read_rnd_deleted 0
2560+
Handler_read_rnd_next 27
2561+
flush status;
2562+
execute stmt1;
2563+
pla_id mat_id
2564+
102 1
2565+
101 1
2566+
100 1
2567+
104 2
2568+
103 2
2569+
105 3
2570+
show status like '%Handler_read%';
2571+
Variable_name Value
2572+
Handler_read_first 0
2573+
Handler_read_key 21
2574+
Handler_read_last 0
2575+
Handler_read_next 6
2576+
Handler_read_prev 0
2577+
Handler_read_rnd 6
2578+
Handler_read_rnd_deleted 0
2579+
Handler_read_rnd_next 27
2580+
deallocate prepare stmt1;
2581+
drop table t1,t2;
24992582
set optimizer_switch=@exit_optimizer_switch;
25002583
set join_cache_level=@exit_join_cache_level;

mysql-test/t/derived_view.test

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,60 @@ DROP TABLE t1,t2;
18271827
--echo # end of 5.3 tests
18281828
--echo #
18291829

1830+
--echo #
1831+
--echo # Bug mdev-11161: The second execution of prepared statement
1832+
--echo # does not use generated key for materialized
1833+
--echo # derived table / view
1834+
--echo # (actually this is a 5.3 bug.)
1835+
--echo #
1836+
1837+
create table t1 (
1838+
mat_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
1839+
matintnum CHAR(6) NOT NULL,
1840+
test MEDIUMINT UNSIGNED NULL
1841+
);
1842+
create table t2 (
1843+
mat_id MEDIUMINT UNSIGNED NOT NULL,
1844+
pla_id MEDIUMINT UNSIGNED NOT NULL
1845+
);
1846+
insert into t1 values
1847+
(NULL, 'a', 1), (NULL, 'b', 2), (NULL, 'c', 3), (NULL, 'd', 4),
1848+
(NULL, 'e', 5), (NULL, 'f', 6), (NULL, 'g', 7), (NULL, 'h', 8),
1849+
(NULL, 'i', 9);
1850+
insert into t2 values
1851+
(1, 100), (1, 101), (1, 102), (2, 100), (2, 103), (2, 104),
1852+
(3, 101), (3, 102), (3, 105);
1853+
1854+
explain
1855+
SELECT STRAIGHT_JOIN d.pla_id, m2.mat_id
1856+
FROM t1 m2
1857+
INNER JOIN
1858+
(SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum
1859+
FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id
1860+
GROUP BY mp.pla_id) d
1861+
ON d.matintnum=m2.matintnum;
1862+
1863+
prepare stmt1 from
1864+
"SELECT STRAIGHT_JOIN d.pla_id, m2.mat_id
1865+
FROM t1 m2
1866+
INNER JOIN
1867+
(SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum
1868+
FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id
1869+
GROUP BY mp.pla_id) d
1870+
ON d.matintnum=m2.matintnum";
1871+
1872+
flush status;
1873+
execute stmt1;
1874+
show status like '%Handler_read%';
1875+
1876+
flush status;
1877+
execute stmt1;
1878+
show status like '%Handler_read%';
1879+
1880+
deallocate prepare stmt1;
1881+
1882+
drop table t1,t2;
1883+
18301884
# The following command must be the last one the file
18311885
set optimizer_switch=@exit_optimizer_switch;
18321886
set join_cache_level=@exit_join_cache_level;

sql/sql_derived.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,8 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
651651

652652
unit->derived= derived;
653653

654+
derived->fill_me= FALSE;
655+
654656
if (!(derived->derived_result= new select_union))
655657
DBUG_RETURN(TRUE); // out of memory
656658

0 commit comments

Comments
 (0)