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
2 changes: 2 additions & 0 deletions cpp/src/arrow/json/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Schema> explicit_schema;

/// Whether objects may be printed across multiple lines (for example pretty-printed)
Expand Down
7 changes: 6 additions & 1 deletion cpp/src/arrow/json/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,12 @@ class RawBuilderSet {
RETURN_NOT_OK(MakeBuilder(*f->type(), leading_nulls, &field_builder));
field_builder.nullable = f->nullable();

Cast<Kind::kObject>(*builder)->AddField(f->name(), field_builder);
auto* object_builder = Cast<Kind::kObject>(*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();
}
Expand Down
29 changes: 29 additions & 0 deletions cpp/src/arrow/json/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockParser> 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,
Expand Down
9 changes: 9 additions & 0 deletions cpp/src/arrow/json/reader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Loading