From 12a6b2c1d01c227df21c507352c5f0d0830f04f4 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 11 Jun 2024 09:14:22 -0700 Subject: [PATCH 01/12] building --- .../libcudf/scalar/scalar_factories.pxd | 1 + .../_lib/pylibcudf/strings/CMakeLists.txt | 2 +- .../cudf/_lib/pylibcudf/strings/__init__.pxd | 1 + .../cudf/_lib/pylibcudf/strings/__init__.py | 1 + .../cudf/_lib/pylibcudf/strings/slice.pxd | 15 +++++ .../cudf/_lib/pylibcudf/strings/slice.pyx | 60 +++++++++++++++++++ 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd create mode 100644 python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx diff --git a/python/cudf/cudf/_lib/pylibcudf/libcudf/scalar/scalar_factories.pxd b/python/cudf/cudf/_lib/pylibcudf/libcudf/scalar/scalar_factories.pxd index 5c4e5bf346fd..c8220df8938e 100644 --- a/python/cudf/cudf/_lib/pylibcudf/libcudf/scalar/scalar_factories.pxd +++ b/python/cudf/cudf/_lib/pylibcudf/libcudf/scalar/scalar_factories.pxd @@ -8,3 +8,4 @@ from cudf._lib.pylibcudf.libcudf.scalar.scalar cimport scalar cdef extern from "cudf/scalar/scalar_factories.hpp" namespace "cudf" nogil: cdef unique_ptr[scalar] make_string_scalar(const string & _string) except + + cdef unique_ptr[scalar] make_fixed_width_scalar[T](T value) except + diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/CMakeLists.txt b/python/cudf/cudf/_lib/pylibcudf/strings/CMakeLists.txt index cb7f71b1912f..b499a127541d 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/CMakeLists.txt +++ b/python/cudf/cudf/_lib/pylibcudf/strings/CMakeLists.txt @@ -13,7 +13,7 @@ # ============================================================================= set(cython_sources capitalize.pyx case.pyx char_types.pyx contains.pyx find.pyx regex_flags.pyx - regex_program.pyx replace.pyx + regex_program.pyx replace.pyx slice.pyx ) set(linked_libraries cudf::cudf) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/__init__.pxd b/python/cudf/cudf/_lib/pylibcudf/strings/__init__.pxd index 959aa94737d0..d1f632d6d8e1 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/__init__.pxd +++ b/python/cudf/cudf/_lib/pylibcudf/strings/__init__.pxd @@ -9,4 +9,5 @@ from . cimport ( regex_flags, regex_program, replace, + slice, ) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/__init__.py b/python/cudf/cudf/_lib/pylibcudf/strings/__init__.py index b73849132866..ef102aff2afc 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/__init__.py +++ b/python/cudf/cudf/_lib/pylibcudf/strings/__init__.py @@ -9,4 +9,5 @@ regex_flags, regex_program, replace, + slice, ) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd new file mode 100644 index 000000000000..ef93e97435a5 --- /dev/null +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd @@ -0,0 +1,15 @@ +# Copyright (c) 2024, NVIDIA CORPORATION. + +from cudf._lib.pylibcudf.column cimport Column +from cudf._lib.pylibcudf.scalar cimport Scalar + +ctypedef fused ColumnOrScalar: + Column + Scalar + +cpdef Column slice_strings( + Column input, + ColumnOrScalar start=*, + ColumnOrScalar stop=*, + ColumnOrScalar step=* +) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx new file mode 100644 index 000000000000..cb047cb494fe --- /dev/null +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx @@ -0,0 +1,60 @@ +# Copyright (c) 2024, NVIDIA CORPORATION. + +from libcpp.memory cimport unique_ptr +from libcpp.utility cimport move + +from cudf._lib.pylibcudf.column cimport Column +from cudf._lib.pylibcudf.libcudf.column.column cimport column +from cudf._lib.pylibcudf.libcudf.scalar.scalar cimport numeric_scalar +from cudf._lib.pylibcudf.libcudf.scalar.scalar_factories cimport ( + make_fixed_width_scalar as cpp_make_fixed_width_scalar, +) +from cudf._lib.pylibcudf.libcudf.strings cimport substring as cpp_slice +from cudf._lib.pylibcudf.libcudf.types cimport size_type +from cudf._lib.pylibcudf.scalar cimport Scalar + +from cython.operator import dereference + + +cpdef Column slice_strings( + Column input, + ColumnOrScalar start=None, + ColumnOrScalar stop=None, + ColumnOrScalar step=None +): + cdef unique_ptr[column] c_result + cdef numeric_scalar[size_type]* cpp_start + cdef numeric_scalar[size_type]* cpp_stop + cdef numeric_scalar[size_type]* cpp_step + + if ColumnOrScalar is Column: + pass + elif ColumnOrScalar is Scalar: + if start is None: + delimiters = Scalar.from_libcudf( + cpp_make_fixed_width_scalar(0) + ) + if stop is None: + delimiters = Scalar.from_libcudf( + cpp_make_fixed_width_scalar(0) + ) + if step is None: + delimiters = Scalar.from_libcudf( + cpp_make_fixed_width_scalar(1) + ) + + cpp_start = start.c_obj.get() + cpp_stop = stop.c_obj.get() + cpp_step = step.c_obj.get() + + with nogil: + c_result = cpp_slice.slice_strings( + input.view(), + dereference(cpp_start), + dereference(cpp_stop), + dereference(cpp_step) + ) + else: + raise ValueError("start, stop, and step must be either Column or Scalar") + + return Column.from_libcudf(move(c_result)) From 2c33cf7fb57fe9350a308f58978be97a608c9ecd Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 11 Jun 2024 11:09:22 -0700 Subject: [PATCH 02/12] plumbing to cuDF cython --- .../cudf/_lib/pylibcudf/strings/slice.pyx | 11 +++- python/cudf/cudf/_lib/strings/substring.pyx | 50 +++++++------------ 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx index cb047cb494fe..bc02100bd07a 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx @@ -28,7 +28,16 @@ cpdef Column slice_strings( cdef numeric_scalar[size_type]* cpp_step if ColumnOrScalar is Column: - pass + if step is not None: + raise ValueError("Column-wise slice does not support step") + + with nogil: + c_result = cpp_slice.slice_strings( + input.view(), + start.view(), + stop.view() + ) + elif ColumnOrScalar is Scalar: if start is None: delimiters = Scalar.from_libcudf( diff --git a/python/cudf/cudf/_lib/strings/substring.pyx b/python/cudf/cudf/_lib/strings/substring.pyx index 170c1016b89b..ccdd4d8623dc 100644 --- a/python/cudf/cudf/_lib/strings/substring.pyx +++ b/python/cudf/cudf/_lib/strings/substring.pyx @@ -20,6 +20,8 @@ from cudf._lib.scalar import as_device_scalar from cudf._lib.pylibcudf.libcudf.scalar.scalar cimport numeric_scalar from cudf._lib.scalar cimport DeviceScalar +import cudf._lib.pylibcudf as plc + @acquire_spill_lock() def slice_strings(Column source_strings, @@ -32,30 +34,18 @@ def slice_strings(Column source_strings, performed in steps by skipping `step` number of characters in a string. """ - cdef unique_ptr[column] c_result - cdef column_view source_view = source_strings.view() - cdef DeviceScalar start_scalar = as_device_scalar(start, np.int32) cdef DeviceScalar end_scalar = as_device_scalar(end, np.int32) cdef DeviceScalar step_scalar = as_device_scalar(step, np.int32) - cdef numeric_scalar[size_type]* start_numeric_scalar = \ - ( - start_scalar.get_raw_ptr()) - cdef numeric_scalar[size_type]* end_numeric_scalar = \ - (end_scalar.get_raw_ptr()) - cdef numeric_scalar[size_type]* step_numeric_scalar = \ - (step_scalar.get_raw_ptr()) - - with nogil: - c_result = move(cpp_slice_strings( - source_view, - start_numeric_scalar[0], - end_numeric_scalar[0], - step_numeric_scalar[0] - )) - - return Column.from_unique_ptr(move(c_result)) + return Column.from_pylibcudf( + plc.strings.slice.slice_strings( + source_strings.to_pylibcudf(mode="read"), + start_scalar.c_value, + end_scalar.c_value, + step_scalar.c_value + ) + ) @acquire_spill_lock() @@ -67,19 +57,13 @@ def slice_from(Column source_strings, at given starts and stops positions. `starts` and `stops` here are positions per element in the string-column. """ - cdef unique_ptr[column] c_result - cdef column_view source_view = source_strings.view() - cdef column_view starts_view = starts.view() - cdef column_view stops_view = stops.view() - - with nogil: - c_result = move(cpp_slice_strings( - source_view, - starts_view, - stops_view - )) - - return Column.from_unique_ptr(move(c_result)) + return Column.from_pylibcudf( + plc.strings.slice.slice_strings( + source_strings.to_pylibcudf(mode="read"), + starts.to_pylibcudf(mode="read"), + stops.to_pylibcudf(mode="read") + ) + ) @acquire_spill_lock() From 65e0011ee10a21b11907a36734926d53afc17231 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 11 Jun 2024 15:07:47 -0700 Subject: [PATCH 03/12] add tests --- .../cudf/pylibcudf_tests/test_string_slice.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 python/cudf/cudf/pylibcudf_tests/test_string_slice.py diff --git a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py new file mode 100644 index 000000000000..78c7f3a6b63f --- /dev/null +++ b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py @@ -0,0 +1,51 @@ +# Copyright (c) 2024, NVIDIA CORPORATION. + +import pyarrow as pa +import pytest +from utils import assert_column_eq + +import cudf._lib.pylibcudf as plc + + +@pytest.fixture(scope="module") +def string_col(): + return pa.array(["AbC"]) + + +@pytest.fixture( + scope="module", + params=[ + (1, 3, 1), + ], +) +def pa_start_stop_step(request): + return ( + pa.scalar(request.param[0], type=pa.int32()), + pa.scalar(request.param[1], type=pa.int32()), + pa.scalar(request.param[2], type=pa.int32()), + ) + + +@pytest.fixture(scope="module") +def plc_start_stop_step(pa_start_stop_step): + return ( + plc.interop.from_arrow(pa_start_stop_step[0]), + plc.interop.from_arrow(pa_start_stop_step[1]), + plc.interop.from_arrow(pa_start_stop_step[2]), + ) + + +def test_slice(string_col, pa_start_stop_step, plc_start_stop_step): + plc_col = plc.interop.from_arrow(string_col) + + pa_start, pa_stop, pa_step = pa_start_stop_step + plc_start, plc_stop, plc_step = plc_start_stop_step + + expected = pa.compute.utf8_slice_codeunits( + string_col, pa_start.as_py(), pa_stop.as_py(), pa_step.as_py() + ) + got = plc.strings.slice.slice_strings( + plc_col, start=plc_start, stop=plc_stop, step=plc_step + ) + + assert_column_eq(expected, got) From a6e9ed2d5401ed0db714ea9e243ddaabfd0feb70 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 11 Jun 2024 17:49:32 -0700 Subject: [PATCH 04/12] excize improper pyarrow function --- .../cudf/pylibcudf_tests/test_string_slice.py | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py index 78c7f3a6b63f..8d72700706fe 100644 --- a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py +++ b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py @@ -16,23 +16,16 @@ def string_col(): scope="module", params=[ (1, 3, 1), + (0, 3, -1), ], ) def pa_start_stop_step(request): - return ( - pa.scalar(request.param[0], type=pa.int32()), - pa.scalar(request.param[1], type=pa.int32()), - pa.scalar(request.param[2], type=pa.int32()), - ) + return tuple(pa.scalar(x, type=pa.int32()) for x in request.param) @pytest.fixture(scope="module") def plc_start_stop_step(pa_start_stop_step): - return ( - plc.interop.from_arrow(pa_start_stop_step[0]), - plc.interop.from_arrow(pa_start_stop_step[1]), - plc.interop.from_arrow(pa_start_stop_step[2]), - ) + return tuple(plc.interop.from_arrow(x) for x in pa_start_stop_step) def test_slice(string_col, pa_start_stop_step, plc_start_stop_step): @@ -41,9 +34,17 @@ def test_slice(string_col, pa_start_stop_step, plc_start_stop_step): pa_start, pa_stop, pa_step = pa_start_stop_step plc_start, plc_stop, plc_step = plc_start_stop_step - expected = pa.compute.utf8_slice_codeunits( - string_col, pa_start.as_py(), pa_stop.as_py(), pa_step.as_py() + def slice_string(st, start, stop, step): + return st[start:stop:step] if st is not None else None + + expected = pa.array( + [ + slice_string(x, pa_start.as_py(), pa_stop.as_py(), pa_step.as_py()) + for x in string_col.to_pylist() + ], + type=pa.string(), ) + got = plc.strings.slice.slice_strings( plc_col, start=plc_start, stop=plc_stop, step=plc_step ) From e52079c8d397c42d738edabac270911ff203919b Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 11 Jun 2024 18:17:41 -0700 Subject: [PATCH 05/12] column tests --- .../cudf/pylibcudf_tests/test_string_slice.py | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py index 8d72700706fe..d94ab8a7f997 100644 --- a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py +++ b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py @@ -8,8 +8,13 @@ @pytest.fixture(scope="module") -def string_col(): - return pa.array(["AbC"]) +def pa_col(): + return pa.array(["AbC", "123abc", "", " ", None]) + + +@pytest.fixture(scope="module") +def plc_col(pa_col): + return plc.interop.from_arrow(pa_col) @pytest.fixture( @@ -28,9 +33,27 @@ def plc_start_stop_step(pa_start_stop_step): return tuple(plc.interop.from_arrow(x) for x in pa_start_stop_step) -def test_slice(string_col, pa_start_stop_step, plc_start_stop_step): - plc_col = plc.interop.from_arrow(string_col) +@pytest.fixture(scope="module") +def pa_starts_col(): + return pa.array([0, 1, 3, -1, 100]) + + +@pytest.fixture(scope="module") +def plc_starts_col(pa_starts_col): + return plc.interop.from_arrow(pa_starts_col) + + +@pytest.fixture(scope="module") +def pa_stops_col(): + return pa.array([1, 3, 4, -1, 100]) + + +@pytest.fixture(scope="module") +def plc_stops_col(pa_stops_col): + return plc.interop.from_arrow(pa_stops_col) + +def test_slice(pa_col, plc_col, pa_start_stop_step, plc_start_stop_step): pa_start, pa_stop, pa_step = pa_start_stop_step plc_start, plc_stop, plc_step = plc_start_stop_step @@ -40,7 +63,7 @@ def slice_string(st, start, stop, step): expected = pa.array( [ slice_string(x, pa_start.as_py(), pa_stop.as_py(), pa_step.as_py()) - for x in string_col.to_pylist() + for x in pa_col.to_pylist() ], type=pa.string(), ) @@ -50,3 +73,28 @@ def slice_string(st, start, stop, step): ) assert_column_eq(expected, got) + + +def test_slice_column( + pa_col, plc_col, pa_starts_col, plc_starts_col, pa_stops_col, plc_stops_col +): + def slice_string(st, start, stop): + return st[start:stop] if st is not None else None + + expected = pa.array( + [ + slice_string(x, start, stop) + for x, start, stop in zip( + pa_col.to_pylist(), + pa_starts_col.to_pylist(), + pa_stops_col.to_pylist(), + ) + ], + type=pa.string(), + ) + + got = plc.strings.slice.slice_strings( + plc_col, plc_starts_col, plc_stops_col + ) + + assert_column_eq(expected, got) From d4559085f6c0bbd1ae7ce006746565eaad0274f1 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 11 Jun 2024 18:33:30 -0700 Subject: [PATCH 06/12] add tests --- python/cudf/cudf/pylibcudf_tests/test_string_slice.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py index d94ab8a7f997..cddebe36a1b9 100644 --- a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py +++ b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py @@ -19,10 +19,7 @@ def plc_col(pa_col): @pytest.fixture( scope="module", - params=[ - (1, 3, 1), - (0, 3, -1), - ], + params=[(1, 3, 1), (0, 3, -1), (3, 2, 1), (1, 5, 5), (1, 100, 2)], ) def pa_start_stop_step(request): return tuple(pa.scalar(x, type=pa.int32()) for x in request.param) From 8e08880bb81216d86c3366db30929dca884ad3b3 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 11 Jun 2024 19:29:05 -0700 Subject: [PATCH 07/12] docs --- .../api_docs/pylibcudf/strings/index.rst | 1 + .../api_docs/pylibcudf/strings/slice.rst | 6 +++++ .../cudf/_lib/pylibcudf/strings/slice.pyx | 27 ++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 docs/cudf/source/user_guide/api_docs/pylibcudf/strings/slice.rst diff --git a/docs/cudf/source/user_guide/api_docs/pylibcudf/strings/index.rst b/docs/cudf/source/user_guide/api_docs/pylibcudf/strings/index.rst index bfaef7325556..cecf1ccc9bb5 100644 --- a/docs/cudf/source/user_guide/api_docs/pylibcudf/strings/index.rst +++ b/docs/cudf/source/user_guide/api_docs/pylibcudf/strings/index.rst @@ -6,3 +6,4 @@ strings contains replace + slice diff --git a/docs/cudf/source/user_guide/api_docs/pylibcudf/strings/slice.rst b/docs/cudf/source/user_guide/api_docs/pylibcudf/strings/slice.rst new file mode 100644 index 000000000000..0ee5af71c033 --- /dev/null +++ b/docs/cudf/source/user_guide/api_docs/pylibcudf/strings/slice.rst @@ -0,0 +1,6 @@ +===== +slice +===== + +.. automodule:: cudf._lib.pylibcudf.strings.slice + :members: diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx index bc02100bd07a..46ed1e29c0a7 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx @@ -20,8 +20,33 @@ cpdef Column slice_strings( Column input, ColumnOrScalar start=None, ColumnOrScalar stop=None, - ColumnOrScalar step=None + Scalar step=None ): + """Perform a slice operation on a strings column. + + ``start`` and ``stop`` may be a + :py:class:`~cudf._lib.pylibcudf.column.Column` or a + :py:class:`~cudf._lib.pylibcudf.scalar.Scalar`. But ``step`` must be a + :py:class:`~cudf._lib.pylibcudf.column.Scalar`. + + For details, see :cpp:func:`cudf::strings::slice_strings`. + + Parameters + ---------- + input : Column + Strings column for this operation + start : Union[Column, Scalar] + The start character position or positions. + stop : Union[Column, Scalar] + The end character position or positions + step : Scalar + Distance between input characters retrieved + + Returns + ------- + pylibcudf.Column + The result of the slice operation + """ cdef unique_ptr[column] c_result cdef numeric_scalar[size_type]* cpp_start cdef numeric_scalar[size_type]* cpp_stop From 5d223b89ce50d9c092e99036d5948643392e944e Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 11 Jun 2024 19:32:01 -0700 Subject: [PATCH 08/12] cleanup --- python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd | 2 +- python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd index ef93e97435a5..7d8d0006ef4c 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pxd @@ -11,5 +11,5 @@ cpdef Column slice_strings( Column input, ColumnOrScalar start=*, ColumnOrScalar stop=*, - ColumnOrScalar step=* + Scalar step=* ) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx index 46ed1e29c0a7..f10a890c9ab4 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx @@ -27,7 +27,7 @@ cpdef Column slice_strings( ``start`` and ``stop`` may be a :py:class:`~cudf._lib.pylibcudf.column.Column` or a :py:class:`~cudf._lib.pylibcudf.scalar.Scalar`. But ``step`` must be a - :py:class:`~cudf._lib.pylibcudf.column.Scalar`. + :py:class:`~cudf._lib.pylibcudf.scalar.Scalar`. For details, see :cpp:func:`cudf::strings::slice_strings`. From fb1a0a10cb165257c983e74dbc4db33c9801d95d Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Mon, 17 Jun 2024 11:30:23 -0700 Subject: [PATCH 09/12] address some reviews --- .../cudf/_lib/pylibcudf/strings/slice.pyx | 6 +-- python/cudf/cudf/_lib/strings/substring.pyx | 38 +++++-------------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx index f10a890c9ab4..50f53230608d 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx @@ -65,15 +65,15 @@ cpdef Column slice_strings( elif ColumnOrScalar is Scalar: if start is None: - delimiters = Scalar.from_libcudf( + start = Scalar.from_libcudf( cpp_make_fixed_width_scalar(0) ) if stop is None: - delimiters = Scalar.from_libcudf( + stop = Scalar.from_libcudf( cpp_make_fixed_width_scalar(0) ) if step is None: - delimiters = Scalar.from_libcudf( + step = Scalar.from_libcudf( cpp_make_fixed_width_scalar(1) ) diff --git a/python/cudf/cudf/_lib/strings/substring.pyx b/python/cudf/cudf/_lib/strings/substring.pyx index ccdd4d8623dc..706c21c06345 100644 --- a/python/cudf/cudf/_lib/strings/substring.pyx +++ b/python/cudf/cudf/_lib/strings/substring.pyx @@ -2,22 +2,12 @@ import numpy as np -from libcpp.memory cimport unique_ptr -from libcpp.utility cimport move - from cudf.core.buffer import acquire_spill_lock from cudf._lib.column cimport Column -from cudf._lib.pylibcudf.libcudf.column.column cimport column -from cudf._lib.pylibcudf.libcudf.column.column_view cimport column_view -from cudf._lib.pylibcudf.libcudf.strings.substring cimport ( - slice_strings as cpp_slice_strings, -) -from cudf._lib.pylibcudf.libcudf.types cimport size_type from cudf._lib.scalar import as_device_scalar -from cudf._lib.pylibcudf.libcudf.scalar.scalar cimport numeric_scalar from cudf._lib.scalar cimport DeviceScalar import cudf._lib.pylibcudf as plc @@ -74,8 +64,7 @@ def get(Column source_strings, character from each input string. The index of characters required can be controlled by passing `index`. """ - cdef unique_ptr[column] c_result - cdef column_view source_view = source_strings.view() + if index < 0: next_index = index - 1 step = -1 @@ -86,20 +75,11 @@ def get(Column source_strings, cdef DeviceScalar end_scalar = as_device_scalar(next_index, np.int32) cdef DeviceScalar step_scalar = as_device_scalar(step, np.int32) - cdef numeric_scalar[size_type]* start_numeric_scalar = \ - ( - start_scalar.get_raw_ptr()) - cdef numeric_scalar[size_type]* end_numeric_scalar = \ - (end_scalar.get_raw_ptr()) - cdef numeric_scalar[size_type]* step_numeric_scalar = \ - (step_scalar.get_raw_ptr()) - - with nogil: - c_result = move(cpp_slice_strings( - source_view, - start_numeric_scalar[0], - end_numeric_scalar[0], - step_numeric_scalar[0] - )) - - return Column.from_unique_ptr(move(c_result)) + return Column.from_pylibcudf( + plc.strings.slice.slice_strings( + source_strings.to_pylibcudf(mode="read"), + start_scalar.c_value, + end_scalar.c_value, + step_scalar.c_value + ) + ) From 78fe2672a766630e541913424aacaf82e6ad1196 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Mon, 24 Jun 2024 18:27:59 -0700 Subject: [PATCH 10/12] slice edge case --- python/cudf/cudf/pylibcudf_tests/test_string_slice.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py index cddebe36a1b9..27edfff00368 100644 --- a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py +++ b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py @@ -76,6 +76,8 @@ def test_slice_column( pa_col, plc_col, pa_starts_col, plc_starts_col, pa_stops_col, plc_stops_col ): def slice_string(st, start, stop): + if stop < 0: + stop = len(st) return st[start:stop] if st is not None else None expected = pa.array( From 8dcdb2c6725f53b2f6602fb1dc7a7e2ea243dc32 Mon Sep 17 00:00:00 2001 From: brandon-b-miller <53796099+brandon-b-miller@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:25:52 -0500 Subject: [PATCH 11/12] Apply suggestions from code review Co-authored-by: Lawrence Mitchell --- python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx index 50f53230608d..adaf67f2867d 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx @@ -17,7 +17,7 @@ from cython.operator import dereference cpdef Column slice_strings( - Column input, + Column input not None, ColumnOrScalar start=None, ColumnOrScalar stop=None, Scalar step=None From dd9fdea0a108938e11e4644896fc4708e8fe8819 Mon Sep 17 00:00:00 2001 From: brandon-b-miller Date: Tue, 25 Jun 2024 09:37:36 -0700 Subject: [PATCH 12/12] edge cases, tests --- .../cudf/cudf/_lib/pylibcudf/strings/slice.pyx | 10 +++++++++- .../cudf/pylibcudf_tests/test_string_slice.py | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx index adaf67f2867d..df75134fb716 100644 --- a/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx +++ b/python/cudf/cudf/_lib/pylibcudf/strings/slice.pyx @@ -17,7 +17,7 @@ from cython.operator import dereference cpdef Column slice_strings( - Column input not None, + Column input, ColumnOrScalar start=None, ColumnOrScalar stop=None, Scalar step=None @@ -52,10 +52,18 @@ cpdef Column slice_strings( cdef numeric_scalar[size_type]* cpp_stop cdef numeric_scalar[size_type]* cpp_step + if input is None: + raise ValueError("input cannot be None") + if ColumnOrScalar is Column: if step is not None: raise ValueError("Column-wise slice does not support step") + if start is None or stop is None: + raise ValueError( + "start and stop must be provided for Column-wise slice" + ) + with nogil: c_result = cpp_slice.slice_strings( input.view(), diff --git a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py index 27edfff00368..bd63987b30fd 100644 --- a/python/cudf/cudf/pylibcudf_tests/test_string_slice.py +++ b/python/cudf/cudf/pylibcudf_tests/test_string_slice.py @@ -97,3 +97,20 @@ def slice_string(st, start, stop): ) assert_column_eq(expected, got) + + +def test_slice_invalid(plc_col, plc_starts_col, plc_stops_col): + with pytest.raises(TypeError): + # no maching signature + plc.strings.slice.slice_strings(None, pa_starts_col, pa_stops_col) + with pytest.raises(ValueError): + # signature found but wrong value passed + plc.strings.slice.slice_strings(plc_col, plc_starts_col, None) + with pytest.raises(TypeError): + # no matching signature (2nd arg) + plc.strings.slice.slice_strings(plc_col, None, plc_stops_col) + with pytest.raises(TypeError): + # can't provide step for columnwise api + plc.strings.slice.slice_strings( + plc_col, plc_starts_col, plc_stops_col, plc_starts_col + )