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::CSVStateMachineCache Class Reference
Inheritance diagram for duckdb::CSVStateMachineCache:
Collaboration diagram for duckdb::CSVStateMachineCache:

Public Member Functions

const StateMachine & Get (const CSVStateMachineOptions &state_machine_options)
string GetObjectType () override
optional_idx GetEstimatedCacheMemory () const override

Static Public Member Functions

static CSVStateMachineCache & Get (ClientContext &context)
static string ObjectType ()

Private Member Functions

void Insert (const CSVStateMachineOptions &state_machine_options)

Private Attributes

unordered_map< CSVStateMachineOptions, StateMachine, HashCSVStateMachineConfig > state_machine_cache
 Cache on delimiter|quote|escape|newline.
mutex main_mutex
 Default value for options used to initialize CSV State Machine Cache.

Detailed Description

The CSVStateMachineCache caches state machines, although small ~2kb, the actual creation of multiple State Machines can become a bottleneck on sniffing, when reading very small csv files. Hence, the cache stores State Machines based on their different delimiter|quote|escape options.

Constructor & Destructor Documentation

◆ CSVStateMachineCache()

duckdb::CSVStateMachineCache::CSVStateMachineCache ( )
174506 {
174507 auto default_quote_escape = DialectCandidates::GetDefaultQuoteEscapeCombination();
174508 auto default_delimiter = DialectCandidates::GetDefaultDelimiter();
174509 auto default_comment = DialectCandidates::GetDefaultComment();
174510
174511 for (auto quote_escape : default_quote_escape) {
174512 for (const auto &delimiter : default_delimiter) {
174513 for (const auto &comment : default_comment) {
174514 for (const bool strict_mode : {true, false}) {
174515 Insert({delimiter, quote_escape.quote, quote_escape.escape, comment, NewLineIdentifier::SINGLE_N,
174516 strict_mode});
174517 Insert({delimiter, quote_escape.quote, quote_escape.escape, comment, NewLineIdentifier::SINGLE_R,
174518 strict_mode});
174519 Insert({delimiter, quote_escape.quote, quote_escape.escape, comment, NewLineIdentifier::CARRY_ON,
174520 strict_mode});
174521 }
174522 }
174523 }
174524 }
174525}
static vector< QuoteEscapeCombination > GetDefaultQuoteEscapeCombination()
Default Quote/Escape combinations in priority order.
Definition duckdb.cpp:171576
static vector< string > GetDefaultDelimiter()
Static functions to get defaults of the search space.
Definition duckdb.cpp:171572

Member Function Documentation

◆ Get() [1/2]

CSVStateMachineCache & duckdb::CSVStateMachineCache::Get ( ClientContext & context)
static

Gets a state machine from the cache, if it's not from one the default options It first caches it, then returns it.

174537 {
174538 auto &cache = ObjectCache::GetObjectCache(context);
174539 return *cache.GetOrCreate<CSVStateMachineCache>(CSVStateMachineCache::ObjectType());
174540}
Here is the caller graph for this function:

◆ Get() [2/2]

const StateMachine & duckdb::CSVStateMachineCache::Get ( const CSVStateMachineOptions & state_machine_options)

Gets a state machine from the cache, if it's not from one the default options It first caches it, then returns it.

174527 {
174528 // Custom State Machine, we need to create it and cache it first
174529 const lock_guard<mutex> parallel_lock(main_mutex);
174530 if (state_machine_cache.find(state_machine_options) == state_machine_cache.end()) {
174531 Insert(state_machine_options);
174532 }
174533 const auto &transition_array = state_machine_cache[state_machine_options];
174534 return transition_array;
174535}
mutex main_mutex
Default value for options used to initialize CSV State Machine Cache.
Definition duckdb.cpp:165851
unordered_map< CSVStateMachineOptions, StateMachine, HashCSVStateMachineConfig > state_machine_cache
Cache on delimiter|quote|escape|newline.
Definition duckdb.cpp:165847

