Coverage Report

Created: 2026-06-17 10:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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 <numeric>
30
#include <ostream>
31
#include <regex>
32
#include <utility>
33
34
#include "common/compiler_util.h" // IWYU pragma: keep
35
#include "common/config.h"
36
#include "common/consts.h"
37
#include "common/status.h"
38
#include "core/block/block.h"
39
#include "core/block/column_with_type_and_name.h"
40
#include "core/column/column_nullable.h"
41
#include "core/column/column_string.h"
42
#include "core/data_type/data_type_factory.hpp"
43
#include "core/data_type_serde/data_type_string_serde.h"
44
#include "exec/scan/scanner.h"
45
#include "format/file_reader/new_plain_binary_line_reader.h"
46
#include "format/file_reader/new_plain_text_line_reader.h"
47
#include "format/line_reader.h"
48
#include "io/file_factory.h"
49
#include "io/fs/broker_file_reader.h"
50
#include "io/fs/buffered_reader.h"
51
#include "io/fs/file_reader.h"
52
#include "io/fs/s3_file_reader.h"
53
#include "io/fs/tracing_file_reader.h"
54
#include "runtime/descriptors.h"
55
#include "runtime/runtime_state.h"
56
#include "util/decompressor.h"
57
#include "util/string_util.h"
58
#include "util/utf8_check.h"
59
60
namespace doris {
61
class RuntimeProfile;
62
class IColumn;
63
namespace io {
64
struct IOContext;
65
enum class FileCachePolicy : uint8_t;
66
} // namespace io
67
} // namespace doris
68
69
namespace doris {
70
71
namespace {
72
73
12.0M
size_t columns_byte_size(const std::vector<MutableColumnPtr>& columns) {
74
12.0M
    size_t bytes = 0;
75
187M
    for (const auto& column : columns) {
76
187M
        DCHECK(column.get() != nullptr);
77
187M
        bytes += column->byte_size();
78
187M
    }
79
12.0M
    return bytes;
80
12.0M
}
81
82
} // namespace
83
84
712
void EncloseCsvTextFieldSplitter::do_split(const Slice& line, std::vector<Slice>* splitted_values) {
85
712
    const char* data = line.data;
86
712
    const auto& column_sep_positions = _text_line_reader_ctx->column_sep_positions();
87
712
    size_t value_start_offset = 0;
88
2.59k
    for (auto idx : column_sep_positions) {
89
2.59k
        process_value_func(data, value_start_offset, idx - value_start_offset, _trimming_char,
90
2.59k
                           splitted_values);
91
2.59k
        value_start_offset = idx + _value_sep_len;
92
2.59k
    }
93
712
    if (line.size >= value_start_offset) {
94
        // process the last column
95
710
        process_value_func(data, value_start_offset, line.size - value_start_offset, _trimming_char,
96
710
                           splitted_values);
97
710
    }
98
712
}
99
100
void PlainCsvTextFieldSplitter::_split_field_single_char(const Slice& line,
101
12.0M
                                                         std::vector<Slice>* splitted_values) {
102
12.0M
    const char* data = line.data;
103
12.0M
    const size_t size = line.size;
104
12.0M
    size_t value_start = 0;
105
1.55G
    for (size_t i = 0; i < size; ++i) {
106
1.53G
        if (data[i] == _value_sep[0]) {
107
187M
            process_value_func(data, value_start, i - value_start, _trimming_char, splitted_values);
108
187M
            value_start = i + _value_sep_len;
109
187M
        }
110
1.53G
    }
111
12.0M
    process_value_func(data, value_start, size - value_start, _trimming_char, splitted_values);
112
12.0M
}
113
114
void PlainCsvTextFieldSplitter::_split_field_multi_char(const Slice& line,
115
162
                                                        std::vector<Slice>* splitted_values) {
116
162
    size_t start = 0;  // point to the start pos of next col value.
117
162
    size_t curpos = 0; // point to the start pos of separator matching sequence.
118
119
    // value_sep : AAAA
120
    // line.data : 1234AAAA5678
121
    // -> 1234,5678
122
123
    //    start   start
124
    //      ▼       ▼
125
    //      1234AAAA5678\0
126
    //          ▲       ▲
127
    //      curpos     curpos
128
129
    //kmp
130
162
    std::vector<int> next(_value_sep_len);
131
162
    next[0] = -1;
132
403
    for (int i = 1, j = -1; i < _value_sep_len; i++) {
133
261
        while (j > -1 && _value_sep[i] != _value_sep[j + 1]) {
134
20
            j = next[j];
135
20
        }
136
241
        if (_value_sep[i] == _value_sep[j + 1]) {
137
130
            j++;
138
130
        }
139
241
        next[i] = j;
140
241
    }
141
142
6.98k
    for (int i = 0, j = -1; i < line.size; i++) {
143
        // i : line
144
        // j : _value_sep
145
7.10k
        while (j > -1 && line[i] != _value_sep[j + 1]) {
146
290
            j = next[j];
147
290
        }
148
6.81k
        if (line[i] == _value_sep[j + 1]) {
149
2.17k
            j++;
150
2.17k
        }
151
6.81k
        if (j == _value_sep_len - 1) {
152
832
            curpos = i - _value_sep_len + 1;
153
154
            /*
155
             * column_separator : "xx"
156
             * data.csv :  data1xxxxdata2
157
             *
158
             * Parse incorrectly:
159
             *      data1[xx]xxdata2
160
             *      data1x[xx]xdata2
161
             *      data1xx[xx]data2
162
             * The string "xxxx" is parsed into three "xx" delimiters.
163
             *
164
             * Parse correctly:
165
             *      data1[xx]xxdata2
166
             *      data1xx[xx]data2
167
             */
168
169
832
            if (curpos >= start) {
170
803
                process_value_func(line.data, start, curpos - start, _trimming_char,
171
803
                                   splitted_values);
172
803
                start = i + 1;
173
803
            }
174
175
832
            j = next[j];
176
832
        }
177
6.81k
    }
178
162
    process_value_func(line.data, start, line.size - start, _trimming_char, splitted_values);
179
162
}
180
181
12.0M
void PlainCsvTextFieldSplitter::do_split(const Slice& line, std::vector<Slice>* splitted_values) {
182
12.1M
    if (is_single_char_delim) {
183
12.1M
        _split_field_single_char(line, splitted_values);
184
18.4E
    } else {
185
18.4E
        _split_field_multi_char(line, splitted_values);
186
18.4E
    }
187
12.0M
}
188
189
CsvReader::CsvReader(RuntimeState* state, RuntimeProfile* profile, ScannerCounter* counter,
190
                     const TFileScanRangeParams& params, const TFileRangeDesc& range,
191
                     const std::vector<SlotDescriptor*>& file_slot_descs, size_t batch_size,
192
                     io::IOContext* io_ctx, std::shared_ptr<io::IOContext> io_ctx_holder)
193
2.41k
        : _profile(profile),
194
2.41k
          _params(params),
195
2.41k
          _file_reader(nullptr),
196
2.41k
          _line_reader(nullptr),
197
2.41k
          _decompressor(nullptr),
198
2.41k
          _state(state),
199
2.41k
          _counter(counter),
200
2.41k
          _range(range),
201
2.41k
          _file_slot_descs(file_slot_descs),
202
2.41k
          _line_reader_eof(false),
203
2.41k
          _skip_lines(0),
204
2.41k
          _io_ctx(io_ctx),
205
2.41k
          _io_ctx_holder(std::move(io_ctx_holder)),
206
2.41k
          _batch_size(std::max(batch_size, 1UL)) {
207
2.41k
    if (_io_ctx == nullptr && _io_ctx_holder) {
208
2.01k
        _io_ctx = _io_ctx_holder.get();
209
2.01k
    }
210
2.41k
    _file_format_type = _params.format_type;
211
2.41k
    _is_proto_format = _file_format_type == TFileFormatType::FORMAT_PROTO;
212
2.41k
    if (_range.__isset.compress_type) {
213
        // for compatibility
214
622
        _file_compress_type = _range.compress_type;
215
1.79k
    } else {
216
1.79k
        _file_compress_type = _params.compress_type;
217
1.79k
    }
218
2.41k
    _size = _range.size;
219
220
2.41k
    _split_values.reserve(_file_slot_descs.size());
221
2.41k
    _init_system_properties();
222
2.41k
    _init_file_description();
223
2.41k
    _serdes = create_data_type_serdes(_file_slot_descs);
224
2.41k
}
225
226
2.41k
void CsvReader::_init_system_properties() {
227
2.41k
    if (_range.__isset.file_type) {
228
        // for compatibility
229
601
        _system_properties.system_type = _range.file_type;
230
1.81k
    } else {
231
1.81k
        _system_properties.system_type = _params.file_type;
232
1.81k
    }
233
2.41k
    _system_properties.properties = _params.properties;
234
2.41k
    _system_properties.hdfs_params = _params.hdfs_params;
235
2.41k
    if (_params.__isset.broker_addresses) {
236
1.68k
        _system_properties.broker_addresses.assign(_params.broker_addresses.begin(),
237
1.68k
                                                   _params.broker_addresses.end());
238
1.68k
    }
239
2.41k
}
240
241
2.41k
void CsvReader::_init_file_description() {
242
2.41k
    _file_description.path = _range.path;
243
2.41k
    _file_description.file_size = _range.__isset.file_size ? _range.file_size : -1;
244
2.41k
    if (_range.__isset.fs_name) {
245
0
        _file_description.fs_name = _range.fs_name;
246
0
    }
247
2.41k
    if (_range.__isset.file_cache_admission) {
248
224
        _file_description.file_cache_admission = _range.file_cache_admission;
249
224
    }
250
2.41k
}
251
252
0
Status CsvReader::init_reader(bool is_load) {
253
    // set the skip lines and start offset
254
0
    _start_offset = _range.start_offset;
255
0
    if (_start_offset == 0) {
256
        // check header typer first
257
0
        if (_params.__isset.file_attributes && _params.file_attributes.__isset.header_type &&
258
0
            !_params.file_attributes.header_type.empty()) {
259
0
            std::string header_type = to_lower(_params.file_attributes.header_type);
260
0
            if (header_type == BeConsts::CSV_WITH_NAMES) {
261
0
                _skip_lines = 1;
262
0
            } else if (header_type == BeConsts::CSV_WITH_NAMES_AND_TYPES) {
263
0
                _skip_lines = 2;
264
0
            }
265
0
        } else if (_params.file_attributes.__isset.skip_lines) {
266
0
            _skip_lines = _params.file_attributes.skip_lines;
267
0
        }
268
0
    } else if (_start_offset != 0) {
269
0
        if ((_file_compress_type != TFileCompressType::PLAIN) ||
270
0
            (_file_compress_type == TFileCompressType::UNKNOWN &&
271
0
             _file_format_type != TFileFormatType::FORMAT_CSV_PLAIN)) {
272
0
            return Status::InternalError<false>("For now we do not support split compressed file");
273
0
        }
274
        // pre-read to promise first line skipped always read
275
0
        int64_t pre_read_len = std::min(
276
0
                static_cast<int64_t>(_params.file_attributes.text_params.line_delimiter.size()),
277
0
                _start_offset);
278
0
        _start_offset -= pre_read_len;
279
0
        _size += pre_read_len;
280
        // not first range will always skip one line
281
0
        _skip_lines = 1;
282
0
    }
283
284
0
    _use_nullable_string_opt.resize(_file_slot_descs.size());
285
0
    for (int i = 0; i < _file_slot_descs.size(); ++i) {
286
0
        auto data_type_ptr = _file_slot_descs[i]->get_data_type_ptr();
287
0
        if (data_type_ptr->is_nullable() && is_string_type(data_type_ptr->get_primitive_type())) {
288
0
            _use_nullable_string_opt[i] = 1;
289
0
        }
290
0
    }
291
292
0
    RETURN_IF_ERROR(_init_options());
293
0
    RETURN_IF_ERROR(_create_file_reader(false));
294
0
    RETURN_IF_ERROR(_create_decompressor());
295
0
    RETURN_IF_ERROR(_create_line_reader());
296
297
0
    _is_load = is_load;
298
0
    if (!_is_load) {
299
        // For query task, there are 2 slot mapping.
300
        // One is from file slot to values in line.
301
        //      eg, the file_slot_descs is k1, k3, k5, and values in line are k1, k2, k3, k4, k5
302
        //      the _col_idxs will save: 0, 2, 4
303
        // The other is from file slot to columns in output block
304
        //      eg, the file_slot_descs is k1, k3, k5, and columns in block are p1, k1, k3, k5
305
        //      where "p1" is the partition col which does not exist in file
306
        //      the _file_slot_idx_map will save: 1, 2, 3
307
0
        DCHECK(_params.__isset.column_idxs);
308
0
        _col_idxs = _params.column_idxs;
309
0
        int idx = 0;
310
0
        for (const auto& slot_info : _params.required_slots) {
311
0
            if (slot_info.is_file_slot) {
312
0
                _file_slot_idx_map.push_back(idx);
313
0
            }
314
0
            idx++;
315
0
        }
316
0
    } else {
317
        // For load task, the column order is same as file column order
318
0
        int i = 0;
319
0
        for (const auto& desc [[maybe_unused]] : _file_slot_descs) {
320
0
            _col_idxs.push_back(i++);
321
0
        }
322
0
    }
323
324
0
    _line_reader_eof = false;
325
0
    return Status::OK();
326
0
}
327
328
// ---- Unified init_reader(ReaderInitContext*) overrides ----
329
330
2.01k
Status CsvReader::_open_file_reader(ReaderInitContext* base_ctx) {
331
2.01k
    _start_offset = _range.start_offset;
332
2.01k
    if (_start_offset == 0) {
333
2.00k
        if (_params.__isset.file_attributes && _params.file_attributes.__isset.header_type &&
334
2.00k
            !_params.file_attributes.header_type.empty()) {
335
44
            std::string header_type = to_lower(_params.file_attributes.header_type);
336
44
            if (header_type == BeConsts::CSV_WITH_NAMES) {
337
24
                _skip_lines = 1;
338
24
            } else if (header_type == BeConsts::CSV_WITH_NAMES_AND_TYPES) {
339
20
                _skip_lines = 2;
340
20
            }
341
1.96k
        } else if (_params.file_attributes.__isset.skip_lines) {
342
1.96k
            _skip_lines = _params.file_attributes.skip_lines;
343
1.96k
        }
344
2.00k
    } else if (_start_offset != 0) {
345
4
        if ((_file_compress_type != TFileCompressType::PLAIN) ||
346
4
            (_file_compress_type == TFileCompressType::UNKNOWN &&
347
4
             _file_format_type != TFileFormatType::FORMAT_CSV_PLAIN)) {
348
0
            return Status::InternalError<false>("For now we do not support split compressed file");
349
0
        }
350
4
        int64_t pre_read_len = std::min(
351
4
                static_cast<int64_t>(_params.file_attributes.text_params.line_delimiter.size()),
352
4
                _start_offset);
353
4
        _start_offset -= pre_read_len;
354
4
        _size += pre_read_len;
355
4
        _skip_lines = 1;
356
4
    }
357
358
2.01k
    RETURN_IF_ERROR(_init_options());
359
2.01k
    RETURN_IF_ERROR(_create_file_reader(false));
360
2.01k
    return Status::OK();
361
2.01k
}
362
363
2.01k
Status CsvReader::_do_init_reader(ReaderInitContext* base_ctx) {
364
2.01k
    auto* ctx = checked_context_cast<CsvInitContext>(base_ctx);
365
2.01k
    _is_load = ctx->is_load;
366
367
2.01k
    _use_nullable_string_opt.resize(_file_slot_descs.size());
368
17.4k
    for (int i = 0; i < _file_slot_descs.size(); ++i) {
369
15.4k
        auto data_type_ptr = _file_slot_descs[i]->get_data_type_ptr();
370
15.4k
        if (data_type_ptr->is_nullable() && is_string_type(data_type_ptr->get_primitive_type())) {
371
15.4k
            _use_nullable_string_opt[i] = 1;
372
15.4k
        }
373
15.4k
    }
374
375
2.01k
    RETURN_IF_ERROR(_create_decompressor());
376
2.01k
    RETURN_IF_ERROR(_create_line_reader());
377
378
2.01k
    if (!_is_load) {
379
332
        DCHECK(_params.__isset.column_idxs);
380
332
        _col_idxs = _params.column_idxs;
381
332
        int idx = 0;
382
2.30k
        for (const auto& slot_info : _params.required_slots) {
383
2.30k
            if (slot_info.is_file_slot) {
384
2.30k
                _file_slot_idx_map.push_back(idx);
385
2.30k
            }
386
2.30k
            idx++;
387
2.30k
        }
388
1.68k
    } else {
389
1.68k
        int i = 0;
390
13.1k
        for (const auto& desc [[maybe_unused]] : _file_slot_descs) {
391
13.1k
            _col_idxs.push_back(i++);
392
13.1k
        }
393
1.68k
    }
394
2.01k
    _line_reader_eof = false;
395
2.01k
    return Status::OK();
396
2.01k
}
397
398
7.19k
void CsvReader::set_batch_size(size_t batch_size) {
399
    // 0 means "not set" / "use default" for the row-based readers; we must
400
    // never let _batch_size be 0 because _do_get_next_block uses it as the
401
    // upper bound of a `while (rows < _batch_size)` loop and a 0 would make
402
    // the reader return empty blocks and incorrectly signal EOF.
403
7.19k
    _batch_size = std::max(batch_size, 1UL);
404
7.19k
}
405
406
// !FIXME: Here we should use MutableBlock
407
7.19k
Status CsvReader::_do_get_next_block(Block* block, size_t* read_rows, bool* eof) {
408
7.19k
    if (_line_reader_eof) {
409
1.96k
        *eof = true;
410
1.96k
        return Status::OK();
411
1.96k
    }
412
413
5.23k
    const size_t batch_size = _batch_size;
414
5.23k
    const auto max_block_bytes = _state->preferred_block_size_bytes();
415
5.23k
    size_t rows = 0;
416
417
5.23k
    bool success = false;
418
5.23k
    bool is_remove_bom = false;
419
5.23k
    if (_push_down_agg_type == TPushAggOp::type::COUNT) {
420
0
        while (rows < batch_size && !_line_reader_eof) {
421
0
            const uint8_t* ptr = nullptr;
422
0
            size_t size = 0;
423
0
            RETURN_IF_ERROR(_line_reader->read_line(&ptr, &size, &_line_reader_eof, _io_ctx));
424
425
            // _skip_lines == 0 means this line is the actual data beginning line for the entire file
426
            // is_remove_bom means _remove_bom should only execute once
427
0
            if (_skip_lines == 0 && !is_remove_bom) {
428
0
                ptr = _remove_bom(ptr, size);
429
0
                is_remove_bom = true;
430
0
            }
431
432
            // _skip_lines > 0 means we do not need to remove bom
433
0
            if (_skip_lines > 0) {
434
0
                _skip_lines--;
435
0
                is_remove_bom = true;
436
0
                continue;
437
0
            }
438
0
            if (size == 0) {
439
0
                if (!_line_reader_eof && _state->is_read_csv_empty_line_as_null()) {
440
0
                    ++rows;
441
0
                }
442
                // Read empty line, continue
443
0
                continue;
444
0
            }
445
446
0
            RETURN_IF_ERROR(_validate_line(Slice(ptr, size), &success));
447
0
            ++rows;
448
0
        }
449
0
        auto mutable_columns_guard = block->mutate_columns_scoped();
450
0
        auto& mutate_columns = mutable_columns_guard.mutable_columns();
451
0
        for (auto& col : mutate_columns) {
452
0
            col->resize(rows);
453
0
        }
454
5.23k
    } else {
455
5.23k
        auto columns_guard = block->mutate_columns_scoped();
456
5.23k
        auto& columns = columns_guard.mutable_columns();
457
5.23k
        _reserve_nullable_string_columns(columns, batch_size);
458
12.0M
        while (rows < batch_size && !_line_reader_eof &&
459
12.0M
               (columns_byte_size(columns) < max_block_bytes)) {
460
12.0M
            const uint8_t* ptr = nullptr;
461
12.0M
            size_t size = 0;
462
12.0M
            RETURN_IF_ERROR(_line_reader->read_line(&ptr, &size, &_line_reader_eof, _io_ctx));
463
464
            // _skip_lines == 0 means this line is the actual data beginning line for the entire file
465
            // is_remove_bom means _remove_bom should only execute once
466
12.0M
            if (!is_remove_bom && _skip_lines == 0) {
467
5.15k
                ptr = _remove_bom(ptr, size);
468
5.15k
                is_remove_bom = true;
469
5.15k
            }
470
471
            // _skip_lines > 0 means we do not remove bom
472
12.0M
            if (_skip_lines > 0) {
473
123
                _skip_lines--;
474
123
                is_remove_bom = true;
475
123
                continue;
476
123
            }
477
12.0M
            if (size == 0) {
478
2.02k
                if (!_line_reader_eof && _state->is_read_csv_empty_line_as_null()) {
479
0
                    RETURN_IF_ERROR(_fill_empty_line(columns, &rows));
480
0
                }
481
                // Read empty line, continue
482
2.02k
                continue;
483
2.02k
            }
484
485
12.0M
            RETURN_IF_ERROR(_validate_line(Slice(ptr, size), &success));
486
12.0M
            if (!success) {
487
128
                continue;
488
128
            }
489
12.0M
            RETURN_IF_ERROR(_fill_dest_columns(Slice(ptr, size), columns, &rows));
490
12.0M
        }
491
5.23k
    }
492
493
5.22k
    *eof = (rows == 0);
494
5.22k
    *read_rows = rows;
495
496
5.22k
    return Status::OK();
497
5.23k
}
498
499
2.01k
Status CsvReader::_get_columns_impl(std::unordered_map<std::string, DataTypePtr>* name_to_type) {
500
15.4k
    for (const auto& slot : _file_slot_descs) {
501
15.4k
        name_to_type->emplace(slot->col_name(), slot->type());
502
15.4k
    }
503
2.01k
    return Status::OK();
504
2.01k
}
505
506
// init decompressor, file reader and line reader for parsing schema
507
398
Status CsvReader::init_schema_reader() {
508
398
    _start_offset = _range.start_offset;
509
398
    if (_start_offset != 0) {
510
0
        return Status::InvalidArgument(
511
0
                "start offset of TFileRangeDesc must be zero in get parsered schema");
512
0
    }
513
398
    if (_params.file_type == TFileType::FILE_BROKER) {
514
0
        return Status::InternalError<false>(
515
0
                "Getting parsered schema from csv file do not support stream load and broker "
516
0
                "load.");
517
0
    }
518
519
    // csv file without names line and types line.
520
398
    _read_line = 1;
521
398
    _is_parse_name = false;
522
523
398
    if (_params.__isset.file_attributes && _params.file_attributes.__isset.header_type &&
524
398
        !_params.file_attributes.header_type.empty()) {
525
48
        std::string header_type = to_lower(_params.file_attributes.header_type);
526
48
        if (header_type == BeConsts::CSV_WITH_NAMES) {
527
25
            _is_parse_name = true;
528
25
        } else if (header_type == BeConsts::CSV_WITH_NAMES_AND_TYPES) {
529
23
            _read_line = 2;
530
23
            _is_parse_name = true;
531
23
        }
532
48
    }
533
534
398
    RETURN_IF_ERROR(_init_options());
535
398
    RETURN_IF_ERROR(_create_file_reader(true));
536
398
    RETURN_IF_ERROR(_create_decompressor());
537
398
    RETURN_IF_ERROR(_create_line_reader());
538
398
    return Status::OK();
539
398
}
540
541
Status CsvReader::get_parsed_schema(std::vector<std::string>* col_names,
542
398
                                    std::vector<DataTypePtr>* col_types) {
543
398
    if (_read_line == 1) {
544
375
        if (!_is_parse_name) { //parse csv file without names and types
545
350
            size_t col_nums = 0;
546
350
            RETURN_IF_ERROR(_parse_col_nums(&col_nums));
547
2.92k
            for (size_t i = 0; i < col_nums; ++i) {
548
2.57k
                col_names->emplace_back("c" + std::to_string(i + 1));
549
2.57k
            }
550
349
        } else { // parse csv file with names
551
25
            RETURN_IF_ERROR(_parse_col_names(col_names));
552
25
        }
553
554
3.05k
        for (size_t j = 0; j < col_names->size(); ++j) {
555
2.67k
            col_types->emplace_back(
556
2.67k
                    DataTypeFactory::instance().create_data_type(PrimitiveType::TYPE_STRING, true));
557
2.67k
        }
558
374
    } else { // parse csv file with names and types
559
23
        RETURN_IF_ERROR(_parse_col_names(col_names));
560
23
        RETURN_IF_ERROR(_parse_col_types(col_names->size(), col_types));
561
23
    }
562
397
    return Status::OK();
563
398
}
564
565
194M
Status CsvReader::_deserialize_nullable_string(IColumn& column, Slice& slice) {
566
    // This is the per-row per-column hot path of CSV load (load reads every column as
567
    // nullable string). The column type was already verified by the checked assert_cast
568
    // in _reserve_nullable_string_columns at the beginning of the batch, so the casts
569
    // here can skip the release-build typeid check.
570
194M
    auto& null_column = assert_cast<ColumnNullable&, TypeCheckOnRelease::DISABLE>(column);
571
194M
    auto& string_column = assert_cast<ColumnString&, TypeCheckOnRelease::DISABLE>(
572
194M
            null_column.get_nested_column());
573
194M
    if (_empty_field_as_null && slice.size == 0) {
574
5
        string_column.insert_default();
575
5
        null_column.get_null_map_data().push_back(1);
576
5
        return Status::OK();
577
5
    }
578
194M
    if (_options.null_len > 0 && !(_options.converted_from_string && slice.trim_double_quotes())) {
579
192M
        if (slice.compare(Slice(_options.null_format, _options.null_len)) == 0) {
580
30.6k
            string_column.insert_default();
581
30.6k
            null_column.get_null_map_data().push_back(1);
582
30.6k
            return Status::OK();
583
30.6k
        }
584
192M
    }
585
    // Same as DataTypeStringSerDe::deserialize_one_cell_from_csv (which never fails),
586
    // written out here to skip the SerDe layer and its per-cell assert_cast.
587
194M
    if (_options.escape_char != 0) {
588
3.24k
        escape_string_for_csv(slice.data, &slice.size, _options.escape_char, _options.quote_char);
589
3.24k
    }
590
194M
    string_column.insert_data(slice.data, slice.size);
591
194M
    null_column.get_null_map_data().push_back(0);
592
194M
    return Status::OK();
593
194M
}
594
595
2.40k
Status CsvReader::_init_options() {
596
    // get column_separator and line_delimiter
597
2.40k
    _value_separator = _params.file_attributes.text_params.column_separator;
598
2.40k
    _value_separator_length = _value_separator.size();
599
2.40k
    _line_delimiter = _params.file_attributes.text_params.line_delimiter;
600
2.40k
    _line_delimiter_length = _line_delimiter.size();
601
2.40k
    if (_params.file_attributes.text_params.__isset.enclose) {
602
2.40k
        _enclose = _params.file_attributes.text_params.enclose;
603
2.40k
    }
604
2.40k
    if (_params.file_attributes.text_params.__isset.escape) {
605
2.40k
        _escape = _params.file_attributes.text_params.escape;
606
2.40k
    }
607
608
2.40k
    _trim_tailing_spaces =
609
2.40k
            (_state != nullptr && _state->trim_tailing_spaces_for_external_table_query());
610
611
2.40k
    _options.escape_char = _escape;
612
2.40k
    _options.quote_char = _enclose;
613
614
2.40k
    if (_params.file_attributes.text_params.collection_delimiter.empty()) {
615
2.40k
        _options.collection_delim = ',';
616
2.40k
    } else {
617
0
        _options.collection_delim = _params.file_attributes.text_params.collection_delimiter[0];
618
0
    }
619
2.40k
    if (_params.file_attributes.text_params.mapkv_delimiter.empty()) {
620
2.40k
        _options.map_key_delim = ':';
621
2.40k
    } else {
622
0
        _options.map_key_delim = _params.file_attributes.text_params.mapkv_delimiter[0];
623
0
    }
624
625
2.40k
    if (_params.file_attributes.text_params.__isset.null_format) {
626
0
        _options.null_format = _params.file_attributes.text_params.null_format.data();
627
0
        _options.null_len = _params.file_attributes.text_params.null_format.length();
628
0
    }
629
630
2.40k
    if (_params.file_attributes.__isset.trim_double_quotes) {
631
2.40k
        _trim_double_quotes = _params.file_attributes.trim_double_quotes;
632
2.40k
    }
633
2.40k
    _options.converted_from_string = _trim_double_quotes;
634
635
2.40k
    if (_state != nullptr) {
636
2.00k
        _keep_cr = _state->query_options().keep_carriage_return;
637
2.00k
    }
638
639
2.40k
    if (_params.file_attributes.text_params.__isset.empty_field_as_null) {
640
2.40k
        _empty_field_as_null = _params.file_attributes.text_params.empty_field_as_null;
641
2.40k
    }
642
2.40k
    return Status::OK();
643
2.40k
}
644
645
2.41k
Status CsvReader::_create_decompressor() {
646
2.41k
    if (_file_compress_type != TFileCompressType::UNKNOWN) {
647
2.32k
        RETURN_IF_ERROR(Decompressor::create_decompressor(_file_compress_type, &_decompressor));
648
2.32k
    } else {
649
86
        RETURN_IF_ERROR(Decompressor::create_decompressor(_file_format_type, &_decompressor));
650
86
    }
651
652
2.41k
    return Status::OK();
653
2.41k
}
654
655
2.41k
Status CsvReader::_create_file_reader(bool need_schema) {
656
2.41k
    if (_params.file_type == TFileType::FILE_STREAM) {
657
1.83k
        RETURN_IF_ERROR(FileFactory::create_pipe_reader(_range.load_id, &_file_reader, _state,
658
1.83k
                                                        need_schema));
659
1.83k
    } else {
660
579
        _file_description.mtime = _range.__isset.modification_time ? _range.modification_time : 0;
661
579
        io::FileReaderOptions reader_options =
662
579
                FileFactory::get_reader_options(_state, _file_description);
663
579
        io::FileReaderSPtr file_reader;
664
579
        if (_io_ctx_holder) {
665
579
            file_reader = DORIS_TRY(io::DelegateReader::create_file_reader(
666
579
                    _profile, _system_properties, _file_description, reader_options,
667
579
                    io::DelegateReader::AccessMode::SEQUENTIAL,
668
579
                    std::static_pointer_cast<const io::IOContext>(_io_ctx_holder),
669
579
                    io::PrefetchRange(_range.start_offset, _range.start_offset + _range.size)));
670
579
        } else {
671
0
            file_reader = DORIS_TRY(io::DelegateReader::create_file_reader(
672
0
                    _profile, _system_properties, _file_description, reader_options,
673
0
                    io::DelegateReader::AccessMode::SEQUENTIAL, _io_ctx,
674
0
                    io::PrefetchRange(_range.start_offset, _range.start_offset + _range.size)));
675
0
        }
676
579
        _file_reader = _io_ctx && _io_ctx->file_reader_stats
677
579
                               ? std::make_shared<io::TracingFileReader>(std::move(file_reader),
678
579
                                                                         _io_ctx->file_reader_stats)
679
579
                               : file_reader;
680
579
    }
681
2.41k
    if (_file_reader->size() == 0 && _params.file_type != TFileType::FILE_STREAM &&
682
2.41k
        _params.file_type != TFileType::FILE_BROKER) {
683
0
        return Status::EndOfFile("init reader failed, empty csv file: " + _range.path);
684
0
    }
685
2.41k
    return Status::OK();
686
2.41k
}
687
688
2.40k
Status CsvReader::_create_line_reader() {
689
2.40k
    std::shared_ptr<TextLineReaderContextIf> text_line_reader_ctx;
690
2.40k
    if (_enclose == 0) {
691
2.30k
        text_line_reader_ctx = std::make_shared<PlainTextLineReaderCtx>(
692
2.30k
                _line_delimiter, _line_delimiter_length, _keep_cr);
693
2.30k
        _fields_splitter = std::make_unique<PlainCsvTextFieldSplitter>(
694
2.30k
                _trim_tailing_spaces, false, _value_separator, _value_separator_length, -1);
695
696
2.30k
    } else {
697
        // in load task, the _file_slot_descs is empty vector, so we need to set col_sep_num to 0
698
96
        size_t col_sep_num = _file_slot_descs.size() > 1 ? _file_slot_descs.size() - 1 : 0;
699
96
        _enclose_reader_ctx = std::make_shared<EncloseCsvLineReaderCtx>(
700
96
                _line_delimiter, _line_delimiter_length, _value_separator, _value_separator_length,
701
96
                col_sep_num, _enclose, _escape, _keep_cr);
702
96
        text_line_reader_ctx = _enclose_reader_ctx;
703
704
96
        _fields_splitter = std::make_unique<EncloseCsvTextFieldSplitter>(
705
96
                _trim_tailing_spaces, true, _enclose_reader_ctx, _value_separator_length, _enclose);
706
96
    }
707
2.40k
    switch (_file_format_type) {
708
2.33k
    case TFileFormatType::FORMAT_CSV_PLAIN:
709
2.33k
        [[fallthrough]];
710
2.33k
    case TFileFormatType::FORMAT_CSV_GZ:
711
2.33k
        [[fallthrough]];
712
2.33k
    case TFileFormatType::FORMAT_CSV_BZ2:
713
2.33k
        [[fallthrough]];
714
2.33k
    case TFileFormatType::FORMAT_CSV_LZ4FRAME:
715
2.33k
        [[fallthrough]];
716
2.33k
    case TFileFormatType::FORMAT_CSV_LZ4BLOCK:
717
2.33k
        [[fallthrough]];
718
2.33k
    case TFileFormatType::FORMAT_CSV_LZOP:
719
2.33k
        [[fallthrough]];
720
2.33k
    case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK:
721
2.33k
        [[fallthrough]];
722
2.33k
    case TFileFormatType::FORMAT_CSV_DEFLATE:
723
2.33k
        _line_reader =
724
2.33k
                NewPlainTextLineReader::create_unique(_profile, _file_reader, _decompressor.get(),
725
2.33k
                                                      text_line_reader_ctx, _size, _start_offset);
726
727
2.33k
        break;
728
77
    case TFileFormatType::FORMAT_PROTO:
729
77
        _fields_splitter = std::make_unique<CsvProtoFieldSplitter>();
730
77
        _line_reader = NewPlainBinaryLineReader::create_unique(_file_reader);
731
77
        break;
732
0
    default:
733
0
        return Status::InternalError<false>(
734
0
                "Unknown format type, cannot init line reader in csv reader, type={}",
735
0
                _file_format_type);
736
2.40k
    }
737
2.40k
    return Status::OK();
738
2.40k
}
739
740
300
Status CsvReader::_deserialize_one_cell(DataTypeSerDeSPtr serde, IColumn& column, Slice& slice) {
741
300
    return serde->deserialize_one_cell_from_csv(column, slice, _options);
742
300
}
743
744
Status CsvReader::_fill_dest_columns(const Slice& line, std::vector<MutableColumnPtr>& columns,
745
12.0M
                                     size_t* rows) {
746
12.0M
    bool is_success = false;
747
748
12.0M
    RETURN_IF_ERROR(_line_split_to_values(line, &is_success));
749
12.0M
    if (UNLIKELY(!is_success)) {
750
        // If not success, which means we met an invalid row, filter this row and return.
751
539
        return Status::OK();
752
539
    }
753
754
199M
    for (int i = 0; i < _file_slot_descs.size(); ++i) {
755
187M
        int col_idx = _col_idxs[i];
756
        // col idx is out of range, fill with null format
757
187M
        auto value = col_idx < _split_values.size()
758
187M
                             ? _split_values[col_idx]
759
18.4E
                             : Slice(_options.null_format, _options.null_len);
760
761
187M
        IColumn* col_ptr = columns[i].get();
762
187M
        if (!_is_load) {
763
20.6M
            col_ptr = columns[_file_slot_idx_map[i]].get();
764
20.6M
        }
765
766
195M
        if (_use_nullable_string_opt[i]) {
767
            // For load task, we always read "string" from file.
768
            // So serdes[i] here must be DataTypeNullableSerDe, and DataTypeNullableSerDe -> nested_serde must be DataTypeStringSerDe.
769
            // So we use deserialize_nullable_string and stringSerDe to reduce virtual function calls.
770
195M
            RETURN_IF_ERROR(_deserialize_nullable_string(*col_ptr, value));
771
18.4E
        } else {
772
18.4E
            RETURN_IF_ERROR(_deserialize_one_cell(_serdes[i], *col_ptr, value));
773
18.4E
        }
774
187M
    }
775
12.0M
    ++(*rows);
776
777
12.0M
    return Status::OK();
778
12.0M
}
779
780
void CsvReader::_reserve_nullable_string_columns(std::vector<MutableColumnPtr>& columns,
781
5.23k
                                                 size_t batch_size) {
782
71.4k
    for (int i = 0; i < _file_slot_descs.size(); ++i) {
783
66.2k
        if (!_use_nullable_string_opt[i]) {
784
15
            continue;
785
15
        }
786
66.2k
        IColumn* col_ptr = _is_load ? columns[i].get() : columns[_file_slot_idx_map[i]].get();
787
        // The checked casts here (once per batch) guarantee the column types for the
788
        // unchecked per-row casts in _deserialize_nullable_string.
789
66.2k
        auto& null_column = assert_cast<ColumnNullable&>(*col_ptr);
790
66.2k
        auto& string_column = assert_cast<ColumnString&>(null_column.get_nested_column());
791
        // Reserve up front so the per-row loop does not pay for incremental growth.
792
        // The string chars are not reserved because their total size is unpredictable.
793
66.2k
        string_column.get_offsets().reserve(string_column.size() + batch_size);
794
66.2k
        null_column.get_null_map_data().reserve(null_column.get_null_map_data().size() +
795
66.2k
                                                batch_size);
796
66.2k
    }
797
5.23k
}
798
799
0
Status CsvReader::_fill_empty_line(std::vector<MutableColumnPtr>& columns, size_t* rows) {
800
0
    for (int i = 0; i < _file_slot_descs.size(); ++i) {
801
0
        IColumn* col_ptr = columns[i].get();
802
0
        if (!_is_load) {
803
0
            col_ptr = columns[_file_slot_idx_map[i]].get();
804
0
        }
805
0
        auto& null_column = assert_cast<ColumnNullable&>(*col_ptr);
806
0
        null_column.insert_data(nullptr, 0);
807
0
    }
808
0
    ++(*rows);
809
0
    return Status::OK();
810
0
}
811
812
12.0M
Status CsvReader::_validate_line(const Slice& line, bool* success) {
813
12.0M
    if (!_is_proto_format && !validate_utf8(_params, line.data, line.size)) {
814
128
        if (!_is_load) {
815
0
            return Status::InternalError<false>("Only support csv data in utf8 codec");
816
128
        } else {
817
128
            _counter->num_rows_filtered++;
818
128
            *success = false;
819
128
            RETURN_IF_ERROR(_state->append_error_msg_to_file(
820
128
                    [&]() -> std::string { return std::string(line.data, line.size); },
821
128
                    [&]() -> std::string {
822
128
                        return "Invalid file encoding: all CSV files must be UTF-8 encoded";
823
128
                    }));
824
128
            return Status::OK();
825
128
        }
826
128
    }
827
12.0M
    *success = true;
828
12.0M
    return Status::OK();
829
12.0M
}
830
831
12.0M
Status CsvReader::_line_split_to_values(const Slice& line, bool* success) {
832
12.0M
    _split_line(line);
833
834
12.0M
    if (_is_load) {
835
        // Only check for load task. For query task, the non exist column will be filled "null".
836
        // if actual column number in csv file is not equal to _file_slot_descs.size()
837
        // then filter this line.
838
10.8M
        bool ignore_col = false;
839
10.8M
        ignore_col = _params.__isset.file_attributes &&
840
10.8M
                     _params.file_attributes.__isset.ignore_csv_redundant_col &&
841
10.8M
                     _params.file_attributes.ignore_csv_redundant_col;
842
843
10.8M
        if ((!ignore_col && _split_values.size() != _file_slot_descs.size()) ||
844
10.8M
            (ignore_col && _split_values.size() < _file_slot_descs.size())) {
845
544
            _counter->num_rows_filtered++;
846
544
            *success = false;
847
544
            RETURN_IF_ERROR(_state->append_error_msg_to_file(
848
544
                    [&]() -> std::string { return std::string(line.data, line.size); },
849
544
                    [&]() -> std::string {
850
544
                        fmt::memory_buffer error_msg;
851
544
                        fmt::format_to(error_msg,
852
544
                                       "Column count mismatch: expected {}, but found {}",
853
544
                                       _file_slot_descs.size(), _split_values.size());
854
544
                        std::string escaped_separator =
855
544
                                std::regex_replace(_value_separator, std::regex("\t"), "\\t");
856
544
                        std::string escaped_delimiter =
857
544
                                std::regex_replace(_line_delimiter, std::regex("\n"), "\\n");
858
544
                        fmt::format_to(error_msg, " (sep:{} delim:{}", escaped_separator,
859
544
                                       escaped_delimiter);
860
544
                        if (_enclose != 0) {
861
544
                            fmt::format_to(error_msg, " encl:{}", _enclose);
862
544
                        }
863
544
                        if (_escape != 0) {
864
544
                            fmt::format_to(error_msg, " esc:{}", _escape);
865
544
                        }
866
544
                        fmt::format_to(error_msg, ")");
867
544
                        return fmt::to_string(error_msg);
868
544
                    }));
869
539
            return Status::OK();
870
544
        }
871
10.8M
    }
872
873
12.0M
    *success = true;
874
12.0M
    return Status::OK();
875
12.0M
}
876
877
12.0M
void CsvReader::_split_line(const Slice& line) {
878
12.0M
    _split_values.clear();
879
12.0M
    _fields_splitter->split_line(line, &_split_values);
880
12.0M
}
881
882
350
Status CsvReader::_parse_col_nums(size_t* col_nums) {
883
350
    const uint8_t* ptr = nullptr;
884
350
    size_t size = 0;
885
350
    RETURN_IF_ERROR(_line_reader->read_line(&ptr, &size, &_line_reader_eof, _io_ctx));
886
350
    if (size == 0) {
887
0
        return Status::InternalError<false>(
888
0
                "The first line is empty, can not parse column numbers");
889
0
    }
890
350
    if (!validate_utf8(_params, reinterpret_cast<const char*>(ptr), size)) {
891
1
        return Status::InternalError<false>("Only support csv data in utf8 codec");
892
1
    }
893
349
    ptr = _remove_bom(ptr, size);
894
349
    _split_line(Slice(ptr, size));
895
349
    *col_nums = _split_values.size();
896
349
    return Status::OK();
897
350
}
898
899
48
Status CsvReader::_parse_col_names(std::vector<std::string>* col_names) {
900
48
    const uint8_t* ptr = nullptr;
901
48
    size_t size = 0;
902
    // no use of _line_reader_eof
903
48
    RETURN_IF_ERROR(_line_reader->read_line(&ptr, &size, &_line_reader_eof, _io_ctx));
904
48
    if (size == 0) {
905
0
        return Status::InternalError<false>("The first line is empty, can not parse column names");
906
0
    }
907
48
    if (!validate_utf8(_params, reinterpret_cast<const char*>(ptr), size)) {
908
0
        return Status::InternalError<false>("Only support csv data in utf8 codec");
909
0
    }
910
48
    ptr = _remove_bom(ptr, size);
911
48
    _split_line(Slice(ptr, size));
912
187
    for (auto _split_value : _split_values) {
913
187
        col_names->emplace_back(_split_value.to_string());
914
187
    }
915
48
    return Status::OK();
916
48
}
917
918
// TODO(ftw): parse type
919
23
Status CsvReader::_parse_col_types(size_t col_nums, std::vector<DataTypePtr>* col_types) {
920
    // delete after.
921
113
    for (size_t i = 0; i < col_nums; ++i) {
922
90
        col_types->emplace_back(make_nullable(std::make_shared<DataTypeString>()));
923
90
    }
924
925
    // 1. check _line_reader_eof
926
    // 2. read line
927
    // 3. check utf8
928
    // 4. check size
929
    // 5. check _split_values.size must equal to col_nums.
930
    // 6. fill col_types
931
23
    return Status::OK();
932
23
}
933
934
5.55k
const uint8_t* CsvReader::_remove_bom(const uint8_t* ptr, size_t& size) {
935
5.55k
    if (size >= 3 && ptr[0] == 0xEF && ptr[1] == 0xBB && ptr[2] == 0xBF) {
936
7
        LOG(INFO) << "remove bom";
937
7
        constexpr size_t bom_size = 3;
938
7
        size -= bom_size;
939
        // In enclose mode, column_sep_positions were computed on the original line
940
        // (including BOM). After shifting the pointer, we must adjust those positions
941
        // so they remain correct relative to the new start.
942
7
        if (_enclose_reader_ctx) {
943
1
            _enclose_reader_ctx->adjust_column_sep_positions(bom_size);
944
1
        }
945
7
        return ptr + bom_size;
946
7
    }
947
5.54k
    return ptr;
948
5.55k
}
949
950
2.01k
Status CsvReader::close() {
951
2.01k
    if (_line_reader) {
952
2.01k
        _line_reader->close();
953
2.01k
    }
954
955
2.01k
    if (_file_reader) {
956
2.01k
        RETURN_IF_ERROR(_file_reader->close());
957
2.01k
    }
958
959
2.01k
    return Status::OK();
960
2.01k
}
961
962
} // namespace doris