Reranko05 commented on code in PR #50945:
URL: https://github.com/apache/arrow/pull/50945#discussion_r3902940506
##########
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()) {
Review Comment:
I also tried returning -1 immediately when `ConsumeDocument()` fails, but it
causes the same streaming error-propagation tests to fail. I will keep the
current `break` behavior.
--
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]