From 7e7a876d27b1d02601ae42d9b488284bca70f959 Mon Sep 17 00:00:00 2001 From: Aman <149809343+AmanBoronbaev@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:10:55 +0300 Subject: [PATCH 1/2] GH-44949: [C++][JSON] Reject duplicate field names in explicit schemas --- cpp/src/arrow/json/options.h | 2 ++ cpp/src/arrow/json/parser.cc | 4 ++++ cpp/src/arrow/json/parser_test.cc | 29 +++++++++++++++++++++++++++++ cpp/src/arrow/json/reader_test.cc | 9 +++++++++ 4 files changed, 44 insertions(+) diff --git a/cpp/src/arrow/json/options.h b/cpp/src/arrow/json/options.h index d7edab9ceddb..b2e4b0154bdc 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 08d83f0c67fb..f7ede009899c 100644 --- a/cpp/src/arrow/json/parser.cc +++ b/cpp/src/arrow/json/parser.cc @@ -511,6 +511,10 @@ class RawBuilderSet { const auto& struct_type = checked_cast(t); for (const auto& f : struct_type.fields()) { + if (Cast(*builder)->FindFieldIndex(f->name()) != -1) { + return Status::Invalid("JSON parse error: duplicate field '", f->name(), + "' in explicit schema"); + } BuilderPtr field_builder; RETURN_NOT_OK(MakeBuilder(*f->type(), leading_nulls, &field_builder)); field_builder.nullable = f->nullable(); diff --git a/cpp/src/arrow/json/parser_test.cc b/cpp/src/arrow/json/parser_test.cc index ae764c718047..c2aea8dad2c4 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 bc533595614a..0217f0fb949b 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 = From ddb215160c40192deb14a6a6cab25ae1a9b00d2e Mon Sep 17 00:00:00 2001 From: Aman <149809343+AmanBoronbaev@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:17:24 +0300 Subject: [PATCH 2/2] GH-44949: Reuse field insertion lookup for duplicate detection Signed-off-by: Aman <149809343+AmanBoronbaev@users.noreply.github.com> --- cpp/src/arrow/json/parser.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/json/parser.cc b/cpp/src/arrow/json/parser.cc index f7ede009899c..2cfae34caed9 100644 --- a/cpp/src/arrow/json/parser.cc +++ b/cpp/src/arrow/json/parser.cc @@ -511,15 +511,16 @@ class RawBuilderSet { const auto& struct_type = checked_cast(t); for (const auto& f : struct_type.fields()) { - if (Cast(*builder)->FindFieldIndex(f->name()) != -1) { - return Status::Invalid("JSON parse error: duplicate field '", f->name(), - "' in explicit schema"); - } BuilderPtr field_builder; 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(); }