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

fix: Ensure that SQL LIKE and ILIKE operators support multi-line matches #20613

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 8, 2025

Closes #20606.

The "%" pattern in SQL LIKE and ILIKE ops is expected to match across linebreaks 1.

Example

import polars as pl

s1 = "Hello World"
s2 = "Hello\nWorld"
s3 = "hello\nWORLD"

df = pl.DataFrame({
  "idx": [10, 20, 30],
  "txt": [s1, s2, s3],
})

🔴 Before

(linebreaks interrupt the match)

df.sql("SELECT * FROM self WHERE txt ILIKE 'Hello%'")
# shape: (1, 2)
# ┌─────┬─────────────┐
# │ idx ┆ txt         │
# │ --- ┆ ---         │
# │ i64 ┆ str         │
# ╞═════╪═════════════╡
# │ 10  ┆ Hello World │
# └─────┴─────────────┘

🟢 After

(linebreaks do not interrupt the match)

df.sql("SELECT * FROM self WHERE txt ILIKE 'Hello%'")
# shape: (3, 2)
# ┌─────┬─────────────┐
# │ idx ┆ txt         │
# │ --- ┆ ---         │
# │ i64 ┆ str         │
# ╞═════╪═════════════╡
# │ 10  ┆ Hello World │
# │ 20  ┆ Hello       │
# │     ┆ World       │
# │ 30  ┆ hello       │
# │     ┆ WORLD       │
# └─────┴─────────────┘

Aside: I think we should look at rendering "\n" in the table output here (and "\t", etc) instead of actually rendering newlines/tabs (etc) as-is. Might take a look at that later 👀

Footnotes

  1. "a percent sign (%) matches any sequence of zero or more characters." (link)

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jan 8, 2025
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jan 8, 2025
@ritchie46 ritchie46 merged commit 09687e4 into pola-rs:main Jan 9, 2025
34 checks passed
@alexander-beedie alexander-beedie deleted the sql-mulitline-like branch January 9, 2025 09:08
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 fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LIKE operator does not work on multiline str
2 participants