Skip to content

Commit 47e9678

Browse files
committed
MDEV-29022 add_slave destroy child list and has dead code
Nowdays subquery in a UNION's ORDER BY placed correctly in fake select, the only problem was incorrect Name_resolution_contect is fixed by this patch in parsing, so we do not need scanning/reseting of ORDER BY of a union.
1 parent 2b89598 commit 47e9678

File tree

10 files changed

+165
-74
lines changed

10 files changed

+165
-74
lines changed

mysql-test/main/union.result

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,5 +2664,90 @@ ALTER TABLE t4 ADD INDEX (`NULL`);
26642664
DROP TABLE t1, t2, t3, t4;
26652665
set @@default_storage_engine=@save_default_storage_engine;
26662666
#
2667+
# MDEV-29022: add_slave destroy child list and has dead code
2668+
# (test added to be sure that ordering by several subqueries works)
2669+
#
2670+
create table t1 (aa int);
2671+
insert into t1 values (-1),(0),(1),(2),(3),(4),(5),(6),(98),(99),(100),(102);
2672+
create table t2 (a int, b int);
2673+
insert into t2 values (2,2),(2,3),(3,4),(3,5);
2674+
select a as a, b as b,
2675+
(select max(aa) from t1 where aa < t2.a) as c,
2676+
(select max(aa) from t1 where aa < t2.b) as d
2677+
from t2
2678+
union select 0 as a, 100 as b,
2679+
(select max(aa) from t1 where aa < 0) as c,
2680+
(select max(aa) from t1 where aa < 100) as d
2681+
union select 0 as a, 99 as b,
2682+
(select max(aa) from t1 where aa < 0) as c,
2683+
(select max(aa) from t1 where aa < 99) as d
2684+
order by (select max(aa) from t1 where aa < a),
2685+
(select max(aa) from t1 where aa < b);
2686+
a b c d
2687+
0 99 -1 98
2688+
0 100 -1 99
2689+
2 2 1 1
2690+
2 3 1 2
2691+
3 4 2 3
2692+
3 5 2 4
2693+
select a as a, b as b,
2694+
(select max(aa) from t1 where aa < t2.a) as c,
2695+
(select 200 - max(aa) from t1 where aa < t2.b) as d
2696+
from t2
2697+
union select 0 as a, 100 as b,
2698+
(select max(aa) from t1 where aa < 0) as c,
2699+
(select 200 - max(aa) from t1 where aa < 100) as d
2700+
union select 0 as a, 99 as b,
2701+
(select max(aa) from t1 where aa < 0) as c,
2702+
(select 200 - max(aa) from t1 where aa < 99) as d
2703+
order by (select max(aa) from t1 where aa < a),
2704+
(select 200 - max(aa) from t1 where aa < b);
2705+
a b c d
2706+
0 100 -1 101
2707+
0 99 -1 102
2708+
2 3 1 198
2709+
2 2 1 199
2710+
3 5 2 196
2711+
3 4 2 197
2712+
(select a as a, b as b,
2713+
(select max(aa) from t1 where aa < t2.a) as c,
2714+
(select max(aa) from t1 where aa < t2.b) as d
2715+
from t2)
2716+
union (select 0 as a, 100 as b,
2717+
(select max(aa) from t1 where aa < 0) as c,
2718+
(select max(aa) from t1 where aa < 100) as d)
2719+
union (select 0 as a, 99 as b,
2720+
(select max(aa) from t1 where aa < 0) as c,
2721+
(select max(aa) from t1 where aa < 99) as d)
2722+
order by (select max(aa) from t1 where aa < a),
2723+
(select max(aa) from t1 where aa < b);
2724+
a b c d
2725+
0 99 -1 98
2726+
0 100 -1 99
2727+
2 2 1 1
2728+
2 3 1 2
2729+
3 4 2 3
2730+
3 5 2 4
2731+
(select a as a, b as b,
2732+
(select max(aa) from t1 where aa < t2.a) as c,
2733+
(select 200 - max(aa) from t1 where aa < t2.b) as d
2734+
from t2)
2735+
union (select 0 as a, 100 as b,
2736+
(select max(aa) from t1 where aa < 0) as c,
2737+
(select 200 - max(aa) from t1 where aa < 100) as d)
2738+
union (select 0 as a, 99 as b,
2739+
(select max(aa) from t1 where aa < 0) as c,
2740+
(select 200 - max(aa) from t1 where aa < 99) as d)
2741+
order by (select max(aa) from t1 where aa < a),
2742+
(select 200 - max(aa) from t1 where aa < b);
2743+
a b c d
2744+
0 100 -1 101
2745+
0 99 -1 102
2746+
2 3 1 198
2747+
2 2 1 199
2748+
3 5 2 196
2749+
3 4 2 197
2750+
drop table t1,t2;
2751+
#
26672752
# End of 10.3 tests
26682753
#

mysql-test/main/union.test

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,76 @@ DROP TABLE t1, t2, t3, t4;
18981898

