Skip to content

Commit

Permalink
Require fixed width types for casting in cudf-polars (#16381)
Browse files Browse the repository at this point in the history
Fixes a bug where numeric <-> string casts are not being properly rejected at the cudf-polars level.

Authors:
  - https://github.com/brandon-b-miller

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #16381
  • Loading branch information
brandon-b-miller authored Jul 25, 2024
1 parent f756e01 commit e553295
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion python/cudf_polars/cudf_polars/dsl/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,11 @@ class Cast(Expr):
def __init__(self, dtype: plc.DataType, value: Expr) -> None:
super().__init__(dtype)
self.children = (value,)
if not plc.unary.is_supported_cast(self.dtype, value.dtype):
if not (
plc.traits.is_fixed_width(self.dtype)
and plc.traits.is_fixed_width(value.dtype)
and plc.unary.is_supported_cast(value.dtype, self.dtype)
):
raise NotImplementedError(
f"Can't cast {self.dtype.id().name} to {value.dtype.id().name}"
)
Expand Down
14 changes: 13 additions & 1 deletion python/cudf_polars/tests/expressions/test_numeric_binops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import polars as pl

from cudf_polars.testing.asserts import assert_gpu_result_equal
from cudf_polars.testing.asserts import (
assert_gpu_result_equal,
assert_ir_translation_raises,
)

dtypes = [
pl.Int8,
Expand Down Expand Up @@ -111,3 +114,12 @@ def test_binop_with_scalar(left_scalar, right_scalar):
q = df.select(lop / rop)

assert_gpu_result_equal(q)


def test_numeric_to_string_cast_fails():
df = pl.DataFrame(
{"a": [1, 1, 2, 3, 3, 4, 1], "b": [None, 2, 3, 4, 5, 6, 7]}
).lazy()
q = df.select(pl.col("a").cast(pl.String))

assert_ir_translation_raises(q, NotImplementedError)
6 changes: 4 additions & 2 deletions python/cudf_polars/tests/expressions/test_stringfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def ldf(with_nulls):
if with_nulls:
a[4] = None
a[-3] = None
return pl.LazyFrame({"a": a, "b": range(len(a))})
return pl.LazyFrame(
{"a": a, "b": range(len(a)), "c": [str(i) for i in range(len(a))]}
)


slice_cases = [
Expand Down Expand Up @@ -84,7 +86,7 @@ def test_contains_re_non_strict_raises(ldf):


def test_contains_re_non_literal_raises(ldf):
q = ldf.select(pl.col("a").str.contains(pl.col("b"), literal=False))
q = ldf.select(pl.col("a").str.contains(pl.col("c"), literal=False))

assert_ir_translation_raises(q, NotImplementedError)

Expand Down

0 comments on commit e553295

Please sign in to comment.