Skip to content

Commit b245023

Browse files
committed
MDEV-16992 Assertion `table_ref->table || table_ref->view' failed in
Field_iterator_table_ref::set_field_iterator Several functions that processed different prepare statements missed the DT_INIT flag in last parameter of the open_normal_and_derived_tables() calls. It made context analysis of derived tables dependent on the order in which the derived tables were processed by mysql_handle_derived(). This order was induced by the order of SELECTs in all_select_list. In 10.4 the order of SELECTs in all_select_list became different and lack of the DT_INIT flags in some open_normal_and_derived_tables() call became critical as some derived tables were not identified as such.
1 parent a8bf27c commit b245023

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

mysql-test/r/ps.result

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5244,5 +5244,48 @@ DROP PROCEDURE p2;
52445244
DROP PROCEDURE p1;
52455245
DROP ROLE testrole;
52465246
#
5247+
# MDEV-16992: prepare of CREATE TABLE, CREATE VIEW, DO, SET, CALL
5248+
# statements with CTE containing materialized derived
5249+
# (the bug is reproducible on 10.4)
5250+
#
5251+
prepare stmt from
5252+
"CREATE TABLE t1 AS
5253+
WITH cte(a) AS (SELECT * FROM (SELECT 1) AS t) SELECT * FROM cte;";
5254+
execute stmt;
5255+
select * from t1;
5256+
a
5257+
1
5258+
prepare stmt from
5259+
"CREATE VIEW v1 AS
5260+
WITH cte(a) AS (SELECT * FROM (SELECT 1) AS t) SELECT * FROM cte;";
5261+
execute stmt;
5262+
select * from v1;
5263+
a
5264+
1
5265+
prepare stmt from
5266+
"DO (SELECT 1
5267+
FROM (WITH cte AS (SELECT * FROM (SELECT 1) AS t)
5268+
SELECT * FROM cte) AS tt);";
5269+
execute stmt;
5270+
prepare stmt from
5271+
"SET @a = (SELECT 1
5272+
FROM (WITH cte AS (SELECT * FROM (SELECT 1) AS t)
5273+
SELECT * FROM cte) AS t);";
5274+
execute stmt;
5275+
create procedure p (i int) insert into t1 values(i);
5276+
prepare stmt from
5277+
"CALL p
5278+
((SELECT 1
5279+
FROM (WITH cte AS (SELECT * FROM (SELECT 1) AS t)
5280+
SELECT * FROM cte) AS tt));";
5281+
execute stmt;
5282+
select * from t1;
5283+
a
5284+
1
5285+
1
5286+
drop procedure p;
5287+
drop view v1;
5288+
drop table t1;
5289+
#
52475290
# End of 10.2 tests
52485291
#

mysql-test/t/ps.test

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4747,6 +4747,52 @@ DROP PROCEDURE p2;
47474747
DROP PROCEDURE p1;
47484748
DROP ROLE testrole;
47494749

4750+
--echo #
4751+
--echo # MDEV-16992: prepare of CREATE TABLE, CREATE VIEW, DO, SET, CALL
4752+
--echo # statements with CTE containing materialized derived
4753+
--echo # (the bug is reproducible on 10.4)
4754+
--echo #
4755+
4756+
--enable_result_log
4757+
4758+
prepare stmt from
4759+
"CREATE TABLE t1 AS
4760+
WITH cte(a) AS (SELECT * FROM (SELECT 1) AS t) SELECT * FROM cte;";
4761+
execute stmt;
4762+
select * from t1;
4763+
4764+
prepare stmt from
4765+
"CREATE VIEW v1 AS
4766+
WITH cte(a) AS (SELECT * FROM (SELECT 1) AS t) SELECT * FROM cte;";
4767+
execute stmt;
4768+
select * from v1;
4769+
4770+
prepare stmt from
4771+
"DO (SELECT 1
4772+
FROM (WITH cte AS (SELECT * FROM (SELECT 1) AS t)
4773+
SELECT * FROM cte) AS tt);";
4774+
execute stmt;
4775+
4776+
prepare stmt from
4777+
"SET @a = (SELECT 1
4778+
FROM (WITH cte AS (SELECT * FROM (SELECT 1) AS t)
4779+
SELECT * FROM cte) AS t);";
4780+
execute stmt;
4781+
4782+
create procedure p (i int) insert into t1 values(i);
4783+
4784+
prepare stmt from
4785+
"CALL p
4786+
((SELECT 1
4787+
FROM (WITH cte AS (SELECT * FROM (SELECT 1) AS t)
4788+
SELECT * FROM cte) AS tt));";
4789+
execute stmt;
4790+
select * from t1;
4791+
4792+
drop procedure p;
4793+
drop view v1;
4794+
drop table t1;
4795+
47504796
--echo #
47514797
--echo # End of 10.2 tests
47524798
--echo #

sql/sql_prepare.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ static bool mysql_test_do_fields(Prepared_statement *stmt,
16371637
DBUG_RETURN(TRUE);
16381638

16391639
if (open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL,
1640-
DT_PREPARE | DT_CREATE))
1640+
DT_INIT | DT_PREPARE | DT_CREATE))
16411641
DBUG_RETURN(TRUE);
16421642
DBUG_RETURN(setup_fields(thd, Ref_ptr_array(),
16431643
*values, MARK_COLUMNS_NONE, 0, NULL, 0));
@@ -1669,7 +1669,7 @@ static bool mysql_test_set_fields(Prepared_statement *stmt,
16691669
if ((tables &&
16701670
check_table_access(thd, SELECT_ACL, tables, FALSE, UINT_MAX, FALSE)) ||
16711671
open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL,
1672-
DT_PREPARE | DT_CREATE))
1672+
DT_INIT | DT_PREPARE | DT_CREATE))
16731673
goto error;
16741674

16751675
while ((var= it++))
@@ -1706,7 +1706,8 @@ static bool mysql_test_call_fields(Prepared_statement *stmt,
17061706

17071707
if ((tables &&
17081708
check_table_access(thd, SELECT_ACL, tables, FALSE, UINT_MAX, FALSE)) ||
1709-
open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL, DT_PREPARE))
1709+
open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL,
1710+
DT_INIT | DT_PREPARE))
17101711
goto err;
17111712

17121713
while ((item= it++))
@@ -1833,7 +1834,7 @@ static bool mysql_test_create_table(Prepared_statement *stmt)
18331834

18341835
if (open_normal_and_derived_tables(stmt->thd, lex->query_tables,
18351836
MYSQL_OPEN_FORCE_SHARED_MDL,
1836-
DT_PREPARE | DT_CREATE))
1837+
DT_INIT | DT_PREPARE | DT_CREATE))
18371838
DBUG_RETURN(TRUE);
18381839

18391840
select_lex->context.resolve_in_select_list= TRUE;
@@ -1854,7 +1855,7 @@ static bool mysql_test_create_table(Prepared_statement *stmt)
18541855
*/
18551856
if (open_normal_and_derived_tables(stmt->thd, lex->query_tables,
18561857
MYSQL_OPEN_FORCE_SHARED_MDL,
1857-
DT_PREPARE))
1858+
DT_INIT | DT_PREPARE))
18581859
DBUG_RETURN(TRUE);
18591860
}
18601861

@@ -2081,7 +2082,7 @@ static bool mysql_test_create_view(Prepared_statement *stmt)
20812082

20822083
lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_VIEW;
20832084
if (open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL,
2084-
DT_PREPARE))
2085+
DT_INIT | DT_PREPARE))
20852086
goto err;
20862087

20872088
res= select_like_stmt_test(stmt, 0, 0);

0 commit comments

Comments
 (0)