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: Query containing subquery with annonymous type fails to translate to server #7844

Closed
smitpatel opened this issue Mar 10, 2017 · 3 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

@smitpatel
Copy link
Contributor

With #7843 We introduced optimization to lift subqueries when outer QM has selector only and it can be pushed down into subquery model.
Query

AssertQuery<Employee, Order>((es, os) =>
    from e in es.Take(9).Select(e => new { e })
    from o in os.Take(1000).Select(o => new { o })
    where e.e.EmployeeID == o.o.EmployeeID
    select new { e, o });

generates query model from parser like this

  Compiling query model: 
  'from Employee e in 
      (from Employee <generated>_1 in DbSet<Employee>
      select <generated>_1)
      .Take(__p_0)
  from <>f__AnonymousType15<Order> o in 
      from Order o in 
          (from Order <generated>_1 in DbSet<Order>
          select <generated>_1)
          .Take(1000)
      select new <>f__AnonymousType15<Order>(o)
  where (Nullable<int>) e.EmployeeID == o.o.EmployeeID
  select new <>f__AnonymousType14<<>f__AnonymousType13<Employee>, <>f__AnonymousType15<Order>>(
      new <>f__AnonymousType13<Employee>(e), 
      o
  )'

which is being optimized into this now

  'from Employee e in 
      (from Employee <generated>_1 in DbSet<Employee>
      select <generated>_1)
      .Take(__p_0)
  from <>f__AnonymousType15<Order> o in 
      (from Order o in DbSet<Order>
      select new <>f__AnonymousType15<Order>(o))
      .Take(1000)
  where (Nullable<int>) e.EmployeeID == o.o.EmployeeID
  select new <>f__AnonymousType14<<>f__AnonymousType13<Employee>, <>f__AnonymousType15<Order>>(
      new <>f__AnonymousType13<Employee>(e), 
      o
  )'

Since the AdditionalFromClause has anonymous type projection, we fail to translate the whole query as cross join.

The root cause here seems to be QueryParser. Even though MainFromClause & AdditionalFromClause both have same structure, the where predicate for MainFromClause is optimized from new { e }.e.EmployeeID to e.EmployeeId. While same optimization does not happen for AdditionalFromClause.

@smitpatel
Copy link
Contributor Author

@anpete - Should we report this to Re-Linq or try to work-around on our side?

@tuespetre
Copy link
Contributor

tuespetre commented Mar 10, 2017

@smitpatel @anpete this is not a problem with Re-Linq. It's just that once you moved the selector down into the subquery, the subquery now gets flagged for RequiresClientProjection unnecessarily. I just locally cherry-picked #7843 on top of my other PR #7767 and it is resolved.

See: tuespetre@4933293

@smitpatel
Copy link
Contributor Author

@tuespetre - Good to know that #7767 fixes this. I did not mean that this is issue in Re-Linq. Though certain an optimization which Re-Linq can expand. 3 components - ReLinq QueryParser, EF QM optimizations & EF Query translations are involved in this translation. So we can apply fix at any place. It is the same difference that instead of lifting subqueries in translation we lifted in QueryOptimizer. If Re-Linq generates more optimized QM then its good for us. If not then we improve translation logic to deal with such cases. (Exactly what you are doing in #7767. Thanks.)

Just wanted to make sure that we have issue tracking the test that we should do something to enable translation to Server.

tuespetre added a commit to tuespetre/EntityFramework that referenced this issue Mar 11, 2017
More accurate marking of RequiresClientProjection (less false positives), which results in less duplicate visitation of child query models in many cases

Fixes dotnet#7844
@ajcvickers ajcvickers added this to the 2.0.0 milestone Mar 13, 2017
tuespetre added a commit to tuespetre/EntityFramework that referenced this issue Mar 15, 2017
More accurate marking of RequiresClientProjection (less false positives), which results in less duplicate visitation of child query models in many cases

Fixes dotnet#7844
tuespetre added a commit to tuespetre/EntityFramework that referenced this issue Mar 15, 2017
More accurate marking of RequiresClientProjection (less false positives), which results in less duplicate visitation of child query models in many cases

Fixes dotnet#7844
tuespetre added a commit to tuespetre/EntityFramework that referenced this issue Mar 16, 2017
More accurate marking of RequiresClientProjection (less false positives), which results in less duplicate visitation of child query models in many cases

Fixes dotnet#7844
@smitpatel smitpatel reopened this Mar 29, 2017
@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 Apr 5, 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
@divega divega changed the title Query: Test Where_subquery_anon fails to translate to server Query: Query containing subquery with annonymous type fails to translate to server 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

4 participants