diff --git a/cpp/src/arrow/json/chunked_builder.cc b/cpp/src/arrow/json/chunked_builder.cc index 48f05495351..5c5a8a9113e 100644 --- a/cpp/src/arrow/json/chunked_builder.cc +++ b/cpp/src/arrow/json/chunked_builder.cc @@ -278,13 +278,14 @@ class ChunkedStructArrayBuilder : public ChunkedArrayBuilder { ChunkedStructArrayBuilder( const std::shared_ptr& task_group, MemoryPool* pool, const PromotionGraph* promotion_graph, - std::vector>> - name_builders) + std::vector, std::shared_ptr>> + field_builders) : ChunkedArrayBuilder(task_group), pool_(pool), promotion_graph_(promotion_graph) { - for (auto&& name_builder : name_builders) { + for (auto&& field_builder : field_builders) { auto index = static_cast(name_to_index_.size()); - name_to_index_.emplace(std::move(name_builder.first), index); - child_builders_.emplace_back(std::move(name_builder.second)); + name_to_index_.emplace(field_builder.first->name(), index); + child_builders_.emplace_back(std::move(field_builder.second)); + child_nullability_.push_back(field_builder.first->nullable()); } } @@ -362,7 +363,8 @@ class ChunkedStructArrayBuilder : public ChunkedArrayBuilder { RETURN_NOT_OK(child_builder->Finish(&child_array)); child_arrays[name_index.second] = child_array; - fields[name_index.second] = field(name_index.first, child_array->type()); + fields[name_index.second] = field(name_index.first, child_array->type(), + child_nullability_[name_index.second]); } auto type = struct_(std::move(fields)); @@ -413,6 +415,7 @@ class ChunkedStructArrayBuilder : public ChunkedArrayBuilder { RETURN_NOT_OK(MakeChunkedArrayBuilder(task_group_, pool_, promotion_graph_, type, /*allow_promotion=*/true, &child_builder)); child_builders_.emplace_back(std::move(child_builder)); + child_nullability_.push_back(fields[i]->nullable()); } auto unconverted_field = unconverted.type()->field(i); @@ -431,6 +434,7 @@ class ChunkedStructArrayBuilder : public ChunkedArrayBuilder { const PromotionGraph* promotion_graph_; std::unordered_map name_to_index_; std::vector> child_builders_; + std::vector child_nullability_; std::vector> child_absent_; BufferVector null_bitmap_chunks_; std::vector chunk_lengths_; @@ -450,13 +454,13 @@ Status MakeChunkedArrayBuilder(const std::shared_ptr& task_group, // If a promotion graph is not provided, unexpected fields are always ignored and // type inference never occurs. if (type->id() == Type::STRUCT) { - std::vector>> + std::vector, std::shared_ptr>> child_builders; for (const auto& f : type->fields()) { std::shared_ptr child_builder; RETURN_NOT_OK(MakeChunkedArrayBuilder(task_group, pool, promotion_graph, f->type(), allow_promotion, &child_builder)); - child_builders.emplace_back(f->name(), std::move(child_builder)); + child_builders.emplace_back(f, std::move(child_builder)); } *out = std::make_shared(task_group, pool, promotion_graph, std::move(child_builders)); diff --git a/python/pyarrow/tests/test_json.py b/python/pyarrow/tests/test_json.py index 3dc54d53fba..a62b684205a 100644 --- a/python/pyarrow/tests/test_json.py +++ b/python/pyarrow/tests/test_json.py @@ -263,6 +263,31 @@ def test_explicit_schema_decimal(self): assert table.schema == schema assert table.to_pydict() == expected + def test_explicit_schema_preserves_nullability(self): + rows = (b'{"required": 1, "optional": 2, ' + b'"nested": {"required": 3, "optional": 4}}\n') + schema = pa.schema([ + pa.field("required", pa.int32(), nullable=False), + pa.field("optional", pa.int32(), nullable=True), + pa.field( + "nested", + pa.struct([ + pa.field("required", pa.int32(), nullable=False), + pa.field("optional", pa.int32(), nullable=True), + ]), + nullable=True, + ), + ]) + + table = self.read_bytes( + rows, + parse_options=ParseOptions( + explicit_schema=schema, + unexpected_field_behavior="error", + ), + ) + assert table.schema == schema + def test_explicit_schema_with_unexpected_behaviour(self): # infer by default rows = (b'{"foo": "bar", "num": 0}\n'