From 57f8222fb04935759e390a8fb8c09cd8ba388e07 Mon Sep 17 00:00:00 2001 From: Christopher Jolly Date: Sun, 22 Sep 2024 01:32:55 +0800 Subject: [PATCH] Make sure that when using PadLeft/PadRight we don't truncate if given length is less than the string length --- .../Internal/NpgsqlStringMethodTranslator.cs | 4 +++- .../NorthwindFunctionsQueryNpgsqlTest.cs | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs index d56f63380..10c40fe57 100644 --- a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs +++ b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs @@ -326,13 +326,15 @@ public NpgsqlStringMethodTranslator(NpgsqlTypeMappingSource typeMappingSource, I ? [instance!, arguments[0]] : new[] { instance!, arguments[0], arguments[1] }; - return _sqlExpressionFactory.Function( + var padFunc = _sqlExpressionFactory.Function( method == PadLeft || method == PadLeftWithChar ? "lpad" : "rpad", args, nullable: true, argumentsPropagateNullability: TrueArrays[args.Length], instance!.Type, instance.TypeMapping); + var lengthFunc = _sqlExpressionFactory.Function("length", [instance], true, [true], typeof(int)); + return _sqlExpressionFactory.Case([new CaseWhenClause(_sqlExpressionFactory.MakeBinary(ExpressionType.GreaterThanOrEqual, lengthFunc, arguments[0], null)!, instance)], padFunc); } if (method.DeclaringType == typeof(string) diff --git a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs index c8fd85fe5..07175326a 100644 --- a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs @@ -298,6 +298,17 @@ public Task PadLeft_with_parameter(bool async) ss => ss.Set().Where(x => x.Address.PadLeft(length).EndsWith("Walserweg 21"))); } + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public Task PadLeft_with_parameter_does_not_truncate(bool async) + { + var length = 5; + + return AssertQuery( + async, + ss => ss.Set().Where(x => x.Address.PadLeft(length).EndsWith("Walserweg 21"))); + } + [ConditionalTheory] [MemberData(nameof(IsAsyncData))] public Task PadLeft_char_with_parameter(bool async) @@ -334,6 +345,17 @@ public Task PadRight_with_parameter(bool async) ss => ss.Set().Where(x => x.Address.PadRight(length).StartsWith("Walserweg 21"))); } + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public Task PadRight_with_parameter_does_not_truncate(bool async) + { + var length = 5; + + return AssertQuery( + async, + ss => ss.Set().Where(x => x.Address.PadRight(length).StartsWith("Walserweg 21"))); + } + [ConditionalTheory] [MemberData(nameof(IsAsyncData))] public Task PadRight_char_with_parameter(bool async)