Skip to content

Commit

Permalink
Cosmos: Add translator for Substring method which map to SUBSTRING bu…
Browse files Browse the repository at this point in the history
…ilt-in functions (#24284)

Cosmos: Add translator for Substring method which map to SUBSTRING built-in function
  • Loading branch information
Marusyk authored Mar 1, 2021
1 parent dea57ad commit 8936a65
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ private static readonly MethodInfo _trimEndMethodInfoWithCharArrayArg
private static readonly MethodInfo _trimMethodInfoWithCharArrayArg
= typeof(string).GetRequiredRuntimeMethod(nameof(string.Trim), new[] { typeof(char[]) });

private static readonly MethodInfo _substringMethodInfo
= typeof(string).GetRequiredRuntimeMethod(nameof(string.Substring), new[] { typeof(int), typeof(int) });

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

if (_substringMethodInfo.Equals(method))
{
return TranslateSystemFunction("SUBSTRING", method.ReturnType, instance, arguments[0], arguments[1]);
}
}

if (_firstOrDefaultMethodInfoWithoutArgs.Equals(method))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ public override async Task Substring_with_zero_startindex(bool async)
await base.Substring_with_zero_startindex(async);

AssertSql(
@"SELECT c[""ContactName""]
@"SELECT SUBSTRING(c[""ContactName""], 0, 3) AS c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""CustomerID""] = ""ALFKI""))");
}
Expand All @@ -760,7 +760,7 @@ public override async Task Substring_with_zero_length(bool async)
await base.Substring_with_zero_length(async);

AssertSql(
@"SELECT c[""ContactName""]
@"SELECT SUBSTRING(c[""ContactName""], 2, 0) AS c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""CustomerID""] = ""ALFKI""))");
}
Expand All @@ -770,7 +770,7 @@ public override async Task Substring_with_constant(bool async)
await base.Substring_with_constant(async);

AssertSql(
@"SELECT c[""ContactName""]
@"SELECT SUBSTRING(c[""ContactName""], 1, 3) AS c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""CustomerID""] = ""ALFKI""))");
}
Expand All @@ -780,7 +780,9 @@ public override async Task Substring_with_closure(bool async)
await base.Substring_with_closure(async);

AssertSql(
@"SELECT c[""ContactName""]
@"@__start_0='2'
SELECT SUBSTRING(c[""ContactName""], @__start_0, 3) AS c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""CustomerID""] = ""ALFKI""))");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,15 +1016,14 @@ FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
WHERE ((c[""Discriminator""] = ""Customer"") AND (SUBSTRING(c[""City""], 1, 2) = ""ea""))");
}

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

0 comments on commit 8936a65

Please sign in to comment.