[MDEV-8013] order operator not working with 'UNION' expression Created: 2015-04-18 Updated: 2015-04-20 Resolved: 2015-04-18 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Parser |
| Affects Version/s: | 10.0.17 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Major |
| Reporter: | Mikhail Gavrilov | Assignee: | Unassigned |
| Resolution: | Not a Bug | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Linux |
||
| Description |
|
Expecting:
But server return:
|
| Comments |
| Comment by Mikhail Gavrilov [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
|
Also order broken in sub-query
| ||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
|
See Documentation:
| ||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
|
> UNION by default produces an unordered set of rows
First statement retrieve always parent record if exists, second statement retrieve ordered child's records. For my case workaround would be adding LIMIT 1000 after "ORDER BY `ord`" But I am afraid in another case I can't do it. | ||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
It won't be a workaround even for your case. See again the quote above:
That is, there is no guarantee that those rows will end up ordered. It can happen by pure luck in certain versions, but you cannot rely on it. | ||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
Thanks for explanation, it's too bad for me | ||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
|
Can we discuss changing this behavior? | ||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
|
Normally I would suggest converting the bug report to a task (feature request), but in this case I'm not even sure it is possible, it is probably the SQL standard that defines this behavior. Maybe you would want to discuss it on the mailing list first? maria-discuss@lists.launchpad.net | ||||||||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
|
A SQL-standard compliant solution is to use the ORDER BY on the top level. You can do it like this:
| ||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-04-18 ] | ||||||||||||||||||||||||||||||||
|
Sergei, thank you for joining the discussion of the problem.
I just do not understand from whom this protection. As I said above, developers which needed in merging large dataset without ordering simple will not use ORDER statement in subqueries. | ||||||||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2015-04-19 ] | ||||||||||||||||||||||||||||||||
|
Exactly. With different sort directions you can do something like
It is not a "protection". It is simply how SQL standard defines the syntax.
As you can see, according to the SQL standard, ORDER BY applies to the whole <query expression>. And on both sides of UNION one can only put <query term>s. SELECT clause starts a <query specification> which is a <simple table>. And tables do not have any specific ordering of rows, so UNION works with two unordered sets of rows. That's the standard. | ||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-04-19 ] | ||||||||||||||||||||||||||||||||
|
Nice try, but even not work with my data set:
I also respect the standards, but seems this issue cannot be solved without temporary tables and stored procedures. What about sub-queries?
I have always believed that all sub-queries are treated as separate requests. | ||||||||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2015-04-20 ] | ||||||||||||||||||||||||||||||||
|
why do you get "sort" as 0 in the second part of the result set? may be 'sort' is unsigned? try casting it to signed (in the first select, of course). You don't understand. see how the standard defines it (above). ORDER BY applies to the result set. It cannot be applied to a SELECT inside a UNION, it cannot be applied to a subquery. According to the standard iy is a syntax error to use ORDER BY the way you did. | ||||||||||||||||||||||||||||||||
| Comment by Mikhail Gavrilov [ 2015-04-20 ] | ||||||||||||||||||||||||||||||||
I specifically show that this variant is completely dependent on the type of sorting field. And will be not worked with VARCHAR data.
Why can not consider that the result set and also have a sub-query? All sub-query having result set and we can sort, group and make calculations in this result sets. I retest my test case in Postgre SQL 9.4 and construction
work as expected.
Can you accept this improvement for MariaDB? When added "(" ")" to query, this query must be treat as subquery. Of course, I am agree that this statement
erroneously. |