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 pylibcudf lists gathering #16170

Merged
merged 7 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions python/cudf/cudf/_lib/pylibcudf/libcudf/lists/gather.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ from cudf._lib.pylibcudf.libcudf.lists.lists_column_view cimport (

cdef extern from "cudf/lists/gather.hpp" namespace "cudf::lists" nogil:
cdef unique_ptr[column] segmented_gather(
const lists_column_view source_column,
const lists_column_view gather_map_list
const lists_column_view& source_column,
const lists_column_view& gather_map_list
) 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 @@ -25,3 +25,5 @@ cpdef Column contains_nulls(Column)
cpdef Column index_of(Column, ColumnOrScalar, bool)

cpdef Column reverse(Column)

cpdef Column segmented_gather(Column, Column)
32 changes: 32 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/lists.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from cudf._lib.pylibcudf.libcudf.column.column cimport column
from cudf._lib.pylibcudf.libcudf.lists cimport (
contains as cpp_contains,
explode as cpp_explode,
gather as cpp_gather,
reverse as cpp_reverse,
)
from cudf._lib.pylibcudf.libcudf.lists.combine cimport (
Expand Down Expand Up @@ -232,3 +233,34 @@ cpdef Column reverse(Column input):
list_view.view(),
))
return Column.from_libcudf(move(c_result))


cpdef Column segmented_gather(Column input, Column gather_map_list):
"""Create a column with elements gathered based on the indices in gather_map_list

For details, see :cpp:func:`segmented_gather`.

Parameters
----------
input : Column
The input column.
gather_map_list : Column
The indices of the lists column to gather.

Returns
-------
Column
A new Column with elements in list of rows
gathered based on gather_map_list
"""

cdef unique_ptr[column] c_result
cdef ListColumnView list_view1 = input.list_view()
cdef ListColumnView list_view2 = gather_map_list.list_view()

with nogil:
c_result = move(cpp_gather.segmented_gather(
list_view1.view(),
list_view2.view(),
))
return Column.from_libcudf(move(c_result))
14 changes: 14 additions & 0 deletions python/cudf/cudf/pylibcudf_tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,17 @@ def test_reverse(test_data):
expect = pa.array([lst[::-1] for lst in list_column])

assert_column_eq(expect, res)


def test_segmented_gather(test_data):
list_column1 = test_data[0][0]
list_column2 = test_data[0][1]

plc_column1 = plc.interop.from_arrow(pa.array(list_column1))
plc_column2 = plc.interop.from_arrow(pa.array(list_column2))

res = plc.lists.segmented_gather(plc_column2, plc_column1)

expect = pa.array([[8, 9], [14], [0], [0, 0]])

assert_column_eq(expect, res)
Loading