Skip to content

Commit

Permalink
Defer to C++ equality and hashing for pylibcudf DataType and Aggregat…
Browse files Browse the repository at this point in the history
…ion objects (#15732)

Since the C++ layer provides implementations of these, use them, rather than redoing an implementation. This avoids things ever getting out of sync.

Authors:
  - Lawrence Mitchell (https://github.com/wence-)

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #15732
  • Loading branch information
wence- authored May 16, 2024
1 parent 4e87069 commit 0a544c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
8 changes: 8 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/aggregation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ cdef class Aggregation:
"Aggregations should not be constructed directly. Use one of the factories."
)

def __eq__(self, other):
return type(self) is type(other) and (
dereference(self.c_obj).is_equal(dereference((<Aggregation>other).c_obj))
)

def __hash__(self):
return dereference(self.c_obj).do_hash()

# TODO: Ideally we would include the return type here, but we need to do so
# in a way that Sphinx understands (currently have issues due to
# https://github.com/cython/cython/issues/5609).
Expand Down
3 changes: 3 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/libcudf/aggregation.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
from libc.stddef cimport size_t
from libc.stdint cimport int32_t
from libcpp cimport bool
from libcpp.memory cimport unique_ptr
Expand Down Expand Up @@ -51,6 +52,8 @@ cdef extern from "cudf/aggregation.hpp" namespace "cudf" nogil:
cdef cppclass aggregation:
Kind kind
unique_ptr[aggregation] clone()
size_t do_hash() noexcept
bool is_equal(const aggregation const) noexcept

cdef cppclass rolling_aggregation(aggregation):
pass
Expand Down
5 changes: 3 additions & 2 deletions python/cudf/cudf/_lib/pylibcudf/libcudf/types.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ cdef extern from "cudf/types.hpp" namespace "cudf" nogil:
data_type(const data_type&) except +
data_type(type_id id) except +
data_type(type_id id, int32_t scale) except +
type_id id() except +
int32_t scale() except +
type_id id() noexcept
int32_t scale() noexcept
bool operator==(const data_type&, const data_type&) noexcept

cpdef enum class interpolation(int32_t):
LINEAR
Expand Down
6 changes: 3 additions & 3 deletions python/cudf/cudf/_lib/pylibcudf/types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ cdef class DataType:
return self.c_obj.scale()

def __eq__(self, other):
if not isinstance(other, DataType):
return False
return self.id() == other.id() and self.scale() == other.scale()
return type(self) is type(other) and (
self.c_obj == (<DataType>other).c_obj
)

@staticmethod
cdef DataType from_libcudf(data_type dt):
Expand Down

0 comments on commit 0a544c2

Please sign in to comment.