diff --git a/cpp/src/reductions/segmented/reductions.cpp b/cpp/src/reductions/segmented/reductions.cpp index c4f6c135dde..4843d95346f 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 @@ -118,6 +119,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 {};