[MDEV-4925] Wrong result - count(distinct), Using index for group-by (scanning) Created: 2013-08-19 Updated: 2014-05-12 Resolved: 2014-05-12 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | 10.0.4, 5.5.32 |
| Fix Version/s: | 5.5.38, 10.0.12 |
| Type: | Bug | Priority: | Minor |
| Reporter: | Patryk Pomykalski | Assignee: | Sergei Golubchik |
| Resolution: | Fixed | Votes: | 1 |
| Labels: | upstream | ||
| Description |
|
Taken from: http://bugs.mysql.com/bug.php?id=70038 test case:
Select returns 5, should be 10. Myisam works correctly. |
| Comments |
| Comment by Timour Katchaounov (Inactive) [ 2013-09-09 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
The only difference between the two test cases below is the presence of a PK. The test case with the PK produces wrong result, the other one works correctly: – Wrong result EXPLAIN EXTENDED
-----
----- SELECT COUNT(DISTINCT b) FROM t1 WHERE a = '777';
-------------------
------------------- – Correct result EXPLAIN EXTENDED
-----
----- SELECT COUNT(DISTINCT b) FROM t2 WHERE a = '777';
-------------------
------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Timour Katchaounov (Inactive) [ 2013-09-09 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Notes during analysis (the actual problem is not understood yet). The query engine requests the next record by calling QUICK_GROUP_MIN_MAX_SELECT::get_next. The second call to get_next, calls next_prefix again, which calls index_next_different. Since this a For whatever reason, this call produces HA_ERR_END_OF_FILE instead of the next key. So the main questions are:
Adding more records to table t1 shows that the current position in the index seems to "jump" over the next key to the following key.
| ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Timour Katchaounov (Inactive) [ 2013-09-09 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Specifically re the state of InnoDB, during the second call to next_prefix, InnoDB executes /-------------------------------------------------------------/ rec = btr_pcur_get_rec(pcur); 'pcur->old_rec' = "102777" This is already the last key, consequently, InnoDB returns EOF. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Timour Katchaounov (Inactive) [ 2013-09-09 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Specifically re the state of InnoDB, during the second call to next_prefix, InnoDB executes /-------------------------------------------------------------/ rec = btr_pcur_get_rec(pcur); 'pcur->old_rec' = "102777" This is already the last key, consequently, InnoDB returns EOF. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Timour Katchaounov (Inactive) [ 2013-09-17 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Explanation by Serg that the reason for the wrong result is because the call pattern to the handler interface is not supported by InnoDB. <serg> I'm not sure about the contract. if you do a search with HA_READ_KEY_EXACT and then you do index_next, index_next, etc. Whether the engine can only return exact matches or it should go till the end of the index Indeed, if one disables EXTENDED keys by commenting out in ha_innodb.cc this line: | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Timour Katchaounov (Inactive) [ 2013-09-17 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Discussion of a possible solution with Serg: <timour> serg, I used HA_READ_KEY_EXACT in order to jump to the first sub-group within a group. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Guangpu Feng [ 2013-09-18 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Glad to see the progress! | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Timour Katchaounov (Inactive) [ 2013-10-09 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Suggestion - add a storage engine property that tells the server if the engine supports this handler call sequence. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Guangpu Feng [ 2013-11-19 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
any progress? | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Patryk Pomykalski [ 2014-05-12 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
It was fixed in mysql 5.5.35 and the fix was merged. No test in mysql though. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2014-05-12 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks! I've added the test case, and will now close this bug report. |