Skip to content
Closed
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
26 changes: 19 additions & 7 deletions cpp/src/arrow/dataset/file_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,19 @@ json::ParseOptions GetInitialParseOptions(json::ParseOptions options) {
return options;
}

// `is_final` means `data` holds the whole file, so a trailing object without a
// newline is complete.
Result<std::shared_ptr<StructType>> ParseToStructType(
std::string_view data, const json::ParseOptions& parse_options, MemoryPool* pool) {
std::string_view data, bool is_final, const json::ParseOptions& parse_options,
MemoryPool* pool) {
auto full_buffer = std::make_shared<Buffer>(data);
std::shared_ptr<Buffer> buffer, partial;
auto chunker = json::MakeChunker(parse_options);
RETURN_NOT_OK(chunker->Process(full_buffer, &buffer, &partial));
if (is_final) {
buffer = full_buffer;
} else {
auto chunker = json::MakeChunker(parse_options);
RETURN_NOT_OK(chunker->Process(full_buffer, &buffer, &partial));
}

std::unique_ptr<json::BlockParser> parser;
RETURN_NOT_OK(json::BlockParser::Make(pool, parse_options, &parser));
Expand All @@ -151,10 +158,11 @@ Result<std::shared_ptr<StructType>> ParseToStructType(
return checked_pointer_cast<StructType>(parsed->type());
}

Result<std::shared_ptr<Schema>> ParseToSchema(std::string_view data,
Result<std::shared_ptr<Schema>> ParseToSchema(std::string_view data, bool is_final,
const json::ParseOptions& parse_options,
MemoryPool* pool) {
ARROW_ASSIGN_OR_RAISE(auto type, ParseToStructType(data, parse_options, pool));
ARROW_ASSIGN_OR_RAISE(auto type,
ParseToStructType(data, is_final, parse_options, pool));
return schema(type->fields());
}

Expand Down Expand Up @@ -289,8 +297,10 @@ Result<Future<ReaderPtr>> DoOpenReader(
// schema for the reader based on the dataset schema.
ARROW_ASSIGN_OR_RAISE(auto first_block,
state->stream->Peek(state->read_options.block_size));
const bool is_final =
static_cast<int64_t>(first_block.size()) < state->read_options.block_size;
ARROW_ASSIGN_OR_RAISE(auto physical_schema,
ParseToSchema(first_block, state->parse_options,
ParseToSchema(first_block, is_final, state->parse_options,
state->scan_options->pool));
ARROW_ASSIGN_OR_RAISE(state->parse_options.explicit_schema,
GetPartialSchema(*state->scan_options, *physical_schema));
Expand Down Expand Up @@ -344,8 +354,10 @@ Result<std::shared_ptr<InspectedFragment>> DoInspectFragment(
stream->Peek(format_options.read_options.block_size));

auto parse_options = GetInitialParseOptions(format_options.parse_options);
const bool is_final =
static_cast<int64_t>(first_block.size()) < format_options.read_options.block_size;
ARROW_ASSIGN_OR_RAISE(auto struct_type,
ParseToStructType(first_block, parse_options, pool));
ParseToStructType(first_block, is_final, parse_options, pool));

std::vector<std::string> column_names;
column_names.reserve(struct_type->num_fields());
Expand Down
18 changes: 18 additions & 0 deletions cpp/src/arrow/dataset/file_json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ class JsonScanMixin {
ASSERT_EQ(num_rows, 3);
}

void TestScanSingleLineWithoutNewline() {
// GH-45394
auto source = ToFileSource("{\"i\":7}");
auto fragment = this_->MakeFragment(*source);
this_->SetSchema({field("i", int64())});
this_->SetJsonOptions();

RecordBatchVector batches;
for (auto maybe_batch : this_->Batches(fragment)) {
ASSERT_OK_AND_ASSIGN(auto batch, maybe_batch);
batches.push_back(std::move(batch));
}
ASSERT_OK_AND_ASSIGN(auto table, Table::FromRecordBatches(batches));
AssertTablesEqual(*TableFromJSON(schema({field("i", int64())}), {R"([{"i": 7}])"}),
*table);
}

void TestScanWithParallelDecoding() {
// Test intra-fragment parallelism, which the JSON reader supports independent of the
// scanner. We set a small block size to stress thread usage alongside the scanner's
Expand Down Expand Up @@ -313,6 +330,7 @@ TEST_P(TestJsonScan, ScanWithBOM) { TestScanWithBOM(); }
TEST_P(TestJsonScan, ScanWithCustomParseOptions) { TestScanWithCustomParseOptions(); }
TEST_P(TestJsonScan, ScanWithCustomBlockSize) { TestScanWithCustomBlockSize(); }
TEST_P(TestJsonScan, ScanWithParallelDecoding) { TestScanWithParallelDecoding(); }
TEST_P(TestJsonScan, ScanSingleLineWithoutNewline) { TestScanSingleLineWithoutNewline(); }

INSTANTIATE_TEST_SUITE_P(TestJsonScan, TestJsonScan,
::testing::ValuesIn(TestFormatParams::Values()),
Expand Down
Loading