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: duckdb replace_all with regex #1784

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions narwhals/_duckdb/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,17 @@ def replace_all(
from duckdb import FunctionExpression

if literal is False:
msg = "`replace_all` for DuckDB currently only supports `literal=True`."
raise NotImplementedError(msg)
return self._compliant_expr._from_call(
lambda _input: FunctionExpression(
"regexp_replace",
_input,
ConstantExpression(pattern),
ConstantExpression(value),
ConstantExpression("g"),
),
"replace_all",
returns_scalar=self._compliant_expr._returns_scalar,
)
return self._compliant_expr._from_call(
lambda _input: FunctionExpression(
"replace", _input, ConstantExpression(pattern), ConstantExpression(value)
Expand Down
3 changes: 0 additions & 3 deletions tests/expr_and_series/str/replace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,12 @@ def test_str_replace_expr(
)
def test_str_replace_all_expr(
constructor: Constructor,
request: pytest.FixtureRequest,
data: dict[str, list[str]],
pattern: str,
value: str,
literal: bool, # noqa: FBT001
expected: dict[str, list[str]],
) -> None:
if "duckdb" in str(constructor) and literal is False:
request.applymarker(pytest.mark.xfail)
df = nw.from_native(constructor(data))
result = df.select(
nw.col("a").str.replace_all(pattern=pattern, value=value, literal=literal)
Expand Down
Loading