-
Notifications
You must be signed in to change notification settings - Fork 4.2k
ARROW-3982: [C++] Allow "binary" input in simple JSON format #3222
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,8 @@ using ::arrow::internal::checked_cast; | |
| static constexpr auto kParseFlags = rj::kParseFullPrecisionFlag | rj::kParseNanAndInfFlag; | ||
|
|
||
| static Status JSONTypeError(const char* expected_type, rj::Type json_type) { | ||
| return Status::Invalid("Expected ", expected_type, " or null, got type ", json_type); | ||
| return Status::Invalid("Expected ", expected_type, " or null, got JSON type ", | ||
| json_type); | ||
| } | ||
|
|
||
| class Converter { | ||
|
|
@@ -91,7 +92,6 @@ class ConcreteConverter : public Converter { | |
| }; | ||
|
|
||
| // TODO : dates and times? | ||
| // TODO : binary / fixed size binary? | ||
|
|
||
| // ------------------------------------------------------------------------ | ||
| // Converter for null arrays | ||
|
|
@@ -284,7 +284,7 @@ class DecimalConverter final : public ConcreteConverter<DecimalConverter> { | |
| }; | ||
|
|
||
| // ------------------------------------------------------------------------ | ||
| // Converter for string arrays | ||
| // Converter for binary and string arrays | ||
|
|
||
| class StringConverter final : public ConcreteConverter<StringConverter> { | ||
| public: | ||
|
|
@@ -313,6 +313,43 @@ class StringConverter final : public ConcreteConverter<StringConverter> { | |
| std::shared_ptr<BinaryBuilder> builder_; | ||
| }; | ||
|
|
||
| // ------------------------------------------------------------------------ | ||
| // Converter for fixed-size binary arrays | ||
|
|
||
| class FixedSizeBinaryConverter final | ||
| : public ConcreteConverter<FixedSizeBinaryConverter> { | ||
| public: | ||
| explicit FixedSizeBinaryConverter(const std::shared_ptr<DataType>& type) { | ||
| this->type_ = type; | ||
| builder_ = std::make_shared<FixedSizeBinaryBuilder>(type, default_memory_pool()); | ||
| } | ||
|
|
||
| Status AppendNull() override { return builder_->AppendNull(); } | ||
|
|
||
| Status AppendValue(const rj::Value& json_obj) override { | ||
| if (json_obj.IsNull()) { | ||
| return AppendNull(); | ||
| } | ||
| if (json_obj.IsString()) { | ||
| auto view = util::string_view(json_obj.GetString(), json_obj.GetStringLength()); | ||
| if (view.length() != static_cast<size_t>(builder_->byte_width())) { | ||
| std::stringstream ss; | ||
|
Member
Author
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. This can be simplified now :-) |
||
| ss << "Invalid string length " << view.length() << " in JSON input for " | ||
| << this->type_->ToString(); | ||
| return Status::Invalid(ss.str()); | ||
| } | ||
| return builder_->Append(view); | ||
| } else { | ||
| return JSONTypeError("string", json_obj.GetType()); | ||
| } | ||
| } | ||
|
|
||
| std::shared_ptr<ArrayBuilder> builder() override { return builder_; } | ||
|
|
||
| protected: | ||
| std::shared_ptr<FixedSizeBinaryBuilder> builder_; | ||
| }; | ||
|
|
||
| // ------------------------------------------------------------------------ | ||
| // Converter for list arrays | ||
|
|
||
|
|
@@ -449,6 +486,8 @@ Status GetConverter(const std::shared_ptr<DataType>& type, | |
| SIMPLE_CONVERTER_CASE(Type::LIST, ListConverter) | ||
| SIMPLE_CONVERTER_CASE(Type::STRUCT, StructConverter) | ||
| SIMPLE_CONVERTER_CASE(Type::STRING, StringConverter) | ||
| SIMPLE_CONVERTER_CASE(Type::BINARY, StringConverter) | ||
| SIMPLE_CONVERTER_CASE(Type::FIXED_SIZE_BINARY, FixedSizeBinaryConverter) | ||
| SIMPLE_CONVERTER_CASE(Type::DECIMAL, DecimalConverter) | ||
| default: { | ||
| return Status::NotImplemented("JSON conversion to ", type->ToString(), | ||
|
|
||
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
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.
I'd refactor this function with a simple switch (if possible?) on Json type and hide the string_view parsing in a static inline function.
This is a style preference, but I think it improves reading since there's less control flow involved.
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.
The current idiom (a chain of
ifs) is used elsewhere in the module, though. So we'd have to change other occurrences as well. IMHO there's not much benefit.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.
I think it's OK to leave the code as is since it's similar to the rest of the file. If future refactoring is needed then whoever is working on it is free to do this code scrubbing