Reranko05 commented on code in PR #50945:
URL: https://github.com/apache/arrow/pull/50945#discussion_r3903071100


##########
cpp/src/arrow/json/chunker.cc:
##########
@@ -124,40 +86,114 @@ namespace {
 // and uses actual JSON parsing to delimit them.
 class ParsingBoundaryFinder : public BoundaryFinder {
  public:
-  Status FindFirst(string_view partial, string_view block, int64_t* out_pos) 
override {
-    auto length = ConsumeWholeObject(MultiStringStream({partial, block}));
-    if (length == string_view::npos) {
+  Status FindFirst(std::string_view partial, std::string_view block,
+                   int64_t* out_pos) override {
+    simdjson::padded_string input;
+
+    if (partial.empty()) {
+      input = simdjson::padded_string(block);
+    } else if (block.empty()) {
+      input = simdjson::padded_string(partial);
+    } else {
+      simdjson::padded_string_builder builder(partial.size() + block.size());
+      builder.append(partial);
+      builder.append(block);
+      input = builder.convert();
+    }
+
+    const std::string_view input_view(input.data(), input.size());
+    const size_t start = ConsumeWhitespace(input_view);
+    if (start < input_view.size() && input_view[start] != '{' &&
+        input_view[start] != '[') {
+      return Status::Invalid("JSON chunk error: invalid data at end of 
document");
+    }
+
+    const auto length = ConsumeWholeObject(input);
+
+    if (length == std::string_view::npos) {
       *out_pos = -1;
     } else if (ARROW_PREDICT_FALSE(length < partial.size())) {
       return Status::Invalid("JSON chunk error: invalid data at end of 
document");
     } else {
       DCHECK_LE(length, partial.size() + block.size());
       *out_pos = static_cast<int64_t>(length - partial.size());
     }
+
     return Status::OK();
   }
 
   Status FindLast(std::string_view block, int64_t* out_pos) override {
     const size_t block_length = block.size();
     size_t consumed_length = 0;
-    while (consumed_length < block_length) {
-      rj::MemoryStream ms(reinterpret_cast<const char*>(block.data()), 
block.size());
-      using InputStream = rj::EncodedInputStream<rj::UTF8<>, rj::MemoryStream>;
-      auto length = ConsumeWholeObject(InputStream(ms));
-      if (length == string_view::npos || length == 0) {
-        // found incomplete object or block is empty
+
+    // Keep the padded buffer alive while iterating the document stream.
+    simdjson::padded_string padded(block);
+    simdjson::ondemand::parser parser;
+    simdjson::ondemand::document_stream stream;
+
+    if (parser.iterate_many(padded).get(stream) != simdjson::SUCCESS) {
+      *out_pos = -1;
+      return Status::OK();
+    }
+
+    auto it = stream.begin();
+    if (it == stream.end()) {
+      *out_pos = -1;
+      return Status::OK();
+    }
+
+    while (it != stream.end()) {
+      if (!ConsumeDocument(it).ok()) {
         break;
       }
-      consumed_length += length;
-      block = block.substr(length);
+
+      consumed_length = it.current_index() + it.source().size();
+      ++it;
     }
+
     if (consumed_length == 0) {
+      const size_t start = ConsumeWhitespace(block);
+
+      if (start < block.size()) {
+        const char first_char = block[start];
+
+        // An incomplete object/array is valid here because it may continue
+        // in the next block. However, non-object/array data cannot start a
+        // JSON record, except for a lone closing delimiter which may be the
+        // remainder of an incomplete value.
+        if (first_char != '{' && first_char != '[') {
+          const size_t remaining_len = block.size() - start;
+
+          if (remaining_len > 1 || (first_char != '}' && first_char != ']')) {
+            return Status::Invalid("JSON parse error: Invalid value");
+          }
+        }
+      }
+
       *out_pos = -1;
     } else {
-      consumed_length += ConsumeWhitespace(block);
+      // Check the suffix after the last complete document. This is the part

Review Comment:
   Same here, removing this check caused the streaming tests to fail



-- 
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