Coverage Report

Created: 2025-07-27 03:09

/root/doris/be/src/olap/schema.cpp
Line
Count
Source (jump to first uncovered line)
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 "olap/schema.h"
19
20
#include <glog/logging.h>
21
22
#include <boost/iterator/iterator_facade.hpp>
23
#include <ostream>
24
#include <unordered_set>
25
#include <utility>
26
27
#include "common/config.h"
28
#include "olap/olap_common.h"
29
#include "runtime/define_primitive_type.h"
30
#include "util/trace.h"
31
#include "vec/columns/column_array.h"
32
#include "vec/columns/column_dictionary.h"
33
#include "vec/columns/column_map.h"
34
#include "vec/columns/column_nullable.h"
35
#include "vec/columns/column_struct.h"
36
#include "vec/columns/columns_number.h"
37
#include "vec/columns/predicate_column.h"
38
#include "vec/core/types.h"
39
#include "vec/data_types/data_type.h"
40
#include "vec/data_types/data_type_factory.hpp"
41
42
namespace doris {
43
44
1
Schema::Schema(const Schema& other) {
45
1
    _copy_from(other);
46
1
}
47
48
0
Schema& Schema::operator=(const Schema& other) {
49
0
    if (this != &other) {
  Branch (49:9): [True: 0, False: 0]
50
0
        _copy_from(other);
51
0
    }
52
0
    return *this;
53
0
}
54
55
1
void Schema::_copy_from(const Schema& other) {
56
1
    _col_ids = other._col_ids;
57
1
    _col_offsets = other._col_offsets;
58
59
1
    _num_key_columns = other._num_key_columns;
60
1
    _schema_size = other._schema_size;
61
62
    // Deep copy _cols
63
    // TODO(lingbin): really need clone?
64
1
    _cols.resize(other._cols.size(), nullptr);
65
2
    for (auto cid : _col_ids) {
  Branch (65:19): [True: 2, False: 1]
66
2
        _cols[cid] = other._cols[cid]->clone();
67
2
    }
68
1
}
69
70
void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
71
5.45k
                   size_t num_key_columns) {
72
5.45k
    _col_ids = col_ids;
73
5.45k
    _num_key_columns = num_key_columns;
74
75
5.45k
    _cols.resize(cols.size(), nullptr);
76
5.45k
    _col_offsets.resize(_cols.size(), -1);
77
78
5.45k
    size_t offset = 0;
79
5.45k
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
80
39.3k
    for (int cid = 0; cid < cols.size(); ++cid) {
  Branch (80:23): [True: 33.8k, False: 5.45k]
81
33.8k
        if (col_id_set.find(cid) == col_id_set.end()) {
  Branch (81:13): [True: 20.8k, False: 13.0k]
82
20.8k
            continue;
83
20.8k
        }
84
13.0k
        _cols[cid] = FieldFactory::create(*cols[cid]);
85
86
13.0k
        _col_offsets[cid] = offset;
87
        // Plus 1 byte for null byte
88
13.0k
        offset += _cols[cid]->size() + 1;
89
13.0k
    }
90
91
5.45k
    _schema_size = offset;
92
5.45k
}
93
94
void Schema::_init(const std::vector<const Field*>& cols, const std::vector<ColumnId>& col_ids,
95
0
                   size_t num_key_columns) {
96
0
    _col_ids = col_ids;
97
0
    _num_key_columns = num_key_columns;
98
99
0
    _cols.resize(cols.size(), nullptr);
100
0
    _col_offsets.resize(_cols.size(), -1);
101
102
0
    size_t offset = 0;
103
0
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
104
0
    for (int cid = 0; cid < cols.size(); ++cid) {
  Branch (104:23): [True: 0, False: 0]
105
0
        if (col_id_set.find(cid) == col_id_set.end()) {
  Branch (105:13): [True: 0, False: 0]
106
0
            continue;
107
0
        }
108
        // TODO(lingbin): is it necessary to clone Field? each SegmentIterator will
109
        // use this func, can we avoid clone?
110
0
        _cols[cid] = cols[cid]->clone();
111
112
0
        _col_offsets[cid] = offset;
113
        // Plus 1 byte for null byte
114
0
        offset += _cols[cid]->size() + 1;
115
0
    }
116
117
0
    _schema_size = offset;
118
0
}
119
120
5.45k
Schema::~Schema() {
121
33.8k
    for (auto col : _cols) {
  Branch (121:19): [True: 33.8k, False: 5.45k]
122
33.8k
        delete col;
123
33.8k
    }
124
5.45k
}
125
126
117k
vectorized::DataTypePtr Schema::get_data_type_ptr(const Field& field) {
127
117k
    return vectorized::DataTypeFactory::instance().create_data_type(field);
128
117k
}
129
130
0
vectorized::IColumn::MutablePtr Schema::get_column_by_field(const Field& field) {
131
0
    return get_data_type_ptr(field)->create_column();
132
0
}
133
134
vectorized::IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type,
135
                                                                 bool is_nullable,
