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 SQL engine support for CONCAT_WS string function #13483

Merged
merged 2 commits into from
Jan 6, 2024

Conversation

alexander-beedie
Copy link
Collaborator

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

Add CONCAT_WS1 support to the SQL engine (aka: "concat with separator").

Interleaving a separator is common when concatenating; this function allows for that with a more lightweight syntax than applying the separators explicitly.

Example

import polars as pl

df = pl.DataFrame({
    "x": ["a", "b", "c", "d"],
    "y": ["e", "f", "g", "h"],
    "z": ["i", "j", "k", "l"],
})

pl.SQLContext(data=df).execute(
    """
    SELECT
      ("x" || ':' || "y" || ':' || "z") AS c1,   -- concat operator
      CONCAT("x", ':', "y", ':', "z")   AS c2,   -- basic concat function
      CONCAT_WS(':', "x", "y", "z")     AS c3,   -- concat 'with separator' variant
    FROM data
    """,
    eager=True,
)

# shape: (4, 3)
# ┌───────┬───────┬───────┐
# │ c1    ┆ c2    ┆ c3    │
# │ ---   ┆ ---   ┆ ---   │
# │ str   ┆ str   ┆ str   │
# ╞═══════╪═══════╪═══════╡
# │ a:e:i ┆ a:e:i ┆ a:e:i │
# │ b:f:j ┆ b:f:j ┆ b:f:j │
# │ c:g:k ┆ c:g:k ┆ c:g:k │
# │ d:h:l ┆ d:h:l ┆ d:h:l │
# └───────┴───────┴───────┘

Footnotes

  1. PostgreSQL docs: concat_ws

@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 6, 2024
@alexander-beedie alexander-beedie force-pushed the sql-concat-ws branch 2 times, most recently from 43da22c to fb21b56 Compare January 6, 2024 13:56
@alexander-beedie alexander-beedie changed the title feat(rust,python,cli): add SQL support for CONCAT_WS string function feat(rust,python,cli): add SQL engine support for CONCAT_WS string function Jan 6, 2024
@ritchie46 ritchie46 merged commit a8f03e7 into pola-rs:main Jan 6, 2024
26 checks passed
@alexander-beedie alexander-beedie deleted the sql-concat-ws branch January 6, 2024 16:32
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jan 7, 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