[MDEV-5414] RAND() in a subselect : different behavior in MariaDB and MySQL Created: 2013-12-08 Updated: 2020-07-01 Resolved: 2013-12-19 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | 5.3.12, 5.5.34, 10.0.6 |
| Fix Version/s: | 5.5.35, 10.0.8, 5.3.13 |
| Type: | Bug | Priority: | Major |
| Reporter: | Francescu GAROBY | Assignee: | Oleksandr Byelkin |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | upstream | ||
| Environment: |
ArchLinux X86_64 |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
I have 2 tables : galeries (id, name) and pictures (id, name, galery_id which is a FK to galeries).
In MySQL, this request works godd : I have a picture randomly selected, and I have all information (like the name). But, in MariaDB, no. RAND() and g.p_random are not equals ! If I modify my request like this :
Is it a bug or is it a normal behavior ? |
| Comments |
| Comment by Elena Stepanova [ 2013-12-08 ] |
|
To answer the question whether it's a bug or not, we'll need an example of actual result you are getting, I'm not sure I understand the problem from the verbal description. If it turns out to be a bug, we will also need structures of the involved tables (SHOW CREATE TABLE <table name>) and, if possible, the data dump would help as well. |
| Comment by Francescu GAROBY [ 2013-12-08 ] |
|
this is what I have when I execute this request on MySQL : |
| Comment by Francescu GAROBY [ 2013-12-08 ] |
|
This is what I have when I execute this request on MariaDB : |
| Comment by Francescu GAROBY [ 2013-12-08 ] |
|
Yes, of course. CREATE TABLE IF NOT EXISTS `galleries` ( The "pictures" table structure is : ALTER TABLE `pictures` Some data : INSERT INTO `galleries` (`id`, `name`, `year`) VALUES INSERT INTO `pictures` (`id`, `name`, `width`, `height`, `year`, `technique`, `comment`, `gallery_id`, `type`) VALUES See attached files (wrong and good results), to see what I have when I execute the request (the one with the 3 fields r1, r2 and r3) |
| Comment by Elena Stepanova [ 2013-12-09 ] |
|
Hi, Thank you for the information. Yes, it's a bug. optimizer_switch = 'derived_merge=off'' in your cnf file and/or client session. |
| Comment by Elena Stepanova [ 2013-12-09 ] |
|
I will put it all together again, just for convenience, no changes in the data or query comparing to the initial one apart from minor fixes in column names (nas => name). Test case: CREATE TABLE IF NOT EXISTS `galleries` ( CREATE TABLE IF NOT EXISTS `pictures` ( ALTER TABLE `pictures` INSERT INTO `galleries` (`id`, `name`, `year`) VALUES INSERT INTO `pictures` (`id`, `name`, `width`, `height`, `year`, `technique`, `comment`, `gallery_id`, `type`) VALUES SELECT g.id AS gallery_id,
Expected result is 10 rows, ordered by the 2nd column (gallery name), no NULLs in 3rd or 4th columns since there is at least one picture for each gallery. Additionally, there are 3 columns r1, r2, r3 which select the exact same column and are supposed to be equal to each other and equal to the 3rd column. Actual result on MySQL or on MariaDB without derived_merge satisfies the condition above, e.g. gallery_id gallery_name picture_id picture_name r1 r2 r3 (actual values in columns 3-7 might differ). Result with derived_merge has several problems:
Example: gallery_id gallery_name picture_id picture_name r1 r2 r3 I wasn't able to reproduce it with a MERGE view instead of the FROM subquery, although maybe I was doing it in a wrong way. |
| Comment by Oleksandr Byelkin [ 2013-12-11 ] |
|
It is problem of derived tables merging (which Mysql does not have so no problem there). Workaround is |
| Comment by Oleksandr Byelkin [ 2013-12-12 ] |
|
This is a bug if different r1,r2,r3 is a bug in following example: create table t1 (a int); create view v1 (a,r) as select a,rand() from t1; select a, r as r1, r as r2, r as r3 from v1; drop view v1; |
| Comment by Oleksandr Byelkin [ 2013-12-12 ] |
|
Actually, materialization could be forced is a VIEW or a DERIVED TABLE uses rand() and other side effect function, but I can't recall why it was not done for views... |
| Comment by Sergei Golubchik [ 2013-12-12 ] |
|
MySQL 5.6 does not have this bug with derived tables, because it doesn't implement the optimization of merging derived tables. |
| Comment by Oleksandr Byelkin [ 2013-12-17 ] |
|
Fix of the bug sent for review. |
| Comment by Oleksandr Byelkin [ 2013-12-18 ] |
|
pushed to 5.3 |