Skip to content

Commit 86d60fc

Browse files
committed
Merge remote-tracking branch 'origin/10.4' into 10.5
2 parents e0ba68b + ef96ec3 commit 86d60fc

File tree

10 files changed

+272
-55
lines changed

10 files changed

+272
-55
lines changed

mysql-test/main/table_value_constr.result

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
748748
3 MATERIALIZED <derived2> ALL NULL NULL NULL NULL 2 100.00
749749
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
750750
Warnings:
751-
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` semi join ((values (1)) `tvc_0`) where 1
751+
Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` semi join ((values (1)) `tvc_0`) where 1
752752
explain extended select * from t1
753753
where a in (select * from (values (1)) as tvc_0);
754754
id select_type table type possible_keys key key_len ref rows filtered Extra
@@ -983,7 +983,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
983983
3 MATERIALIZED <derived2> ALL NULL NULL NULL NULL 2 100.00
984984
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
985985
Warnings:
986-
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` semi join ((values (1),(2)) `tvc_0`) where 1
986+
Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` semi join ((values (1),(2)) `tvc_0`) where 1
987987
explain extended select * from t1
988988
where a = any (select * from (values (1),(2)) as tvc_0);
989989
id select_type table type possible_keys key key_len ref rows filtered Extra
@@ -2776,6 +2776,110 @@ id select_type table type possible_keys key key_len ref rows Extra
27762776
2 SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
27772777
drop table t1;
27782778
#
2779+
# MDEV-24910: TVC containing subquery used as a subselect
2780+
#
2781+
create table t1 (a int) engine=myisam;
2782+
insert into t1 values (3), (7), (1);
2783+
create table t2 (b int) engine=myisam;
2784+
insert into t2 values (1), (2);
2785+
select (values ((select 2))) from t2;
2786+
(values ((select 2)))
2787+
2
2788+
2
2789+
explain select (values ((select 2))) from t2;
2790+
id select_type table type possible_keys key key_len ref rows Extra
2791+
1 PRIMARY t2 ALL NULL NULL NULL NULL 2
2792+
4 SUBQUERY <derived2> ALL NULL NULL NULL NULL 2
2793+
2 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
2794+
Warnings:
2795+
Note 1249 Select 3 was reduced during optimization
2796+
prepare stmt from "select (values ((select 2))) from t2";
2797+
execute stmt;
2798+
(values ((select 2)))
2799+
2
2800+
2
2801+
execute stmt;
2802+
(values ((select 2)))
2803+
2
2804+
2
2805+
deallocate prepare stmt;
2806+
select (values ((select * from t1 where a > 10))) from t2;
2807+
(values ((select * from t1 where a > 10)))
2808+
NULL
2809+
NULL
2810+
explain select (values ((select * from t1 where a > 10))) from t2;
2811+
id select_type table type possible_keys key key_len ref rows Extra
2812+
1 PRIMARY t2 ALL NULL NULL NULL NULL 2
2813+
4 SUBQUERY <derived2> ALL NULL NULL NULL NULL 2
2814+
2 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
2815+
3 SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
2816+
prepare stmt from "select (values ((select * from t1 where a > 10))) from t2";
2817+
execute stmt;
2818+
(values ((select * from t1 where a > 10)))
2819+
NULL
2820+
NULL
2821+
execute stmt;
2822+
(values ((select * from t1 where a > 10)))
2823+
NULL
2824+
NULL
2825+
deallocate prepare stmt;
2826+
create table t3 (a int);
2827+
insert into t3 values
2828+
(3), (7), (7), (1), (3), (9), (7), (9), (8), (7), (8);
2829+
create view v1 as select count(a) as c from t3 group by a;
2830+
select
2831+
(values ((select * from t3 where a in (select * from v1))));
2832+
(values ((select * from t3 where a in (select * from v1))))
2833+
1
2834+
explain select
2835+
(values ((select * from t3 where a in (select * from v1))));
2836+
id select_type table type possible_keys key key_len ref rows Extra
2837+
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
2838+
6 SUBQUERY <derived2> ALL NULL NULL NULL NULL 2
2839+
2 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
2840+
3 SUBQUERY t3 ALL NULL NULL NULL NULL 11 Using where
2841+
3 SUBQUERY <derived5> ref key0 key0 8 test.t3.a 2 Using where; FirstMatch(t3)
2842+
5 DERIVED t3 ALL NULL NULL NULL NULL 11 Using temporary; Using filesort
2843+
prepare stmt from "select
2844+
(values ((select * from t3 where a in (select * from v1))))";
2845+
execute stmt;
2846+
(values ((select * from t3 where a in (select * from v1))))
2847+
1
2848+
execute stmt;
2849+
(values ((select * from t3 where a in (select * from v1))))
2850+
1
2851+
deallocate prepare stmt;
2852+
select
2853+
(values ((select * from t3
2854+
where a > 10 and a in (select * from v1))));
2855+
(values ((select * from t3
2856+
where a > 10 and a in (select * from v1))))
2857+
NULL
2858+
explain select
2859+
(values ((select * from t3
2860+
where a > 10 and a in (select * from v1))));
2861+
id select_type table type possible_keys key key_len ref rows Extra
2862+
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
2863+
6 SUBQUERY <derived2> ALL NULL NULL NULL NULL 2
2864+
2 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
2865+
3 SUBQUERY t3 ALL NULL NULL NULL NULL 11 Using where
2866+
3 SUBQUERY <derived5> ref key0 key0 8 test.t3.a 2 Using where; FirstMatch(t3)
2867+
5 DERIVED t3 ALL NULL NULL NULL NULL 11 Using temporary; Using filesort
2868+
prepare stmt from "select
2869+
(values ((select * from t3
2870+
where a > 10 and a in (select * from v1))))";
2871+
execute stmt;
2872+
(values ((select * from t3
2873+
where a > 10 and a in (select * from v1))))
2874+
NULL
2875+
execute stmt;
2876+
(values ((select * from t3
2877+
where a > 10 and a in (select * from v1))))
2878+
NULL
2879+
deallocate prepare stmt;
2880+
drop view v1;
2881+
drop table t1,t2,t3;
2882+
#
27792883
# End of 10.3 tests
27802884
#
27812885
#

