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

Delete unused code from stringfunction evaluator #16032

Merged
Show file tree
Hide file tree
Changes from 3 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
36 changes: 9 additions & 27 deletions python/cudf_polars/cudf_polars/dsl/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,13 +688,12 @@ def do_evaluate(
else pat.obj
)
return Column(plc.strings.find.contains(column.obj, pattern))
else:
assert isinstance(arg, Literal)
prog = plc.strings.regex_program.RegexProgram.create(
arg.value.as_py(),
flags=plc.strings.regex_flags.RegexFlags.DEFAULT,
)
return Column(plc.strings.contains.contains_re(column.obj, prog))
assert isinstance(arg, Literal)
prog = plc.strings.regex_program.RegexProgram.create(
arg.value.as_py(),
flags=plc.strings.regex_flags.RegexFlags.DEFAULT,
)
return Column(plc.strings.contains.contains_re(column.obj, prog))
columns = [
child.evaluate(df, context=context, mapping=mapping)
for child in self.children
Expand Down Expand Up @@ -725,26 +724,9 @@ def do_evaluate(
else prefix.obj,
)
)
else:
columns = [
child.evaluate(df, context=context, mapping=mapping)
for child in self.children
]
if self.name == pl_expr.StringFunction.Lowercase:
(column,) = columns
return Column(plc.strings.case.to_lower(column.obj))
elif self.name == pl_expr.StringFunction.Uppercase:
(column,) = columns
return Column(plc.strings.case.to_upper(column.obj))
elif self.name == pl_expr.StringFunction.EndsWith:
column, suffix = columns
return Column(plc.strings.find.ends_with(column.obj, suffix.obj))
elif self.name == pl_expr.StringFunction.StartsWith:
column, suffix = columns
return Column(plc.strings.find.starts_with(column.obj, suffix.obj))
raise NotImplementedError(
f"StringFunction {self.name}"
) # pragma: no cover; handled by init raising
raise NotImplementedError(
f"StringFunction {self.name}"
) # pragma: no cover; handled by init raising


class Sort(Expr):
Expand Down
71 changes: 70 additions & 1 deletion python/cudf_polars/tests/expressions/test_stringfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

from functools import partial

import pytest

import polars as pl

from cudf_polars import translate_ir
from cudf_polars import execute_with_cudf, translate_ir
from cudf_polars.testing.asserts import assert_gpu_result_equal


Expand Down Expand Up @@ -39,3 +41,70 @@ def test_unsupported_stringfunction():

with pytest.raises(NotImplementedError):
_ = translate_ir(q._ldf.visit())


def test_contains_re_non_strict_raises():
df = pl.LazyFrame({"a": ["a"]})

q = df.select(pl.col("a").str.contains(".", strict=False))

with pytest.raises(NotImplementedError):
_ = translate_ir(q._ldf.visit())


def test_contains_re_non_literal_raises():
df = pl.LazyFrame({"a": ["a", "b"], "b": [".", "e"]})

q = df.select(pl.col("a").str.contains(pl.col("b"), literal=False))

with pytest.raises(NotImplementedError):
_ = translate_ir(q._ldf.visit())


@pytest.fixture
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
def ldf():
return pl.DataFrame(
{"a": ["AbC", "de", "FGHI", "j", "kLm", "nOPq", None, "RsT", None, "uVw"]}
).lazy()


@pytest.mark.parametrize(
"substr",
[
"A",
"de",
".*",
"^a",
"^A",
"[^a-z]",
"[a-z]{3,}",
"^[A-Z]{2,}",
"j|u",
],
)
def test_contains_regex(ldf, substr):
query = ldf.select(pl.col("a").str.contains(substr))
assert_gpu_result_equal(query)


@pytest.mark.parametrize(
"literal", ["A", "de", "FGHI", "j", "kLm", "nOPq", "RsT", "uVw"]
)
def test_contains_literal(ldf, literal):
query = ldf.select(pl.col("a").str.contains(pl.lit(literal), literal=True))
assert_gpu_result_equal(query)


def test_contains_column(ldf):
query = ldf.select(pl.col("a").str.contains(pl.col("a"), literal=True))
assert_gpu_result_equal(query)


@pytest.mark.parametrize("pat", ["["])
def test_contains_invalid(ldf, pat):
query = ldf.select(pl.col("a").str.contains(pat))

with pytest.raises(pl.exceptions.ComputeError):
query.collect()
with pytest.raises(pl.exceptions.ComputeError):
query.collect(post_opt_callback=partial(execute_with_cudf, raise_on_fail=True))
61 changes: 0 additions & 61 deletions python/cudf_polars/tests/test_string.py

This file was deleted.

Loading