-
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.
Rewrite string equality with LIKE on SQL Server
Since on SQL Server the equality operator ignores trailing whitespace, we can use LIKE when comparing to constants. Fixes #19402 Note: this is WIP, two tests are failing.
- Loading branch information
Showing
16 changed files
with
383 additions
and
72 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
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
29 changes: 29 additions & 0 deletions
29
src/EFCore.SqlServer/Query/Internal/SqlServerQueryTranslationPostprocessor.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,29 @@ | ||
// 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.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
public class SqlServerQueryTranslationPostprocessor : RelationalQueryTranslationPostprocessor | ||
{ | ||
public SqlServerQueryTranslationPostprocessor( | ||
[NotNull] QueryTranslationPostprocessorDependencies dependencies, | ||
[NotNull] RelationalQueryTranslationPostprocessorDependencies relationalDependencies, | ||
[NotNull] QueryCompilationContext queryCompilationContext) | ||
: base(dependencies, relationalDependencies, queryCompilationContext) | ||
{ | ||
} | ||
|
||
public override Expression Process(Expression query) | ||
{ | ||
query = new StringEqualityConvertingExpressionVisitor(RelationalDependencies.SqlExpressionFactory).Visit(query); | ||
|
||
query = base.Process(query); | ||
|
||
return query; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/EFCore.SqlServer/Query/Internal/SqlServerQueryTranslationPostprocessorFactory.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,47 @@ | ||
// 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 JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// 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. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Singleton" />. This means a single instance | ||
/// is used by many <see cref="DbContext" /> instances. The implementation must be thread-safe. | ||
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped" />. | ||
/// </para> | ||
/// </summary> | ||
public class SqlServerQueryTranslationPostprocessorFactory : IQueryTranslationPostprocessorFactory | ||
{ | ||
private readonly QueryTranslationPostprocessorDependencies _dependencies; | ||
private readonly RelationalQueryTranslationPostprocessorDependencies _relationalDependencies; | ||
|
||
public SqlServerQueryTranslationPostprocessorFactory( | ||
[NotNull] QueryTranslationPostprocessorDependencies dependencies, | ||
[NotNull] RelationalQueryTranslationPostprocessorDependencies relationalDependencies) | ||
{ | ||
_dependencies = dependencies; | ||
_relationalDependencies = relationalDependencies; | ||
} | ||
|
||
public virtual QueryTranslationPostprocessor Create(QueryCompilationContext queryCompilationContext) | ||
{ | ||
Check.NotNull(queryCompilationContext, nameof(queryCompilationContext)); | ||
|
||
return new SqlServerQueryTranslationPostprocessor( | ||
_dependencies, | ||
_relationalDependencies, | ||
queryCompilationContext); | ||
} | ||
} | ||
} |
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
73 changes: 73 additions & 0 deletions
73
src/EFCore.SqlServer/Query/Internal/StringEqualityConvertingExpressionVisitor.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,73 @@ | ||
// 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.Linq; | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
/// <summary> | ||
/// In SQL Server, string equality ignores trailing whitespace. This replaces equality with constant strings to | ||
/// LIKE, which does an exact comparison and still utilizes indexes. | ||
/// </summary> | ||
public class StringEqualityConvertingExpressionVisitor : ExpressionVisitor | ||
{ | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
public StringEqualityConvertingExpressionVisitor(ISqlExpressionFactory sqlExpressionFactory) | ||
=> _sqlExpressionFactory = sqlExpressionFactory; | ||
|
||
protected override Expression VisitExtension(Expression extensionExpression) | ||
{ | ||
Check.NotNull(extensionExpression, nameof(extensionExpression)); | ||
|
||
if (extensionExpression is ShapedQueryExpression shapedQueryExpression) | ||
{ | ||
return shapedQueryExpression.Update( | ||
Visit(shapedQueryExpression.QueryExpression), | ||
shapedQueryExpression.ShaperExpression); | ||
} | ||
|
||
if (extensionExpression is SqlBinaryExpression binaryExpression | ||
&& (binaryExpression.OperatorType == ExpressionType.Equal | ||
|| binaryExpression.OperatorType == ExpressionType.NotEqual) | ||
&& binaryExpression.Left.TypeMapping is StringTypeMapping | ||
&& binaryExpression.Right.TypeMapping is StringTypeMapping | ||
// Specifically avoid rewriting if both sides are constant (e.g. N'' = N'') - this gets handled | ||
// elsewhere and rewriting here interferes | ||
&& (!(binaryExpression.Left is SqlConstantExpression) | ||
|| !(binaryExpression.Right is SqlConstantExpression))) | ||
{ | ||
var likeExpression = | ||
TransformToLikeIfPossible(binaryExpression.Left, binaryExpression.Right) ?? | ||
TransformToLikeIfPossible(binaryExpression.Right, binaryExpression.Left); | ||
|
||
if (likeExpression != null) | ||
{ | ||
return binaryExpression.OperatorType == ExpressionType.Equal | ||
? likeExpression | ||
: (SqlExpression)_sqlExpressionFactory.Not(likeExpression); | ||
} | ||
} | ||
|
||
return base.VisitExtension(extensionExpression); | ||
} | ||
|
||
private LikeExpression TransformToLikeIfPossible(SqlExpression left, SqlExpression right) | ||
=> right is SqlConstantExpression constantExpression | ||
&& constantExpression.Value is string value | ||
&& ( | ||
value.Length == 0 | ||
|| value.All(c => !IsLikeWildChar(c)) | ||
&& char.IsWhiteSpace(value[^1])) | ||
? _sqlExpressionFactory.Like(left, right) | ||
: null; | ||
|
||
// See https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql | ||
private bool IsLikeWildChar(char c) => c == '%' || c == '_' || c == '['; | ||
} | ||
} |
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
Oops, something went wrong.