[MDEV-9681] select with inner join against empty table brings unexpected values Created: 2016-03-03  Updated: 2023-11-28

Status: Confirmed
Project: MariaDB Server
Component/s: Optimizer, Storage Engine - InnoDB
Affects Version/s: 10.0.21, 5.5, 10.0, 10.1, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9
Fix Version/s: 10.4, 10.5, 10.6

Type: Bug Priority: Minor
Reporter: Luciano Barcaro Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: upstream
Environment:

Gentoo linux



 Description   

MariaDB brings values passed on where clause as result field values in queries multi table with INNER join against empty table (innodb engine).

also tested against aria and myisam (both works as expected)

At the moment, I can't check against newer MariaDB versions.

How to reproduce:

-- Create temporary tables
create table tmp1 (idTmp1 int unsigned not null primary key);
create table tmp2 (idTmp2 int unsigned not null primary key,
  idTmp1 int unsigned not null,
  vlr decimal(12,2),
  foreign key (idTmp1) references tmp1(idTmp1));
 
-- Insert some data
insert into tmp1 values (1);
 
-- without where works as expected
select tmp1.idTmp1, tmp2.idTmp2, sum(tmp2.vlr)
from tmp1
inner join tmp2 on tmp2.idTmp1=tmp1.idTmp1;
-- returns Null, Null, Null
 
-- with an equality condition in tmp1 brings unexpected data in first field
select tmp1.idTmp1, tmp2.idTmp2, sum(tmp2.vlr)
from tmp1
inner join tmp2 on tmp2.idTmp1=tmp1.idTmp1
where tmp1.idTmp1=1;
-- returns 1, Null, Null
 
-- with a range (simulating equality) in tmp1 brings unexpected data in first field
select tmp1.idTmp1, tmp2.idTmp2, sum(tmp2.vlr)
from tmp1
inner join tmp2 on tmp2.idTmp1=tmp1.idTmp1
where tmp1.idTmp1 between 1 and 1;
-- returns 1, Null, Null
 
-- with an range (different range) in tmp1 works as expected
select tmp1.idTmp1, tmp2.idTmp2, sum(tmp2.vlr)
from tmp1
inner join tmp2 on tmp2.idTmp1=tmp1.idTmp1
where tmp1.idTmp1 between 0 and 2;
-- returns Null, Null, Null



 Comments   
Comment by Elena Stepanova [ 2016-03-03 ]

Technically, the result is indeed incorrect, because the join itself produces an empty result.

However, please note that for all practical purposes you should avoid mixing aggregate and non-aggregate columns in queries without GROUP BY, because result in general case is non-deterministic. If JOIN produced more than one row, idTmp1 and idTmp2 would be officially allowed to have any value from the ones produced by join, so it would really be useless.
If JOIN produced exactly one row, then values should have been the ones from the row. But here we have a corner case – JOIN produces no rows at all, and NULL values for the non-aggregate columns are just placeholders since SUM by definition must produce NULL.

Also reproducible on MySQL 5.5, 5.6, 5.7 (for 5.7 sql_mode needs to be modified because by default it now requires the full group-by).

Comment by Luciano Barcaro [ 2016-03-03 ]

Hi Elena,
I just catched this bug because someone forgot the group by

Generated at Thu Feb 08 07:36:31 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.