Skip to content

GH-46439: [C++} Address post-merge review comments in PR exposing {Array,...}FromJSON helpers in public API #46447

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cpp/examples/arrow/from_json_string_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ arrow::Status RunExample() {
"[[11, 22], null, [null, 33]]"));

// ChunkedArrayFromJSONString
std::shared_ptr<arrow::ChunkedArray> chunked_array;
ARROW_RETURN_NOT_OK(ChunkedArrayFromJSONString(
arrow::int32(), {"[5, 10]", "[null]", "[16]"}, &chunked_array));
ARROW_ASSIGN_OR_RAISE(
auto chunked_array,
ChunkedArrayFromJSONString(arrow::int32(), {"[5, 10]", "[null]", "[16]"}));

// DictArrayFromJSONString
std::shared_ptr<arrow::Array> dict_array;
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/arrow/dataset/test_util_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2140,8 +2140,8 @@ class WriteFileSystemDatasetMixin : public MakeFileSystemDatasetMixin {
actual_struct = std::dynamic_pointer_cast<Array>(struct_array);
}

auto expected_struct = arrow::ArrayFromJSON(
struct_(expected_physical_schema_->fields()), file_contents->second);
auto expected_struct = ArrayFromJSON(struct_(expected_physical_schema_->fields()),
file_contents->second);

AssertArraysEqual(*expected_struct, *actual_struct, /*verbose=*/true);
}
Expand Down
10 changes: 5 additions & 5 deletions cpp/src/arrow/json/from_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1003,17 +1003,17 @@ Result<std::shared_ptr<Array>> ArrayFromJSONString(const std::shared_ptr<DataTyp
return ArrayFromJSONString(type, std::string_view(json_string));
}

