GH-50944: [C++] Replace RapidJSON with simdjson in JSON chunker - #50945
GH-50944: [C++] Replace RapidJSON with simdjson in JSON chunker#50945Reranko05 wants to merge 5 commits into
Conversation
e9fc9fe to
be4c2a1
Compare
pitrou
left a comment
There was a problem hiding this comment.
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.
|
|
||
| namespace arrow { | ||
|
|
||
| using std::string_view; |
There was a problem hiding this comment.
Can we remove this if it's not useful anymore?
| if (escape_next) { | ||
| escape_next = false; | ||
| continue; | ||
| } | ||
|
|
||
| if (c == '\\' && in_string) { | ||
| escape_next = true; | ||
| continue; | ||
| } |
There was a problem hiding this comment.
This does not account for unicode escapes. Do we have to parse JSON by hand like this?
| std::string combined; | ||
| combined.reserve(partial.size() + block.size()); | ||
| combined.append(partial); | ||
| combined.append(block); |
There was a problem hiding this comment.
We should only concatenate if both substrings are non-empty.
| return Status::Invalid("JSON chunk error: invalid data at end of document"); | ||
| return Status::Invalid("JSON parse error: Invalid value"); |
| 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"); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Please add comments explaining what this does and why it is necessary.
I initially tried using That said, I agree that parsing JSON manually here is not ideal. I'll revisit this using |
|
@pitrou I tried using Manual structural parsing approach preserved the existing test behavior. @rok, since your earlier implementation was helpful here, do you have any suggestions on how to preserve the current error behavior with |
As long as an error is reported while reading the JSON stream, I don't think we care if it's reported by the chunker or the parser. |
|
@pitrou I gave |
Hmm, I see. Thanks for trying anyway :-)
Well, as a last resort, yes. The problem:
|
|
Try to understand the issue. Is it that json strings legal for rapidjson may fail on simdjson, makes future Arrow release potentially incompatible to old version? Writing our own optimized version looks not ideal. Can we just use simdjson? It's state-of-the-art, and even with self written object delimiter, there's still incompatibility risk I'm afraid. |
|
@cyb70289 I don't think the issue is JSON compatibility between RapidJSON and simdjson. The main issue I ran into is the boundary/streaming semantics. The previous RapidJSON implementation uses With I agree that writing our own optimized JSON parser would not be ideal. The manual approach I used, and which @rok also implemented in rok#47, only scans for the boundary of the first complete object or array while respecting strings and escapes, and then lets simdjson perform the actual JSON validation. But I agree this still introduces complexity and needs careful testing. If there is a way to use simdjson directly while preserving the old stop-after-one-value semantics, that would definitely be preferable. |
Is that a problem? We want to keep compatibility when parsing valid JSON streams. The failure mode for an invalid JSON stream can change. |
|
A discussion about ignoring trailing garbage in simdjson. Looks there're real use cases lenient parsing can be useful. |
|
Trailing garbage is not the problem here. We are parsing a stream of valid JSON documents. We are happy to error out on trailing garbage. |
227961f to
3f15314
Compare
626bb0d to
b9af8ce
Compare
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
simdjson::dom::parser.Fixes: #50944