Skip to content
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: we don't convert JOIN to APPLY for query with subquery inside argument to Take method #32303

Open
maumar opened this issue Nov 15, 2023 · 2 comments

Comments

@maumar
Copy link
Contributor

maumar commented Nov 15, 2023

query:

ss.Set<City>().OrderBy(x => x.Name).Select(x => x.BornGears.OrderBy(g => g.FullName).Take(x.StationedGears.Any() ? 1 : 2))

sql:

SELECT [c].[Name], [t0].[Nickname], [t0].[SquadId], [t0].[AssignedCityName], [t0].[CityOfBirthName], [t0].[Discriminator], [t0].[FullName], [t0].[HasSoulPatch], [t0].[LeaderNickname], [t0].[LeaderSquadId], [t0].[Rank]
FROM [Cities] AS [c]
LEFT JOIN (
    SELECT [t].[Nickname], [t].[SquadId], [t].[AssignedCityName], [t].[CityOfBirthName], [t].[Discriminator], [t].[FullName], [t].[HasSoulPatch], [t].[LeaderNickname], [t].[LeaderSquadId], [t].[Rank]
    FROM (
        SELECT [g].[Nickname], [g].[SquadId], [g].[AssignedCityName], [g].[CityOfBirthName], [g].[Discriminator], [g].[FullName], [g].[HasSoulPatch], [g].[LeaderNickname], [g].[LeaderSquadId], [g].[Rank], ROW_NUMBER() OVER(PARTITION BY [g].[CityOfBirthName] ORDER BY [g].[FullName]) AS [row]
        FROM [Gears] AS [g]
    ) AS [t]
    WHERE [t].[row] <= CASE
        WHEN EXISTS (
            SELECT 1
            FROM [Gears] AS [g0]
            WHERE [c].[Name] = [g0].[AssignedCityName]) THEN 1
        ELSE 2
    END
) AS [t0] ON [c].[Name] = [t0].[CityOfBirthName]
ORDER BY [c].[Name], [t0].[CityOfBirthName], [t0].[FullName], [t0].[Nickname]

error:

SqlException : The multi-part identifier "c.Name" could not be bound.

note that converting to outer apply works just fine

SELECT [c].[Name], [t0].[Nickname], [t0].[SquadId], [t0].[AssignedCityName], [t0].[CityOfBirthName], [t0].[Discriminator], [t0].[FullName], [t0].[HasSoulPatch], [t0].[LeaderNickname], [t0].[LeaderSquadId], [t0].[Rank]
FROM [Cities] AS [c]
OUTER APPLY (
    SELECT [t].[Nickname], [t].[SquadId], [t].[AssignedCityName], [t].[CityOfBirthName], [t].[Discriminator], [t].[FullName], [t].[HasSoulPatch], [t].[LeaderNickname], [t].[LeaderSquadId], [t].[Rank]
    FROM (
        SELECT [g].[Nickname], [g].[SquadId], [g].[AssignedCityName], [g].[CityOfBirthName], [g].[Discriminator], [g].[FullName], [g].[HasSoulPatch], [g].[LeaderNickname], [g].[LeaderSquadId], [g].[Rank], ROW_NUMBER() OVER(PARTITION BY [g].[CityOfBirthName] ORDER BY [g].[FullName]) AS [row]
        FROM [Gears] AS [g]
    ) AS [t]
    WHERE [c].[Name] = [t].[CityOfBirthName] AND [t].[row] <= CASE
        WHEN EXISTS (
            SELECT 1
            FROM [Gears] AS [g0]
            WHERE [c].[Name] = [g0].[AssignedCityName]) THEN 1
        ELSE 2
    END
) AS [t0]
ORDER BY [c].[Name], [t0].[CityOfBirthName], [t0].[FullName], [t0].[Nickname]
@roji
Copy link
Member

roji commented Nov 15, 2023

@maumar that looks similar to #32217, i.e. could this be a result of nav expansion on visiting the argument of Take()? If so, this would be a dup of that.

@maumar
Copy link
Contributor Author

maumar commented Nov 15, 2023

this is what happens after 32217 is fixed - there is another bug later in the pipeline - currently it produces untranslatable tree

@ajcvickers ajcvickers added this to the 9.0.0 milestone Nov 17, 2023
@maumar maumar modified the milestones: 9.0.0, Backlog Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants