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): add LENGTH and OCTET_LENGTH string functions for SQL #9860

Merged

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jul 13, 2023

Support for SQL string length functions, in terms of both characters (length) and bytes (octet_length).

Also:

  • adds the related functions into StringNameSpace on the Rust side.
  • sorts some string function definitions for clarity).

Example

import polars as pl

df = pl.DataFrame({
    "words": ["Café", None, "東京"],
})

with pl.SQLContext( frame=df ) as ctx:
    res = ctx.execute(
        """
        SELECT
          words,
          LENGTH(words) AS n_chars,
          OCTET_LENGTH(words) AS n_bytes
        FROM frame
        """
    ).collect()

# shape: (3, 3)
# ┌───────┬─────────┬─────────┐
# │ words ┆ n_chars ┆ n_bytes │
# │ ---   ┆ ---     ┆ ---     │
# │ str   ┆ u32     ┆ u32     │
# ╞═══════╪═════════╪═════════╡
# │ Café  ┆ 4       ┆ 5       │
# │ null  ┆ null    ┆ null    │
# │ 東京  ┆ 2       ┆ 6       │
# └───────┴─────────┴─────────┘

@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 Jul 13, 2023
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jul 13, 2023
pub fn from_radix(self, radix: u32, strict: bool) -> Expr {
self.0
.map_private(FunctionExpr::StringExpr(StringFunction::FromRadix(
radix, strict,
)))
}

/// Return the number of characters in the string (not bytes).
pub fn n_chars(self) -> Expr {
Copy link
Member

Choose a reason for hiding this comment

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

Nice, can we now properly dispatch the python side to those?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops! Missed the final link in the chain 😅 Should be good now - jumping on a plane in about an hour (at the airport now), so if there's anything else then I'll pick it up when I land and get home this evening. (In the meantime, let's see if I can get anything done from the emergency exit row at 10km up :)

Copy link
Member

Choose a reason for hiding this comment

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

All good! You will land in a world with this PR merged. ;)

@ritchie46 ritchie46 merged commit 7b0527c into pola-rs:main Jul 14, 2023
c-peters pushed a commit to c-peters/polars that referenced this pull request Jul 14, 2023
@alexander-beedie alexander-beedie deleted the sql-string-chars-bytes-length branch July 16, 2023 21:03
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