-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query: subquery with order by but without Skip/Take gets the orderby removed #16086
Comments
Isn't the point that rows coming out of the subquery aren't guaranteed to be sorted, even when the subquery contains OrderBy? Shouldn't we lift the OrderBy outside to the external query instead? SELECT [t].[Name], [g].[FullName]
FROM [Gears] AS [g]
INNER JOIN (
SELECT [ww].*
FROM [Weapons] AS [ww]
) AS [t] ON [g].[FullName] = [t].[OwnerFullName]
WHERE [g].[Discriminator] IN (N'Officer', N'Gear')
ORDER BY [t].[Name] |
Is there any point of lifting or applying Order by in such cases?
|
I'm probably missing something, but can't there be more than one weapon for a given gear in the above example? Or does we call that situation a collection join? However, I do agree that order of rows coming out of the subquery isn't guaranteed, so order+skip inside the subquery may be (or are) meaningless.
On second look you're right - it makes no sense to alter the order of gears because OrderBy was specified on joined weapons. However, unless I'm mistaken the expected output here is to have gears come out in random/natural order, but for each gear's weapons to be sorted (i.e. by name). So for a projection of If so, this could be accomplished by lifting the OrderBy as an additional ordering on the outer: SELECT [t].[Name], [g].[FullName]
FROM [Gears] AS [g]
INNER JOIN (
SELECT [ww].*
FROM [Weapons] AS [ww]
) AS [t] ON [g].[FullName] = [t].[OwnerFullName]
WHERE [g].[Discriminator] IN (N'Officer', N'Gear')
ORDER BY [g].Id, [t].[Name]
FWIW same in PostgreSQL ( |
Yes, we call them collection join. Since collection join has different processing (traversing multiple rows to generate result). |
Thanks for the explanations @smitpatel. |
There are test assertions disabled with this bug number. Assigning to @maumar to clean that up and close this issue. |
…t without Skip/Take gets the orderby removed Sql we generate is not perfect (ordering on the joined set is not preserved) but there is not much we can do in the general case. We could improve this for some cases (outer set it's unambiguously sorted or is entity which we can sort ourselves). Probably not worth given the marginal benefit.
…t without Skip/Take gets the orderby removed Sql we generate is not perfect (ordering on the joined set is not preserved) but there is not much we can do in the general case. We could improve this for some cases (outer set it's unambiguously sorted or is entity which we can sort ourselves). Probably not worth given the marginal benefit.
resolved by e2ff7d8 |
we should consider adding Skip(0) instead
query:
we could do:
The text was updated successfully, but these errors were encountered: