-
Notifications
You must be signed in to change notification settings - Fork 4.3k
GH-51013: [C++] Replace RapidJSON in JSON test utilities #51014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Reranko05
wants to merge
5
commits into
apache:main
Choose a base branch
from
Reranko05:gh-35460-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |||||||||||
|
|
||||||||||||
| #include "arrow/io/interfaces.h" | ||||||||||||
| #include "arrow/io/slow.h" | ||||||||||||
| #include "arrow/json/json_writer_internal.h" | ||||||||||||
| #include "arrow/json/options.h" | ||||||||||||
| #include "arrow/json/reader.h" | ||||||||||||
| #include "arrow/json/test_common.h" | ||||||||||||
|
|
@@ -550,10 +551,14 @@ class StreamingReaderTestBase { | |||||||||||
| auto options = GenerateOptions::Defaults(); | ||||||||||||
| options.null_probability = 0; | ||||||||||||
| for (int i = 0; i < num_rows; ++i) { | ||||||||||||
| StringBuffer string_buffer; | ||||||||||||
| Writer writer(string_buffer); | ||||||||||||
| Writer writer; | ||||||||||||
| ABORT_NOT_OK(Generate(data_fields, engine, &writer, options)); | ||||||||||||
| std::string json = string_buffer.GetString(); | ||||||||||||
|
|
||||||||||||
| auto json_result = writer.GetString(); | ||||||||||||
| ABORT_NOT_OK(json_result.status()); | ||||||||||||
| auto json_view = std::move(json_result).ValueOrDie(); | ||||||||||||
| std::string json(json_view); | ||||||||||||
|
Comment on lines
+557
to
+560
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can write this more concisely as:
Suggested change
|
||||||||||||
|
|
||||||||||||
| rows[i] = Join({"{\"i\":", std::to_string(i), ",\"d\":", json, "}\n"}); | ||||||||||||
| max_row_size = std::max(max_row_size, rows[i].size()); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,35 +25,31 @@ | |
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <simdjson.h> | ||
|
|
||
| #include "arrow/array.h" | ||
| #include "arrow/array/builder_binary.h" | ||
| #include "arrow/io/memory.h" | ||
| #include "arrow/json/converter.h" | ||
| #include "arrow/json/json_writer_internal.h" | ||
| #include "arrow/json/options.h" | ||
| #include "arrow/json/parser.h" | ||
| #include "arrow/json/rapidjson_defs.h" | ||
| #include "arrow/result.h" | ||
| #include "arrow/testing/gtest_util.h" | ||
| #include "arrow/testing/random.h" | ||
| #include "arrow/type.h" | ||
| #include "arrow/util/checked_cast.h" | ||
| #include "arrow/util/simdjson_internal.h" | ||
| #include "arrow/visit_type_inline.h" | ||
|
|
||
| #include "rapidjson/document.h" | ||
| #include "rapidjson/prettywriter.h" | ||
| #include "rapidjson/reader.h" | ||
| #include "rapidjson/writer.h" | ||
|
|
||
| namespace arrow { | ||
|
|
||
| using internal::checked_cast; | ||
|
|
||
| namespace json { | ||
|
|
||
| namespace rj = arrow::rapidjson; | ||
|
|
||
| using rj::StringBuffer; | ||
| using std::string_view; | ||
| using Writer = rj::Writer<StringBuffer>; | ||
| using Writer = JsonWriter; | ||
|
|
||
| struct GenerateOptions { | ||
| // Probability of a field being written | ||
|
|
@@ -87,35 +83,43 @@ inline static Status Generate( | |
|
|
||
| template <typename Engine> | ||
| struct GenerateImpl { | ||
| Status Visit(const NullType&) { return OK(writer.Null()); } | ||
| Status Visit(const NullType&) { | ||
| writer.Null(); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status Visit(const BooleanType&) { | ||
| return OK(writer.Bool(std::uniform_int_distribution<uint16_t>{}(e) & 1)); | ||
| writer.Bool(std::uniform_int_distribution<uint16_t>{}(e) & 1); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| template <typename T> | ||
| enable_if_physical_unsigned_integer<T, Status> Visit(const T&) { | ||
| auto val = std::uniform_int_distribution<>{}(e); | ||
| return OK(writer.Uint64(static_cast<typename T::c_type>(val))); | ||
| writer.Uint64(static_cast<typename T::c_type>(val)); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| template <typename T> | ||
| enable_if_physical_signed_integer<T, Status> Visit(const T&) { | ||
| auto val = std::uniform_int_distribution<>{}(e); | ||
| return OK(writer.Int64(static_cast<typename T::c_type>(val))); | ||
| writer.Int64(static_cast<typename T::c_type>(val)); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| template <typename T> | ||
| enable_if_physical_floating_point<T, Status> Visit(const T&) { | ||
| auto val = std::normal_distribution<typename T::c_type>{0, 1 << 10}(e); | ||
| return OK(writer.Double(val)); | ||
| writer.Double(val); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status GenerateUtf8(const DataType&) { | ||
| auto num_codepoints = std::poisson_distribution<>{4}(e); | ||
| auto seed = std::uniform_int_distribution<uint32_t>{}(e); | ||
| std::string s = RandomUtf8String(seed, num_codepoints); | ||
| return OK(writer.String(s)); | ||
| writer.String(s); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| template <typename T> | ||
|
|
@@ -132,7 +136,8 @@ struct GenerateImpl { | |
| for (int i = 0; i < size; ++i) { | ||
| RETURN_NOT_OK(Generate(t.value_type(), e, &writer, options)); | ||
| } | ||
| return OK(writer.EndArray(size)); | ||
| writer.EndArray(); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status Visit(const ListViewType& t) { return NotImplemented(t); } | ||
|
|
@@ -162,7 +167,7 @@ struct GenerateImpl { | |
| } | ||
|
|
||
| Engine& e; | ||
| rj::Writer<rj::StringBuffer>& writer; | ||
| Writer& writer; | ||
| const GenerateOptions& options; | ||
| }; | ||
|
|
||
|
|
@@ -180,12 +185,9 @@ inline static Status Generate(const std::shared_ptr<DataType>& type, Engine& e, | |
| template <typename Engine> | ||
| inline static Status Generate(const std::vector<std::shared_ptr<Field>>& fields, | ||
| Engine& e, Writer* writer, const GenerateOptions& options) { | ||
| RETURN_NOT_OK(OK(writer->StartObject())); | ||
|
|
||
| int num_fields = 0; | ||
| writer->StartObject(); | ||
| auto write_field = [&](const Field& f) { | ||
| ++num_fields; | ||
| writer->Key(f.name().c_str()); | ||
| writer->Key(f.name()); | ||
| return Generate(f.type(), e, writer, options); | ||
| }; | ||
|
|
||
|
|
@@ -210,7 +212,8 @@ inline static Status Generate(const std::vector<std::shared_ptr<Field>>& fields, | |
| } | ||
| } | ||
|
|
||
| return OK(writer->EndObject(num_fields)); | ||
| writer->EndObject(); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| inline static Status MakeStream(string_view src_str, | ||
|
|
@@ -258,14 +261,26 @@ inline static Status ParseFromString(ParseOptions options, string_view src_str, | |
| } | ||
|
|
||
| static inline std::string PrettyPrint(string_view one_line) { | ||
| rj::Document document; | ||
| simdjson::ondemand::parser parser; | ||
|
|
||
| // Must pass size to avoid ASAN issues. | ||
| document.Parse(one_line.data(), one_line.size()); | ||
| rj::StringBuffer sb; | ||
| rj::PrettyWriter<rj::StringBuffer> writer(sb); | ||
| document.Accept(writer); | ||
| return sb.GetString(); | ||
| simdjson::padded_string json(one_line.data(), one_line.size()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use |
||
|
|
||
| auto document_result = | ||
| internal::ResolveSimdjsonResult(parser.iterate(json), "Failed to parse JSON"); | ||
| ABORT_NOT_OK(document_result.status()); | ||
| auto document = std::move(document_result).ValueOrDie(); | ||
|
|
||
| auto value_result = | ||
| internal::ResolveSimdjsonResult(document.get_value(), "Failed to get JSON value"); | ||
| ABORT_NOT_OK(value_result.status()); | ||
| auto value = std::move(value_result).ValueOrDie(); | ||
|
|
||
| std::string result; | ||
| result.reserve(one_line.size()); | ||
|
|
||
| ABORT_NOT_OK(internal::PrettyPrintJsonValue(value, &result)); | ||
| return result; | ||
| } | ||
|
|
||
| template <typename T> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -755,6 +755,7 @@ if needs_testing | |
| filesystem_dep, | ||
| gmock_dep, | ||
| gtest_dep, | ||
| simdjson_dep, | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem like a great place for this test but I can't think of anything better.