Skip to content

Must be reductible node #11933

Closed
Closed

Description

@NicolasDorier

Describe what is not working as expected.

I am migrating to .NET 2.1, but it seems entity framework broke:
This was working in 2.0:

var userId = GetUserId();
using (var ctx = _ContextFactory.CreateContext())
{
    return await ctx.UserStore
        .Where(us => us.ApplicationUserId == userId)
        .SelectMany(us => us.StoreData.Apps.Select(app => new ListAppsViewModel.ListAppViewModel()
        {
            IsOwner = us.Role == StoreRoles.Owner,
            StoreId = us.StoreDataId,
            StoreName = us.Role,
            AppName = app.Name,
            AppType = app.AppType,
            Id = app.Id
        }))
        .ToArrayAsync();

Now it throws

at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at BTCPayServer.Tests.UnitTest1.CanUsePoSApp() in C:\Sources\btcpayserver\BTCPayServer.Tests\UnitTest1.cs:line 1180
----- Inner Stack Trace -----
   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.MemberAssignmentRewriter..ctor(MemberAssignment binding, StackSpiller spiller, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.BindingRewriter.Create(MemberBinding binding, StackSpiller spiller, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberInitExpression(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](Expression`1 lambda)
   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)
   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](Expression`1 lambda)
   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)
   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 expression)
   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 expression)
   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](Expression`1 lambda)
   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
   at System.Linq.Expressions.Expression`1.Compile(Boolean preferInterpretation)
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambda[TResults]()
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateAsyncQueryExecutor[TResult](QueryModel queryModel)
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.System.Collections.Generic.IAsyncEnumerable<TResult>.GetEnumerator()
   at System.Collections.Generic.AsyncEnumerableHelpers.ToArrayWithLength[T](IAsyncEnumerable`1 source, CancellationToken cancellationToken)
   at System.Collections.Generic.AsyncEnumerableHelpers.ToArray[T](IAsyncEnumerable`1 source, CancellationToken cancellationToken)
   at BTCPayServer.Controllers.AppsController.GetAllApps() in C:\Sources\btcpayserver\BTCPayServer\Controllers\AppsController.cs:line 178
   at BTCPayServer.Controllers.AppsController.ListApps() in C:\Sources\btcpayserver\BTCPayServer\Controllers\AppsController.cs:line 44

Note that if I comment

            StoreId = us.StoreDataId,
            StoreName = us.Role,

it does not crash.

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-rc1-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version=" 2.1.0-rc1-final" PrivateAssets="All" />

Workaround:

return await ctx.UserStore
    .Where(us => us.ApplicationUserId == userId)
    .Join(ctx.Apps, us => us.StoreDataId, app => app.StoreDataId,
    (us, app) =>
    new ListAppsViewModel.ListAppViewModel()
    {
        IsOwner = us.Role == StoreRoles.Owner,
        StoreId = us.StoreDataId,
        StoreName = us.StoreData.StoreName,
        AppName = app.Name,
        AppType = app.AppType,
        Id = app.Id
    })
    .ToArrayAsync();

Activity

  1. roji commented on May 8, 2018

    @roji
    Member

    Just to get more data, would it be possible to test with 2.1.0-preview2-final for all packages (both Microsoft and Npgsql)? This would tell us whether is coming from mixing 2.0 and 2.1

    (Npgsql's rc1 packages aren't out yet, but that should happen very soon)

  2. NicolasDorier commented on May 8, 2018

    @NicolasDorier
    Author

    sure one moment

  3. NicolasDorier commented on May 8, 2018

    @NicolasDorier
    Author

    Same issue in:

    • 2.1.0-preview2-final
    • 2.1.0-preview1-final
  4. NicolasDorier commented on May 8, 2018

    @NicolasDorier
    Author

    Note I did not updated Npgsql

  5. roji commented on May 8, 2018

    @roji
    Member

    Note I did not updated Npgsql

    @NicolasDorier as I wrote above the idea is to align both Microsoft and Npgsql versions... Can you please try with all packages on 2.1.0-preview2, both Microsoft and Npgsql?

  6. NicolasDorier commented on May 8, 2018

    @NicolasDorier
    Author

    Ah sorry, I missed the preview package for Postgres as they were not listed by the nuget UI, trying now.

  7. NicolasDorier commented on May 8, 2018

    @NicolasDorier
    Author

    Both 2.1.0-preview2, same issue. It is interesting that postgres 2.0.2 works with 2.0.1rc, but postgres 2.1preview does not work with 2.0.1rc. (unrelated error)

  8. bmarder commented on May 8, 2018

    @bmarder

    I received this error frequently with certain queries as well, using sqlserver and 2.1 preview 2. @NicolasDorier Thanks for giving a full repo for this issue.

  9. ajcvickers commented on May 8, 2018

    @ajcvickers
    Contributor

    @maumar Can you investigate this with regard to it being a regression in 2.1?

  10. maumar commented on May 8, 2018

    @maumar
    Contributor

    @ajcvickers verified this is a regression - the code was working fine in 2.0.2

  11. maumar commented on May 8, 2018

    @maumar
    Contributor

    query plan (with non-reducible nodes highlighted):

    (QueryContext queryContext) => IAsyncEnumerable<ListAppViewModel> _InterceptExceptions(
        source: IAsyncEnumerable<ListAppViewModel> _Select(
            source: IAsyncEnumerable<TransparentIdentifier<TransparentIdentifier<ValueBuffer, ValueBuffer>, ListAppViewModel>> _SelectMany(
                source: IAsyncEnumerable<TransparentIdentifier<ValueBuffer, ValueBuffer>> _ShapedQuery(
                    queryContext: queryContext, 
                    shaperCommandContext: SelectExpression: 
                        SELECT [us.StoreData].[Id], [us].[Role], [us].[StoreDataId]
                        FROM [UserStore] AS [us]
                        INNER JOIN [StoreData] AS [us.StoreData] ON [us].[StoreDataId] = [us.StoreData].[Id]
                        WHERE [us].[ApplicationUserId] = @__userId_0, 
                    shaper: TypedCompositeShaper<ValueBufferShaper, ValueBuffer, ValueBufferShaper, ValueBuffer, TransparentIdentifier<ValueBuffer, ValueBuffer>>), 
                collectionSelector: (TransparentIdentifier<ValueBuffer, ValueBuffer> t0) => IAsyncEnumerable<ListAppViewModel> _Select(
                    source: IAsyncEnumerable<ValueBuffer> _ShapedQuery(
                        queryContext: queryContext, 
                        shaperCommandContext: SelectExpression: 
                            SELECT [t].[IsOwner], [t].[StoreId], [t].[StoreName], [t].[AppName], [t].[AppType], [t].[Id]
                            FROM (
                                SELECT CASE
                                    WHEN [us].[Role] = 1
                                    THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
                                END AS [IsOwner], [us].[StoreDataId] AS [StoreId], [us].[Role] AS [StoreName], [app].[Name] AS [AppName], [app].[AppType], [app].[Id]
                                FROM [App] AS [app]
                                WHERE [us.StoreData].[Id] = [app].[StoreDataId]
                            ) AS [t], 
                        shaper: ValueBufferShaper), 
                    selector: (ValueBuffer app) => new ListAppViewModel{ 
                        IsOwner = (bool)Nullable<bool> TryReadValue(app, 0, null), 
                        StoreId =  ---> [us] <--- .StoreDataId, 
                        StoreName =  ---> [us] <--- .Role, 
                        AppName = string TryReadValue(app, 3, App.Name), 
                        AppType = AppType TryReadValue(app, 4, App.AppType), 
                        Id = int TryReadValue(app, 5, App.Id) 
                    }
                ), 
                resultSelector: (TransparentIdentifier<ValueBuffer, ValueBuffer> t0 | ListAppViewModel <generated>_0) => TransparentIdentifier<TransparentIdentifier<ValueBuffer, ValueBuffer>, ListAppViewModel> CreateTransparentIdentifier(
                    outer: t0, 
                    inner: <generated>_0)), 
            selector: (TransparentIdentifier<TransparentIdentifier<ValueBuffer, ValueBuffer>, ListAppViewModel> t1) => t1.Inner), 
        contextType: Repro11933.MyContext, 
        logger: DiagnosticsLogger<Query>, 
        queryContext: queryContext)
    
  12. maumar commented on May 8, 2018

    @maumar
    Contributor

    query plan in 2.0.2 - SelectMany gets translated, in current bits it gets client-evaled

    Microsoft.EntityFrameworkCore.Query[10107]
          (QueryContext queryContext) => IAsyncEnumerable<ListAppViewModel> _InterceptExceptions(
              source: IAsyncEnumerable<ListAppViewModel> _ShapedQuery(
                  queryContext: queryContext,
                  shaperCommandContext: SelectExpression:
                      SELECT CASE
                          WHEN [us].[Role] = 1
                          THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
                      END AS [IsOwner], [us].[StoreDataId] AS [StoreId], [us].[Role] AS [StoreName], [us.StoreData.Apps].[Name] AS [AppName], [us.StoreData.Apps].[AppType], [us.StoreData.Apps].[Id]
                      FROM [UserStore] AS [us]
                      INNER JOIN [StoreData] AS [us.StoreData] ON [us].[StoreDataId] = [us.StoreData].[Id]
                      INNER JOIN [App] AS [us.StoreData.Apps] ON [us.StoreData].[Id] = [us.StoreData.Apps].[StoreDataId]
                      WHERE [us].[ApplicationUserId] = @__userId_0,
                  shaper: TypedProjectionShaper<TypedCompositeShaper<TypedCompositeShaper<ValueBufferShaper, ValueBuffer, ValueBufferShaper, ValueBuffer, TransparentIdentifier<ValueBuffer, ValueBuffer>>, TransparentIdentifier<ValueBuffer, ValueBuffer>, ValueBufferShaper, ValueBuffer, TransparentIdentifier<TransparentIdentifier<ValueBuffer, ValueBuffer>, ValueBuffer>>, TransparentIdentifier<TransparentIdentifier<ValueBuffer, ValueBuffer>, ValueBuffer>, ListAppViewModel>),
              contextType: EFSampleApp.MyContext,
              logger: DiagnosticsLogger<Query>,
              queryContext: queryContext)
    
  13. ajcvickers commented on May 8, 2018

    @ajcvickers
    Contributor

    @maumar Do you need @anpete or @smitpatel to help on this?
    /cc @divega

  14. 48 remaining items

  15. modified the milestones: , 3.0.0 on Sep 11, 2019
  16. maumar commented on Sep 11, 2019

    @maumar
    Contributor

    verified this issue has been fixed in 3.0

  17. smitpatel commented on Sep 11, 2019

    @smitpatel
    Contributor

    @maumar - Regression test.

  18. maumar commented on Sep 11, 2019

    @maumar
    Contributor

    @smitpatel i have test ready, will send PR with regressions for multiple issues

  19. btecu commented on Sep 11, 2019

    @btecu
    Contributor

    @ajcvickers I tested preview 9 and confirm that previous query that in 2.x was broken now works.

  20. added 2 commits that reference this issue on Sep 12, 2019
    6443c98
    36a7bdf
  21. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions