/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/predicate_column.h" |
37 | | #include "vec/core/types.h" |
38 | | #include "vec/data_types/data_type.h" |
39 | | #include "vec/data_types/data_type_factory.hpp" |
40 | | |
41 | | namespace doris { |
42 | | |
43 | 5.08M | Schema::Schema(const Schema& other) { |
44 | 5.08M | _copy_from(other); |
45 | 5.08M | } |
46 | | |
47 | 0 | Schema& Schema::operator=(const Schema& other) { |
48 | 0 | if (this != &other) { |
49 | 0 | _copy_from(other); |
50 | 0 | } |
51 | 0 | return *this; |
52 | 0 | } |
53 | | |
54 | 5.11M | void Schema::_copy_from(const Schema& other) { |
55 | 5.11M | _col_ids = other._col_ids; |
56 | 5.11M | _col_offsets = other._col_offsets; |
57 | | |
58 | 5.11M | _num_key_columns = other._num_key_columns; |
59 | 5.11M | _schema_size = other._schema_size; |
60 | | |
61 | | // Deep copy _cols |
62 | | // TODO(lingbin): really need clone? |
63 | 5.11M | _cols.resize(other._cols.size(), nullptr); |
64 | 6.93M | for (auto cid : _col_ids) { |
65 | 6.93M | _cols[cid] = other._cols[cid]->clone(); |
66 | 6.93M | } |
67 | 5.11M | } |
68 | | |
69 | | void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids, |
70 | 8.60M | size_t num_key_columns) { |
71 | 8.60M | _col_ids = col_ids; |
72 | 8.60M | _num_key_columns = num_key_columns; |
73 | | |
74 | 8.60M | _cols.resize(cols.size(), nullptr); |
75 | 8.60M | _col_offsets.resize(_cols.size(), -1); |
76 | | |
77 | 8.60M | size_t offset = 0; |
78 | 8.60M | std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end()); |
79 | 131M | for (int cid = 0; cid < cols.size(); ++cid) { |
80 | 122M | if (col_id_set.find(cid) == col_id_set.end()) { |
81 | 72.8M | continue; |
82 | 72.8M | } |
83 | 50.1M | _cols[cid] = FieldFactory::create(*cols[cid]); |
84 | | |
85 | 50.1M | _col_offsets[cid] = offset; |
86 | | // Plus 1 byte for null byte |
87 | 50.1M | offset += _cols[cid]->size() + 1; |
88 | 50.1M | } |
89 | | |
90 | 8.60M | _schema_size = offset; |
91 | 8.60M | } |
92 | | |
93 | | void Schema::_init(const std::vector<const Field*>& cols, const std::vector<ColumnId>& col_ids, |
94 | 1.46M | size_t num_key_columns) { |
95 | 1.46M | _col_ids = col_ids; |
96 | 1.46M | _num_key_columns = num_key_columns; |
97 | | |
98 | 1.46M | _cols.resize(cols.size(), nullptr); |
99 | 1.46M | _col_offsets.resize(_cols.size(), -1); |
100 | | |
101 | 1.46M | size_t offset = 0; |
102 | 1.46M | std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end()); |
103 | 6.74M | for (int cid = 0; cid < cols.size(); ++cid) { |
104 | 5.27M | if (col_id_set.find(cid) == col_id_set.end()) { |
105 | 0 | continue; |
106 | 0 | } |
107 | | // TODO(lingbin): is it necessary to clone Field? each SegmentIterator will |
108 | | // use this func, can we avoid clone? |
109 | 5.27M | _cols[cid] = cols[cid]->clone(); |
110 | | |
111 | 5.27M | _col_offsets[cid] = offset; |
112 | | // Plus 1 byte for null byte |
113 | 5.27M | offset += _cols[cid]->size() + 1; |
114 | 5.27M | } |
115 | | |
116 | 1.46M | _schema_size = offset; |
117 | 1.46M | } |
118 | | |
119 | 15.2M | Schema::~Schema() { |
120 | 186M | for (auto col : _cols) { |
121 | 186M | delete col; |
122 | 186M | } |
123 | 15.2M | } |
124 | | |
125 | 50.1M | vectorized::DataTypePtr Schema::get_data_type_ptr(const Field& field) { |
126 | 50.1M | return vectorized::DataTypeFactory::instance().create_data_type(field); |
127 | 50.1M | } |
128 | | |
129 | 5.44M | vectorized::IColumn::MutablePtr Schema::get_column_by_field(const Field& field) { |
130 | 5.44M | return get_data_type_ptr(field)->create_column(); |
131 | 5.44M | } |
132 | | |
133 | | vectorized::IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type, |
134 | | bool is_nullable, |
135 | 1.36M | const ReaderType reader_type) { |
136 | 1.36M | vectorized::IColumn::MutablePtr ptr = nullptr; |
137 | 1.36M | switch (type) { |
138 | 214 | case FieldType::OLAP_FIELD_TYPE_BOOL: |
139 | 214 | ptr = doris::vectorized::PredicateColumnType<TYPE_BOOLEAN>::create(); |
140 | 214 | break; |
141 | 1.31M | case FieldType::OLAP_FIELD_TYPE_TINYINT: |
142 | 1.31M | ptr = doris::vectorized::PredicateColumnType<TYPE_TINYINT>::create(); |
143 | 1.31M | break; |
144 | 879 | case FieldType::OLAP_FIELD_TYPE_SMALLINT: |
145 | 879 | ptr = doris::vectorized::PredicateColumnType<TYPE_SMALLINT>::create(); |
146 | 879 | break; |
147 | 21.6k | case FieldType::OLAP_FIELD_TYPE_INT: |
148 | 21.6k | ptr = doris::vectorized::PredicateColumnType<TYPE_INT>::create(); |
149 | 21.6k | break; |
150 | 0 | case FieldType::OLAP_FIELD_TYPE_FLOAT: |
151 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_FLOAT>::create(); |
152 | 0 | break; |
153 | 0 | case FieldType::OLAP_FIELD_TYPE_DOUBLE: |
154 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DOUBLE>::create(); |
155 | 0 | break; |
156 | 3.63k | case FieldType::OLAP_FIELD_TYPE_BIGINT: |
157 | 3.63k | ptr = doris::vectorized::PredicateColumnType<TYPE_BIGINT>::create(); |
158 | 3.63k | break; |
159 | 516 | case FieldType::OLAP_FIELD_TYPE_LARGEINT: |
160 | 516 | ptr = doris::vectorized::PredicateColumnType<TYPE_LARGEINT>::create(); |
161 | 516 | break; |
162 | 203 | case FieldType::OLAP_FIELD_TYPE_DATE: |
163 | 203 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATE>::create(); |
164 | 203 | break; |
165 | 4.85k | case FieldType::OLAP_FIELD_TYPE_DATEV2: |
166 | 4.85k | ptr = doris::vectorized::PredicateColumnType<TYPE_DATEV2>::create(); |
167 | 4.85k | break; |
168 | 1.68k | case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: |
169 | 1.68k | ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIMEV2>::create(); |
170 | 1.68k | break; |
171 | 386 | case FieldType::OLAP_FIELD_TYPE_DATETIME: |
172 | 386 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIME>::create(); |
173 | 386 | break; |
174 | 1.54k | case FieldType::OLAP_FIELD_TYPE_CHAR: |
175 | 1.55k | if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) { |
176 | 1.54k | ptr = doris::vectorized::ColumnDictI32::create(type); |
177 | 1.54k | } else { |
178 | 3 | ptr = doris::vectorized::PredicateColumnType<TYPE_CHAR>::create(); |
179 | 3 | } |
180 | 1.54k | break; |
181 | 0 | case FieldType::OLAP_FIELD_TYPE_VARCHAR: |
182 | 11.0k | case FieldType::OLAP_FIELD_TYPE_STRING: |
183 | 11.0k | case FieldType::OLAP_FIELD_TYPE_JSONB: |
184 | 11.0k | if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) { |
185 | 10.0k | ptr = doris::vectorized::ColumnDictI32::create(type); |
186 | 10.0k | } else { |
187 | 1.02k | ptr = doris::vectorized::PredicateColumnType<TYPE_STRING>::create(); |
188 | 1.02k | } |
189 | 11.0k | break; |
190 | 5 | case FieldType::OLAP_FIELD_TYPE_DECIMAL: |
191 | 5 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMALV2>::create(); |
192 | 5 | break; |
193 | 195 | case FieldType::OLAP_FIELD_TYPE_DECIMAL32: |
194 | 195 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL32>::create(); |
195 | 195 | break; |
196 | 380 | case FieldType::OLAP_FIELD_TYPE_DECIMAL64: |
197 | 380 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL64>::create(); |
198 | 380 | break; |
199 | 549 | case FieldType::OLAP_FIELD_TYPE_DECIMAL128I: |
200 | 549 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL128I>::create(); |
201 | 549 | break; |
202 | 195 | case FieldType::OLAP_FIELD_TYPE_DECIMAL256: |
203 | 195 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL256>::create(); |
204 | 195 | break; |
205 | 144 | case FieldType::OLAP_FIELD_TYPE_IPV4: |
206 | 144 | ptr = doris::vectorized::PredicateColumnType<TYPE_IPV4>::create(); |
207 | 144 | break; |
208 | 130 | case FieldType::OLAP_FIELD_TYPE_IPV6: |
209 | 130 | ptr = doris::vectorized::PredicateColumnType<TYPE_IPV6>::create(); |
210 | 130 | break; |
211 | 0 | default: |
212 | 0 | throw Exception( |
213 | 0 | ErrorCode::SCHEMA_SCHEMA_FIELD_INVALID, |
214 | 0 | fmt::format("Unexpected type when choosing predicate column, type={}", int(type))); |
215 | 1.36M | } |
216 | | |
217 | 1.36M | if (is_nullable) { |
218 | 23.8k | return doris::vectorized::ColumnNullable::create(std::move(ptr), |
219 | 23.8k | doris::vectorized::ColumnUInt8::create()); |
220 | 23.8k | } |
221 | 1.34M | return ptr; |
222 | 1.36M | } |
223 | | |
224 | | } // namespace doris |