Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that when using PadLeft/PadRight we don't truncate if given length is less than the string length #3288

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ public Task PadLeft_with_parameter(bool async)
ss => ss.Set<Customer>().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<Customer>().Where(x => x.Address.PadLeft(length).EndsWith("Walserweg 21")));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public Task PadLeft_char_with_parameter(bool async)
Expand Down Expand Up @@ -334,6 +345,17 @@ public Task PadRight_with_parameter(bool async)
ss => ss.Set<Customer>().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<Customer>().Where(x => x.Address.PadRight(length).StartsWith("Walserweg 21")));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public Task PadRight_char_with_parameter(bool async)
Expand Down
Loading