Skip to content

Implement VisitChildren for InExpression and ExistsExpression - #7646

Merged
maumar merged 1 commit into
dotnet:devfrom
tuespetre:in-exists-composite-expressions
Feb 19, 2017
Merged

maumar merged 1 commit into
dotnet:devfrom
tuespetre:in-exists-composite-expressions

Conversation

@tuespetre

Copy link
Copy Markdown
Contributor
  • Introduce CompositeExpression to explicitly represent a set of various expressions

More changes being split out of #7543. The immediate benefits are that further optimizations are applied to subqueries within InExpression and ExistExpression, and instead of using various odd means of representing a small collection of Expressions (such as IReadOnlyCollection<Expression> and ConstantExpression with an Expression[] value) we have an explicit expression type to represent that concept.

In the longer term, CompositeExpression will gain a Flatten method which allows for more opportunities like complex/nested grouping keys and easier processing of complex/nested projections.

@dnfclas

dnfclas commented Feb 17, 2017

Copy link
Copy Markdown

Hi @tuespetre, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by .NET Foundation and real humans are currently evaluating your PR.

TTYL, DNFBOT;

@smitpatel smitpatel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ?: syntax.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please switch this to:

return expression != Expression
 ? new ExistsExpression(expression)
 : this;

We tend to use this pattern whenever we can.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tuespetre

Copy link
Copy Markdown
Contributor Author

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.

As I said above:

In the longer term, CompositeExpression will gain a Flatten method which allows for more opportunities like complex/nested grouping keys and easier processing of complex/nested projections.

Its advantage becomes clearer in the context of other changes I still have pending.

@smitpatel

Copy link
Copy Markdown
Contributor

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')

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smitpatel this change is actually due to the InExpression.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. Just implementing VisitChildren in ExistExpression gives above change. I tested on latest dev.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the specific test Optional_navigation_type_compensation_works_with_contains()?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tuespetre tuespetre Feb 18, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuespetre

        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;
        }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just tested that implementation and all tests seem to be passing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tuespetre
tuespetre force-pushed the in-exists-composite-expressions branch from 517dc24 to 8bf4e39 Compare February 18, 2017 14:21
@tuespetre

Copy link
Copy Markdown
Contributor Author

🆙📅

@maumar maumar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good otherwise!

@tuespetre
tuespetre force-pushed the in-exists-composite-expressions branch from 8bf4e39 to ca688c4 Compare February 19, 2017 20:07
@tuespetre

Copy link
Copy Markdown
Contributor Author

🆙📅

@maumar

maumar commented Feb 19, 2017

Copy link
Copy Markdown
Contributor

@tuespetre looks good! Can you rebase on the latest dev?

@tuespetre
tuespetre force-pushed the in-exists-composite-expressions branch from ca688c4 to f79fd5d Compare February 19, 2017 22:08
@tuespetre

Copy link
Copy Markdown
Contributor Author

👍 and 🏁

@maumar
maumar merged commit f79fd5d into dotnet:dev Feb 19, 2017
@tuespetre
tuespetre deleted the in-exists-composite-expressions branch February 19, 2017 22:19
@maumar

maumar commented Feb 19, 2017

Copy link
Copy Markdown
Contributor

Thank you for the contribution!

@tuespetre

Copy link
Copy Markdown
Contributor Author

Thank you two for the patience having to deal with me 😉

}

return valuesChanged || newOperand != Operand || newSubQuery != SubQuery
? new InExpression(newOperand, newValues.AsReadOnly(), newSubQuery)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 indeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants