diff --git a/cpp/src/io/csv/writer_impl.cu b/cpp/src/io/csv/writer_impl.cu index 3a7b4bace9c..b5a62973b08 100644 --- a/cpp/src/io/csv/writer_impl.cu +++ b/cpp/src/io/csv/writer_impl.cu @@ -285,8 +285,16 @@ void writer::impl::write_chunked_begin(table_view const& table, const table_metadata* metadata, rmm::cuda_stream_view stream) { - if ((metadata != nullptr) && (options_.is_enabled_include_header())) { - auto const& column_names = metadata->column_names; + if (options_.is_enabled_include_header()) { + // need to generate column names if metadata is not provided + std::vector generated_col_names; + if (metadata == nullptr) { + generated_col_names.resize(table.num_columns()); + thrust::tabulate(generated_col_names.begin(), generated_col_names.end(), [](auto idx) { + return std::to_string(idx); + }); + } + auto const& column_names = (metadata == nullptr) ? generated_col_names : metadata->column_names; CUDF_EXPECTS(column_names.size() == static_cast(table.num_columns()), "Mismatch between number of column headers and table columns."); diff --git a/cpp/tests/io/csv_test.cpp b/cpp/tests/io/csv_test.cpp index 5b6270a8be1..a9904fae666 100644 --- a/cpp/tests/io/csv_test.cpp +++ b/cpp/tests/io/csv_test.cpp @@ -407,7 +407,8 @@ TYPED_TEST(CsvFixedPointWriterTest, SingleColumnNegativeScale) auto filepath = temp_env->get_temp_dir() + "FixedPointSingleColumnNegativeScale.csv"; cudf_io::csv_writer_options writer_options = - cudf_io::csv_writer_options::builder(cudf_io::sink_info(filepath), input_table); + cudf_io::csv_writer_options::builder(cudf_io::sink_info(filepath), input_table) + .include_header(false); cudf_io::write_csv(writer_options); @@ -453,7 +454,8 @@ TYPED_TEST(CsvFixedPointWriterTest, SingleColumnPositiveScale) auto filepath = temp_env->get_temp_dir() + "FixedPointSingleColumnPositiveScale.csv"; cudf_io::csv_writer_options writer_options = - cudf_io::csv_writer_options::builder(cudf_io::sink_info(filepath), input_table); + cudf_io::csv_writer_options::builder(cudf_io::sink_info(filepath), input_table) + .include_header(false); cudf_io::write_csv(writer_options); @@ -2198,4 +2200,32 @@ TEST_F(CsvReaderTest, DtypesMapInvalid) EXPECT_THROW(cudf_io::read_csv(in_opts), cudf::logic_error); } +TEST_F(CsvReaderTest, CsvDefaultOptionsWriteReadMatch) +{ + auto const filepath = temp_env->get_temp_dir() + "issue.csv"; + + // make up some kind of dataframe + auto int_column = column_wrapper{10, 20, 30}; + auto str_column = column_wrapper{"abc", "mno", "xyz"}; + cudf::table_view input_table(std::vector{int_column, str_column}); + + // write that dataframe to a csv using default options to some temporary file + cudf_io::csv_writer_options writer_options = + cudf_io::csv_writer_options::builder(cudf_io::sink_info{filepath}, input_table); + cudf_io::write_csv(writer_options); + + // read the temp csv file using default options + cudf_io::csv_reader_options read_options = + cudf_io::csv_reader_options::builder(cudf_io::source_info{filepath}) + .dtypes(std::vector{dtype(), dtype()}); + + cudf_io::table_with_metadata new_table_and_metadata = cudf_io::read_csv(read_options); + + // verify that the tables are identical, or as identical as expected. + const auto new_table_view = new_table_and_metadata.tbl->view(); + CUDF_TEST_EXPECT_TABLES_EQUIVALENT(input_table, new_table_view); + EXPECT_EQ(new_table_and_metadata.metadata.column_names[0], "0"); + EXPECT_EQ(new_table_and_metadata.metadata.column_names[1], "1"); +} + CUDF_TEST_PROGRAM_MAIN()