pitrou commented on code in PR #14741:
URL: https://github.com/apache/arrow/pull/14741#discussion_r1039814129
##########
cpp/src/arrow/json/reader_test.cc:
##########
@@ -305,5 +305,117 @@ TEST(ReaderTest, ListArrayWithFewValues) {
AssertTablesEqual(*actual_table, *expected_table);
}
+// ARROW-18106
+TEST(ReaderTest, FailOnTimeUnitMismatch) {
+ std::string json = R"({"t":"2022-09-05T08:08:46.000"})";
+
+ auto read_options = ReadOptions::Defaults();
+ read_options.use_threads = false;
+ auto parse_options = ParseOptions::Defaults();
+ parse_options.explicit_schema = schema({field("t",
timestamp(TimeUnit::SECOND))});
+
+ std::shared_ptr<io::InputStream> input;
+ std::shared_ptr<TableReader> reader;
+ for (auto behavior : {UnexpectedFieldBehavior::Error,
UnexpectedFieldBehavior::Ignore,
+ UnexpectedFieldBehavior::InferType}) {
+ parse_options.unexpected_field_behavior = behavior;
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(),
input,
+ read_options,
parse_options));
+ ASSERT_RAISES(Invalid, reader->Read());
+ }
+}
+
+TEST(ReaderTest, InferNestedFieldsWithSchema) {
+ std::string json = R"({}
+ {"a": {"c": null}}
+ {"a": {"c": {}}}
+ {"a": {"c": {"d": null}}}
+ {"a": {"c": {"d": []}}}
+ {"a": {"c": {"d": [null]}}}
+ {"a": {"c": {"d": [{}]}}}
+ {"a": {"c": {"d": [{"e": null}]}}}
+ {"a": {"c": {"d": [{"e": true}]}}}
+ )";
+
+ auto read_options = ReadOptions::Defaults();
+ read_options.use_threads = false;
+ auto parse_options = ParseOptions::Defaults();
+ parse_options.explicit_schema =
+ schema({field("a", struct_({field("b", timestamp(TimeUnit::SECOND))}))});
+ parse_options.unexpected_field_behavior = UnexpectedFieldBehavior::InferType;
+
+ auto expected_schema = schema({field(
+ "a", struct_({field("b", timestamp(TimeUnit::SECOND)),
+ field("c", struct_({field(
+ "d", list(struct_({field("e",
boolean())})))}))}))});
+ auto expected_batch = RecordBatchFromJSON(expected_schema, R"([
+ {"a": null},
+ {"a": {"b": null, "c": null}},
+ {"a": {"b": null, "c": {"d": null}}},
+ {"a": {"b": null, "c": {"d": null}}},
+ {"a": {"b": null, "c": {"d": []}}},
+ {"a": {"b": null, "c": {"d": [null]}}},
+ {"a": {"b": null, "c": {"d": [{"e": null}]}}},
+ {"a": {"b": null, "c": {"d": [{"e": null}]}}},
+ {"a": {"b": null, "c": {"d": [{"e": true}]}}}
+ ])");
+ ASSERT_OK_AND_ASSIGN(auto expected_table,
Table::FromRecordBatches({expected_batch}));
+
+ std::shared_ptr<io::InputStream> input;
+ std::shared_ptr<TableReader> reader;
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(), input,
+ read_options, parse_options));
+ ASSERT_OK_AND_ASSIGN(auto actual_table, reader->Read());
+ AssertTablesEqual(*actual_table, *expected_table);
+
+ json += std::string(R"({"a": {"b": "2022-09-05T08:08:46.000"}})") + "\n";
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(), input,
+ read_options, parse_options));
+ ASSERT_RAISES(Invalid, reader->Read());
Review Comment:
Can we also test something about the error message (to make sure we aren't
triggering some other condition than expected)?
##########
cpp/src/arrow/json/chunked_builder.cc:
##########
@@ -466,5 +484,13 @@ Status MakeChunkedArrayBuilder(const
std::shared_ptr<TaskGroup>& task_group,
return Status::OK();
}
+Status MakeChunkedArrayBuilder(const std::shared_ptr<TaskGroup>& task_group,
Review Comment:
IIUC, this overload is only called for top-level fields of an explicit
schema? Add a comment explaining this?
##########
cpp/src/arrow/json/reader_test.cc:
##########
@@ -305,5 +305,117 @@ TEST(ReaderTest, ListArrayWithFewValues) {
AssertTablesEqual(*actual_table, *expected_table);
}
+// ARROW-18106
+TEST(ReaderTest, FailOnTimeUnitMismatch) {
+ std::string json = R"({"t":"2022-09-05T08:08:46.000"})";
+
+ auto read_options = ReadOptions::Defaults();
+ read_options.use_threads = false;
+ auto parse_options = ParseOptions::Defaults();
+ parse_options.explicit_schema = schema({field("t",
timestamp(TimeUnit::SECOND))});
+
+ std::shared_ptr<io::InputStream> input;
+ std::shared_ptr<TableReader> reader;
+ for (auto behavior : {UnexpectedFieldBehavior::Error,
UnexpectedFieldBehavior::Ignore,
+ UnexpectedFieldBehavior::InferType}) {
+ parse_options.unexpected_field_behavior = behavior;
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(),
input,
+ read_options,
parse_options));
+ ASSERT_RAISES(Invalid, reader->Read());
+ }
+}
+
+TEST(ReaderTest, InferNestedFieldsWithSchema) {
+ std::string json = R"({}
+ {"a": {"c": null}}
+ {"a": {"c": {}}}
+ {"a": {"c": {"d": null}}}
+ {"a": {"c": {"d": []}}}
+ {"a": {"c": {"d": [null]}}}
+ {"a": {"c": {"d": [{}]}}}
+ {"a": {"c": {"d": [{"e": null}]}}}
+ {"a": {"c": {"d": [{"e": true}]}}}
+ )";
+
+ auto read_options = ReadOptions::Defaults();
+ read_options.use_threads = false;
+ auto parse_options = ParseOptions::Defaults();
+ parse_options.explicit_schema =
+ schema({field("a", struct_({field("b", timestamp(TimeUnit::SECOND))}))});
+ parse_options.unexpected_field_behavior = UnexpectedFieldBehavior::InferType;
+
+ auto expected_schema = schema({field(
+ "a", struct_({field("b", timestamp(TimeUnit::SECOND)),
+ field("c", struct_({field(
+ "d", list(struct_({field("e",
boolean())})))}))}))});
+ auto expected_batch = RecordBatchFromJSON(expected_schema, R"([
+ {"a": null},
+ {"a": {"b": null, "c": null}},
+ {"a": {"b": null, "c": {"d": null}}},
+ {"a": {"b": null, "c": {"d": null}}},
+ {"a": {"b": null, "c": {"d": []}}},
+ {"a": {"b": null, "c": {"d": [null]}}},
+ {"a": {"b": null, "c": {"d": [{"e": null}]}}},
+ {"a": {"b": null, "c": {"d": [{"e": null}]}}},
+ {"a": {"b": null, "c": {"d": [{"e": true}]}}}
+ ])");
+ ASSERT_OK_AND_ASSIGN(auto expected_table,
Table::FromRecordBatches({expected_batch}));
+
+ std::shared_ptr<io::InputStream> input;
+ std::shared_ptr<TableReader> reader;
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(), input,
+ read_options, parse_options));
+ ASSERT_OK_AND_ASSIGN(auto actual_table, reader->Read());
Review Comment:
This snippet occurs quite a few times, factor it out in a test fixture
perhaps?
##########
cpp/src/arrow/json/reader_test.cc:
##########
@@ -305,5 +305,117 @@ TEST(ReaderTest, ListArrayWithFewValues) {
AssertTablesEqual(*actual_table, *expected_table);
}
+// ARROW-18106
+TEST(ReaderTest, FailOnTimeUnitMismatch) {
+ std::string json = R"({"t":"2022-09-05T08:08:46.000"})";
+
+ auto read_options = ReadOptions::Defaults();
+ read_options.use_threads = false;
+ auto parse_options = ParseOptions::Defaults();
+ parse_options.explicit_schema = schema({field("t",
timestamp(TimeUnit::SECOND))});
+
+ std::shared_ptr<io::InputStream> input;
+ std::shared_ptr<TableReader> reader;
+ for (auto behavior : {UnexpectedFieldBehavior::Error,
UnexpectedFieldBehavior::Ignore,
+ UnexpectedFieldBehavior::InferType}) {
+ parse_options.unexpected_field_behavior = behavior;
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(),
input,
+ read_options,
parse_options));
+ ASSERT_RAISES(Invalid, reader->Read());
+ }
+}
+
+TEST(ReaderTest, InferNestedFieldsWithSchema) {
+ std::string json = R"({}
+ {"a": {"c": null}}
+ {"a": {"c": {}}}
+ {"a": {"c": {"d": null}}}
+ {"a": {"c": {"d": []}}}
+ {"a": {"c": {"d": [null]}}}
+ {"a": {"c": {"d": [{}]}}}
+ {"a": {"c": {"d": [{"e": null}]}}}
+ {"a": {"c": {"d": [{"e": true}]}}}
+ )";
+
+ auto read_options = ReadOptions::Defaults();
+ read_options.use_threads = false;
+ auto parse_options = ParseOptions::Defaults();
+ parse_options.explicit_schema =
+ schema({field("a", struct_({field("b", timestamp(TimeUnit::SECOND))}))});
+ parse_options.unexpected_field_behavior = UnexpectedFieldBehavior::InferType;
+
+ auto expected_schema = schema({field(
+ "a", struct_({field("b", timestamp(TimeUnit::SECOND)),
+ field("c", struct_({field(
+ "d", list(struct_({field("e",
boolean())})))}))}))});
+ auto expected_batch = RecordBatchFromJSON(expected_schema, R"([
+ {"a": null},
+ {"a": {"b": null, "c": null}},
+ {"a": {"b": null, "c": {"d": null}}},
+ {"a": {"b": null, "c": {"d": null}}},
+ {"a": {"b": null, "c": {"d": []}}},
+ {"a": {"b": null, "c": {"d": [null]}}},
+ {"a": {"b": null, "c": {"d": [{"e": null}]}}},
+ {"a": {"b": null, "c": {"d": [{"e": null}]}}},
+ {"a": {"b": null, "c": {"d": [{"e": true}]}}}
+ ])");
+ ASSERT_OK_AND_ASSIGN(auto expected_table,
Table::FromRecordBatches({expected_batch}));
+
+ std::shared_ptr<io::InputStream> input;
+ std::shared_ptr<TableReader> reader;
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(), input,
+ read_options, parse_options));
+ ASSERT_OK_AND_ASSIGN(auto actual_table, reader->Read());
+ AssertTablesEqual(*actual_table, *expected_table);
+
+ json += std::string(R"({"a": {"b": "2022-09-05T08:08:46.000"}})") + "\n";
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(), input,
+ read_options, parse_options));
+ ASSERT_RAISES(Invalid, reader->Read());
+}
+
+TEST(ReaderTest, InferNestedFieldsInListWithSchema) {
+ std::string json = R"({}
+ {"a": [{"b": "2022-09-05T08:08:00"}]}
+ {"a": [{"b": "2022-09-05T08:08:01", "c": null}]}
+ {"a": [{"b": "2022-09-05T08:08:02", "c": {"d": true}}]}
+ )";
+
+ auto read_options = ReadOptions::Defaults();
+ read_options.use_threads = false;
+ auto parse_options = ParseOptions::Defaults();
+ parse_options.explicit_schema =
+ schema({field("a", list(struct_({field("b",
timestamp(TimeUnit::SECOND))})))});
+ parse_options.unexpected_field_behavior = UnexpectedFieldBehavior::InferType;
+
+ auto expected_schema =
+ schema({field("a", list(struct_({field("b", timestamp(TimeUnit::SECOND)),
+ field("c", struct_({field("d",
boolean())}))})))});
+ auto expected_batch = RecordBatchFromJSON(expected_schema, R"([
+ {"a": null},
+ {"a": [{"b": "2022-09-05T08:08:00", "c": null}]},
+ {"a": [{"b": "2022-09-05T08:08:01", "c": null}]},
+ {"a": [{"b": "2022-09-05T08:08:02", "c": {"d": true}}]}
+ ])");
+ ASSERT_OK_AND_ASSIGN(auto expected_table,
Table::FromRecordBatches({expected_batch}));
+
+ std::shared_ptr<io::InputStream> input;
+ std::shared_ptr<TableReader> reader;
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(), input,
+ read_options, parse_options));
+ ASSERT_OK_AND_ASSIGN(auto actual_table, reader->Read());
+ AssertTablesEqual(*actual_table, *expected_table);
+
+ json += std::string(R"({"a": {"b": "2022-09-05T08:08:03.000", "c": {}}})") +
"\n";
+ ASSERT_OK(MakeStream(json, &input));
+ ASSERT_OK_AND_ASSIGN(reader, TableReader::Make(default_memory_pool(), input,
+ read_options, parse_options));
+ ASSERT_RAISES(Invalid, reader->Read());
Review Comment:
Same here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]