Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading of CSV files with blank second row #12098

Merged
Merged
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
16 changes: 7 additions & 9 deletions cpp/src/io/csv/reader_impl.cu
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ std::vector<std::string> get_column_names(std::vector<char> const& header,
if (header.size() <= 1) { return col_names; }

std::vector<char> first_row = header;
int num_cols = 0;

bool quotation = false;
for (size_t pos = 0, prev = 0; pos < first_row.size(); ++pos) {
Expand Down Expand Up @@ -163,17 +162,16 @@ std::vector<std::string> get_column_names(std::vector<char> const& header,

const string new_col_name(first_row.data() + prev, col_name_len);
col_names.push_back(removeQuotes(new_col_name, parse_opts.quotechar));

// Stop parsing when we hit the line terminator; relevant when there is
// a blank line following the header. In this case, first_row includes
// multiple line terminators at the end, as the new recStart belongs to
// a line that comes after the blank line(s)
if (!quotation && first_row[pos] == parse_opts.terminator) { break; }
} else {
// This is the first data row, add the automatically generated name
col_names.push_back(prefix + std::to_string(num_cols));
col_names.push_back(prefix + std::to_string(col_names.size()));
}
num_cols++;

// Stop parsing when we hit the line terminator; relevant when there is
// a blank line following the header. In this case, first_row includes
// multiple line terminators at the end, as the new recStart belongs to
// a line that comes after the blank line(s)
if (!quotation && first_row[pos] == parse_opts.terminator) { break; }

// Skip adjacent delimiters if delim_whitespace is set
while (parse_opts.multi_delimiter && pos < first_row.size() &&
Expand Down
25 changes: 25 additions & 0 deletions cpp/tests/io/csv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2244,4 +2244,29 @@ TEST_F(CsvReaderTest, CsvDefaultOptionsWriteReadMatch)
EXPECT_EQ(new_table_and_metadata.metadata.column_names[1], "1");
}

TEST_F(CsvReaderTest, BlankLineAfterFirstRow)
{
std::string csv_in{"12,9., 10\n\n"};

{
cudf::io::csv_reader_options no_header_opts =
cudf::io::csv_reader_options::builder(cudf::io::source_info{csv_in.c_str(), csv_in.size()})
.header(-1);
// No header, getting column names/count from first row
auto result = cudf::io::read_csv(no_header_opts);

const auto result_table = result.tbl->view();
ASSERT_EQ(result_table.num_columns(), 3);
}
{
cudf::io::csv_reader_options header_opts =
cudf::io::csv_reader_options::builder(cudf::io::source_info{csv_in.c_str(), csv_in.size()});
// Getting column names/count from header
auto result = cudf::io::read_csv(header_opts);

const auto result_table = result.tbl->view();
ASSERT_EQ(result_table.num_columns(), 3);
}
}

CUDF_TEST_PROGRAM_MAIN()