Repository navigation
Implement VisitChildren for InExpression and ExistsExpression - #7646
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; |
smitpatel
left a comment
There was a problem hiding this comment.
What is justification for adding an Expression just for the Expression[]. While it may seem to simplify things in InExpression.VisitChildren, the same method can be written to take care of all the different kinds of properties it can have.
| { | ||
| var expression = visitor.Visit(Expression); | ||
|
|
||
| if (expression != Expression) |
There was a problem hiding this comment.
please switch this to:
return expression != Expression
? new ExistsExpression(expression)
: this;
We tend to use this pattern whenever we can.
There was a problem hiding this comment.
Oh, shoot, I missed this one
| LEFT JOIN ( | ||
| SELECT [t.Gear].[Nickname], [t.Gear].[SquadId], [t.Gear].[AssignedCityName], [t.Gear].[CityOrBirthName], [t.Gear].[Discriminator], [t.Gear].[FullName], [t.Gear].[HasSoulPatch], [t.Gear].[LeaderNickname], [t.Gear].[LeaderSquadId], [t.Gear].[Rank] | ||
| FROM [Gear] AS [t.Gear] | ||
| WHERE ([t.Gear].[Discriminator] = N'Officer') OR ([t.Gear].[Discriminator] = N'Gear') |
There was a problem hiding this comment.
Given that optimizations in CompositePredicateVisitorExpression are running on all where predicates appearing anywhere in the select expression, these changes should be captured by it. At present it is not happening due to incorrect VisitChildren method in ExistExpression (notice that all these changes are happening when there is Exists in query). So these tests would be improved with just ExistExpression changes. What is the benefit of all other source changes here? Nothing is visible in tests.
There was a problem hiding this comment.
I suppose you are correct here. I was going off of another comment from @maumar about splitting out the VisitChildren implementation of InExpression into its own PR.
As I said above:
Its advantage becomes clearer in the context of other changes I still have pending. |
|
As happened earlier, add the custom Expression when you actually need it. 😄 |
| SELECT [g].[SquadId] | ||
| FROM [Gear] AS [g] | ||
| WHERE ([g].[Discriminator] = N'Officer') OR ([g].[Discriminator] = N'Gear') | ||
| WHERE [g].[Discriminator] IN (N'Officer', N'Gear') |
There was a problem hiding this comment.
@smitpatel this change is actually due to the InExpression.
There was a problem hiding this comment.
Not really. Just implementing VisitChildren in ExistExpression gives above change. I tested on latest dev.
There was a problem hiding this comment.
For the specific test Optional_navigation_type_compensation_works_with_contains()?
There was a problem hiding this comment.
Irrespective, I think there is value in implementing VisitChildren on InExpression. However, I do agree with @smitpatel that there is not enough value to justify creating CompositeExpression node in this PR.
Supposedly, the other PR in which the new node provides real value doesn't get accepted (due to some fundamental issue, or we decide it's too risky or there is better solution, etc) we would then end up with extra Expression that doesn't really do us much good.
There was a problem hiding this comment.
Sorry, my mistake. I did not see full SQL for this test. Though this should be fixed easily by writing correct VisitChildren for InExpression without bringing in any additional expression.
There was a problem hiding this comment.
Question: any thoughts on how to handle the ListInitExpression then? The reason I opted to use CompositeExpression here is because then instead of having three different properties hanging off of the InExpression (for ListInitExpression, IReadOnlyCollection<Expression>, and SelectExpression), there could be a single Expression Values { get; } property that could be pattern matched. ListInitExpression has to be handled specially in order to implement VisitChildren and it doesn't work when shoved into a IReadOnlyCollection<Expression>.
I do care very much about aesthetics but I guess this is one of those cases where I want to be clear that it wasn't just an aesthetic decision and I did have reasons for doing it. I'm sorry I am not very good at offering up explanations.
There was a problem hiding this comment.
private InExpression(
Expression operand,
IReadOnlyList<Expression> values,
SelectExpression subQuery)
{
Operand = operand;
Values = values;
SubQuery = subQuery;
} protected override Expression VisitChildren(ExpressionVisitor visitor)
{
var newOperand = visitor.Visit(Operand);
var newSubQuery = (SelectExpression)visitor.Visit(SubQuery);
var valuesChanged = false;
var newValues = new List<Expression>();
if (Values != null)
{
foreach (var value in Values)
{
var newValue = visitor.Visit(value);
if (newValue is BlockExpression && value is ListInitExpression)
{
newValues.Add(value);
}
else
{
newValues.Add(newValue);
valuesChanged |= newValue != value;
}
}
}
return valuesChanged || newOperand != Operand || newSubQuery != SubQuery
? new InExpression(newOperand, newValues.AsReadOnly(), newSubQuery)
: this;
}There was a problem hiding this comment.
just tested that implementation and all tests seem to be passing
There was a problem hiding this comment.
Alright, I will tweak that when I have time this weekend.
Having the ListInitExpression be nested inside of a list of expressions doesn't feel weird to you though?
There was a problem hiding this comment.
It does feel a bit hacky, but the code churn is not worth it. We might as well reconsider this once/if CompositeExpression is justified by a future change.
517dc24 to
8bf4e39
Compare
|
🆙📅 |
8bf4e39 to
ca688c4
Compare
|
🆙📅 |
|
@tuespetre looks good! Can you rebase on the latest dev? |
ca688c4 to
f79fd5d
Compare
|
👍 and 🏁 |
|
Thank you for the contribution! |
|
Thank you two for the patience having to deal with me 😉 |
| } | ||
|
|
||
| return valuesChanged || newOperand != Operand || newSubQuery != SubQuery | ||
| ? new InExpression(newOperand, newValues.AsReadOnly(), newSubQuery) |
There was a problem hiding this comment.
This line is incorrect. If we are having InExpression with subquery (where Values is null), and if the operand or subquery changes after visiting then we will create a new InExpression with Empty List as Values instead of null. That makes InExpression to produce 0=1 hence incorrect SQL.
More changes being split out of #7543. The immediate benefits are that further optimizations are applied to subqueries within
InExpressionandExistExpression, and instead of using various odd means of representing a small collection ofExpressions (such asIReadOnlyCollection<Expression>andConstantExpressionwith anExpression[]value) we have an explicit expression type to represent that concept.In the longer term,
CompositeExpressionwill gain aFlattenmethod which allows for more opportunities like complex/nested grouping keys and easier processing of complex/nested projections.