mysql-test/main/table_value_constr.test

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,63 @@ eval explain $q3;
14591459

14601460
drop table t1;
14611461

1462+
--echo #
1463+
--echo # MDEV-24910: TVC containing subquery used as a subselect
1464+
--echo #
1465+
1466+
create table t1 (a int) engine=myisam;
1467+
insert into t1 values (3), (7), (1);
1468+
create table t2 (b int) engine=myisam;
1469+
insert into t2 values (1), (2);
1470+
1471+
let $q1=
1472+
select (values ((select 2))) from t2;
1473+
eval $q1;
1474+
eval explain $q1;
1475+
eval prepare stmt from "$q1";
1476+
execute stmt;
1477+
execute stmt;
1478+
deallocate prepare stmt;
1479+
1480+
let $q2=
1481+
select (values ((select * from t1 where a > 10))) from t2;
1482+
eval $q2;
1483+
eval explain $q2;
1484+
eval prepare stmt from "$q2";
1485+
execute stmt;
1486+
execute stmt;
1487+
deallocate prepare stmt;
1488+
1489+
create table t3 (a int);
1490+
insert into t3 values
1491+
(3), (7), (7), (1), (3), (9), (7), (9), (8), (7), (8);
1492+
1493+
create view v1 as select count(a) as c from t3 group by a;
1494+
1495+
let $q3=
1496+
select
1497+
(values ((select * from t3 where a in (select * from v1))));
1498+
eval $q3;
1499+
eval explain $q3;
1500+
eval prepare stmt from "$q3";
1501+
execute stmt;
1502+
execute stmt;
1503+
deallocate prepare stmt;
1504+
1505+
let $q4=
1506+
select
1507+
(values ((select * from t3
1508+
where a > 10 and a in (select * from v1))));
1509+
eval $q4;
1510+
eval explain $q4;
1511+
eval prepare stmt from "$q4";
1512+
execute stmt;
1513+
execute stmt;
1514+
deallocate prepare stmt;
1515+
1516+
drop view v1;
1517+
drop table t1,t2,t3;
1518+
14621519
--echo #
14631520
--echo # End of 10.3 tests
14641521
--echo #

