From c0089bb941ebe1a3c8dfd889b146cf281b4b4c7d Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Tue, 16 Apr 2024 16:04:51 +0000 Subject: [PATCH] Implement __eq__ and __hash__ for Aggregation objects Use the matching C++ implementations for OAOO. --- python/cudf/cudf/_lib/cpp/aggregation.pxd | 3 +++ python/cudf/cudf/_lib/pylibcudf/aggregation.pyx | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/python/cudf/cudf/_lib/cpp/aggregation.pxd b/python/cudf/cudf/_lib/cpp/aggregation.pxd index 91b9d7d024f..1e2ced926d0 100644 --- a/python/cudf/cudf/_lib/cpp/aggregation.pxd +++ b/python/cudf/cudf/_lib/cpp/aggregation.pxd @@ -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 @@ -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 diff --git a/python/cudf/cudf/_lib/pylibcudf/aggregation.pyx b/python/cudf/cudf/_lib/pylibcudf/aggregation.pyx index fe7daea38bf..3ef186b6fba 100644 --- a/python/cudf/cudf/_lib/pylibcudf/aggregation.pyx +++ b/python/cudf/cudf/_lib/pylibcudf/aggregation.pyx @@ -77,6 +77,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((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).