From 5889959c46a706cdd7a14cd12a30cbbbca4e524f Mon Sep 17 00:00:00 2001 From: Aikhan Jumashukurov <117706338+aikhanj@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:04:26 -0500 Subject: [PATCH] GH-33055: [C++] Allow null JSON structs with non-nullable children A null or missing nullable struct still appends placeholder slots to its child builders so later rows stay aligned. RawBuilderSet::AppendNull checked those hidden slots against each child's nullability, so reading {} or {"my_struct": null} failed with "a required field was null". Pass parent_is_null through the recursion so slots masked by a null parent skip that check. Real JSON nulls and absent values for required fields are still rejected. Generated-by: OpenAI Codex, Claude Code Co-Authored-By: Claude Opus 5.5 --- cpp/src/arrow/json/parser.cc | 8 +++-- cpp/src/arrow/json/parser_test.cc | 52 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/cpp/src/arrow/json/parser.cc b/cpp/src/arrow/json/parser.cc index 08d83f0c67fb..408d5c0b9137 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 ae764c718047..e2e7cb5b68df 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())});