From 15115d81f4e0663961a5dbadeae34fddd8c9000f Mon Sep 17 00:00:00 2001 From: Roman Marusyk Date: Tue, 23 Feb 2021 00:05:26 +0200 Subject: [PATCH] Cosmos: Add translator for ToLower method which map to LOWER built-in function (#24203) --- .../Query/Internal/StringMethodTranslator.cs | 26 ++++++++++++------- .../NorthwindFunctionsQueryCosmosTest.cs | 3 +-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs b/src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs index 434e2ba2add..dc21aa2926d 100644 --- a/src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs +++ b/src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs @@ -30,6 +30,9 @@ private static readonly MethodInfo _startsWithMethodInfo private static readonly MethodInfo _endsWithMethodInfo = typeof(string).GetRequiredRuntimeMethod(nameof(string.EndsWith), new[] { typeof(string) }); + private static readonly MethodInfo _toLowerMethodInfo + = typeof(string).GetRequiredRuntimeMethod(nameof(string.ToLower), Array.Empty()); + private static readonly MethodInfo _firstOrDefaultMethodInfoWithoutArgs = typeof(Enumerable).GetRuntimeMethods().Single( m => m.Name == nameof(Enumerable.FirstOrDefault) @@ -85,38 +88,43 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto { if (_containsMethodInfo.Equals(method)) { - return TranslateSystemFunction("CONTAINS", instance, arguments[0], typeof(bool)); + return TranslateSystemFunction("CONTAINS", typeof(bool), instance, arguments[0]); } if (_startsWithMethodInfo.Equals(method)) { - return TranslateSystemFunction("STARTSWITH", instance, arguments[0], typeof(bool)); + return TranslateSystemFunction("STARTSWITH", typeof(bool), instance, arguments[0]); } if (_endsWithMethodInfo.Equals(method)) { - return TranslateSystemFunction("ENDSWITH", instance, arguments[0], typeof(bool)); + return TranslateSystemFunction("ENDSWITH", typeof(bool), instance, arguments[0]); + } + + if (_toLowerMethodInfo.Equals(method)) + { + return TranslateSystemFunction("LOWER", method.ReturnType, instance); } } if (_firstOrDefaultMethodInfoWithoutArgs.Equals(method)) { - return TranslateSystemFunction("LEFT", arguments[0], _sqlExpressionFactory.Constant(1), typeof(char)); + return TranslateSystemFunction("LEFT", typeof(char), arguments[0], _sqlExpressionFactory.Constant(1)); } if (_lastOrDefaultMethodInfoWithoutArgs.Equals(method)) { - return TranslateSystemFunction("RIGHT", arguments[0], _sqlExpressionFactory.Constant(1), typeof(char)); + return TranslateSystemFunction("RIGHT", typeof(char), arguments[0], _sqlExpressionFactory.Constant(1)); } - if(_stringConcatWithTwoArguments.Equals(method)) + if (_stringConcatWithTwoArguments.Equals(method)) { return _sqlExpressionFactory.Add( arguments[0], arguments[1]); } - if(_stringConcatWithThreeArguments.Equals(method)) + if (_stringConcatWithThreeArguments.Equals(method)) { return _sqlExpressionFactory.Add( arguments[0], @@ -139,7 +147,7 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto return null; } - private SqlExpression TranslateSystemFunction(string function, SqlExpression instance, SqlExpression pattern, Type returnType) - => _sqlExpressionFactory.Function(function, new[] { instance, pattern }, returnType); + private SqlExpression TranslateSystemFunction(string function, Type returnType, params SqlExpression[] arguments) + => _sqlExpressionFactory.Function(function, arguments, returnType); } } diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindFunctionsQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindFunctionsQueryCosmosTest.cs index e3770d9c77b..dd1daa52a21 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindFunctionsQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindFunctionsQueryCosmosTest.cs @@ -617,7 +617,6 @@ FROM root c WHERE (c[""Discriminator""] = ""Customer"")"); } - [ConditionalTheory(Skip = "Issue #17246")] public override async Task Where_string_to_lower(bool async) { await base.Where_string_to_lower(async); @@ -625,7 +624,7 @@ public override async Task Where_string_to_lower(bool async) AssertSql( @"SELECT c FROM root c -WHERE (c[""Discriminator""] = ""Customer"")"); +WHERE ((c[""Discriminator""] = ""Customer"") AND (LOWER(c[""CustomerID""]) = ""alfki""))"); } [ConditionalTheory(Skip = "Issue #17246")]