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

Generated JOIN for multiple MIN is wrong #27083

Closed
fschick opened this issue Dec 31, 2021 · 0 comments
Closed

Generated JOIN for multiple MIN is wrong #27083

fschick opened this issue Dec 31, 2021 · 0 comments
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@fschick
Copy link

fschick commented Dec 31, 2021

Generated JOIN for multiple MIN is wrong

When multiple MIN are used, the translated ON clause of subsequent JOINs is wrong

The following query:

var timSheets = await dbContext
    .Set<TimeSheet>()
    .Where(x => x.OrderId != null)
    .GroupBy(x => x.OrderId)
    .Select(x => new
    {
        HourlyRate = x.Min(f => f.Order.HourlyRate),
        CustomerId = x.Min(f => f.Project.Customer.Id),
        CustomerName = x.Min(f => f.Project.Customer.Name),
    })
    .ToListAsync();

Is translated to:

SELECT MIN("o"."HourlyRate") AS "HourlyRate",
	MIN("c"."Id") AS "CustomerId",
	MIN("c0"."Name") AS "CustomerName"
FROM "TimeSheet" AS "t"
LEFT JOIN "Order" AS "o" ON "t"."OrderId" = "o"."Id"
INNER JOIN "Project" AS "p" ON "t"."ProjectId" = "p"."Id"
INNER JOIN "Customer" AS "c" ON "p"."CustomerId" = "c"."Id"
INNER JOIN "Project" AS "p0" ON "t"."ProjectId" = "p"."Id" -- <== THIS SHOULD BE ... = "p0"."Id"
INNER JOIN "Customer" AS "c0" ON "p0"."CustomerId" = "c"."Id" -- <== THIS SHOULD BE ... = "c0"."Id"
WHERE "t"."OrderId" IS NOT NULL
GROUP BY "t"."OrderId"

The generated ON clause for "p0" and "c0" produces a cartesian product.

See WhenGroupWithMinIsUsed_CustomerIdAndTitleMatches to reproduce

Code to reproduce

git clone https://github.com/fschick/EF.GeneratedSelectBug.git
cd EF.GeneratedSelectBug
dotnet test

Provider and version information

EF Core version: 6.0.1
Database provider: at least Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 6.0
Operating system: All
IDE: All

@ajcvickers ajcvickers added this to the 6.0.x milestone Jan 4, 2022
smitpatel added a commit that referenced this issue Jan 5, 2022
…up by aggregate subquery

When replacing columns, we used the outer select expression which had additional joins from previous term whose aliases match tables in current join and it got updated with wrong table.
The fix is to utilize the original tables when replacing columns. These column replacement is to map the columns from initial tables of group by from subquery to outer group by query.

Resolves #27083
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 5, 2022
smitpatel added a commit that referenced this issue Jan 5, 2022
…up by aggregate subquery

When replacing columns, we used the outer select expression which had additional joins from previous term whose aliases match tables in current join and it got updated with wrong table.
The fix is to utilize the original tables when replacing columns. These column replacement is to map the columns from initial tables of group by from subquery to outer group by query.

Resolves #27083
@ajcvickers ajcvickers modified the milestones: 6.0.x, 6.0.2 Jan 10, 2022
smitpatel added a commit that referenced this issue Jan 10, 2022
…up by aggregate subquery

When replacing columns, we used the outer select expression which had additional joins from previous term whose aliases match tables in current join and it got updated with wrong table.
The fix is to utilize the original tables when replacing columns. These column replacement is to map the columns from initial tables of group by from subquery to outer group by query.

Resolves #27083
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants