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::CSVReaderOptions Struct Reference
Collaboration diagram for duckdb::CSVReaderOptions:

Public Member Functions

 CSVReaderOptions (CSVOption< char > single_byte_delimiter, const CSVOption< string > &multi_byte_delimiter)
string GetUserDefinedParameters () const
 Returns a list of user-defined parameters in string format.
void SetCompression (const string &compression)
bool GetHeader () const
void SetHeader (bool has_header)
string GetEscape () const
void SetEscape (const string &escape)
idx_t GetSkipRows () const
void SetSkipRows (int64_t rows)
void SetQuote (const string &quote)
string GetQuote () const
void SetComment (const string &comment)
string GetComment () const
void SetDelimiter (const string &delimiter)
string GetDelimiter () const
bool IgnoreErrors () const
 If we can safely ignore errors (i.e., they are being ignored and not being stored in a rejects table).
string GetNewline () const
void SetNewline (const string &input)
bool GetRFC4180 () const
void SetRFC4180 (bool rfc4180)
CSVOption< char > GetSingleByteDelimiter () const
CSVOption< string > GetMultiByteDelimiter () const
bool SetBaseOption (const string &loption, const Value &value, bool write_option=false)
 Set an option that is supported by both reading and writing functions, called by the SetReadOption and SetWriteOption methods.
void SetReadOption (const string &loption, const Value &value, vector< string > &expected_names)
 loption - lowercase string set - argument(s) to the option expected_names - names expected if the option is "columns"
void SetWriteOption (const string &loption, const Value &value)
void SetDateFormat (LogicalTypeId type, const string &format, bool read_format)
void ToNamedParameters (named_parameter_map_t &out) const
 This function is used to remember options set by the sniffer, for use in ReadCSVRelation.
void FromNamedParameters (const named_parameter_map_t &in, ClientContext &context, MultiFileOptions &file_options)
void ParseOption (ClientContext &context, const string &key, const Value &val)
void Verify (MultiFileOptions &file_options)
 Verify options are not conflicting.
string ToString (const String &current_file_path) const
bool WasTypeManuallySet (idx_t i) const
 If the type for column with idx i was manually set.
string NewLineIdentifierToString () const

Public Attributes

DialectOptions dialect_options
 See struct above.
CSVOption< bool > ignore_errors = false
 Whether we should ignore InvalidInput errors.
CSVOption< bool > store_rejects = false
 Whether we store CSV Errors in the rejects table or not.
CSVOption< string > rejects_table_name = {"reject_errors"}
 Rejects table name (Name of the table the store rejects errors).
CSVOption< string > rejects_scan_name = {"reject_scans"}
 Rejects Scan name (Name of the table the store rejects scans).
idx_t rejects_limit = 0
 Rejects table entry limit (0 = no limit).
idx_t buffer_sample_size = static_cast<idx_t>(STANDARD_VECTOR_SIZE * 50)
 Number of samples to buffer.
vector< string > null_str = {""}
 Specifies the strings that represents a null value.
FileCompressionType compression = FileCompressionType::AUTO_DETECT
 Whether file is compressed or not, and if so which compression type AUTO_DETECT (default; infer from file extension).
bool allow_quoted_nulls = true
 Option to convert quoted values to NULL values.
