Skip to content

Generated JOIN for multiple MIN is wrong #27083

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

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 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
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants