fix: substring window is wrong for start positions below 1 - #480
Open
spokodev wants to merge 1 commit into
Open
Conversation
substring(str from S for L) selects the window [S, S+L) clipped to valid
positions. When S was below 1 the length was measured from the clamped
start instead of the original one, so too many characters were returned:
substring('012345678' from 0 for 3); -- was '012', postgres '01'
substring('012345678' from -1 for 3); -- was '012', postgres '0'
substr('012345678', 0, 3); -- was '012', postgres '01'
Also default an omitted FROM to position 1 (so 'substring(x for n)' is
unchanged) and update overlay(), which relied on the old clamped call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
substring(str from S for L)selects the window[S, S+L)clipped to valid (>= 1) positions. WhenSis below 1, pg-mem applies the length from the clamped start (position 1) instead of the originalS, so it returns too many characters:The same
sqlSubstringhelper backs thesubstring/substrfunctions and thesubstring(... from ... for ...)form.Ref: Postgres string functions
Cause
sqlSubstringclamped the start to 0 but still passed the full length tosubstr(start, len), so the window's end shifted right by the amount that was clamped off the front.Fix
Compute the window end from the original start (
end = from + len), clamp only the start, and return an empty string when the window ends at or before the start. Also default an omittedFROMto position 1 (sosubstring(x for n)is unchanged) and updateoverlay(), which passedfrom = 0relying on the old clamp. Tests added insimple-queries.spec.ts.