mysql-test/suite/compat/oracle/r/table_value_constr.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
746746
3 MATERIALIZED <derived2> ALL NULL NULL NULL NULL 2 100.00
747747
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
748748
Warnings:
749-
Note 1003 select "test"."t1"."a" AS "a","test"."t1"."b" AS "b" from "test"."t1" semi join ((values (1)) "tvc_0") where 1
749+
Note 1003 /* select#1 */ select "test"."t1"."a" AS "a","test"."t1"."b" AS "b" from "test"."t1" semi join ((values (1)) "tvc_0") where 1
750750
explain extended select * from t1
751751
where a in (select * from (values (1)) as tvc_0);
752752
id select_type table type possible_keys key key_len ref rows filtered Extra
@@ -981,7 +981,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
981981
3 MATERIALIZED <derived2> ALL NULL NULL NULL NULL 2 100.00
982982
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
983983
Warnings:
984-
Note 1003 select "test"."t1"."a" AS "a","test"."t1"."b" AS "b" from "test"."t1" semi join ((values (1),(2)) "tvc_0") where 1
984+
Note 1003 /* select#1 */ select "test"."t1"."a" AS "a","test"."t1"."b" AS "b" from "test"."t1" semi join ((values (1),(2)) "tvc_0") where 1
985985
explain extended select * from t1
986986
where a = any (select * from (values (1),(2)) as tvc_0);
987987
id select_type table type possible_keys key key_len ref rows filtered Extra

sql/sql_lex.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,6 +3068,8 @@ void st_select_lex_node::add_slave(st_select_lex_node *slave_arg)
30683068
{
30693069
slave= slave_arg;
30703070
slave_arg->master= this;
3071+
slave->prev= &master->slave;
3072+
slave->next= 0;
30713073
}
30723074
}
30733075

@@ -3089,6 +3091,27 @@ void st_select_lex_node::link_chain_down(st_select_lex_node *first)
30893091
slave= first;
30903092
}
30913093

