Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.9k
[fix](be) Align CSV V2 enclosed field parsing#65501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c80d6557d2dd9d0be70b50cd8739File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| #pragma once | ||
| #include <cstddef> | ||
| namespace doris { | ||
| inline bool is_hive_text_separator_escaped(const char* data, size_t separator_pos, | ||
| char escape_char) { | ||
| size_t escape_count = 0; | ||
| while (separator_pos > escape_count && data[separator_pos - escape_count - 1] == escape_char) { | ||
| ++escape_count; | ||
| } | ||
| return escape_count % 2 == 1; | ||
| } | ||
| } // namespace doris |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -134,14 +134,13 @@ Status CsvReader::_create_line_reader() { | ||
| text_line_reader_ctx = std::make_shared<PlainTextLineReaderCtx>( | ||
| _line_delimiter, _line_delimiter.size(), _keep_cr); | ||
| } else { | ||
| // The enclosed-line context finds logical records that may span physical newlines. | ||
| // Field slicing still happens in `_split_line()` because the v2 scan request may ask | ||
| // for CSV ordinals in a different order from the physical file. | ||
| const size_t col_sep_num = | ||
| _source_file_slot_descs.size() > 1 ? _source_file_slot_descs.size() - 1 : 0; | ||
| text_line_reader_ctx = std::make_shared<EncloseCsvLineReaderCtx>( | ||
| _enclose_reader_ctx = std::make_shared<EncloseCsvLineReaderCtx>( | ||
| _line_delimiter, _line_delimiter.size(), _value_separator, | ||
| _value_separator.size(), col_sep_num, _enclose, _escape, _keep_cr); | ||
| _value_separator.size(), col_sep_num, _enclose, _escape, _keep_cr, | ||
| _start_offset == 0); | ||
| text_line_reader_ctx = _enclose_reader_ctx; | ||
| } | ||
| _line_reader = NewPlainTextLineReader::create_unique( | ||
| _profile, _file_reader, _decompressor.get(), std::move(text_line_reader_ctx), _size, | ||
| @@ -174,77 +173,40 @@ void CsvReader::_split_line(const Slice& line) { | ||
| return; | ||
| } | ||
| // The text line reader is responsible for split boundaries and multi-line quoted fields. | ||
| // Field slicing still happens here because FileScannerV2 asks columns by file-local id, so we | ||
| // must be able to materialize only the requested CSV ordinals without building a row object. | ||
| // Example: for `1,"a,b",10` and column separator `,`, this loop returns three slices: | ||
| // `1`, `a,b`, and `10`; the comma inside quotes does not create an extra field. | ||
| bool in_quote = false; | ||
| bool escaped = false; | ||
| size_t start = 0; | ||
| size_t i = 0; | ||
| while (i < line.size) { | ||
| const char ch = line.data[i]; | ||
| if (_enclose != 0) { | ||
| if (escaped) { | ||
| escaped = false; | ||
| ++i; | ||
| continue; | ||
| } | ||
| if (_escape != 0 && ch == _escape) { | ||
| escaped = true; | ||
| ++i; | ||
| continue; | ||
| } | ||
| if (ch == _enclose) { | ||
| if (in_quote && i + 1 < line.size && line.data[i + 1] == _enclose) { | ||
| i += 2; | ||
| continue; | ||
| } | ||
| in_quote = !in_quote; | ||
| ++i; | ||
| continue; | ||
| } | ||
| const auto append_value = [&](size_t value_start, size_t value_len) { | ||
| while (_trim_tailing_spaces && value_len > 0 && | ||
| line.data[value_start + value_len - 1] == ' ') { | ||
| --value_len; | ||
| } | ||
| if (!in_quote && starts_with_at(line, i, _value_separator)) { | ||
| size_t value_start = start; | ||
| size_t value_len = i - start; | ||
| while (_trim_tailing_spaces && value_len > 0 && | ||
| line.data[value_start + value_len - 1] == ' ') { | ||
| --value_len; | ||
| } | ||
| if (_trim_double_quotes && value_len > 1 && line.data[value_start] == '"' && | ||
| line.data[value_start + value_len - 1] == '"') { | ||
| ++value_start; | ||
| value_len -= 2; | ||
| } else if (_enclose != 0 && value_len > 1 && line.data[value_start] == _enclose && | ||
| line.data[value_start + value_len - 1] == _enclose) { | ||
| ++value_start; | ||
| value_len -= 2; | ||
| } | ||
| _split_values.emplace_back(line.data + value_start, value_len); | ||
| i += _value_separator.size(); | ||
| start = i; | ||
| continue; | ||
| if (_enclose != 0 && value_len > 1 && line.data[value_start] == _enclose && | ||
| line.data[value_start + value_len - 1] == _enclose) { | ||
| ++value_start; | ||
| value_len -= 2; | ||
| } | ||
| ++i; | ||
| } | ||
| _split_values.emplace_back(line.data + value_start, value_len); | ||
| }; | ||
| size_t value_start = start; | ||
| size_t value_len = line.size - start; | ||
| while (_trim_tailing_spaces && value_len > 0 && line.data[value_start + value_len - 1] == ' ') { | ||
| --value_len; | ||
| } | ||
| if (_trim_double_quotes && value_len > 1 && line.data[value_start] == '"' && | ||
| line.data[value_start + value_len - 1] == '"') { | ||
| ++value_start; | ||
| value_len -= 2; | ||
| } else if (_enclose != 0 && value_len > 1 && line.data[value_start] == _enclose && | ||
| line.data[value_start + value_len - 1] == _enclose) { | ||
| ++value_start; | ||
| value_len -= 2; | ||
| size_t value_start = 0; | ||
| if (_enclose_reader_ctx != nullptr) { | ||
| for (const size_t separator_position : _enclose_reader_ctx->column_sep_positions()) { | ||
Gabriel39 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| DORIS_CHECK_LE(value_start, separator_position); | ||
| DORIS_CHECK_LE(separator_position, line.size); | ||
| append_value(value_start, separator_position - value_start); | ||
| value_start = separator_position + _value_separator.size(); | ||
Gabriel39 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } else { | ||
| for (size_t i = 0; i < line.size;) { | ||
| if (starts_with_at(line, i, _value_separator)) { | ||
| append_value(value_start, i - value_start); | ||
| i += _value_separator.size(); | ||
| value_start = i; | ||
| } else { | ||
| ++i; | ||
| } | ||
| } | ||
| } | ||
| _split_values.emplace_back(line.data + value_start, value_len); | ||
| DORIS_CHECK_LE(value_start, line.size); | ||
| append_value(value_start, line.size - value_start); | ||
| } | ||
| Status CsvReader::_deserialize_one_cell(const RequestedColumn& column, IColumn* output, | ||
| @@ -261,7 +223,8 @@ Status CsvReader::_deserialize_one_cell(const RequestedColumn& column, IColumn* | ||
| // CSV keeps empty-field handling separate from null_format matching. An empty | ||
| // null_format must not turn every empty CSV field into NULL unless FE explicitly sets | ||
| // empty_field_as_null; OpenCSV-compatible tables expect empty fields to stay empty strings. | ||
| if (_options.null_len > 0 && value.size == _options.null_len && | ||
| const bool quoted = _options.converted_from_string && value.trim_double_quotes(); | ||
| if (!quoted && _options.null_len > 0 && value.size == _options.null_len && | ||
| std::memcmp(value.data, _options.null_format, value.size) == 0) { | ||
| null_column.insert_data(nullptr, 0); | ||
| return Status::OK(); | ||
| @@ -292,4 +255,10 @@ bool CsvReader::_can_split() const { | ||
| _file_format_type == TFileFormatType::FORMAT_CSV_PLAIN); | ||
| } | ||
| void CsvReader::_on_bom_removed(size_t bom_size) { | ||
| if (_enclose_reader_ctx != nullptr) { | ||
| _enclose_reader_ctx->adjust_column_sep_positions(bom_size); | ||
| } | ||
| } | ||
| } // namespace doris::format::csv | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.