Skip to content

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

Merged
roji merged 2 commits into
npgsql:mainfrom
ChrisJollyAU:padleftright
Oct 17, 2026
Merged

roji merged 2 commits into
npgsql:mainfrom
ChrisJollyAU:padleftright

Conversation

@ChrisJollyAU

@ChrisJollyAU ChrisJollyAU commented Sep 21, 2024 •

Copy link
Copy Markdown
Contributor

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

@roji
roji force-pushed the main branch 9 times, most recently from 79fae3a to f894de9 Compare February 16, 2026 16:00
@roji
roji force-pushed the main branch 2 times, most recently from 75c2ea9 to d038259 Compare February 25, 2026 14:25

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ChrisJollyAU and others added 2 commits October 17, 2026 12:27
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 23f083e5-3c81-42bc-9498-227af60cbb89
@ChrisJollyAU

Copy link
Copy Markdown
Contributor Author

@roji Nice!. Its been a while waiting hasnt it?

roji added a commit that referenced this pull request Oct 17, 2026
… length is less than the string length (#3288) (#3994)

Co-authored-by: Christopher Jolly <[email protected]>
roji added a commit that referenced this pull request Oct 17, 2026
…#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]>
roji added a commit that referenced this pull request Oct 17, 2026
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
@roji

roji commented Oct 17, 2026

Copy link
Copy Markdown
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

string.PadLeft and string.PadRight wrong translation

2 participants