From 8936a6526cad41dd93c8a705f00c5aeede6ce1fd Mon Sep 17 00:00:00 2001 From: Roman Marusyk Date: Mon, 1 Mar 2021 04:10:45 +0200 Subject: [PATCH] Cosmos: Add translator for Substring method which map to SUBSTRING built-in functions (#24284) Cosmos: Add translator for Substring method which map to SUBSTRING built-in function --- .../Query/Internal/StringMethodTranslator.cs | 8 ++++++++ .../Query/NorthwindFunctionsQueryCosmosTest.cs | 10 ++++++---- .../Query/NorthwindWhereQueryCosmosTest.cs | 3 +-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs b/src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs index 81b480ef0c8..30d95ff6077 100644 --- a/src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs +++ b/src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs @@ -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) @@ -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)) diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindFunctionsQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindFunctionsQueryCosmosTest.cs index 6fbc55eee35..cb32b067de2 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindFunctionsQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindFunctionsQueryCosmosTest.cs @@ -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""))"); } @@ -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""))"); } @@ -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""))"); } @@ -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""))"); } diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindWhereQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindWhereQueryCosmosTest.cs index 9dbd4290501..4a118d3ad3e 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindWhereQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindWhereQueryCosmosTest.cs @@ -1016,7 +1016,6 @@ 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); @@ -1024,7 +1023,7 @@ public override async Task Where_string_substring(bool 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")]