diff --git a/cpp/src/arrow/json/parser.cc b/cpp/src/arrow/json/parser.cc index 08d83f0c67f..408d5c0b913 100644 --- a/cpp/src/arrow/json/parser.cc +++ b/cpp/src/arrow/json/parser.cc @@ -526,11 +526,12 @@ class RawBuilderSet { /// Appending null is slightly tricky since null count is stored inline /// for builders of Kind::kNull. Append nulls using this helper - Result AppendNull(BuilderPtr parent, int field_index) { + Result AppendNull(BuilderPtr parent, int field_index, + bool parent_is_null = false) { BuilderPtr builder = parent.kind == Kind::kArray ? Cast(parent)->value_builder() : Cast(parent)->field_builder(field_index); - if (ARROW_PREDICT_FALSE(!builder.nullable)) { + if (ARROW_PREDICT_FALSE(!builder.nullable && !parent_is_null)) { return ParseError("a required field was null"); } switch (builder.kind) { @@ -572,7 +573,8 @@ class RawBuilderSet { RETURN_NOT_OK(struct_builder->AppendNull()); for (int i = 0; i < struct_builder->num_fields(); ++i) { - RETURN_NOT_OK(AppendNull(builder, i)); + // Child slots are masked by the null struct, regardless of nullability. + RETURN_NOT_OK(AppendNull(builder, i, /*parent_is_null=*/true)); } break; } diff --git a/cpp/src/arrow/json/parser_test.cc b/cpp/src/arrow/json/parser_test.cc index ae764c71804..e2e7cb5b68d 100644 --- a/cpp/src/arrow/json/parser_test.cc +++ b/cpp/src/arrow/json/parser_test.cc @@ -228,6 +228,58 @@ TEST(BlockParserWithSchema, Nested) { R"([{"ps":null}, {}, {"ps":"78"}, {"ps":"90"}])"}); } +TEST(BlockParserWithSchema, NullableStructWithRequiredChildren) { + auto options = ParseOptions::Defaults(); + auto nested = struct_({field("flag", boolean(), false)}); + options.explicit_schema = + schema({field("my_struct", struct_({field("some_information", int64(), false), + field("nested", nested, false)}))}); + std::shared_ptr parsed; + ASSERT_OK(ParseFromString(options, R"( + {} + {"my_struct": null} + {"my_struct": {"some_information": 42, "nested": {"flag": true}}} + )", + &parsed)); + ASSERT_OK(parsed->ValidateFull()); + + auto column = checked_cast(*parsed).field(0); + ASSERT_EQ(column->length(), 3); + EXPECT_TRUE(column->IsNull(0)); + EXPECT_TRUE(column->IsNull(1)); + EXPECT_FALSE(column->IsNull(2)); + auto expected_type = + struct_({field("some_information", utf8(), false), field("nested", nested, false)}); + auto expected = ArrayFromJSON(expected_type, R"( + [{"some_information": "42", "nested": {"flag": true}}] + )"); + AssertUnconvertedArraysEqual(*expected, *column->Slice(2)); +} + +TEST(BlockParserWithSchema, RequiredChildrenOfValidStruct) { + auto options = ParseOptions::Defaults(); + options.explicit_schema = + schema({field("my_struct", struct_({field("required", int64(), false)}))}); + std::shared_ptr parsed; + std::string null_row = "{\"my_struct\": null}\n"; + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, testing::HasSubstr("a required field was absent"), + ParseFromString(options, null_row + R"({"my_struct": {}})", &parsed)); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, testing::HasSubstr("a required field was null"), + ParseFromString(options, null_row + R"({"my_struct": {"required": null}})", + &parsed)); + + options.explicit_schema = + schema({options.explicit_schema->field(0)->WithNullable(false)}); + EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, + testing::HasSubstr("a required field was absent"), + ParseFromString(options, "{}", &parsed)); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, testing::HasSubstr("a required field was null"), + ParseFromString(options, R"({"my_struct": null})", &parsed)); +} + TEST(BlockParserWithSchema, FailOnIncompleteJson) { auto options = ParseOptions::Defaults(); options.explicit_schema = schema({field("a", int32())});