Skip to content

Commit

Permalink
ARROW-15611: [C++] Migrate arrow::ipc::internal::json::ArrayFromJSON …
Browse files Browse the repository at this point in the history
…to Result<>

Migrate arrow::ipc::internal::json::ArrayFromJSON to Result<>

Closes #12713 from AlvinJ15/ARROW-15611#Migrate_arrow_ipc_internal_json_ArrayFromJSON_to_Result

Authored-by: Alvin Chunga <alvinchma@gmail.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
  • Loading branch information
AlvinJ15 authored and pitrou committed Mar 29, 2022
1 parent 556ee23 commit 77e2a11
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 233 deletions.
2 changes: 1 addition & 1 deletion cpp/src/arrow/ipc/generate_fuzz_corpus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Result<std::shared_ptr<RecordBatch>> MakeMapBatch() {
[]
]
)";
RETURN_NOT_OK(ArrayFromJSON(map(int16(), int32()), json_input, &array));
ARROW_ASSIGN_OR_RAISE(array, ArrayFromJSON(map(int16(), int32()), json_input));
auto schema = ::arrow::schema({field("f0", array->type())});
return RecordBatch::Make(schema, array->length(), {array});
}
Expand Down
34 changes: 18 additions & 16 deletions cpp/src/arrow/ipc/json_simple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ Status GetConverter(const std::shared_ptr<DataType>& type,

} // namespace

Status ArrayFromJSON(const std::shared_ptr<DataType>& type, util::string_view json_string,
std::shared_ptr<Array>* out) {
Result<std::shared_ptr<Array>> ArrayFromJSON(const std::shared_ptr<DataType>& type,
util::string_view json_string) {
std::shared_ptr<Converter> converter;
RETURN_NOT_OK(GetConverter(type, &converter));

Expand All @@ -919,17 +919,19 @@ Status ArrayFromJSON(const std::shared_ptr<DataType>& type, util::string_view js

// The JSON document should be an array, append it
RETURN_NOT_OK(converter->AppendValues(json_doc));
return converter->Finish(out);
std::shared_ptr<Array> out;
RETURN_NOT_OK(converter->Finish(&out));
return out;
}

Status ArrayFromJSON(const std::shared_ptr<DataType>& type,
const std::string& json_string, std::shared_ptr<Array>* out) {
return ArrayFromJSON(type, util::string_view(json_string), out);
Result<std::shared_ptr<Array>> ArrayFromJSON(const std::shared_ptr<DataType>& type,
const std::string& json_string) {
return ArrayFromJSON(type, util::string_view(json_string));
}

Status ArrayFromJSON(const std::shared_ptr<DataType>& type, const char* json_string,
std::shared_ptr<Array>* out) {
return ArrayFromJSON(type, util::string_view(json_string), out);
Result<std::shared_ptr<Array>> ArrayFromJSON(const std::shared_ptr<DataType>& type,
const char* json_string) {
return ArrayFromJSON(type, util::string_view(json_string));
}

Status ChunkedArrayFromJSON(const std::shared_ptr<DataType>& type,
Expand All @@ -939,7 +941,7 @@ Status ChunkedArrayFromJSON(const std::shared_ptr<DataType>& type,
out_chunks.reserve(json_strings.size());
for (const std::string& chunk_json : json_strings) {
out_chunks.emplace_back();
RETURN_NOT_OK(ArrayFromJSON(type, chunk_json, &out_chunks.back()));
ARROW_ASSIGN_OR_RAISE(out_chunks.back(), ArrayFromJSON(type, chunk_json));
}
*out = std::make_shared<ChunkedArray>(std::move(out_chunks), type);
return Status::OK();
Expand All @@ -954,10 +956,10 @@ Status DictArrayFromJSON(const std::shared_ptr<DataType>& type,

const auto& dictionary_type = checked_cast<const DictionaryType&>(*type);

std::shared_ptr<Array> indices, dictionary;
RETURN_NOT_OK(ArrayFromJSON(dictionary_type.index_type(), indices_json, &indices));
RETURN_NOT_OK(
ArrayFromJSON(dictionary_type.value_type(), dictionary_json, &dictionary));
ARROW_ASSIGN_OR_RAISE(auto indices,
ArrayFromJSON(dictionary_type.index_type(), indices_json));
ARROW_ASSIGN_OR_RAISE(auto dictionary,
ArrayFromJSON(dictionary_type.value_type(), dictionary_json));

return DictionaryArray::FromArrays(type, std::move(indices), std::move(dictionary))
.Value(out);
Expand Down Expand Up @@ -995,8 +997,8 @@ Status DictScalarFromJSON(const std::shared_ptr<DataType>& type,
std::shared_ptr<Scalar> index;
std::shared_ptr<Array> dictionary;
RETURN_NOT_OK(ScalarFromJSON(dictionary_type.index_type(), index_json, &index));
RETURN_NOT_OK(
ArrayFromJSON(dictionary_type.value_type(), dictionary_json, &dictionary));
ARROW_ASSIGN_OR_RAISE(dictionary,
ArrayFromJSON(dictionary_type.value_type(), dictionary_json));

*out = DictionaryScalar::Make(std::move(index), std::move(dictionary));
return Status::OK();
Expand Down
12 changes: 6 additions & 6 deletions cpp/src/arrow/ipc/json_simple.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ namespace internal {
namespace json {

ARROW_EXPORT
Status ArrayFromJSON(const std::shared_ptr<DataType>&, const std::string& json,
std::shared_ptr<Array>* out);
Result<std::shared_ptr<Array>> ArrayFromJSON(const std::shared_ptr<DataType>&,
const std::string& json);

ARROW_EXPORT
Status ArrayFromJSON(const std::shared_ptr<DataType>&, util::string_view json,
std::shared_ptr<Array>* out);
Result<std::shared_ptr<Array>> ArrayFromJSON(const std::shared_ptr<DataType>&,
util::string_view json);

ARROW_EXPORT
Status ArrayFromJSON(const std::shared_ptr<DataType>&, const char* json,
std::shared_ptr<Array>* out);
Result<std::shared_ptr<Array>> ArrayFromJSON(const std::shared_ptr<DataType>&,
const char* json);

ARROW_EXPORT
Status ChunkedArrayFromJSON(const std::shared_ptr<DataType>& type,
Expand Down
Loading

0 comments on commit 77e2a11

Please sign in to comment.