-
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.
Fix to #474 - Query: Improve translation of String's StartsWith, Ends…
…With and Contains We were returning wrong results for StartsWith, EndsWith and Contains with patters containing wildcard characters (%, _) Fix is to improve the translation as follows: Contains: (CHARINDEX(pattern, string) > 0) OR pattern = '' StartsWith: (string LIKE pattern + "%" AND CHARINDEX(pattern, string) = 1) OR pattern = '' EndsWith: (RIGHT(string, LEN(pattern) = pattern) OR pattern = '' We can sometimes remove the final term (pattern = ''), i.e. when the pattern is a constant. If it's value is '', we just return true, otherwise the final term is redundant and will not be generated. We can't really do same trick for parameters without changing the way caching works (currently it only looks at the nullability of the parameter, but in this case the value itself is important)
- Loading branch information
Showing
21 changed files
with
920 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
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
43 changes: 43 additions & 0 deletions
43
...re.SqlServer/Query/ExpressionTranslators/Internal/SqlServerContainsCharIndexTranslator.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,43 @@ | ||
// 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 System.Reflection; | ||
using Microsoft.EntityFrameworkCore.Query.Expressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class SqlServerContainsCharIndexTranslator : IMethodCallTranslator | ||
{ | ||
private static readonly MethodInfo _methodInfo | ||
= typeof(string).GetRuntimeMethod(nameof(string.Contains), new[] { typeof(string) }); | ||
|
||
public virtual Expression Translate(MethodCallExpression methodCallExpression) | ||
{ | ||
if (ReferenceEquals(methodCallExpression.Method, _methodInfo)) | ||
{ | ||
var patternExpression = methodCallExpression.Arguments[0]; | ||
var patternConstantExpression = patternExpression as ConstantExpression; | ||
|
||
var charIndexExpression = Expression.GreaterThan( | ||
new SqlFunctionExpression("CHARINDEX", typeof(int), new[] { patternExpression, methodCallExpression.Object }), | ||
Expression.Constant(0)); | ||
|
||
return | ||
patternConstantExpression != null | ||
? (string)patternConstantExpression.Value == string.Empty | ||
? (Expression)Expression.Constant(true) | ||
: charIndexExpression | ||
: Expression.OrElse( | ||
charIndexExpression, | ||
Expression.Equal(patternExpression, Expression.Constant(string.Empty))); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...re.SqlServer/Query/ExpressionTranslators/Internal/SqlServerEndsWithCharIndexTranslator.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,51 @@ | ||
// 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 System.Reflection; | ||
using Microsoft.EntityFrameworkCore.Query.Expressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class SqlServerEndsWithCharIndexTranslator : IMethodCallTranslator | ||
{ | ||
private static readonly MethodInfo _methodInfo | ||
= typeof(string).GetRuntimeMethod(nameof(string.EndsWith), new[] { typeof(string) }); | ||
|
||
public virtual Expression Translate(MethodCallExpression methodCallExpression) | ||
{ | ||
if (ReferenceEquals(methodCallExpression.Method, _methodInfo)) | ||
{ | ||
var patternExpression = methodCallExpression.Arguments[0]; | ||
var patternConstantExpression = patternExpression as ConstantExpression; | ||
|
||
var endsWithExpression = Expression.Equal( | ||
new SqlFunctionExpression( | ||
"RIGHT", | ||
// ReSharper disable once PossibleNullReferenceException | ||
methodCallExpression.Object.Type, | ||
new[] | ||
{ | ||
methodCallExpression.Object, | ||
new SqlFunctionExpression("LEN", typeof(int), new[] { patternExpression }) | ||
}), | ||
patternExpression); | ||
|
||
return new NotNullableExpression( | ||
patternConstantExpression != null | ||
? (string)patternConstantExpression.Value == string.Empty | ||
? (Expression)Expression.Constant(true) | ||
: endsWithExpression | ||
: Expression.OrElse( | ||
endsWithExpression, | ||
Expression.Equal(patternExpression, Expression.Constant(string.Empty)))); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
....SqlServer/Query/ExpressionTranslators/Internal/SqlServerStartsWithCharIndexTranslator.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,50 @@ | ||
// 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 System.Reflection; | ||
using Microsoft.EntityFrameworkCore.Query.Expressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class SqlServerStartsWithCharIndexTranslator : IMethodCallTranslator | ||
{ | ||
private static readonly MethodInfo _methodInfo | ||
= typeof(string).GetRuntimeMethod(nameof(string.StartsWith), new[] { typeof(string) }); | ||
|
||
private static readonly MethodInfo _concat | ||
= typeof(string).GetRuntimeMethod(nameof(string.Concat), new[] { typeof(string), typeof(string) }); | ||
|
||
public virtual Expression Translate(MethodCallExpression methodCallExpression) | ||
{ | ||
if (ReferenceEquals(methodCallExpression.Method, _methodInfo)) | ||
{ | ||
var patternExpression = methodCallExpression.Arguments[0]; | ||
var patternConstantExpression = patternExpression as ConstantExpression; | ||
|
||
var startsWithExpression = Expression.AndAlso( | ||
new LikeExpression( | ||
// ReSharper disable once AssignNullToNotNullAttribute | ||
methodCallExpression.Object, | ||
Expression.Add(methodCallExpression.Arguments[0], Expression.Constant("%", typeof(string)), _concat)), | ||
Expression.Equal( | ||
new SqlFunctionExpression("CHARINDEX", typeof(int), new[] { patternExpression, methodCallExpression.Object }), | ||
Expression.Constant(1))); | ||
|
||
return patternConstantExpression != null | ||
? (string)patternConstantExpression.Value == string.Empty | ||
? (Expression)Expression.Constant(true) | ||
: startsWithExpression | ||
: Expression.OrElse( | ||
startsWithExpression, | ||
Expression.Equal(patternExpression, Expression.Constant(string.Empty))); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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.