Repository navigation
Make sure that when using PadLeft/PadRight we don't truncate if given length is less than the string length - #3288
Conversation
79fae3a to
f894de9
Compare
75c2ea9 to
d038259
Compare
roji
left a comment
There was a problem hiding this comment.
Recommendation: This is a justified, appropriately small fix for #3116. I found no blocking correctness issue in the reported truncation scenario; I have two non-blocking suggestions below.
Problem. PostgreSQL's lpad/rpad truncate inputs longer than the requested width, while .NET leaves them unchanged. For a stored Name = "1234", this query currently returns "12" for both properties, rather than "1234":
context.Blogs.Select(b => new
{
Left = b.Name.PadLeft(2, '0'),
Right = b.Name.PadRight(2, '0')
});Expected approach. Keep the existing padding functions and guard them with CASE WHEN length(value) >= width THEN value ELSE lpad/rpad(...) END. That requires only a local translator change and regression coverage; no new database function, public API, plugin changes, or expanded abstraction is needed. No public API documentation change is necessary for this behavioral correction.
Alignment. The translator change follows that approach and applies it to all four overloads. The scope is tight. The issue also reproduces on providers 7.0.18, 8.0.0, 8.0.2, 9.0.4, and 10.0.3, so it is not a newly introduced 8.x/9.x/10.x regression.
Gaps. The new tests catch the one-argument, parameter-width case, but not the original report's explicit-padding-character overload. I'd add that small regression case, preferably checking projected values. The comparison expression can also use the existing typed helper instead of MakeBinary(...)!; neither suggestion needs additional infrastructure.
Manual validation. I built this exact PR head and exercised it through a separate console application against PostgreSQL 18.4, not merely through static inspection or unit tests. All four overloads matched .NET for the 88 projected values checked, covering parameter/constant/column widths, nonnegative boundary widths, empty inputs, and SQL null propagation; four additional predicate/null queries passed. All 20 targeted padding functional tests also passed. For the historical test project, I used public NuGet restore, C# 13, and runtime roll-forward to the installed .NET 10 runtime; its pre-existing dependency audit warning remained visible. These results establish the truncation fix, not a claim of exhaustive .NET/PostgreSQL string-semantic equivalence.
Posted by copilot
… length is less than the string length
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 23f083e5-3c81-42bc-9498-227af60cbb89
|
@roji Nice!. Its been a while waiting hasnt it? |
… length is less than the string length (#3288) (#3994) Co-authored-by: Christopher Jolly <[email protected]>
…#3993) Backport #3288 to hotfix/10.0.4, preserving the release branch translator structure. (cherry picked from commit 8798528) Co-authored-by: Christopher Jolly <[email protected]>
Backport #3288 (8798528) to hotfix/9.0.5, preserving the existing if-based translator structure. Co-authored-by: Christopher Jolly <[email protected]> Co-authored-by: Copilot App <[email protected]> Copilot-Session: 6e7b833f-1022-4feb-8784-b03b1e2a1340
|
Yep, sorry about that... I've long been busy with others stuff and haven't yet time to properly take care of EFCore.PG. But I'm doing a push now for 11.0. Thanks for submitting this! |
From #3116 the lpad/rpad in npgsql truncate the string if the length given is less than the string length. This is different to .Net where it doesn't truncate
This fixes the SQL so that it matches the .Net expectation
Closes #3116