Skip to content

Commit

Permalink
Return empty result for segmented_reduce if input and offsets are bot…
Browse files Browse the repository at this point in the history
…h 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: #17437
  • Loading branch information
davidwendt authored Dec 4, 2024
1 parent 43fac3b commit 4505c53
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cpp/src/reductions/segmented/reductions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/column/column_factories.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/reduction.hpp>
Expand Down Expand Up @@ -120,6 +121,11 @@ std::unique_ptr<column> 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(
Expand Down
20 changes: 20 additions & 0 deletions cpp/tests/reductions/segmented_reduction_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cudf::size_type>{};
auto result =
cudf::segmented_reduce(str_empty,
cudf::column_view{int_empty},
*cudf::make_max_aggregation<cudf::segmented_reduce_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::segmented_reduce_aggregation>(),
cudf::data_type{cudf::type_id::INT32},
cudf::null_policy::INCLUDE);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, int_empty);
}

template <typename T>
struct SegmentedReductionFixedPointTest : public cudf::test::BaseFixture {};

Expand Down

0 comments on commit 4505c53

Please sign in to comment.