18991899
set @@default_storage_engine=@save_default_storage_engine;
19001900

1901+
--echo #
1902+
--echo # MDEV-29022: add_slave destroy child list and has dead code
1903+
--echo # (test added to be sure that ordering by several subqueries works)
1904+
--echo #
1905+
1906+
create table t1 (aa int);
1907+
1908+
insert into t1 values (-1),(0),(1),(2),(3),(4),(5),(6),(98),(99),(100),(102);
1909+
1910+
1911+
create table t2 (a int, b int);
1912+
1913+
insert into t2 values (2,2),(2,3),(3,4),(3,5);
1914+
1915+
1916+
select a as a, b as b,
1917+
(select max(aa) from t1 where aa < t2.a) as c,
1918+
(select max(aa) from t1 where aa < t2.b) as d
1919+
from t2
1920+
union select 0 as a, 100 as b,
1921+
(select max(aa) from t1 where aa < 0) as c,
1922+
(select max(aa) from t1 where aa < 100) as d
1923+
union select 0 as a, 99 as b,
1924+
(select max(aa) from t1 where aa < 0) as c,
1925+
(select max(aa) from t1 where aa < 99) as d
1926+
order by (select max(aa) from t1 where aa < a),
1927+
(select max(aa) from t1 where aa < b);
1928+
1929+
select a as a, b as b,
1930+
(select max(aa) from t1 where aa < t2.a) as c,
1931+
(select 200 - max(aa) from t1 where aa < t2.b) as d
1932+
from t2
1933+
union select 0 as a, 100 as b,
1934+
(select max(aa) from t1 where aa < 0) as c,
1935+
(select 200 - max(aa) from t1 where aa < 100) as d
1936+
union select 0 as a, 99 as b,
1937+
(select max(aa) from t1 where aa < 0) as c,
1938+
(select 200 - max(aa) from t1 where aa < 99) as d
1939+
order by (select max(aa) from t1 where aa < a),
1940+
(select 200 - max(aa) from t1 where aa < b);
1941+
1942+
1943+
(select a as a, b as b,
1944+
(select max(aa) from t1 where aa < t2.a) as c,
1945+
(select max(aa) from t1 where aa < t2.b) as d
1946+
from t2)
1947+
union (select 0 as a, 100 as b,
1948+
(select max(aa) from t1 where aa < 0) as c,
1949+
(select max(aa) from t1 where aa < 100) as d)
1950+
union (select 0 as a, 99 as b,
1951+
(select max(aa) from t1 where aa < 0) as c,
1952+
(select max(aa) from t1 where aa < 99) as d)
1953+
order by (select max(aa) from t1 where aa < a),
1954+
(select max(aa) from t1 where aa < b);
1955+
1956+
(select a as a, b as b,
1957+
(select max(aa) from t1 where aa < t2.a) as c,
1958+
(select 200 - max(aa) from t1 where aa < t2.b) as d
1959+
from t2)
1960+
union (select 0 as a, 100 as b,
1961+
(select max(aa) from t1 where aa < 0) as c,
1962+
(select 200 - max(aa) from t1 where aa < 100) as d)
1963+
union (select 0 as a, 99 as b,
1964+
(select max(aa) from t1 where aa < 0) as c,
1965+
(select 200 - max(aa) from t1 where aa < 99) as d)
1966+
order by (select max(aa) from t1 where aa < a),
1967+
(select 200 - max(aa) from t1 where aa < b);
1968+
1969+
drop table t1,t2;
1970+
19011971
--echo #
19021972
--echo # End of 10.3 tests
19031973
--echo #

sql/item.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,6 @@ class Item: public Value_source,
17581758
virtual bool enumerate_field_refs_processor(void *arg) { return 0; }
17591759
virtual bool mark_as_eliminated_processor(void *arg) { return 0; }
17601760
virtual bool eliminate_subselect_processor(void *arg) { return 0; }
1761-
virtual bool set_fake_select_as_master_processor(void *arg) { return 0; }
17621761
virtual bool view_used_tables_processor(void *arg) { return 0; }
17631762
virtual bool eval_not_null_tables(void *arg) { return 0; }
17641763
virtual bool is_subquery_processor(void *arg) { return 0; }

sql/item_subselect.cc

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -386,50 +386,6 @@ bool Item_subselect::eliminate_subselect_processor(void *arg)
386386
}
387387

388388

