[MDEV-18431] Select max + row_number giving incorrect result Created: 2019-01-31 Updated: 2019-03-12 Resolved: 2019-03-12 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer - Window functions |
| Affects Version/s: | 10.3.11, 10.3.12, 10.2, 10.3, 10.4 |
| Fix Version/s: | 10.2.23 |
| Type: | Bug | Priority: | Major |
| Reporter: | Jarno Antikainen | Assignee: | Varun Gupta (Inactive) |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Server version: 10.3.12-MariaDB-1:10.3.12+maria~bionic mariadb.org binary distribution |
||
| Description |
|
I have a table like this:
With data:
It is a simplified version of a table used for storing events of entities, identified by an entity id and an increasing entity version number, used for event sourcing. Some background for the use case: I wanted to insert some new events for the entities using insert...select, with possibly more than one event to add for each entity. So, in the select part I need to get the next available entity version number for each new event to insert. I figured I would get max(entity_version) and row_number() and add them together to get the entity_version to insert for each event. I found an issue in the select part of the insert...select, and the SQL statements below are simplified to concentrate on issue. Now to the actual issue: If I select the max entity_version and row_number in separate columns, the results are what I expected:
But for insert...select I need to add them together, and then I get something I didn't expect:
I tried the same with MySQL 8.0.14, and that gives the expected results:
I might miss something as this is the first time I need to use row_number, and I think I can work around this with a temporary table, but at this point I suspect this might be a bug. I have only tried the versions mentioned above. |
| Comments |
| Comment by Alice Sherepa [ 2019-01-31 ] | ||||||||||||||||||||||||||||||||||||||
|
Thanks a lot for the report and the test case! Reproducible on 10.2-10.4, with MyIsam and Innodb
expected result:
| ||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2019-03-09 ] | ||||||||||||||||||||||||||||||||||||||
|
After discussion with psergey, there are 2 approaches to solve this issue: Approach 1:
Approach 2:
| ||||||||||||||||||||||||||||||||||||||
| Comment by Oleksandr Byelkin [ 2019-03-12 ] | ||||||||||||||||||||||||||||||||||||||
|
OK, to push |