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

feat(rust,python,cli): support negative indexing and expressions for LEFT, RIGHT and SUBSTR SQL string funcs #13888

Merged
merged 1 commit into from
Jan 24, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 21, 2024

Description

Builds on top of @reswqa's great work expressifying string slice (see #13747).

  • Extends SQL LEFT, RIGHT, and SUBSTR SQL string functions with expression support.
  • Adds support for negative length to both the LEFT and RIGHT functions.
  • Adds supports for negative start and negative length for SUBSTR.
  • Also fixes 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, withSUBSTR:

  • Starting at -10 doesn't mean "start 10 chars back from the end of the string", as you would likely expect; instead it really does mean "start 10 chars back from the front". So, SUBSTR(colt,-10,13) and SUBSTR(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 and RIGHT functions you have the opportunity to set negative length:

  • LEFT("s",-n) means "all but the last n chars"
  • RIGHT("s",-n) means "all but first n 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.

Screenshot 2024-01-17 at 00 08 15
  • (Screenshot: having fun confirming RIGHT equivalence) 😅

References

PostgreSQL string function docs:
https://www.postgresql.org/docs/9.1/functions-string.html#FUNCTIONS-STRING-OTHER

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Jan 21, 2024
@alexander-beedie alexander-beedie changed the title feat(rust,python,cli): support negative indexing and expressions for LEFT,RIGHT and SUBSTR string funcs feat(rust,python,cli): support negative indexing and expressions for LEFT, RIGHT and SUBSTR string funcs Jan 21, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jan 21, 2024
@alexander-beedie alexander-beedie force-pushed the sql-enhanced-left-right branch 3 times, most recently from 57e8261 to 8f51ba5 Compare January 23, 2024 05:58
@alexander-beedie alexander-beedie changed the title feat(rust,python,cli): support negative indexing and expressions for LEFT, RIGHT and SUBSTR string funcs feat(rust,python,cli): support negative indexing and expressions for LEFT, RIGHT and SUBSTR SQL string funcs Jan 24, 2024
_ => {
polars_bail!(InvalidOperation: "Invalid 'length' for Left: {}", function.args[1]);
when(length.clone().gt_eq(lit(0)))
Copy link
Member

Choose a reason for hiding this comment

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

Good thing we have CSE!

Copy link
Collaborator Author

@alexander-beedie alexander-beedie Jan 24, 2024

Choose a reason for hiding this comment

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

Definitely! Mapping back to SQL behaviour was awkward enough without having to worry about eating a performance hit too 😅

@ritchie46 ritchie46 merged commit af79f69 into pola-rs:main Jan 24, 2024
30 checks passed
@alexander-beedie alexander-beedie deleted the sql-enhanced-left-right branch January 24, 2024 07:29
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql Area: Polars SQL functionality enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants