Bug #26559 Selects with common part affect each other's results
Submitted: 22 Feb 2007 10:24 Modified: 22 Feb 2007 11:14
Reporter: Rob Desbois Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.0.22 OS:Windows (Win2k)
Assigned to: CPU Architecture:Any

[22 Feb 2007 10:24] Rob Desbois
Description:
I have two select statements which have a common part in a join:
   (Paccess.network_id = Phome.network_id OR Paccess.network_id IS NULL)

I get inconsistent results wherever I run this. In the database where this was found, running the second prevents the first from returning the expected results. The only way to get it working is to comment out the common part mentioned, run it, uncomment and rerun. This happens every time.

I copied the table structure and data to another database running on the same server. This time, running the second query will break the first but only once -- running it again returns the expected results.
I have duplicated this setup (same version, same my.ini, same structure & data) on a completely separate machine but cannot replicate.

I know I am several versions behind but given that I cannot even replicate it even on the same server but different DB, I have reported it in case its non-appearance in 5.0.27 is not because it's been fixed.
I apologise if this *has* already been fixed but if somebody could point me towards the relevant bug report (I couldn't find one) I'd be grateful. 

How to repeat:
CREATE TABLE `user` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `name` varchar(32) collate latin1_general_cs NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs;

INSERT INTO user VALUES (1, 'charles'), (2, 'bob');

CREATE TABLE `privilege` (
  `user_id` mediumint(8) unsigned NOT NULL,
  `network_id` mediumint(8) unsigned default NULL,
  `type` varchar(32) collate latin1_general_cs NOT NULL,
  UNIQUE KEY `user_id` (`user_id`,`network_id`,`type`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs;

INSERT INTO privilege VALUES (1, 1, 'user-manage'), (2, 1, 'network-member');

Run this query to see the expected results:
   SELECT *
   FROM privilege Paccess
   JOIN privilege Phome
      ON Phome.user_id = '2'
      AND Phome.type = 'network-member'
      AND (Paccess.network_id = Phome.network_id OR Paccess.network_id IS NULL)
   WHERE Paccess.user_id = '1'
      AND Paccess.type = 'user-manage';

Run this query:
   SELECT U.name
   FROM user U
   -- find each user's home network
   JOIN privilege Phome
      ON Phome.user_id = U.id
      AND Phome.type = 'network-member'
   -- current user's user-management access rights
   LEFT JOIN privilege Paccess
      ON Paccess.user_id = '2'
      AND (Paccess.network_id = Phome.network_id OR Paccess.network_id IS NULL)
      AND Paccess.type = 'user-manage';

If you now rerun the first query, you'll see that it doesn't return any results this time. Run it again and it works as expected.

If you change the common part of the two queries from:
      AND (Paccess.network_id = Phome.network_id OR Paccess.network_id IS NULL)
to:
      AND (Paccess.network_id IS NULL OR Paccess.network_id = Phome.network_id)
you can see that the second query no longer breaks the first.
[22 Feb 2007 10:32] Sveta Smirnova
Thank you for the report.

i can not repeat it using last development sources. Please try with current 5.0.27 version and say result.
[22 Feb 2007 11:06] Rob Desbois
Sveta -- I am unable to reproduce under 5.0.27

Regardless of that I reported it as I am not always able to reproduce on 5.0.22 (so it could still be present in 5.0.27) and unable to find a bug report that matches its description.
[22 Feb 2007 11:14] Sveta Smirnova
Rob,

since 5.0.22 were fixed many bugs, seems your case fixed too. So I close this one as "Can't repeat". If you can repeat issue with newer version feel free to reopen the bug report.