3094+
/*
3095+
@brief
3096+
Substitute this node in select tree for a newly creates node
3097+
3098+
@param subst the node to substitute for
3099+
3100+
@details
3101+
The function substitute this node in the select tree for a newly
3102+
created node subst. This node is just removed from the tree but all
3103+
its link fields and the attached sub-tree remain untouched.
3104+
*/
3105+
3106+
void st_select_lex_node::substitute_in_tree(st_select_lex_node *subst)
3107+
{
3108+
if ((subst->next= next))
3109+
next->prev= &subst->next;
3110+
subst->prev= prev;
3111+
(*prev)= subst;
3112+
subst->master= master;
3113+
}
3114+
30923115
/*
30933116
include on level down (but do not link)
30943117

sql/sql_lex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ class st_select_lex_node {
793793
link_next= NULL;
794794
link_prev= NULL;
795795
}
796-
796+
void substitute_in_tree(st_select_lex_node *subst);
797797

798798
void set_slave(st_select_lex_node *slave_arg) { slave= slave_arg; }
799799
void move_node(st_select_lex_node *where_to_move)

sql/sql_tvc.cc

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -685,44 +685,61 @@ st_select_lex *wrap_tvc(THD *thd, st_select_lex *tvc_sl,
685685

686686
Query_arena backup;
687687
Query_arena *arena= thd->activate_stmt_arena_if_needed(&backup);
688+
689+
Item *item;
690+
SELECT_LEX *wrapper_sl;
691+
SELECT_LEX_UNIT *derived_unit;
692+
688693
/*
689-
Create SELECT_LEX of the select used in the result of transformation
694+
Create SELECT_LEX wrapper_sl of the select used in the result
695+
of the transformation
690696
*/
691-
lex->current_select= tvc_sl;
692-
if (mysql_new_select(lex, 0, NULL))
697+
if (!(wrapper_sl= new (thd->mem_root) SELECT_LEX()))
693698
goto err;
694-
mysql_init_select(lex);
695-
/* Create item list as '*' for the subquery SQ */
696-
Item *item;
697-
SELECT_LEX *wrapper_sl;
698-
wrapper_sl= lex->current_select;
699+
wrapper_sl->select_number= ++thd->lex->stmt_lex->current_select_number;
700+
wrapper_sl->parent_lex= lex; /* Used in init_query. */
701+
wrapper_sl->init_query();
702+
wrapper_sl->init_select();
703+
704+
wrapper_sl->nest_level= tvc_sl->nest_level;
705+
wrapper_sl->parsing_place= tvc_sl->parsing_place;
699706
wrapper_sl->set_linkage(tvc_sl->get_linkage());
700-
wrapper_sl->parsing_place= SELECT_LIST;
707+
708+
lex->current_select= wrapper_sl;
701709
item= new (thd->mem_root) Item_field(thd, &wrapper_sl->context,
702710
star_clex_str);
703711
if (item == NULL || add_item_to_list(thd, item))
704712
goto err;
705713
(wrapper_sl->with_wild)++;
706-
707-
/* Exclude SELECT with TVC */
708-
tvc_sl->exclude();
714+
715+
/* Include the newly created select into the global list of selects */
716+
wrapper_sl->include_global((st_select_lex_node**)&lex->all_selects_list);
717+
718+
/* Substitute select node used of TVC for the newly created select */
719+
tvc_sl->substitute_in_tree(wrapper_sl);
720+
709721
/*
710-
Create derived table DT that will wrap TVC in the result of transformation
722+
Create a unit for the substituted select used for TVC and attach it
723+
to the the wrapper select wrapper_sl as the only unit. The created
724+
unit is the unit for the derived table tvc_x of the transformation.
711725
*/
712-
SELECT_LEX *tvc_select; // select for tvc
713-
SELECT_LEX_UNIT *derived_unit; // unit for tvc_select
714-
if (mysql_new_select(lex, 1, tvc_sl))
726+
if (!(derived_unit= new (thd->mem_root) SELECT_LEX_UNIT()))
715727
goto err;
716-
tvc_select= lex->current_select;
717-
derived_unit= tvc_select->master_unit();
718-
tvc_select->set_linkage(DERIVED_TABLE_TYPE);
728+
derived_unit->init_query();
729+
derived_unit->thd= thd;
730+
derived_unit->include_down(wrapper_sl);
719731

720-
lex->current_select= wrapper_sl;
732+
/*
733+
Attach the select used of TVC as the only slave to the unit for
734+
the derived table tvc_x of the transformation
735+
*/
736+
derived_unit->add_slave(tvc_sl);
737+
tvc_sl->set_linkage(DERIVED_TABLE_TYPE);
721738

722739
/*
723-
Create the name of the wrapping derived table and
724-
add it to the FROM list of the wrapper
725-
*/
740+
Generate the name of the derived table created for TVC and
741+
add it to the FROM list of the wrapping select
742+
*/
726743
Table_ident *ti;
727744
LEX_CSTRING alias;
728745
TABLE_LIST *derived_tab;
@@ -741,10 +758,6 @@ st_select_lex *wrap_tvc(THD *thd, st_select_lex *tvc_sl,
741758
wrapper_sl->table_list.first->derived_type= DTYPE_TABLE | DTYPE_MATERIALIZE;
742759
lex->derived_tables|= DERIVED_SUBQUERY;
743760

744-
wrapper_sl->where= 0;
745-
wrapper_sl->set_braces(false);
746-
derived_unit->set_with_clause(0);
747-
748761
if (arena)
749762
thd->restore_active_arena(arena, &backup);
750763
thd->lex->result= save_result;

0 commit comments

Comments
 (0)