Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::CSVErrorHandler Class Reference
Collaboration diagram for duckdb::CSVErrorHandler:

Public Member Functions

 CSVErrorHandler (bool ignore_errors=false)
void Error (const CSVError &csv_error, bool force_error=false)
 Throws the error.
void ErrorIfNeeded ()
 If we have a cached error, and we can now error, we error.
void ErrorIfAny ()
 Throw an error if there are any.
void ErrorIfTypeExists (CSVErrorType error_type)
 Throws an error if a given type exists.
void Insert (idx_t boundary_idx, idx_t rows)
 Inserts a finished error info.
idx_t GetLine (const LinesPerBoundary &error_info)
void NewMaxLineSize (idx_t scan_line_size)
bool AnyErrors ()
 Returns true if there are any errors.
bool HasError (CSVErrorType error_type)
CSVError GetFirstError (CSVErrorType error_type)
idx_t GetMaxLineLength ()
void DontPrintErrorLine ()
void SetIgnoreErrors (bool ignore_errors)
idx_t GetSize ()
void FillRejectsTable (InternalAppender &errors_appender, idx_t file_idx, idx_t scan_idx, const CSVFileScan &file, CSVRejectsTable &rejects, const MultiFileBindData &bind_data, idx_t limit)

Private Member Functions

bool PrintLineNumber (const CSVError &error) const
void ThrowError (const CSVError &csv_error)
 Method that actually throws the error.
bool CanGetLine (idx_t boundary_index)
 If we processed all boundaries before the one that error-ed.
idx_t GetLineInternal (const LinesPerBoundary &error_info)
 Return the 1-indexed line number.

Private Attributes

mutex main_mutex
 CSV Error Handler Mutex.
unordered_map< idx_t, LinesPerBoundary > lines_per_batch_map
 Map of <boundary indexes> -> lines read.
idx_t max_line_length = 0
bool ignore_errors = false
bool print_line = true
vector< CSVError > errors
 Set of errors.

Constructor & Destructor Documentation

◆ CSVErrorHandler()

duckdb::CSVErrorHandler::CSVErrorHandler ( bool ignore_errors = false)
explicit
175556 : ignore_errors(ignore_errors_p) {
175557}

Member Function Documentation

◆ Error()

void duckdb::CSVErrorHandler::Error ( const CSVError & csv_error,
bool force_error = false )

Throws the error.

175598 {
175599 lock_guard<mutex> parallel_lock(main_mutex);
175600 if (!force_error && (ignore_errors || (PrintLineNumber(csv_error) && !CanGetLine(csv_error.GetBoundaryIndex())))) {
175601 // We store this error, we can't throw it now, or we are ignoring it
175602 errors.push_back(csv_error);
175603 return;
175604 }
175605 // Otherwise we can throw directly
175606 ThrowError(csv_error);
175607}
mutex main_mutex
CSV Error Handler Mutex.
Definition duckdb.cpp:166306
vector< CSVError > errors
Set of errors.
Definition duckdb.cpp:166313
bool CanGetLine(idx_t boundary_index)
If we processed all boundaries before the one that error-ed.
Definition duckdb.cpp:176153
bool PrintLineNumber(const CSVError &error) const
Definition duckdb.cpp:176135
void ThrowError(const CSVError &csv_error)
Method that actually throws the error.
Definition duckdb.cpp:175559
Here is the call graph for this function:

◆ ErrorIfNeeded()

void duckdb::CSVErrorHandler::ErrorIfNeeded ( )

If we have a cached error, and we can now error, we error.

175609 {
175610 lock_guard<mutex> parallel_lock(main_mutex);
175611 if (ignore_errors || errors.empty()) {
175612 // Nothing to error
175613 return;
175614 }
175615
175616 if (CanGetLine(errors[0].error_info.boundary_idx)) {
175617 ThrowError(errors[0]);
175618 }
175619}
Here is the call graph for this function:

◆ ErrorIfAny()

void duckdb::CSVErrorHandler::ErrorIfAny ( )

Throw an error if there are any.

175621 {
175622 lock_guard<mutex> parallel_lock(main_mutex);
175623 if (ignore_errors || errors.empty()) {
175624 // Nothing to error
175625 return;
175626 }
175627 if (!CanGetLine(errors[0].error_info.boundary_idx)) {
175628 throw InternalException("Failed to get error information for boundary index");
175629 }
175630 ThrowError(errors[0]);
175631}
Here is the call graph for this function:

◆ ErrorIfTypeExists()

void duckdb::CSVErrorHandler::ErrorIfTypeExists ( CSVErrorType error_type)

Throws an error if a given type exists.

175633 {
175634 lock_guard<mutex> parallel_lock(main_mutex);
175635 for (auto &error : errors) {
175636 if (error.type == error_type) {
175637 // If it's a maximum line size error, we can do it now.
175638 ThrowError(error);
175639 }
175640 }
175641}
Here is the call graph for this function:

◆ Insert()

void duckdb::CSVErrorHandler::Insert ( idx_t boundary_idx,
idx_t rows )

Inserts a finished error info.

175643 {
175644 lock_guard<mutex> parallel_lock(main_mutex);
175645 if (lines_per_batch_map.find(boundary_idx) == lines_per_batch_map.end()) {
175646 lines_per_batch_map[boundary_idx] = {boundary_idx, rows};
175647 } else {
175648 lines_per_batch_map[boundary_idx].lines_in_batch += rows;
175649 }
175650}
unordered_map< idx_t, LinesPerBoundary > lines_per_batch_map
Map of <boundary indexes> -> lines read.
Definition duckdb.cpp:166308
int rows

◆ GetLine()

idx_t duckdb::CSVErrorHandler::GetLine ( const LinesPerBoundary & error_info)
176162 {
176163 lock_guard<mutex> parallel_lock(main_mutex);
176164 return GetLineInternal(error_info);
176165}
idx_t GetLineInternal(const LinesPerBoundary &error_info)
Return the 1-indexed line number.
Definition duckdb.cpp:176166

◆ NewMaxLineSize()

void duckdb::CSVErrorHandler::NewMaxLineSize ( idx_t scan_line_size)
175652 {
175653 lock_guard<mutex> parallel_lock(main_mutex);
175654 max_line_length = std::max(scan_line_size, max_line_length);
175655}

◆ AnyErrors()

bool duckdb::CSVErrorHandler::AnyErrors ( )

Returns true if there are any errors.

175657 {
175658 lock_guard<mutex> parallel_lock(main_mutex);
175659 return !errors.empty();
175660}

◆ HasError()

bool duckdb::CSVErrorHandler::HasError ( CSVErrorType error_type)
175662 {
175663 lock_guard<mutex> parallel_lock(main_mutex);
175664 for (const auto &er : errors) {
175665 if (er.type == error_type) {
175666 return true;
175667 }
175668 }
175669 return false;
175670}

◆ GetFirstError()

CSVError duckdb::CSVErrorHandler::GetFirstError ( CSVErrorType error_type)
175672 {
175673 lock_guard<mutex> parallel_lock(main_mutex);
175674 for (const auto &er : errors) {
175675 if (er.type == error_type) {
175676 return er;
175677 }
175678 }
175679 throw InternalException("CSVErrorHandler::GetFirstError was called without having an appropriate error type");
175680}

◆ GetMaxLineLength()

idx_t duckdb::CSVErrorHandler::GetMaxLineLength ( )
175794 {
175795 lock_guard<mutex> parallel_lock(main_mutex);
175796 return max_line_length;
175797}

◆ DontPrintErrorLine()

void duckdb::CSVErrorHandler::DontPrintErrorLine ( )
175799 {
175800 lock_guard<mutex> parallel_lock(main_mutex);
175801 print_line = false;
175802}