389-
/**
390-
Adjust the master select of the subquery to be the fake_select which
391-
represents the whole UNION right above the subquery, instead of the
392-
last query of the UNION.
393-
394-
@param arg pointer to the fake select
395-
396-
@return
397-
FALSE to force the evaluation of the processor for the subsequent items.
398-
*/
399-
400-
bool Item_subselect::set_fake_select_as_master_processor(void *arg)
401-
{
402-
SELECT_LEX *fake_select= (SELECT_LEX*) arg;
403-
/*
404-
Move the st_select_lex_unit of a subquery from a global ORDER BY clause to
405-
become a direct child of the fake_select of a UNION. In this way the
406-
ORDER BY that is applied to the temporary table that contains the result of
407-
the whole UNION, and all columns in the subquery are resolved against this
408-
table. The transformation is applied only for immediate child subqueries of
409-
a UNION query.
410-
*/
411-
if (unit->outer_select()->master_unit()->fake_select_lex == fake_select)
412-
{
413-
/*
414-
Set the master of the subquery to be the fake select (i.e. the whole
415-
UNION), instead of the last query in the UNION.
416-
*/
417-
fake_select->add_slave(unit);
418-
DBUG_ASSERT(unit->outer_select() == fake_select);
419-
/* Adjust the name resolution context hierarchy accordingly. */
420-
for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select())
421-
sl->context.outer_context= &(fake_select->context);
422-
/*
423-
Undo Item_subselect::eliminate_subselect_processor because at that phase
424-
we don't know yet that the ORDER clause will be moved to the fake select.
425-
*/
426-
unit->item= this;
427-
eliminated= FALSE;
428-
}
429-
return FALSE;
430-
}
431-
432-
433389
bool Item_subselect::mark_as_dependent(THD *thd, st_select_lex *select,
434390
Item *item)
435391
{

sql/item_subselect.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ class Item_subselect :public Item_result_field,
238238
bool walk(Item_processor processor, bool walk_subquery, void *arg);
239239
bool mark_as_eliminated_processor(void *arg);
240240
bool eliminate_subselect_processor(void *arg);
241-
bool set_fake_select_as_master_processor(void *arg);
242241
bool enumerate_field_refs_processor(void *arg);
243242
bool check_vcol_func_processor(void *arg)
244243
{

sql/sql_lex.cc

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,23 +2434,9 @@ void st_select_lex_node::include_down(st_select_lex_node *upper)
24342434
}
24352435

24362436

2437-
void st_select_lex_node::add_slave(st_select_lex_node *slave_arg)
2437+
void st_select_lex_node::attach_single(st_select_lex_node *slave_arg)
24382438
{
2439-
for (; slave; slave= slave->next)
2440-
if (slave == slave_arg)
2441-
return;
2442-
2443-
if (slave)
2444-
{
2445-
st_select_lex_node *slave_arg_slave= slave_arg->slave;
2446-
/* Insert in the front of list of slaves if any. */
2447-
slave_arg->include_neighbour(slave);
2448-
/* include_neighbour() sets slave_arg->slave=0, restore it. */
2449-
slave_arg->slave= slave_arg_slave;
2450-
/* Count on include_neighbour() setting the master. */
2451-
DBUG_ASSERT(slave_arg->master == this);
2452-
}
2453-
else
2439+
DBUG_ASSERT(slave == 0);
24542440
{
24552441
slave= slave_arg;
24562442
slave_arg->master= this;

sql/sql_lex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ class st_select_lex_node {
705705

706706
inline st_select_lex_node* get_master() { return master; }
707707
void include_down(st_select_lex_node *upper);
708-
void add_slave(st_select_lex_node *slave_arg);
708+
void attach_single(st_select_lex_node *slave_arg);
709709
void include_neighbour(st_select_lex_node *before);
710710
void include_standalone(st_select_lex_node *sel, st_select_lex_node **ref);
711711
void include_global(st_select_lex_node **plink);

sql/sql_tvc.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ st_select_lex *wrap_tvc(THD *thd, st_select_lex *tvc_sl,
708708
Attach the select used of TVC as the only slave to the unit for
709709
the derived table tvc_x of the transformation
710710
*/
711-
derived_unit->add_slave(tvc_sl);
711+
derived_unit->attach_single(tvc_sl);
712712
tvc_sl->linkage= DERIVED_TABLE_TYPE;
713713

714714
/*

sql/sql_union.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -633,15 +633,6 @@ st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg,
633633
order= order->next)
634634
order->item= &order->item_ptr;
635635
}
636-
for (ORDER *order= global_parameters()->order_list.first;
637-
order;
638-
order=order->next)
639-
{
640-
(*order->item)->walk(&Item::change_context_processor, 0,
641-
&fake_select_lex->context);
642-
(*order->item)->walk(&Item::set_fake_select_as_master_processor, 0,
643-
fake_select_lex);
644-
}
645636
}
646637

647638

sql/sql_yacc.yy

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12910,11 +12910,16 @@ order_clause:
1291012910
*/
1291112911
DBUG_ASSERT(sel->master_unit()->fake_select_lex);
1291212912
lex->current_select= sel->master_unit()->fake_select_lex;
12913+
lex->push_context(&sel->master_unit()->fake_select_lex->context, thd->mem_root);
1291312914
}
1291412915
}
1291512916
order_list
1291612917
{
12917-
12918+
if (Lex->current_select ==
12919+
Lex->current_select->master_unit()->fake_select_lex)
12920+
{
12921+
Lex->pop_context();
12922+
}
1291812923
}
1291912924
;
1292012925

0 commit comments

Comments
 (0)