Skip to content

QueryRewrite: add navigation expansion support for GroupBy #15249

Closed

Description

@maumar

This is complicated because navigations that appear in the grouping selector need to be expanded before the groupby call itself. This probably requires a number of "pending" operations to be stored on NavigationExpansionExpression, just like we do for projections and order by.

Activity

  1. self-assigned this
    on Apr 3, 2019
  2. added this to the 3.0.0 milestone on Apr 3, 2019
  3. smitpatel commented on Jul 2, 2019

    @smitpatel
    Contributor
    IQueryable<<>f__AnonymousType276<Customer, int>> Join(
    |__ outer: DbSet<Customer>, 
    |__ inner: IQueryable<<>f__AnonymousType273<string, int>> Select(
    |   |__ source: IQueryable<IGrouping<string, Order>> Where(
    |   |   |__ source: IQueryable<IGrouping<string, Order>> GroupBy(
    |   |   |   |__ source: DbSet<Order>, 
    |   |   |   |__ keySelector: (Order o) => o.CustomerID), 
    |   |   |__ predicate: (IGrouping<string, Order> g) => int Count(g) > 5), 
    |   |__ selector: (IGrouping<string, Order> g) => new { 
    |           CustomerID = g.Key, 
    |           LastOrderID = int Max(
    |           |__ source: g, 
    |           |__ selector: (Order o) => o.OrderID)
    |        }), 
    |__ outerKeySelector: (Customer c) => c.CustomerID, 
    |__ innerKeySelector: (<>f__AnonymousType273<string, int> a) => a.CustomerID, 
    |__ resultSelector: (Customer c | <>f__AnonymousType273<string, int> a) => new { 
            c = c, 
            LastOrderID = a.LastOrderID
         })
    

    After nav expansion

    IQueryable<<>f__AnonymousType276<Customer, int>> Select(
    |__ source: IQueryable<TransparentIdentifier<Customer, IGrouping<string, Order>>> Join(
    |   |__ outer: DbSet<Customer>, 
    |   |__ inner: IQueryable<IGrouping<string, Order>> Where(
    |   |   |__ source: IQueryable<IGrouping<string, Order>> GroupBy(
    |   |   |   |__ source: DbSet<Order>, 
    |   |   |   |__ keySelector: (Order o) => o.CustomerID), 
    |   |   |__ predicate: (IGrouping<string, Order> Param_0) => int Count(Param_0) > 5), 
    |   |__ outerKeySelector: (Customer Param_0) => Param_0.CustomerID, 
    |   |__ innerKeySelector: (IGrouping<string, Order> Param_0) => Param_0.Key, 
    |   |__ resultSelector: (Customer Param_0 | IGrouping<string, Order> Param_0) => new TransparentIdentifier<Customer, IGrouping<string, Order>>(
    |           Outer = Param_0, 
    |           Inner = Param_0
    |       )), 
    |__ selector: (TransparentIdentifier<Customer, IGrouping<string, Order>> join) => new { 
            c = join.Outer, 
            LastOrderID = int Max(
            |__ source: join.Inner, 
            |__ selector: (Order Param_0) => Param_0.OrderID)
         })
    

    Nav expansion moving aggregates inside joins make it hard to translate.

  4. added 2 commits that reference this issue on Aug 21, 2019
    72a01e3
    f26440b
  5. added a commit that references this issue on Aug 21, 2019
    cb043a2
  6. modified the milestones: 3.0.0, on Aug 21, 2019
  7. kosinsky commented on Aug 26, 2019

    @kosinsky
    Contributor

    @smitpatel For queries like (in #14152 you mentioned that it's duplicate form this issue):

                return AssertQuery<Customer>(
                    isAsync,
                    cs =>
                        cs.GroupBy(c => c.City)
                            .Select(
                                g => new
                                {
                                    g.Key,
                                    c = g.SelectMany(o=>o.Orders).Count()
                                }),
                    e => e.Key);

    It still fails with:

     System.InvalidOperationException : Processing of the LINQ expression 'AsQueryable<Order>(SelectMany<Customer, Order>(
            source: NavigationTreeExpression
                Value: default(IGrouping<string, Customer>)
                Expression: (Unhandled parameter: e), 
            selector: (o) => o.Orders))' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core.
    

    My guess it caused by g.SelectMany(o=>o.Orders).Count() really being g.SelectMany(o=>o.Orders).AsQueryable().Count() expression and as a result (firstArgument is NavigationExpansionExpression source) is false

    Should I create separate issue for that?

  8. smitpatel commented on Aug 26, 2019

    @smitpatel
    Contributor

    That would be a separate issue. Due to complexity of identifying navigations after applying GroupBy, that is not implemented yet.
    Work-around

                return AssertQuery<Customer>(
                                isAsync,
                                cs =>
                                    cs.SelectMany(c => c.Orders, (c, o) => new {c.City, o})
                                    .GroupBy(c => c.City)
                                        .Select(
                                            g => new
                                            {
                                                g.Key,
                                                c = g.Count()
                                            }),
                                e => e.Key);
  9. modified the milestones: , 3.0.0 on Nov 11, 2019
  10. 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

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions