Repository navigation
Closed as not planned
Closed as not planned
Copy link
Description
There are several query-related efforts being worked on or considered for the 3.0 release. This issue is a single place to track and discuss these efforts at a high level as we make progress and decide on direction.
Initial areas to consider:
- Increase test coverage
- Fix query bugs
- Decide on architecture changes, if any. For example internalize or remove Relinq.
- Consolidate and document guiding principles for different types of translations and evaluations. For example, Should we be treating objects created by the user in a query differently from the ones we materialize? #12668, Query: revisit design for function non-determinism and client evaluatability #12672, Query: Consider adding method to force client evaluation of an expression #12736, Query: Interpret path passed to Include() and allow partial matches #12737, Warning when query materializes an entity outside the top projection #12667, .Any() on collection navigations after projection can cause client-side evaluation #12728, Query: Consider adding method to force client evaluation of an expression #12736, Make IQueryOptimizer/QueryOptimizer non-internal #12756, Query: Enable translations that generate different SQL depending on a value to be cached #12764, Query: Include for both navigations of One-To-Many relationship should use split query #12775, Change split query implementation to fetch dependents by keys, without reevaluating principal query #12776, Query: compilation error (dangling qsre) for query with SelectMany, order by and include collection #12794, Support (custom) member/method translations on value-converted types #10434, Query: consider expanding null safety beyond nav rewrite #12284 (null safety), ParameterExtractingExpressionVisitor::GetValue #13899, Query: projecting naked correlated collection when tracking should return collection object of it's parent entity #14046, Query: Top projection should fully materialize and track entities before they are passed to client methods #12761 (comment) (top projection rewrite), Query: correlated collection optimization doesn't get applied for subqueries with AsQueryable #12195
- Decide on keeping full client-eval fallback, going to explicit client-eval only, or moving to a mixture. For example, see Using Func<> for where predicate does not work #12765, Provide a way to control client evaluation behavior on a per query basis #12844, InMemory provider queries on tracked entities (unlike SQLServer provider) #12945, Query: Allow non static methods in top level projection #13048, Support client evaluation when store evaluation is not appropriate #10265
Reacted by Austin Drenski, Shay Rojansky, Derek Gray, AlekseyMartynov, Galdin Raphael, Pavel Voronin, Romain Verdier, Mendel Monteiro-Beckerman, TsengSR, Yann Zahringer Ferrando and 5 more
Activity
divega commentedon Aug 3, 2018
Including here the seminal client-side evaluation proposal Colin Meek wrote:
Implicit boundaries in LINQ to Entities (client-side evaluation)
Overview
LINQ forces us to blur the boundary between the server and the client. For a provider like LINQ to Entities, this means that a query supplied to the stack is always partly evaluated in the client application and partly in the database server. For instance, the query
includes a selection evaluated by the server (
p.ProductId == @productId), but also client expressions, e.g. the binding of the free variableproductIdand materialization of XML nodes in the projection. It also includes a call to a client-side method on a predicate over a server correlated expression, something that is not supported by either LINQ to Entities or in most LINQ to * implementations.While we have found it convenient to talk about LINQ to Entities as a strict implementation – all server or nothing – and other implementations such as LINQ to SQL as hybrid implementations – splitting the query into client and server expressions – these implementations really exist along a continuum, and it makes sense to examine this continuum in more detail to clarify the current behavior and how we could improve EF.
It is convenient to discuss this continuum with respect to the following expression scopes:
productIdis compiled into something likeExpression.Field(Expression.Constant(CS$<>8__locals6), fieldof(<>c__DisplayClass5.productId)), which does not depend on the current scope of the query (i.e. it is not correlated to the contents of any table in the database). The process of identifying and evaluating these independent sub-expressions has become known as expression funcletization at Microsoft.IQueryableroots, e.g.dbContext.DbSet<Product>()in the above example.new XElement…DetermineProductPriority…, but depend on intermediate results from the server, e.g.DetermineProductPriority(<u>p</u>).Note that these categories are somewhat arbitrary. A client source is a kind of independent sub-expression and a dependent sub-expression is in some ways just a generalization of client projections. The categories are still intuitively useful however: independent sub-expressions often map to parameters while client sources mostly map to scans, and; client projections are frequently benign while dependent sub-expressions are often cause for concern (consider high selectivity filters).
Implicit boundaries are dangerous but also an essential feature of LINQ. Finding the appropriate balance is important. From feedback we have received over the years, we know that users expect magic and may be disappointed if we either throw because we’re overly strict or we end up streaming 1,000,000 rows from the database into the client to find the 10 rows matching a client predicate because we were too loose.
Independent sub-expressions and client sources
Options:
1), primitive type constructors (new DateTime(2008, 5, 28)) and even array initialization patterns (new int[] {…}).dbContext.DbSet<T>()andcontext.CreateQuery<T>(string)are not recognized.stringBuilder.ToString().Length. While thestringBuilder.ToString()part can only be evaluated on the client,.Lengthcould either be evaluated on the client alongside the rest of the expression or translated to LEN(@PARAM) in the store. LEN() in SQL Server has subtly different semantics form string.Lenght in the CLR in that it ignores trailing blanks. We have three options on what we can funcletize:a. The minimal sub-expression that cannot be evaluated on the server
b. The minimal sub-expression that cannot be evaluated on the client plus any expression that can be evaluated on either the client or the server with identical semantics
c. The maximal sub-expression that can be evaluated on the client
Client projection
Options:
select ClientMethod1(ClientMethod2(e.X, e.Y), e.Z)becomesselect new { e.X, e.Y, e.Z } into f select ClientMethod1(ClientMethod2(f.X, f.Y), f.Z).Note that method calls may introduce additional round-trips to the server. Before people start shouting about “nanny state APIs”, consider that users are not complaining that they wanted the round-trips but that we failed to crack the methods to figure out how to avoid them…
Dependent sub-expressions
What happens when a sub-expression cannot be evaluated by the server but depends on intermediate results?
Interface considerations
If we implement support for these patterns, we should also consider allowing the user to disable them. The user can exercise whatever level of control they want over the client-server partitioning of the query. In addition, we should make the partitioned plan visible to the user, either by using documented boundary expressions or through a debugger visualizer.
Implementation considerations
We can include a separate pass to identify supported and unsupported expressions in the query tree, similar to other LINQ implementations.
tuespetre commentedon Aug 20, 2018
I love the example with
new XElement(...)and immediately see the potential translation intoFOR XML PATH. 😉As for architectural changes and managing query bugs, I feel like there is a lot I could say but I don't know how effective I would be at communicating it.
ajcvickers commentedon Aug 20, 2018
@tuespetre I'm pretty sure we will want to talk to you about some of this stuff, so stay tuned. :-)
pmiddleton commentedon Aug 20, 2018
@ajcvickers - In regards to looking at ReLinq. Is #12048 the driving issue behind that, or are there other things driving it as well?
It seems like a major undertaking to remove/replace it given how the tightly the query system is architected around it with a lot of risks for breaking things.
ajcvickers commentedon Aug 20, 2018
@pmiddleton Other things too, And yes, I agree that it is risky; that is one of the considerations. Sorry for being a bit ambiguous here. Like I said, stay tuned. Don't be impatient. 😉
pmiddleton commentedon Aug 20, 2018
@ajcvickers - I have the open PR for TVF and am currently working on a pivot feature. Both have tie-ins to ReLinq so the possible change peaked my interest as there might be some rework required on my part. :)
ajcvickers commentedon Aug 20, 2018
@pmiddleton Agreed.
tuespetre commentedon Aug 20, 2018
@ajcvickers Heyyy, I’m onto you 👀
smitpatel commentedon Sep 4, 2018
(not linking individual issues here).
Currently, there are few blockers in current query pipeline due to various reasons which requires some architecture level changes. e.g tracking entity types in all cases properly, Include/correlated subquery with client eval, Type inference in Sql Translation, Include path specification. let queries, client evals and side effects.
Some of them are also linked to things coming from relinq like let queries which generates same reference to subquery model which can be mutated. EF property over collection type props, tracking which we try to identify by tracing querysources.
#12048 Brought us need to do something about external dependencies. As "grass is always greener" on other side, we debated & researched on the line, how about removing relinq as query parser and work with expression trees directly. Certainly it doesn't solve all issues with query but it still allows us to get away from limitations we are facing from Relinq right now. As our friend @tuespetre have already written implementation of EF Core query compiler without relinq, we got inspiration that it may be possible to have a query compiler just with expression tree world. So that is what we are going to attempt for 3.0. Even though 3.0 is little bit further out, replacing whole query compiler is a huge task. The big chunk of work will come into SQL translation which is currently done from relinq query model. Trying to change the whole pipeline and also fixing issues (or make it extensible to fix in future), is optimistic goal. We are going to try for it though we may keep older pipeline for a while till we reach good progress with new compiler. This will also require changes in provider codebase.
In the same timeframe we are also going to internalize/remove IX-async and also replace IAsyncEnumerable coming from C# 8.0.
Along the way we are also going to make design decision for some of the issue linked here on the basis of value/usefulness vs complexity. Individual issues would be updated as we make decision.
Hope this clarifies people looking for update. If any confusion, feel free to ask questions.
30 remaining items
smitpatel commentedon Oct 17, 2019
@jjxtra @linkerro - Did you guys look at #17068?