◆ ObjectType()

string duckdb::CSVStateMachineCache::ObjectType ( )
inlinestatic
165832 {
165833 return "CSV_STATE_MACHINE_CACHE";
165834 }

◆ GetObjectType()

string duckdb::CSVStateMachineCache::GetObjectType ( )
inlineoverridevirtual

Implements duckdb::ObjectCacheEntry.

165836 {
165837 return ObjectType();
165838 }

◆ GetEstimatedCacheMemory()

optional_idx duckdb::CSVStateMachineCache::GetEstimatedCacheMemory ( ) const
inlineoverridevirtual

Get the rough cache memory usage in bytes for this entry. Used for eviction decisions. Return invalid index to prevent eviction.

Implements duckdb::ObjectCacheEntry.

165840 {
165841 return optional_idx {};
165842 }

◆ Insert()

void duckdb::CSVStateMachineCache::Insert ( const CSVStateMachineOptions & state_machine_options)
private
174059 {
174060 D_ASSERT(state_machine_cache.find(state_machine_options) == state_machine_cache.end());
174061 // Initialize transition array with default values to the Standard option
174062 auto &transition_array = state_machine_cache[state_machine_options];
174063
174064 for (uint32_t i = 0; i < StateMachine::NUM_STATES; i++) {
174065 const auto cur_state = static_cast<CSVState>(i);
174066 switch (cur_state) {
174068 case CSVState::QUOTED:
174070 case CSVState::ESCAPE:
174071 InitializeTransitionArray(transition_array, cur_state, CSVState::QUOTED);
174072 break;
174073 case CSVState::UNQUOTED:
174074 if (state_machine_options.strict_mode.GetValue()) {
174075 // If we have an unquoted state, following rfc 4180, our base state is invalid
174076 InitializeTransitionArray(transition_array, cur_state, CSVState::INVALID);
174077 } else {
174078 // This will allow us to accept unescaped quotes
174079 InitializeTransitionArray(transition_array, cur_state, CSVState::UNQUOTED);
174080 }
174081 break;
174082 case CSVState::COMMENT:
174083 InitializeTransitionArray(transition_array, cur_state, CSVState::COMMENT);
174084 break;
174086 if (state_machine_options.strict_mode.GetValue()) {
174087 // If we have an unquoted state, following rfc 4180, our base state is invalid
174088 InitializeTransitionArray(transition_array, cur_state, CSVState::INVALID);
174089 } else {
174090 // This will allow us to accept unescaped quotes
174091 InitializeTransitionArray(transition_array, cur_state, CSVState::STANDARD);
174092 }
174093 break;
174094 default:
174095 InitializeTransitionArray(transition_array, cur_state, CSVState::STANDARD);
174096 break;
174097 }
174098 }
174099
174100 const auto delimiter_value = state_machine_options.delimiter.GetValue();
174101 uint8_t delimiter_first_byte;
174102 if (!delimiter_value.empty()) {
174103 delimiter_first_byte = static_cast<uint8_t>(delimiter_value[0]);
174104 } else {
174105 delimiter_first_byte = static_cast<uint8_t>('\0');
174106 }
174107 const auto quote = static_cast<uint8_t>(state_machine_options.quote.GetValue());
174108 const auto escape = static_cast<uint8_t>(state_machine_options.escape.GetValue());
174109 const auto comment = static_cast<uint8_t>(state_machine_options.comment.GetValue());
174110
174111 const auto new_line_id = state_machine_options.new_line.GetValue();
174112
174113 const bool multi_byte_delimiter = delimiter_value.size() > 1;
174114
174115 const bool enable_unquoted_escape = state_machine_options.strict_mode.GetValue() == false &&
174116 state_machine_options.quote != state_machine_options.escape &&
174117 state_machine_options.escape != '\0';
174118 // Now set values depending on configuration
174119 // 1) Standard/Invalid State
174120 const vector<uint8_t> std_inv {static_cast<uint8_t>(CSVState::STANDARD), static_cast<uint8_t>(CSVState::INVALID),
174121 static_cast<uint8_t>(CSVState::STANDARD_NEWLINE)};
174122 for (const auto &state : std_inv) {
174123 if (multi_byte_delimiter) {
174124 transition_array[delimiter_first_byte][state] = CSVState::DELIMITER_FIRST_BYTE;
174125 } else {
174126 transition_array[delimiter_first_byte][state] = CSVState::DELIMITER;
174127 }
174128 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174129 transition_array[static_cast<uint8_t>('\r')][state] = CSVState::CARRIAGE_RETURN;
174130 if (state == static_cast<uint8_t>(CSVState::STANDARD_NEWLINE)) {
174131 transition_array[static_cast<uint8_t>('\n')][state] = CSVState::STANDARD;
174132 } else if (!state_machine_options.strict_mode.GetValue()) {
174133 transition_array[static_cast<uint8_t>('\n')][state] = CSVState::RECORD_SEPARATOR;
174134 } else {
174135 transition_array[static_cast<uint8_t>('\n')][state] = CSVState::INVALID;
174136 }
174137 } else if (new_line_id == NewLineIdentifier::SINGLE_N) {
174138 transition_array[static_cast<uint8_t>('\n')][state] = CSVState::RECORD_SEPARATOR;
174139 if (!state_machine_options.strict_mode.GetValue()) {
174140 transition_array[static_cast<uint8_t>('\r')][state] = CSVState::RECORD_SEPARATOR;
174141 } else {
174142 transition_array[static_cast<uint8_t>('\r')][state] = CSVState::INVALID;
174143 }
174144 } else if (new_line_id == NewLineIdentifier::SINGLE_R) {
174145 transition_array[static_cast<uint8_t>('\r')][state] = CSVState::RECORD_SEPARATOR;
174146 if (!state_machine_options.strict_mode.GetValue()) {
174147 transition_array[static_cast<uint8_t>('\n')][state] = CSVState::RECORD_SEPARATOR;
174148 } else {
174149 transition_array[static_cast<uint8_t>('\n')][state] = CSVState::INVALID;
174150 }
174151 } else {
174152 transition_array[static_cast<uint8_t>('\r')][state] = CSVState::RECORD_SEPARATOR;
174153 transition_array[static_cast<uint8_t>('\n')][state] = CSVState::RECORD_SEPARATOR;
174154 }
174155
174156 if (comment != '\0') {
174157 transition_array[comment][state] = CSVState::COMMENT;
174158 }
174159 if (enable_unquoted_escape) {
174160 transition_array[escape][state] = CSVState::UNQUOTED_ESCAPE;
174161 }
174162 }
174163 // 2) Field Separator State
174164 if (quote != '\0') {
174165 transition_array[quote][static_cast<uint8_t>(CSVState::DELIMITER)] = CSVState::QUOTED;
174166 }
174167 if (delimiter_first_byte != ' ') {
174168 transition_array[' '][static_cast<uint8_t>(CSVState::DELIMITER)] = CSVState::EMPTY_SPACE;
174169 }
174170
174171 const vector<uint8_t> delimiter_states {
174174
174175 // These are the same transitions for all delimiter states
174176 for (auto &state : delimiter_states) {
174177 if (multi_byte_delimiter) {
174178 transition_array[delimiter_first_byte][state] = CSVState::DELIMITER_FIRST_BYTE;
174179 } else {
174180 transition_array[delimiter_first_byte][state] = CSVState::DELIMITER;
174181 }
174182 transition_array[static_cast<uint8_t>('\n')][state] = CSVState::RECORD_SEPARATOR;
174183 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174184 transition_array[static_cast<uint8_t>('\r')][state] = CSVState::CARRIAGE_RETURN;
174185 } else {
174186 transition_array[static_cast<uint8_t>('\r')][state] = CSVState::RECORD_SEPARATOR;
174187 }
174188 if (comment != '\0') {
174189 transition_array[comment][static_cast<uint8_t>(CSVState::DELIMITER)] = CSVState::COMMENT;
174190 }
174191 }
174192 // Deal other multi-byte delimiters
174193 if (delimiter_value.size() == 2) {
174194 transition_array[static_cast<uint8_t>(delimiter_value[1])]
174196 } else if (delimiter_value.size() == 3) {
174197 if (delimiter_first_byte == delimiter_value[1]) {
174198 transition_array[static_cast<uint8_t>(delimiter_value[1])]
174200 }
174201 transition_array[static_cast<uint8_t>(delimiter_value[1])]
174203 transition_array[static_cast<uint8_t>(delimiter_value[2])]
174205 } else if (delimiter_value.size() == 4) {
174206 if (delimiter_first_byte == delimiter_value[2]) {
174207 transition_array[static_cast<uint8_t>(delimiter_value[1])]
174209 }
174210 if (delimiter_first_byte == delimiter_value[1] && delimiter_value[1] == delimiter_value[2]) {
174211 transition_array[static_cast<uint8_t>(delimiter_value[1])]
174213 }
174214 transition_array[static_cast<uint8_t>(delimiter_value[1])]
174216 transition_array[static_cast<uint8_t>(delimiter_value[2])]
174218 transition_array[static_cast<uint8_t>(delimiter_value[3])]
174220 }
174221 if (enable_unquoted_escape) {
174222 transition_array[escape][static_cast<uint8_t>(CSVState::DELIMITER)] = CSVState::UNQUOTED_ESCAPE;
174223 }
174224
174225 // 3) Record Separator State
174226 if (multi_byte_delimiter) {
174227 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] =
174229 } else {
174230 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] = CSVState::DELIMITER;
174231 }
174232 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] =
174234 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174235 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] =
174237 } else {
174238 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] =
174240 }
174241 if (quote != '\0') {
174242 transition_array[quote][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] = CSVState::QUOTED;
174243 }
174244 if (delimiter_first_byte != ' ') {
174245 transition_array[' '][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] = CSVState::EMPTY_SPACE;
174246 }
174247 if (comment != '\0') {
174248 transition_array[comment][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] = CSVState::COMMENT;
174249 }
174250 if (enable_unquoted_escape) {
174251 transition_array[escape][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] = CSVState::UNQUOTED_ESCAPE;
174252 }
174253 if (state_machine_options.strict_mode.GetValue()) {
174254 // strict rules to error on the new line delimiter
174255 switch (new_line_id) {
174256 case NewLineIdentifier::CARRY_ON:
174257 case NewLineIdentifier::SINGLE_R:
174258 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] =
174260 break;
174261 case NewLineIdentifier::SINGLE_N:
174262 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::RECORD_SEPARATOR)] =
174264 break;
174265 default:
174266 break;
174267 }
174268 }
174269
174270 // 4) Carriage Return State
174271 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::CARRIAGE_RETURN)] =
174273 if (state_machine_options.strict_mode.GetValue()) {
174274 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::CARRIAGE_RETURN)] =
174276 } else {
174277 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::CARRIAGE_RETURN)] =
174279 }
174280
174281 if (quote != '\0') {
174282 transition_array[quote][static_cast<uint8_t>(CSVState::CARRIAGE_RETURN)] = CSVState::QUOTED;
174283 }
174284 if (delimiter_first_byte != ' ') {
174285 transition_array[' '][static_cast<uint8_t>(CSVState::CARRIAGE_RETURN)] = CSVState::EMPTY_SPACE;
174286 }
174287 if (comment != '\0') {
174288 transition_array[comment][static_cast<uint8_t>(CSVState::CARRIAGE_RETURN)] = CSVState::COMMENT;
174289 }
174290 if (enable_unquoted_escape) {
174291 transition_array[escape][static_cast<uint8_t>(CSVState::CARRIAGE_RETURN)] = CSVState::UNQUOTED_ESCAPE;
174292 }
174293
174294 // 5) Quoted State
174295 transition_array[quote][static_cast<uint8_t>(CSVState::QUOTED)] = CSVState::UNQUOTED;
174296 transition_array['\n'][static_cast<uint8_t>(CSVState::QUOTED)] = CSVState::QUOTED_NEW_LINE;
174297 transition_array['\r'][static_cast<uint8_t>(CSVState::QUOTED)] = CSVState::QUOTED_NEW_LINE;
174298
174299 if (state_machine_options.quote != state_machine_options.escape &&
174300 state_machine_options.escape.GetValue() != '\0') {
174301 transition_array[escape][static_cast<uint8_t>(CSVState::QUOTED)] = CSVState::ESCAPE;
174302 }
174303 // 6) Unquoted State
174304 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::RECORD_SEPARATOR;
174305 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174306 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::UNQUOTED)] =
174308 } else {
174309 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::UNQUOTED)] =
174311 }
174312 if (multi_byte_delimiter) {
174313 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::UNQUOTED)] =
174315 } else {
174316 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::DELIMITER;
174317 }
174318 if (state_machine_options.quote == state_machine_options.escape) {
174319 transition_array[quote][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::QUOTED;
174320 }
174321 if (state_machine_options.strict_mode.GetValue()) {
174322 // strict rules to error on the new line delimiter
174323 switch (new_line_id) {
174324 case NewLineIdentifier::CARRY_ON:
174325 case NewLineIdentifier::SINGLE_R:
174326 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::INVALID;
174327 break;
174328 case NewLineIdentifier::SINGLE_N:
174329 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::INVALID;
174330 break;
174331 default:
174332 break;
174333 }
174334 } else {
174335 if (escape == '\0') {
174336 // If escape is defined, it limits a bit how relaxed quotes can be in a reliable way.
174337 transition_array[quote][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::MAYBE_QUOTED;
174338 } else {
174339 transition_array[quote][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::QUOTED;
174340 }
174341 }
174342 if (comment != '\0') {
174343 transition_array[comment][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::COMMENT;
174344 }
174345 if (delimiter_first_byte != ' ' && quote != ' ' && escape != ' ' && comment != ' ') {
174346 // If space is not a special character, we can safely ignore it in an unquoted state
174347 transition_array[' '][static_cast<uint8_t>(CSVState::UNQUOTED)] = CSVState::UNQUOTED;
174348 }
174349
174350 // 8) Not Set
174351 if (multi_byte_delimiter) {
174352 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::NOT_SET)] =
174354 } else {
174355 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::NOT_SET)] = CSVState::DELIMITER;
174356 }
174357 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::NOT_SET)] = CSVState::RECORD_SEPARATOR;
174358 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174359 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::NOT_SET)] =
174361 } else {
174362 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::NOT_SET)] =
174364 }
174365 if (quote != '\0') {
174366 transition_array[quote][static_cast<uint8_t>(CSVState::NOT_SET)] = CSVState::QUOTED;
174367 }
174368 if (delimiter_first_byte != ' ') {
174369 transition_array[' '][static_cast<uint8_t>(CSVState::NOT_SET)] = CSVState::EMPTY_SPACE;
174370 }
174371 if (comment != '\0') {
174372 transition_array[comment][static_cast<uint8_t>(CSVState::NOT_SET)] = CSVState::COMMENT;
174373 }
174374 if (enable_unquoted_escape) {
174375 transition_array[escape][static_cast<uint8_t>(CSVState::NOT_SET)] = CSVState::UNQUOTED_ESCAPE;
174376 }
174377
174378 // 9) Quoted NewLine
174379 transition_array[quote][static_cast<uint8_t>(CSVState::QUOTED_NEW_LINE)] = CSVState::UNQUOTED;
174380 if (state_machine_options.quote != state_machine_options.escape &&
174381 state_machine_options.escape.GetValue() != '\0') {
174382 transition_array[escape][static_cast<uint8_t>(CSVState::QUOTED_NEW_LINE)] = CSVState::ESCAPE;
174383 }
174384
174385 // 10) Empty Value State (Not first value)
174386 if (multi_byte_delimiter) {
174387 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::EMPTY_SPACE)] =
174389 } else {
174390 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::EMPTY_SPACE)] = CSVState::DELIMITER;
174391 }
174392 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::EMPTY_SPACE)] =
174394 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174395 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::EMPTY_SPACE)] =
174397 } else {
174398 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::EMPTY_SPACE)] =
174400 }
174401 if (quote != '\0') {
174402 transition_array[quote][static_cast<uint8_t>(CSVState::EMPTY_SPACE)] = CSVState::QUOTED;
174403 }
174404 if (comment != '\0') {
174405 transition_array[comment][static_cast<uint8_t>(CSVState::EMPTY_SPACE)] = CSVState::COMMENT;
174406 }
174407 if (enable_unquoted_escape) {
174408 transition_array[escape][static_cast<uint8_t>(CSVState::EMPTY_SPACE)] = CSVState::UNQUOTED_ESCAPE;
174409 }
174410
174411 // 11) Comment State
174412 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::COMMENT)] = CSVState::RECORD_SEPARATOR;
174413 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174414 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::COMMENT)] =
174416 } else {
174417 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::COMMENT)] =
174419 }
174420
174421 // 12) Unquoted Escape State
174422 if (enable_unquoted_escape) {
174423 // Any character can be escaped, so default to STANDARD
174424 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174425 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::UNQUOTED_ESCAPE)] =
174427 }
174428 }
174429
174430 // 13) Escaped Return State
174431 if (enable_unquoted_escape) {
174432 // The new state is STANDARD for \r + \n and \r + ordinary character.
174433 // Other special characters need to be handled.
174434 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::ESCAPED_RETURN)] = CSVState::DELIMITER;
174435 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174436 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::ESCAPED_RETURN)] =
174438 } else {
174439 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::ESCAPED_RETURN)] =
174441 }
174442 if (comment != '\0') {
174443 transition_array[comment][static_cast<uint8_t>(CSVState::ESCAPED_RETURN)] = CSVState::COMMENT;
174444 }
174445 transition_array[escape][static_cast<uint8_t>(CSVState::ESCAPED_RETURN)] = CSVState::UNQUOTED_ESCAPE;
174446 }
174447
174448 // 14) Maybe quoted
174449 transition_array[quote][static_cast<uint8_t>(CSVState::MAYBE_QUOTED)] = CSVState::MAYBE_QUOTED;
174450
174451 transition_array[static_cast<uint8_t>('\n')][static_cast<uint8_t>(CSVState::MAYBE_QUOTED)] =
174453 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174454 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::MAYBE_QUOTED)] =
174456 } else {
174457 transition_array[static_cast<uint8_t>('\r')][static_cast<uint8_t>(CSVState::MAYBE_QUOTED)] =
174459 }
174460 if (multi_byte_delimiter) {
174461 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::MAYBE_QUOTED)] =
174463 } else {
174464 transition_array[delimiter_first_byte][static_cast<uint8_t>(CSVState::MAYBE_QUOTED)] = CSVState::DELIMITER;
174465 }
174466
174467 // Initialize characters we can skip during processing, for Standard and Quoted states
174468 for (idx_t i = 0; i < StateMachine::NUM_TRANSITIONS; i++) {
174469 transition_array.skip_standard[i] = true;
174470 transition_array.skip_quoted[i] = true;
174471 transition_array.skip_comment[i] = true;
174472 }
174473 // For standard states we only care for delimiters \r and \n
174474 transition_array.skip_standard[delimiter_first_byte] = false;
174475 transition_array.skip_standard[static_cast<uint8_t>('\n')] = false;
174476 transition_array.skip_standard[static_cast<uint8_t>('\r')] = false;
174477 transition_array.skip_standard[comment] = false;
174478 if (enable_unquoted_escape) {
174479 transition_array.skip_standard[escape] = false;
174480 }
174481
174482 // For quoted we only care about quote, escape and for delimiters \r and \n
174483 transition_array.skip_quoted[quote] = false;
174484 transition_array.skip_quoted[escape] = false;
174485 transition_array.skip_quoted[static_cast<uint8_t>('\n')] = false;
174486 transition_array.skip_quoted[static_cast<uint8_t>('\r')] = false;
174487
174488 transition_array.skip_comment[static_cast<uint8_t>('\r')] = false;
174489 transition_array.skip_comment[static_cast<uint8_t>('\n')] = false;
174490
174491 transition_array.delimiter = delimiter_first_byte;
174492 transition_array.new_line = static_cast<uint8_t>('\n');
174493 transition_array.carriage_return = static_cast<uint8_t>('\r');
174494 transition_array.quote = quote;
174495 transition_array.escape = escape;
174496
174497 // Shift and OR to replicate across all bytes
174498 ShiftAndReplicateBits(transition_array.delimiter);
174499 ShiftAndReplicateBits(transition_array.new_line);
174500 ShiftAndReplicateBits(transition_array.carriage_return);
174501 ShiftAndReplicateBits(transition_array.quote);
174502 ShiftAndReplicateBits(transition_array.escape);
174503 ShiftAndReplicateBits(transition_array.comment);
174504}
::uint8_t uint8_t
CSVState
All States of CSV parsing.
Definition duckdb.cpp:59173
@ DELIMITER_FIRST_BYTE
----------— Multi-byte Delimiter States (up to 4 bytes) -------------------------------------------—!...
Definition duckdb.cpp:59177
@ NOT_SET
Got to an Invalid State, this should error.
Definition duckdb.cpp:59187
@ STANDARD_NEWLINE
If we are in a comment state, and hence have to skip the whole line.
Definition duckdb.cpp:59191
@ DELIMITER_SECOND_BYTE
State when encountering the first delimiter byte of a multi-byte delimiter.
Definition duckdb.cpp:59178
@ UNQUOTED_ESCAPE
State used for figuring out a new line.
Definition duckdb.cpp:59192
@ QUOTED
State after encountering a carriage return(i.e., \r).
Definition duckdb.cpp:59183
@ CARRIAGE_RETURN
State after encountering a record separator (i.e., ).
Definition duckdb.cpp:59182
@ DELIMITER
Regular unquoted field state.
Definition duckdb.cpp:59175
@ UNQUOTED
State when inside a quoted field.
Definition duckdb.cpp:59184
@ INVALID
State when encountering an escape character (e.g., ).
Definition duckdb.cpp:59186
@ DELIMITER_THIRD_BYTE
State when encountering the second delimiter byte of a multi-byte delimiter.
Definition duckdb.cpp:59179
@ QUOTED_NEW_LINE
If the state is not set, usually the first state before getting the first character.
Definition duckdb.cpp:59188
@ ESCAPE
State when leaving a quoted field.
Definition duckdb.cpp:59185
@ ESCAPED_RETURN
State when encountering an escape character (e.g., ) in an unquoted field.
Definition duckdb.cpp:59193
@ EMPTY_SPACE
If we have a quoted newline.
Definition duckdb.cpp:59189
@ MAYBE_QUOTED
State when the carriage return is preceded by an escape character (for '\r ' newline only).
Definition duckdb.cpp:59194
@ RECORD_SEPARATOR
-----------------------------------------------------------------------------------------------------...
Definition duckdb.cpp:59181
@ COMMENT
If we have empty spaces in the beginning and end of value.
Definition duckdb.cpp:59190
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

Member Data Documentation

◆ main_mutex

mutex duckdb::CSVStateMachineCache::main_mutex
private

Default value for options used to initialize CSV State Machine Cache.

Because the state machine cache can be accessed in Parallel we need a mutex.


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