diff --git a/cpp/src/arrow/json/options.h b/cpp/src/arrow/json/options.h index d7edab9cedd..b2e4b0154bd 100644 --- a/cpp/src/arrow/json/options.h +++ b/cpp/src/arrow/json/options.h @@ -43,6 +43,8 @@ struct ARROW_EXPORT ParseOptions { // Parsing options /// Optional explicit schema (disables type inference on those fields) + /// + /// Field names must be unique within each object, including nested objects. std::shared_ptr explicit_schema; /// Whether objects may be printed across multiple lines (for example pretty-printed) diff --git a/cpp/src/arrow/json/parser.cc b/cpp/src/arrow/json/parser.cc index 08d83f0c67f..2cfae34caed 100644 --- a/cpp/src/arrow/json/parser.cc +++ b/cpp/src/arrow/json/parser.cc @@ -515,7 +515,12 @@ class RawBuilderSet { RETURN_NOT_OK(MakeBuilder(*f->type(), leading_nulls, &field_builder)); field_builder.nullable = f->nullable(); - Cast(*builder)->AddField(f->name(), field_builder); + auto* object_builder = Cast(*builder); + const auto next_index = object_builder->num_fields(); + if (object_builder->AddField(f->name(), field_builder) != next_index) { + return Status::Invalid("JSON parse error: duplicate field '", f->name(), + "' in explicit schema"); + } } return Status::OK(); } diff --git a/cpp/src/arrow/json/parser_test.cc b/cpp/src/arrow/json/parser_test.cc index ae764c71804..c2aea8dad2c 100644 --- a/cpp/src/arrow/json/parser_test.cc +++ b/cpp/src/arrow/json/parser_test.cc @@ -210,6 +210,35 @@ TEST_P(BlockParserTypeError, FailOnDuplicateKeysNoSchema) { testing::StartsWith("JSON parse error: Column(/a) was specified twice in row 0")); } +TEST_P(BlockParserTypeError, FailOnDuplicateSchemaFields) { + for (const auto& name : {"a", ""}) { + for (const auto& other_type : {boolean(), utf8()}) { + auto duplicate_fields = struct_( + {field(name, boolean()), field("other", int32()), field(name, other_type)}); + for (const auto& explicit_schema : + {schema(duplicate_fields->fields()), + schema({field("nested", duplicate_fields)}), + schema({field("nested", list(duplicate_fields))})}) { + SCOPED_TRACE(explicit_schema->ToString()); + std::unique_ptr parser; + ASSERT_RAISES_WITH_MESSAGE(Invalid, + "Invalid: JSON parse error: duplicate field '" + + std::string(name) + "' in explicit schema", + BlockParser::Make(Options(explicit_schema), &parser)); + } + } + } +} + +TEST_P(BlockParserTypeError, AllowSameFieldNameInDifferentObjects) { + auto nested_type = struct_({field("a", boolean())}); + auto fields = FieldVector{field("a", boolean()), field("nested", nested_type), + field("items", list(nested_type))}; + AssertParseColumns(Options(schema(fields)), + R"({"a":true,"nested":{"a":false},"items":[{"a":true}]})", fields, + {"[true]", R"([{"a":false}])", R"([[{"a":true}]])"}); +} + INSTANTIATE_TEST_SUITE_P(BlockParserTypeError, BlockParserTypeError, ::testing::Values(UnexpectedFieldBehavior::Ignore, UnexpectedFieldBehavior::Error, diff --git a/cpp/src/arrow/json/reader_test.cc b/cpp/src/arrow/json/reader_test.cc index bc533595614..0217f0fb949 100644 --- a/cpp/src/arrow/json/reader_test.cc +++ b/cpp/src/arrow/json/reader_test.cc @@ -146,6 +146,15 @@ TEST_P(ReaderTest, Nested) { AssertTablesEqual(*expected_table, *table_); } +TEST_P(ReaderTest, DuplicateSchemaFields) { + parse_options_.explicit_schema = + schema({field("foo", boolean()), field("foo", boolean())}); + read_options_.use_threads = GetParam(); + ASSERT_RAISES_WITH_MESSAGE( + Invalid, "Invalid: JSON parse error: duplicate field 'foo' in explicit schema", + ReadToTable(R"({"foo": true, "other": "bar"})", read_options_, parse_options_)); +} + TEST_P(ReaderTest, PartialSchema) { parse_options_.unexpected_field_behavior = UnexpectedFieldBehavior::InferType; parse_options_.explicit_schema =