char comment = '\0'
char thousands_separator = '\0'
 Thousands separator option (to be able to accept "100,000.220" as a double or decimal.
case_insensitive_map_t< idx_t > sql_types_per_column
 SQL Type list mapping of name to SQL type index in sql_type_list.
vector< LogicalType > sql_type_list
 User-defined SQL type list.
vector< string > name_list
 User-defined name list.
bool columns_set = false
 If the names and types were set by the columns parameter.
vector< LogicalType > auto_type_candidates
 Types considered as candidates for auto-detection ordered by ascending specificity (~ from low to high).
string sniffer_user_mismatch_error
 In case the sniffer found a mismatch error from user defined types or dialect.
vector< bool > was_type_manually_set
 In case the sniffer found a mismatch error from user defined types or dialect.
CSVOption< idx_t > maximum_line_size = max_line_size_default
bool normalize_names = false
 Whether header names shall be normalized.
unordered_set< string > force_not_null_names
 True, if column with that index must skip null check.
vector< bool > force_not_null
 True, if column with that index must skip null check.
int64_t files_to_sniff = 10
 In case this is a glob or list of multiple files, how many shall be used to sniff. -1 means all.
idx_t sample_size_chunks = 20480 / sniff_size
 Number of sample chunks used in auto-detection.
bool all_varchar = false
 Consider all columns to be of type varchar.
bool auto_detect = true
 Whether to automatically detect dialect and datatypes.
string file_path
 The file path of the CSV file to read.
CSVOption< idx_t > buffer_size_option = CSVBuffer::ROWS_PER_BUFFER * max_line_size_default
 Buffer Size (Parallel Scan).
string decimal_separator = "."
 Decimal separator when reading as numeric.
bool null_padding = false
 Whether to pad rows that do not have enough columns with NULL values.
bool parallel = true
 If we should attempt to run parallel scanning over one file.
string encoding = "utf-8"
 By default, our encoding is always UTF-8.
map< string, string > user_defined_parameters
 User defined parameters.
vector< bool > force_quote
 True, if column with that index must be quoted.
string prefix
 Prefix/suffix/custom newline the entire file once (enables writing of files as JSON arrays).
string suffix
string write_newline
map< LogicalTypeId, Value > write_date_format = {{LogicalTypeId::DATE, Value()}, {LogicalTypeId::TIMESTAMP, Value()}}
 The date format to use for writing (if any is specified).
map< LogicalTypeId, bool > has_format = {{LogicalTypeId::DATE, false}, {LogicalTypeId::TIMESTAMP, false}}
 Whether a type format is specified.
bool multi_file_reader = false
 If this reader is a multifile reader.

Static Public Attributes

static constexpr idx_t max_line_size_default = 2000000
 Maximum CSV line size: specified because if we reach this amount, we likely have wrong delimiters (default: 2MB) note that this is the guaranteed line length that will succeed, longer lines may be accepted if slightly above.
static constexpr idx_t sniff_size = 2048
 Result size of sniffing phases.

Constructor & Destructor Documentation

◆ CSVReaderOptions() [1/2]

duckdb::CSVReaderOptions::CSVReaderOptions ( )
inline
51719 {
51720 }

◆ CSVReaderOptions() [2/2]

duckdb::CSVReaderOptions::CSVReaderOptions ( CSVOption< char > single_byte_delimiter,
const CSVOption< string > & multi_byte_delimiter )
176188 {
176189 if (multi_byte_delimiter.GetValue().empty()) {
176190 const char single_byte_value = single_byte_delimiter.GetValue();
176191 const string value(1, single_byte_value);
176192 dialect_options.state_machine_options.delimiter = value;
176193 } else {
176194 dialect_options.state_machine_options.delimiter = multi_byte_delimiter;
176195 }
176196}
DialectOptions dialect_options
See struct above.
Definition duckdb.hpp:51726

Member Function Documentation

◆ GetUserDefinedParameters()

string duckdb::CSVReaderOptions::GetUserDefinedParameters ( ) const

Returns a list of user-defined parameters in string format.

176777 {
176778 string result;
176779 for (auto &udf_parameter : user_defined_parameters) {
176780 if (!result.empty()) {
176781 result += ", ";
176782 }
176783 result += udf_parameter.first + "=" + udf_parameter.second;
176784 }
176785 return result;
176786}
map< string, string > user_defined_parameters
User defined parameters.
Definition duckdb.hpp:51811

◆ SetCompression()

void duckdb::CSVReaderOptions::SetCompression ( const string & compression)
176265 {
176266 this->compression = FileCompressionTypeFromString(compression_p);
176267}
FileCompressionType compression
Whether file is compressed or not, and if so which compression type AUTO_DETECT (default; infer from ...
Definition duckdb.hpp:51743

◆ GetHeader()

bool duckdb::CSVReaderOptions::GetHeader ( ) const
176257 {
176258 return this->dialect_options.header.GetValue();
176259}

◆ SetHeader()

void duckdb::CSVReaderOptions::SetHeader ( bool has_header)
176261 {
176262 this->dialect_options.header.Set(input);
176263}

◆ GetEscape()

string duckdb::CSVReaderOptions::GetEscape ( ) const
176269 {
176270 return std::string(1, this->dialect_options.state_machine_options.escape.GetValue());
176271}

◆ SetEscape()

void duckdb::CSVReaderOptions::SetEscape ( const string & escape)
176273 {
176274 auto escape_str = input;
176275 if (escape_str.size() > 1) {
176276 throw InvalidInputException("The escape option cannot exceed a size of 1 byte.");
176277 }
176278 if (escape_str.empty()) {
176279 escape_str = string("\0", 1);
176280 }
176281 this->dialect_options.state_machine_options.escape.Set(escape_str[0]);
176282}

◆ GetSkipRows()

idx_t duckdb::CSVReaderOptions::GetSkipRows ( ) const
176284 {
176285 return NumericCast<idx_t>(this->dialect_options.skip_rows.GetValue());
176286}

◆ SetSkipRows()

void duckdb::CSVReaderOptions::SetSkipRows ( int64_t rows)
176288 {
176289 if (skip_rows < 0) {
176290 throw InvalidInputException("skip_rows option from read_csv scanner, must be equal or higher than 0");
176291 }
176292 dialect_options.skip_rows.Set(NumericCast<idx_t>(skip_rows));
176293}

◆ SetQuote()

void duckdb::CSVReaderOptions::SetQuote ( const string & quote)
176315 {
176316 auto quote_str = quote_p;
176317 if (quote_str.size() > 1) {
176318 throw InvalidInputException("The quote option cannot exceed a size of 1 byte.");
176319 }
176320 if (quote_str.empty()) {
176321 quote_str = string("\0", 1);
176322 }
176323 this->dialect_options.state_machine_options.quote.Set(quote_str[0]);
176324}

◆ GetQuote()

string duckdb::CSVReaderOptions::GetQuote ( ) const
176311 {
176312 return std::string(1, this->dialect_options.state_machine_options.quote.GetValue());
176313}

◆ SetComment()

void duckdb::CSVReaderOptions::SetComment ( const string & comment)
176330 {
176331 auto comment_str = comment_p;
176332 if (comment_str.size() > 1) {
176333 throw InvalidInputException("The comment option cannot exceed a size of 1 byte.");
176334 }
176335 if (comment_str.empty()) {
176336 comment_str = string("\0", 1);
176337 }
176338 this->dialect_options.state_machine_options.comment.Set(comment_str[0]);
176339}

◆ GetComment()

string duckdb::CSVReaderOptions::GetComment ( ) const
176326 {
176327 return std::string(1, this->dialect_options.state_machine_options.comment.GetValue());
176328}

◆ SetDelimiter()

void duckdb::CSVReaderOptions::SetDelimiter ( const string & delimiter)
176299 {
176300 auto delim_str = StringUtil::Replace(input, "\\t", "\t");
176301 if (delim_str.size() > 4) {
176302 throw InvalidInputException("The delimiter option cannot exceed a size of 4 bytes.");
176303 }
176304 if (this->dialect_options.state_machine_options.delimiter.IsSetByUser()) {
176305 // we can't know in which order delim and sep were specified, so we throw an exception here
176306 throw BinderException("CSV Reader function option delim and sep are aliases, only one can be supplied");
176307 }
176308 this->dialect_options.state_machine_options.delimiter.Set(delim_str);
176309}

◆ GetDelimiter()

string duckdb::CSVReaderOptions::GetDelimiter ( ) const
176295 {
176296 return this->dialect_options.state_machine_options.delimiter.GetValue();
176297}

◆ IgnoreErrors()

bool duckdb::CSVReaderOptions::IgnoreErrors ( ) const

If we can safely ignore errors (i.e., they are being ignored and not being stored in a rejects table).

176376 {
176377 return ignore_errors.GetValue() && !store_rejects.GetValue();
176378}
CSVOption< bool > store_rejects
Whether we store CSV Errors in the rejects table or not.
Definition duckdb.hpp:51730
CSVOption< bool > ignore_errors
Whether we should ignore InvalidInput errors.
Definition duckdb.hpp:51728

◆ GetNewline()

string duckdb::CSVReaderOptions::GetNewline ( ) const
176341 {
176342 switch (dialect_options.state_machine_options.new_line.GetValue()) {
176343 case NewLineIdentifier::CARRY_ON:
176344 return "\\r\\n";
176345 case NewLineIdentifier::SINGLE_R:
176346 return "\\r";
176347 case NewLineIdentifier::SINGLE_N:
176348 return "\\n";
176349 case NewLineIdentifier::NOT_SET:
176350 return "";
176351 default:
176352 throw NotImplementedException("New line type not supported");
176353 }
176354}

◆ SetNewline()

void duckdb::CSVReaderOptions::SetNewline ( const string & input)
176356 {
176357 if (input == "\\n") {
176358 dialect_options.state_machine_options.new_line.Set(NewLineIdentifier::SINGLE_N);
176359 } else if (input == "\\r") {
176360 dialect_options.state_machine_options.new_line.Set(NewLineIdentifier::SINGLE_R);
176361 } else if (input == "\\r\\n") {
176362 dialect_options.state_machine_options.new_line.Set(NewLineIdentifier::CARRY_ON);
176363 } else {
176364 throw InvalidInputException("This is not accepted as a newline: " + input);
176365 }
176366}

◆ GetRFC4180()

bool duckdb::CSVReaderOptions::GetRFC4180 ( ) const
176368 {
176369 return this->dialect_options.state_machine_options.strict_mode.GetValue();
176370}

◆ SetRFC4180()

void duckdb::CSVReaderOptions::SetRFC4180 ( bool rfc4180)
176372 {
176373 this->dialect_options.state_machine_options.strict_mode.Set(input);
176374}

◆ GetSingleByteDelimiter()

CSVOption< char > duckdb::CSVReaderOptions::GetSingleByteDelimiter ( ) const
176380 {
176381 auto &delim = dialect_options.state_machine_options.delimiter;
176382 return CSVOption<char>(delim.GetValue()[0], delim.IsSetByUser());
176383}

◆ GetMultiByteDelimiter()

CSVOption< string > duckdb::CSVReaderOptions::GetMultiByteDelimiter ( ) const
176385 {
176386 return dialect_options.state_machine_options.delimiter;
176387}

◆ SetBaseOption()

bool duckdb::CSVReaderOptions::SetBaseOption ( const string & loption,
const Value & value,
bool write_option = false )

Set an option that is supported by both reading and writing functions, called by the SetReadOption and SetWriteOption methods.

176561 {
176562 // Make sure this function was only called after the option was turned into lowercase
176563 D_ASSERT(!std::any_of(loption.begin(), loption.end(), ::isupper));
176564
176565 if (StringUtil::StartsWith(loption, "delim") || StringUtil::StartsWith(loption, "sep")) {
176566 SetDelimiter(ParseString(value, loption));
176567 } else if (loption == "quote") {
176568 SetQuote(ParseString(value, loption));
176569 } else if (loption == "comment") {
176570 SetComment(ParseString(value, loption));
176571 } else if (loption == "new_line") {
176572 SetNewline(ParseString(value, loption));
176573 } else if (loption == "escape") {
176574 SetEscape(ParseString(value, loption));
176575 } else if (loption == "header") {
176576 SetHeader(ParseBoolean(value, loption));
176577 } else if (loption == "nullstr" || loption == "null") {
176578 auto &child_type = value.type();
176579 null_str.clear();
176580 if (child_type.id() != LogicalTypeId::LIST && child_type.id() != LogicalTypeId::VARCHAR) {
176581 throw BinderException("CSV Reader function option %s requires a string or a list as input", loption);
176582 }
176583 if (!null_str.empty()) {
176584 throw BinderException("CSV Reader function option nullstr can only be supplied once");
176585 }
176586 if (child_type.id() == LogicalTypeId::LIST) {
176587 auto &list_child = ListType::GetChildType(child_type);
176588 const vector<Value> *children = nullptr;
176589 if (list_child.id() == LogicalTypeId::LIST) {
176590 // This can happen if it comes from a copy FROM/TO
176591 auto &list_grandchild = ListType::GetChildType(list_child);
176592 auto &children_ref = ListValue::GetChildren(value);
176593 if (list_grandchild.id() != LogicalTypeId::VARCHAR || children_ref.size() != 1) {
176594 throw BinderException("CSV Reader function option %s requires a non-empty list of possible null "
176595 "strings (varchar) as input",
176596 loption);
176597 }
176598 children = &ListValue::GetChildren(children_ref.back());
176599 } else if (list_child.id() != LogicalTypeId::VARCHAR) {
176600 throw BinderException("CSV Reader function option %s requires a non-empty list of possible null "
176601 "strings (varchar) as input",
176602 loption);
176603 }
176604 if (!children) {
176605 children = &ListValue::GetChildren(value);
176606 }
176607 for (auto &child : *children) {
176608 if (child.IsNull()) {
176609 throw BinderException(
176610 "CSV Reader function option %s does not accept NULL values as a valid nullstr option", loption);
176611 }
176612 null_str.push_back(StringValue::Get(child));
176613 }
176614 } else {
176615 null_str.push_back(StringValue::Get(ParseString(value, loption)));
176616 }
176617 if (null_str.size() > 1 && write_option) {
176618 throw BinderException("CSV Writer function option %s only accepts one nullstr value.", loption);
176619 }
176620
176621 } else if (loption == "compression") {
176622 SetCompression(ParseString(value, loption));
176623 } else if (loption == "strict_mode") {
176624 SetRFC4180(ParseBoolean(value, loption));
176625 } else {
176626 // unrecognized option in base CSV
176627 return false;
176628 }
176629 return true;
176630}
static DUCKDB_API bool StartsWith(string str, string prefix)
Returns true if the target string starts with the given prefix.
Definition duckdb.cpp:109377
vector< string > null_str
Specifies the strings that represents a null value.
Definition duckdb.hpp:51740
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetReadOption()

void duckdb::CSVReaderOptions::SetReadOption ( const string & loption,
const Value & value,
vector< string > & expected_names )

loption - lowercase string set - argument(s) to the option expected_names - names expected if the option is "columns"

176406 {
176407 if (SetBaseOption(loption, value)) {
176408 return;
176409 }
176410 if (loption == "auto_detect") {
176411 auto_detect = ParseBoolean(value, loption);
176412 } else if (loption == "sample_size") {
176413 const auto sample_size_option = ParseInteger(value, loption);
176414 if (sample_size_option < 1 && sample_size_option != -1) {
176415 throw BinderException("Unsupported parameter for SAMPLE_SIZE: cannot be smaller than 1");
176416 }
176417 if (sample_size_option == -1) {
176418 // If -1, we basically read the whole thing
176419 sample_size_chunks = NumericLimits<idx_t>().Maximum();
176420 } else {
176421 sample_size_chunks = NumericCast<idx_t>(sample_size_option / STANDARD_VECTOR_SIZE);
176422 if (sample_size_option % STANDARD_VECTOR_SIZE != 0) {
176424 }
176425 }
176426
176427 } else if (loption == "skip") {
176428 SetSkipRows(ParseInteger(value, loption));
176429 } else if (loption == "max_line_size" || loption == "maximum_line_size") {
176430 auto line_size = ParseInteger(value, loption);
176431 if (line_size < 0) {
176432 throw BinderException("Invalid value for MAX_LINE_SIZE parameter: it cannot be smaller than 0");
176433 }
176434 maximum_line_size.Set(NumericCast<idx_t>(line_size));
176435 if (buffer_size_option.IsSetByUser() && maximum_line_size.GetValue() > buffer_size_option.GetValue()) {
176436 throw InvalidInputException("Buffer Size of %d must be a higher value than the maximum line size %d",
176437 buffer_size_option.GetValue(), maximum_line_size.GetValue());
176438 }
176439 } else if (loption == "date_format" || loption == "dateformat") {
176440 string format = ParseString(value, loption);
176441 SetDateFormat(LogicalTypeId::DATE, format, true);
176442 } else if (loption == "timestamp_format" || loption == "timestampformat") {
176443 string format = ParseString(value, loption);
176444 SetDateFormat(LogicalTypeId::TIMESTAMP, format, true);
176445 } else if (loption == "ignore_errors") {
176446 ignore_errors.Set(ParseBoolean(value, loption));
176447 } else if (loption == "buffer_size") {
176448 buffer_size_option.Set(NumericCast<idx_t>(ParseInteger(value, loption)));
176449 if (buffer_size_option == 0) {
176450 throw InvalidInputException("Buffer Size option must be higher than 0");
176451 }
176452 if (maximum_line_size.IsSetByUser() && maximum_line_size.GetValue() > buffer_size_option.GetValue()) {
176453 throw InvalidInputException("Buffer Size of %d must be a higher value than the maximum line size %d",
176454 buffer_size_option.GetValue(), maximum_line_size.GetValue());
176455 } else {
176456 maximum_line_size.Set(buffer_size_option.GetValue(), false);
176457 }
176458 } else if (loption == "decimal_separator") {
176459 decimal_separator = ParseString(value, loption);
176460 if (decimal_separator != "." && decimal_separator != ",") {
176461 throw BinderException("Unsupported parameter for DECIMAL_SEPARATOR: should be '.' or ','");
176462 }
176463 } else if (loption == "null_padding") {
176464 null_padding = ParseBoolean(value, loption);
176465 } else if (loption == "parallel") {
176466 parallel = ParseBoolean(value, loption);
176467 } else if (loption == "allow_quoted_nulls") {
176468 allow_quoted_nulls = ParseBoolean(value, loption);
176469 } else if (loption == "store_rejects") {
176470 store_rejects.Set(ParseBoolean(value, loption));
176471 } else if (loption == "force_not_null") {
176472 if (!expected_names.empty()) {
176473 force_not_null = ParseColumnList(value, expected_names, loption);
176474 } else {
176475 if (value.IsNull()) {
176476 throw BinderException("Invalid value for 'force_not_null' paramenter");
176477 }
176478 // Get the list of columns to use as a recovery key
176479 auto &children = ListValue::GetChildren(value);
176480 for (auto &child : children) {
176481 auto col_name = child.GetValue<string>();
176482 force_not_null_names.insert(col_name);
176483 }
176484 }
176485
176486 } else if (loption == "rejects_table") {
176487 // skip, handled in SetRejectsOptions
176488 auto table_name = ParseString(value, loption);
176489 if (table_name.empty()) {
176490 throw BinderException("REJECTS_TABLE option cannot be empty");
176491 }
176492 if (KeywordHelper::RequiresQuotes(table_name)) {
176493 throw BinderException("rejects_scan option: %s requires quotes to be used as an identifier", table_name);
176494 }
176495 rejects_table_name.Set(table_name);
176496 } else if (loption == "rejects_scan") {
176497 // skip, handled in SetRejectsOptions
176498 auto table_name = ParseString(value, loption);
176499 if (table_name.empty()) {
176500 throw BinderException("rejects_scan option cannot be empty");
176501 }
176502 if (KeywordHelper::RequiresQuotes(table_name)) {
176503 throw BinderException("rejects_scan option: %s requires quotes to be used as an identifier", table_name);
176504 }
176505 rejects_scan_name.Set(table_name);
176506 } else if (loption == "rejects_limit") {
176507 auto limit = ParseInteger(value, loption);
176508 if (limit < 0) {
176509 throw BinderException("Unsupported parameter for REJECTS_LIMIT: cannot be negative");
176510 }
176511 rejects_limit = NumericCast<idx_t>(limit);
176512 } else if (loption == "encoding") {
176513 encoding = ParseString(value, loption);
176514 } else if (loption == "thousands") {
176515 if (!value.IsNull()) {
176516 auto thousands_separator_string = ParseString(value, loption);
176517 if (thousands_separator_string.size() > 1) {
176518 throw BinderException("Unsupported parameter for THOUSANDS: should be max one character");
176519 }
176520 if (!thousands_separator_string.empty()) {
176521 thousands_separator = thousands_separator_string[0];
176522 }
176523 }
176524 } else {
176525 throw BinderException("Unrecognized option for CSV reader \"%s\"", loption);
176526 }
176527}
static bool RequiresQuotes(const string &text, bool allow_caps=true, KeywordCategory category=KeywordCategory::KEYWORD_NONE)
Returns true if the given string needs to be quoted when written as an identifier.
Definition duckdb.cpp:338003
std::basic_string< Char > format(const S &format_str, Args &&... args)
Definition duckdb.cpp:16295
idx_t rejects_limit
Rejects table entry limit (0 = no limit).
Definition duckdb.hpp:51736
bool auto_detect
Whether to automatically detect dialect and datatypes.
Definition duckdb.hpp:51796
string encoding
By default, our encoding is always UTF-8.
Definition duckdb.hpp:51809
bool allow_quoted_nulls
Option to convert quoted values to NULL values.
Definition duckdb.hpp:51745
CSVOption< string > rejects_scan_name
Rejects Scan name (Name of the table the store rejects scans).
Definition duckdb.hpp:51734
unordered_set< string > force_not_null_names
True, if column with that index must skip null check.
Definition duckdb.hpp:51781
CSVOption< string > rejects_table_name
Rejects table name (Name of the table the store rejects errors).
Definition duckdb.hpp:51732
char thousands_separator
Thousands separator option (to be able to accept "100,000.220" as a double or decimal.
Definition duckdb.hpp:51749
bool null_padding
Whether to pad rows that do not have enough columns with NULL values.
Definition duckdb.hpp:51804
idx_t sample_size_chunks
Number of sample chunks used in auto-detection.
Definition duckdb.hpp:51792
CSVOption< idx_t > buffer_size_option
Buffer Size (Parallel Scan).
Definition duckdb.hpp:51800
bool parallel
If we should attempt to run parallel scanning over one file.
Definition duckdb.hpp:51806
vector< bool > force_not_null
True, if column with that index must skip null check.
Definition duckdb.hpp:51783
string decimal_separator
Decimal separator when reading as numeric.
Definition duckdb.hpp:51802
bool SetBaseOption(const string &loption, const Value &value, bool write_option=false)
Set an option that is supported by both reading and writing functions, called by the SetReadOption an...
Definition duckdb.cpp:176561
Here is the call graph for this function:

◆ SetWriteOption()

void duckdb::CSVReaderOptions::SetWriteOption ( const string & loption,
const Value & value )
176529 {
176530 if (loption == "new_line") {
176531 // Steal this from SetBaseOption so we can write different newlines (e.g., format JSON ARRAY)
176532 write_newline = ParseString(value, loption);
176533 return;
176534 }
176535
176536 if (SetBaseOption(loption, value, true)) {
176537 return;
176538 }
176539
176540 if (loption == "force_quote") {
176541 force_quote = ParseColumnList(value, name_list, loption);
176542 } else if (loption == "date_format" || loption == "dateformat") {
176543 string format = ParseString(value, loption);
176544 SetDateFormat(LogicalTypeId::DATE, format, false);
176545 } else if (loption == "timestamp_format" || loption == "timestampformat") {
176546 string format = ParseString(value, loption);
176547 if (StringUtil::Lower(format) == "iso") {
176548 format = "%Y-%m-%dT%H:%M:%S.%fZ";
176549 }
176550 SetDateFormat(LogicalTypeId::TIMESTAMP, format, false);
176551 SetDateFormat(LogicalTypeId::TIMESTAMP_TZ, format, false);
176552 } else if (loption == "prefix") {
176553 prefix = ParseString(value, loption);
176554 } else if (loption == "suffix") {
176555 suffix = ParseString(value, loption);
176556 } else {
176557 throw BinderException("Unrecognized option CSV writer \"%s\"", loption);
176558 }
176559}
static DUCKDB_API string Lower(const string &str)
Convert a string to lowercase.
Definition duckdb.cpp:109630
vector< bool > force_quote
True, if column with that index must be quoted.
Definition duckdb.hpp:51820
vector< string > name_list
User-defined name list.
Definition duckdb.hpp:51759
string prefix
Prefix/suffix/custom newline the entire file once (enables writing of files as JSON arrays).
Definition duckdb.hpp:51822

◆ SetDateFormat()

void duckdb::CSVReaderOptions::SetDateFormat ( LogicalTypeId type,
const string & format,
bool read_format )
176389 {
176390 string error;
176391 const string auto_format = StringUtil::Lower(format);
176392 if (auto_format != "auto") {
176393 if (read_format) {
176394 StrpTimeFormat strpformat;
176395 error = StrTimeFormat::ParseFormatSpecifier(format, strpformat);
176396 dialect_options.date_format[type].Set(strpformat);
176397 } else {
176398 write_date_format[type] = Value(format);
176399 }
176400 if (!error.empty()) {
176401 throw InvalidInputException("Could not parse DATEFORMAT: %s", error.c_str());
176402 }
176403 }
176404}
map< LogicalTypeId, Value > write_date_format
The date format to use for writing (if any is specified).
Definition duckdb.hpp:51827

◆ ToNamedParameters()

void duckdb::CSVReaderOptions::ToNamedParameters ( named_parameter_map_t & out) const

This function is used to remember options set by the sniffer, for use in ReadCSVRelation.

176940 {
176941 auto &delimiter = dialect_options.state_machine_options.delimiter;
176942 auto &quote = dialect_options.state_machine_options.quote;
176943 auto &escape = dialect_options.state_machine_options.escape;
176944 auto &comment = dialect_options.state_machine_options.comment;
176945 auto &strict_mode = dialect_options.state_machine_options.strict_mode;
176946 auto &header = dialect_options.header;
176947 if (delimiter.IsSetByUser()) {
176948 named_params["delim"] = Value(GetDelimiter());
176949 }
176950 if (dialect_options.state_machine_options.new_line.IsSetByUser()) {
176951 named_params["new_line"] = Value(GetNewline());
176952 }
176953 if (quote.IsSetByUser()) {
176954 named_params["quote"] = Value(GetQuote());
176955 }
176956 if (escape.IsSetByUser()) {
176957 named_params["escape"] = Value(GetEscape());
176958 }
176959 if (comment.IsSetByUser()) {
176960 named_params["comment"] = Value(GetComment());
176961 }
176962 if (header.IsSetByUser()) {
176963 named_params["header"] = Value(GetHeader());
176964 }
176965 if (strict_mode.IsSetByUser()) {
176966 named_params["strict_mode"] = Value(GetRFC4180());
176967 }
176968 named_params["max_line_size"] = Value::BIGINT(NumericCast<int64_t>(maximum_line_size.GetValue()));
176969 if (dialect_options.skip_rows.IsSetByUser()) {
176970 named_params["skip"] = Value::UBIGINT(GetSkipRows());
176971 }
176972 named_params["null_padding"] = Value::BOOLEAN(null_padding);
176973 named_params["parallel"] = Value::BOOLEAN(parallel);
176974 if (!dialect_options.date_format.at(LogicalType::DATE).GetValue().format_specifier.empty()) {
176975 named_params["dateformat"] =
176976 Value(dialect_options.date_format.at(LogicalType::DATE).GetValue().format_specifier);
176977 }
176978 if (!dialect_options.date_format.at(LogicalType::TIMESTAMP).GetValue().format_specifier.empty()) {
176979 named_params["timestampformat"] =
176980 Value(dialect_options.date_format.at(LogicalType::TIMESTAMP).GetValue().format_specifier);
176981 }
176982
176983 named_params["normalize_names"] = Value::BOOLEAN(normalize_names);
176984 if (!name_list.empty() && !named_params.count("columns") && !named_params.count("column_names") &&
176985 !named_params.count("names")) {
176986 named_params["column_names"] = StringVectorToValue(name_list);
176987 }
176988 named_params["all_varchar"] = Value::BOOLEAN(all_varchar);
176989}
static DUCKDB_API Value BOOLEAN(bool value)
Create a boolean Value from a specified value.
Definition duckdb.cpp:132118
static DUCKDB_API Value BIGINT(int64_t value)
Create a bigint Value from a specified value.
Definition duckdb.cpp:132146
static DUCKDB_API Value UBIGINT(uint64_t value)
Create an unsigned bigint Value from a specified value.
Definition duckdb.cpp:132202
bool normalize_names
Whether header names shall be normalized.
Definition duckdb.hpp:51779
bool all_varchar
Consider all columns to be of type varchar.
Definition duckdb.hpp:51794
Here is the call graph for this function:

◆ FromNamedParameters()

void duckdb::CSVReaderOptions::FromNamedParameters ( const named_parameter_map_t & in,
ClientContext & context,
MultiFileOptions & file_options )
176789 {
176790 for (auto &kv : in) {
176791 auto loption = StringUtil::Lower(kv.first);
176792 if (MultiFileReader().ParseOption(loption, kv.second, file_options, context)) {
176793 continue;
176794 }
176795 ParseOption(context, kv.first, kv.second);
176796 }
176797}

◆ ParseOption()

void duckdb::CSVReaderOptions::ParseOption ( ClientContext & context,
const string & key,
const Value & val )
176799 {
176800 auto loption = StringUtil::Lower(key);
176801 // skip variables that are specific to auto-detection
176802 if (StoreUserDefinedParameter(loption)) {
176803 user_defined_parameters[loption] = val.ToSQLString();
176804 }
176805 if (loption == "columns") {
176806 if (!name_list.empty()) {
176807 throw BinderException("read_csv column_names/names can only be supplied once");
176808 }
176809 columns_set = true;
176810 auto &child_type = val.type();
176811 if (child_type.id() != LogicalTypeId::STRUCT) {
176812 throw BinderException("read_csv columns requires a struct as input");
176813 }
176814 auto &struct_children = StructValue::GetChildren(val);
176815 D_ASSERT(StructType::GetChildCount(child_type) == struct_children.size());
176816 for (idx_t i = 0; i < struct_children.size(); i++) {
176817 auto &name = StructType::GetChildName(child_type, i);
176818 auto &val = struct_children[i];
176819 name_list.push_back(name);
176820 if (val.type().id() != LogicalTypeId::VARCHAR) {
176821 throw BinderException("read_csv requires a type specification as string");
176822 }
176823 sql_types_per_column[name] = i;
176824 sql_type_list.emplace_back(TransformStringToLogicalType(StringValue::Get(val), context));
176825 }
176826 if (name_list.empty()) {
176827 throw BinderException("read_csv requires at least a single column as input!");
176828 }
176829 } else if (loption == "auto_type_candidates") {
176830 auto_type_candidates.clear();
176831 map<uint8_t, LogicalType> candidate_types;
176832 // We always have the extremes of Null and Varchar, so we can default to varchar if the
176833 // sniffer is not able to confidently detect that column type
176834 candidate_types[GetCandidateSpecificity(LogicalType::VARCHAR)] = LogicalType::VARCHAR;
176835 candidate_types[GetCandidateSpecificity(LogicalType::SQLNULL)] = LogicalType::SQLNULL;
176836
176837 auto &child_type = val.type();
176838 if (child_type.id() != LogicalTypeId::LIST) {
176839 throw BinderException("read_csv auto_types requires a list as input");
176840 }
176841 auto &list_children = ListValue::GetChildren(val);
176842 if (list_children.empty()) {
176843 throw BinderException("auto_type_candidates requires at least one type");
176844 }
176845 for (auto &child : list_children) {
176846 if (child.type().id() != LogicalTypeId::VARCHAR) {
176847 throw BinderException("auto_type_candidates requires a type specification as string");
176848 }
176849 auto candidate_type = TransformStringToLogicalType(StringValue::Get(child), context);
176850 candidate_types[GetCandidateSpecificity(candidate_type)] = candidate_type;
176851 }
176852 for (auto &candidate_type : candidate_types) {
176853 auto_type_candidates.emplace_back(candidate_type.second);
176854 }
176855 } else if (loption == "column_names" || loption == "names") {
176856 unordered_set<string> column_names;
176857 if (!name_list.empty()) {
176858 throw BinderException("read_csv column_names/names can only be supplied once");
176859 }
176860 if (val.IsNull()) {
176861 throw BinderException("read_csv %s cannot be NULL", key);
176862 }
176863 auto &children = ListValue::GetChildren(val);
176864 for (auto &child : children) {
176865 if (child.IsNull()) {
176866 throw BinderException("read_csv %s parameter cannot have a NULL value", key);
176867 }
176868 name_list.push_back(StringValue::Get(child));
176869 }
176870 for (auto &name : name_list) {
176871 bool empty = true;
176872 for (auto &c : name) {
176873 if (!StringUtil::CharacterIsSpace(c)) {
176874 empty = false;
176875 break;
176876 }
176877 }
176878 if (empty) {
176879 throw BinderException("read_csv %s cannot have empty (or all whitespace) value", key);
176880 }
176881 if (column_names.find(name) != column_names.end()) {
176882 throw BinderException("read_csv %s must have unique values. \"%s\" is repeated.", key, name);
176883 }
176884 column_names.insert(name);
176885 }
176886 } else if (loption == "column_types" || loption == "types" || loption == "dtypes") {
176887 auto &child_type = val.type();
176888 if (child_type.id() != LogicalTypeId::STRUCT && child_type.id() != LogicalTypeId::LIST) {
176889 throw BinderException("read_csv %s requires a struct or list as input", key);
176890 }
176891 if (!sql_type_list.empty()) {
176892 throw BinderException("read_csv column_types/types/dtypes can only be supplied once");
176893 }
176894 vector<string> sql_type_names;
176895 if (child_type.id() == LogicalTypeId::STRUCT) {
176896 auto &struct_children = StructValue::GetChildren(val);
176897 D_ASSERT(StructType::GetChildCount(child_type) == struct_children.size());
176898 for (idx_t i = 0; i < struct_children.size(); i++) {
176899 auto &name = StructType::GetChildName(child_type, i);
176900 auto &val = struct_children[i];
176901 if (val.type().id() != LogicalTypeId::VARCHAR) {
176902 throw BinderException("read_csv %s requires a type specification as string", key);
176903 }
176904 sql_type_names.push_back(StringValue::Get(val));
176905 sql_types_per_column[name] = i;
176906 }
176907 } else {
176908 auto &list_child = ListType::GetChildType(child_type);
176909 if (list_child.id() != LogicalTypeId::VARCHAR) {
176910 throw BinderException("read_csv %s requires a list of types (varchar) as input", key);
176911 }
176912 auto &children = ListValue::GetChildren(val);
176913 for (auto &child : children) {
176914 sql_type_names.push_back(StringValue::Get(child));
176915 }
176916 }
176917 sql_type_list.reserve(sql_type_names.size());
176918 for (auto &sql_type : sql_type_names) {
176919 auto def_type = TransformStringToLogicalType(sql_type, context);
176920 if (def_type.id() == LogicalTypeId::UNBOUND) {
176921 throw BinderException("Unrecognized type \"%s\" for read_csv %s definition", sql_type, key);
176922 }
176923 sql_type_list.push_back(std::move(def_type));
176924 }
176925 } else if (loption == "all_varchar") {
176926 all_varchar = GetBooleanValue(loption, val);
176927 } else if (loption == "files_to_sniff") {
176928 files_to_sniff = ParseInteger(val, loption);
176929 if (files_to_sniff < 1 && files_to_sniff != -1) {
176930 throw BinderException(
176931 "Unsupported parameter for files_to_sniff: value must be -1 for all files or higher than one.");
176932 }
176933 } else if (loption == "normalize_names") {
176934 normalize_names = GetBooleanValue(loption, val);
176935 } else {
176936 SetReadOption(loption, val, name_list);
176937 }
176938}
DUCKDB_API string ToSQLString() const
Convert this value to a SQL-parseable string.
Definition duckdb.cpp:133325
static uint8_t GetCandidateSpecificity(const LogicalType &candidate_type)
Definition duckdb.cpp:176701
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
vector< LogicalType > sql_type_list
User-defined SQL type list.
Definition duckdb.hpp:51757
case_insensitive_map_t< idx_t > sql_types_per_column
SQL Type list mapping of name to SQL type index in sql_type_list.
Definition duckdb.hpp:51755
vector< LogicalType > auto_type_candidates
Types considered as candidates for auto-detection ordered by ascending specificity (~ from low to hig...
Definition duckdb.hpp:51763
void SetReadOption(const string &loption, const Value &value, vector< string > &expected_names)
loption - lowercase string set - argument(s) to the option expected_names - names expected if the opt...
Definition duckdb.cpp:176406
int64_t files_to_sniff
In case this is a glob or list of multiple files, how many shall be used to sniff....
Definition duckdb.hpp:51789
bool columns_set
If the names and types were set by the columns parameter.
Definition duckdb.hpp:51761

◆ Verify()

void duckdb::CSVReaderOptions::Verify ( MultiFileOptions & file_options)

Verify options are not conflicting.

176730 {
176731 if (rejects_table_name.IsSetByUser() && !store_rejects.GetValue() && store_rejects.IsSetByUser()) {
176732 throw BinderException("REJECTS_TABLE option is only supported when store_rejects is not manually set to false");
176733 }
176734 if (rejects_scan_name.IsSetByUser() && !store_rejects.GetValue() && store_rejects.IsSetByUser()) {
176735 throw BinderException("REJECTS_SCAN option is only supported when store_rejects is not manually set to false");
176736 }
176737 if (rejects_scan_name.IsSetByUser() || rejects_table_name.IsSetByUser()) {
176738 // Ensure we set store_rejects to true automagically
176739 store_rejects.Set(true, false);
176740 }
176741 // Validate rejects_table options
176742 if (store_rejects.GetValue()) {
176743 if (!ignore_errors.GetValue() && ignore_errors.IsSetByUser()) {
176744 throw BinderException(
176745 "STORE_REJECTS option is only supported when IGNORE_ERRORS is not manually set to false");
176746 }
176747 // Ensure we set ignore errors to true automagically
176748 ignore_errors.Set(true, false);
176749 if (file_options.union_by_name) {
176750 throw BinderException("REJECTS_TABLE option is not supported when UNION_BY_NAME is set to true");
176751 }
176752 }
176753 if (rejects_limit != 0 && !store_rejects.GetValue()) {
176754 throw BinderException("REJECTS_LIMIT option is only supported when REJECTS_TABLE is set to a table name");
176755 }
176756 // Validate CSV Buffer and max_line_size do not conflict.
176757 if (buffer_size_option.IsSetByUser() && maximum_line_size.IsSetByUser()) {
176758 if (buffer_size_option.GetValue() < maximum_line_size.GetValue()) {
176759 throw BinderException("BUFFER_SIZE option was set to %d, while MAX_LINE_SIZE was set to %d. BUFFER_SIZE "
176760 "must have always be set to value bigger than MAX_LINE_SIZE",
176761 buffer_size_option.GetValue(), maximum_line_size.GetValue());
176762 }
176763 } else if (maximum_line_size.IsSetByUser() && maximum_line_size.GetValue() > max_line_size_default) {
176764 // If the max line size is set by the user and bigger than we have by default, we make it part of our buffer
176765 // size decision.
176766 buffer_size_option.Set(CSVBuffer::ROWS_PER_BUFFER * maximum_line_size.GetValue(), false);
176767 }
176768}
static constexpr idx_t ROWS_PER_BUFFER
By default, we use CSV_BUFFER_SIZE to allocate each buffer.
Definition duckdb.hpp:45985
static constexpr idx_t max_line_size_default
Maximum CSV line size: specified because if we reach this amount, we likely have wrong delimiters (de...
Definition duckdb.hpp:51776

◆ ToString()

string duckdb::CSVReaderOptions::ToString ( const String & current_file_path) const
176644 {
176645 auto &delimiter = dialect_options.state_machine_options.delimiter;
176646 auto &quote = dialect_options.state_machine_options.quote;
176647 auto &escape = dialect_options.state_machine_options.escape;
176648 auto &comment = dialect_options.state_machine_options.comment;
176649 auto &new_line = dialect_options.state_machine_options.new_line;
176650 auto &strict_mode = dialect_options.state_machine_options.strict_mode;
176651 auto &skip_rows = dialect_options.skip_rows;
176652
176653 auto &header = dialect_options.header;
176654 string error = " file = " + current_file_path.ToStdString() + "\n ";
176655 // Let's first print options that can either be set by the user or by the sniffer
176656 // delimiter
176657 error += FormatOptionLine("delimiter", delimiter);
176658 // quote
176659 error += FormatOptionLine("quote", quote);
176660 // escape
176661 error += FormatOptionLine("escape", escape);
176662 // newline
176663 error += FormatOptionLine("new_line", new_line);
176664 // has_header
176665 error += FormatOptionLine("header", header);
176666 // skip_rows
176667 error += FormatOptionLine("skip_rows", skip_rows);
176668 // comment
176669 error += FormatOptionLine("comment", comment);
176670 // strict_mode
176671 error += FormatOptionLine("strict_mode", strict_mode);
176672 // date format
176673 error += FormatOptionLine("date_format", dialect_options.date_format.at(LogicalType::DATE));
176674 // timestamp format
176675 error += FormatOptionLine("timestamp_format", dialect_options.date_format.at(LogicalType::TIMESTAMP));
176676
176677 // Now we do options that can only be set by the user, that might hold some general significance
176678 // null padding
176679 error += "null_padding = " + std::to_string(null_padding) + "\n ";
176680 // sample_size
176681 error += "sample_size = " + std::to_string(sample_size_chunks * STANDARD_VECTOR_SIZE) + "\n ";
176682 // ignore_errors
176683 error += "ignore_errors = " + ignore_errors.FormatValue() + "\n ";
176684 // all_varchar
176685 error += "all_varchar = " + std::to_string(all_varchar) + "\n";
176686
176687 // Add information regarding sniffer mismatches (if any)
176689 return error;
176690}
string sniffer_user_mismatch_error
In case the sniffer found a mismatch error from user defined types or dialect.
Definition duckdb.hpp:51768

◆ WasTypeManuallySet()

bool duckdb::CSVReaderOptions::WasTypeManuallySet ( idx_t i) const

If the type for column with idx i was manually set.

176637 {
176638 if (i >= was_type_manually_set.size()) {
176639 return false;
176640 }
176641 return was_type_manually_set[i];
176642}
vector< bool > was_type_manually_set
In case the sniffer found a mismatch error from user defined types or dialect.
Definition duckdb.hpp:51770
Here is the caller graph for this function:

◆ NewLineIdentifierToString()

string duckdb::CSVReaderOptions::NewLineIdentifierToString ( ) const
inline
51883 {
51884 switch (dialect_options.state_machine_options.new_line.GetValue()) {
51885 case NewLineIdentifier::SINGLE_N:
51886 return "\\n";
51887 case NewLineIdentifier::SINGLE_R:
51888 return "\\r";
51889 case NewLineIdentifier::CARRY_ON:
51890 return "\\r\\n";
51891 default:
51892 return "";
51893 }
51894 }

Member Data Documentation

◆ rejects_table_name

CSVOption<string> duckdb::CSVReaderOptions::rejects_table_name = {"reject_errors"}

Rejects table name (Name of the table the store rejects errors).

51732{"reject_errors"};

◆ rejects_scan_name

CSVOption<string> duckdb::CSVReaderOptions::rejects_scan_name = {"reject_scans"}

Rejects Scan name (Name of the table the store rejects scans).

51734{"reject_scans"};

◆ null_str

vector<string> duckdb::CSVReaderOptions::null_str = {""}

Specifies the strings that represents a null value.

51740{""};

◆ auto_type_candidates

vector<LogicalType> duckdb::CSVReaderOptions::auto_type_candidates
Initial value:
= {
LogicalType::VARCHAR, LogicalType::DOUBLE, LogicalType::BIGINT,
LogicalType::TIMESTAMP_TZ, LogicalType::TIMESTAMP, LogicalType::DATE,
LogicalType::TIME, LogicalType::BOOLEAN, LogicalType::SQLNULL}

Types considered as candidates for auto-detection ordered by ascending specificity (~ from low to high).

51763 {
51764 LogicalType::VARCHAR, LogicalType::DOUBLE, LogicalType::BIGINT,
51765 LogicalType::TIMESTAMP_TZ, LogicalType::TIMESTAMP, LogicalType::DATE,
51766 LogicalType::TIME, LogicalType::BOOLEAN, LogicalType::SQLNULL};

◆ write_date_format

map<LogicalTypeId, Value> duckdb::CSVReaderOptions::write_date_format = {{LogicalTypeId::DATE, Value()}, {LogicalTypeId::TIMESTAMP, Value()}}

The date format to use for writing (if any is specified).

51827{{LogicalTypeId::DATE, Value()}, {LogicalTypeId::TIMESTAMP, Value()}};

◆ has_format

map<LogicalTypeId, bool> duckdb::CSVReaderOptions::has_format = {{LogicalTypeId::DATE, false}, {LogicalTypeId::TIMESTAMP, false}}

Whether a type format is specified.

51829{{LogicalTypeId::DATE, false}, {LogicalTypeId::TIMESTAMP, false}};

The documentation for this struct was generated from the following files: