be/src/format/csv/csv_reader.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "format/csv/csv_reader.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/PlanNodes_types.h> |
22 | | #include <gen_cpp/Types_types.h> |
23 | | #include <glog/logging.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <cstddef> |
27 | | #include <map> |
28 | | #include <memory> |
29 | | #include <ostream> |
30 | | #include <regex> |
31 | | #include <utility> |
32 | | |
33 | | #include "common/compiler_util.h" // IWYU pragma: keep |
34 | | #include "common/consts.h" |
35 | | #include "common/status.h" |
36 | | #include "core/block/block.h" |
37 | | #include "core/block/column_with_type_and_name.h" |
38 | | #include "core/data_type/data_type_factory.hpp" |
39 | | #include "exec/scan/scanner.h" |
40 | | #include "format/file_reader/new_plain_binary_line_reader.h" |
41 | | #include "format/file_reader/new_plain_text_line_reader.h" |
42 | | #include "format/line_reader.h" |
43 | | #include "io/file_factory.h" |
44 | | #include "io/fs/broker_file_reader.h" |
45 | | #include "io/fs/buffered_reader.h" |
46 | | #include "io/fs/file_reader.h" |
47 | | #include "io/fs/s3_file_reader.h" |
48 | | #include "io/fs/tracing_file_reader.h" |
49 | | #include "runtime/descriptors.h" |
50 | | #include "runtime/runtime_state.h" |
51 | | #include "util/decompressor.h" |
52 | | #include "util/string_util.h" |
53 | | #include "util/utf8_check.h" |
54 | | |
55 | | namespace doris { |
56 | | class RuntimeProfile; |
57 | | class IColumn; |
58 | | namespace io { |
59 | | struct IOContext; |
60 | | enum class FileCachePolicy : uint8_t; |
61 | | } // namespace io |
62 | | } // namespace doris |
63 | | |
64 | | namespace doris { |
65 | | #include "common/compile_check_begin.h" |
66 | | |
67 | 714 | void EncloseCsvTextFieldSplitter::do_split(const Slice& line, std::vector<Slice>* splitted_values) { |
68 | 714 | const char* data = line.data; |
69 | 714 | const auto& column_sep_positions = _text_line_reader_ctx->column_sep_positions(); |
70 | 714 | size_t value_start_offset = 0; |
71 | 2.60k | for (auto idx : column_sep_positions) { |
72 | 2.60k | process_value_func(data, value_start_offset, idx - value_start_offset, _trimming_char, |
73 | 2.60k | splitted_values); |
74 | 2.60k | value_start_offset = idx + _value_sep_len; |
75 | 2.60k | } |
76 | 714 | if (line.size >= value_start_offset) { |
77 | | // process the last column |
78 | 712 | process_value_func(data, value_start_offset, line.size - value_start_offset, _trimming_char, |
79 | 712 | splitted_values); |
80 | 712 | } |
81 | 714 | } |
82 | | |
83 | | void PlainCsvTextFieldSplitter::_split_field_single_char(const Slice& line, |
84 | 12.1M | std::vector<Slice>* splitted_values) { |
85 | 12.1M | const char* data = line.data; |
86 | 12.1M | const size_t size = line.size; |
87 | 12.1M | size_t value_start = 0; |
88 | 1.55G | for (size_t i = 0; i < size; ++i) { |
89 | 1.54G | if (data[i] == _value_sep[0]) { |
90 | 189M | process_value_func(data, value_start, i - value_start, _trimming_char, splitted_values); |
91 | 189M | value_start = i + _value_sep_len; |
92 | 189M | } |
93 | 1.54G | } |
94 | 12.1M | process_value_func(data, value_start, size - value_start, _trimming_char, splitted_values); |
95 | 12.1M | } |
96 | | |
97 | | void PlainCsvTextFieldSplitter::_split_field_multi_char(const Slice& line, |
98 | 162 | std::vector<Slice>* splitted_values) { |
99 | 162 | size_t start = 0; // point to the start pos of next col value. |
100 | 162 | size_t curpos = 0; // point to the start pos of separator matching sequence. |
101 | | |
102 | | // value_sep : AAAA |
103 | | // line.data : 1234AAAA5678 |
104 | | // -> 1234,5678 |
105 | | |
106 | | // start start |
107 | | // ▼ ▼ |
108 | | // 1234AAAA5678\0 |
109 | | // ▲ ▲ |
110 | | // curpos curpos |
111 | | |
112 | | //kmp |
113 | 162 | std::vector<int> next(_value_sep_len); |
114 | 162 | next[0] = -1; |
115 | 403 | for (int i = 1, j = -1; i < _value_sep_len; i++) { |
116 | 261 | while (j > -1 && _value_sep[i] != _value_sep[j + 1]) { |
117 | 20 | j = next[j]; |
118 | 20 | } |
119 | 241 | if (_value_sep[i] == _value_sep[j + 1]) { |
120 | 130 | j++; |
121 | 130 | } |
122 | 241 | next[i] = j; |
123 | 241 | } |
124 | | |
125 | 6.96k | for (int i = 0, j = -1; i < line.size; i++) { |
126 | | // i : line |
127 | | // j : _value_sep |
128 | 7.09k | while (j > -1 && line[i] != _value_sep[j + 1]) { |
129 | 290 | j = next[j]; |
130 | 290 | } |
131 | 6.80k | if (line[i] == _value_sep[j + 1]) { |
132 | 2.17k | j++; |
133 | 2.17k | } |
134 | 6.80k | if (j == _value_sep_len - 1) { |
135 | 832 | curpos = i - _value_sep_len + 1; |
136 | | |
137 | | /* |
138 | | * column_separator : "xx" |
139 | | * data.csv : data1xxxxdata2 |
140 | | * |
141 | | * Parse incorrectly: |
142 | | * data1[xx]xxdata2 |
143 | | * data1x[xx]xdata2 |
144 | | * data1xx[xx]data2 |
145 | | * The string "xxxx" is parsed into three "xx" delimiters. |
146 | | * |
147 | | * Parse correctly: |
148 | | * data1[xx]xxdata2 |
149 | | * data1xx[xx]data2 |
150 | | */ |
151 | | |
152 | 832 | if (curpos >= start) { |
153 | 803 | process_value_func(line.data, start, curpos - start, _trimming_char, |
154 | 803 | splitted_values); |
155 | 803 | start = i + 1; |
156 | 803 | } |
157 | | |
158 | 832 | j = next[j]; |
159 | 832 | } |
160 | 6.80k | } |
161 | 162 | process_value_func(line.data, start, line.size - start, _trimming_char, splitted_values); |
162 | 162 | } |
163 | | |
164 | 12.2M | void PlainCsvTextFieldSplitter::do_split(const Slice& line, std::vector<Slice>* splitted_values) { |
165 | 12.2M | if (is_single_char_delim) { |
166 | 12.2M | _split_field_single_char(line, splitted_values); |
167 | 18.4E | } else { |
168 | 18.4E | _split_field_multi_char(line, splitted_values); |
169 | 18.4E | } |
170 | 12.2M | } |
171 | | |
172 | | CsvReader::CsvReader(RuntimeState* state, RuntimeProfile* profile, ScannerCounter* counter, |
173 | | const TFileScanRangeParams& params, const TFileRangeDesc& range, |
174 | | const std::vector<SlotDescriptor*>& file_slot_descs, io::IOContext* io_ctx, |
175 | | std::shared_ptr<io::IOContext> io_ctx_holder) |
176 | 2.40k | : _profile(profile), |
177 | 2.40k | _params(params), |
178 | 2.40k | _file_reader(nullptr), |
179 | 2.40k | _line_reader(nullptr), |
180 | 2.40k | _decompressor(nullptr), |
181 | 2.40k | _state(state), |
182 | 2.40k | _counter(counter), |
183 | 2.40k | _range(range), |
184 | 2.40k | _file_slot_descs(file_slot_descs), |
185 | 2.40k | _line_reader_eof(false), |
186 | 2.40k | _skip_lines(0), |
187 | 2.40k | _io_ctx(io_ctx), |
188 | 2.40k | _io_ctx_holder(std::move(io_ctx_holder)) { |
189 | 2.40k | if (_io_ctx == nullptr && _io_ctx_holder) { |
190 | 0 | _io_ctx = _io_ctx_holder.get(); |
191 | 0 | } |
192 | 2.40k | _file_format_type = _params.format_type; |
193 | 2.40k | _is_proto_format = _file_format_type == TFileFormatType::FORMAT_PROTO; |
194 | 2.40k | if (_range.__isset.compress_type) { |
195 | | // for compatibility |
196 | 586 | _file_compress_type = _range.compress_type; |
197 | 1.81k | } else { |
198 | 1.81k | _file_compress_type = _params.compress_type; |
199 | 1.81k | } |
200 | 2.40k | _size = _range.size; |
201 | | |
202 | 2.40k | _split_values.reserve(_file_slot_descs.size()); |
203 | 2.40k | _init_system_properties(); |
204 | 2.40k | _init_file_description(); |
205 | 2.40k | _serdes = create_data_type_serdes(_file_slot_descs); |
206 | 2.40k | } |
207 | | |
208 | 2.40k | void CsvReader::_init_system_properties() { |
209 | 2.40k | if (_range.__isset.file_type) { |
210 | | // for compatibility |
211 | 566 | _system_properties.system_type = _range.file_type; |
212 | 1.83k | } else { |
213 | 1.83k | _system_properties.system_type = _params.file_type; |
214 | 1.83k | } |
215 | 2.40k | _system_properties.properties = _params.properties; |
216 | 2.40k | _system_properties.hdfs_params = _params.hdfs_params; |
217 | 2.40k | if (_params.__isset.broker_addresses) { |
218 | 1.70k | _system_properties.broker_addresses.assign(_params.broker_addresses.begin(), |
219 | 1.70k | _params.broker_addresses.end()); |
220 | 1.70k | } |
221 | 2.40k | } |
222 | | |
223 | 2.40k | void CsvReader::_init_file_description() { |
224 | 2.40k | _file_description.path = _range.path; |
225 | 2.40k | _file_description.file_size = _range.__isset.file_size ? _range.file_size : -1; |
226 | 2.40k | if (_range.__isset.fs_name) { |
227 | 0 | _file_description.fs_name = _range.fs_name; |
228 | 0 | } |
229 | 2.40k | } |
230 | | |
231 | 2.02k | Status CsvReader::init_reader(bool is_load) { |
232 | | // set the skip lines and start offset |
233 | 2.02k | _start_offset = _range.start_offset; |
234 | 2.02k | if (_start_offset == 0) { |
235 | | // check header typer first |
236 | 2.02k | if (_params.__isset.file_attributes && _params.file_attributes.__isset.header_type && |
237 | 2.02k | !_params.file_attributes.header_type.empty()) { |
238 | 44 | std::string header_type = to_lower(_params.file_attributes.header_type); |
239 | 44 | if (header_type == BeConsts::CSV_WITH_NAMES) { |
240 | 24 | _skip_lines = 1; |
241 | 24 | } else if (header_type == BeConsts::CSV_WITH_NAMES_AND_TYPES) { |
242 | 20 | _skip_lines = 2; |
243 | 20 | } |
244 | 1.98k | } else if (_params.file_attributes.__isset.skip_lines) { |
245 | 1.98k | _skip_lines = _params.file_attributes.skip_lines; |
246 | 1.98k | } |
247 | 2.02k | } else if (_start_offset != 0) { |
248 | 0 | if ((_file_compress_type != TFileCompressType::PLAIN) || |
249 | 0 | (_file_compress_type == TFileCompressType::UNKNOWN && |
250 | 0 | _file_format_type != TFileFormatType::FORMAT_CSV_PLAIN)) { |
251 | 0 | return Status::InternalError<false>("For now we do not support split compressed file"); |
252 | 0 | } |
253 | | // pre-read to promise first line skipped always read |
254 | 0 | int64_t pre_read_len = std::min( |
255 | 0 | static_cast<int64_t>(_params.file_attributes.text_params.line_delimiter.size()), |
256 | 0 | _start_offset); |
257 | 0 | _start_offset -= pre_read_len; |
258 | 0 | _size += pre_read_len; |
259 | | // not first range will always skip one line |
260 | 0 | _skip_lines = 1; |
261 | 0 | } |
262 | | |
263 | 2.02k | _use_nullable_string_opt.resize(_file_slot_descs.size()); |
264 | 17.7k | for (int i = 0; i < _file_slot_descs.size(); ++i) { |
265 | 15.7k | auto data_type_ptr = _file_slot_descs[i]->get_data_type_ptr(); |
266 | 15.7k | if (data_type_ptr->is_nullable() && is_string_type(data_type_ptr->get_primitive_type())) { |
267 | 15.7k | _use_nullable_string_opt[i] = 1; |
268 | 15.7k | } |
269 | 15.7k | } |
270 | | |
271 | 2.02k | RETURN_IF_ERROR(_init_options()); |
272 | 2.02k | RETURN_IF_ERROR(_create_file_reader(false)); |
273 | 2.02k | RETURN_IF_ERROR(_create_decompressor()); |
274 | 2.02k | RETURN_IF_ERROR(_create_line_reader()); |
275 | | |
276 | 2.02k | _is_load = is_load; |
277 | 2.02k | if (!_is_load) { |
278 | | // For query task, there are 2 slot mapping. |
279 | | // One is from file slot to values in line. |
280 | | // eg, the file_slot_descs is k1, k3, k5, and values in line are k1, k2, k3, k4, k5 |
281 | | // the _col_idxs will save: 0, 2, 4 |
282 | | // The other is from file slot to columns in output block |
283 | | // eg, the file_slot_descs is k1, k3, k5, and columns in block are p1, k1, k3, k5 |
284 | | // where "p1" is the partition col which does not exist in file |
285 | | // the _file_slot_idx_map will save: 1, 2, 3 |
286 | 318 | DCHECK(_params.__isset.column_idxs); |
287 | 318 | _col_idxs = _params.column_idxs; |
288 | 318 | int idx = 0; |
289 | 2.23k | for (const auto& slot_info : _params.required_slots) { |
290 | 2.23k | if (slot_info.is_file_slot) { |
291 | 2.23k | _file_slot_idx_map.push_back(idx); |
292 | 2.23k | } |
293 | 2.23k | idx++; |
294 | 2.23k | } |
295 | 1.70k | } else { |
296 | | // For load task, the column order is same as file column order |
297 | 1.70k | int i = 0; |
298 | 13.4k | for (const auto& desc [[maybe_unused]] : _file_slot_descs) { |
299 | 13.4k | _col_idxs.push_back(i++); |
300 | 13.4k | } |
301 | 1.70k | } |
302 | | |
303 | 2.02k | _line_reader_eof = false; |
304 | 2.02k | return Status::OK(); |
305 | 2.02k | } |
306 | | |
307 | | // !FIXME: Here we should use MutableBlock |
308 | 6.71k | Status CsvReader::get_next_block(Block* block, size_t* read_rows, bool* eof) { |
309 | 6.71k | if (_line_reader_eof) { |
310 | 1.97k | *eof = true; |
311 | 1.97k | return Status::OK(); |
312 | 1.97k | } |
313 | | |
314 | 4.73k | const int batch_size = std::max(_state->batch_size(), (int)_MIN_BATCH_SIZE); |
315 | 4.73k | size_t rows = 0; |
316 | | |
317 | 4.73k | bool success = false; |
318 | 4.73k | bool is_remove_bom = false; |
319 | 4.73k | if (_push_down_agg_type == TPushAggOp::type::COUNT) { |
320 | 0 | while (rows < batch_size && !_line_reader_eof) { |
321 | 0 | const uint8_t* ptr = nullptr; |
322 | 0 | size_t size = 0; |
323 | 0 | RETURN_IF_ERROR(_line_reader->read_line(&ptr, &size, &_line_reader_eof, _io_ctx)); |
324 | | |
325 | | // _skip_lines == 0 means this line is the actual data beginning line for the entire file |
326 | | // is_remove_bom means _remove_bom should only execute once |
327 | 0 | if (_skip_lines == 0 && !is_remove_bom) { |
328 | 0 | ptr = _remove_bom(ptr, size); |
329 | 0 | is_remove_bom = true; |
330 | 0 | } |
331 | | |
332 | | // _skip_lines > 0 means we do not need to remove bom |
333 | 0 | if (_skip_lines > 0) { |
334 | 0 | _skip_lines--; |
335 | 0 | is_remove_bom = true; |
336 | 0 | continue; |
337 | 0 | } |
338 | 0 | if (size == 0) { |
339 | 0 | if (!_line_reader_eof && _state->is_read_csv_empty_line_as_null()) { |
340 | 0 | ++rows; |
341 | 0 | } |
342 | | // Read empty line, continue |
343 | 0 | continue; |
344 | 0 | } |
345 | | |
346 | 0 | RETURN_IF_ERROR(_validate_line(Slice(ptr, size), &success)); |
347 | 0 | ++rows; |
348 | 0 | } |
349 | 0 | auto mutate_columns = block->mutate_columns(); |
350 | 0 | for (auto& col : mutate_columns) { |
351 | 0 | col->resize(rows); |
352 | 0 | } |
353 | 0 | block->set_columns(std::move(mutate_columns)); |
354 | 4.73k | } else { |
355 | 4.73k | auto columns = block->mutate_columns(); |
356 | 12.1M | while (rows < batch_size && !_line_reader_eof) { |
357 | 12.1M | const uint8_t* ptr = nullptr; |
358 | 12.1M | size_t size = 0; |
359 | 12.1M | RETURN_IF_ERROR(_line_reader->read_line(&ptr, &size, &_line_reader_eof, _io_ctx)); |
360 | | |
361 | | // _skip_lines == 0 means this line is the actual data beginning line for the entire file |
362 | | // is_remove_bom means _remove_bom should only execute once |
363 | 12.1M | if (!is_remove_bom && _skip_lines == 0) { |
364 | 4.66k | ptr = _remove_bom(ptr, size); |
365 | 4.66k | is_remove_bom = true; |
366 | 4.66k | } |
367 | | |
368 | | // _skip_lines > 0 means we do not remove bom |
369 | 12.1M | if (_skip_lines > 0) { |
370 | 119 | _skip_lines--; |
371 | 119 | is_remove_bom = true; |
372 | 119 | continue; |
373 | 119 | } |
374 | 12.1M | if (size == 0) { |
375 | 2.03k | if (!_line_reader_eof && _state->is_read_csv_empty_line_as_null()) { |
376 | 0 | RETURN_IF_ERROR(_fill_empty_line(block, columns, &rows)); |
377 | 0 | } |
378 | | // Read empty line, continue |
379 | 2.03k | continue; |
380 | 2.03k | } |
381 | | |
382 | 12.1M | RETURN_IF_ERROR(_validate_line(Slice(ptr, size), &success)); |
383 | 12.1M | if (!success) { |
384 | 128 | continue; |
385 | 128 | } |
386 | 12.1M | RETURN_IF_ERROR(_fill_dest_columns(Slice(ptr, size), block, columns, &rows)); |
387 | 12.1M | } |
388 | 4.72k | block->set_columns(std::move(columns)); |
389 | 4.72k | } |
390 | | |
391 | 4.72k | *eof = (rows == 0); |
392 | 4.72k | *read_rows = rows; |
393 | | |
394 | 4.72k | return Status::OK(); |
395 | 4.73k | } |
396 | | |
397 | | Status CsvReader::get_columns(std::unordered_map<std::string, DataTypePtr>* name_to_type, |
398 | 2.02k | std::unordered_set<std::string>* missing_cols) { |
399 | 15.7k | for (const auto& slot : _file_slot_descs) { |
400 | 15.7k | name_to_type->emplace(slot->col_name(), slot->type()); |
401 | 15.7k | } |
402 | 2.02k | return Status::OK(); |
403 | 2.02k | } |
404 | | |
405 | | // init decompressor, file reader and line reader for parsing schema |
406 | 376 | Status CsvReader::init_schema_reader() { |
407 | 376 | _start_offset = _range.start_offset; |
408 | 376 | if (_start_offset != 0) { |
409 | 0 | return Status::InvalidArgument( |
410 | 0 | "start offset of TFileRangeDesc must be zero in get parsered schema"); |
411 | 0 | } |
412 | 376 | if (_params.file_type == TFileType::FILE_BROKER) { |
413 | 0 | return Status::InternalError<false>( |
414 | 0 | "Getting parsered schema from csv file do not support stream load and broker " |
415 | 0 | "load."); |
416 | 0 | } |
417 | | |
418 | | // csv file without names line and types line. |
419 | 376 | _read_line = 1; |
420 | 376 | _is_parse_name = false; |
421 | | |
422 | 376 | if (_params.__isset.file_attributes && _params.file_attributes.__isset.header_type && |
423 | 376 | !_params.file_attributes.header_type.empty()) { |
424 | 48 | std::string header_type = to_lower(_params.file_attributes.header_type); |
425 | 48 | if (header_type == BeConsts::CSV_WITH_NAMES) { |
426 | 25 | _is_parse_name = true; |
427 | 25 | } else if (header_type == BeConsts::CSV_WITH_NAMES_AND_TYPES) { |
428 | 23 | _read_line = 2; |
429 | 23 | _is_parse_name = true; |
430 | 23 | } |
431 | 48 | } |
432 | | |
433 | 376 | RETURN_IF_ERROR(_init_options()); |
434 | 376 | RETURN_IF_ERROR(_create_file_reader(true)); |
435 | 376 | RETURN_IF_ERROR(_create_decompressor()); |
436 | 376 | RETURN_IF_ERROR(_create_line_reader()); |
437 | 376 | return Status::OK(); |
438 | 376 | } |
439 | | |
440 | | Status CsvReader::get_parsed_schema(std::vector<std::string>* col_names, |
441 | 376 | std::vector<DataTypePtr>* col_types) { |
442 | 376 | if (_read_line == 1) { |
443 | 353 | if (!_is_parse_name) { //parse csv file without names and types |
444 | 328 | size_t col_nums = 0; |
445 | 328 | RETURN_IF_ERROR(_parse_col_nums(&col_nums)); |
446 | 2.81k | for (size_t i = 0; i < col_nums; ++i) { |
447 | 2.48k | col_names->emplace_back("c" + std::to_string(i + 1)); |
448 | 2.48k | } |
449 | 327 | } else { // parse csv file with names |
450 | 25 | RETURN_IF_ERROR(_parse_col_names(col_names)); |
451 | 25 | } |
452 | | |
453 | 2.93k | for (size_t j = 0; j < col_names->size(); ++j) { |
454 | 2.58k | col_types->emplace_back( |
455 | 2.58k | DataTypeFactory::instance().create_data_type(PrimitiveType::TYPE_STRING, true)); |
456 | 2.58k | } |
457 | 352 | } else { // parse csv file with names and types |
458 | 23 | RETURN_IF_ERROR(_parse_col_names(col_names)); |
459 | 23 | RETURN_IF_ERROR(_parse_col_types(col_names->size(), col_types)); |
460 | 23 | } |
461 | 375 | return Status::OK(); |
462 | 376 | } |
463 | | |
464 | 195M | Status CsvReader::_deserialize_nullable_string(IColumn& column, Slice& slice) { |
465 | 195M | auto& null_column = assert_cast<ColumnNullable&>(column); |
466 | 195M | if (_empty_field_as_null) { |
467 | 60 | if (slice.size == 0) { |
468 | 5 | null_column.insert_data(nullptr, 0); |
469 | 5 | return Status::OK(); |
470 | 5 | } |
471 | 60 | } |
472 | 196M | if (_options.null_len > 0 && !(_options.converted_from_string && slice.trim_double_quotes())) { |
473 | 196M | if (slice.compare(Slice(_options.null_format, _options.null_len)) == 0) { |
474 | 30.8k | null_column.insert_data(nullptr, 0); |
475 | 30.8k | return Status::OK(); |
476 | 30.8k | } |
477 | 196M | } |
478 | 195M | static DataTypeStringSerDe stringSerDe(TYPE_STRING); |
479 | 195M | auto st = stringSerDe.deserialize_one_cell_from_csv(null_column.get_nested_column(), slice, |
480 | 195M | _options); |
481 | 195M | if (!st.ok()) { |
482 | | // fill null if fail |
483 | 0 | null_column.insert_data(nullptr, 0); // 0 is meaningless here |
484 | 0 | return Status::OK(); |
485 | 0 | } |
486 | | // fill not null if success |
487 | 195M | null_column.get_null_map_data().push_back(0); |
488 | 195M | return Status::OK(); |
489 | 195M | } |
490 | | |
491 | 2.39k | Status CsvReader::_init_options() { |
492 | | // get column_separator and line_delimiter |
493 | 2.39k | _value_separator = _params.file_attributes.text_params.column_separator; |
494 | 2.39k | _value_separator_length = _value_separator.size(); |
495 | 2.39k | _line_delimiter = _params.file_attributes.text_params.line_delimiter; |
496 | 2.39k | _line_delimiter_length = _line_delimiter.size(); |
497 | 2.39k | if (_params.file_attributes.text_params.__isset.enclose) { |
498 | 2.39k | _enclose = _params.file_attributes.text_params.enclose; |
499 | 2.39k | } |
500 | 2.39k | if (_params.file_attributes.text_params.__isset.escape) { |
501 | 2.39k | _escape = _params.file_attributes.text_params.escape; |
502 | 2.39k | } |
503 | | |
504 | 2.39k | _trim_tailing_spaces = |
505 | 2.39k | (_state != nullptr && _state->trim_tailing_spaces_for_external_table_query()); |
506 | | |
507 | 2.39k | _options.escape_char = _escape; |
508 | 2.39k | _options.quote_char = _enclose; |
509 | | |
510 | 2.39k | if (_params.file_attributes.text_params.collection_delimiter.empty()) { |
511 | 2.39k | _options.collection_delim = ','; |
512 | 2.39k | } else { |
513 | 0 | _options.collection_delim = _params.file_attributes.text_params.collection_delimiter[0]; |
514 | 0 | } |
515 | 2.39k | if (_params.file_attributes.text_params.mapkv_delimiter.empty()) { |
516 | 2.39k | _options.map_key_delim = ':'; |
517 | 2.39k | } else { |
518 | 0 | _options.map_key_delim = _params.file_attributes.text_params.mapkv_delimiter[0]; |
519 | 0 | } |
520 | | |
521 | 2.39k | if (_params.file_attributes.text_params.__isset.null_format) { |
522 | 0 | _options.null_format = _params.file_attributes.text_params.null_format.data(); |
523 | 0 | _options.null_len = _params.file_attributes.text_params.null_format.length(); |
524 | 0 | } |
525 | | |
526 | 2.39k | if (_params.file_attributes.__isset.trim_double_quotes) { |
527 | 2.39k | _trim_double_quotes = _params.file_attributes.trim_double_quotes; |
528 | 2.39k | } |
529 | 2.39k | _options.converted_from_string = _trim_double_quotes; |
530 | | |
531 | 2.39k | if (_state != nullptr) { |
532 | 2.02k | _keep_cr = _state->query_options().keep_carriage_return; |
533 | 2.02k | } |
534 | | |
535 | 2.39k | if (_params.file_attributes.text_params.__isset.empty_field_as_null) { |
536 | 2.39k | _empty_field_as_null = _params.file_attributes.text_params.empty_field_as_null; |
537 | 2.39k | } |
538 | 2.39k | return Status::OK(); |
539 | 2.39k | } |
540 | | |
541 | 2.40k | Status CsvReader::_create_decompressor() { |
542 | 2.40k | if (_file_compress_type != TFileCompressType::UNKNOWN) { |
543 | 2.31k | RETURN_IF_ERROR(Decompressor::create_decompressor(_file_compress_type, &_decompressor)); |
544 | 2.31k | } else { |
545 | 86 | RETURN_IF_ERROR(Decompressor::create_decompressor(_file_format_type, &_decompressor)); |
546 | 86 | } |
547 | | |
548 | 2.40k | return Status::OK(); |
549 | 2.40k | } |
550 | | |
551 | 2.40k | Status CsvReader::_create_file_reader(bool need_schema) { |
552 | 2.40k | if (_params.file_type == TFileType::FILE_STREAM) { |
553 | 1.86k | RETURN_IF_ERROR(FileFactory::create_pipe_reader(_range.load_id, &_file_reader, _state, |
554 | 1.86k | need_schema)); |
555 | 1.86k | } else { |
556 | 541 | _file_description.mtime = _range.__isset.modification_time ? _range.modification_time : 0; |
557 | 541 | io::FileReaderOptions reader_options = |
558 | 541 | FileFactory::get_reader_options(_state, _file_description); |
559 | 541 | io::FileReaderSPtr file_reader; |
560 | 541 | if (_io_ctx_holder) { |
561 | 256 | file_reader = DORIS_TRY(io::DelegateReader::create_file_reader( |
562 | 256 | _profile, _system_properties, _file_description, reader_options, |
563 | 256 | io::DelegateReader::AccessMode::SEQUENTIAL, |
564 | 256 | std::static_pointer_cast<const io::IOContext>(_io_ctx_holder), |
565 | 256 | io::PrefetchRange(_range.start_offset, _range.start_offset + _range.size))); |
566 | 285 | } else { |
567 | 285 | file_reader = DORIS_TRY(io::DelegateReader::create_file_reader( |
568 | 285 | _profile, _system_properties, _file_description, reader_options, |
569 | 285 | io::DelegateReader::AccessMode::SEQUENTIAL, _io_ctx, |
570 | 285 | io::PrefetchRange(_range.start_offset, _range.start_offset + _range.size))); |
571 | 285 | } |
572 | 541 | _file_reader = _io_ctx ? std::make_shared<io::TracingFileReader>(std::move(file_reader), |
573 | 541 | _io_ctx->file_reader_stats) |
574 | 541 | : file_reader; |
575 | 541 | } |
576 | 2.40k | if (_file_reader->size() == 0 && _params.file_type != TFileType::FILE_STREAM && |
577 | 2.40k | _params.file_type != TFileType::FILE_BROKER) { |
578 | 0 | return Status::EndOfFile("init reader failed, empty csv file: " + _range.path); |
579 | 0 | } |
580 | 2.40k | return Status::OK(); |
581 | 2.40k | } |
582 | | |
583 | 2.39k | Status CsvReader::_create_line_reader() { |
584 | 2.39k | std::shared_ptr<TextLineReaderContextIf> text_line_reader_ctx; |
585 | 2.39k | if (_enclose == 0) { |
586 | 2.29k | text_line_reader_ctx = std::make_shared<PlainTextLineReaderCtx>( |
587 | 2.29k | _line_delimiter, _line_delimiter_length, _keep_cr); |
588 | 2.29k | _fields_splitter = std::make_unique<PlainCsvTextFieldSplitter>( |
589 | 2.29k | _trim_tailing_spaces, false, _value_separator, _value_separator_length, -1); |
590 | | |
591 | 2.29k | } else { |
592 | | // in load task, the _file_slot_descs is empty vector, so we need to set col_sep_num to 0 |
593 | 98 | size_t col_sep_num = _file_slot_descs.size() > 1 ? _file_slot_descs.size() - 1 : 0; |
594 | 98 | _enclose_reader_ctx = std::make_shared<EncloseCsvLineReaderCtx>( |
595 | 98 | _line_delimiter, _line_delimiter_length, _value_separator, _value_separator_length, |
596 | 98 | col_sep_num, _enclose, _escape, _keep_cr); |
597 | 98 | text_line_reader_ctx = _enclose_reader_ctx; |
598 | | |
599 | 98 | _fields_splitter = std::make_unique<EncloseCsvTextFieldSplitter>( |
600 | 98 | _trim_tailing_spaces, true, _enclose_reader_ctx, _value_separator_length, _enclose); |
601 | 98 | } |
602 | 2.39k | switch (_file_format_type) { |
603 | 2.32k | case TFileFormatType::FORMAT_CSV_PLAIN: |
604 | 2.32k | [[fallthrough]]; |
605 | 2.32k | case TFileFormatType::FORMAT_CSV_GZ: |
606 | 2.32k | [[fallthrough]]; |
607 | 2.32k | case TFileFormatType::FORMAT_CSV_BZ2: |
608 | 2.32k | [[fallthrough]]; |
609 | 2.32k | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
610 | 2.32k | [[fallthrough]]; |
611 | 2.32k | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
612 | 2.32k | [[fallthrough]]; |
613 | 2.32k | case TFileFormatType::FORMAT_CSV_LZOP: |
614 | 2.32k | [[fallthrough]]; |
615 | 2.32k | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
616 | 2.32k | [[fallthrough]]; |
617 | 2.32k | case TFileFormatType::FORMAT_CSV_DEFLATE: |
618 | 2.32k | _line_reader = |
619 | 2.32k | NewPlainTextLineReader::create_unique(_profile, _file_reader, _decompressor.get(), |
620 | 2.32k | text_line_reader_ctx, _size, _start_offset); |
621 | | |
622 | 2.32k | break; |
623 | 77 | case TFileFormatType::FORMAT_PROTO: |
624 | 77 | _fields_splitter = std::make_unique<CsvProtoFieldSplitter>(); |
625 | 77 | _line_reader = NewPlainBinaryLineReader::create_unique(_file_reader); |
626 | 77 | break; |
627 | 0 | default: |
628 | 0 | return Status::InternalError<false>( |
629 | 0 | "Unknown format type, cannot init line reader in csv reader, type={}", |
630 | 0 | _file_format_type); |
631 | 2.39k | } |
632 | 2.39k | return Status::OK(); |
633 | 2.39k | } |
634 | | |
635 | 300 | Status CsvReader::_deserialize_one_cell(DataTypeSerDeSPtr serde, IColumn& column, Slice& slice) { |
636 | 300 | return serde->deserialize_one_cell_from_csv(column, slice, _options); |
637 | 300 | } |
638 | | |
639 | | Status CsvReader::_fill_dest_columns(const Slice& line, Block* block, |
640 | 12.2M | std::vector<MutableColumnPtr>& columns, size_t* rows) { |
641 | 12.2M | bool is_success = false; |
642 | | |
643 | 12.2M | RETURN_IF_ERROR(_line_split_to_values(line, &is_success)); |
644 | 12.2M | if (UNLIKELY(!is_success)) { |
645 | | // If not success, which means we met an invalid row, filter this row and return. |
646 | 539 | return Status::OK(); |
647 | 539 | } |
648 | | |
649 | 203M | for (int i = 0; i < _file_slot_descs.size(); ++i) { |
650 | 191M | int col_idx = _col_idxs[i]; |
651 | | // col idx is out of range, fill with null format |
652 | 191M | auto value = col_idx < _split_values.size() |
653 | 192M | ? _split_values[col_idx] |
654 | 18.4E | : Slice(_options.null_format, _options.null_len); |
655 | | |
656 | 191M | IColumn* col_ptr = columns[i].get(); |
657 | 191M | if (!_is_load) { |
658 | | // block is a Block*, and get_by_position returns a ColumnPtr, |
659 | | // which is a const pointer. Therefore, using const_cast is permissible. |
660 | 20.5M | col_ptr = const_cast<IColumn*>( |
661 | 20.5M | block->get_by_position(_file_slot_idx_map[i]).column.get()); |
662 | 20.5M | } |
663 | | |
664 | 199M | if (_use_nullable_string_opt[i]) { |
665 | | // For load task, we always read "string" from file. |
666 | | // So serdes[i] here must be DataTypeNullableSerDe, and DataTypeNullableSerDe -> nested_serde must be DataTypeStringSerDe. |
667 | | // So we use deserialize_nullable_string and stringSerDe to reduce virtual function calls. |
668 | 199M | RETURN_IF_ERROR(_deserialize_nullable_string(*col_ptr, value)); |
669 | 18.4E | } else { |
670 | 18.4E | RETURN_IF_ERROR(_deserialize_one_cell(_serdes[i], *col_ptr, value)); |
671 | 18.4E | } |
672 | 191M | } |
673 | 12.2M | ++(*rows); |
674 | | |
675 | 12.2M | return Status::OK(); |
676 | 12.2M | } |
677 | | |
678 | | Status CsvReader::_fill_empty_line(Block* block, std::vector<MutableColumnPtr>& columns, |
679 | 0 | size_t* rows) { |
680 | 0 | for (int i = 0; i < _file_slot_descs.size(); ++i) { |
681 | 0 | IColumn* col_ptr = columns[i].get(); |
682 | 0 | if (!_is_load) { |
683 | | // block is a Block*, and get_by_position returns a ColumnPtr, |
684 | | // which is a const pointer. Therefore, using const_cast is permissible. |
685 | 0 | col_ptr = const_cast<IColumn*>( |
686 | 0 | block->get_by_position(_file_slot_idx_map[i]).column.get()); |
687 | 0 | } |
688 | 0 | auto& null_column = assert_cast<ColumnNullable&>(*col_ptr); |
689 | 0 | null_column.insert_data(nullptr, 0); |
690 | 0 | } |
691 | 0 | ++(*rows); |
692 | 0 | return Status::OK(); |
693 | 0 | } |
694 | | |
695 | 12.2M | Status CsvReader::_validate_line(const Slice& line, bool* success) { |
696 | 12.2M | if (!_is_proto_format && !validate_utf8(_params, line.data, line.size)) { |
697 | 128 | if (!_is_load) { |
698 | 0 | return Status::InternalError<false>("Only support csv data in utf8 codec"); |
699 | 128 | } else { |
700 | 128 | _counter->num_rows_filtered++; |
701 | 128 | *success = false; |
702 | 128 | RETURN_IF_ERROR(_state->append_error_msg_to_file( |
703 | 128 | [&]() -> std::string { return std::string(line.data, line.size); }, |
704 | 128 | [&]() -> std::string { |
705 | 128 | return "Invalid file encoding: all CSV files must be UTF-8 encoded"; |
706 | 128 | })); |
707 | 128 | return Status::OK(); |
708 | 128 | } |
709 | 128 | } |
710 | 12.2M | *success = true; |
711 | 12.2M | return Status::OK(); |
712 | 12.2M | } |
713 | | |
714 | 12.2M | Status CsvReader::_line_split_to_values(const Slice& line, bool* success) { |
715 | 12.2M | _split_line(line); |
716 | | |
717 | 12.2M | if (_is_load) { |
718 | | // Only check for load task. For query task, the non exist column will be filled "null". |
719 | | // if actual column number in csv file is not equal to _file_slot_descs.size() |
720 | | // then filter this line. |
721 | 10.9M | bool ignore_col = false; |
722 | 10.9M | ignore_col = _params.__isset.file_attributes && |
723 | 10.9M | _params.file_attributes.__isset.ignore_csv_redundant_col && |
724 | 10.9M | _params.file_attributes.ignore_csv_redundant_col; |
725 | | |
726 | 10.9M | if ((!ignore_col && _split_values.size() != _file_slot_descs.size()) || |
727 | 10.9M | (ignore_col && _split_values.size() < _file_slot_descs.size())) { |
728 | 544 | _counter->num_rows_filtered++; |
729 | 544 | *success = false; |
730 | 544 | RETURN_IF_ERROR(_state->append_error_msg_to_file( |
731 | 544 | [&]() -> std::string { return std::string(line.data, line.size); }, |
732 | 544 | [&]() -> std::string { |
733 | 544 | fmt::memory_buffer error_msg; |
734 | 544 | fmt::format_to(error_msg, |
735 | 544 | "Column count mismatch: expected {}, but found {}", |
736 | 544 | _file_slot_descs.size(), _split_values.size()); |
737 | 544 | std::string escaped_separator = |
738 | 544 | std::regex_replace(_value_separator, std::regex("\t"), "\\t"); |
739 | 544 | std::string escaped_delimiter = |
740 | 544 | std::regex_replace(_line_delimiter, std::regex("\n"), "\\n"); |
741 | 544 | fmt::format_to(error_msg, " (sep:{} delim:{}", escaped_separator, |
742 | 544 | escaped_delimiter); |
743 | 544 | if (_enclose != 0) { |
744 | 544 | fmt::format_to(error_msg, " encl:{}", _enclose); |
745 | 544 | } |
746 | 544 | if (_escape != 0) { |
747 | 544 | fmt::format_to(error_msg, " esc:{}", _escape); |
748 | 544 | } |
749 | 544 | fmt::format_to(error_msg, ")"); |
750 | 544 | return fmt::to_string(error_msg); |
751 | 544 | })); |
752 | 539 | return Status::OK(); |
753 | 544 | } |
754 | 10.9M | } |
755 | | |
756 | 12.2M | *success = true; |
757 | 12.2M | return Status::OK(); |
758 | 12.2M | } |
759 | | |
760 | 12.2M | void CsvReader::_split_line(const Slice& line) { |
761 | 12.2M | _split_values.clear(); |
762 | 12.2M | _fields_splitter->split_line(line, &_split_values); |
763 | 12.2M | } |
764 | | |
765 | 328 | Status CsvReader::_parse_col_nums(size_t* col_nums) { |
766 | 328 | const uint8_t* ptr = nullptr; |
767 | 328 | size_t size = 0; |
768 | 328 | RETURN_IF_ERROR(_line_reader->read_line(&ptr, &size, &_line_reader_eof, _io_ctx)); |
769 | 328 | if (size == 0) { |
770 | 0 | return Status::InternalError<false>( |
771 | 0 | "The first line is empty, can not parse column numbers"); |
772 | 0 | } |
773 | 328 | if (!validate_utf8(_params, reinterpret_cast<const char*>(ptr), size)) { |
774 | 1 | return Status::InternalError<false>("Only support csv data in utf8 codec"); |
775 | 1 | } |
776 | 327 | ptr = _remove_bom(ptr, size); |
777 | 327 | _split_line(Slice(ptr, size)); |
778 | 327 | *col_nums = _split_values.size(); |
779 | 327 | return Status::OK(); |
780 | 328 | } |
781 | | |
782 | 48 | Status CsvReader::_parse_col_names(std::vector<std::string>* col_names) { |
783 | 48 | const uint8_t* ptr = nullptr; |
784 | 48 | size_t size = 0; |
785 | | // no use of _line_reader_eof |
786 | 48 | RETURN_IF_ERROR(_line_reader->read_line(&ptr, &size, &_line_reader_eof, _io_ctx)); |
787 | 48 | if (size == 0) { |
788 | 0 | return Status::InternalError<false>("The first line is empty, can not parse column names"); |
789 | 0 | } |
790 | 48 | if (!validate_utf8(_params, reinterpret_cast<const char*>(ptr), size)) { |
791 | 0 | return Status::InternalError<false>("Only support csv data in utf8 codec"); |
792 | 0 | } |
793 | 48 | ptr = _remove_bom(ptr, size); |
794 | 48 | _split_line(Slice(ptr, size)); |
795 | 187 | for (auto _split_value : _split_values) { |
796 | 187 | col_names->emplace_back(_split_value.to_string()); |
797 | 187 | } |
798 | 48 | return Status::OK(); |
799 | 48 | } |
800 | | |
801 | | // TODO(ftw): parse type |
802 | 23 | Status CsvReader::_parse_col_types(size_t col_nums, std::vector<DataTypePtr>* col_types) { |
803 | | // delete after. |
804 | 113 | for (size_t i = 0; i < col_nums; ++i) { |
805 | 90 | col_types->emplace_back(make_nullable(std::make_shared<DataTypeString>())); |
806 | 90 | } |
807 | | |
808 | | // 1. check _line_reader_eof |
809 | | // 2. read line |
810 | | // 3. check utf8 |
811 | | // 4. check size |
812 | | // 5. check _split_values.size must equal to col_nums. |
813 | | // 6. fill col_types |
814 | 23 | return Status::OK(); |
815 | 23 | } |
816 | | |
817 | 5.03k | const uint8_t* CsvReader::_remove_bom(const uint8_t* ptr, size_t& size) { |
818 | 5.03k | if (size >= 3 && ptr[0] == 0xEF && ptr[1] == 0xBB && ptr[2] == 0xBF) { |
819 | 7 | LOG(INFO) << "remove bom"; |
820 | 7 | constexpr size_t bom_size = 3; |
821 | 7 | size -= bom_size; |
822 | | // In enclose mode, column_sep_positions were computed on the original line |
823 | | // (including BOM). After shifting the pointer, we must adjust those positions |
824 | | // so they remain correct relative to the new start. |
825 | 7 | if (_enclose_reader_ctx) { |
826 | 1 | _enclose_reader_ctx->adjust_column_sep_positions(bom_size); |
827 | 1 | } |
828 | 7 | return ptr + bom_size; |
829 | 7 | } |
830 | 5.03k | return ptr; |
831 | 5.03k | } |
832 | | |
833 | 2.02k | Status CsvReader::close() { |
834 | 2.02k | if (_line_reader) { |
835 | 2.02k | _line_reader->close(); |
836 | 2.02k | } |
837 | | |
838 | 2.02k | if (_file_reader) { |
839 | 2.02k | RETURN_IF_ERROR(_file_reader->close()); |
840 | 2.02k | } |
841 | | |
842 | 2.02k | return Status::OK(); |
843 | 2.02k | } |
844 | | |
845 | | #include "common/compile_check_end.h" |
846 | | } // namespace doris |