@@ -406,45 +406,35 @@ INSTANTIATE_TEST_SUITE_P(
406406 "\n2016-02-29 10:42:23-0700,2016-02-29 17:42:23Z\n")));
407407#endif
408408
409- // GH-36889: Empty batches at the start should not cause duplicate headers
410- TEST (TestWriteCSV, EmptyBatchAtStart) {
409+ TEST (TestWriteCSV, EmptyBatchShouldNotPolluteOutput) {
411410 auto schema = arrow::schema ({field (" col1" , utf8 ())});
412411 auto empty_batch = RecordBatchFromJSON (schema, " []" );
413- auto data_batch = RecordBatchFromJSON (schema, R"( [{"col1": "a"}, {"col1": "b"}])" );
412+ auto batch_a = RecordBatchFromJSON (schema, R"( [{"col1": "a"}])" );
413+ auto batch_b = RecordBatchFromJSON (schema, R"( [{"col1": "b"}])" );
414414
415- // Concatenate empty table with data table
416- ASSERT_OK_AND_ASSIGN (auto empty_table, Table::FromRecordBatches (schema, {empty_batch}));
417- ASSERT_OK_AND_ASSIGN (auto data_table, Table::FromRecordBatches (schema, {data_batch}));
418- ASSERT_OK_AND_ASSIGN (auto combined_table, ConcatenateTables ({empty_table, data_table}));
419-
420- ASSERT_OK_AND_ASSIGN (auto out, io::BufferOutputStream::Create ());
421- ASSERT_OK (WriteCSV (*combined_table, WriteOptions::Defaults (), out.get ()));
422- ASSERT_OK_AND_ASSIGN (auto buffer, out->Finish ());
423-
424- std::string result (reinterpret_cast <const char *>(buffer->data ()), buffer->size ());
425- // Should have exactly one header, not two
426- EXPECT_EQ (result, " \" col1\"\n\" a\"\n\" b\"\n " );
427- }
428-
429- // GH-36889: Empty batches in the middle should not cause issues
430- TEST (TestWriteCSV, EmptyBatchInMiddle) {
431- auto schema = arrow::schema ({field (" col1" , utf8 ())});
432- auto batch1 = RecordBatchFromJSON (schema, R"( [{"col1": "a"}])" );
433- auto empty_batch = RecordBatchFromJSON (schema, " []" );
434- auto batch2 = RecordBatchFromJSON (schema, R"( [{"col1": "b"}])" );
435-
436- ASSERT_OK_AND_ASSIGN (auto table1, Table::FromRecordBatches (schema, {batch1}));
437- ASSERT_OK_AND_ASSIGN (auto empty_table, Table::FromRecordBatches (schema, {empty_batch}));
438- ASSERT_OK_AND_ASSIGN (auto table2, Table::FromRecordBatches (schema, {batch2}));
439- ASSERT_OK_AND_ASSIGN (auto combined_table,
440- ConcatenateTables ({table1, empty_table, table2}));
415+ struct TestParam {
416+ std::shared_ptr<Table> table;
417+ std::string expected_output;
418+ };
441419
442- ASSERT_OK_AND_ASSIGN (auto out, io::BufferOutputStream::Create ());
443- ASSERT_OK (WriteCSV (*combined_table, WriteOptions::Defaults (), out.get ()));
444- ASSERT_OK_AND_ASSIGN (auto buffer, out->Finish ());
420+ std::vector<TestParam> test_params = {
421+ // Empty batch in the beginning
422+ {Table::FromRecordBatches (schema, {empty_batch, batch_a, batch_b}).ValueOrDie (),
423+ " \" col1\"\n\" a\"\n\" b\"\n " },
424+ // Empty batch in the middle
425+ {Table::FromRecordBatches (schema, {batch_a, empty_batch, batch_b}).ValueOrDie (),
426+ " \" col1\"\n\" a\"\n\" b\"\n " },
427+ // Empty batch in the end
428+ {Table::FromRecordBatches (schema, {batch_a, batch_b, empty_batch}).ValueOrDie (),
429+ " \" col1\"\n\" a\"\n\" b\"\n " },
430+ };
445431
446- std::string result (reinterpret_cast <const char *>(buffer->data ()), buffer->size ());
447- EXPECT_EQ (result, " \" col1\"\n\" a\"\n\" b\"\n " );
432+ for (const auto & param : test_params) {
433+ ASSERT_OK_AND_ASSIGN (auto out, io::BufferOutputStream::Create ());
434+ ASSERT_OK (WriteCSV (*param.table , WriteOptions::Defaults (), out.get ()));
435+ ASSERT_OK_AND_ASSIGN (auto buffer, out->Finish ());
436+ EXPECT_EQ (buffer->ToString (), param.expected_output );
437+ }
448438}
449439
450440} // namespace csv
0 commit comments