Bug #6462 Same request on same data returns different results.
Submitted: 5 Nov 2004 13:32 Modified: 22 Nov 2004 13:32
Reporter: Viktor Kaydalov Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.1.7 OS:Windows (Win 2000)
Assigned to: Dmitry Lenev CPU Architecture:Any

[5 Nov 2004 13:32] Viktor Kaydalov
Description:
Same request on same data returns different results.

How to repeat:
CREATE TABLE `client` (                                                                                                                                                                                                                   
          `m_n` int(11) NOT NULL default '0',
          `m_type` int(11) default NULL,                                                                                                                                                                                                          
          `m_code_n` int(11) default NULL,                                                                                                                                                                                                        
          `m_short_name` varchar(80) default NULL,                                                                                                                                                                                                
          PRIMARY KEY  (`m_n`),
          UNIQUE KEY key1 (`m_type`,`m_code_n`)
        ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `account` (                                                                                                         
           `m_id` int(11) NOT NULL default '0',                                                                                                   
           `m_n` int(11) NOT NULL default '0',
           `m_acc_bal` int(11) default NULL,                                                                                               
           PRIMARY KEY  (`m_id`),
           KEY key1(`m_n`)
         ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `accrest` (                                                                                                                                           
           `m_id` int(11)  NOT NULL default '0',                                                                                                                                     
           `m_date` date NOT NULL default '0000-00-00',                                                                                                                                      
           `m_money` decimal(14,2) default NULL,                                                                                                                             
           PRIMARY KEY  (`m_id`,`m_date`)
         ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

insert into client (m_n,m_type,m_code_n,m_short_name) values (1,3,1000,'acme1'),(2,3,1001,'acme2'),(3,5,1002,'acme3');
insert into account (m_id,m_n,m_acc_bal) values (1,1,3300),(2,1,3000),(3,2,3000),(4,3,3000),(5,3,3200);
insert into accrest (m_id,m_date,m_money) values (1,'2004-10-29',10),(2,'2004-10-29',20),(3,'2004-10-29',30),(4,'2004-10-29',40),(5,'2004-10-29',50);
         
set @WORK_DATE:='2004-10-29';

set @TOTAL:=
(
select sum(M_money) as SUM_MONEY_ALL
from account
join accrest on account.M_id=accrest.M_id and accrest.M_date=@WORK_DATE
join client on client.M_N=account.M_N
)
;

select M_type,M_Code_N,M_short_name,sum(M_money) as SUM_MONEY
from account
join accrest on account.M_id=accrest.M_id and accrest.M_date=@WORK_DATE
join client on client.M_N=account.M_N
group by M_type,M_Code_N
;

I expect to see next (correct) result

M_type  M_Code_N  M_short_name    SUM_MONEY
3          1000          acme1              30.00
3          1001          acme2              30.00
5          1002          acme3              90.00  

,but after execution of 2 last statements I obtain result like this:
M_type  M_Code_N  M_short_name    SUM_MONEY
5          1002          acme3              150.00                        

After the second and further executions of LAST STATEMENT ONLY result is correct.

Additional info: replacement of "M_date=@WORK_DATE" to "M_date='2004-10-29'" in "set @TOTAL:=..." statement removes this bug.
[8 Nov 2004 16:52] MySQL Verification Team
I was able to repeat on Windows with 4.1.7 and on Linux with 4.1.8
crashes the server:

ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...

I will test again on Linux/Windows.

Thank you for the bug report.
[22 Nov 2004 13:32] Dmitry Lenev
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

ChangeSet 1.2103.4.1 2004/11/22 13:05:10 dlenev@brandersnatch.localdomain
  Fix for bug #6462 "Same request on same data returns different
  results." a.k.a. "Proper cleanup of subqueries is missing for SET and DO
  statements". (Version #2 with after-review fixes).

Fix will appear in version 4.1.8