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

[Enhancement] Provide clear error message if skip_headers is set in select from files option #49503

Merged
merged 1 commit into from
Aug 8, 2024
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
13 changes: 12 additions & 1 deletion be/src/exec/csv_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,18 @@ Status CSVScanner::_init_reader() {
if (_parse_options.skip_header) {
for (int64_t i = 0; i < _parse_options.skip_header; i++) {
CSVReader::Record dummy;
RETURN_IF_ERROR(_curr_reader->next_record(&dummy));
auto st = _curr_reader->next_record(&dummy);
if (!st.ok()) {
if (st.is_end_of_file()) {
auto err_msg = fmt::format(
"The parameter 'skip_header' is set to {}, but there are only {} rows in the csv file",
_parse_options.skip_header, i);

return Status::EndOfFile(err_msg);
} else {
return st;
}
}
}
}
return Status::OK();
Expand Down
26 changes: 26 additions & 0 deletions be/test/exec/csv_scanner_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,32 @@ TEST_P(CSVScannerTest, test_flexible_column_mapping) {
EXPECT_EQ("[10, NULL, 'grapefruit', '2021-02-19', 'grapefruit', NULL]", chunk->debug_row(2));
}

TEST_P(CSVScannerTest, test_skip_headers) {
std::vector<TBrokerRangeDesc> ranges;
TBrokerRangeDesc range;
range.__set_path("./be/test/exec/test_data/csv_scanner/small.csv");
range.__set_num_of_columns_from_file(0);
ranges.push_back(range);

TBrokerScanRangeParams* params = _obj_pool.add(new TBrokerScanRangeParams());
params->__set_row_delimiter('\n');
// there are only 2 rows within file small.csv
// if we set skip first 3 line, we expect to get a clear error message
params->__set_skip_header(3);
params->__set_column_separator(',');
params->__set_enclose('"');
params->__set_escape('\\');

auto scanner = create_csv_scanner({}, ranges, params);
EXPECT_OK(scanner->open());
std::vector<SlotDescriptor> schema;
auto st = scanner->get_schema(&schema);
EXPECT_FALSE(st.ok());
EXPECT_EQ(0, schema.size());
EXPECT_EQ(st.to_string(false),
"End of file: The parameter 'skip_header' is set to 3, but there are only 2 rows in the csv file");
}

INSTANTIATE_TEST_CASE_P(CSVScannerTestParams, CSVScannerTest, Values(true, false));
INSTANTIATE_TEST_CASE_P(CSVScannerTestParams, CSVScannerTrimSpaceTest, Values(true));

Expand Down
Loading