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: incorrect query plan produced for complex query with groupjoins #7654

Closed
maumar opened this issue Feb 18, 2017 · 4 comments
Closed

Query: incorrect query plan produced for complex query with groupjoins #7654

maumar opened this issue Feb 18, 2017 · 4 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Feb 18, 2017

query:

                var query =
                from l1_outer in
                    (from l1_inner in ctx.LevelOne
                     join l2_inner in ctx.LevelTwo on l1_inner.Id equals l2_inner.Level1_Optional_Id into grouping_inner
                     from l2_inner in grouping_inner.DefaultIfEmpty()
                     select l2_inner).Take(2)
                join l2_outer in ctx.LevelTwo on l1_outer.Id equals l2_outer.Level1_Optional_Id into grouping_outer
                from l2_outer in grouping_outer.DefaultIfEmpty()
                select l2_outer.Name;

                var result = query.ToList();

throws:

must be reducible node
at System.Linq.Expressions.Expression.ReduceAndCheck()
at System.Linq.Expressions.Expression.ReduceExtensions()
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteUnaryExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression1 lambda) at System.Linq.Expressions.Expression1.Accept(StackSpiller spiller)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression1 lambda) at System.Linq.Expressions.Expression1.Accept(StackSpiller spiller)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Linq.Expressions.Expression`1.Compile()
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambdaTResults
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)

@rowanmiller rowanmiller added this to the 2.0.0 milestone Feb 22, 2017
maumar added a commit that referenced this issue Apr 13, 2017
The scenario itself is working already
@maumar
Copy link
Contributor Author

maumar commented Apr 13, 2017

This works in relational now, but still fails for InMemory. Exception:

System.InvalidOperationException : variable 't1' of type 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Storage.ValueBuffer,System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Storage.ValueBuffer]],Microsoft.EntityFrameworkCore.Storage.ValueBuffer]' referenced from scope '', but it is not defined
	at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
	at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
	at System.Linq.Expressions.ParameterExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.ExpressionVisitor.VisitMember(MemberExpression node)
	at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
	at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
	at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.Compiler.VariableBinder.VisitUnary(UnaryExpression node)
	at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
	at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
	at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
	at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
	at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
	at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
	at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
	at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
	at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
	at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
	at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
	at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
	at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
	at System.Linq.Expressions.Expression`1.Compile()

@maumar
Copy link
Contributor Author

maumar commented Apr 13, 2017

query plan:

(QueryContext queryContext) => IEnumerable<string> _InterceptExceptions(
    source: IEnumerable<string> _Select(
        source: IEnumerable<TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>, ValueBuffer>> _SelectMany(
            source: IEnumerable<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>> _GroupJoin(
                outer: IEnumerable<ValueBuffer> _Select(
                    source: IEnumerable<TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>, ValueBuffer>> Take(
                        source: IEnumerable<TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>, ValueBuffer>> _SelectMany(
                            source: IEnumerable<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>> _GroupJoin(
                                outer: IEnumerable<ValueBuffer> ProjectionQuery(
                                    queryContext: queryContext, 
                                    entityType: EntityType: Level1), 
                                inner: IEnumerable<ValueBuffer> ProjectionQuery(
                                    queryContext: queryContext, 
                                    entityType: EntityType: Level2), 
                                outerKeySelector: (ValueBuffer l1_inner) => (Nullable<int>) int TryReadValue(l1_inner, 0, Level1.Id), 
                                innerKeySelector: (ValueBuffer l2_inner) => Nullable<int> TryReadValue(l2_inner, 2, Level2.Level1_Optional_Id), 
                                resultSelector: (ValueBuffer l1_inner | IEnumerable<ValueBuffer> grouping_inner) => TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>> CreateTransparentIdentifier(
                                    outer: l1_inner, 
                                    inner: grouping_inner)), 
                            collectionSelector: (TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>> t0) => IEnumerable<ValueBuffer> DefaultIfEmpty(t0.Inner), 
                            resultSelector: (TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>> t0 | ValueBuffer l2_inner) => TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>, ValueBuffer> CreateTransparentIdentifier(
                                outer: t0, 
                                inner: l2_inner)), 
                        count: int GetParameterValue(
                            queryContext: queryContext, 
                            parameterName: "__p_0")), 
                    selector: (TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>, ValueBuffer> t1) => t1.Inner), 
                inner: IEnumerable<ValueBuffer> ProjectionQuery(
                    queryContext: queryContext, 
                    entityType: EntityType: Level2), 
                outerKeySelector: (ValueBuffer l1_outer) => (Nullable<int>) int TryReadValue(Unhandled parameter: t1.Inner, 0, Level2.Id), 
                innerKeySelector: (ValueBuffer l2_outer) => Nullable<int> TryReadValue(l2_outer, 2, Level2.Level1_Optional_Id), 
                resultSelector: (ValueBuffer l1_outer | IEnumerable<ValueBuffer> grouping_outer) => TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>> CreateTransparentIdentifier(
                    outer: l1_outer, 
                    inner: grouping_outer)), 
            collectionSelector: (TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>> t0) => IEnumerable<ValueBuffer> DefaultIfEmpty(t0.Inner), 
            resultSelector: (TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>> t0 | ValueBuffer l2_outer) => TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>, ValueBuffer> CreateTransparentIdentifier(
                outer: t0, 
                inner: l2_outer)), 
        selector: (TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<ValueBuffer>>, ValueBuffer> t1) => string TryReadValue(t1.Inner, 4, Level2.Name)), 
    contextType: Specification.Tests.TestModels.ComplexNavigationsModel.ComplexNavigationsContext, 
    logger: InterceptingLogger<Query>, 
    queryContext: queryContext)

maumar added a commit that referenced this issue Apr 13, 2017
@smitpatel
Copy link
Member

Verify if the InMemory failure is same as this #4311

maumar added a commit that referenced this issue Apr 13, 2017
@maumar
Copy link
Contributor Author

maumar commented Apr 13, 2017

Fixed, test added in 28a2d66

@maumar maumar closed this as completed Apr 13, 2017
@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 Apr 13, 2017
@ajcvickers ajcvickers changed the title Query : incorrect query plan produced for complex query with groupjoins Query: incorrect query plan produced for complex query with groupjoins May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
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. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants