/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 | 2.97k | Schema::Schema(const Schema& other) { |
44 | 2.97k | _copy_from(other); |
45 | 2.97k | } |
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 | 2.97k | void Schema::_copy_from(const Schema& other) { |
55 | 2.97k | _col_ids = other._col_ids; |
56 | 2.97k | _col_offsets = other._col_offsets; |
57 | | |
58 | 2.97k | _num_key_columns = other._num_key_columns; |
59 | 2.97k | _schema_size = other._schema_size; |
60 | | |
61 | | // Deep copy _cols |
62 | | // TODO(lingbin): really need clone? |
63 | 2.97k | _cols.resize(other._cols.size(), nullptr); |
64 | 10.2k | for (auto cid : _col_ids) { |
65 | 10.2k | _cols[cid] = other._cols[cid]->clone(); |
66 | 10.2k | } |
67 | 2.97k | } |
68 | | |
69 | | void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids, |
70 | 11.6k | size_t num_key_columns) { |
71 | 11.6k | _col_ids = col_ids; |
72 | 11.6k | _num_key_columns = num_key_columns; |
73 | | |
74 | 11.6k | _cols.resize(cols.size(), nullptr); |
75 | 11.6k | _col_offsets.resize(_cols.size(), -1); |
76 | | |
77 | 11.6k | size_t offset = 0; |
78 | 11.6k | std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end()); |
79 | 134k | for (int cid = 0; cid < cols.size(); ++cid) { |
80 | 122k | if (col_id_set.find(cid) == col_id_set.end()) { |
81 | 44.5k | continue; |
82 | 44.5k | } |
83 | 78.0k | _cols[cid] = FieldFactory::create(*cols[cid]); |
84 | | |
85 | 78.0k | _col_offsets[cid] = offset; |
86 | | // Plus 1 byte for null byte |
87 | 78.0k | offset += _cols[cid]->size() + 1; |
88 | 78.0k | } |
89 | | |
90 | 11.6k | _schema_size = offset; |
91 | 11.6k | } |
92 | | |
93 | | void Schema::_init(const std::vector<const Field*>& cols, const std::vector<ColumnId>& col_ids, |
94 | 3.41k | size_t num_key_columns) { |
95 | 3.41k | _col_ids = col_ids; |
96 | 3.41k | _num_key_columns = num_key_columns; |
97 | | |
98 | 3.41k | _cols.resize(cols.size(), nullptr); |
99 | 3.41k | _col_offsets.resize(_cols.size(), -1); |
100 | | |
101 | 3.41k | size_t offset = 0; |
102 | 3.41k | std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end()); |
103 | 16.7k | for (int cid = 0; cid < cols.size(); ++cid) { |
104 | 13.3k | 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 | 13.3k | _cols[cid] = cols[cid]->clone(); |
110 | | |
111 | 13.3k | _col_offsets[cid] = offset; |
112 | | // Plus 1 byte for null byte |
113 | 13.3k | offset += _cols[cid]->size() + 1; |
114 | 13.3k | } |
115 | | |
116 | 3.41k | _schema_size = offset; |
117 | 3.41k | } |
118 | | |
119 | 18.0k | Schema::~Schema() { |
120 | 177k | for (auto col : _cols) { |
121 | 177k | delete col; |
122 | 177k | } |
123 | 18.0k | } |
124 | | |
125 | 225k | vectorized::DataTypePtr Schema::get_data_type_ptr(const Field& field) { |
126 | 225k | return vectorized::DataTypeFactory::instance().create_data_type(field); |
127 | 225k | } |
128 | | |
129 | 13.4k | vectorized::IColumn::MutablePtr Schema::get_column_by_field(const Field& field) { |
130 | 13.4k | return get_data_type_ptr(field)->create_column(); |
131 | 13.4k | } |
132 | | |
133 | | vectorized::IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type, |
134 | | bool is_nullable, |
135 | 3.83k | const ReaderType reader_type) { |
136 | 3.83k | vectorized::IColumn::MutablePtr ptr = nullptr; |
137 | 3.83k | switch (type) { |
138 | 0 | case FieldType::OLAP_FIELD_TYPE_BOOL: |
139 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_BOOLEAN>::create(); |
140 | 0 | break; |
141 | 3.33k | case FieldType::OLAP_FIELD_TYPE_TINYINT: |
142 | 3.33k | ptr = doris::vectorized::PredicateColumnType<TYPE_TINYINT>::create(); |
143 | 3.33k | break; |
144 | 0 | case FieldType::OLAP_FIELD_TYPE_SMALLINT: |
145 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_SMALLINT>::create(); |
146 | 0 | break; |
147 | 333 | case FieldType::OLAP_FIELD_TYPE_INT: |
148 | 333 | ptr = doris::vectorized::PredicateColumnType<TYPE_INT>::create(); |
149 | 333 | 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 | 0 | case FieldType::OLAP_FIELD_TYPE_BIGINT: |
157 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_BIGINT>::create(); |
158 | 0 | break; |
159 | 0 | case FieldType::OLAP_FIELD_TYPE_LARGEINT: |
160 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_LARGEINT>::create(); |
161 | 0 | break; |
162 | 0 | case FieldType::OLAP_FIELD_TYPE_DATE: |
163 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATE>::create(); |
164 | 0 | break; |
165 | 0 | case FieldType::OLAP_FIELD_TYPE_DATEV2: |
166 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATEV2>::create(); |
167 | 0 | break; |
168 | 0 | case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: |
169 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIMEV2>::create(); |
170 | 0 | break; |
171 | 0 | case FieldType::OLAP_FIELD_TYPE_DATETIME: |
172 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIME>::create(); |
173 | 0 | break; |
174 | 0 | case FieldType::OLAP_FIELD_TYPE_CHAR: |
175 | 0 | if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) { |
176 | 0 | ptr = doris::vectorized::ColumnDictI32::create(type); |
177 | 0 | } else { |
178 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_CHAR>::create(); |
179 | 0 | } |
180 | 0 | break; |
181 | 0 | case FieldType::OLAP_FIELD_TYPE_VARCHAR: |
182 | 166 | case FieldType::OLAP_FIELD_TYPE_STRING: |
183 | 166 | case FieldType::OLAP_FIELD_TYPE_JSONB: |
184 | 166 | if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) { |
185 | 26 | ptr = doris::vectorized::ColumnDictI32::create(type); |
186 | 140 | } else { |
187 | 140 | ptr = doris::vectorized::PredicateColumnType<TYPE_STRING>::create(); |
188 | 140 | } |
189 | 166 | break; |
190 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL: |
191 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMALV2>::create(); |
192 | 0 | break; |
193 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL32: |
194 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL32>::create(); |
195 | 0 | break; |
196 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL64: |
197 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL64>::create(); |
198 | 0 | break; |
199 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL128I: |
200 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL128I>::create(); |
201 | 0 | break; |
202 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL256: |
203 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL256>::create(); |
204 | 0 | break; |
205 | 0 | case FieldType::OLAP_FIELD_TYPE_IPV4: |
206 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_IPV4>::create(); |
207 | 0 | break; |
208 | 0 | case FieldType::OLAP_FIELD_TYPE_IPV6: |
209 | 0 | ptr = doris::vectorized::PredicateColumnType<TYPE_IPV6>::create(); |
210 | 0 | 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 | 3.83k | } |
216 | | |
217 | 3.83k | if (is_nullable) { |
218 | 166 | return doris::vectorized::ColumnNullable::create(std::move(ptr), |
219 | 166 | doris::vectorized::ColumnUInt8::create()); |
220 | 166 | } |
221 | 3.66k | return ptr; |
222 | 3.83k | } |
223 | | |
224 | | } // namespace doris |