◆ SetIgnoreErrors()

void duckdb::CSVErrorHandler::SetIgnoreErrors ( bool ignore_errors)
175804 {
175805 lock_guard<mutex> parallel_lock(main_mutex);
175806 ignore_errors = ignore_errors_p;
175807}

◆ GetSize()

idx_t duckdb::CSVErrorHandler::GetSize ( )
175682 {
175683 lock_guard<mutex> parallel_lock(main_mutex);
175684 return errors.size();
175685}

◆ FillRejectsTable()

void duckdb::CSVErrorHandler::FillRejectsTable ( InternalAppender & errors_appender,
idx_t file_idx,
idx_t scan_idx,
const CSVFileScan & file,
CSVRejectsTable & rejects,
const MultiFileBindData & bind_data,
idx_t limit )
175724 {
175725 lock_guard<mutex> parallel_lock(main_mutex);
175726 // We first insert the file into the file scans table
175727 for (auto &error : file.error_handler->errors) {
175728 if (!IsCSVErrorAcceptedReject(error.type)) {
175729 continue;
175730 }
175731 // short circuit if we already have too many rejects
175732 if (limit == 0 || rejects.count < limit) {
175733 if (limit != 0 && rejects.count >= limit) {
175734 break;
175735 }
175736 rejects.count++;
175737 const auto row_line = file.error_handler->GetLineInternal(error.error_info);
175738 const auto col_idx = error.column_idx;
175739 // Add the row to the rejects table
175740 errors_appender.BeginRow();
175741 // 1. Scan ID
175742 errors_appender.Append(scan_idx);
175743 // 2. File ID
175744 errors_appender.Append(file_idx);
175745 // 3. Row Line
175746 errors_appender.Append(row_line);
175747 // 4. Byte Position of the row error
175748 errors_appender.Append(error.row_byte_position + 1);
175749 // 5. Byte Position where error occurred
175750 if (!error.byte_position.IsValid()) {
175751 // This means this error comes from a flush, and we don't support this yet, so we give it
175752 // a null
175753 errors_appender.Append(Value());
175754 } else {
175755 errors_appender.Append(error.byte_position.GetIndex() + 1);
175756 }
175757 // 6. Column Index
175758 if (error.type == CSVErrorType::MAXIMUM_LINE_SIZE) {
175759 errors_appender.Append(Value());
175760 } else {
175761 errors_appender.Append(col_idx + 1);
175762 }
175763 // 7. Column Name (If Applicable)
175764 switch (error.type) {
175767 errors_appender.Append(Value());
175768 break;
175770 if (col_idx + 1 < bind_data.names.size()) {
175771 errors_appender.Append(string_t(bind_data.names[col_idx + 1]));
175772 } else {
175773 errors_appender.Append(Value());
175774 }
175775 break;
175776 default:
175777 if (col_idx < bind_data.names.size()) {
175778 errors_appender.Append(string_t(bind_data.names[col_idx]));
175779 } else {
175780 errors_appender.Append(Value());
175781 }
175782 }
175783 // 8. Error Type
175784 errors_appender.Append(string_t(CSVErrorTypeToEnum(error.type)));
175785 // 9. Original CSV Line
175786 errors_appender.Append(string_t(error.csv_row));
175787 // 10. Full Error Message
175788 errors_appender.Append(string_t(error.error_message));
175789 errors_appender.EndRow();
175790 }
175791 }
175792}
@ TOO_MANY_COLUMNS
If the CSV has too few columns.
Definition duckdb.cpp:166188
@ TOO_FEW_COLUMNS
If there is a mismatch between Column Names and Types.
Definition duckdb.cpp:166187
@ MAXIMUM_LINE_SIZE
If something went wrong during sniffing and was not possible to find suitable candidates.
Definition duckdb.cpp:166191

◆ PrintLineNumber()

