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

EFCore translates query with GROUP BY and JOIN into invalid SQL #12304

Closed
in10se opened this issue Jun 8, 2018 · 6 comments
Closed

EFCore translates query with GROUP BY and JOIN into invalid SQL #12304

in10se opened this issue Jun 8, 2018 · 6 comments

Comments

@in10se
Copy link

in10se commented Jun 8, 2018

EFCore translates LINQ query into invalid T-SQL code.

Exception message:

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'AS'.

Steps to reproduce

//code with sensitive fields/names changed
var allowedIds = new int[] {}; //static array of int ids

var query = (from c in dbContext.Calculations
				group c by c.Period into rp
				join x in dbContext.Calculations on rp.Key equals x.Period
				where allowedIds.Contains(x.Id) && !x.IsOfficial && x.ExpirationDate == null
				orderby rp.Key
				select x);

//get SQL to be executed
string sql = query.ToSql();
Console.WriteLine(sql);

//error happens here
var calcs = query.ToList();

The EFCore query produces the following T-SQL code:

SELECT 1
FROM [Calculations] AS [c]
INNER JOIN [Calculations] AS [x] ON [c].[Period] AS [Key] = [x].[Period]
GROUP BY [c].[Period]
HAVING ([x].[Id] IN (CAST(1827 AS bigint), CAST(7186 AS bigint)) AND ([x].[IsOfficial] = 0)) AND [x].[ExpirationDate] IS NULL
ORDER BY [Key]

The SQL is invalid.

Further technical details

EF Core version: 2.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: 15.7.3 / 15.8.0 PREVIEW 2

@in10se in10se changed the title EFCore translated query with GROUP BY and JOIN into invalid SQL EFCore translates query with GROUP BY and JOIN into invalid SQL Jun 8, 2018
@ajcvickers ajcvickers added this to the 2.2.0 milestone Jun 8, 2018
@smitpatel
Copy link
Member

Duplicate of #10012

@smitpatel smitpatel marked this as a duplicate of #10012 Jun 8, 2018
@smitpatel smitpatel removed this from the 2.2.0 milestone Jun 8, 2018
@smitpatel
Copy link
Member

Except in this case the lack of aggregate operation blocked us from making it client eval.

Making it client eval is not possible due to #11215

And it could be mitigated fully by #12089

@ajcvickers
Copy link
Member

@smitpatel to document workaround. Fix is considered too risky at this time for patch release.

@ajcvickers ajcvickers added this to the 2.2.0 milestone Jun 12, 2018
@smitpatel
Copy link
Member

Work-around:

                var allowedIds = new int[] { 1827, 7186 }; //static array of int ids

                var query = (from rp in (from c in db.Calculations
                                        select new { Key = c.Period })
                                        .Distinct()
                             join x in db.Calculations on rp.Key equals x.Period
                             where allowedIds.Contains(x.Id) && !x.IsOfficial && x.ExpirationDate == null
                             orderby rp.Key
                             select x);

                //error happens here
                var calcs = query.ToList();

Generates SQL

      SELECT [t].[Key], [x].[Id], [x].[ExpirationDate], [x].[IsOfficial], [x].[Period]
      FROM (
          SELECT DISTINCT [c].[Period] AS [Key]
          FROM [Calculations] AS [c]
      ) AS [t]
      INNER JOIN [Calculations] AS [x] ON [t].[Key] = [x].[Period]
      WHERE ([x].[Id] IN (1827, 7186) AND ([x].[IsOfficial] = 0)) AND [x].[ExpirationDate] IS NULL
      ORDER BY [t].[Key]

The error happens because grouping is not being referenced apart from key selector so our blocking mechanism in #10012 fails and we translate group by with join generating invalid SQL.
Since only grouping key is used you can remove group by and do custom projection of grouping key and call distinct over that to get same results.

@smitpatel
Copy link
Member

Duplicate of #12826

@smitpatel
Copy link
Member

Closing as #12826 tracks adding regression test for this.

@smitpatel smitpatel removed this from the Backlog milestone Dec 6, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
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