Skip to content

GH-50944: [C++] Replace RapidJSON with simdjson in JSON chunker - #50945

Open
Reranko05 wants to merge 2 commits into
apache:mainfrom
Reranko05:gh-35460-chunker
Open

GH-50944: [C++] Replace RapidJSON with simdjson in JSON chunker#50945
Reranko05 wants to merge 2 commits into
apache:mainfrom
Reranko05:gh-35460-chunker

Conversation

@Reranko05

@Reranko05 Reranko05 commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Rationale for this change

This PR continues the simdjson migration by replacing the RapidJSON-based JSON boundary detection used by the JSON chunker.

The existing implementation uses RapidJSON's streaming parser to identify complete JSON values. This change replaces that logic with structural boundary detection and simdjson validation.

Changes

  • Replace the RapidJSON custom stream and boundary detection.
  • Detect complete JSON values with structural scanning.
  • Validate complete candidates with simdjson::dom::parser.
  • Preserve incomplete-value, block-boundary, and error handling behavior.
  • Update the affected error expectation.

Fixes: #50944

@Reranko05 Reranko05 added CI: Extra: C++ Run extra C++ CI and removed awaiting review Awaiting review labels Aug 21, 2026
@Reranko05

Copy link
Copy Markdown
Collaborator Author

While working on this migration, @rok's earlier implementation rok#47 of the simdjson-based JSON chunker. It was very helpful. Thanks :)

@Reranko05
Reranko05 marked this pull request as ready for review August 21, 2026 16:33
@Reranko05
Reranko05 requested a review from pitrou as a code owner August 21, 2026 16:33
Copilot AI lite review requested due to automatic review settings August 21, 2026 16:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Reranko05
Reranko05 requested review from kou and rok August 21, 2026 16:33
@github-actions github-actions Bot added the awaiting review Awaiting review label Aug 21, 2026

@pitrou pitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is parsing JSON by hand?

It seems that we might be able to use simdjson::ondemand::parser::iterate_many.

Comment thread cpp/src/arrow/json/chunker.cc Outdated

namespace arrow {

using std::string_view;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this if it's not useful anymore?

Comment on lines +64 to +72
if (escape_next) {
escape_next = false;
continue;
}

if (c == '\\' && in_string) {
escape_next = true;
continue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not account for unicode escapes. Do we have to parse JSON by hand like this?

Comment on lines +120 to +123
std::string combined;
combined.reserve(partial.size() + block.size());
combined.append(partial);
combined.append(block);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only concatenate if both substrings are non-empty.

Comment on lines -132 to +135
return Status::Invalid("JSON chunk error: invalid data at end of document");
return Status::Invalid("JSON parse error: Invalid value");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keep the error message?

Comment on lines +159 to +171
const size_t start = ConsumeWhitespace(block);

if (start < block.size()) {
const char first_char = block[start];

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");
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments explaining what this does and why it is necessary.

@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Aug 24, 2026
@Reranko05

Copy link
Copy Markdown
Collaborator Author

I don't understand why this is parsing JSON by hand?
It seems that we might be able to use simdjson::ondemand::parser::iterate_many.

I initially tried using simdjson::ondemand::parser::iterate_many, but after a few attempts I ran into boundary-handling issues with the chunker semantics. I then referred to an earlier implementation by @rok as a reference and followed that approach.

That said, I agree that parsing JSON manually here is not ideal. I'll revisit this using iterate_many and address the other review comments as well.

Copilot AI review requested due to automatic review settings August 24, 2026 20:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting committer review Awaiting committer review CI: Extra: C++ Run extra C++ CI Component: C++

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] Replace RapidJSON with simdjson in JSON chunker

3 participants