Skip to content

Commit e90aab7

Browse files
MDEV-34931 MDEV-31466 name resolution fails in --view
Fix for MDEV-31466 - add optional derived table column names. Column names within a SELECT_LEX structure can be left in a non-reparsable state (as printed out from *::print) after JOIN::prepare. This caused an incorrect view definition to be written into the .FRM file. Fixed by resetting item list names in SELECT_LEX structures representing derived tables before writing out the view definition. Reviewed by Igor Babaev (igor@mariadb.com)
1 parent 10008b3 commit e90aab7

File tree

6 files changed

+77
-15
lines changed

6 files changed

+77
-15
lines changed

mysql-test/main/derived.result

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,15 @@ union
20842084
select c1, c2, sum(c3) as s from t2 group by c1, c2 having s > 3
20852085
)
20862086
as d2 (f1, f2, f3);
2087+
create view v6 as select a1 from
2088+
(
2089+
select * from t1 union select * from t2 order by c1
2090+
) as d3 (a1, a2, a3);
2091+
create view v7 (b1) as select a1 from (select c1 from t1) dt (a1);
2092+
create view v8 (b1, b2, b3) as select a1, a2, a3 from
2093+
(
2094+
select * from t1 union select * from t2 order by c1
2095+
) as d3 (a1, a2, a3);
20872096
# test parent query mergability
20882097
explain format=json select a1 from v1;
20892098
EXPLAIN
@@ -2156,6 +2165,31 @@ select * from v4 where e3 < 10;
21562165
e1 e2 e3
21572166
1 2 3
21582167
4 5 6
2168+
select * from v6;
2169+
a1
2170+
1
2171+
4
2172+
7
2173+
10
2174+
show create view v6;
2175+
View Create View character_set_client collation_connection
2176+
v6 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v6` AS select `d3`.`a1` AS `a1` from (select `t1`.`c1` AS `a1`,`t1`.`c2` AS `a2`,`t1`.`c3` AS `a3` from `t1` union select `t2`.`c1` AS `c1`,`t2`.`c2` AS `c2`,`t2`.`c3` AS `c3` from `t2` order by `c1`) `d3`(`a1`,`a2`,`a3`) latin1 latin1_swedish_ci
2177+
select * from v7;
2178+
b1
2179+
1
2180+
4
2181+
show create view v7;
2182+
View Create View character_set_client collation_connection
2183+
v7 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v7` AS select `dt`.`a1` AS `b1` from (select `t1`.`c1` AS `a1` from `t1`) `dt`(`a1`) latin1 latin1_swedish_ci
2184+
select * from v8;
2185+
b1 b2 b3
2186+
1 2 3
2187+
4 5 6
2188+
7 8 9
2189+
10 11 12
2190+
show create view v8;
2191+
View Create View character_set_client collation_connection
2192+
v8 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v8` AS select `d3`.`a1` AS `b1`,`d3`.`a2` AS `b2`,`d3`.`a3` AS `b3` from (select `t1`.`c1` AS `a1`,`t1`.`c2` AS `a2`,`t1`.`c3` AS `a3` from `t1` union select `t2`.`c1` AS `c1`,`t2`.`c2` AS `c2`,`t2`.`c3` AS `c3` from `t2` order by `c1`) `d3`(`a1`,`a2`,`a3`) latin1 latin1_swedish_ci
21592193
# show materialization and condition pushdown into having
21602194
explain format=json select * from v4 where e3 < 10;
21612195
EXPLAIN
@@ -3169,9 +3203,9 @@ EXPLAIN
31693203
}
31703204
# pushdown through GROUP BY
31713205
create function f1(a int) returns int DETERMINISTIC return (a+1);
3172-
create view v6 as select * from
3206+
create view v9 as select * from
31733207
(select c1, f1(c2), sum(c3) from t1 group by c1, f1(c2)) as f (c1, c2, c3);
3174-
explain format=json select * from v6 where (c3+1) > 10 and c1 > 1 and c2 > 123;
3208+
explain format=json select * from v9 where (c3+1) > 10 and c1 > 1 and c2 > 123;
31753209
EXPLAIN
31763210
{
31773211
"query_block": {
@@ -5124,7 +5158,7 @@ where c1=d2 limit 1)
51245158
alter table t1 rename column c1 to cc1;
51255159
select * from v1;
51265160
ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
5127-
drop view v1, v2, v3, v4, v5, v6;
5161+
drop view v1, v2, v3, v4, v5, v6, v7, v8, v9;
51285162
drop table t1, t2, t3;
51295163
create table t10 (a int);
51305164
create table t20 (b int);
@@ -5148,14 +5182,14 @@ select * from v20;
51485182
c x u1 y1 b2
51495183
1 X U Y 1
51505184
3 NULL NULL NULL NULL
5151-
select * from v20 limit 9;
5185+
select * from v20 order by c limit 9;
51525186
c x u1 y1 b2
51535187
1 X U Y 1
51545188
3 NULL NULL NULL NULL
5155-
select * from v21;
5189+
select * from v21 order by c;
51565190
c x u1 b1
5157-
3 NULL NULL NULL
51585191
1 X U 1
5192+
3 NULL NULL NULL
51595193
drop view v10, v20, v11, v21;
51605194
drop table t10, t20, t30;
51615195
#

mysql-test/main/derived.test

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,15 @@ create view v5 as select * from
16401640
select c1, c2, sum(c3) as s from t2 group by c1, c2 having s > 3
16411641
)
16421642
as d2 (f1, f2, f3);
1643+
create view v6 as select a1 from
1644+
(
1645+
select * from t1 union select * from t2 order by c1
1646+
) as d3 (a1, a2, a3);
1647+
create view v7 (b1) as select a1 from (select c1 from t1) dt (a1);
1648+
create view v8 (b1, b2, b3) as select a1, a2, a3 from
1649+
(
1650+
select * from t1 union select * from t2 order by c1
1651+
) as d3 (a1, a2, a3);
16431652

16441653
--echo # test parent query mergability
16451654
--source include/explain-no-costs.inc
@@ -1655,6 +1664,12 @@ select a1, a2 from v1 union select b1, b2 from v2;
16551664
select * from v1 union select * from v2;
16561665
select * from v3 intersect select * from v2 union select * from v1;
16571666
select * from v4 where e3 < 10;
1667+
select * from v6;
1668+
show create view v6;
1669+
select * from v7;
1670+
show create view v7;
1671+
select * from v8;
1672+
show create view v8;
16581673
--echo # show materialization and condition pushdown into having
16591674
--source include/explain-no-costs.inc
16601675
explain format=json select * from v4 where e3 < 10;
@@ -1814,9 +1829,9 @@ explain format=json select * from v1 where if( a1 regexp 'def', 'foo', a2 )
18141829

18151830
--echo # pushdown through GROUP BY
18161831
create function f1(a int) returns int DETERMINISTIC return (a+1);
1817-
create view v6 as select * from
1832+
create view v9 as select * from
18181833
(select c1, f1(c2), sum(c3) from t1 group by c1, f1(c2)) as f (c1, c2, c3);
1819-
explain format=json select * from v6 where (c3+1) > 10 and c1 > 1 and c2 > 123;
1834+
explain format=json select * from v9 where (c3+1) > 10 and c1 > 1 and c2 > 123;
18201835
drop function f1;
18211836

18221837
--echo # name resolution for multi select units
@@ -2057,7 +2072,7 @@ alter table t1 rename column c1 to cc1;
20572072
--error ER_VIEW_INVALID
20582073
select * from v1;
20592074

2060-
drop view v1, v2, v3, v4, v5, v6;
2075+
drop view v1, v2, v3, v4, v5, v6, v7, v8, v9;
20612076
drop table t1, t2, t3;
20622077

20632078
# test derived embedded in views
@@ -2080,8 +2095,8 @@ create view v21 as select * from t30
20802095
left join (select 'X' as x, v11.u, v11.b1 from v11) dt2 (x, u1, b1)
20812096
on t30.c=dt2.b1 order by x;
20822097
select * from v20;
2083-
select * from v20 limit 9;
2084-
select * from v21;
2098+
select * from v20 order by c limit 9;
2099+
select * from v21 order by c;
20852100
drop view v10, v20, v11, v21;
20862101
drop table t10, t20, t30;
20872102

sql/sql_derived.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ bool mysql_derived_reinit(THD *thd, LEX *lex, TABLE_LIST *derived)
13541354
derived->get_unit()));
13551355
st_select_lex_unit *unit= derived->get_unit();
13561356

1357-
if (derived->original_names_are_saved)
1357+
if (derived->original_names_source)
13581358
unit->first_select()->set_item_list_names(derived->original_names);
13591359

13601360
// reset item names to that saved after wildcard expansion in JOIN::prepare

sql/sql_view.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,19 @@ bool mysql_create_view(THD *thd, TABLE_LIST *views,
662662
}
663663
#endif
664664

665+
/*
666+
Reset item list names within derived tables so that when reparsed in the
667+
view, references elsewhere within this select_lex can be correctly resolved
668+
*/
669+
for (SELECT_LEX *sl= lex->all_selects_list; sl; sl= sl->next_select_in_list())
670+
{
671+
for (TABLE_LIST *tl= sl->get_table_list(); tl && !res; tl= tl->next_local)
672+
{
673+
if (tl->original_names_source)
674+
tl->original_names_source->set_item_list_names(tl->original_names);
675+
}
676+
}
677+
665678
res= mysql_register_view(thd, &ddl_log_state, view, mode, backup_file_name);
666679

667680
/*

sql/table.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10336,7 +10336,7 @@ bool TABLE_LIST::save_original_names(st_select_lex *derived)
1033610336
{
1033710337
if (unlikely(derived->with_wild))
1033810338
return false;
10339-
if (original_names_are_saved)
10339+
if (original_names_source)
1034010340
return false;
1034110341

1034210342
// these elements allocated in LEX::parsed_derived_table
@@ -10356,7 +10356,7 @@ bool TABLE_LIST::save_original_names(st_select_lex *derived)
1035610356
(original_name= overwrite_iterator++))
1035710357
lex_string_set( original_name, item_list_element->name.str);
1035810358

10359-
original_names_are_saved= true;
10359+
original_names_source= derived;
1036010360
return false;
1036110361
}
1036210362

sql/table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,7 @@ struct TABLE_LIST
26402640
With_element *with; /* With element defining this table (if any) */
26412641
List<Lex_ident_sys> *column_names; /* list of correlation column names */
26422642
List<Lex_ident_sys> *original_names;/* list of original column names */
2643-
bool original_names_are_saved:1;
2643+
st_select_lex *original_names_source;
26442644
bool save_original_names(st_select_lex *derived);
26452645

26462646
/* Bitmap of the defining with element */

0 commit comments

Comments
 (0)