Skip to content

Commit

Permalink
handle runtime conversion failure
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-b-miller committed Oct 16, 2024
1 parent 7feb1f3 commit 6d699ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
38 changes: 30 additions & 8 deletions python/cudf_polars/cudf_polars/dsl/expressions/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@

import pyarrow as pa
import pylibcudf as plc
from pylibcudf.strings.convert.convert_floats import from_floats, is_float, to_floats
from pylibcudf.strings.convert.convert_integers import (
from_integers,
is_integer,
to_integers,
)
from pylibcudf.traits import is_floating_point

from polars.exceptions import InvalidOperationError

from cudf_polars.containers import Column
from cudf_polars.dsl.expressions.base import AggInfo, ExecutionContext, Expr
from cudf_polars.dsl.expressions.literal import Literal
Expand Down Expand Up @@ -61,18 +69,32 @@ def do_evaluate(
def _handle_string_cast(self, column: Column) -> plc.Column:
if self.dtype.id() == plc.TypeId.STRING:
if is_floating_point(column.obj.type()):
result = plc.strings.convert.convert_floats.from_floats(column.obj)
result = from_floats(column.obj)
else:
result = plc.strings.convert.convert_integers.from_integers(column.obj)
result = from_integers(column.obj)
else:
if is_floating_point(self.dtype):
result = plc.strings.convert.convert_floats.to_floats(
column.obj, self.dtype
)
floats = is_float(column.obj)
if not plc.interop.to_arrow(
plc.reduce.reduce(
floats,
plc.aggregation.all(),
plc.DataType(plc.TypeId.BOOL8),
)
).as_py():
raise InvalidOperationError("Conversion from `str` failed.")
result = to_floats(column.obj, self.dtype)
else:
result = plc.strings.convert.convert_integers.to_integers(
column.obj, self.dtype
)
integers = is_integer(column.obj)
if not plc.interop.to_arrow(
plc.reduce.reduce(
integers,
plc.aggregation.all(),
plc.DataType(plc.TypeId.BOOL8),
)
).as_py():
raise InvalidOperationError("Conversion from `str` failed.")
result = to_integers(column.obj, self.dtype)
return result

def collect_agg(self, *, depth: int) -> AggInfo:
Expand Down
10 changes: 10 additions & 0 deletions python/cudf_polars/tests/expressions/test_stringfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,13 @@ def test_string_to_numeric(str_to_numeric_data, numeric_type):
def test_string_from_numeric(str_from_numeric_data):
query = str_from_numeric_data.select(pl.col("a").cast(pl.String))
assert_gpu_result_equal(query)


def test_string_to_numeric_invalid(numeric_type):
df = pl.LazyFrame({"a": ["a", "b", "c"]})
q = df.select(pl.col("a").cast(numeric_type))
assert_collect_raises(
q,
polars_except=pl.exceptions.InvalidOperationError,
cudf_except=pl.exceptions.ComputeError,
)

0 comments on commit 6d699ac

Please sign in to comment.