174059 {
174061
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) {
174071 InitializeTransitionArray(transition_array, cur_state,
CSVState::QUOTED);
174072 break;
174074 if (state_machine_options.strict_mode.GetValue()) {
174075
174077 } else {
174078
174080 }
174081 break;
174084 break;
174086 if (state_machine_options.strict_mode.GetValue()) {
174087
174089 } else {
174090
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
174119
174122 for (const auto &state : std_inv) {
174123 if (multi_byte_delimiter) {
174125 } else {
174127 }
174128 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174131 transition_array[
static_cast<uint8_t>(
'\n')][state] = CSVState::STANDARD;
174132 } else if (!state_machine_options.strict_mode.GetValue()) {
174134 } else {
174136 }
174137 } else if (new_line_id == NewLineIdentifier::SINGLE_N) {
174139 if (!state_machine_options.strict_mode.GetValue()) {
174141 } else {
174143 }
174144 } else if (new_line_id == NewLineIdentifier::SINGLE_R) {
174146 if (!state_machine_options.strict_mode.GetValue()) {
174148 } else {
174150 }
174151 } else {
174154 }
174155
174156 if (comment != '\0') {
174158 }
174159 if (enable_unquoted_escape) {
174161 }
174162 }
174163
174164 if (quote != '\0') {
174166 }
174167 if (delimiter_first_byte != ' ') {
174169 }
174170
174171 const vector<uint8_t> delimiter_states {
174174
174175
174176 for (auto &state : delimiter_states) {
174177 if (multi_byte_delimiter) {
174179 } else {
174181 }
174183 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174185 } else {
174187 }
174188 if (comment != '\0') {
174190 }
174191 }
174192
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) {
174223 }
174224
174225
174226 if (multi_byte_delimiter) {
174229 } else {
174231 }
174234 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174237 } else {
174240 }
174241 if (quote != '\0') {
174243 }
174244 if (delimiter_first_byte != ' ') {
174246 }
174247 if (comment != '\0') {
174249 }
174250 if (enable_unquoted_escape) {
174252 }
174253 if (state_machine_options.strict_mode.GetValue()) {
174254
174255 switch (new_line_id) {
174256 case NewLineIdentifier::CARRY_ON:
174257 case NewLineIdentifier::SINGLE_R:
174260 break;
174261 case NewLineIdentifier::SINGLE_N:
174264 break;
174265 default:
174266 break;
174267 }
174268 }
174269
174270
174273 if (state_machine_options.strict_mode.GetValue()) {
174276 } else {
174279 }
174280
174281 if (quote != '\0') {
174283 }
174284 if (delimiter_first_byte != ' ') {
174286 }
174287 if (comment != '\0') {
174289 }
174290 if (enable_unquoted_escape) {
174292 }
174293
174294
174298
174299 if (state_machine_options.quote != state_machine_options.escape &&
174300 state_machine_options.escape.GetValue() != '\0') {
174302 }
174303
174305 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174308 } else {
174311 }
174312 if (multi_byte_delimiter) {
174315 } else {
174317 }
174318 if (state_machine_options.quote == state_machine_options.escape) {
174320 }
174321 if (state_machine_options.strict_mode.GetValue()) {
174322
174323 switch (new_line_id) {
174324 case NewLineIdentifier::CARRY_ON:
174325 case NewLineIdentifier::SINGLE_R:
174327 break;
174328 case NewLineIdentifier::SINGLE_N:
174330 break;
174331 default:
174332 break;
174333 }
174334 } else {
174335 if (escape == '\0') {
174336
174338 } else {
174340 }
174341 }
174342 if (comment != '\0') {
174344 }
174345 if (delimiter_first_byte != ' ' && quote != ' ' && escape != ' ' && comment != ' ') {
174346
174348 }
174349
174350
174351 if (multi_byte_delimiter) {
174354 } else {
174356 }
174358 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174361 } else {
174364 }
174365 if (quote != '\0') {
174367 }
174368 if (delimiter_first_byte != ' ') {
174370 }
174371 if (comment != '\0') {
174373 }
174374 if (enable_unquoted_escape) {
174376 }
174377
174378
174380 if (state_machine_options.quote != state_machine_options.escape &&
174381 state_machine_options.escape.GetValue() != '\0') {
174383 }
174384
174385
174386 if (multi_byte_delimiter) {
174389 } else {
174391 }
174394 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174397 } else {
174400 }
174401 if (quote != '\0') {
174403 }
174404 if (comment != '\0') {
174406 }
174407 if (enable_unquoted_escape) {
174409 }
174410
174411
174413 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174416 } else {
174419 }
174420
174421
174422 if (enable_unquoted_escape) {
174423
174424 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174427 }
174428 }
174429
174430
174431 if (enable_unquoted_escape) {
174432
174433
174435 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174438 } else {
174441 }
174442 if (comment != '\0') {
174444 }
174446 }
174447
174448
174450
174453 if (new_line_id == NewLineIdentifier::CARRY_ON) {
174456 } else {
174459 }
174460 if (multi_byte_delimiter) {
174463 } else {
174465 }
174466
174467
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
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
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
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}
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