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

Use cudf::distinct in Python binding #11230

Closed
wants to merge 7 commits into from
Closed
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
6 changes: 0 additions & 6 deletions python/cudf/cudf/_lib/cpp/sorting.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ cdef extern from "cudf/sorting.hpp" namespace "cudf" nogil:
vector[libcudf_types.order] column_order,
vector[libcudf_types.null_order] null_precedence) except +

cdef unique_ptr[table] stable_sort_by_key(
const table_view& values,
const table_view& keys,
vector[libcudf_types.order] column_order,
vector[libcudf_types.null_order] null_precedence) except +

cdef unique_ptr[column] rank(
column_view input_view,
rank_method method,
Expand Down
3 changes: 2 additions & 1 deletion python/cudf/cudf/_lib/cpp/stream_compaction.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ from cudf._lib.cpp.types cimport (
cdef extern from "cudf/stream_compaction.hpp" namespace "cudf" \
nogil:
ctypedef enum duplicate_keep_option:
KEEP_ANY 'cudf::duplicate_keep_option::KEEP_ANY'
KEEP_FIRST 'cudf::duplicate_keep_option::KEEP_FIRST'
KEEP_LAST 'cudf::duplicate_keep_option::KEEP_LAST'
KEEP_NONE 'cudf::duplicate_keep_option::KEEP_NONE'
Expand All @@ -33,7 +34,7 @@ cdef extern from "cudf/stream_compaction.hpp" namespace "cudf" \
column_view boolean_mask
) except +

cdef unique_ptr[table] unique(
cdef unique_ptr[table] distinct(
table_view source_table,
vector[size_type] keys,
duplicate_keep_option keep,
Expand Down
36 changes: 3 additions & 33 deletions python/cudf/cudf/_lib/stream_compaction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@ from libcpp.vector cimport vector

from cudf._lib.column cimport Column
from cudf._lib.cpp.column.column_view cimport column_view
from cudf._lib.cpp.sorting cimport stable_sort_by_key as cpp_stable_sort_by_key
from cudf._lib.cpp.stream_compaction cimport (
apply_boolean_mask as cpp_apply_boolean_mask,
distinct as cpp_distinct,
distinct_count as cpp_distinct_count,
drop_nulls as cpp_drop_nulls,
duplicate_keep_option,
unique as cpp_unique,
)
from cudf._lib.cpp.table.table cimport table
from cudf._lib.cpp.table.table_view cimport table_view
from cudf._lib.cpp.types cimport (
nan_policy,
null_equality,
null_order,
null_policy,
order,
size_type,
)
from cudf._lib.utils cimport (
Expand Down Expand Up @@ -148,40 +145,13 @@ def drop_duplicates(list columns,
else null_equality.UNEQUAL
)

cdef vector[order] column_order = (
vector[order](
cpp_keys.size(),
order.ASCENDING
)
)
cdef vector[null_order] null_precedence = (
vector[null_order](
cpp_keys.size(),
null_order.BEFORE
)
)

cdef table_view source_table_view = table_view_from_columns(columns)
cdef table_view keys_view = source_table_view.select(cpp_keys)
cdef unique_ptr[table] sorted_source_table
cdef unique_ptr[table] c_result

with nogil:
# cudf::unique keeps unique rows in each consecutive group of
# equivalent rows. To match the behavior of pandas.DataFrame.
# drop_duplicates, users need to stable sort the input first
# and then invoke cudf::unique.
sorted_source_table = move(
cpp_stable_sort_by_key(
source_table_view,
keys_view,
column_order,
null_precedence
)
)
c_result = move(
cpp_unique(
sorted_source_table.get().view(),
cpp_distinct(
source_table_view,
cpp_keys,
cpp_keep_option,
cpp_nulls_equal
Expand Down