Skip to content

Commit d57c44f

Browse files
committed
MDEV-31277 Wrong result on 2-nd execution of PS to select from view using derived
As a result of this bug the second execution of the prepared statement created for select from materialized view could return a wrong result set if - the specification of the view used a left join - an inner table the left join was a mergeable derived table - the derived table contained a constant column. The problem appeared because the flag 'maybe-null' of the wrapper Item_direct_view_ref constructed for the constant field of the mergeable derived table was not set to 'true' on the second execution of the prepared statement. The patch always sets this flag properly when calling the function Item_direct_view_ref::set_null_ref-table(). The latter is invoked in Item_direct_view_ref constructor if it is created for some reference of a constant column belonging to a mergeable derived table. Approved by Oleksandr Byelkin <sanja@mariadb.com>
1 parent 0f0da95 commit d57c44f

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

mysql-test/main/derived_view.result

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,3 +4250,35 @@ dim1 dim2 dim3 p SUM(p)
42504250
100 10 1 2 371
42514251
DROP VIEW v;
42524252
# End of 10.4 tests
4253+
#
4254+
# MDEV-31277: 2-nd execution of PS to select from materialized view
4255+
# specified as left join whose inner table is mergeable
4256+
# derived containing a constant column
4257+
#
4258+
create table t1 (
4259+
Election int(10) unsigned NOT NULL
4260+
) engine=MyISAM;
4261+
insert into t1 (Election) values (1), (4);
4262+
create table t2 (
4263+
VoteID int(10),
4264+
ElectionID int(10),
4265+
UserID int(10)
4266+
);
4267+
insert into t2 (ElectionID, UserID) values (2, 30), (3, 30);
4268+
create view v1 as select * from t1
4269+
left join ( select 'Y' AS Voted, ElectionID from t2 ) AS T
4270+
on T.ElectionID = t1.Election
4271+
limit 9;
4272+
prepare stmt1 from "select * from v1";
4273+
execute stmt1;
4274+
Election Voted ElectionID
4275+
1 NULL NULL
4276+
4 NULL NULL
4277+
execute stmt1;
4278+
Election Voted ElectionID
4279+
1 NULL NULL
4280+
4 NULL NULL
4281+
deallocate prepare stmt1;
4282+
drop view v1;
4283+
drop table t1, t2;
4284+
# End of 10.5 tests

mysql-test/main/derived_view.test

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,3 +2832,39 @@ SELECT d.*, SUM(p) FROM demo d;
28322832
DROP VIEW v;
28332833

28342834
--echo # End of 10.4 tests
2835+
2836+
--echo #
2837+
--echo # MDEV-31277: 2-nd execution of PS to select from materialized view
2838+
--echo # specified as left join whose inner table is mergeable
2839+
--echo # derived containing a constant column
2840+
--echo #
2841+
2842+
create table t1 (
2843+
Election int(10) unsigned NOT NULL
2844+
) engine=MyISAM;
2845+
2846+
insert into t1 (Election) values (1), (4);
2847+
2848+
create table t2 (
2849+
VoteID int(10),
2850+
ElectionID int(10),
2851+
UserID int(10)
2852+
);
2853+
2854+
insert into t2 (ElectionID, UserID) values (2, 30), (3, 30);
2855+
create view v1 as select * from t1
2856+
left join ( select 'Y' AS Voted, ElectionID from t2 ) AS T
2857+
on T.ElectionID = t1.Election
2858+
limit 9;
2859+
2860+
prepare stmt1 from "select * from v1";
2861+
2862+
execute stmt1;
2863+
execute stmt1;
2864+
2865+
deallocate prepare stmt1;
2866+
2867+
drop view v1;
2868+
drop table t1, t2;
2869+
2870+
--echo # End of 10.5 tests

mysql-test/main/join_outer.test

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,11 +2372,8 @@ create view v1 as select * from t1
23722372
left join ( select 'Y' AS Voted, ElectionID from t2 ) AS T
23732373
on T.ElectionID = t1.Election
23742374
limit 9;
2375-
#enable after fix MDEV-31277
2376-
--disable_ps2_protocol
23772375
# limit X causes merge algorithm select as opposed to temp table
23782376
select * from v1;
2379-
--enable_ps2_protocol
23802377
drop table t1, t2;
23812378
drop view v1;
23822379

@@ -2391,10 +2388,7 @@ create view v10 as select *, 'U' as u from t10 left join (select 'Y' as y, t20.b
23912388
create table t30 (c int);
23922389
insert into t30 values (1),(3);
23932390
create view v20 as select * from t30 left join (select 'X' as x, v10.u, v10.y, v10.b from v10) dt2 on t30.c=dt2.b limit 6;
2394-
#check after fix MDEV-31277
2395-
--disable_ps2_protocol
23962391
select * from v20 limit 9;
2397-
--enable_ps2_protocol
23982392
drop view v10, v20;
23992393
drop table t10, t20, t30;
24002394

@@ -2408,8 +2402,6 @@ insert into t3 values (3),(1);
24082402
create table t1 (a int);
24092403
insert into t1 values (1),(2),(7),(1);
24102404

2411-
#check after fix MDEV-31277
2412-
--disable_ps2_protocol
24132405
select * from
24142406
(
24152407
select * from
@@ -2422,7 +2414,6 @@ select * from
24222414
on dt1.a=dt2.b
24232415
limit 9
24242416
) dt;
2425-
--enable_ps2_protocol
24262417

24272418
## Same as dt3 above
24282419
create view v3(x,c) as select * from (select 'X' as x, t3.c from t3) dt3;
@@ -2436,10 +2427,7 @@ create view v0(y,b,x,c) as select * from v2 left join v3 on v2.b=v3.c;
24362427
# Same as above select statement
24372428
create view v1 as select 'Z' as z, t1.a, v0.* from t1 left join v0 on t1.a=v0.b limit 9;
24382429

2439-
#check after fix MDEV-31277
2440-
--disable_ps2_protocol
24412430
select * from v1;
2442-
--enable_ps2_protocol
24432431

24442432
set statement join_cache_level=0 for
24452433
select * from v1;

sql/item.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5876,6 +5876,8 @@ class Item_direct_view_ref :public Item_direct_ref
58765876
if (!view->is_inner_table_of_outer_join() ||
58775877
!(null_ref_table= view->get_real_join_table()))
58785878
null_ref_table= NO_NULL_TABLE;
5879+
if (null_ref_table && null_ref_table != NO_NULL_TABLE)
5880+
maybe_null= true;
58795881
}
58805882

58815883
bool check_null_ref()

0 commit comments

Comments
 (0)