Repository navigation
Conversation
|
Hi @tuespetre, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! The agreement was validated by .NET Foundation and real humans are currently evaluating your PR. TTYL, DNFBOT; |
| join l2_inner in context.LevelTwo.ToList() on l1_inner.Id equals l2_inner.Level1_Optional_Id into grouping | ||
| from l2_inner in grouping.DefaultIfEmpty() | ||
| select l1_inner).Distinct().Count() > 7 | ||
| select ClientStringMethod(l1_inner.Name)).Count() > 7 |
| } | ||
|
|
||
| do | ||
| { |
There was a problem hiding this comment.
thanks, will change it from do...while to while
There was a problem hiding this comment.
actually changed the preceding condition to return early if the ancestor is null instead
| || unwrappedExpression.IsAliasWithColumnExpression(); | ||
| } | ||
|
|
||
| public static Expression MaybeAnonymousSubquery([CanBeNull] this Expression expression) |
There was a problem hiding this comment.
Can you elaborate what this would generate and why it is needed?
There was a problem hiding this comment.
SqlTranslatingExpressionVisitor.VisitSubQuery could return a SelectExpression, but it was always setting the Alias to string.Empty, which can cause invalid SQL in some cases now that more queries can be inlined. The solution was to only make the resulting SelectExpression into an anonymous subquery when appropriate.
There was a problem hiding this comment.
Ah, I see. We need to investigate why it is needed. Ideally, we start with top level select expression without setting any alias. We need alias only when we add it as subquery in another select expression. While flattening can cause one of more select expression to be joined, there is no code path which would cause select expression to split, hence we should not have to set the alias to string.Empty ever. If we are doing so then we are assigning alias incorrectly in first place which should be fixed.
There was a problem hiding this comment.
Well the string.Empty makes it a parenthesized, anonymous subquery. It's just that with new optimizations, it should only be applied on the right occasion instead of unconditionally like it was before.
where (select count(*) from ....) > 7
and so forth. Sorry for the formatting, on a phone right now
There was a problem hiding this comment.
Good point. Are all such calls go through single path where we can put it instead of having a function to do it?
There was a problem hiding this comment.
They did go through a single path but now not everything coming out of that single path should have it applied
There was a problem hiding this comment.
I looked into usages of this function. They are being used at various expressions without any corresponding logic around it. We shouldn't have it this way.
Encapsulate this code into the particular code path with condition where this is required. Subquery with empty string as alias is pitfall for many invalid SQL. We should identify the pattern where this is required and apply for those cases only. Ideally this function should be removed.
| bool bindParentQueries = false, | ||
| bool inProjection = false); | ||
| bool inProjection = false, | ||
| bool mutateProjections = true); |
There was a problem hiding this comment.
Purpose of mutateProjections?
There was a problem hiding this comment.
There are places where the visitor should not be adding to the projections, like in the projection visitor, some result operator handlers... this prevents every place from having to use a 'projection count map' or whatever
There was a problem hiding this comment.
Can we name it something different? We are trying to make SelectExpression as less Mutable as possible. 😄
| LEFT JOIN [Level2] AS [l1.OneToOne_Required_FK] ON [l10].[Id] = [l1.OneToOne_Required_FK].[Level1_Required_Id] | ||
| WHERE ([l10].[Name] <> N'L1 01') OR [l10].[Name] IS NULL | ||
| ) AS [t]", | ||
| @"SELECT [l10].[Id], [l10].[Date], [l10].[Name], [l10].[OneToMany_Optional_Self_InverseId], [l10].[OneToMany_Required_Self_InverseId], [l10].[OneToOne_Optional_SelfId], [l1.OneToOne_Required_FK].[Id], [l1.OneToOne_Required_FK].[Date], [l1.OneToOne_Required_FK].[Level1_Optional_Id], [l1.OneToOne_Required_FK].[Level1_Required_Id], [l1.OneToOne_Required_FK].[Name], [l1.OneToOne_Required_FK].[OneToMany_Optional_InverseId], [l1.OneToOne_Required_FK].[OneToMany_Optional_Self_InverseId], [l1.OneToOne_Required_FK].[OneToMany_Required_InverseId], [l1.OneToOne_Required_FK].[OneToMany_Required_Self_InverseId], [l1.OneToOne_Required_FK].[OneToOne_Optional_PK_InverseId], [l1.OneToOne_Required_FK].[OneToOne_Optional_SelfId] |
There was a problem hiding this comment.
I think I have a relatively simple fix in progress that makes this better, so that we don't regress here
There was a problem hiding this comment.
Is this a regression though? The resulting SQL is smaller
There was a problem hiding this comment.
@maumar - Subquery is getting lifted. Better SQL.
There was a problem hiding this comment.
Oh it looks like the selected columns are different. (More being selected.)
29c71b0 to
1876724
Compare
|
🆙📅
Inventory of regressed tests (all from ComplexNavigationsQuerySqlServerTest): On these, the subquery is lifted, more columns selected than before -- not sure why, but the main from clauses do show up as 'requires materialization' so I'm sure that has something to do with it:
On this one, this is related to a workaround for group join query sources being forced to materialize, which I had to implement to get a number of other tests passing:
|
| return handledPropertyExpression; | ||
| } | ||
|
|
||
| var handledMemberExpression |
| var handledPropertyExpression | ||
| = TryHandlePropertyExpression( | ||
| node, | ||
| node.Arguments.FirstOrDefault().RemoveConvert(), |
There was a problem hiding this comment.
Possible NRE.
Also why we need to use FirstOrDefault instead of direct indexes? Property method always have 2 args.
There was a problem hiding this comment.
We don't need to, that should be an indexer ✔️
| { | ||
| private readonly IRequiresMaterializationExpressionVisitorFactory _requiresMaterializationExpressionVisitorFactory; | ||
| private readonly IEntityQueryModelVisitorFactory _entityQueryModelVisitorFactory; | ||
| private readonly IDictionary<QueryModel, EntityQueryModelVisitor> _entityQueryModelVisitors |
There was a problem hiding this comment.
This change is good in a way that we have reference to QMV which visited this QM already. Hence avoiding revising same QM again. @anpete - Any side effects of this? (like visiting a QM adding something to SelectExpression of some other QM)
There was a problem hiding this comment.
@tuespetre - This change would improve perf for query compiler though there is a slight chance of side-effects of caching querymodelvisitors if there are any changed flags in parent qmv in between multiple visits of same QM. The only way to verify is to just make caching changes and see if no tests change. Can you submit a separate PR for that?
There was a problem hiding this comment.
I have approached it that way before (it was one of the first split-out PRs I tried to do) but it does not work (a lot of stuff breaks without other changes made in this PR). 🐔 ➕ 🥚; also with these changes there is no multiple visiting of the same query model.
There was a problem hiding this comment.
If caching QMVs is breaking tests that means it has unintentional side-effects and we cannot cache it. While binding/lifting, QMV peeks into parent QMV and even though same query model is being visited it can produce different results. Mostly, caching is not usable here.
There was a problem hiding this comment.
even though same query model is being visited it can produce different results
That's my point, the same query model will not be visited more than once
There was a problem hiding this comment.
If caching QMVs is breaking tests that means it has unintentional side-effects
The problem I found was not with the caching itself, but that introducing caching actually prevents the query models from being visited more than once, and once you do that, you have to use/account for new and different flags on the parent QMV, such as CanBindOuterProperties, RequiresOuterParameterBinding, etc. in order for that one-and-only visit of the query model to produce the desired result
There was a problem hiding this comment.
I will try removing the caching from this PR and see if it breaks anything. If it doesn't break, I will push up a commit that doesn't have the caching. Does that sound good to you
There was a problem hiding this comment.
I tested out caching part and it breaks many tests. In current pipeline, visiting a QM not only dependent on outer qmv flags but also mutates the QM itself. When we fail to translate the QM, we have to revert the QM. We have methods like PopulateQueryModelMapping & RecreateQueryModelFromMapping for that. That is enough evidence to say that current pipeline generate different results while visiting same query model again.
Before we introduce caching on QMVs, we need to make sure that pipeline can generate the required translation of QM which can be used under any circumstances later.
Remove the caching part from this PR. We can add it when we reach above stage.
There was a problem hiding this comment.
Ohhh you gave me an idea for how to deal with all the unnecessary uniquification without the caching bits (for another PR, another time) and also I think I know how to get around the removal of the caching for this PR.
|
|
||
| var newNewExpression = base.VisitNew(expression); | ||
| if (QueryModelVisitor.QueryCompilationContext.QuerySourceRequiresMaterialization(_querySource)) | ||
| { |
There was a problem hiding this comment.
@smitpatel, regarding MaybeAnonymousSubquery():
I added this condition as a workaround for not being able to always inline a subquery into a projection (due to an EntityShaper being present, or otherwise.) It looks like maybe this takes care of the reason I had put in MaybeAnonymousSubquery so I can remove it after all. 🎉
1876724 to
70c70b9
Compare
|
🆙📅 Was able to remove |
70c70b9 to
566a485
Compare
This commit adds the
OuterPropertyExpression, which is a dedicated expression to represent expressions bound to parent query models. This expression allows us to defer the decision of whether to bind directly to the parent query (i.e. anAliasExpression) or to bind via an outer parameter. There are several improved generated SQL queries in this PR as a result.To facilitate this change, other changes take place:
FROM Orders AS o4instead ofFROM Orders AS o)_bindParentQueriesfield/parameter toRelationalQueryModelVisitorandSqlTranslatingExpressionVisitoris now gone, instead being driven by the context of the query model visitor and its ancestors viaCanBindOuterProperties,CanBindOuterParameters, andRequiresOuterParameterInjection_targetSelectExpressionfield/parameter toSqlTranslatingExpressionVisitoris now gone, being replaced by a_mutateProjectionsfield/parameter. This helps to make it clearer what is being done every time and place the visitor is used (also, there were cases where the_targetSelectExpressioncomparison bit would hinder things from binding correctly.)This PR is split from (and integral to) #7650 and #7543, and this PR includes/depends on the changes that make up #7619.