Skip to content

Commit

Permalink
Fix cudf::strings::findall error with empty input (#16928)
Browse files Browse the repository at this point in the history
Fixes `cudf::strings::findall` error when passed an empty input column.
Also adds a gtest for empty input and for all-rows do not match case.

Authors:
  - David Wendt (https://github.com/davidwendt)

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

URL: #16928
  • Loading branch information
davidwendt authored Sep 26, 2024
1 parent 40075f1 commit fa12901
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cpp/src/strings/search/findall.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cudf/detail/null_mask.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/offsets_iterator_factory.cuh>
#include <cudf/lists/detail/lists_column_factories.hpp>
#include <cudf/strings/detail/strings_column_factories.cuh>
#include <cudf/strings/findall.hpp>
#include <cudf/strings/string_view.cuh>
Expand Down Expand Up @@ -97,8 +98,11 @@ std::unique_ptr<column> findall(strings_column_view const& input,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
auto const strings_count = input.size();
auto const d_strings = column_device_view::create(input.parent(), stream);
if (input.is_empty()) {
return cudf::lists::detail::make_empty_lists_column(input.parent().type(), stream, mr);
}

auto const d_strings = column_device_view::create(input.parent(), stream);

// create device object from regex_program
auto d_prog = regex_device_builder::create_prog_device(prog, stream);
Expand All @@ -113,7 +117,7 @@ std::unique_ptr<column> findall(strings_column_view const& input,
auto strings_output = findall_util(*d_strings, *d_prog, total_matches, d_offsets, stream, mr);

// Build the lists column from the offsets and the strings
return make_lists_column(strings_count,
return make_lists_column(input.size(),
std::move(offsets),
std::move(strings_output),
input.null_count(),
Expand Down
28 changes: 28 additions & 0 deletions cpp/tests/strings/findall_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,31 @@ TEST_F(StringsFindallTests, LargeRegex)
LCW expected({LCW{large_regex.c_str()}, LCW{}, LCW{}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(results->view(), expected);
}

TEST_F(StringsFindallTests, NoMatches)
{
cudf::test::strings_column_wrapper input({"abc\nfff\nabc", "fff\nabc\nlll", "abc", "", "abc\n"});
auto sv = cudf::strings_column_view(input);

auto pattern = std::string("(^zzz$)");
using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
LCW expected({LCW{}, LCW{}, LCW{}, LCW{}, LCW{}});
auto prog = cudf::strings::regex_program::create(pattern);
auto results = cudf::strings::findall(sv, *prog);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(results->view(), expected);
}

TEST_F(StringsFindallTests, EmptyTest)
{
std::string pattern = R"(\w+)";

auto prog = cudf::strings::regex_program::create(pattern);

cudf::test::strings_column_wrapper input;
auto sv = cudf::strings_column_view(input);
auto results = cudf::strings::findall(sv, *prog);

using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
LCW expected;
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(results->view(), expected);
}

0 comments on commit fa12901

Please sign in to comment.