TODO: this is almost entirely the same as ReaderMixin::Parse(). Refactor?
| 401 | |
| 402 | // TODO: this is almost entirely the same as ReaderMixin::Parse(). Refactor? |
| 403 | Result<ParsedBlock> operator()(const CSVBlock& block) { |
| 404 | constexpr int32_t max_num_rows = std::numeric_limits<int32_t>::max(); |
| 405 | auto parser = std::make_shared<BlockParser>( |
| 406 | io_context_.pool(), parse_options_, num_csv_cols_, num_rows_seen_, max_num_rows); |
| 407 | |
| 408 | std::shared_ptr<Buffer> straddling; |
| 409 | std::vector<std::string_view> views; |
| 410 | if (block.partial->size() != 0 || block.completion->size() != 0) { |
| 411 | if (block.partial->size() == 0) { |
| 412 | straddling = block.completion; |
| 413 | } else if (block.completion->size() == 0) { |
| 414 | straddling = block.partial; |
| 415 | } else { |
| 416 | ARROW_ASSIGN_OR_RAISE( |
| 417 | straddling, |
| 418 | ConcatenateBuffers({block.partial, block.completion}, io_context_.pool())); |
| 419 | } |
| 420 | views = {std::string_view(*straddling), std::string_view(*block.buffer)}; |
| 421 | } else { |
| 422 | views = {std::string_view(*block.buffer)}; |
| 423 | } |
| 424 | uint32_t parsed_size; |
| 425 | if (block.is_final) { |
| 426 | RETURN_NOT_OK(parser->ParseFinal(views, &parsed_size)); |
| 427 | } else { |
| 428 | RETURN_NOT_OK(parser->Parse(views, &parsed_size)); |
| 429 | } |
| 430 | |
| 431 | // `partial + completion` should have been entirely consumed. |
| 432 | const int64_t bytes_before_buffer = block.partial->size() + block.completion->size(); |
| 433 | if (static_cast<int64_t>(parsed_size) < bytes_before_buffer) { |
| 434 | // This can happen if `newlines_in_values` is not enabled and |
| 435 | // `partial + completion` ends with a newline inside a quoted string. |
| 436 | // In this case, the BlockParser stops at the truncated data in the first |
| 437 | // block (see gh-39857). |
| 438 | return Status::Invalid( |
| 439 | "CSV parser got out of sync with chunker. This can mean the data file " |
| 440 | "contains cell values spanning multiple lines; please consider enabling " |
| 441 | "the option 'newlines_in_values'."); |
| 442 | } |
| 443 | |
| 444 | if (count_rows_) { |
| 445 | num_rows_seen_ += parser->total_num_rows(); |
| 446 | } |
| 447 | |
| 448 | if (block.consume_bytes) { |
| 449 | RETURN_NOT_OK(block.consume_bytes(parsed_size)); |
| 450 | } |
| 451 | return ParsedBlock{std::move(parser), block.block_index, |
| 452 | static_cast<int64_t>(parsed_size) + block.bytes_skipped}; |
| 453 | } |
| 454 | |
| 455 | int num_csv_cols() const { return num_csv_cols_; } |
| 456 |
nothing calls this directly
no test coverage detected