136
467
                                                                 const ReaderType reader_type) {
137
467
    vectorized::IColumn::MutablePtr ptr = nullptr;
138
467
    switch (type) {
139
0
    case FieldType::OLAP_FIELD_TYPE_BOOL:
  Branch (139:5): [True: 0, False: 467]
140
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_BOOLEAN>::create();
141
0
        break;
142
0
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
  Branch (142:5): [True: 0, False: 467]
143
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_TINYINT>::create();
144
0
        break;
145
0
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
  Branch (145:5): [True: 0, False: 467]
146
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_SMALLINT>::create();
147
0
        break;
148
327
    case FieldType::OLAP_FIELD_TYPE_INT:
  Branch (148:5): [True: 327, False: 140]
149
327
        ptr = doris::vectorized::PredicateColumnType<TYPE_INT>::create();
150
327
        break;
151
0
    case FieldType::OLAP_FIELD_TYPE_FLOAT:
  Branch (151:5): [True: 0, False: 467]
152
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_FLOAT>::create();
153
0
        break;
154
0
    case FieldType::OLAP_FIELD_TYPE_DOUBLE:
  Branch (154:5): [True: 0, False: 467]
155
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DOUBLE>::create();
156
0
        break;
157
0
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
  Branch (157:5): [True: 0, False: 467]
158
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_BIGINT>::create();
159
0
        break;
160
0
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
  Branch (160:5): [True: 0, False: 467]
161
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_LARGEINT>::create();
162
0
        break;
163
0
    case FieldType::OLAP_FIELD_TYPE_DATE:
  Branch (163:5): [True: 0, False: 467]
164
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATE>::create();
165
0
        break;
166
0
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
  Branch (166:5): [True: 0, False: 467]
167
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATEV2>::create();
168
0
        break;
169
0
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
  Branch (169:5): [True: 0, False: 467]
170
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIMEV2>::create();
171
0
        break;
172
0
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
  Branch (172:5): [True: 0, False: 467]
173
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIME>::create();
174
0
        break;
175
0
    case FieldType::OLAP_FIELD_TYPE_CHAR:
  Branch (175:5): [True: 0, False: 467]
176
0
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
  Branch (176:13): [True: 0, False: 0]
  Branch (176:56): [True: 0, False: 0]
177
0
            ptr = doris::vectorized::ColumnDictionary<doris::vectorized::Int32>::create(type);
178
0
        } else {
179
0
            ptr = doris::vectorized::PredicateColumnType<TYPE_CHAR>::create();
180
0
        }
181
0
        break;
182
0
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
  Branch (182:5): [True: 0, False: 467]
183
140
    case FieldType::OLAP_FIELD_TYPE_STRING:
  Branch (183:5): [True: 140, False: 327]
184
140
    case FieldType::OLAP_FIELD_TYPE_JSONB:
  Branch (184:5): [True: 0, False: 467]
185
140
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
  Branch (185:13): [True: 140, False: 0]
  Branch (185:56): [True: 0, False: 140]
186
0
            ptr = doris::vectorized::ColumnDictionary<doris::vectorized::Int32>::create(type);
187
140
        } else {
188
140
            ptr = doris::vectorized::PredicateColumnType<TYPE_STRING>::create();
189
140
        }
190
140
        break;
191
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
  Branch (191:5): [True: 0, False: 467]
192
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMALV2>::create();
193
0
        break;
194
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
  Branch (194:5): [True: 0, False: 467]
195
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL32>::create();
196
0
        break;
197
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
  Branch (197:5): [True: 0, False: 467]
198
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL64>::create();
199
0
        break;
200
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
  Branch (200:5): [True: 0, False: 467]
201
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL128I>::create();
202
0
        break;
203
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
  Branch (203:5): [True: 0, False: 467]
204
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL256>::create();
205
0
        break;
206
0
    case FieldType::OLAP_FIELD_TYPE_IPV4:
  Branch (206:5): [True: 0, False: 467]
207
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_IPV4>::create();
208
0
        break;
209
0
    case FieldType::OLAP_FIELD_TYPE_IPV6:
  Branch (209:5): [True: 0, False: 467]
210
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_IPV6>::create();
211
0
        break;
212
0
    default:
  Branch (212:5): [True: 0, False: 467]
213
0
        throw Exception(
214
0
                ErrorCode::SCHEMA_SCHEMA_FIELD_INVALID,
215
0
                fmt::format("Unexpected type when choosing predicate column, type={}", int(type)));
216
467
    }
217
218
467
    if (is_nullable) {
  Branch (218:9): [True: 140, False: 327]
219
140
        return doris::vectorized::ColumnNullable::create(std::move(ptr),
220
140
                                                         doris::vectorized::ColumnUInt8::create());
221
140
    }
222
327
    return ptr;
223
467
}
224
225
} // namespace doris