Skip to content

Commit de02bfd

Browse files
committed
The code that pushed conditions into derived did not
take into account that the list of equal items in an Item_equal object may contain items with type() == REF_ITEM.
1 parent 7faff4d commit de02bfd

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

mysql-test/r/derived_view.result

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,6 @@ f1 f11
513513
3 3
514514
5 5
515515
mat in merged derived join mat in merged derived
516-
set @save_optimizer_switch=@@optimizer_switch;
517-
set optimizer_switch='condition_pushdown_for_derived=off';
518516
explain extended select * from
519517
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) x
520518
join
@@ -526,7 +524,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
526524
5 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
527525
3 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
528526
Warnings:
529-
Note 1003 select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11`,`tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where (`test`.`t1`.`f1` < 7) group by `test`.`t1`.`f1`) `tt` join (select `t1`.`f1` AS `f1`,`t1`.`f11` AS `f11` from `test`.`t1` where (`t1`.`f1` < 7) group by `t1`.`f1`) `tt` where ((`tt`.`f1` = `tt`.`f1`) and (`tt`.`f1` > 2) and (`tt`.`f1` > 2))
527+
Note 1003 select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11`,`tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where ((`test`.`t1`.`f1` < 7) and (`test`.`t1`.`f1` > 2) and (`test`.`t1`.`f1` > 2)) group by `test`.`t1`.`f1`) `tt` join (select `t1`.`f1` AS `f1`,`t1`.`f11` AS `f11` from `test`.`t1` where ((`t1`.`f1` < 7) and (`t1`.`f1` > 2) and (`t1`.`f1` > 2)) group by `t1`.`f1`) `tt` where ((`tt`.`f1` = `tt`.`f1`) and (`tt`.`f1` > 2) and (`tt`.`f1` > 2))
530528
explain format=json select * from
531529
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) x
532530
join
@@ -553,7 +551,7 @@ EXPLAIN
553551
"access_type": "ALL",
554552
"rows": 11,
555553
"filtered": 100,
556-
"attached_condition": "(t1.f1 < 7)"
554+
"attached_condition": "((t1.f1 < 7) and (t1.f1 > 2) and (t1.f1 > 2))"
557555
}
558556
}
559557
}
@@ -581,7 +579,7 @@ EXPLAIN
581579
"access_type": "ALL",
582580
"rows": 11,
583581
"filtered": 100,
584-
"attached_condition": "(t1.f1 < 7)"
582+
"attached_condition": "((t1.f1 < 7) and (t1.f1 > 2) and (t1.f1 > 2))"
585583
}
586584
}
587585
}
@@ -606,11 +604,10 @@ Handler_read_key 2
606604
Handler_read_last 0
607605
Handler_read_next 2
608606
Handler_read_prev 0
609-
Handler_read_rnd 8
607+
Handler_read_rnd 4
610608
Handler_read_rnd_deleted 0
611-
Handler_read_rnd_next 39
609+
Handler_read_rnd_next 33
612610
flush status;
613-
set optimizer_switch=@save_optimizer_switch;
614611
merged in merged derived join merged in merged derived
615612
explain extended select * from
616613
(select * from
@@ -857,9 +854,51 @@ explain extended select * from v6 join v7 on f2=f1;
857854
id select_type table type possible_keys key key_len ref rows filtered Extra
858855
1 PRIMARY t2 ALL NULL NULL NULL NULL 11 100.00 Using where
859856
1 PRIMARY <derived5> ref key0 key0 5 test.t2.f2 2 100.00
860-
5 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using temporary; Using filesort
857+
5 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
861858
Warnings:
862859
Note 1003 select `test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22`,`v1`.`f1` AS `f1`,`v1`.`f11` AS `f11` from `test`.`t2` join `test`.`v1` where ((`v1`.`f1` = `test`.`t2`.`f2`) and (`test`.`t2`.`f2` < 7) and (`test`.`t2`.`f2` in (2,3)))
860+
explain format=json select * from v6 join v7 on f2=f1;
861+
EXPLAIN
862+
{
863+
"query_block": {
864+
"select_id": 1,
865+
"table": {
866+
"table_name": "t2",
867+
"access_type": "ALL",
868+
"rows": 11,
869+
"filtered": 100,
870+
"attached_condition": "((t2.f2 < 7) and (t2.f2 in (2,3)) and (t2.f2 is not null))"
871+
},
872+
"table": {
873+
"table_name": "<derived5>",
874+
"access_type": "ref",
875+
"possible_keys": ["key0"],
876+
"key": "key0",
877+
"key_length": "5",
878+
"used_key_parts": ["f1"],
879+
"ref": ["test.t2.f2"],
880+
"rows": 2,
881+
"filtered": 100,
882+
"materialized": {
883+
"query_block": {
884+
"select_id": 5,
885+
"filesort": {
886+
"sort_key": "t1.f1",
887+
"temporary_table": {
888+
"table": {
889+
"table_name": "t1",
890+
"access_type": "ALL",
891+
"rows": 11,
892+
"filtered": 100,
893+
"attached_condition": "(t1.f1 in (2,3))"
894+
}
895+
}
896+
}
897+
}
898+
}
899+
}
900+
}
901+
}
863902
select * from v6 join v7 on f2=f1;
864903
f2 f22 f1 f11
865904
3 3 3 3

mysql-test/t/derived_view.test

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ select * from (select * from
143143
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) zz;
144144

145145
--echo mat in merged derived join mat in merged derived
146-
set @save_optimizer_switch=@@optimizer_switch;
147-
set optimizer_switch='condition_pushdown_for_derived=off';
148146
explain extended select * from
149147
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) x
150148
join
@@ -164,7 +162,6 @@ join
164162
on x.f1 = z.f1;
165163
show status like 'Handler_read%';
166164
flush status;
167-
set optimizer_switch=@save_optimizer_switch;
168165

