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

Migrate lists/count_elements to pylibcudf #16072

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 3 additions & 15 deletions python/cudf/cudf/_lib/lists.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ from libcpp.utility cimport move
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.lists.count_elements cimport (
count_elements as cpp_count_elements,
)
from cudf._lib.pylibcudf.libcudf.lists.extract cimport extract_list_element
from cudf._lib.pylibcudf.libcudf.lists.lists_column_view cimport (
lists_column_view,
Expand All @@ -38,19 +35,10 @@ from cudf._lib.pylibcudf cimport Scalar

@acquire_spill_lock()
def count_elements(Column col):

# shared_ptr required because lists_column_view has no default
# ctor
cdef shared_ptr[lists_column_view] list_view = (
make_shared[lists_column_view](col.view())
return Column.from_pylibcudf(
pylibcudf.lists.count_elements(
col.to_pylibcudf(mode="read"))
)
cdef unique_ptr[column] c_result

with nogil:
c_result = move(cpp_count_elements(list_view.get()[0]))

result = Column.from_unique_ptr(move(c_result))
return result


@acquire_spill_lock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ from cudf._lib.pylibcudf.libcudf.lists.lists_column_view cimport (


cdef extern from "cudf/lists/count_elements.hpp" namespace "cudf::lists" nogil:
cdef unique_ptr[column] count_elements(const lists_column_view) except +
cdef unique_ptr[column] count_elements(const lists_column_view&) except +
2 changes: 2 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/lists.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ cpdef Column index_of(Column, ColumnOrScalar, bool)
cpdef Column reverse(Column)

cpdef Column segmented_gather(Column, Column)

cpdef Column count_elements(Column)
27 changes: 27 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/lists.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ from cudf._lib.pylibcudf.libcudf.lists.combine cimport (
concatenate_null_policy,
concatenate_rows as cpp_concatenate_rows,
)
from cudf._lib.pylibcudf.libcudf.lists.count_elements cimport (
count_elements as cpp_count_elements,
)
from cudf._lib.pylibcudf.libcudf.table.table cimport table
from cudf._lib.pylibcudf.libcudf.types cimport size_type
from cudf._lib.pylibcudf.lists cimport ColumnOrScalar
Expand Down Expand Up @@ -264,3 +267,27 @@ cpdef Column segmented_gather(Column input, Column gather_map_list):
list_view2.view(),
))
return Column.from_libcudf(move(c_result))


cpdef Column count_elements(Column input):
"""Count the number of rows in each
list element in the given lists column.
For details, see :cpp:func:`count_elements`.

Parameters
----------
input : Column
The input column

Returns
-------
Column
A new Column of the lengths of each list element
"""
cdef ListColumnView list_view = input.list_view()
cdef unique_ptr[column] c_result

with nogil:
c_result = move(cpp_count_elements(list_view.view()))

return Column.from_libcudf(move(c_result))
10 changes: 10 additions & 0 deletions python/cudf/cudf/pylibcudf_tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,13 @@ def test_segmented_gather(test_data):
expect = pa.array([[8, 9], [14], [0], [0, 0]])

assert_column_eq(expect, res)


def test_count_elements(test_data):
arr = pa.array(test_data[0][1])
plc_column = plc.interop.from_arrow(arr)
res = plc.lists.count_elements(plc_column)

expect = pa.array([1, 1, 0, 3], type=pa.int32())

assert_column_eq(expect, res)
Loading