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

Query: compilation error for complex query with group by and correlated subquery #11178

Closed
maumar opened this issue Mar 7, 2018 · 8 comments
Closed

Comments

@maumar
Copy link
Contributor

maumar commented Mar 7, 2018

query:

                var query = ctx.Parents
                        .Select(p => new
                        {
                            ParentId = p.Id,
                            Bars = p.Children
                                        .Select(c => c.Group)
                                        .GroupBy(g => g)
                                        .Select(g => new
                                        {
                                            Group = g.Key,
                                            GroupCount = g.Count()
                                        })
                        }).ToList();

throws:

Unhandled Exception: System.ArgumentException: Expression of type 'System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Storage.ValueBuffer]' cannot be used for parameter of type 'Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext' of method 'System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Storage.ValueBuffer] _ShapedQuery[ValueBuffer](Microsoft.EntityFrameworkCore.Query.QueryContext, Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext, Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.IShaper`1[Microsoft.EntityFrameworkCore.Storage.ValueBuffer])'
Parameter name: arg1
   at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
   at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0, Expression arg1, Expression arg2)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.GroupByAggregateTranslatingExpressionVisitor.CanTranslate(Expression expression) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\ExpressionVisitors\RelationalProjectionExpressionVisitor.cs:line 459
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.GroupByAggregateTranslatingExpressionVisitor.Translate(Expression expression) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\ExpressionVisitors\RelationalProjectionExpressionVisitor.cs:line 313
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\ExpressionVisitors\RelationalProjectionExpressionVisitor.cs:line 161
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore\Query\EntityQueryModelVisitor.cs:line 1166
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\RelationalQueryModelVisitor.cs:line 1130
   at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore\Query\EntityQueryModelVisitor.cs:line 758
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\RelationalQueryModelVisitor.cs:line 292
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ProjectionExpressionVisitor.VisitSubQuery(SubQueryExpression expression) in D:\git\EntityFrameworkCore\src\EFCore\Query\ExpressionVisitors\ProjectionExpressionVisitor.cs:line 192
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\ExpressionVisitors\RelationalProjectionExpressionVisitor.cs:line 215
   at System.Linq.Expressions.ExpressionVisitor.VisitAndConvert[T](ReadOnlyCollection`1 nodes, String callerName)
   at Remotion.Linq.Parsing.RelinqExpressionVisitor.VisitNew(NewExpression expression)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.VisitNew(NewExpression newExpression) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\ExpressionVisitors\RelationalProjectionExpressionVisitor.cs:line 123
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\ExpressionVisitors\RelationalProjectionExpressionVisitor.cs:line 187
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore\Query\EntityQueryModelVisitor.cs:line 1166
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\RelationalQueryModelVisitor.cs:line 1130
   at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore\Query\EntityQueryModelVisitor.cs:line 758
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore.Relational\Query\RelationalQueryModelVisitor.cs:line 292
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore\Query\EntityQueryModelVisitor.cs:line 175
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore\Storage\Database.cs:line 70
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType) in D:\git\EntityFrameworkCore\src\EFCore\Query\Internal\QueryCompiler.cs:line 151
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0`1.<Execute>b__0() in D:\git\EntityFrameworkCore\src\EFCore\Query\Internal\QueryCompiler.cs:line 91
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler) in D:\git\EntityFrameworkCore\src\EFCore\Query\Internal\CompiledQueryCache.cs:line 69
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) in D:\git\EntityFrameworkCore\src\EFCore\Query\Internal\CompiledQueryCache.cs:line 44
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) in D:\git\EntityFrameworkCore\src\EFCore\Query\Internal\QueryCompiler.cs:line 87
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) in D:\git\EntityFrameworkCore\src\EFCore\Query\Internal\EntityQueryProvider.cs:line 62
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
@maumar
Copy link
Contributor Author

maumar commented Mar 7, 2018

GroupByAggregateTranslatingExpressionVisitor.CanTranslate() tries to extract ShapedQuery from _queryModel.Expression. It doesn't take into account that expression can have InjectParameters around it.

@smitpatel
Copy link
Member

:trollface: not needed.

@ajcvickers ajcvickers added this to the Backlog milestone Mar 9, 2018
@gnaeus
Copy link

gnaeus commented Mar 23, 2018

I have the same exception with more simple example:

public class User
{
    public int Id { get; set; }
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public bool IsDeleted { get; set; }
}

IEnumerable<Post> posts = context.Users
    .Select(u => u.Posts
        .Where(p => !p.IsDeleted)) // throws same Exception
    .FirstOrDefault();

But if we select List<T> instead of IEnumerable<T>, everything works Ok:

List<Post> posts = context.Users
    .Select(u => u.Posts
        .Where(p => !p.IsDeleted)
        .ToList()) // works Ok
    .FirstOrDefault();

Also, if we rewrite the original query with .ToList(), it should work too:

var query = ctx.Parents
    .Select(p => new
    {
        ParentId = p.Id,
        Bars = p.Children
            .Select(c => c.Group)
            .GroupBy(g => g)
            .Select(g => new
            {
                Group = g.Key,
                GroupCount = g.Count()
            })
            .ToList() // <---
    })
    .ToList();

@smitpatel
Copy link
Member

@maumar - Can you run the first query in current dev?

@maumar
Copy link
Contributor Author

maumar commented Mar 23, 2018

@smitpatel repros in current dev

@maumar maumar removed this from the Backlog milestone Mar 23, 2018
@divega divega added this to the 2.1.0 milestone Mar 23, 2018
@divega
Copy link
Contributor

divega commented Mar 23, 2018

@maumar please create the new issue and put this back in the backlog.

@maumar
Copy link
Contributor Author

maumar commented Mar 23, 2018

filed #11403

@smitpatel
Copy link
Member

In the above query Bars requires correlation predicate with outer to effectively generate the collection. Due to GroupBy we cannot add additional column hence it cannot be translated to server.

@smitpatel smitpatel removed their assignment Nov 22, 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

5 participants