-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Handle IN optimizer transformation of ordering to constant #16380
Conversation
src/EFCore.Relational/Query/Pipeline/InExpressionValuesExpandingExpressionVisitor.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to react in cosmos tests since you are modifying SimpleQueryTestBase.
Our InExpressionValuesExpanding visitor can turn an an IN expression to a constant (i.e. when the values list is empty), which is unsupported in orderings in SqlServer. Detect this and turn these expressions to a simple constant expression detectable by the SQL generator, which will generate (SELECT 1). Fixes #15713
protected override Expression VisitExtension(Expression extensionExpression) | ||
{ | ||
var visited = base.VisitExtension(extensionExpression); | ||
if (visited is OrderingExpression orderingExpression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this may not be cause of perf regression reported #16405
Let's improve it somewhat since this is runtime and not compile time.
- Only visit orderingExpression if inner expression was actually changed.
- Initialize the visitor only once in the ctor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smitpatel should we track this in an issue so that we don't miss it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am reverting this commit. Hit an issue in group by translation. This logic is actually incorrect.
ORDER BY COUNT(*)
is valid and does not reference any column expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Submitted #16422.
Thanks for the perf suggestions, have made the changes. Note that constant searching now only occurs when an expression was changed to constant (other changes do not trigger this).
For ORDER BY COUNT(*), have modified the visitor to also recognize ParameterExpression (and added a test). I also added SqlFragmentExpression which could contain a column - or not - but it seems better to assume it's non-constant (better to get a "constant ordering" error from the DB than wrong results).
I think that should cover it - do you see any other case where this would wrongly identify something as constant?
Our InExpressionValuesExpanding visitor can turn an an IN expression to a constant (i.e. when the values list is empty), which is unsupported in orderings in SqlServer. Detect this and turn these expressions to a simple constant expression detectable by the SQL generator, which will generate (SELECT 1).
Fixes #15713
Some notes: