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: Support PostgreSQL ^@ ("starts with"), and ~~,~~*,!~~,!~~* ("like", "ilike") string-matching operators #17251

Merged
merged 1 commit into from
Jun 28, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jun 27, 2024

Adds support for the following PostgreSQL string matching operators 1:

  • ^@: "starts with"
  • ~~: "like"
  • ~~*: "ilike"
  • !~~: "not like"
  • !~~*: "not ilike"

Example

import polars as pl

df = pl.DataFrame({
    "x": ["aaa", "bbb", "a"],
    "y": ["abc", "b", "aa"],
})
df.sql("""
  SELECT
    x,
    x ^@ 'a' AS x_starts_with_a,
    x ~~* '%B' AS x_ends_with_b
  FROM self
""")
# shape: (3, 3)
# ┌─────┬─────────────────┬───────────────┐
# │ x   ┆ x_starts_with_a ┆ x_ends_with_b │
# │ --- ┆ ---             ┆ ---           │
# │ str ┆ bool            ┆ bool          │
# ╞═════╪═════════════════╪═══════════════╡
# │ aaa ┆ true            ┆ false         │
# │ bbb ┆ false           ┆ true          │
# │ a   ┆ true            ┆ false         │
# └─────┴─────────────────┴───────────────┘
df.sql("""
  SELECT
    x,
    y,
    x ^@ y AS x_starts_with_y
    x ~~ '%a' AS x_ends_with_a
  FROM self
""")
# shape: (3, 4)
# ┌─────┬─────┬─────────────────┬───────────────┐
# │ x   ┆ y   ┆ x_starts_with_y ┆ x_ends_with_a │
# │ --- ┆ --- ┆ ---             ┆ ---           │
# │ str ┆ str ┆ bool            ┆ bool          │
# ╞═════╪═════╪═════════════════╪═══════════════╡
# │ aaa ┆ abc ┆ false           ┆ true          │
# │ bbb ┆ b   ┆ true            ┆ false         │
# │ a   ┆ aa  ┆ false           ┆ true          │
# └─────┴─────┴─────────────────┴───────────────┘

Footnotes

  1. https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-LIKE

@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 Jun 27, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jun 27, 2024
Copy link

codecov bot commented Jun 27, 2024

Codecov Report

Attention: Patch coverage is 95.45455% with 2 lines in your changes missing coverage. Please review.

Project coverage is 80.85%. Comparing base (4d2f928) to head (ca7aa57).
Report is 3 commits behind head on main.

Files Patch % Lines
crates/polars-sql/src/sql_expr.rs 95.45% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17251      +/-   ##
==========================================
+ Coverage   80.82%   80.85%   +0.02%     
==========================================
  Files        1466     1466              
  Lines      192313   192336      +23     
  Branches     2745     2745              
==========================================
+ Hits       155446   155509      +63     
+ Misses      36364    36324      -40     
  Partials      503      503              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alexander-beedie alexander-beedie force-pushed the sql-startswith-operator branch 2 times, most recently from 026ceb9 to 433d5b2 Compare June 27, 2024 20:39
@alexander-beedie alexander-beedie force-pushed the sql-startswith-operator branch from 433d5b2 to ca7aa57 Compare June 27, 2024 20:48
@alexander-beedie alexander-beedie changed the title feat: Support PostgreSQL ^@ starts-with operator feat: Support PostgreSQL ^@ ("starts with"), and ~~, ~~*, !~~, !~~*` ("like/ilike") operators Jun 27, 2024
@alexander-beedie alexander-beedie changed the title feat: Support PostgreSQL ^@ ("starts with"), and ~~, ~~*, !~~, !~~*` ("like/ilike") operators feat: Support PostgreSQL ^@ ("starts with"), and ~~,~~*,!~~,!~~* ("like/ilike") operators Jun 27, 2024
@alexander-beedie alexander-beedie changed the title feat: Support PostgreSQL ^@ ("starts with"), and ~~,~~*,!~~,!~~* ("like/ilike") operators feat: Support PostgreSQL ^@ ("starts with"), and ~~,~~*,!~~,!~~* ("like/ilike") string-matching operators Jun 27, 2024
@alexander-beedie alexander-beedie changed the title feat: Support PostgreSQL ^@ ("starts with"), and ~~,~~*,!~~,!~~* ("like/ilike") string-matching operators feat: Support PostgreSQL ^@ ("starts with"), and ~~,~~*,!~~,!~~* ("like", "ilike") string-matching operators Jun 27, 2024
@ritchie46 ritchie46 merged commit 079de10 into pola-rs:main Jun 28, 2024
30 checks passed
@alexander-beedie alexander-beedie deleted the sql-startswith-operator branch June 28, 2024 09:02
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