Coverage Report

Created: 2026-04-13 11:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/text/text_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/text/text_reader.h"
19
20
#include <gen_cpp/PlanNodes_types.h>
21
#include <gen_cpp/Types_types.h>
22
#include <glog/logging.h>
23
24
#include <cstddef>
25
#include <vector>
26
27
#include "common/compiler_util.h" // IWYU pragma: keep
28
#include "common/status.h"
29
#include "core/block/block.h"
30
#include "exec/scan/scanner.h"
31
#include "format/csv/csv_reader.h"
32
#include "format/file_reader/new_plain_text_line_reader.h"
33
#include "format/line_reader.h"
34
#include "io/file_factory.h"
35
#include "io/fs/buffered_reader.h"
36
#include "io/fs/file_reader.h"
37
#include "io/fs/s3_file_reader.h"
38
#include "runtime/descriptors.h"
39
#include "runtime/runtime_state.h"
40
41
namespace doris {
42
43
33
void HiveTextFieldSplitter::do_split(const Slice& line, std::vector<Slice>* splitted_values) {
44
33
    if (_value_sep_len == 1) {
45
16
        _split_field_single_char(line, splitted_values);
46
17
    } else {
47
17
        _split_field_multi_char(line, splitted_values);
48
17
    }
49
33
}
50
51
void HiveTextFieldSplitter::_split_field_single_char(const Slice& line,
52
16
                                                     std::vector<Slice>* splitted_values) {
53
16
    const char* data = line.data;
54
16
    const size_t size = line.size;
55
16
    size_t value_start = 0;
56
933
    for (size_t i = 0; i < size; ++i) {
57
917
        if (data[i] == _value_sep[0]) {
58
            // hive will escape the field separator in string
59
127
            if (_escape_char != 0 && i > 0 && data[i - 1] == _escape_char) {
60
1
                continue;
61
1
            }
62
126
            process_value_func(data, value_start, i - value_start, _trimming_char, splitted_values);
63
126
            value_start = i + _value_sep_len;
64
126
        }
65
917
    }
66
16
    process_value_func(data, value_start, size - value_start, _trimming_char, splitted_values);
67
16
}
68
69
void HiveTextFieldSplitter::_split_field_multi_char(const Slice& line,
70
17
                                                    std::vector<Slice>* splitted_values) {
71
17
    const char* data = line.data;
72
17
    const size_t size = line.size;
73
17
    size_t start = 0;
74
75
17
    std::vector<int> next(_value_sep_len);
76
17
    next[0] = -1;
77
50
    for (int i = 1, j = -1; i < (int)_value_sep_len; i++) {
78
33
        while (j >= 0 && _value_sep[i] != _value_sep[j + 1]) {
79
0
            j = next[j];
80
0
        }
81
33
        if (_value_sep[i] == _value_sep[j + 1]) {
82
22
            j++;
83
22
        }
84
33
        next[i] = j;
85
33
    }
86
87
    // KMP search
88
198
    for (int i = 0, j = -1; i < (int)size; i++) {
89
200
        while (j >= 0 && data[i] != _value_sep[j + 1]) {
90
19
            j = next[j];
91
19
        }
92
181
        if (data[i] == _value_sep[j + 1]) {
93
90
            j++;
94
90
        }
95
181
        if (j == (int)_value_sep_len - 1) {
96
34
            size_t curpos = i - _value_sep_len + 1;
97
34
            if (_escape_char != 0 && curpos > 0 && data[curpos - 1] == _escape_char) {
98
2
                j = next[j];
99
2
                continue;
100
2
            }
101
102
32
            if (curpos >= start) {
103
25
                process_value_func(data, start, curpos - start, _trimming_char, splitted_values);
104
25
                start = curpos + _value_sep_len;
105
25
            }
106
107
32
            j = next[j];
108
32
        }
109
181
    }
110
17
    process_value_func(data, start, size - start, _trimming_char, splitted_values);
111
17
}
112
113
TextReader::TextReader(RuntimeState* state, RuntimeProfile* profile, ScannerCounter* counter,
114
                       const TFileScanRangeParams& params, const TFileRangeDesc& range,
115
                       const std::vector<SlotDescriptor*>& file_slot_descs, io::IOContext* io_ctx)
116
3
        : CsvReader(state, profile, counter, params, range, file_slot_descs, io_ctx) {}
117
118
3
Status TextReader::_init_options() {
119
    // get column_separator and line_delimiter
120
3
    _value_separator = _params.file_attributes.text_params.column_separator;
121
3
    _value_separator_length = _value_separator.size();
122
3
    _line_delimiter = _params.file_attributes.text_params.line_delimiter;
123
3
    _line_delimiter_length = _line_delimiter.size();
124
125
3
    if (_params.file_attributes.text_params.__isset.escape) {
126
0
        _escape = _params.file_attributes.text_params.escape;
127
0
    }
128
3
    _options.escape_char = _escape;
129
130
3
    _options.collection_delim = _params.file_attributes.text_params.collection_delimiter[0];
131
3
    _options.map_key_delim = _params.file_attributes.text_params.mapkv_delimiter[0];
132
133
3
    _options.null_format = _params.file_attributes.text_params.null_format.data();
134
3
    _options.null_len = _params.file_attributes.text_params.null_format.length();
135
136
3
    return Status::OK();
137
3
}
138
139
0
Status TextReader::_deserialize_one_cell(DataTypeSerDeSPtr serde, IColumn& column, Slice& slice) {
140
0
    return serde->deserialize_one_cell_from_hive_text(column, slice, _options);
141
0
}
142
143
3
Status TextReader::_create_line_reader() {
144
3
    std::shared_ptr<TextLineReaderContextIf> text_line_reader_ctx;
145
146
3
    text_line_reader_ctx = std::make_shared<PlainTextLineReaderCtx>(_line_delimiter,
147
3
                                                                    _line_delimiter_length, false);
148
149
3
    _fields_splitter = std::make_unique<HiveTextFieldSplitter>(
150
3
            false, false, _value_separator, _value_separator_length, -1, _escape);
151
152
3
    _line_reader =
153
3
            NewPlainTextLineReader::create_unique(_profile, _file_reader, _decompressor.get(),
154
3
                                                  text_line_reader_ctx, _size, _start_offset);
155
156
3
    return Status::OK();
157
3
}
158
159
9
Status TextReader::_validate_line(const Slice& line, bool* success) {
160
    // text file do not need utf8 check
161
9
    *success = true;
162
9
    return Status::OK();
163
9
}
164
165
126
Status TextReader::_deserialize_nullable_string(IColumn& column, Slice& slice) {
166
126
    auto& null_column = assert_cast<ColumnNullable&>(column);
167
126
    if (slice.compare(Slice(_options.null_format, _options.null_len)) == 0) {
168
0
        null_column.insert_data(nullptr, 0);
169
0
        return Status::OK();
170
0
    }
171
126
    static DataTypeStringSerDe stringSerDe(TYPE_STRING);
172
126
    auto st = stringSerDe.deserialize_one_cell_from_hive_text(null_column.get_nested_column(),
173
126
                                                              slice, _options);
174
126
    if (!st.ok()) {
175
        // fill null if fail
176
0
        null_column.insert_data(nullptr, 0); // 0 is meaningless here
177
0
        return Status::OK();
178
0
    }
179
    // fill not null if success
180
126
    null_column.get_null_map_data().push_back(0);
181
126
    return Status::OK();
182
126
}
183
184
} // namespace doris