-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SqlServer: Generate False predicate when skip/take both are 0 (#23192)
Resolves #22525
- Loading branch information
Showing
8 changed files
with
255 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/EFCore.SqlServer/Query/Internal/SkipTakeCollapsingExpressionVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class SkipTakeCollapsingExpressionVisitor : ExpressionVisitor | ||
{ | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
private IReadOnlyDictionary<string, object> _parameterValues; | ||
private bool _canCache; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public SkipTakeCollapsingExpressionVisitor([NotNull] ISqlExpressionFactory sqlExpressionFactory) | ||
{ | ||
Check.NotNull(sqlExpressionFactory, nameof(sqlExpressionFactory)); | ||
|
||
_sqlExpressionFactory = sqlExpressionFactory; | ||
_parameterValues = null!; | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public virtual SelectExpression Process( | ||
[NotNull] SelectExpression selectExpression, | ||
[NotNull] IReadOnlyDictionary<string, object> parametersValues, | ||
out bool canCache) | ||
{ | ||
Check.NotNull(selectExpression, nameof(selectExpression)); | ||
Check.NotNull(parametersValues, nameof(parametersValues)); | ||
|
||
_parameterValues = parametersValues; | ||
_canCache = true; | ||
|
||
var result = (SelectExpression)Visit(selectExpression); | ||
|
||
canCache = _canCache; | ||
|
||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
protected override Expression VisitExtension(Expression extensionExpression) | ||
{ | ||
if (extensionExpression is SelectExpression selectExpression) | ||
{ | ||
if (IsZero(selectExpression.Limit) | ||
&& IsZero(selectExpression.Offset)) | ||
{ | ||
return selectExpression.Update( | ||
selectExpression.Projection, | ||
selectExpression.Tables, | ||
selectExpression.GroupBy.Count > 0 ? selectExpression.Predicate : _sqlExpressionFactory.Constant(false), | ||
selectExpression.GroupBy, | ||
selectExpression.GroupBy.Count > 0 ? _sqlExpressionFactory.Constant(false) : null, | ||
new List<OrderingExpression>(0), | ||
limit: null, | ||
offset: null); | ||
} | ||
|
||
bool IsZero(SqlExpression? sqlExpression) | ||
{ | ||
switch (sqlExpression) | ||
{ | ||
case SqlConstantExpression constant: | ||
return ((int)constant.Value) == 0; | ||
case SqlParameterExpression parameter: | ||
_canCache = false; | ||
return ((int)_parameterValues[parameter.Name]) == 0; | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return base.VisitExtension(extensionExpression); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters