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

Invalid query generated for simple group by #14243

Closed
NitinNotebook opened this issue Dec 24, 2018 · 5 comments
Closed

Invalid query generated for simple group by #14243

NitinNotebook opened this issue Dec 24, 2018 · 5 comments

Comments

@NitinNotebook
Copy link

NitinNotebook commented Dec 24, 2018

Describe what is not working as expected.
Simple group by generating invalid query and also warning that Sum getting evaluated in local

List<int> lstShiftIds;
var efQuery = from shiftDetail in ShiftDetailSet
                          where lstShiftIds.Contains(shiftDetail.ShiftId) 
                          group shiftDetail by new { shiftDetail.ShiftId, shiftDetail.TpId } into g                          
                          select new ShiftNetAmount
                          {
                              ShiftId = g.Key.ShiftId,
                              TpId = g.Key.TpId,
                              NetAmount = g.Sum(s=> s.Jul) 
                          };

Exception message:
Issue 1
Above LINQ expression generates Invalid Query:
SELECT "shiftDetail"."ShiftId", "shiftDetail"."TpId", "shiftDetail"."Jul" AS "NetAmount"
FROM "ShiftDetail" AS "shiftDetail"
WHERE "shiftDetail"."ShiftId" IN (1000000, 1000100, 1000200)
GROUP BY "shiftDetail"."ShiftId", "shiftDetail"."TpId"

Column: "shiftDetail"."Jul" is not in Group By but in select

Issue 2
Sum should be evaluated on DB Server, EF core throws following warning

Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'Sum()' could not be translated and will be evaluated locally.

Steps to reproduce

EF query provided above, let me know if more information is required.

Further technical details

EF Core version: 2.2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlLite 2.2.0
Operating system: Win 10
IDE: Visual Studio 2017 15.9.4

@smitpatel
Copy link
Member

I am not able to reproduce the issue based on above data. The exact same query is working fine for me.
Please submit a detailed repro code which demonstrate the issue you are seeing
Repro Code:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace EFSampleApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var db = new MyContext())
            {
                // Recreate database
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();

                // Seed database


                db.SaveChanges();
            }

            using (var db = new MyContext())
            {
                // Run queries
                var ids = new List<int> { 1, 2, 3 };
                var efQuery = from blog in db.Blogs
                              where ids.Contains(blog.Id)
                              group blog by new { blog.Id, blog.TpId } into g
                              select new NetAmountDto
                              {
                                  Id = g.Key.Id,
                                  TpId = g.Key.TpId,
                                  NetAmount = g.Sum(s => s.Jul)
                              };

                efQuery.ToList();
            }

            Console.WriteLine("Program finished.");
        }
    }


    public class MyContext : DbContext
    {
        private static ILoggerFactory LoggerFactory => new LoggerFactory().AddConsole(LogLevel.Trace);

        // Declare DBSets
        public DbSet<Blog> Blogs { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // Select 1 provider
            optionsBuilder
                .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0")
                //.UseSqlite("filename=_modelApp.db")
                //.UseInMemoryDatabase(databaseName: "_modelApp")
                .EnableSensitiveDataLogging()
                .UseLoggerFactory(LoggerFactory);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Configure model
        }
    }

    public class Blog
    {
        public int Id { get; set; }
        public int TpId { get; set; }
        public int Jul { get; set; }
    }

    public class NetAmountDto
    {
        public int Id { get; set; }
        public int TpId { get; set; }
        public int NetAmount { get; set; }
    }
}

Generated SQL:

      SELECT [blog].[Id], [blog].[TpId], SUM([blog].[Jul]) AS [NetAmount]
      FROM [Blogs] AS [blog]
      WHERE [blog].[Id] IN (1, 2, 3)
      GROUP BY [blog].[Id], [blog].[TpId]

@NitinNotebook
Copy link
Author

NitinNotebook commented Dec 29, 2018

EfCoreSample.zip

Thank you for quick response. I have attached only relevant version of the code in solution where I was able to repro. The Query printed in (debug) output window is as follows:
SELECT "sd"."ShiftId", "sd"."TpId", "sd"."Jan", "sd"."Feb", "sd"."Mar"
FROM "ShiftDetail" AS "sd"
WHERE "sd"."ShiftId" IN (1, 2)
GROUP BY "sd"."ShiftId", "sd"."TpId"

#Update: I tried putting your code in SqlLite it still does not work in my sample
Just checked with UseSqlServer, it works fine as you have suggested. So the problem is only with SqlLite.

Another observation:
Below code leads to evaluation of query on client
NetAmount = g.Sum(s => s.Jan + s.Feb + s.Mar)

And below generates a proper query with server side aggregation:
NetAmount = g.Sum(s => s.Jan) + g.Sum(s => s.Feb) + g.Sum(s => s.Mar)

@ajcvickers
Copy link
Member

@smitpatel to follow up.

@smitpatel
Copy link
Member

Duplicate of #14083

@smitpatel smitpatel marked this as a duplicate of #14083 Jan 4, 2019
@smitpatel
Copy link
Member

Duplicate of #12089

@smitpatel smitpatel marked this as a duplicate of #12089 Jan 4, 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