Skip to content
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

Additional perf improvements around null semantics scenarios #18694

Merged
merged 1 commit into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -80,46 +80,49 @@ public ParameterNullabilityBasedSqlExpressionOptimizingExpressionVisitor(

protected override Expression VisitExtension(Expression extensionExpression)
{
if (extensionExpression is SelectExpression selectExpression)
// workaround for issue #18492
var newExpression = base.VisitExtension(extensionExpression);
if (newExpression is SelectExpression newSelectExpression)
{
var newSelectExpression = (SelectExpression)base.VisitExtension(extensionExpression);

// if predicate is optimized to true, we can simply remove it
var newPredicate = newSelectExpression.Predicate is SqlConstantExpression newSelectPredicateConstant
&& !(selectExpression.Predicate is SqlConstantExpression)
? (bool)newSelectPredicateConstant.Value
? null
: SqlExpressionFactory.Equal(
newSelectPredicateConstant,
SqlExpressionFactory.Constant(true, newSelectPredicateConstant.TypeMapping))
: newSelectExpression.Predicate;

var newHaving = newSelectExpression.Having is SqlConstantExpression newSelectHavingConstant
&& !(selectExpression.Having is SqlConstantExpression)
? (bool)newSelectHavingConstant.Value
? null
: SqlExpressionFactory.Equal(
newSelectHavingConstant,
SqlExpressionFactory.Constant(true, newSelectHavingConstant.TypeMapping))
: newSelectExpression.Having;

return !ReferenceEquals(newPredicate, newSelectExpression.Predicate)
|| !ReferenceEquals(newHaving, newSelectExpression.Having)
? newSelectExpression.Update(
newSelectExpression.Projection.ToList(),
newSelectExpression.Tables.ToList(),
newPredicate,
newSelectExpression.GroupBy.ToList(),
newHaving,
newSelectExpression.Orderings.ToList(),
newSelectExpression.Limit,
newSelectExpression.Offset,
newSelectExpression.IsDistinct,
newSelectExpression.Alias)
: newSelectExpression;
var changed = false;
var newPredicate = newSelectExpression.Predicate;
var newHaving = newSelectExpression.Having;
if (newSelectExpression.Predicate is SqlConstantExpression predicateConstantExpression
&& predicateConstantExpression.Value is bool predicateBoolValue
&& !predicateBoolValue)
{
changed = true;
newPredicate = SqlExpressionFactory.Equal(
predicateConstantExpression,
SqlExpressionFactory.Constant(true, predicateConstantExpression.TypeMapping));
}

if (newSelectExpression.Having is SqlConstantExpression havingConstantExpression
&& havingConstantExpression.Value is bool havingBoolValue
&& !havingBoolValue)
{
changed = true;
newHaving = SqlExpressionFactory.Equal(
havingConstantExpression,
SqlExpressionFactory.Constant(true, havingConstantExpression.TypeMapping));
}

return changed
? newSelectExpression.Update(
newSelectExpression.Projection.ToList(),
newSelectExpression.Tables.ToList(),
newPredicate,
newSelectExpression.GroupBy.ToList(),
newHaving,
newSelectExpression.Orderings.ToList(),
newSelectExpression.Limit,
newSelectExpression.Offset,
newSelectExpression.IsDistinct,
newSelectExpression.Alias)
: newSelectExpression;
}

return base.VisitExtension(extensionExpression);
return newExpression;
}

protected override Expression VisitSqlUnaryExpression(SqlUnaryExpression sqlUnaryExpression)
Expand All @@ -142,6 +145,33 @@ protected override Expression VisitSqlUnaryExpression(SqlUnaryExpression sqlUnar

return result;
}

protected override Expression VisitSqlBinaryExpression(SqlBinaryExpression sqlBinaryExpression)
{
var result = base.VisitSqlBinaryExpression(sqlBinaryExpression);
if (result is SqlBinaryExpression sqlBinaryResult)
{
var leftNullParameter = sqlBinaryResult.Left is SqlParameterExpression leftParameter
&& _parametersValues[leftParameter.Name] == null;

var rightNullParameter = sqlBinaryResult.Right is SqlParameterExpression rightParameter
&& _parametersValues[rightParameter.Name] == null;

if ((sqlBinaryResult.OperatorType == ExpressionType.Equal || sqlBinaryResult.OperatorType == ExpressionType.NotEqual)
&& (leftNullParameter || rightNullParameter))
{
return SimplifyNullComparisonExpression(
sqlBinaryResult.OperatorType,
sqlBinaryResult.Left,
sqlBinaryResult.Right,
leftNullParameter,
rightNullParameter,
sqlBinaryResult.TypeMapping);
}
}

return result;
}
}

private class InExpressionValuesExpandingExpressionVisitor : ExpressionVisitor
Expand Down
Loading