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 all commits
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
26 changes: 17 additions & 9 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 @@ -85,38 +88,43 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto
{
if (_containsMethodInfo.Equals(method))
{
return TranslateSystemFunction("CONTAINS", instance, arguments[0], typeof(bool));
return TranslateSystemFunction("CONTAINS", typeof(bool), instance, arguments[0]);
}

if (_startsWithMethodInfo.Equals(method))
{
return TranslateSystemFunction("STARTSWITH", instance, arguments[0], typeof(bool));
return TranslateSystemFunction("STARTSWITH", typeof(bool), instance, arguments[0]);
}

if (_endsWithMethodInfo.Equals(method))
{
return TranslateSystemFunction("ENDSWITH", instance, arguments[0], typeof(bool));
return TranslateSystemFunction("ENDSWITH", typeof(bool), instance, arguments[0]);
}

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

if (_firstOrDefaultMethodInfoWithoutArgs.Equals(method))
{
return TranslateSystemFunction("LEFT", arguments[0], _sqlExpressionFactory.Constant(1), typeof(char));
return TranslateSystemFunction("LEFT", typeof(char), arguments[0], _sqlExpressionFactory.Constant(1));
}

if (_lastOrDefaultMethodInfoWithoutArgs.Equals(method))
{
return TranslateSystemFunction("RIGHT", arguments[0], _sqlExpressionFactory.Constant(1), typeof(char));
return TranslateSystemFunction("RIGHT", typeof(char), arguments[0], _sqlExpressionFactory.Constant(1));
}

if(_stringConcatWithTwoArguments.Equals(method))
if (_stringConcatWithTwoArguments.Equals(method))
{
return _sqlExpressionFactory.Add(
arguments[0],
arguments[1]);
}

if(_stringConcatWithThreeArguments.Equals(method))
if (_stringConcatWithThreeArguments.Equals(method))
{
return _sqlExpressionFactory.Add(
arguments[0],
Expand All @@ -139,7 +147,7 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto
return null;
}

private SqlExpression TranslateSystemFunction(string function, SqlExpression instance, SqlExpression pattern, Type returnType)
=> _sqlExpressionFactory.Function(function, new[] { instance, pattern }, returnType);
private SqlExpression TranslateSystemFunction(string function, Type returnType, params SqlExpression[] arguments)
=> _sqlExpressionFactory.Function(function, arguments, returnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -617,15 +617,14 @@ FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_string_to_lower(bool async)
{
await base.Where_string_to_lower(async);

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

[ConditionalTheory(Skip = "Issue #17246")]
Expand Down