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

GroupBy FK_Id.HasValue on Sql Server generates invalid sql #15641

Closed
lakeman opened this issue May 7, 2019 · 2 comments · Fixed by #19700
Closed

GroupBy FK_Id.HasValue on Sql Server generates invalid sql #15641

lakeman opened this issue May 7, 2019 · 2 comments · Fixed by #19700
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 punted-for-3.0 type-bug
Milestone

Comments

@lakeman
Copy link

lakeman commented May 7, 2019

Context.Table
.GroupBy(s => new{ s.FKId.HasValue })
.Select(g => new{ g.Key.HasValue })

Generates invalid Sql;

SELECT CASE
   WHEN fk_id IS NOT NULL
   THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END AS [HasValue]
GROUP BY fk_id IS NOT NULL

And fails;

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'IS'.
   at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__122_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)

Generating the same case statement in the group by would fix it.

EF Core version: 2.2.3
Database Provider: Sql Server

@lakeman
Copy link
Author

lakeman commented May 28, 2019

While looking for a workaround I also hit this;

Context.Table
.GroupBy(s => new{ HasValue = s.FKId.HasValue ? 1 : 0 })
.Select(g => new{ HasValue = (g.Key.HasValue == 1)})

Which generated;

SELECT CASE
   WHEN CASE
       WHEN [FKId] IS NOT NULL
       THEN 1 ELSE 0
   END AS [HasValue] = 1
   THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END AS [HasValue]
...

Where the intermediate value shouldn't be named. And finally discovered that this does work;

Context.Table
.GroupBy(s => new{ HasValue = s.FKId.HasValue ? 1 : 0 })
.Select(g => new{ g.Key.HasValue })

And generates straight forward SQL. Though the result set is obviously an int instead of bool, I can live with that for now.

@divega divega modified the milestones: 3.0.0, Backlog Jun 21, 2019
@smitpatel smitpatel removed their assignment Jun 24, 2019
@smitpatel
Copy link
Member

Generated SQL in 3.1

      SELECT CASE
          WHEN [t].[FKId] IS NOT NULL THEN CAST(1 AS bit)
          ELSE CAST(0 AS bit)
      END AS [HasValue]
      FROM [Table] AS [t]
      GROUP BY CASE
          WHEN [t].[FKId] IS NOT NULL THEN CAST(1 AS bit)
          ELSE CAST(0 AS bit)
      END

2nd query gets

      SELECT CASE
          WHEN CASE
              WHEN [t].[FKId] IS NOT NULL THEN 1
              ELSE 0
          END = 1 THEN CAST(1 AS bit)
          ELSE CAST(0 AS bit)
      END AS [HasValue]
      FROM [Table] AS [t]
      GROUP BY CASE
          WHEN [t].[FKId] IS NOT NULL THEN 1
          ELSE 0
      END

@smitpatel smitpatel modified the milestones: Backlog, 5.0.0 Dec 10, 2019
maumar added a commit that referenced this issue Jan 24, 2020
…tes invalid sql

The issue has been fixed earlier.

Resolves #15641
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 25, 2020
@maumar maumar closed this as completed in 6fd1862 Jan 25, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview1 Mar 13, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview1, 5.0.0 Nov 7, 2020
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 punted-for-3.0 type-bug
Projects
None yet
5 participants