Skip to content

Commit

Permalink
Adding translation for string.Length on Cosmos
Browse files Browse the repository at this point in the history
  • Loading branch information
maumar committed Aug 2, 2021
1 parent 15167f6 commit 7b4fe04
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
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

0 comments on commit 7b4fe04

Please sign in to comment.