feat(rust,python,cli): support negative indexing and expressions for LEFT
, RIGHT
and SUBSTR
SQL string funcs
#13888
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.
Description
Builds on top of @reswqa's great work expressifying string
slice
(see #13747).LEFT
,RIGHT
, andSUBSTR
SQL string functions with expression support.LEFT
andRIGHT
functions.SUBSTR
.ROUND
error checking for negative 'n_decimals' as a drive-by.Notes on negative start/length
Getting this correct was a bit headache-inducing, as SQL has a different idea of what negative values mean here and it's 1-indexed.
For example, with
SUBSTR
:SUBSTR(colt,-10,13)
andSUBSTR(colx,1,2)
both return the same result (due to offsets and 1-indexing), which would be the first two letters of the string 🤔With the
LEFT
andRIGHT
functions you have the opportunity to set negative length:LEFT("s",-n)
means "all but the lastn
chars"RIGHT("s",-n)
means "all but firstn
chars"Testing
This probably took more time than the actual implementation, due to the notable differences between what you'd typically expect (as a programmer) and what the SQL standard actually defines. I ended up with a set of unit tests that I could also run directly on a dockerised PostgreSQL instance to validate the behaviour, covering all major parameter characteristics.
RIGHT
equivalence) 😅References
PostgreSQL string function docs:
https://www.postgresql.org/docs/9.1/functions-string.html#FUNCTIONS-STRING-OTHER