Skip to content

Commit

Permalink
Rewrite string equality with LIKE on SQL Server
Browse files Browse the repository at this point in the history
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
roji committed Feb 11, 2020
1 parent ab4c8aa commit 540b4ef
Show file tree
Hide file tree
Showing 16 changed files with 383 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected override Expression VisitCase(CaseExpression caseExpression)

// if there are no whenClauses left (e.g. their tests evaluated to false):
// - if there is Else block, return it
// - if there is no Else block, return null
// - if there is no Else block, return null
if (whenClauses.Count == 0)
{
return elseResult == null
Expand Down Expand Up @@ -392,8 +392,16 @@ protected override Expression VisitLike(LikeExpression likeExpression)
var (match, matchNullable) = VisitInternal<SqlExpression>(likeExpression.Match);
var (pattern, patternNullable) = VisitInternal<SqlExpression>(likeExpression.Pattern);
var (escapeChar, escapeCharNullable) = VisitInternal<SqlExpression>(likeExpression.EscapeChar);
_nullable = matchNullable || patternNullable || escapeCharNullable;

if (match is SqlConstantExpression matchConstant && matchConstant.Value is null
|| pattern is SqlConstantExpression patternConstant && patternConstant.Value is null
|| escapeChar is SqlConstantExpression escapeCharConstant && escapeCharConstant.Value is null)
{
_nullable = false;
return SqlExpressionFactory.Constant(false, likeExpression.TypeMapping);
}

_nullable = matchNullable || patternNullable || escapeCharNullable;
return likeExpression.Update(match, pattern, escapeChar);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static IServiceCollection AddEntityFrameworkSqlServer([NotNull] this ISer
.TryAdd<IMemberTranslatorProvider, SqlServerMemberTranslatorProvider>()
.TryAdd<IQuerySqlGeneratorFactory, SqlServerQuerySqlGeneratorFactory>()
.TryAdd<IRelationalSqlTranslatingExpressionVisitorFactory, SqlServerSqlTranslatingExpressionVisitorFactory>()
.TryAdd<IQueryTranslationPostprocessorFactory, SqlServerQueryTranslationPostprocessorFactory>()
.TryAdd<IRelationalParameterBasedQueryTranslationPostprocessorFactory, SqlServerParameterBasedQueryTranslationPostprocessorFactory>()
.TryAddProviderSpecificServices(
b => b
Expand Down
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;
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private SqlExpression TranslateStartsEndsWith(SqlExpression instance, SqlExpress
_sqlExpressionFactory.Constant(null, stringTypeMapping));
}

return constantString.Any(c => IsLikeWildChar(c))
return constantString.Any(IsLikeWildChar)
? _sqlExpressionFactory.Like(
instance,
_sqlExpressionFactory.Constant(
Expand Down
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 == '[';
}
}
28 changes: 28 additions & 0 deletions test/EFCore.Specification.Tests/Query/FunkyDataQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,34 @@ public virtual Task String_ends_with_not_equals_nullable_column(bool async)
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_equals_with_trailing_whitespace_constant(bool async)
=> AssertQuery(
async,
ss => ss.Set<FunkyCustomer>().Where(c => c.LastName == "WithoutTrailing ").Select(c => c.FirstName));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_equals_with_trailing_whitespace_column(bool async)
=> AssertQuery(
async,
ss => ss.Set<FunkyCustomer>().Where(c => c.FirstName == "WithTrailing").Select(c => c.FirstName));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_equals_with_like_wildcard(bool async)
=> AssertQuery(
async,
ss => ss.Set<FunkyCustomer>().Where(c => c.FirstName == "With%Wildcard").Select(c => c.FirstName));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_equals_with_trailing_whitespace_and_like_wildcard(bool async)
=> AssertQuery(
async,
ss => ss.Set<FunkyCustomer>().Where(c => c.FirstName == "WithTrailingAnd%Wildcard").Select(c => c.FirstName));

protected FunkyDataContext CreateContext() => Fixture.CreateContext();

protected virtual void ClearLog()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public virtual Task String_StartsWith_MethodCall(bool async)
entryCount: 12);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_StartsWith_Parameter_with_whitespace(bool async)
{
var pattern = "hello";
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.ContactName.StartsWith(pattern)));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_EndsWith_Literal(bool async)
Expand Down Expand Up @@ -117,6 +127,16 @@ public virtual Task String_EndsWith_MethodCall(bool async)
entryCount: 1);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_EndsWith_Parameter_with_whitespace(bool async)
{
var pattern = "hello";
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.ContactName.EndsWith(pattern)));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_Contains_Literal(bool async)
Expand Down Expand Up @@ -157,6 +177,16 @@ public virtual Task String_Contains_MethodCall(bool async)
entryCount: 19);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_Contains_Parameter_with_whitespace(bool async)
{
var pattern = " ";
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.ContactName.Contains(pattern)));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task String_Compare_simple_zero(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ public static IReadOnlyList<FunkyCustomer> CreateFunkyCustomers()
FirstName = "B[[a^r",
LastName = "B[[",
NullableBool = true
},
new FunkyCustomer
{
Id = 20,
FirstName = "WithTrailing ",
LastName = "WithoutTrailing"
},
new FunkyCustomer
{
Id = 21,
FirstName = "With%Wildcard"
},
new FunkyCustomer
{
Id = 22,
FirstName = "WithTrailingAnd%Wildcard "
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public override async Task Method_call_on_optional_navigation_translates_to_null
@"SELECT [l].[Id], [l].[Date], [l].[Name], [l].[OneToMany_Optional_Self_Inverse1Id], [l].[OneToMany_Required_Self_Inverse1Id], [l].[OneToOne_Optional_Self1Id]
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Optional_Id]
WHERE ([l0].[Name] = N'') OR ([l0].[Name] IS NOT NULL AND (LEFT([l0].[Name], LEN([l0].[Name])) = [l0].[Name]))");
WHERE ([l0].[Name] LIKE N'') OR ([l0].[Name] IS NOT NULL AND (LEFT([l0].[Name], LEN([l0].[Name])) = [l0].[Name]))");
}

public override async Task Optional_navigation_inside_method_call_translated_to_join_keeps_original_nullability(bool async)
Expand Down
Loading

0 comments on commit 540b4ef

Please sign in to comment.