Skip to content

Commit 2e78910

Browse files
committed
MDEV-25635 Assertion failure when pushing from HAVING into WHERE of view
This bug could manifest itself after pushing a where condition over a mergeable derived table / view / CTE DT into a grouping view / derived table / CTE V whose item list contained set functions with constant arguments such as MIN(2), SUM(1) etc. In such cases the field references used in the condition pushed into the view V that correspond set functions are wrapped into Item_direct_view_ref wrappers. Due to a wrong implementation of the virtual method const_item() for the class Item_direct_view_ref the wrapped set functions with constant arguments could be erroneously taken for constant items. This could lead to a wrong result set returned by the main select query in 10.2. In 10.4 where a possibility of pushing condition from HAVING into WHERE had been added this could cause a crash. Approved by Sergey Petrunya <sergey.petrunya@mariadb.com>
1 parent a8434c6 commit 2e78910

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

mysql-test/r/derived_cond_pushdown.result

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10634,4 +10634,43 @@ m
1063410634
7
1063510635
drop view v1;
1063610636
drop table t1;
10637+
#
10638+
# MDEV-25635: pushdown into grouping view using aggregate functions
10639+
# with constant arguments via a mergeable derived table
10640+
#
10641+
create table t1 (a int);
10642+
insert into t1 values (3), (7), (1), (3), (7), (7), (3);
10643+
create view v1 as select a, sum(1) as f, sum(1) as g from t1 group by a;
10644+
select * from v1;
10645+
a f g
10646+
1 1 1
10647+
3 3 3
10648+
7 3 3
10649+
select * from (select * from v1) as dt where a=f and a=g;
10650+
a f g
10651+
1 1 1
10652+
3 3 3
10653+
explain extended select * from (select * from v1) as dt where a=f and a=g;
10654+
id select_type table type possible_keys key key_len ref rows filtered Extra
10655+
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 7 100.00 Using where
10656+
3 DERIVED t1 ALL NULL NULL NULL NULL 7 100.00 Using temporary; Using filesort
10657+
Warnings:
10658+
Note 1003 select `v1`.`a` AS `a`,`v1`.`f` AS `f`,`v1`.`g` AS `g` from `test`.`v1` where `v1`.`a` = `v1`.`f` and `v1`.`a` = `v1`.`g`
10659+
create view v2 as select a, min(1) as f, min(1) as g from t1 group by a;
10660+
select * from v2;
10661+
a f g
10662+
1 1 1
10663+
3 1 1
10664+
7 1 1
10665+
select * from (select * from v2) as dt where a=f and a=g;
10666+
a f g
10667+
1 1 1
10668+
explain extended select * from (select * from v2) as dt where a=f and a=g;
10669+
id select_type table type possible_keys key key_len ref rows filtered Extra
10670+
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 7 100.00 Using where
10671+
3 DERIVED t1 ALL NULL NULL NULL NULL 7 100.00 Using temporary; Using filesort
10672+
Warnings:
10673+
Note 1003 select `v2`.`a` AS `a`,`v2`.`f` AS `f`,`v2`.`g` AS `g` from `test`.`v2` where `v2`.`f` = `v2`.`a` and `v2`.`g` = `v2`.`a`
10674+
drop view v1,v2;
10675+
drop table t1;
1063710676
# End of 10.2 tests

mysql-test/t/derived_cond_pushdown.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,4 +2212,29 @@ select * from v1 where m > 0;
22122212
drop view v1;
22132213
drop table t1;
22142214

2215+
--echo #
2216+
--echo # MDEV-25635: pushdown into grouping view using aggregate functions
2217+
--echo # with constant arguments via a mergeable derived table
2218+
--echo #
2219+
2220+
create table t1 (a int);
2221+
insert into t1 values (3), (7), (1), (3), (7), (7), (3);
2222+
2223+
create view v1 as select a, sum(1) as f, sum(1) as g from t1 group by a;
2224+
select * from v1;
2225+
let $q1=
2226+
select * from (select * from v1) as dt where a=f and a=g;
2227+
eval $q1;
2228+
eval explain extended $q1;
2229+
2230+
create view v2 as select a, min(1) as f, min(1) as g from t1 group by a;
2231+
select * from v2;
2232+
let $q2=
2233+
select * from (select * from v2) as dt where a=f and a=g;
2234+
eval $q2;
2235+
eval explain extended $q2;
2236+
2237+
drop view v1,v2;
2238+
drop table t1;
2239+
22152240
--echo # End of 10.2 tests

sql/item.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4952,7 +4952,10 @@ class Item_direct_view_ref :public Item_direct_ref
49524952
table_map used_tables() const;
49534953
void update_used_tables();
49544954
table_map not_null_tables() const;
4955-
bool const_item() const { return used_tables() == 0; }
4955+
bool const_item() const
4956+
{
4957+
return (*ref)->const_item() && (null_ref_table == NO_NULL_TABLE);
4958+
}
49564959
TABLE *get_null_ref_table() const { return null_ref_table; }
49574960
bool walk(Item_processor processor, bool walk_subquery, void *arg)
49584961
{

0 commit comments

Comments
 (0)