169166
--echo merged in merged derived join merged in merged derived
170167
explain extended select * from
@@ -232,6 +229,7 @@ select * from (select * from v7 group by 1) tt;
232229

233230
--echo join of above two
234231
explain extended select * from v6 join v7 on f2=f1;
232+
explain format=json select * from v6 join v7 on f2=f1;
235233
select * from v6 join v7 on f2=f1;
236234

237235
--echo test two keys

sql/item.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6800,12 +6800,12 @@ Item *Item_field::derived_field_transformer_for_having(THD *thd, uchar *arg)
68006800
Item *item;
68016801
while ((item=li++))
68026802
{
6803-
if (item->used_tables() == map && item->type() == FIELD_ITEM)
6803+
if (item->used_tables() == map && item->real_item()->type() == FIELD_ITEM)
68046804
{
68056805
Item_ref *rf=
68066806
new (thd->mem_root) Item_ref(thd, &sl->context,
68076807
NullS, NullS,
6808-
((Item_field*) item)->field_name);
6808+
((Item_field*) (item->real_item()))->field_name);
68096809
if (!rf)
68106810
return 0;
68116811
return rf;
@@ -6836,11 +6836,11 @@ Item *Item_field::derived_field_transformer_for_where(THD *thd, uchar *arg)
68366836
Item *item;
68376837
while ((item=it++))
68386838
{
6839-
if (item->used_tables() == map && item->type() == FIELD_ITEM)
6839+
if (item->used_tables() == map && item->real_item()->type() == FIELD_ITEM)
68406840
{
6841-
Item_field *field_item= (Item_field *) item;
6841+
Item_field *field_item= (Item_field *) (item->real_item());
68426842
li.rewind();
6843-
uint field_no= ((Item_field*) this)->field->field_index;
6843+
uint field_no= field_item->field->field_index;
68446844
for (uint i= 0; i <= field_no; i++)
68456845
producing_item= li++;
68466846
return producing_item->build_clone(thd, thd->mem_root);
@@ -6873,9 +6873,9 @@ Item *Item_field::derived_grouping_field_transformer_for_where(THD *thd,
68736873
Item *item;
68746874
while ((item=it++))
68756875
{
6876-
if (item->used_tables() == map && item->type() == FIELD_ITEM)
6876+
if (item->used_tables() == map && item->real_item()->type() == FIELD_ITEM)
68776877
{
6878-
Item_field *field_item= (Item_field *) item;
6878+
Item_field *field_item= (Item_field *) (item->real_item());
68796879
li.rewind();
68806880
while ((field=li++))
68816881
{
@@ -10147,12 +10147,12 @@ bool Item_field::exclusive_dependence_on_grouping_fields_processor(void *arg)
1014710147
Item *item;
1014810148
while ((item=it++))
1014910149
{
10150-
if (item->used_tables() == map && item->type() == FIELD_ITEM)
10150+
if (item->used_tables() == map && item->real_item()->type() == FIELD_ITEM)
1015110151
{
1015210152
li.rewind();
1015310153
while ((field=li++))
1015410154
{
10155-
if (((Item_field *)item)->field == field->tmp_field)
10155+
if (((Item_field *)(item->real_item()))->field == field->tmp_field)
1015610156
return false;
1015710157
}
1015810158
}

0 commit comments

Comments
 (0)