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 TrimStart/TrimEnd/Trim #24247

Merged
merged 1 commit into from
Feb 25, 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
44 changes: 43 additions & 1 deletion src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ private static readonly MethodInfo _toLowerMethodInfo
private static readonly MethodInfo _toUpperMethodInfo
= typeof(string).GetRequiredRuntimeMethod(nameof(string.ToUpper), Array.Empty<Type>());

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

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

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

private static readonly MethodInfo _trimStartMethodInfoWithCharArrayArg
= typeof(string).GetRequiredRuntimeMethod(nameof(string.TrimStart), new[] { typeof(char[]) });

private static readonly MethodInfo _trimEndMethodInfoWithCharArrayArg
= typeof(string).GetRequiredRuntimeMethod(nameof(string.TrimEnd), new[] { typeof(char[]) });

private static readonly MethodInfo _trimMethodInfoWithCharArrayArg
= typeof(string).GetRequiredRuntimeMethod(nameof(string.Trim), new[] { typeof(char[]) });

private static readonly MethodInfo _firstOrDefaultMethodInfoWithoutArgs
= typeof(Enumerable).GetRuntimeMethods().Single(
m => m.Name == nameof(Enumerable.FirstOrDefault)
Expand Down Expand Up @@ -108,11 +126,35 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto
{
return TranslateSystemFunction("LOWER", method.ReturnType, instance);
}

if (_toUpperMethodInfo.Equals(method))
{
return TranslateSystemFunction("UPPER", method.ReturnType, instance);
}

if (_trimStartMethodInfoWithoutArgs?.Equals(method) == true
|| (_trimStartMethodInfoWithCharArrayArg.Equals(method)
// Cosmos DB LTRIM does not take arguments
&& ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0))
{
return TranslateSystemFunction("LTRIM", method.ReturnType, instance);
}

if (_trimEndMethodInfoWithoutArgs?.Equals(method) == true
|| (_trimEndMethodInfoWithCharArrayArg.Equals(method)
// Cosmos DB RTRIM does not take arguments
&& ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0))
{
return TranslateSystemFunction("RTRIM", method.ReturnType, instance);
}

if (_trimMethodInfoWithoutArgs?.Equals(method) == true
|| (_trimMethodInfoWithCharArrayArg.Equals(method)
// Cosmos DB TRIM does not take arguments
&& ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0))
{
return TranslateSystemFunction("TRIM", method.ReturnType, instance);
}
}

if (_firstOrDefaultMethodInfoWithoutArgs.Equals(method))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,15 +843,14 @@ FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
WHERE ((c[""Discriminator""] = ""Customer"") AND (LTRIM(c[""ContactTitle""]) = ""Owner""))");
}

[ConditionalTheory(Skip = "Issue #17246")]
Expand All @@ -876,15 +875,14 @@ FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
WHERE ((c[""Discriminator""] = ""Customer"") AND (RTRIM(c[""ContactTitle""]) = ""Owner""))");
}

[ConditionalTheory(Skip = "Issue #17246")]
Expand All @@ -909,15 +907,14 @@ FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
WHERE ((c[""Discriminator""] = ""Customer"") AND (TRIM(c[""ContactTitle""]) = ""Owner""))");
}

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