From 4505c5399a7aea119e07dded7b54084be713e985 Mon Sep 17 00:00:00 2001 From: David Wendt <45795991+davidwendt@users.noreply.github.com> Date: Wed, 4 Dec 2024 13:52:38 -0500 Subject: [PATCH] Return empty result for segmented_reduce if input and offsets are both empty (#17437) Changes the behavior of `cudf::segmented_reduce` to return an empty column if both the input and the offsets parameter are empty. Closes #17433 Authors: - David Wendt (https://github.com/davidwendt) Approvers: - Karthikeyan (https://github.com/karthikeyann) - Bradley Dice (https://github.com/bdice) - Basit Ayantunde (https://github.com/lamarrr) - Nghia Truong (https://github.com/ttnghia) URL: https://github.com/rapidsai/cudf/pull/17437 --- cpp/src/reductions/segmented/reductions.cpp | 6 ++++++ .../reductions/segmented_reduction_tests.cpp | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/cpp/src/reductions/segmented/reductions.cpp b/cpp/src/reductions/segmented/reductions.cpp index dedfc4b0734..1c3a2b0c0f3 100644 --- a/cpp/src/reductions/segmented/reductions.cpp +++ b/cpp/src/reductions/segmented/reductions.cpp @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include #include @@ -120,6 +121,11 @@ std::unique_ptr segmented_reduce(column_view const& segmented_values, CUDF_FAIL( "Initial value is only supported for SUM, PRODUCT, MIN, MAX, ANY, and ALL aggregation types"); } + + if (segmented_values.is_empty() && offsets.empty()) { + return cudf::make_empty_column(output_dtype); + } + CUDF_EXPECTS(offsets.size() > 0, "`offsets` should have at least 1 element."); return cudf::detail::aggregation_dispatcher( diff --git a/cpp/tests/reductions/segmented_reduction_tests.cpp b/cpp/tests/reductions/segmented_reduction_tests.cpp index bc0321bd40a..2281a517aa6 100644 --- a/cpp/tests/reductions/segmented_reduction_tests.cpp +++ b/cpp/tests/reductions/segmented_reduction_tests.cpp @@ -1122,6 +1122,26 @@ TEST_F(SegmentedReductionTestUntyped, EmptyInputWithOffsets) CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expect_bool); } +TEST_F(SegmentedReductionTestUntyped, EmptyInputEmptyOffsets) +{ + auto const str_empty = cudf::test::strings_column_wrapper{}; + auto const int_empty = cudf::test::fixed_width_column_wrapper{}; + auto result = + cudf::segmented_reduce(str_empty, + cudf::column_view{int_empty}, + *cudf::make_max_aggregation(), + cudf::data_type{cudf::type_id::STRING}, + cudf::null_policy::EXCLUDE); + CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, str_empty); + + result = cudf::segmented_reduce(int_empty, + cudf::column_view{int_empty}, + *cudf::make_min_aggregation(), + cudf::data_type{cudf::type_id::INT32}, + cudf::null_policy::INCLUDE); + CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, int_empty); +} + template struct SegmentedReductionFixedPointTest : public cudf::test::BaseFixture {};