From 10d50e08792b51fc211a3ed2eef32722be299418 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Wed, 24 Jul 2024 08:07:49 -0700 Subject: [PATCH 1/4] require fixed width types for gpu casting --- python/cudf_polars/cudf_polars/dsl/expr.py | 6 +++++- .../tests/expressions/test_numeric_binops.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/python/cudf_polars/cudf_polars/dsl/expr.py b/python/cudf_polars/cudf_polars/dsl/expr.py index 6325feced94..8d6d67d853e 100644 --- a/python/cudf_polars/cudf_polars/dsl/expr.py +++ b/python/cudf_polars/cudf_polars/dsl/expr.py @@ -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(self.dtype, value.dtype) + ): raise NotImplementedError( f"Can't cast {self.dtype.id().name} to {value.dtype.id().name}" ) diff --git a/python/cudf_polars/tests/expressions/test_numeric_binops.py b/python/cudf_polars/tests/expressions/test_numeric_binops.py index b6bcd0026fa..8f68bbc460c 100644 --- a/python/cudf_polars/tests/expressions/test_numeric_binops.py +++ b/python/cudf_polars/tests/expressions/test_numeric_binops.py @@ -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, @@ -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) From d3dcb3988fc070ee69a1aa5fe3c29ae06f972b50 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Wed, 24 Jul 2024 11:16:44 -0700 Subject: [PATCH 2/4] adjust logic --- python/cudf_polars/cudf_polars/dsl/expr.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/python/cudf_polars/cudf_polars/dsl/expr.py b/python/cudf_polars/cudf_polars/dsl/expr.py index 8d6d67d853e..e9eded6fadb 100644 --- a/python/cudf_polars/cudf_polars/dsl/expr.py +++ b/python/cudf_polars/cudf_polars/dsl/expr.py @@ -1188,14 +1188,17 @@ class Cast(Expr): def __init__(self, dtype: plc.DataType, value: Expr) -> None: super().__init__(dtype) self.children = (value,) - if ( - not plc.traits.is_fixed_width(self.dtype) + if not ( + plc.traits.is_fixed_width(self.dtype) and plc.traits.is_fixed_width(value.dtype) - and plc.unary.is_supported_cast(self.dtype, 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}" ) + raise NotImplementedError( + f"Can't cast {self.dtype.id().name} to {value.dtype.id().name}" + ) def do_evaluate( self, From 17944dfaa9949c966f4cf099b9cf669685749fff Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Wed, 24 Jul 2024 11:17:25 -0700 Subject: [PATCH 3/4] fix --- python/cudf_polars/cudf_polars/dsl/expr.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/python/cudf_polars/cudf_polars/dsl/expr.py b/python/cudf_polars/cudf_polars/dsl/expr.py index e9eded6fadb..9e0fca3f52f 100644 --- a/python/cudf_polars/cudf_polars/dsl/expr.py +++ b/python/cudf_polars/cudf_polars/dsl/expr.py @@ -1196,9 +1196,6 @@ def __init__(self, dtype: plc.DataType, value: Expr) -> None: raise NotImplementedError( f"Can't cast {self.dtype.id().name} to {value.dtype.id().name}" ) - raise NotImplementedError( - f"Can't cast {self.dtype.id().name} to {value.dtype.id().name}" - ) def do_evaluate( self, From 6faf4806da8f6fe70ca0a73ee0634fd79040f682 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Wed, 24 Jul 2024 18:40:38 -0700 Subject: [PATCH 4/4] adjust contains test that was falling back early --- python/cudf_polars/tests/expressions/test_stringfunction.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/cudf_polars/tests/expressions/test_stringfunction.py b/python/cudf_polars/tests/expressions/test_stringfunction.py index 8cf65dd51ac..df08e15baa4 100644 --- a/python/cudf_polars/tests/expressions/test_stringfunction.py +++ b/python/cudf_polars/tests/expressions/test_stringfunction.py @@ -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 = [ @@ -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)