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

Cosmos: Add translator for ToLower method which map to LOWER built-in function #24203

Merged
merged 3 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ private static readonly MethodInfo _startsWithMethodInfo
private static readonly MethodInfo _endsWithMethodInfo
= typeof(string).GetRequiredRuntimeMethod(nameof(string.EndsWith), new[] { typeof(string) });

private static readonly MethodInfo _toLowerMethodInfo
= typeof(string).GetRequiredRuntimeMethod(nameof(string.ToLower), Array.Empty<Type>());

private static readonly MethodInfo _firstOrDefaultMethodInfoWithoutArgs
= typeof(Enumerable).GetRuntimeMethods().Single(
m => m.Name == nameof(Enumerable.FirstOrDefault)
Expand Down Expand Up @@ -97,6 +100,11 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto
{
return TranslateSystemFunction("ENDSWITH", instance, arguments[0], typeof(bool));
}

if (_toLowerMethodInfo.Equals(method))
{
return TranslateSystemFunction("LOWER", instance, method.ReturnType);
}
}

if (_firstOrDefaultMethodInfoWithoutArgs.Equals(method))
Expand Down Expand Up @@ -141,5 +149,8 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto

private SqlExpression TranslateSystemFunction(string function, SqlExpression instance, SqlExpression pattern, Type returnType)
=> _sqlExpressionFactory.Function(function, new[] { instance, pattern }, returnType);

private SqlExpression TranslateSystemFunction(string function, SqlExpression instance, Type returnType)
Marusyk marked this conversation as resolved.
Show resolved Hide resolved
=> _sqlExpressionFactory.Function(function, new[] { instance }, returnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,36 @@ FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND ((c[""ContactName""] != null) AND ((""m"" != null) AND ENDSWITH(c[""ContactName""], ""m""))))");
}

public override async Task String_ToLower_Literal(bool async)
{
await base.String_ToLower_Literal(async);

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (LOWER(c[""ContactName""]) = ""maria anders""))");
}

public override async Task String_ToLower_Column(bool async)
{
await base.String_ToLower_Column(async);

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""ContactName""] != LOWER(c[""ContactName""])))");
}

public override async Task String_ToLower_MethodCall(bool async)
{
await base.String_ToLower_MethodCall(async);

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (LOWER(c[""ContactName""]) = ""maria anders""))");
}

public override async Task String_Contains_Literal(bool async)
{
await base.String_Contains_Literal(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ public virtual Task String_EndsWith_MethodCall(bool async)
entryCount: 1);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_ToLower_Literal(bool async)
Marusyk marked this conversation as resolved.
Show resolved Hide resolved
{
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.ContactName.ToLower() == "maria anders"),
entryCount: 1);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_ToLower_Column(bool async)
Marusyk marked this conversation as resolved.
Show resolved Hide resolved
{
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.ContactName != c.ContactName.ToLower()),
entryCount: 91);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_ToLower_MethodCall(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.ContactName.ToLower() == LocalMethod3()),
entryCount: 1);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_Contains_Literal(bool async)
Expand Down Expand Up @@ -743,6 +773,9 @@ protected static string LocalMethod1()
protected static string LocalMethod2()
=> "m";

protected static string LocalMethod3()
=> "maria anders";

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_math_abs1(bool async)
Expand Down