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

[SMALL] Adding translation for string.Length on Cosmos #25393

Merged
merged 1 commit into from
Aug 2, 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public CosmosMemberTranslatorProvider(
IEnumerable<IMemberTranslatorPlugin> plugins)
{
_plugins.AddRange(plugins.SelectMany(p => p.Translators));
//_translators
// .AddRange(
// new[]
// {
// new NullableMemberTranslator(sqlExpressionFactory),
// });
_translators
.AddRange(
new[]
{
new CosmosStringMemberTranslator(sqlExpressionFactory),
});
}

/// <summary>
Expand Down
59 changes: 59 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/CosmosStringMemberTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
/// <summary>
/// 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.
/// </summary>
public class CosmosStringMemberTranslator : IMemberTranslator
{
private readonly ISqlExpressionFactory _sqlExpressionFactory;

/// <summary>
/// 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.
/// </summary>
public CosmosStringMemberTranslator(ISqlExpressionFactory sqlExpressionFactory)
{
_sqlExpressionFactory = sqlExpressionFactory;
}

/// <summary>
/// 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.
/// </summary>
public virtual SqlExpression? Translate(
SqlExpression? instance,
MemberInfo member,
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(member, nameof(member));
Check.NotNull(returnType, nameof(returnType));
Check.NotNull(logger, nameof(logger));

if (member.Name == nameof(string.Length)
&& instance?.Type == typeof(string))
{
return _sqlExpressionFactory.Function(
"LENGTH",
new[] { instance },
returnType);
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public override async Task Select_anonymous_constant_in_expression(bool async)
await base.Select_anonymous_constant_in_expression(async);

AssertSql(
@"SELECT c[""CustomerID""]
@"SELECT VALUE {""CustomerID"" : c[""CustomerID""], ""Expression"" : (LENGTH(c[""CustomerID""]) + 5)}
FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}
Expand Down Expand Up @@ -535,7 +535,7 @@ public override async Task Select_non_matching_value_types_from_length_introduce
await base.Select_non_matching_value_types_from_length_introduces_explicit_cast(async);

AssertSql(
@"SELECT c[""CustomerID""]
@"SELECT LENGTH(c[""CustomerID""]) AS c
FROM root c
WHERE ((c[""Discriminator""] = ""Order"") AND (c[""CustomerID""] = ""ALFKI""))
ORDER BY c[""OrderID""]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -983,15 +983,15 @@ FROM root c
WHERE ((c[""Discriminator""] = ""Employee"") AND (c[""ReportsTo""] = null))");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
WHERE ((c[""Discriminator""] = ""Customer"") AND (LENGTH(c[""City""]) = 6))");
}

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