bool duckdb::CSVErrorHandler::PrintLineNumber ( const CSVError & error) const
private

Private methods should always be locked by parent method. If we should print the line of an error

176135 {
176136 if (!print_line) {
176137 return false;
176138 }
176139 switch (error.type) {
176140 case CAST_ERROR:
176141 case UNTERMINATED_QUOTES:
176142 case TOO_FEW_COLUMNS:
176143 case TOO_MANY_COLUMNS:
176144 case MAXIMUM_LINE_SIZE:
176146 case INVALID_ENCODING:
176147 return true;
176148 default:
176149 return false;
176150 }
176151}
@ INVALID_ENCODING
If the null_padding option is set, and we have quoted new values in parallel.
Definition duckdb.cpp:166193
@ UNTERMINATED_QUOTES
If the CSV has too many column.
Definition duckdb.cpp:166189
@ NULLPADDED_QUOTED_NEW_VALUE
Maximum line size was exceeded by a line in the CSV File.
Definition duckdb.cpp:166192
Here is the caller graph for this function:

◆ ThrowError()

void duckdb::CSVErrorHandler::ThrowError ( const CSVError & csv_error)
private

Method that actually throws the error.

175559 {
175560 auto error_to_throw = csv_error;
175561 idx_t error_to_throw_row = GetLineInternal(error_to_throw.error_info);
175562 if (PrintLineNumber(error_to_throw) && !errors.empty()) {
175563 // We stored a previous error here, we pick the one that happens the earliest to throw
175564 for (const auto &error : errors) {
175565 if (CanGetLine(error.GetBoundaryIndex())) {
175566 idx_t cur_error_to_throw = GetLineInternal(error.error_info);
175567 if (cur_error_to_throw < error_to_throw_row) {
175568 error_to_throw = error;
175569 error_to_throw_row = cur_error_to_throw;
175570 }
175571 }
175572 }
175573 }
175574 std::ostringstream error;
175575 if (PrintLineNumber(error_to_throw)) {
175576 error << "CSV Error on Line: " << error_to_throw_row << '\n';
175577 if (!error_to_throw.csv_row.empty()) {
175578 error << "Original Line: " << error_to_throw.csv_row << '\n';
175579 }
175580 }
175581 if (error_to_throw.full_error_message.empty()) {
175582 error << error_to_throw.error_message;
175583 } else {
175584 error << error_to_throw.full_error_message;
175585 }
175586 switch (csv_error.type) {
175587 case CAST_ERROR:
175588 throw ConversionException(error.str());
175590 throw BinderException(error.str());
175592 throw ParameterNotAllowedException(error.str());
175593 default:
175594 throw InvalidInputException(error.str());
175595 }
175596}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
@ COLUMN_NAME_TYPE_MISMATCH
If when casting a value from string to the column type fails.
Definition duckdb.cpp:166186
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CanGetLine()

bool duckdb::CSVErrorHandler::CanGetLine ( idx_t boundary_index)
private

If we processed all boundaries before the one that error-ed.

176153 {
176154 for (idx_t i = 0; i < boundary_index; i++) {
176155 if (lines_per_batch_map.find(i) == lines_per_batch_map.end()) {
176156 return false;
176157 }
176158 }
176159 return true;
176160}
Here is the caller graph for this function:

◆ GetLineInternal()

idx_t duckdb::CSVErrorHandler::GetLineInternal ( const LinesPerBoundary & error_info)
private

Return the 1-indexed line number.

176166 {
176167 // We start from one, since the lines are 1-indexed
176168 idx_t current_line = 1 + error_info.lines_in_batch;
176169 for (idx_t boundary_idx = 0; boundary_idx < error_info.boundary_idx; boundary_idx++) {
176170 current_line += lines_per_batch_map[boundary_idx].lines_in_batch;
176171 }
176172 return current_line;
176173}
Here is the caller graph for this function:

The documentation for this class was generated from the following file: