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

Simplification of Count(*) with multiple group by can generate wrong result #27130

Open
Tracked by #30173
smitpatel opened this issue Jan 6, 2022 · 1 comment
Open
Tracked by #30173
Labels
area-groupby area-query customer-reported priority-bug Issues which requires API breaks and have bigger impact hence should be fixed earlier in the release punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-bug
Milestone

Comments

@smitpatel
Copy link
Member

ss.Set<Order>().GroupBy(e => e.CustomerID)
                        .Select(g => new
                        {
                            g.Key,
                            A = ss.Set<Employee>().Where(e => e.City == "Seattle").GroupBy(e => e.City)
                                    .Select(g2 => g2.Count() + g.Min(e => e.OrderID))
                                    .OrderBy(e => 1)
                                    .FirstOrDefault()
                        })

Generates

SELECT [o].[CustomerID] AS [Key], COALESCE((
    SELECT TOP(1) COUNT(*) + MIN([o].[OrderID])
    FROM [Employees] AS [e]
    WHERE [e].[City] = N'Seattle'
    GROUP BY [e].[City]
    ORDER BY (SELECT 1)), 0) AS [A]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]

Which works correctly.
But if the g.Min(e => e.OrderID) is converted to g.Count() then generate the SQL like this

SELECT [o].[CustomerID] AS [Key], COALESCE((
    SELECT TOP(1) COUNT(*) + COUNT(*)
    FROM [Employees] AS [e]
    WHERE [e].[City] = N'Seattle'
    GROUP BY [e].[City]
    ORDER BY (SELECT 1)), 0) AS [A]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]

Now all of sudden the aggregate which is supposed to count records on outer grouping is computing that on inner one producing wrong results.

@smitpatel
Copy link
Member Author

We should block query patterns like this. Basically when the COUNT(*) is used if there are multiple GroupBys. The subquery (non-lifted translation would also cause incorrect results. Users can specify specific column to count on.

@smitpatel smitpatel added the priority-bug Issues which requires API breaks and have bigger impact hence should be fixed earlier in the release label May 2, 2022
@smitpatel smitpatel added the punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. label Aug 13, 2022
@smitpatel smitpatel modified the milestones: 7.0.0, Backlog Aug 13, 2022
@smitpatel smitpatel removed their assignment Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-groupby area-query customer-reported priority-bug Issues which requires API breaks and have bigger impact hence should be fixed earlier in the release punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-bug
Projects
None yet
Development

No branches or pull requests

2 participants