/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 | 5.29M | Schema::Schema(const Schema& other) { |
45 | 5.29M | _copy_from(other); |
46 | 5.29M | } |
47 | | |
48 | 0 | Schema& Schema::operator=(const Schema& other) { |
49 | 0 | if (this != &other) { |
50 | 0 | _copy_from(other); |
51 | 0 | } |
52 | 0 | return *this; |
53 | 0 | } |
54 | | |
55 | 5.30M | void Schema::_copy_from(const Schema& other) { |
56 | 5.30M | _col_ids = other._col_ids; |
57 | 5.30M | _col_offsets = other._col_offsets; |
58 | | |
59 | 5.30M | _num_key_columns = other._num_key_columns; |
60 | 5.30M | _schema_size = other._schema_size; |
61 | | |
62 | | // Deep copy _cols |
63 | | // TODO(lingbin): really need clone? |
64 | 5.30M | _cols.resize(other._cols.size(), nullptr); |
65 | 8.50M | for (auto cid : _col_ids) { |
66 | 8.50M | _cols[cid] = other._cols[cid]->clone(); |
67 | 8.50M | } |
68 | 5.30M | } |
69 | | |
70 | | void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids, |
71 | 9.59M | size_t num_key_columns) { |
72 | 9.59M | _col_ids = col_ids; |
73 | 9.59M | _num_key_columns = num_key_columns; |
74 | | |
75 | 9.59M | _cols.resize(cols.size(), nullptr); |
76 | 9.59M | _col_offsets.resize(_cols.size(), -1); |
77 | | |
78 | 9.59M | size_t offset = 0; |
79 | 9.59M | std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end()); |
80 | 149M | for (int cid = 0; cid < cols.size(); ++cid) { |
81 | 140M | if (col_id_set.find(cid) == col_id_set.end()) { |
82 | 72.8M | continue; |
83 | 72.8M | } |
84 | 67.4M | _cols[cid] = FieldFactory::create(*cols[cid]); |
85 | | |
86 | 67.4M | _col_offsets[cid] = offset; |
87 | | // Plus 1 byte for null byte |
88 | 67.4M | offset += _cols[cid]->size() + 1; |
89 | 67.4M | } |
90 | | |
91 | 9.59M | _schema_size = offset; |
92 | 9.59M | } |
93 | | |
94 | | void Schema::_init(const std::vector<const Field*>& cols, const std::vector<ColumnId>& col_ids, |
95 | 1.95M | size_t num_key_columns) { |
96 | 1.95M | _col_ids = col_ids; |
97 | 1.95M | _num_key_columns = num_key_columns; |
98 | | |
99 | 1.95M | _cols.resize(cols.size(), nullptr); |
100 | 1.95M | _col_offsets.resize(_cols.size(), -1); |
101 | | |
102 | 1.95M | size_t offset = 0; |
103 | 1.95M | std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end()); |
104 | 9.12M | for (int cid = 0; cid < cols.size(); ++cid) { |
105 | 7.17M | if (col_id_set.find(cid) == col_id_set.end()) { |
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 | 7.17M | _cols[cid] = cols[cid]->clone(); |
111 | | |
112 | 7.17M | _col_offsets[cid] = offset; |
113 | | // Plus 1 byte for null byte |
114 | 7.17M | offset += _cols[cid]->size() + 1; |
115 | 7.17M | } |
116 | | |
117 | 1.95M | _schema_size = offset; |
118 | 1.95M | } |
119 | | |
120 | 16.8M | Schema::~Schema() { |
121 | 208M | for (auto col : _cols) { |
122 | 208M | delete col; |
123 | 208M | } |
124 | 16.8M | } |
125 | | |
126 | 77.5M | vectorized::DataTypePtr Schema::get_data_type_ptr(const Field& field) { |
127 | 77.5M | return vectorized::DataTypeFactory::instance().create_data_type(field); |
128 | 77.5M | } |
129 | | |
130 | 7.35M | vectorized::IColumn::MutablePtr Schema::get_column_by_field(const Field& field) { |
131 | 7.35M | return get_data_type_ptr(field)->create_column(); |
132 | 7.35M | } |
133 | | |
134 | | vectorized::IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type, |
135 | | bool is_nullable, |
136 | 1.83M | const ReaderType reader_type) { |
137 | 1.83M | vectorized::IColumn::MutablePtr ptr = nullptr; |
138 | 1.83M | switch (type) { |
139 | 300 | case FieldType::OLAP_FIELD_TYPE_BOOL: |
140 | 300 | ptr = doris::vectorized::PredicateColumnType<TYPE_BOOLEAN>::create(); |
141 | 300 | break; |
142 | 1.78M | case FieldType::OLAP_FIELD_TYPE_TINYINT: |
143 | 1.78M | ptr = doris::vectorized::PredicateColumnType<TYPE_TINYINT>::create(); |
144 | 1.78M | break; |
145 | 778 | case FieldType::OLAP_FIELD_TYPE_SMALLINT: |
146 | 778 | ptr = doris::vectorized::PredicateColumnType<TYPE_SMALLINT>::create(); |
147 | 778 | break; |
148 | 16.5k | case FieldType::OLAP_FIELD_TYPE_INT: |
149 | 16.5k | ptr = doris::vectorized::PredicateColumnType<TYPE_INT>::create(); |
150 | 16.5k | break; |
151 | 0 | case FieldType::OLAP_FIELD_TYPE_FLOAT: |
152 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_FLOAT>::create(); |
153 | 0 | break; |
154 | 0 | case FieldType::OLAP_FIELD_TYPE_DOUBLE: |
155 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DOUBLE>::create(); |
156 | 0 | break; |
157 | 4.08k | case FieldType::OLAP_FIELD_TYPE_BIGINT: |
158 | 4.08k | ptr = doris::vectorized::PredicateColumnType<TYPE_BIGINT>::create(); |
159 | 4.08k | break; |
160 | 586 | case FieldType::OLAP_FIELD_TYPE_LARGEINT: |
161 | 586 | ptr = doris::vectorized::PredicateColumnType<TYPE_LARGEINT>::create(); |
162 | 586 | break; |
163 | 352 | case FieldType::OLAP_FIELD_TYPE_DATE: |
164 | 352 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATE>::create(); |
165 | 352 | break; |
166 | 5.02k | case FieldType::OLAP_FIELD_TYPE_DATEV2: |
167 | 5.02k | ptr = doris::vectorized::PredicateColumnType<TYPE_DATEV2>::create(); |
168 | 5.02k | break; |
169 | 1.81k | case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: |
170 | 1.81k | ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIMEV2>::create(); |
171 | 1.81k | break; |
172 | 216 | case FieldType::OLAP_FIELD_TYPE_DATETIME: |
173 | 216 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIME>::create(); |
174 | 216 | break; |
175 | 1.74k | case FieldType::OLAP_FIELD_TYPE_CHAR: |
176 | 1.74k | if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) { |
177 | 1.74k | ptr = doris::vectorized::ColumnDictionary<doris::vectorized::Int32>::create(type); |
178 | 1.74k | } else { |
179 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_CHAR>::create(); |
180 | 0 | } |
181 | 1.74k | break; |
182 | 0 | case FieldType::OLAP_FIELD_TYPE_VARCHAR: |
183 | 11.0k | case FieldType::OLAP_FIELD_TYPE_STRING: |
184 | 11.1k | case FieldType::OLAP_FIELD_TYPE_JSONB: |
185 | 11.1k | if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) { |
186 | 10.1k | ptr = doris::vectorized::ColumnDictionary<doris::vectorized::Int32>::create(type); |
187 | 10.1k | } else { |
188 | 998 | ptr = doris::vectorized::PredicateColumnType<TYPE_STRING>::create(); |
189 | 998 | } |
190 | 11.1k | break; |
191 | 1 | case FieldType::OLAP_FIELD_TYPE_DECIMAL: |
192 | 1 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMALV2>::create(); |
193 | 1 | break; |
194 | 209 | case FieldType::OLAP_FIELD_TYPE_DECIMAL32: |
195 | 209 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL32>::create(); |
196 | 209 | break; |
197 | 322 | case FieldType::OLAP_FIELD_TYPE_DECIMAL64: |
198 | 322 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL64>::create(); |
199 | 322 | break; |
200 | 585 | case FieldType::OLAP_FIELD_TYPE_DECIMAL128I: |
201 | 585 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL128I>::create(); |
202 | 585 | break; |
203 | 195 | case FieldType::OLAP_FIELD_TYPE_DECIMAL256: |
204 | 195 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL256>::create(); |
205 | 195 | break; |
206 | 142 | case FieldType::OLAP_FIELD_TYPE_IPV4: |
207 | 142 | ptr = doris::vectorized::PredicateColumnType<TYPE_IPV4>::create(); |
208 | 142 | break; |
209 | 130 | case FieldType::OLAP_FIELD_TYPE_IPV6: |
210 | 130 | ptr = doris::vectorized::PredicateColumnType<TYPE_IPV6>::create(); |
211 | 130 | break; |
212 | 0 | default: |
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 | 1.83M | } |
217 | | |
218 | 1.83M | if (is_nullable) { |
219 | 23.7k | return doris::vectorized::ColumnNullable::create(std::move(ptr), |
220 | 23.7k | doris::vectorized::ColumnUInt8::create()); |
221 | 23.7k | } |
222 | 1.81M | return ptr; |
223 | 1.83M | } |
224 | | |
225 | | } // namespace doris |