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

Convert to Nullable in a group key throws ArgumentException (regression in 3.0-preview7) #16844

Closed
AlekseyMartynov opened this issue Jul 30, 2019 · 2 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@AlekseyMartynov
Copy link

Microsoft.EntityFrameworkCore.SqlServer version 3.0.0-preview7.19362.6

The query below runs fine with EF Core 2.x.

Code to reproduce

class Program {
    static void Main(string[] args) {
        using(var db = new MyContext()) {
            var query = db.Set<MyClass1>()
                .GroupBy(obj => new { I0 = (int?)obj.Date.Value.Year }) // HERE
                .OrderBy(g => g.Key.I0)
                .Select(g => new {
                    I0 = g.Count(),
                    I1 = g.Key.I0
                });

            var data = query.ToArray();
        }
    }
}

public class MyClass1 {
    public int ID { get; set; }
    public DateTime? Date { get; set; }
}

public class MyContext : DbContext {
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
        if(!optionsBuilder.IsConfigured)
            optionsBuilder.UseSqlServer(...);
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.Entity<MyClass1>();
    }
}

Exception

System.ArgumentException
  HResult=0x80070057
  Message= Argument type 'System.Int32' does not match the corresponding member type 'System.Nullable`1[System.Int32]' 
  Source=System.Linq.Expressions
  StackTrace:
   at System.Linq.Expressions.Expression.ValidateNewArgs(ConstructorInfo constructor, ReadOnlyCollection`1& arguments, ReadOnlyCollection`1& members)
   at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments, IEnumerable`1 members)
   at System.Linq.Expressions.NewExpression.Update(IEnumerable`1 arguments)
   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateGroupBy(ShapedQueryExpression source, LambdaExpression keySelector, LambdaExpression elementSelector, LambdaExpression resultSelector)
   at Microsoft.EntityFrameworkCore.Query.Pipeline.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.Pipeline.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.Pipeline.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.Pipeline.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Program.Main(String[] args) in s:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 16
@smitpatel
Copy link
Member

Need to wrap convert nodes in TranslateGroupingKey method to match types in client side code. (also necessary changes when the grouping key is used to plug into Sql tree)

@ajcvickers ajcvickers added this to the 3.0.0 milestone Aug 5, 2019
@smitpatel
Copy link
Member

poaching.

@smitpatel smitpatel assigned smitpatel and unassigned maumar Aug 15, 2019
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 15, 2019
smitpatel added a commit that referenced this issue Aug 15, 2019
Constant/Parameter can be put in projection even if not appearing in grouping key in SQL.
So now we avoid putting Constant/parameter in GROUP BY clause but treat it as normal in other places.
When generating grouping key, wrap convert node to match types in initialization expression (as SQL tree ignores type nullability)
Also merged leftover async group by async tests in single version.

Resolves #14152
Resovles #16844
smitpatel added a commit that referenced this issue Aug 15, 2019
Constant/Parameter can be put in projection even if not appearing in grouping key in SQL.
So now we avoid putting Constant/parameter in GROUP BY clause but treat it as normal in other places.
When generating grouping key, wrap convert node to match types in initialization expression (as SQL tree ignores type nullability)
Also merged leftover async group by async tests in single version.

Resolves #14152
Resovles #16844
smitpatel added a commit that referenced this issue Aug 16, 2019
Constant/Parameter can be put in projection even if not appearing in grouping key in SQL.
So now we avoid putting Constant/parameter in GROUP BY clause but treat it as normal in other places.
When generating grouping key, wrap convert node to match types in initialization expression (as SQL tree ignores type nullability)
Also merged leftover async group by async tests in single version.

Resolves #14152
Resovles #16844
smitpatel added a commit that referenced this issue Aug 16, 2019
Constant/Parameter can be put in projection even if not appearing in grouping key in SQL.
So now we avoid putting Constant/parameter in GROUP BY clause but treat it as normal in other places.
When generating grouping key, wrap convert node to match types in initialization expression (as SQL tree ignores type nullability)
Also merged leftover async group by async tests in single version.

Resolves #14152
Resovles #16844
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview9 Aug 21, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview9, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

No branches or pull requests

4 participants