rok commented on code in PR #55771:
URL: https://github.com/apache/arrow/pull/55771#discussion_r4758159179


##########
cpp/src/arrow/compute/kernels/scalar_string_ascii.cc:
##########
@@ -2958,7 +2958,7 @@ struct SplitWhitespaceAsciiFinder : public 
StringSplitFinderBase<SplitOptions> {
         *separator_end = i + 1;
         do {
           i--;
-        } while (IsSpaceCharacterAscii(*i) && i >= begin);
+        } while (i >= begin && IsSpaceCharacterAscii(*i));

Review Comment:
   `FindReverse` (here and in `SplitWhitespaceUtf8Finder::FindReverse`) 
initializes `const uint8_t* i = end - 1; while (i >= begin)`. If an 
all-empty-string array has a `nullptr` data buffer, `end - 1` wraps around.
   
   A single guard in `StringSplitExec::SplitString` 
(`cpp/src/arrow/compute/kernels/scalar_string_internal.h`, line 384) avoids 
calling `FindReverse` on an empty range for all reverse splitters (and skips 
the redundant call after matching a leading separator):
   ```cpp
   while (max_splits != 0 && i > begin) {
   ```
   Could you add it to this PR?



##########
cpp/src/arrow/compute/kernels/scalar_string_test.cc:
##########
@@ -2011,6 +2040,48 @@ TYPED_TEST(TestStringKernels, 
SplitWhitespaceUTF8Reverse) {
                    "[[\"foo\", \"bar\"], [\"foo\xe2\x80\x88  bar\", \"ba\"]]",
                    &options_max);
 }
+
+TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8LeadingTrailing) {
+  SplitOptions options;
+  SplitOptions options_max{1};
+  SplitOptions options_reverse{-1, /*reverse=*/true};
+  SplitOptions options_reverse_max{1, /*reverse=*/true};
+  // \xe2\x80\x88 is punctuation space
+  const char* input =
+      "[\"foo  \", \"  foo\", \"  foo  \", \" \", \"   \", \"\", "
+      "\"foo\xe2\x80\x88\xe2\x80\x88\", \"\xe2\x80\x88 foo\"]";
+  this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
+                   R"([["foo", ""], ["", "foo"], ["", "foo", ""], ["", ""], 
["", ""],
+                       [""], ["foo", ""], ["", "foo"]])",
+                   &options);
+  this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
+                   R"([["foo", ""], ["", "foo"], ["", "foo  "], ["", ""], ["", 
""],
+                       [""], ["foo", ""], ["", "foo"]])",
+                   &options_max);
+  this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
+                   R"([["foo", ""], ["", "foo"], ["", "foo", ""], ["", ""], 
["", ""],
+                       [""], ["foo", ""], ["", "foo"]])",
+                   &options_reverse);
+  this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
+                   R"([["foo", ""], ["", "foo"], ["  foo", ""], ["", ""], ["", 
""],
+                       [""], ["foo", ""], ["", "foo"]])",
+                   &options_reverse_max);
+}
+
+TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8StaysInBounds) {
+  SplitOptions options;
+  SplitOptions options_reverse{-1, /*reverse=*/true};
+  auto input = MakeArrayInExactBuffer<TypeParam>({"  foo  ", "  bar  "}, 
"\xff", "\xff");
+  this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
+                   R"([["", "foo", ""], ["", "bar", ""]])", &options);
+  this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
+                   R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse);
+  input = MakeArrayInExactBuffer<TypeParam>({"  foo  ", "  bar  "});

Review Comment:
   Optional nit: covering a single multi-byte whitespace at the exact buffer 
boundary doesn't require changing the expected output:
   ```suggestion
     input = MakeArrayInExactBuffer<TypeParam>({"  foo  ", 
"\xe2\x80\x88bar\xe2\x80\x88"});
   ```



##########
cpp/src/arrow/compute/kernels/scalar_string_test.cc:
##########
@@ -1986,6 +2005,16 @@ TYPED_TEST(TestStringKernels, 
SplitWhitespaceAsciiReverse) {
                    &options_max);
 }
 
+TYPED_TEST(TestStringKernels, SplitWhitespaceAsciiStaysInBounds) {
+  SplitOptions options;
+  SplitOptions options_reverse{-1, /*reverse=*/true};
+  auto input = MakeArrayInExactBuffer<TypeParam>({"  foo  ", "  bar  "});
+  this->CheckUnary("ascii_split_whitespace", input, list(this->type()),
+                   R"([["", "foo", ""], ["", "bar", ""]])", &options);
+  this->CheckUnary("ascii_split_whitespace", input, list(this->type()),
+                   R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse);

Review Comment:
   Optional nit: we could also include whitespace-only and empty string cases 
here:
   ```suggestion
     auto input = MakeArrayInExactBuffer<TypeParam>({"  foo  ", " ", "", "  bar 
 "});
     this->CheckUnary("ascii_split_whitespace", input, list(this->type()),
                      R"([["", "foo", ""], ["", ""], [""], ["", "bar", ""]])", 
&options);
     this->CheckUnary("ascii_split_whitespace", input, list(this->type()),
                      R"([["", "foo", ""], ["", ""], [""], ["", "bar", ""]])",
                      &options_reverse);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to