Skip to content
Open
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
8 changes: 5 additions & 3 deletions cpp/src/arrow/json/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuilderPtr> AppendNull(BuilderPtr parent, int field_index) {
Result<BuilderPtr> AppendNull(BuilderPtr parent, int field_index,
bool parent_is_null = false) {
BuilderPtr builder = parent.kind == Kind::kArray
? Cast<Kind::kArray>(parent)->value_builder()
: Cast<Kind::kObject>(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) {
Expand Down Expand Up @@ -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;
}
Expand Down
52 changes: 52 additions & 0 deletions cpp/src/arrow/json/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array> 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<const StructArray&>(*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<Array> 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())});
Expand Down
Loading