Bug #8654 | corelated subquery refering to rand() in outer query returns wrong result (OR CR | ||
---|---|---|---|
Submitted: | 21 Feb 2005 16:49 | Modified: | 10 Jul 2005 15:57 |
Reporter: | Martin Friebe (Gold Quality Contributor) (OCA) | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server | Severity: | S3 (Non-critical) |
Version: | 4.1.10 | OS: | |
Assigned to: | Oleksandr Byelkin | CPU Architecture: | Any |
[21 Feb 2005 16:49]
Martin Friebe
[21 Feb 2005 17:32]
Martin Friebe
also look at these, on the same table as above, cut of, for simplification select a, rand(1), rand(1) x, rand(1) from r2 ; +------+-------------------+-------------------+-------------------+ | a | rand(1) | x | rand(1) | +------+-------------------+-------------------+-------------------+ | 1 | 0.40540353712198 | 0.40540353712198 | 0.40540353712198 | | 2 | 0.87161418038571 | 0.87161418038571 | 0.87161418038571 | | 3 | 0.14186032129625 | 0.14186032129625 | 0.14186032129625 | rand(1) stays the same for all of each row (correct) select a, rand(1), rand(1) x, (select rand(1) ), rand(1) from r2 ; +------+-------------------+-------------------+--------------------+-------------------+ | a | rand(1) | x | (select rand(1) ) | rand(1) | +------+-------------------+-------------------+--------------------+-------------------+ | 1 | 0.40540353712198 | 0.40540353712198 | 0.40540353712198 | 0.40540353712198 | | 2 | 0.87161418038571 | 0.87161418038571 | 0.87161418038571 | 0.87161418038571 | | 3 | 0.14186032129625 | 0.14186032129625 | 0.14186032129625 | 0.14186032129625 | | 4 | 0.094459096057768 | 0.094459096057768 | 0.094459096057768 | 0.094459096057768 | #again the same, again correct # now with reference to the alias, the value also changes in the outer query within one row #from row 2 onwards select a, rand(1), rand(1) x, (select x ), rand(1) from r2 ; +------+-------------------+-------------------+-------------------+-------------------+ | a | rand(1) | x | (select x ) | rand(1) | +------+-------------------+-------------------+-------------------+-------------------+ | 1 | 0.40540353712198 | 0.40540353712198 | 0.87161418038571 | 0.40540353712198 | | 2 | 0.87161418038571 | 0.14186032129625 | 0.094459096057768 | 0.87161418038571 | | 3 | 0.14186032129625 | 0.046714547133739 | 0.95019547822903 | 0.14186032129625 | | 4 | 0.094459096057768 | 0.6108337804776 | 0.20358249843454 | 0.094459096057768 |
[21 Feb 2005 17:42]
Martin Friebe
and this one causes a crash select a, rand(1) x, (select group_concat(x) from r2 ) from r2 ;
[21 Feb 2005 17:49]
Martin Friebe
After bug 8216, I believe this is the same as the bug, with random above select a y, (select max(b) from r2 where a< y ) x, (select group_concat(x) from r2 ) from r2 ; ERROR 2013 (HY000): Lost connection to MySQL server during query
[21 Feb 2005 17:57]
Martin Friebe
sorry the last comment, is a seperate issue, nothing to to with refering to rand or subquery. added the last one as bug #8656
[27 Jun 2005 8:18]
Oleksandr Byelkin
Thank you for bugreport! It is not a bug but unsupported behaviour. In http://dev.mysql.com/doc/mysql/en/mathematical-functions.html we have following: "You can't use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times." Subqueries evaluate reference on rand several times, too. Of course it have to be explained well in documentation. (and I checked it with group_concat - it do not carash server any more as far as bug#8656 is fixed)
[10 Jul 2005 15:57]
Jon Stephens
Should have been closed as Not A Bug.