Status ChunkedArrayFromJSONString(const std::shared_ptr<DataType>& type,
const std::vector<std::string>& json_strings,
std::shared_ptr<ChunkedArray>* out) {
Result<std::shared_ptr<ChunkedArray>> ChunkedArrayFromJSONString(
const std::shared_ptr<DataType>& type, const std::vector<std::string>& json_strings) {
ArrayVector out_chunks;
out_chunks.reserve(json_strings.size());
for (const std::string& chunk_json : json_strings) {
out_chunks.emplace_back();
ARROW_ASSIGN_OR_RAISE(out_chunks.back(), ArrayFromJSONString(type, chunk_json));
}
*out = std::make_shared<ChunkedArray>(std::move(out_chunks), type);
return Status::OK();
std::shared_ptr<ChunkedArray> out =
std::make_shared<ChunkedArray>(std::move(out_chunks), type);
return out;
}

Status DictArrayFromJSONString(const std::shared_ptr<DataType>& type,
Expand Down
12 changes: 5 additions & 7 deletions cpp/src/arrow/json/from_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ Result<std::shared_ptr<Array>> ArrayFromJSONString(const std::shared_ptr<DataTyp
/// \brief Create a ChunkedArray from a JSON string
///
/// \code {.cpp}
/// std::shared_ptr<ChunkedArray> chunked_array;
/// ChunkedArrayFromJSONString(
/// int64(), {R"([5, 10])", R"([null])", R"([16])"}, &chunked_array
/// );
/// std::shared_ptr<ChunkedArray> chunked_array =
/// ChunkedArrayFromJSONString(int64(), {R"([5, 10])", R"([null])", R"([16])"})
/// .ValueOrDie();
/// \endcode
ARROW_EXPORT
Status ChunkedArrayFromJSONString(const std::shared_ptr<DataType>& type,
const std::vector<std::string>& json_strings,
std::shared_ptr<ChunkedArray>* out);
Result<std::shared_ptr<ChunkedArray>> ChunkedArrayFromJSONString(
const std::shared_ptr<DataType>& type, const std::vector<std::string>& json_strings);

/// \brief Create a DictionaryArray from a JSON string
///
Expand Down
14 changes: 7 additions & 7 deletions cpp/src/arrow/json/from_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1481,20 +1481,20 @@ TEST(TestDictArrayFromJSON, Errors) {

TEST(TestChunkedArrayFromJSON, Basics) {
auto type = int32();
std::shared_ptr<ChunkedArray> chunked_array;
ASSERT_OK(ChunkedArrayFromJSONString(type, {}, &chunked_array));
ASSERT_OK_AND_ASSIGN(auto chunked_array, ChunkedArrayFromJSONString(type, {}));
ASSERT_OK(chunked_array->ValidateFull());
ASSERT_EQ(chunked_array->num_chunks(), 0);
AssertTypeEqual(type, chunked_array->type());

ASSERT_OK(ChunkedArrayFromJSONString(type, {"[1, 2]", "[3, null, 4]"}, &chunked_array));
ASSERT_OK(chunked_array->ValidateFull());
ASSERT_EQ(chunked_array->num_chunks(), 2);
ASSERT_OK_AND_ASSIGN(auto chunked_array_two,
ChunkedArrayFromJSONString(type, {"[1, 2]", "[3, null, 4]"}));
ASSERT_OK(chunked_array_two->ValidateFull());
ASSERT_EQ(chunked_array_two->num_chunks(), 2);
std::shared_ptr<Array> expected_chunk;
ASSERT_OK_AND_ASSIGN(expected_chunk, ArrayFromJSONString(type, "[1, 2]"));
AssertArraysEqual(*expected_chunk, *chunked_array->chunk(0), /*verbose=*/true);
AssertArraysEqual(*expected_chunk, *chunked_array_two->chunk(0), /*verbose=*/true);
ASSERT_OK_AND_ASSIGN(expected_chunk, ArrayFromJSONString(type, "[3, null, 4]"));
AssertArraysEqual(*expected_chunk, *chunked_array->chunk(1), /*verbose=*/true);
AssertArraysEqual(*expected_chunk, *chunked_array_two->chunk(1), /*verbose=*/true);
}

TEST(TestScalarFromJSON, Basics) {
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/arrow/testing/gtest_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ std::shared_ptr<Array> DictArrayFromJSON(const std::shared_ptr<DataType>& type,

std::shared_ptr<ChunkedArray> ChunkedArrayFromJSON(const std::shared_ptr<DataType>& type,
const std::vector<std::string>& json) {
std::shared_ptr<ChunkedArray> out;
ABORT_NOT_OK(json::ChunkedArrayFromJSONString(type, json, &out));
EXPECT_OK_AND_ASSIGN(auto out, json::ChunkedArrayFromJSONString(type, json));
return out;
}

Expand Down
13 changes: 6 additions & 7 deletions python/pyarrow/src/arrow/python/gdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,12 @@ void TestSession() {
key_value_metadata({"key1", "key2", "key3"}, {"value1", "value2", "value3"}));

// Table
ChunkedArrayVector table_columns{2};
ARROW_CHECK_OK(
ChunkedArrayFromJSONString(int32(), {"[1, 2, 3]", "[4, 5]"}, &table_columns[0]));
ARROW_CHECK_OK(ChunkedArrayFromJSONString(
utf8(), {R"(["abc", null])", R"(["def"])", R"(["ghi", "jkl"])"},
&table_columns[1]));
auto table = Table::Make(batch_schema, table_columns);
ASSERT_OK_AND_ASSIGN(auto col1,
ChunkedArrayFromJSONString(int32(), {"[1, 2, 3]", "[4, 5]"}));
ASSERT_OK_AND_ASSIGN(
auto col2, ChunkedArrayFromJSONString(
utf8(), {R"(["abc", null])", R"(["def"])", R"(["ghi", "jkl"])"}));
auto table = Table::Make(batch_schema, {col1, col2});

// Datum
Datum empty_datum{};
Expand Down
Loading