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

Don't add grouping key to projection when Distinct is applied #28039

Closed
apolloLegends opened this issue May 17, 2022 · 4 comments · Fixed by #28072
Closed

Don't add grouping key to projection when Distinct is applied #28039

apolloLegends opened this issue May 17, 2022 · 4 comments · Fixed by #28072
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

@apolloLegends
Copy link

apolloLegends commented May 17, 2022

Background

I am upgrading my solution from .NET 5.0.13 to .NET 6.0.5. However, I'm blocked by incorrectly generated SQL that i have been unable to find a workaround for.

I have checked numerous open and closed cases and have not found something that matches my problem.

Problem

-- NET 5.0.13 generated code

exec sp_executesql N'SELECT TOP(@__p_0) [t].[JSON]
FROM [TableData] AS [t]
INNER JOIN (
    SELECT DISTINCT [i].[Parcel]
    FROM [IndexData] AS [i]
    WHERE [i].[Parcel] = N''some condition''
    GROUP BY [i].[Parcel], [i].[RowId]
    HAVING COUNT(*) = 1
) AS [t0] ON [t].[ParcelNumber] = [t0].[Parcel]
WHERE [t].[TableId] = 123
ORDER BY [t].[ParcelNumber]',N'@__p_0 int',@__p_0=123456

-- NET 6.0.1 and 6.0.5 generated code

exec sp_executesql N'SELECT TOP(@__p_0) [t].[JSON]
FROM [TableData] AS [t]
INNER JOIN (
    SELECT DISTINCT [i].[Parcel], [i].[RowId]
    FROM [IndexData] AS [i]
    WHERE [i].[Parcel] = N''some condition''
    GROUP BY [i].[Parcel], [i].[RowId]
    HAVING COUNT(*) = 1
) AS [t0] ON [t].[ParcelNumber] = [t0].[Parcel]
WHERE [t].[TableId] = 123
ORDER BY [t].[ParcelNumber]',N'@__p_0 int',@__p_0=
123456

For clarity, [i].[RowId] should not be in the generated SQL.

Cause

The above SQL is being generated by this code:

var queryResults = (from i in indexResults
                    group i by new { i.ParcelNumber, i.RowId } into grp
                    where grp.Count() == 1
                    select grp.Key.ParcelNumber).Distinct();

var jsonLookup = (from dcv in db.TableData.Where(a => a.TableId == 123)
                  join wos in queryResults
                  on dcv.ParcelNumber equals wos
                  orderby dcv.ParcelNumber
                  select dcv.JSON).Take(123456);

var jsonData = jsonLookup.ToList();

If queryResults is converted to a list such as:

var queryResults = (from i in indexResults
                    group i by new { i.ParcelNumber, i.RowId } into grp
                    where grp.Count() == 1
                    select grp.Key.ParcelNumber).Distinct().ToList();

**It doesnt include the RowId in the query so the problem is somehow related to the join and/or the distinct**

SELECT DISTINCT [i].[Parcel]
FROM [IndexData] AS [i]
WHERE [i].[Parcel] = N'some condition'
GROUP BY [i].[Parcel], [i].[RowId]
HAVING COUNT(*) = 1

Sample

I have attached a simple repro project

  • Create a local SQL database called LinqToSQLBug1 using integrated security
  • Apply migrations
  • Run, review the single generated SQL command in SQL Server Profiler

PP.Core6.SqlBug1.zip

Include provider and version information

EF Core version: 6.0.1 and 6.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.1.6

Thank you!

@AndriySvyryd
Copy link
Member

@smitpatel

@apolloLegends
Copy link
Author

apolloLegends commented May 24, 2022

Thanks for the quick resolution.

Will this be part of 6.0.6?

@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview5 May 25, 2022
@ajcvickers
Copy link
Member

Re-open to discuss patching.

@ajcvickers ajcvickers reopened this May 26, 2022
@ajcvickers ajcvickers added this to the 6.0.x milestone May 31, 2022
@ajcvickers
Copy link
Member

Note from triage: we will prepare a 6.0 patch for this.

@ajcvickers ajcvickers modified the milestones: 6.0.x, 6.0.7 Jun 14, 2022
@ajcvickers ajcvickers changed the title Incorrect SQL Generated Don't add grouping key to projection when Distinct is applied Jul 21, 2022
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

Successfully merging a pull request may close this issue.

4 participants