be/src/storage/segment/column_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 "storage/segment/column_reader.h" |
19 | | |
20 | | #include <assert.h> |
21 | | #include <gen_cpp/Descriptors_types.h> |
22 | | #include <gen_cpp/segment_v2.pb.h> |
23 | | #include <glog/logging.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <memory> |
27 | | #include <ostream> |
28 | | #include <set> |
29 | | #include <utility> |
30 | | |
31 | | #include "common/compiler_util.h" // IWYU pragma: keep |
32 | | #include "common/status.h" |
33 | | #include "core/assert_cast.h" |
34 | | #include "core/binary_cast.hpp" |
35 | | #include "core/column/column.h" |
36 | | #include "core/column/column_array.h" |
37 | | #include "core/column/column_map.h" |
38 | | #include "core/column/column_nullable.h" |
39 | | #include "core/column/column_struct.h" |
40 | | #include "core/column/column_vector.h" |
41 | | #include "core/data_type/data_type_agg_state.h" |
42 | | #include "core/data_type/data_type_factory.hpp" |
43 | | #include "core/data_type/data_type_nullable.h" |
44 | | #include "core/data_type/define_primitive_type.h" |
45 | | #include "core/decimal12.h" |
46 | | #include "core/string_ref.h" |
47 | | #include "core/types.h" |
48 | | #include "core/value/decimalv2_value.h" |
49 | | #include "core/value/vdatetime_value.h" //for VecDateTime |
50 | | #include "io/fs/file_reader.h" |
51 | | #include "storage/index/ann/ann_index_reader.h" |
52 | | #include "storage/index/bloom_filter/bloom_filter.h" |
53 | | #include "storage/index/bloom_filter/bloom_filter_index_reader.h" |
54 | | #include "storage/index/index_file_reader.h" |
55 | | #include "storage/index/index_reader.h" |
56 | | #include "storage/index/inverted/analyzer/analyzer.h" |
57 | | #include "storage/index/inverted/inverted_index_reader.h" |
58 | | #include "storage/index/zone_map/zone_map_index.h" |
59 | | #include "storage/iterators.h" |
60 | | #include "storage/olap_common.h" |
61 | | #include "storage/predicate/block_column_predicate.h" |
62 | | #include "storage/predicate/column_predicate.h" |
63 | | #include "storage/segment/binary_dict_page.h" // for BinaryDictPageDecoder |
64 | | #include "storage/segment/binary_plain_page.h" |
65 | | #include "storage/segment/column_meta_accessor.h" |
66 | | #include "storage/segment/encoding_info.h" // for EncodingInfo |
67 | | #include "storage/segment/page_decoder.h" |
68 | | #include "storage/segment/page_handle.h" // for PageHandle |
69 | | #include "storage/segment/page_io.h" |
70 | | #include "storage/segment/page_pointer.h" // for PagePointer |
71 | | #include "storage/segment/row_ranges.h" |
72 | | #include "storage/segment/segment.h" |
73 | | #include "storage/segment/segment_prefetcher.h" |
74 | | #include "storage/segment/variant/variant_column_reader.h" |
75 | | #include "storage/tablet/tablet_schema.h" |
76 | | #include "storage/types.h" // for TypeInfo |
77 | | #include "util/bitmap.h" |
78 | | #include "util/block_compression.h" |
79 | | #include "util/concurrency_stats.h" |
80 | | #include "util/defer_op.h" |
81 | | #include "util/rle_encoding.h" // for RleDecoder |
82 | | #include "util/slice.h" |
83 | | |
84 | | namespace doris::segment_v2 { |
85 | | #include "storage/segment/column_reader.h" |
86 | | |
87 | 638 | inline bool read_as_string(PrimitiveType type) { |
88 | 638 | return type == PrimitiveType::TYPE_STRING || type == PrimitiveType::INVALID_TYPE || |
89 | 638 | type == PrimitiveType::TYPE_BITMAP || type == PrimitiveType::TYPE_FIXED_LENGTH_OBJECT; |
90 | 638 | } |
91 | | |
92 | | Status ColumnReader::create_array(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
93 | | const io::FileReaderSPtr& file_reader, |
94 | 50.0k | std::shared_ptr<ColumnReader>* reader) { |
95 | 50.0k | DCHECK(meta.children_columns_size() == 2 || meta.children_columns_size() == 3); |
96 | | |
97 | 50.0k | std::shared_ptr<ColumnReader> item_reader; |
98 | 50.0k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0), |
99 | 50.0k | meta.children_columns(0).num_rows(), file_reader, |
100 | 50.0k | &item_reader)); |
101 | | |
102 | 50.0k | std::shared_ptr<ColumnReader> offset_reader; |
103 | 50.0k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1), |
104 | 50.0k | meta.children_columns(1).num_rows(), file_reader, |
105 | 50.0k | &offset_reader)); |
106 | | |
107 | 50.0k | std::shared_ptr<ColumnReader> null_reader; |
108 | 50.0k | if (meta.is_nullable()) { |
109 | 34.9k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2), |
110 | 34.9k | meta.children_columns(2).num_rows(), file_reader, |
111 | 34.9k | &null_reader)); |
112 | 34.9k | } |
113 | | |
114 | | // The num rows of the array reader equals to the num rows of the length reader. |
115 | 50.0k | uint64_t array_num_rows = meta.children_columns(1).num_rows(); |
116 | 50.0k | std::shared_ptr<ColumnReader> array_reader( |
117 | 50.0k | new ColumnReader(opts, meta, array_num_rows, file_reader)); |
118 | | // array reader do not need to init |
119 | 50.0k | array_reader->_sub_readers.resize(meta.children_columns_size()); |
120 | 50.0k | array_reader->_sub_readers[0] = std::move(item_reader); |
121 | 50.0k | array_reader->_sub_readers[1] = std::move(offset_reader); |
122 | 50.0k | if (meta.is_nullable()) { |
123 | 34.7k | array_reader->_sub_readers[2] = std::move(null_reader); |
124 | 34.7k | } |
125 | 50.0k | array_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_ARRAY; |
126 | 50.0k | *reader = std::move(array_reader); |
127 | 50.0k | return Status::OK(); |
128 | 50.0k | } |
129 | | |
130 | | Status ColumnReader::create_map(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
131 | | const io::FileReaderSPtr& file_reader, |
132 | 29.3k | std::shared_ptr<ColumnReader>* reader) { |
133 | | // map reader now has 3 sub readers for key, value, offsets(scalar), null(scala) |
134 | 29.3k | DCHECK(meta.children_columns_size() == 3 || meta.children_columns_size() == 4); |
135 | 29.3k | std::shared_ptr<ColumnReader> key_reader; |
136 | 29.3k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0), |
137 | 29.3k | meta.children_columns(0).num_rows(), file_reader, |
138 | 29.3k | &key_reader)); |
139 | 29.3k | std::shared_ptr<ColumnReader> val_reader; |
140 | 29.3k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1), |
141 | 29.3k | meta.children_columns(1).num_rows(), file_reader, |
142 | 29.3k | &val_reader)); |
143 | 29.3k | std::shared_ptr<ColumnReader> offset_reader; |
144 | 29.3k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2), |
145 | 29.3k | meta.children_columns(2).num_rows(), file_reader, |
146 | 29.3k | &offset_reader)); |
147 | 29.3k | std::shared_ptr<ColumnReader> null_reader; |
148 | 29.3k | if (meta.is_nullable()) { |
149 | 12.1k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(3), |
150 | 12.1k | meta.children_columns(3).num_rows(), file_reader, |
151 | 12.1k | &null_reader)); |
152 | 12.1k | } |
153 | | |
154 | | // The num rows of the map reader equals to the num rows of the length reader. |
155 | 29.3k | uint64_t map_num_rows = meta.children_columns(2).num_rows(); |
156 | 29.3k | std::shared_ptr<ColumnReader> map_reader( |
157 | 29.3k | new ColumnReader(opts, meta, map_num_rows, file_reader)); |
158 | 29.3k | map_reader->_sub_readers.resize(meta.children_columns_size()); |
159 | | |
160 | 29.3k | map_reader->_sub_readers[0] = std::move(key_reader); |
161 | 29.3k | map_reader->_sub_readers[1] = std::move(val_reader); |
162 | 29.3k | map_reader->_sub_readers[2] = std::move(offset_reader); |
163 | 29.3k | if (meta.is_nullable()) { |
164 | 12.0k | map_reader->_sub_readers[3] = std::move(null_reader); |
165 | 12.0k | } |
166 | 29.3k | map_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_MAP; |
167 | 29.3k | *reader = std::move(map_reader); |
168 | 29.3k | return Status::OK(); |
169 | 29.3k | } |
170 | | |
171 | | Status ColumnReader::create_struct(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
172 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
173 | 2.77k | std::shared_ptr<ColumnReader>* reader) { |
174 | | // not support empty struct |
175 | 2.77k | DCHECK(meta.children_columns_size() >= 1); |
176 | | // create struct column reader |
177 | 2.77k | std::shared_ptr<ColumnReader> struct_reader( |
178 | 2.77k | new ColumnReader(opts, meta, num_rows, file_reader)); |
179 | 2.77k | struct_reader->_sub_readers.reserve(meta.children_columns_size()); |
180 | | // now we support struct column can add the children columns according to the schema-change behavior |
181 | 14.7k | for (int i = 0; i < meta.children_columns_size(); i++) { |
182 | 11.9k | std::shared_ptr<ColumnReader> sub_reader; |
183 | 11.9k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(i), |
184 | 11.9k | meta.children_columns(i).num_rows(), file_reader, |
185 | 11.9k | &sub_reader)); |
186 | 11.9k | struct_reader->_sub_readers.push_back(std::move(sub_reader)); |
187 | 11.9k | } |
188 | 2.77k | struct_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_STRUCT; |
189 | 2.77k | *reader = std::move(struct_reader); |
190 | 2.77k | return Status::OK(); |
191 | 2.77k | } |
192 | | |
193 | | Status ColumnReader::create_agg_state(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
194 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
195 | 640 | std::shared_ptr<ColumnReader>* reader) { |
196 | 640 | if (!meta.has_function_name()) { // meet old version ColumnMetaPB |
197 | 0 | std::shared_ptr<ColumnReader> reader_local( |
198 | 0 | new ColumnReader(opts, meta, num_rows, file_reader)); |
199 | 0 | RETURN_IF_ERROR(reader_local->init(&meta)); |
200 | 0 | *reader = std::move(reader_local); |
201 | 0 | return Status::OK(); |
202 | 0 | } |
203 | | |
204 | 640 | auto data_type = DataTypeFactory::instance().create_data_type(meta); |
205 | 640 | const auto* agg_state_type = assert_cast<const DataTypeAggState*>(data_type.get()); |
206 | 640 | agg_state_type->check_function_compatibility(opts.be_exec_version); |
207 | 640 | auto type = agg_state_type->get_serialized_type()->get_primitive_type(); |
208 | | |
209 | 640 | if (read_as_string(type)) { |
210 | 590 | std::shared_ptr<ColumnReader> reader_local( |
211 | 590 | new ColumnReader(opts, meta, num_rows, file_reader)); |
212 | 590 | RETURN_IF_ERROR(reader_local->init(&meta)); |
213 | 590 | *reader = std::move(reader_local); |
214 | 590 | return Status::OK(); |
215 | 590 | } else if (type == PrimitiveType::TYPE_MAP) { |
216 | 36 | return create_map(opts, meta, file_reader, reader); |
217 | 36 | } else if (type == PrimitiveType::TYPE_ARRAY) { |
218 | 10 | return create_array(opts, meta, file_reader, reader); |
219 | 10 | } else if (type == PrimitiveType::TYPE_STRUCT) { |
220 | 0 | return create_struct(opts, meta, num_rows, file_reader, reader); |
221 | 0 | } |
222 | | |
223 | 4 | return Status::InternalError("Not supported type: {}, serialized type: {}", |
224 | 4 | agg_state_type->get_name(), int(type)); |
225 | 640 | } |
226 | | |
227 | 107k | bool ColumnReader::is_compaction_reader_type(ReaderType type) { |
228 | 107k | return type == ReaderType::READER_BASE_COMPACTION || |
229 | 107k | type == ReaderType::READER_CUMULATIVE_COMPACTION || |
230 | 107k | type == ReaderType::READER_COLD_DATA_COMPACTION || |
231 | 107k | type == ReaderType::READER_SEGMENT_COMPACTION || |
232 | 107k | type == ReaderType::READER_FULL_COMPACTION; |
233 | 107k | } |
234 | | |
235 | | Status ColumnReader::create(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
236 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
237 | 2.00M | std::shared_ptr<ColumnReader>* reader) { |
238 | 2.00M | if (is_scalar_type((FieldType)meta.type())) { |
239 | 1.91M | std::shared_ptr<ColumnReader> reader_local( |
240 | 1.91M | new ColumnReader(opts, meta, num_rows, file_reader)); |
241 | 1.91M | RETURN_IF_ERROR(reader_local->init(&meta)); |
242 | 1.91M | *reader = std::move(reader_local); |
243 | 1.91M | return Status::OK(); |
244 | 1.91M | } else { |
245 | 89.3k | auto type = (FieldType)meta.type(); |
246 | 89.3k | switch (type) { |
247 | 641 | case FieldType::OLAP_FIELD_TYPE_AGG_STATE: { |
248 | 641 | return create_agg_state(opts, meta, num_rows, file_reader, reader); |
249 | 0 | } |
250 | 2.77k | case FieldType::OLAP_FIELD_TYPE_STRUCT: { |
251 | 2.77k | return create_struct(opts, meta, num_rows, file_reader, reader); |
252 | 0 | } |
253 | 50.2k | case FieldType::OLAP_FIELD_TYPE_ARRAY: { |
254 | 50.2k | return create_array(opts, meta, file_reader, reader); |
255 | 0 | } |
256 | 29.2k | case FieldType::OLAP_FIELD_TYPE_MAP: { |
257 | 29.2k | return create_map(opts, meta, file_reader, reader); |
258 | 0 | } |
259 | 6.79k | case FieldType::OLAP_FIELD_TYPE_VARIANT: { |
260 | | // Read variant only root data using a single ColumnReader |
261 | 6.79k | std::shared_ptr<ColumnReader> reader_local( |
262 | 6.79k | new ColumnReader(opts, meta, num_rows, file_reader)); |
263 | 6.79k | RETURN_IF_ERROR(reader_local->init(&meta)); |
264 | 6.79k | *reader = std::move(reader_local); |
265 | 6.79k | return Status::OK(); |
266 | 6.79k | } |
267 | 0 | default: |
268 | 0 | return Status::NotSupported("unsupported type for ColumnReader: {}", |
269 | 0 | std::to_string(int(type))); |
270 | 89.3k | } |
271 | 89.3k | } |
272 | 2.00M | } |
273 | | |
274 | 6.84k | ColumnReader::ColumnReader() = default; |
275 | | |
276 | | ColumnReader::ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
277 | | uint64_t num_rows, io::FileReaderSPtr file_reader) |
278 | 2.00M | : _use_index_page_cache(!config::disable_storage_page_cache), |
279 | 2.00M | _opts(opts), |
280 | 2.00M | _num_rows(num_rows), |
281 | 2.00M | _file_reader(std::move(file_reader)), |
282 | 2.00M | _dict_encoding_type(UNKNOWN_DICT_ENCODING) { |
283 | 2.00M | _meta_length = meta.length(); |
284 | 2.00M | _meta_type = (FieldType)meta.type(); |
285 | 2.00M | if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) { |
286 | 50.3k | _meta_children_column_type = (FieldType)meta.children_columns(0).type(); |
287 | 50.3k | } |
288 | 2.00M | _data_type = DataTypeFactory::instance().create_data_type(meta); |
289 | 2.00M | _meta_is_nullable = meta.is_nullable(); |
290 | 2.00M | _meta_dict_page = meta.dict_page(); |
291 | 2.00M | _meta_compression = meta.compression(); |
292 | 2.00M | } |
293 | | |
294 | 1.81M | ColumnReader::~ColumnReader() = default; |
295 | | |
296 | 1.92M | int64_t ColumnReader::get_metadata_size() const { |
297 | 1.92M | return sizeof(ColumnReader) + (_segment_zone_map ? _segment_zone_map->ByteSizeLong() : 0); |
298 | 1.92M | } |
299 | | |
300 | | #ifdef BE_TEST |
301 | | /// This function is only used in UT to verify the correctness of data read from zone map |
302 | | /// See UT case 'SegCompactionMoWTest.SegCompactionInterleaveWithBig_ooooOOoOooooooooO' |
303 | | /// be/test/olap/segcompaction_mow_test.cpp |
304 | | void ColumnReader::check_data_by_zone_map_for_test(const MutableColumnPtr& dst) const { |
305 | | if (!_segment_zone_map) { |
306 | | return; |
307 | | } |
308 | | |
309 | | const auto rows = dst->size(); |
310 | | if (rows == 0) { |
311 | | return; |
312 | | } |
313 | | |
314 | | FieldType type = _type; |
315 | | |
316 | | if (type != FieldType::OLAP_FIELD_TYPE_INT) { |
317 | | return; |
318 | | } |
319 | | |
320 | | auto* non_nullable_column = |
321 | | is_column_nullable(*dst) |
322 | | ? assert_cast<ColumnNullable*>(dst.get())->get_nested_column_ptr().get() |
323 | | : dst.get(); |
324 | | |
325 | | /// Only verify when the destination column carries Field-accessible TYPE_INT data. |
326 | | if (check_and_get_column<ColumnVector<TYPE_INT>>(non_nullable_column) == nullptr) { |
327 | | return; |
328 | | } |
329 | | |
330 | | ZoneMap zone_map; |
331 | | THROW_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
332 | | |
333 | | if (zone_map.has_null) { |
334 | | return; |
335 | | } |
336 | | |
337 | | for (size_t i = 0; i != rows; ++i) { |
338 | | Field field; |
339 | | dst->get(i, field); |
340 | | DCHECK(!field.is_null()); |
341 | | const auto v = field.get<TYPE_INT>(); |
342 | | DCHECK_GE(v, zone_map.min_value.get<TYPE_INT>()); |
343 | | DCHECK_LE(v, zone_map.max_value.get<TYPE_INT>()); |
344 | | } |
345 | | } |
346 | | #endif |
347 | | |
348 | 1.92M | Status ColumnReader::init(const ColumnMetaPB* meta) { |
349 | 1.92M | _type = (FieldType)meta->type(); |
350 | | |
351 | 1.92M | if (meta->has_be_exec_version()) { |
352 | 1.75M | _be_exec_version = meta->be_exec_version(); |
353 | 1.75M | } |
354 | | |
355 | 1.92M | if (_type == FieldType::OLAP_FIELD_TYPE_NONE || _type == FieldType::OLAP_FIELD_TYPE_UNKNOWN) { |
356 | 0 | return Status::NotSupported("unsupported typeinfo, type={}", meta->type()); |
357 | 0 | } |
358 | 1.92M | RETURN_IF_ERROR(EncodingInfo::get(_type, meta->encoding(), &_encoding_info)); |
359 | | |
360 | 5.43M | for (int i = 0; i < meta->indexes_size(); i++) { |
361 | 3.51M | const auto& index_meta = meta->indexes(i); |
362 | 3.51M | switch (index_meta.type()) { |
363 | 0 | case BITMAP_INDEX: |
364 | 0 | break; |
365 | 1.88M | case ORDINAL_INDEX: |
366 | 1.88M | _ordinal_index.reset( |
367 | 1.88M | new OrdinalIndexReader(_file_reader, _num_rows, index_meta.ordinal_index())); |
368 | 1.88M | break; |
369 | 1.62M | case ZONE_MAP_INDEX: |
370 | 1.62M | _segment_zone_map = |
371 | 1.62M | std::make_unique<ZoneMapPB>(index_meta.zone_map_index().segment_zone_map()); |
372 | 1.62M | _zone_map_index.reset(new ZoneMapIndexReader( |
373 | 1.62M | _file_reader, index_meta.zone_map_index().page_zone_maps())); |
374 | 1.62M | break; |
375 | 4.93k | case BLOOM_FILTER_INDEX: |
376 | 4.93k | _bloom_filter_index.reset( |
377 | 4.93k | new BloomFilterIndexReader(_file_reader, index_meta.bloom_filter_index())); |
378 | 4.93k | break; |
379 | 0 | case NESTED_OFFSETS_INDEX: |
380 | 0 | break; |
381 | 0 | default: |
382 | 0 | return Status::Corruption("Bad file {}: invalid column index type {}", |
383 | 0 | _file_reader->path().native(), index_meta.type()); |
384 | 3.51M | } |
385 | 3.51M | } |
386 | 1.92M | update_metadata_size(); |
387 | | |
388 | | // ArrayColumnWriter writes a single empty array and flushes. In this scenario, |
389 | | // the item writer doesn't write any data and the corresponding ordinal index is empty. |
390 | 1.92M | if (_ordinal_index == nullptr && !is_empty()) { |
391 | 0 | return Status::Corruption("Bad file {}: missing ordinal index for column {}", |
392 | 0 | _file_reader->path().native(), meta->column_id()); |
393 | 0 | } |
394 | | |
395 | 1.92M | return Status::OK(); |
396 | 1.92M | } |
397 | | |
398 | | Status ColumnReader::new_index_iterator(const std::shared_ptr<IndexFileReader>& index_file_reader, |
399 | | const TabletIndex* index_meta, const std::string& rowset_id, |
400 | | uint32_t segment_id, size_t rows_of_segment, |
401 | 73.6k | std::unique_ptr<IndexIterator>* iterator) { |
402 | 73.6k | RETURN_IF_ERROR( |
403 | 73.6k | _load_index(index_file_reader, index_meta, rowset_id, segment_id, rows_of_segment)); |
404 | 73.6k | { |
405 | 73.6k | std::shared_lock<std::shared_mutex> rlock(_load_index_lock); |
406 | 73.6k | auto iter = _index_readers.find(index_meta->index_id()); |
407 | 74.0k | if (iter != _index_readers.end()) { |
408 | 74.1k | if (iter->second != nullptr) { |
409 | 74.1k | RETURN_IF_ERROR(iter->second->new_iterator(iterator)); |
410 | 74.1k | } |
411 | 74.0k | } |
412 | 73.6k | } |
413 | 73.6k | return Status::OK(); |
414 | 73.6k | } |
415 | | |
416 | | Status ColumnReader::read_page(const ColumnIteratorOptions& iter_opts, const PagePointer& pp, |
417 | | PageHandle* handle, Slice* page_body, PageFooterPB* footer, |
418 | 2.05M | BlockCompressionCodec* codec) const { |
419 | 2.05M | SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().column_reader_read_page); |
420 | 2.05M | iter_opts.sanity_check(); |
421 | 2.05M | PageReadOptions opts(iter_opts.io_ctx); |
422 | 2.05M | opts.verify_checksum = _opts.verify_checksum; |
423 | 2.05M | opts.use_page_cache = iter_opts.use_page_cache; |
424 | 2.05M | opts.kept_in_memory = _opts.kept_in_memory; |
425 | 2.05M | opts.type = iter_opts.type; |
426 | 2.05M | opts.file_reader = iter_opts.file_reader; |
427 | 2.05M | opts.page_pointer = pp; |
428 | 2.05M | opts.codec = codec; |
429 | 2.05M | opts.stats = iter_opts.stats; |
430 | 2.05M | opts.encoding_info = _encoding_info; |
431 | | |
432 | 2.05M | return PageIO::read_and_decompress_page(opts, handle, page_body, footer); |
433 | 2.05M | } |
434 | | |
435 | | Status ColumnReader::get_row_ranges_by_zone_map( |
436 | | const AndBlockColumnPredicate* col_predicates, |
437 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
438 | 130k | RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts) { |
439 | 130k | std::vector<uint32_t> page_indexes; |
440 | 130k | RETURN_IF_ERROR( |
441 | 130k | _get_filtered_pages(col_predicates, delete_predicates, &page_indexes, iter_opts)); |
442 | 130k | RETURN_IF_ERROR(_calculate_row_ranges(page_indexes, row_ranges, iter_opts)); |
443 | 130k | return Status::OK(); |
444 | 130k | } |
445 | | |
446 | 20.0k | Status ColumnReader::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const { |
447 | 20.0k | if (_segment_zone_map == nullptr) { |
448 | 0 | return Status::InternalError("segment zonemap not exist"); |
449 | 0 | } |
450 | | // TODO: this work to get min/max value seems should only do once |
451 | 20.0k | ZoneMap zone_map; |
452 | 20.0k | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
453 | | |
454 | 20.0k | dst->reserve(*n); |
455 | 20.0k | if (!zone_map.has_not_null) { |
456 | 1.20k | assert_cast<ColumnNullable&>(*dst).insert_many_defaults(*n); |
457 | 1.20k | return Status::OK(); |
458 | 1.20k | } |
459 | 18.8k | dst->insert(zone_map.max_value); |
460 | 37.6k | for (int i = 1; i < *n; ++i) { |
461 | 18.8k | dst->insert(zone_map.min_value); |
462 | 18.8k | } |
463 | 18.8k | return Status::OK(); |
464 | 20.0k | } |
465 | | |
466 | | Status ColumnReader::match_condition(const AndBlockColumnPredicate* col_predicates, |
467 | 1.92M | bool* matched) const { |
468 | 1.92M | *matched = true; |
469 | 1.92M | if (_zone_map_index == nullptr) { |
470 | 0 | return Status::OK(); |
471 | 0 | } |
472 | 1.92M | ZoneMap zone_map; |
473 | 1.92M | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
474 | | |
475 | 1.92M | *matched = _zone_map_match_condition(zone_map, col_predicates); |
476 | 1.92M | return Status::OK(); |
477 | 1.92M | } |
478 | | |
479 | | Status ColumnReader::prune_predicates_by_zone_map( |
480 | | std::vector<std::shared_ptr<ColumnPredicate>>& predicates, const int column_id, |
481 | 26.1M | bool* pruned) const { |
482 | 26.1M | *pruned = false; |
483 | 26.1M | if (_zone_map_index == nullptr) { |
484 | 29.6k | return Status::OK(); |
485 | 29.6k | } |
486 | | |
487 | 26.1M | ZoneMap zone_map; |
488 | 26.1M | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
489 | 26.1M | if (zone_map.pass_all) { |
490 | 0 | return Status::OK(); |
491 | 0 | } |
492 | | |
493 | 52.6M | for (auto it = predicates.begin(); it != predicates.end();) { |
494 | 26.5M | auto predicate = *it; |
495 | 26.5M | if (predicate->column_id() == column_id && predicate->is_always_true(zone_map)) { |
496 | 12.3k | *pruned = true; |
497 | 12.3k | it = predicates.erase(it); |
498 | 26.4M | } else { |
499 | 26.4M | ++it; |
500 | 26.4M | } |
501 | 26.5M | } |
502 | 26.1M | return Status::OK(); |
503 | 26.1M | } |
504 | | |
505 | | bool ColumnReader::_zone_map_match_condition(const ZoneMap& zone_map, |
506 | 2.01M | const AndBlockColumnPredicate* col_predicates) const { |
507 | 2.01M | if (zone_map.pass_all) { |
508 | 0 | return true; |
509 | 0 | } |
510 | | |
511 | 2.01M | return col_predicates->evaluate_and(zone_map); |
512 | 2.01M | } |
513 | | |
514 | | Status ColumnReader::_get_filtered_pages( |
515 | | const AndBlockColumnPredicate* col_predicates, |
516 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
517 | 130k | std::vector<uint32_t>* page_indexes, const ColumnIteratorOptions& iter_opts) { |
518 | 130k | RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
519 | | |
520 | 130k | const std::vector<ZoneMapPB>& zone_maps = _zone_map_index->page_zone_maps(); |
521 | 130k | size_t page_size = _zone_map_index->num_pages(); |
522 | 310k | for (size_t i = 0; i < page_size; ++i) { |
523 | 179k | if (zone_maps[i].pass_all()) { |
524 | 85.3k | page_indexes->push_back(cast_set<uint32_t>(i)); |
525 | 94.2k | } else { |
526 | 94.2k | segment_v2::ZoneMap zone_map; |
527 | 94.2k | RETURN_IF_ERROR(ZoneMap::from_proto(zone_maps[i], _data_type, zone_map)); |
528 | 94.3k | if (_zone_map_match_condition(zone_map, col_predicates)) { |
529 | 94.3k | bool should_read = true; |
530 | 94.3k | if (delete_predicates != nullptr) { |
531 | 4 | for (auto del_pred : *delete_predicates) { |
532 | | // TODO: Both `min_value` and `max_value` should be 0 or neither should be 0. |
533 | | // So nullable only need to judge once. |
534 | 4 | if (del_pred->evaluate_del(zone_map)) { |
535 | 1 | should_read = false; |
536 | 1 | break; |
537 | 1 | } |
538 | 4 | } |
539 | 3 | } |
540 | 94.3k | if (should_read) { |
541 | 94.3k | page_indexes->push_back(cast_set<uint32_t>(i)); |
542 | 94.3k | } |
543 | 94.3k | } |
544 | 94.2k | } |
545 | 179k | } |
546 | 18.4E | VLOG(1) << "total-pages: " << page_size << " not-filtered-pages: " << page_indexes->size() |
547 | 18.4E | << " filtered-percent:" |
548 | 18.4E | << 1.0 - (static_cast<double>(page_indexes->size()) / |
549 | 18.4E | (static_cast<double>(page_size) * 1.0)); |
550 | 130k | return Status::OK(); |
551 | 130k | } |
552 | | |
553 | | Status ColumnReader::_calculate_row_ranges(const std::vector<uint32_t>& page_indexes, |
554 | | RowRanges* row_ranges, |
555 | 130k | const ColumnIteratorOptions& iter_opts) { |
556 | 130k | row_ranges->clear(); |
557 | 130k | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
558 | 179k | for (auto i : page_indexes) { |
559 | 179k | ordinal_t page_first_id = _ordinal_index->get_first_ordinal(i); |
560 | 179k | ordinal_t page_last_id = _ordinal_index->get_last_ordinal(i); |
561 | 179k | RowRanges page_row_ranges(RowRanges::create_single(page_first_id, page_last_id + 1)); |
562 | 179k | RowRanges::ranges_union(*row_ranges, page_row_ranges, row_ranges); |
563 | 179k | } |
564 | 130k | return Status::OK(); |
565 | 130k | } |
566 | | |
567 | | Status ColumnReader::get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
568 | | RowRanges* row_ranges, |
569 | 98 | const ColumnIteratorOptions& iter_opts) { |
570 | 98 | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
571 | 98 | RETURN_IF_ERROR( |
572 | 98 | _load_bloom_filter_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
573 | 98 | RowRanges bf_row_ranges; |
574 | 98 | std::unique_ptr<BloomFilterIndexIterator> bf_iter; |
575 | 98 | RETURN_IF_ERROR(_bloom_filter_index->new_iterator(&bf_iter, iter_opts.stats)); |
576 | 98 | size_t range_size = row_ranges->range_size(); |
577 | | // get covered page ids |
578 | 98 | std::set<uint32_t> page_ids; |
579 | 196 | for (int i = 0; i < range_size; ++i) { |
580 | 98 | int64_t from = row_ranges->get_range_from(i); |
581 | 98 | int64_t idx = from; |
582 | 98 | int64_t to = row_ranges->get_range_to(i); |
583 | 98 | auto iter = _ordinal_index->seek_at_or_before(from); |
584 | 224 | while (idx < to && iter.valid()) { |
585 | 126 | page_ids.insert(iter.page_index()); |
586 | 126 | idx = iter.last_ordinal() + 1; |
587 | 126 | iter.next(); |
588 | 126 | } |
589 | 98 | } |
590 | 126 | for (auto& pid : page_ids) { |
591 | 126 | std::unique_ptr<BloomFilter> bf; |
592 | 126 | RETURN_IF_ERROR(bf_iter->read_bloom_filter(pid, &bf)); |
593 | 126 | if (col_predicates->evaluate_and(bf.get())) { |
594 | 21 | bf_row_ranges.add(RowRange(_ordinal_index->get_first_ordinal(pid), |
595 | 21 | _ordinal_index->get_last_ordinal(pid) + 1)); |
596 | 21 | } |
597 | 126 | } |
598 | 98 | RowRanges::ranges_intersection(*row_ranges, bf_row_ranges, row_ranges); |
599 | 98 | return Status::OK(); |
600 | 98 | } |
601 | | |
602 | | Status ColumnReader::_load_ordinal_index(bool use_page_cache, bool kept_in_memory, |
603 | 1.73M | const ColumnIteratorOptions& iter_opts) { |
604 | 1.73M | if (!_ordinal_index) { |
605 | 0 | return Status::InternalError("ordinal_index not inited"); |
606 | 0 | } |
607 | 1.73M | return _ordinal_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
608 | 1.73M | } |
609 | | |
610 | | Status ColumnReader::_load_zone_map_index(bool use_page_cache, bool kept_in_memory, |
611 | 131k | const ColumnIteratorOptions& iter_opts) { |
612 | 131k | if (_zone_map_index != nullptr) { |
613 | 131k | return _zone_map_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
614 | 131k | } |
615 | 18.4E | return Status::OK(); |
616 | 131k | } |
617 | | |
618 | 2.86k | Status ColumnReader::get_segment_zone_map(segment_v2::ZoneMap* zone_map) const { |
619 | 2.86k | DORIS_CHECK(zone_map != nullptr); |
620 | 2.86k | DORIS_CHECK(_segment_zone_map != nullptr); |
621 | 2.86k | return ZoneMap::from_proto(*_segment_zone_map, _data_type, *zone_map); |
622 | 2.86k | } |
623 | | |
624 | | Status ColumnReader::get_page_zone_maps(const ColumnIteratorOptions& iter_opts, |
625 | 609 | const std::vector<ZoneMapPB>** zone_maps) { |
626 | 609 | DORIS_CHECK(zone_maps != nullptr); |
627 | 609 | if (_zone_map_index == nullptr) { |
628 | 0 | *zone_maps = nullptr; |
629 | 0 | return Status::OK(); |
630 | 0 | } |
631 | 609 | RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
632 | 609 | *zone_maps = &_zone_map_index->page_zone_maps(); |
633 | 609 | return Status::OK(); |
634 | 609 | } |
635 | | |
636 | | Status ColumnReader::get_row_range_for_page(uint32_t page_index, |
637 | | const ColumnIteratorOptions& iter_opts, |
638 | 1.06k | RowRange* row_range) { |
639 | 1.06k | DORIS_CHECK(row_range != nullptr); |
640 | 1.06k | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
641 | 1.06k | DORIS_CHECK(page_index < _ordinal_index->num_data_pages()); |
642 | 1.06k | *row_range = RowRange(_ordinal_index->get_first_ordinal(page_index), |
643 | 1.06k | _ordinal_index->get_last_ordinal(page_index) + 1); |
644 | 1.06k | return Status::OK(); |
645 | 1.06k | } |
646 | | |
647 | | Status ColumnReader::_load_index(const std::shared_ptr<IndexFileReader>& index_file_reader, |
648 | | const TabletIndex* index_meta, const std::string& rowset_id, |
649 | 73.5k | uint32_t segment_id, size_t rows_of_segment) { |
650 | 73.5k | std::unique_lock<std::shared_mutex> wlock(_load_index_lock); |
651 | | |
652 | 73.5k | if (index_meta == nullptr) { |
653 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
654 | 0 | "Failed to load inverted index: index metadata is null"); |
655 | 0 | } |
656 | | |
657 | 73.5k | auto it = _index_readers.find(index_meta->index_id()); |
658 | 73.5k | if (it != _index_readers.end()) { |
659 | 41.6k | return Status::OK(); |
660 | 41.6k | } |
661 | | |
662 | 31.9k | bool should_analyzer = |
663 | 31.9k | inverted_index::InvertedIndexAnalyzer::should_analyzer(index_meta->properties()); |
664 | | |
665 | 31.9k | FieldType type; |
666 | 31.9k | if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) { |
667 | 2.27k | type = _meta_children_column_type; |
668 | 29.6k | } else { |
669 | 29.6k | type = _type; |
670 | 29.6k | } |
671 | | |
672 | 31.9k | if (index_meta->index_type() == IndexType::ANN) { |
673 | 66 | _index_readers[index_meta->index_id()] = std::make_shared<AnnIndexReader>( |
674 | 66 | index_meta, index_file_reader, rowset_id, segment_id, rows_of_segment); |
675 | 66 | return Status::OK(); |
676 | 66 | } |
677 | | |
678 | 31.8k | IndexReaderPtr index_reader; |
679 | | |
680 | 31.8k | if (is_string_type(type)) { |
681 | 11.7k | if (should_analyzer) { |
682 | 5.65k | try { |
683 | 5.65k | index_reader = FullTextIndexReader::create_shared(index_meta, index_file_reader); |
684 | 5.65k | } catch (const CLuceneError& e) { |
685 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
686 | 0 | "create FullTextIndexReader error: {}", e.what()); |
687 | 0 | } |
688 | 6.10k | } else { |
689 | 6.10k | try { |
690 | 6.10k | index_reader = |
691 | 6.10k | StringTypeInvertedIndexReader::create_shared(index_meta, index_file_reader); |
692 | 6.10k | } catch (const CLuceneError& e) { |
693 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
694 | 0 | "create StringTypeInvertedIndexReader error: {}", e.what()); |
695 | 0 | } |
696 | 6.10k | } |
697 | 20.5k | } else if (is_numeric_type(type)) { |
698 | 20.5k | try { |
699 | 20.5k | index_reader = BkdIndexReader::create_shared(index_meta, index_file_reader); |
700 | 20.5k | } catch (const CLuceneError& e) { |
701 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
702 | 0 | "create BkdIndexReader error: {}", e.what()); |
703 | 0 | } |
704 | 18.4E | } else { |
705 | 18.4E | return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>( |
706 | 18.4E | "Field type {} is not supported for inverted index", type); |
707 | 18.4E | } |
708 | 32.3k | _index_readers[index_meta->index_id()] = index_reader; |
709 | 32.3k | return Status::OK(); |
710 | 31.8k | } |
711 | | |
712 | 106k | bool ColumnReader::has_bloom_filter_index(bool ngram) const { |
713 | 107k | if (_bloom_filter_index == nullptr) return false; |
714 | | |
715 | 18.4E | if (ngram) { |
716 | 77 | return _bloom_filter_index->algorithm() == BloomFilterAlgorithmPB::NGRAM_BLOOM_FILTER; |
717 | 18.4E | } else { |
718 | 18.4E | return _bloom_filter_index->algorithm() != BloomFilterAlgorithmPB::NGRAM_BLOOM_FILTER; |
719 | 18.4E | } |
720 | 18.4E | } |
721 | | |
722 | | Status ColumnReader::_load_bloom_filter_index(bool use_page_cache, bool kept_in_memory, |
723 | 98 | const ColumnIteratorOptions& iter_opts) { |
724 | 98 | if (_bloom_filter_index != nullptr) { |
725 | 98 | return _bloom_filter_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
726 | 98 | } |
727 | 0 | return Status::OK(); |
728 | 98 | } |
729 | | |
730 | | Status ColumnReader::seek_at_or_before(ordinal_t ordinal, OrdinalPageIndexIterator* iter, |
731 | 1.59M | const ColumnIteratorOptions& iter_opts) { |
732 | 1.59M | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
733 | 1.59M | *iter = _ordinal_index->seek_at_or_before(ordinal); |
734 | 1.59M | if (!iter->valid()) { |
735 | 0 | return Status::NotFound("Failed to seek to ordinal {}, ", ordinal); |
736 | 0 | } |
737 | 1.59M | return Status::OK(); |
738 | 1.59M | } |
739 | | |
740 | | Status ColumnReader::get_ordinal_index_reader(OrdinalIndexReader*& reader, |
741 | 882k | OlapReaderStatistics* index_load_stats) { |
742 | 18.4E | CHECK(_ordinal_index) << fmt::format("ordinal index is null for column reader of type {}", |
743 | 18.4E | std::to_string(int(_meta_type))); |
744 | 882k | RETURN_IF_ERROR( |
745 | 882k | _ordinal_index->load(_use_index_page_cache, _opts.kept_in_memory, index_load_stats)); |
746 | 882k | reader = _ordinal_index.get(); |
747 | 882k | return Status::OK(); |
748 | 882k | } |
749 | | |
750 | 369k | Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column) { |
751 | 369k | return new_iterator(iterator, tablet_column, nullptr); |
752 | 369k | } |
753 | | |
754 | | Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column, |
755 | 28.0M | const StorageReadOptions* opt) { |
756 | 28.0M | if (is_empty()) { |
757 | 42.7k | *iterator = std::make_unique<EmptyFileColumnIterator>(); |
758 | 42.7k | return Status::OK(); |
759 | 42.7k | } |
760 | 27.9M | if (is_scalar_type(_meta_type)) { |
761 | 27.8M | if (is_string_type(_meta_type)) { |
762 | 16.7M | *iterator = std::make_unique<StringFileColumnIterator>(shared_from_this()); |
763 | 16.7M | } else { |
764 | 11.1M | *iterator = std::make_unique<FileColumnIterator>(shared_from_this()); |
765 | 11.1M | } |
766 | 27.8M | (*iterator)->set_column_name(tablet_column ? tablet_column->name() : ""); |
767 | 27.8M | return Status::OK(); |
768 | 27.8M | } else { |
769 | 96.4k | auto type = _meta_type; |
770 | 96.4k | switch (type) { |
771 | 883 | case FieldType::OLAP_FIELD_TYPE_AGG_STATE: { |
772 | 883 | return new_agg_state_iterator(iterator); |
773 | 0 | } |
774 | 7.96k | case FieldType::OLAP_FIELD_TYPE_STRUCT: { |
775 | 7.96k | return new_struct_iterator(iterator, tablet_column); |
776 | 0 | } |
777 | 64.4k | case FieldType::OLAP_FIELD_TYPE_ARRAY: { |
778 | 64.4k | return new_array_iterator(iterator, tablet_column); |
779 | 0 | } |
780 | 36.3k | case FieldType::OLAP_FIELD_TYPE_MAP: { |
781 | 36.3k | return new_map_iterator(iterator, tablet_column); |
782 | 0 | } |
783 | 0 | default: |
784 | 0 | return Status::NotSupported("unsupported type to create iterator: {}", |
785 | 0 | std::to_string(int(type))); |
786 | 96.4k | } |
787 | 96.4k | } |
788 | 27.9M | } |
789 | | |
790 | 874 | Status ColumnReader::new_agg_state_iterator(ColumnIteratorUPtr* iterator) { |
791 | 874 | *iterator = std::make_unique<FileColumnIterator>(shared_from_this()); |
792 | 874 | return Status::OK(); |
793 | 874 | } |
794 | | |
795 | | Status ColumnReader::new_array_iterator(ColumnIteratorUPtr* iterator, |
796 | 64.2k | const TabletColumn* tablet_column) { |
797 | 64.2k | ColumnIteratorUPtr item_iterator; |
798 | 64.2k | RETURN_IF_ERROR(_sub_readers[0]->new_iterator( |
799 | 64.2k | &item_iterator, tablet_column && tablet_column->get_subtype_count() > 0 |
800 | 64.2k | ? &tablet_column->get_sub_column(0) |
801 | 64.2k | : nullptr)); |
802 | | |
803 | 64.2k | item_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : ""); |
804 | | |
805 | 64.2k | ColumnIteratorUPtr offset_iterator; |
806 | 64.2k | RETURN_IF_ERROR(_sub_readers[1]->new_iterator(&offset_iterator, nullptr)); |
807 | 64.2k | auto* file_iter = static_cast<FileColumnIterator*>(offset_iterator.release()); |
808 | 64.2k | OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>( |
809 | 64.2k | std::unique_ptr<FileColumnIterator>(file_iter)); |
810 | | |
811 | 64.2k | ColumnIteratorUPtr null_iterator; |
812 | 64.2k | if (is_nullable()) { |
813 | 46.3k | RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&null_iterator, nullptr)); |
814 | 46.3k | } |
815 | 64.2k | *iterator = std::make_unique<ArrayFileColumnIterator>(shared_from_this(), std::move(ofcIter), |
816 | 64.2k | std::move(item_iterator), |
817 | 64.2k | std::move(null_iterator)); |
818 | 64.2k | return Status::OK(); |
819 | 64.2k | } |
820 | | |
821 | | Status ColumnReader::new_map_iterator(ColumnIteratorUPtr* iterator, |
822 | 36.3k | const TabletColumn* tablet_column) { |
823 | 36.3k | ColumnIteratorUPtr key_iterator; |
824 | 36.3k | RETURN_IF_ERROR(_sub_readers[0]->new_iterator( |
825 | 36.3k | &key_iterator, tablet_column && tablet_column->get_subtype_count() > 1 |
826 | 36.3k | ? &tablet_column->get_sub_column(0) |
827 | 36.3k | : nullptr)); |
828 | 36.3k | key_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : ""); |
829 | 36.3k | ColumnIteratorUPtr val_iterator; |
830 | 36.3k | RETURN_IF_ERROR(_sub_readers[1]->new_iterator( |
831 | 36.3k | &val_iterator, tablet_column && tablet_column->get_subtype_count() > 1 |
832 | 36.3k | ? &tablet_column->get_sub_column(1) |
833 | 36.3k | : nullptr)); |
834 | 36.3k | val_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(1).name() : ""); |
835 | 36.3k | ColumnIteratorUPtr offsets_iterator; |
836 | 36.3k | RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&offsets_iterator, nullptr)); |
837 | 36.3k | auto* file_iter = static_cast<FileColumnIterator*>(offsets_iterator.release()); |
838 | 36.3k | OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>( |
839 | 36.3k | std::unique_ptr<FileColumnIterator>(file_iter)); |
840 | | |
841 | 36.3k | ColumnIteratorUPtr null_iterator; |
842 | 36.3k | if (is_nullable()) { |
843 | 15.4k | RETURN_IF_ERROR(_sub_readers[3]->new_iterator(&null_iterator, nullptr)); |
844 | 15.4k | } |
845 | 36.3k | *iterator = std::make_unique<MapFileColumnIterator>( |
846 | 36.3k | shared_from_this(), std::move(null_iterator), std::move(ofcIter), |
847 | 36.3k | std::move(key_iterator), std::move(val_iterator)); |
848 | 36.3k | return Status::OK(); |
849 | 36.3k | } |
850 | | |
851 | | Status ColumnReader::new_struct_iterator(ColumnIteratorUPtr* iterator, |
852 | 7.95k | const TabletColumn* tablet_column) { |
853 | 7.95k | std::vector<ColumnIteratorUPtr> sub_column_iterators; |
854 | 7.95k | size_t child_size = is_nullable() ? _sub_readers.size() - 1 : _sub_readers.size(); |
855 | 18.4E | size_t tablet_column_size = tablet_column ? tablet_column->get_sub_columns().size() : 0; |
856 | 7.95k | sub_column_iterators.reserve(child_size); |
857 | | |
858 | 35.3k | for (uint64_t i = 0; i < child_size; i++) { |
859 | 27.3k | ColumnIteratorUPtr sub_column_iterator; |
860 | 27.3k | RETURN_IF_ERROR(_sub_readers[i]->new_iterator( |
861 | 27.3k | &sub_column_iterator, tablet_column ? &tablet_column->get_sub_column(i) : nullptr)); |
862 | 27.3k | sub_column_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(i).name() |
863 | 18.4E | : ""); |
864 | 27.3k | sub_column_iterators.emplace_back(std::move(sub_column_iterator)); |
865 | 27.3k | } |
866 | | |
867 | | // create default_iterator for schema-change behavior which increase column |
868 | 9.26k | for (size_t i = child_size; i < tablet_column_size; i++) { |
869 | 1.30k | TabletColumn column = tablet_column->get_sub_column(i); |
870 | 1.30k | ColumnIteratorUPtr it; |
871 | 1.30k | RETURN_IF_ERROR(Segment::new_default_iterator(column, &it)); |
872 | 1.30k | it->set_column_name(column.name()); |
873 | 1.30k | sub_column_iterators.emplace_back(std::move(it)); |
874 | 1.30k | } |
875 | | |
876 | 7.95k | ColumnIteratorUPtr null_iterator; |
877 | 7.95k | if (is_nullable()) { |
878 | 7.11k | RETURN_IF_ERROR(_sub_readers[child_size]->new_iterator(&null_iterator, nullptr)); |
879 | 7.11k | } |
880 | 7.95k | *iterator = std::make_unique<StructFileColumnIterator>( |
881 | 7.95k | shared_from_this(), std::move(null_iterator), std::move(sub_column_iterators)); |
882 | 7.95k | return Status::OK(); |
883 | 7.95k | } |
884 | | |
885 | | Result<TColumnAccessPaths> ColumnIterator::_get_sub_access_paths( |
886 | 117k | const TColumnAccessPaths& access_paths) { |
887 | 117k | TColumnAccessPaths sub_access_paths = access_paths; |
888 | 181k | for (auto it = sub_access_paths.begin(); it != sub_access_paths.end();) { |
889 | 63.6k | TColumnAccessPath& name_path = *it; |
890 | 63.6k | if (name_path.data_access_path.path.empty()) { |
891 | 1 | return ResultError( |
892 | 1 | Status::InternalError("Invalid access path for struct column: path is empty")); |
893 | 1 | } |
894 | | |
895 | 63.6k | if (!StringCaseEqual()(name_path.data_access_path.path[0], _column_name)) { |
896 | 1 | return ResultError(Status::InternalError( |
897 | 1 | R"(Invalid access path for column: expected name "{}", got "{}")", _column_name, |
898 | 1 | name_path.data_access_path.path[0])); |
899 | 1 | } |
900 | | |
901 | 63.6k | name_path.data_access_path.path.erase(name_path.data_access_path.path.begin()); |
902 | 63.6k | if (!name_path.data_access_path.path.empty()) { |
903 | 7.10k | ++it; |
904 | 56.5k | } else { |
905 | 56.5k | set_need_to_read(); |
906 | 56.5k | it = sub_access_paths.erase(it); |
907 | 56.5k | } |
908 | 63.6k | } |
909 | 117k | return sub_access_paths; |
910 | 117k | } |
911 | | |
912 | | ///====================== MapFileColumnIterator ============================//// |
913 | | MapFileColumnIterator::MapFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
914 | | ColumnIteratorUPtr null_iterator, |
915 | | OffsetFileColumnIteratorUPtr offsets_iterator, |
916 | | ColumnIteratorUPtr key_iterator, |
917 | | ColumnIteratorUPtr val_iterator) |
918 | 36.3k | : _map_reader(reader), |
919 | 36.3k | _offsets_iterator(std::move(offsets_iterator)), |
920 | 36.3k | _key_iterator(std::move(key_iterator)), |
921 | 36.3k | _val_iterator(std::move(val_iterator)) { |
922 | 36.3k | if (_map_reader->is_nullable()) { |
923 | 15.3k | _null_iterator = std::move(null_iterator); |
924 | 15.3k | } |
925 | 36.3k | } |
926 | | |
927 | 36.2k | Status MapFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
928 | 36.2k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
929 | 31 | DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading."; |
930 | 31 | return Status::OK(); |
931 | 31 | } |
932 | 36.2k | RETURN_IF_ERROR(_key_iterator->init(opts)); |
933 | 36.2k | RETURN_IF_ERROR(_val_iterator->init(opts)); |
934 | 36.2k | RETURN_IF_ERROR(_offsets_iterator->init(opts)); |
935 | 36.2k | if (_map_reader->is_nullable()) { |
936 | 15.3k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
937 | 15.3k | } |
938 | 36.2k | return Status::OK(); |
939 | 36.2k | } |
940 | | |
941 | 20.3k | Status MapFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
942 | 20.3k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
943 | 0 | DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading."; |
944 | 0 | return Status::OK(); |
945 | 0 | } |
946 | | |
947 | 20.3k | if (read_null_map_only()) { |
948 | | // In NULL_MAP_ONLY mode, only seek the null iterator; skip offset/key/val iterators |
949 | 11 | if (_map_reader->is_nullable() && _null_iterator) { |
950 | 11 | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
951 | 11 | } |
952 | 11 | return Status::OK(); |
953 | 11 | } |
954 | | |
955 | 20.3k | if (_map_reader->is_nullable()) { |
956 | 10.9k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
957 | 10.9k | } |
958 | 20.3k | RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(ord)); |
959 | 20.3k | if (read_offset_only()) { |
960 | | // In OFFSET_ONLY mode, key/value iterators are SKIP_READING, no need to seek them |
961 | 468 | return Status::OK(); |
962 | 468 | } |
963 | | // here to use offset info |
964 | 19.8k | ordinal_t offset = 0; |
965 | 19.8k | RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&offset)); |
966 | 19.8k | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(offset)); |
967 | 19.8k | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(offset)); |
968 | 19.8k | return Status::OK(); |
969 | 19.8k | } |
970 | | |
971 | 6.66k | Status MapFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
972 | 6.66k | RETURN_IF_ERROR(_offsets_iterator->init_prefetcher(params)); |
973 | 6.66k | if (_map_reader->is_nullable()) { |
974 | 4.79k | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
975 | 4.79k | } |
976 | 6.66k | RETURN_IF_ERROR(_key_iterator->init_prefetcher(params)); |
977 | 6.66k | RETURN_IF_ERROR(_val_iterator->init_prefetcher(params)); |
978 | 6.66k | return Status::OK(); |
979 | 6.66k | } |
980 | | |
981 | | void MapFileColumnIterator::collect_prefetchers( |
982 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
983 | 6.65k | PrefetcherInitMethod init_method) { |
984 | 6.65k | _offsets_iterator->collect_prefetchers(prefetchers, init_method); |
985 | 6.65k | if (_map_reader->is_nullable()) { |
986 | 4.78k | _null_iterator->collect_prefetchers(prefetchers, init_method); |
987 | 4.78k | } |
988 | | // the actual data pages to read of key/value column depends on the read result of offset column, |
989 | | // so we can't init prefetch blocks according to rowids, just prefetch all data blocks here. |
990 | 6.65k | _key_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
991 | 6.65k | _val_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
992 | 6.65k | } |
993 | | |
994 | 20.3k | Status MapFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
995 | 20.3k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
996 | 0 | DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading."; |
997 | 0 | dst->insert_many_defaults(*n); |
998 | 0 | return Status::OK(); |
999 | 0 | } |
1000 | | |
1001 | 20.3k | if (read_null_map_only()) { |
1002 | | // NULL_MAP_ONLY mode: read null map, fill nested ColumnMap with empty defaults |
1003 | 11 | DORIS_CHECK(is_column_nullable(*dst)); |
1004 | 11 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
1005 | 11 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1006 | 11 | size_t num_read = *n; |
1007 | 11 | if (_null_iterator) { |
1008 | 11 | bool null_signs_has_null = false; |
1009 | 11 | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1010 | 11 | RETURN_IF_ERROR( |
1011 | 11 | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1012 | 11 | } else { |
1013 | | // schema-change: column became nullable but old segment has no null data |
1014 | 0 | null_map_ptr->insert_many_vals(0, num_read); |
1015 | 0 | } |
1016 | 11 | DCHECK(num_read == *n); |
1017 | | // fill nested ColumnMap with empty (zero-element) maps |
1018 | 11 | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1019 | 11 | nullable_col.get_nested_column()); |
1020 | 11 | column_map.insert_many_defaults(num_read); |
1021 | 11 | *has_null = true; |
1022 | 11 | return Status::OK(); |
1023 | 11 | } |
1024 | | |
1025 | 20.3k | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1026 | 20.3k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1027 | 20.3k | : *dst); |
1028 | 20.3k | auto column_offsets_ptr = IColumn::mutate(std::move(column_map.get_offsets_ptr())); |
1029 | 20.3k | Defer defer_offsets {[&] { |
1030 | 20.3k | auto typed_column_offsets_ptr = ColumnMap::COffsets::cast_to_column_mutptr( |
1031 | 20.3k | assert_cast<ColumnMap::COffsets*, TypeCheckOnRelease::DISABLE>( |
1032 | 20.3k | column_offsets_ptr.get())); |
1033 | 20.3k | column_offsets_ptr = nullptr; |
1034 | 20.3k | column_map.get_offsets_ptr() = std::move(typed_column_offsets_ptr); |
1035 | 20.3k | }}; |
1036 | 20.3k | bool offsets_has_null = false; |
1037 | 20.3k | ssize_t start = column_offsets_ptr->size(); |
1038 | 20.3k | RETURN_IF_ERROR(_offsets_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null)); |
1039 | 20.3k | if (*n == 0) { |
1040 | 0 | return Status::OK(); |
1041 | 0 | } |
1042 | 20.3k | auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr); |
1043 | 20.3k | RETURN_IF_ERROR(_offsets_iterator->_calculate_offsets(start, column_offsets)); |
1044 | 20.3k | DCHECK(column_offsets.get_data().back() >= column_offsets.get_data()[start - 1]); |
1045 | 20.3k | size_t num_items = |
1046 | 20.3k | column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid |
1047 | | |
1048 | 20.3k | if (num_items > 0) { |
1049 | 17.5k | auto key_ptr = IColumn::mutate(std::move(column_map.get_keys_ptr())); |
1050 | 17.5k | auto val_ptr = IColumn::mutate(std::move(column_map.get_values_ptr())); |
1051 | 17.5k | Defer defer_keys {[&] { column_map.get_keys_ptr() = std::move(key_ptr); }}; |
1052 | 17.5k | Defer defer_values {[&] { column_map.get_values_ptr() = std::move(val_ptr); }}; |
1053 | 17.5k | if (read_offset_only()) { |
1054 | | // OFFSET_ONLY mode: skip reading actual key/value data, fill with defaults |
1055 | 468 | key_ptr->insert_many_defaults(num_items); |
1056 | 468 | val_ptr->insert_many_defaults(num_items); |
1057 | 17.0k | } else { |
1058 | 17.0k | size_t num_read = num_items; |
1059 | 17.0k | bool key_has_null = false; |
1060 | 17.0k | bool val_has_null = false; |
1061 | 17.0k | RETURN_IF_ERROR(_key_iterator->next_batch(&num_read, key_ptr, &key_has_null)); |
1062 | 17.0k | RETURN_IF_ERROR(_val_iterator->next_batch(&num_read, val_ptr, &val_has_null)); |
1063 | 17.0k | DCHECK(num_read == num_items); |
1064 | 17.0k | } |
1065 | 17.5k | } |
1066 | | |
1067 | 20.3k | if (is_column_nullable(*dst)) { |
1068 | 10.9k | size_t num_read = *n; |
1069 | 10.9k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1070 | | // in not-null to null linked-schemachange mode, |
1071 | | // actually we do not change dat data include meta in footer, |
1072 | | // so may dst from changed meta which is nullable but old data is not nullable, |
1073 | | // if so, we should set null_map to all null by default |
1074 | 10.9k | if (_null_iterator) { |
1075 | 10.9k | bool null_signs_has_null = false; |
1076 | 10.9k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1077 | 10.9k | RETURN_IF_ERROR( |
1078 | 10.9k | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1079 | 18.4E | } else { |
1080 | 18.4E | null_map_ptr->insert_many_vals(0, num_read); |
1081 | 18.4E | } |
1082 | 10.9k | DCHECK(num_read == *n); |
1083 | 10.9k | } |
1084 | 20.3k | return Status::OK(); |
1085 | 20.3k | } |
1086 | | |
1087 | | Status MapFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
1088 | 17.0k | MutableColumnPtr& dst) { |
1089 | 17.0k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1090 | 1 | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
1091 | 1 | dst->insert_many_defaults(count); |
1092 | 1 | return Status::OK(); |
1093 | 1 | } |
1094 | | |
1095 | 17.0k | if (read_null_map_only()) { |
1096 | | // NULL_MAP_ONLY mode: read null map by rowids, fill nested ColumnMap with empty defaults |
1097 | 7 | DORIS_CHECK(is_column_nullable(*dst)); |
1098 | 7 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
1099 | 7 | if (_null_iterator) { |
1100 | 7 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1101 | 7 | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1102 | 7 | RETURN_IF_ERROR(_null_iterator->read_by_rowids(rowids, count, null_map_column)); |
1103 | 7 | } else { |
1104 | | // schema-change: column became nullable but old segment has no null data |
1105 | 0 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1106 | 0 | null_map_ptr->insert_many_vals(0, count); |
1107 | 0 | } |
1108 | | // fill nested ColumnMap with empty (zero-element) maps |
1109 | 7 | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1110 | 7 | nullable_col.get_nested_column()); |
1111 | 7 | column_map.insert_many_defaults(count); |
1112 | 7 | return Status::OK(); |
1113 | 7 | } |
1114 | | |
1115 | 17.0k | if (count == 0) { |
1116 | 0 | return Status::OK(); |
1117 | 0 | } |
1118 | | // resolve ColumnMap and nullable wrapper |
1119 | 17.0k | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1120 | 17.0k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1121 | 17.0k | : *dst); |
1122 | 17.0k | auto offsets_ptr = IColumn::mutate(std::move(column_map.get_offsets_ptr())); |
1123 | 17.0k | Defer defer_offsets {[&] { |
1124 | 17.0k | auto typed_offsets_ptr = ColumnMap::COffsets::cast_to_column_mutptr( |
1125 | 17.0k | assert_cast<ColumnMap::COffsets*, TypeCheckOnRelease::DISABLE>(offsets_ptr.get())); |
1126 | 17.0k | offsets_ptr = nullptr; |
1127 | 17.0k | column_map.get_offsets_ptr() = std::move(typed_offsets_ptr); |
1128 | 17.0k | }}; |
1129 | 17.0k | auto& offsets = static_cast<ColumnArray::ColumnOffsets&>(*offsets_ptr); |
1130 | 17.0k | size_t base = offsets.get_data().empty() ? 0 : offsets.get_data().back(); |
1131 | | |
1132 | | // 1. bulk read null-map if nullable |
1133 | 17.0k | std::vector<uint8_t> null_mask; // 0: not null, 1: null |
1134 | 17.0k | if (_map_reader->is_nullable()) { |
1135 | | // For nullable map columns, the destination column must also be nullable. |
1136 | 3.11k | if (UNLIKELY(!is_column_nullable(*dst))) { |
1137 | 0 | return Status::InternalError( |
1138 | 0 | "unexpected non-nullable destination column for nullable map reader"); |
1139 | 0 | } |
1140 | 3.11k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1141 | 3.11k | size_t null_before = null_map_ptr->size(); |
1142 | 3.11k | auto* null_map_col = null_map_ptr.get(); |
1143 | 3.11k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1144 | 3.11k | RETURN_IF_ERROR(_null_iterator->read_by_rowids(rowids, count, null_map_column)); |
1145 | | // extract a light-weight view to decide element reads |
1146 | 3.11k | null_mask.reserve(count); |
1147 | 56.4k | for (size_t i = 0; i < count; ++i) { |
1148 | 53.3k | null_mask.push_back(null_map_col->get_element(null_before + i)); |
1149 | 53.3k | } |
1150 | 13.8k | } else if (is_column_nullable(*dst)) { |
1151 | | // in not-null to null linked-schemachange mode, |
1152 | | // actually we do not change dat data include meta in footer, |
1153 | | // so may dst from changed meta which is nullable but old data is not nullable, |
1154 | | // if so, we should set null_map to all null by default |
1155 | 1 | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1156 | 1 | null_map_ptr->insert_many_vals(0, count); |
1157 | 1 | } |
1158 | | |
1159 | | // 2. bulk read start ordinals for requested rows |
1160 | 17.0k | MutableColumnPtr starts_col = ColumnOffset64::create(); |
1161 | 17.0k | starts_col->reserve(count); |
1162 | 17.0k | RETURN_IF_ERROR(_offsets_iterator->read_by_rowids(rowids, count, starts_col)); |
1163 | | |
1164 | | // 3. bulk read next-start ordinals for rowid+1 (within bounds) |
1165 | 17.0k | std::vector<rowid_t> next_rowids(count); |
1166 | 3.60M | for (size_t i = 0; i < count; ++i) { |
1167 | 3.58M | uint64_t nr = rowids[i] + 1; |
1168 | 3.58M | next_rowids[i] = nr < _map_reader->num_rows() ? static_cast<rowid_t>(nr) |
1169 | 3.58M | : static_cast<rowid_t>(0); // placeholder |
1170 | 3.58M | } |
1171 | 17.0k | MutableColumnPtr next_starts_col = ColumnOffset64::create(); |
1172 | 17.0k | next_starts_col->reserve(count); |
1173 | | // read for all; we'll fix out-of-bound cases below |
1174 | 17.0k | RETURN_IF_ERROR(_offsets_iterator->read_by_rowids(next_rowids.data(), count, next_starts_col)); |
1175 | | |
1176 | | // 4. fix next_start for rows whose next_rowid is out-of-bound (rowid == num_rows-1) |
1177 | 3.60M | for (size_t i = 0; i < count; ++i) { |
1178 | 3.58M | if (rowids[i] + 1 >= _map_reader->num_rows()) { |
1179 | | // seek to the last row and consume one to move decoder to end-of-page, |
1180 | | // then peek page-tail sentinel next_array_item_ordinal as next_start |
1181 | 14.0k | RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(rowids[i])); |
1182 | 14.0k | size_t one = 1; |
1183 | 14.0k | bool has_null_unused = false; |
1184 | 14.0k | MutableColumnPtr tmp = ColumnOffset64::create(); |
1185 | 14.0k | RETURN_IF_ERROR(_offsets_iterator->next_batch(&one, tmp, &has_null_unused)); |
1186 | 14.0k | ordinal_t ns = 0; |
1187 | 14.0k | RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&ns)); |
1188 | | // overwrite with sentinel |
1189 | 14.0k | assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*next_starts_col) |
1190 | 14.0k | .get_data()[i] = ns; |
1191 | 14.0k | } |
1192 | 3.58M | } |
1193 | | |
1194 | | // 5. compute sizes and append offsets prefix-sum |
1195 | 17.0k | auto& starts_data = assert_cast<ColumnOffset64&>(*starts_col).get_data(); |
1196 | 17.0k | auto& next_starts_data = assert_cast<ColumnOffset64&>(*next_starts_col).get_data(); |
1197 | 17.0k | std::vector<size_t> sizes(count, 0); |
1198 | 17.0k | size_t acc = base; |
1199 | 17.0k | const auto original_size = offsets.get_data().back(); |
1200 | 17.0k | offsets.get_data().reserve(offsets.get_data().size() + count); |
1201 | 3.59M | for (size_t i = 0; i < count; ++i) { |
1202 | 3.57M | size_t sz = static_cast<size_t>(next_starts_data[i] - starts_data[i]); |
1203 | 3.57M | if (_map_reader->is_nullable() && !null_mask.empty() && null_mask[i]) { |
1204 | 725 | sz = 0; // null rows do not consume elements |
1205 | 725 | } |
1206 | 3.57M | sizes[i] = sz; |
1207 | 3.57M | acc += sz; |
1208 | 3.57M | offsets.get_data().push_back(acc); |
1209 | 3.57M | } |
1210 | | |
1211 | | // 6. read key/value elements for non-empty sizes |
1212 | 17.0k | auto keys_ptr = IColumn::mutate(std::move(column_map.get_keys_ptr())); |
1213 | 17.0k | auto vals_ptr = IColumn::mutate(std::move(column_map.get_values_ptr())); |
1214 | 17.0k | Defer defer_keys {[&] { column_map.get_keys_ptr() = std::move(keys_ptr); }}; |
1215 | 17.0k | Defer defer_values {[&] { column_map.get_values_ptr() = std::move(vals_ptr); }}; |
1216 | | |
1217 | 17.0k | size_t this_run = sizes[0]; |
1218 | 17.0k | auto start_idx = starts_data[0]; |
1219 | 17.0k | auto last_idx = starts_data[0] + this_run; |
1220 | 3.57M | for (size_t i = 1; i < count; ++i) { |
1221 | 3.56M | size_t sz = sizes[i]; |
1222 | 3.56M | if (sz == 0) { |
1223 | 29.8k | continue; |
1224 | 29.8k | } |
1225 | 3.53M | auto start = static_cast<ordinal_t>(starts_data[i]); |
1226 | 3.53M | if (start != last_idx) { |
1227 | 124k | size_t n = this_run; |
1228 | 124k | bool dummy_has_null = false; |
1229 | | |
1230 | 124k | if (this_run != 0) { |
1231 | 124k | if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) { |
1232 | 124k | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx)); |
1233 | 124k | RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null)); |
1234 | 124k | DCHECK(n == this_run); |
1235 | 124k | } |
1236 | | |
1237 | 124k | if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) { |
1238 | 124k | n = this_run; |
1239 | 124k | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx)); |
1240 | 124k | RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null)); |
1241 | 124k | DCHECK(n == this_run); |
1242 | 124k | } |
1243 | 124k | } |
1244 | 124k | start_idx = start; |
1245 | 124k | this_run = sz; |
1246 | 124k | last_idx = start + sz; |
1247 | 124k | continue; |
1248 | 124k | } |
1249 | | |
1250 | 3.40M | this_run += sz; |
1251 | 3.40M | last_idx += sz; |
1252 | 3.40M | } |
1253 | | |
1254 | 17.0k | size_t n = this_run; |
1255 | 17.0k | const size_t total_count = offsets.get_data().back() - original_size; |
1256 | 17.0k | bool dummy_has_null = false; |
1257 | 17.0k | if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) { |
1258 | 16.9k | if (this_run != 0) { |
1259 | 8.26k | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx)); |
1260 | 8.26k | RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null)); |
1261 | 8.26k | DCHECK(n == this_run); |
1262 | 8.26k | } |
1263 | 16.9k | } else { |
1264 | 9 | keys_ptr->insert_many_defaults(total_count); |
1265 | 9 | } |
1266 | | |
1267 | 17.0k | if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) { |
1268 | 16.9k | if (this_run != 0) { |
1269 | 8.25k | n = this_run; |
1270 | 8.25k | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx)); |
1271 | 8.25k | RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null)); |
1272 | 8.25k | DCHECK(n == this_run); |
1273 | 8.25k | } |
1274 | 16.9k | } else { |
1275 | 18 | vals_ptr->insert_many_defaults(total_count); |
1276 | 18 | } |
1277 | | |
1278 | 17.0k | return Status::OK(); |
1279 | 17.0k | } |
1280 | | |
1281 | 5.70k | void MapFileColumnIterator::set_need_to_read() { |
1282 | 5.70k | set_reading_flag(ReadingFlag::NEED_TO_READ); |
1283 | 5.70k | _key_iterator->set_need_to_read(); |
1284 | 5.70k | _val_iterator->set_need_to_read(); |
1285 | 5.70k | } |
1286 | | |
1287 | 6.88k | void MapFileColumnIterator::remove_pruned_sub_iterators() { |
1288 | 6.88k | _key_iterator->remove_pruned_sub_iterators(); |
1289 | 6.88k | _val_iterator->remove_pruned_sub_iterators(); |
1290 | 6.88k | } |
1291 | | |
1292 | | Status MapFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths, |
1293 | 6.67k | const TColumnAccessPaths& predicate_access_paths) { |
1294 | 6.67k | if (all_access_paths.empty()) { |
1295 | 1.51k | return Status::OK(); |
1296 | 1.51k | } |
1297 | | |
1298 | 5.16k | if (!predicate_access_paths.empty()) { |
1299 | 32 | set_reading_flag(ReadingFlag::READING_FOR_PREDICATE); |
1300 | 32 | DLOG(INFO) << "Map column iterator set sub-column " << _column_name |
1301 | 32 | << " to READING_FOR_PREDICATE"; |
1302 | 32 | } |
1303 | | |
1304 | 5.16k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
1305 | 5.16k | auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths)); |
1306 | | |
1307 | 5.16k | if (sub_all_access_paths.empty()) { |
1308 | 3.95k | return Status::OK(); |
1309 | 3.95k | } |
1310 | | |
1311 | | // Check for meta-only modes (OFFSET_ONLY or NULL_MAP_ONLY) |
1312 | 1.21k | _check_and_set_meta_read_mode(sub_all_access_paths); |
1313 | 1.21k | if (read_offset_only()) { |
1314 | 474 | _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1315 | 474 | _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1316 | 474 | DLOG(INFO) << "Map column iterator set column " << _column_name |
1317 | 474 | << " to OFFSET_ONLY reading mode, key/value columns set to SKIP_READING"; |
1318 | 474 | return Status::OK(); |
1319 | 474 | } |
1320 | 736 | if (read_null_map_only()) { |
1321 | 18 | _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1322 | 18 | _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1323 | 18 | DLOG(INFO) << "Map column iterator set column " << _column_name |
1324 | 18 | << " to NULL_MAP_ONLY reading mode, key/value columns set to SKIP_READING"; |
1325 | 18 | return Status::OK(); |
1326 | 18 | } |
1327 | | |
1328 | 718 | TColumnAccessPaths key_all_access_paths; |
1329 | 718 | TColumnAccessPaths val_all_access_paths; |
1330 | 718 | TColumnAccessPaths key_predicate_access_paths; |
1331 | 718 | TColumnAccessPaths val_predicate_access_paths; |
1332 | | |
1333 | 753 | for (auto paths : sub_all_access_paths) { |
1334 | 753 | if (paths.data_access_path.path[0] == ACCESS_ALL) { |
1335 | | // ACCESS_ALL means element_at(map, key) style access: the key column must be |
1336 | | // fully read so that the runtime can match the requested key, while any sub-path |
1337 | | // qualifiers (e.g. OFFSET) apply only to the value column. |
1338 | | // For key: create a path with just the column name (= full data access). |
1339 | 199 | TColumnAccessPath key_path; |
1340 | 199 | key_path.__set_type(paths.type); |
1341 | 199 | TDataAccessPath key_data_path; |
1342 | 199 | key_data_path.__set_path({_key_iterator->column_name()}); |
1343 | 199 | key_path.__set_data_access_path(key_data_path); |
1344 | 199 | key_all_access_paths.emplace_back(std::move(key_path)); |
1345 | | // For value: pass the full sub-path so qualifiers like OFFSET propagate. |
1346 | 199 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1347 | 199 | val_all_access_paths.emplace_back(paths); |
1348 | 554 | } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) { |
1349 | 282 | paths.data_access_path.path[0] = _key_iterator->column_name(); |
1350 | 282 | key_all_access_paths.emplace_back(paths); |
1351 | 282 | } else if (paths.data_access_path.path[0] == ACCESS_MAP_VALUES) { |
1352 | 272 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1353 | 272 | val_all_access_paths.emplace_back(paths); |
1354 | 272 | } |
1355 | 753 | } |
1356 | 718 | const auto need_read_keys = !key_all_access_paths.empty(); |
1357 | 718 | const auto need_read_values = !val_all_access_paths.empty(); |
1358 | | |
1359 | 718 | for (auto paths : sub_predicate_access_paths) { |
1360 | 22 | if (paths.data_access_path.path[0] == ACCESS_ALL) { |
1361 | | // Same logic as above: key needs full data, value gets the sub-path. |
1362 | 17 | TColumnAccessPath key_path; |
1363 | 17 | key_path.__set_type(paths.type); |
1364 | 17 | TDataAccessPath key_data_path; |
1365 | 17 | key_data_path.__set_path({_key_iterator->column_name()}); |
1366 | 17 | key_path.__set_data_access_path(key_data_path); |
1367 | 17 | key_predicate_access_paths.emplace_back(std::move(key_path)); |
1368 | 17 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1369 | 17 | val_predicate_access_paths.emplace_back(paths); |
1370 | 17 | } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) { |
1371 | 3 | paths.data_access_path.path[0] = _key_iterator->column_name(); |
1372 | 3 | key_predicate_access_paths.emplace_back(paths); |
1373 | 3 | } else if (paths.data_access_path.path[0] == ACCESS_MAP_VALUES) { |
1374 | 2 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1375 | 2 | val_predicate_access_paths.emplace_back(paths); |
1376 | 2 | } |
1377 | 22 | } |
1378 | | |
1379 | 718 | if (need_read_keys) { |
1380 | 480 | _key_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ); |
1381 | 480 | RETURN_IF_ERROR( |
1382 | 480 | _key_iterator->set_access_paths(key_all_access_paths, key_predicate_access_paths)); |
1383 | 480 | } else { |
1384 | 238 | _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1385 | 238 | DLOG(INFO) << "Map column iterator set key column to SKIP_READING"; |
1386 | 238 | } |
1387 | | |
1388 | 718 | if (need_read_values) { |
1389 | 470 | _val_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ); |
1390 | 470 | RETURN_IF_ERROR( |
1391 | 470 | _val_iterator->set_access_paths(val_all_access_paths, val_predicate_access_paths)); |
1392 | 470 | } else { |
1393 | 248 | _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1394 | 248 | DLOG(INFO) << "Map column iterator set value column to SKIP_READING"; |
1395 | 248 | } |
1396 | 718 | return Status::OK(); |
1397 | 718 | } |
1398 | | |
1399 | | //////////////////////////////////////////////////////////////////////////////// |
1400 | | |
1401 | | StructFileColumnIterator::StructFileColumnIterator( |
1402 | | std::shared_ptr<ColumnReader> reader, ColumnIteratorUPtr null_iterator, |
1403 | | std::vector<ColumnIteratorUPtr>&& sub_column_iterators) |
1404 | 7.95k | : _struct_reader(reader), _sub_column_iterators(std::move(sub_column_iterators)) { |
1405 | 7.95k | if (_struct_reader->is_nullable()) { |
1406 | 7.10k | _null_iterator = std::move(null_iterator); |
1407 | 7.10k | } |
1408 | 7.95k | } |
1409 | | |
1410 | 7.93k | Status StructFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1411 | 7.93k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1412 | 0 | DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading."; |
1413 | 0 | return Status::OK(); |
1414 | 0 | } |
1415 | | |
1416 | 27.7k | for (auto& column_iterator : _sub_column_iterators) { |
1417 | 27.7k | RETURN_IF_ERROR(column_iterator->init(opts)); |
1418 | 27.7k | } |
1419 | 7.93k | if (_struct_reader->is_nullable()) { |
1420 | 7.09k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
1421 | 7.09k | } |
1422 | 7.93k | return Status::OK(); |
1423 | 7.93k | } |
1424 | | |
1425 | 4.66k | Status StructFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1426 | 4.66k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1427 | 0 | DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading."; |
1428 | 0 | dst->insert_many_defaults(*n); |
1429 | 0 | return Status::OK(); |
1430 | 0 | } |
1431 | | |
1432 | 4.66k | if (read_null_map_only()) { |
1433 | | // NULL_MAP_ONLY mode: read null map, fill nested ColumnStruct with empty defaults |
1434 | 2 | DORIS_CHECK(is_column_nullable(*dst)); |
1435 | 2 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
1436 | 2 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1437 | 2 | size_t num_read = *n; |
1438 | 2 | if (_null_iterator) { |
1439 | 2 | bool null_signs_has_null = false; |
1440 | 2 | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1441 | 2 | RETURN_IF_ERROR( |
1442 | 2 | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1443 | 2 | } else { |
1444 | | // schema-change: column became nullable but old segment has no null data |
1445 | 0 | null_map_ptr->insert_many_vals(0, num_read); |
1446 | 0 | } |
1447 | 2 | DCHECK(num_read == *n); |
1448 | | // fill nested ColumnStruct with defaults to maintain consistent column sizes |
1449 | 2 | auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>( |
1450 | 2 | nullable_col.get_nested_column()); |
1451 | 2 | column_struct.insert_many_defaults(num_read); |
1452 | 2 | *has_null = true; |
1453 | 2 | return Status::OK(); |
1454 | 2 | } |
1455 | | |
1456 | 4.66k | auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>( |
1457 | 4.66k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1458 | 4.66k | : *dst); |
1459 | 19.5k | for (size_t i = 0; i < column_struct.tuple_size(); i++) { |
1460 | 14.8k | size_t num_read = *n; |
1461 | 14.8k | auto sub_column_ptr = IColumn::mutate(std::move(column_struct.get_column_ptr(i))); |
1462 | 14.8k | Defer defer_sub_column { |
1463 | 14.8k | [&] { column_struct.get_column_ptr(i) = std::move(sub_column_ptr); }}; |
1464 | 14.8k | bool column_has_null = false; |
1465 | 14.8k | RETURN_IF_ERROR( |
1466 | 14.8k | _sub_column_iterators[i]->next_batch(&num_read, sub_column_ptr, &column_has_null)); |
1467 | 14.8k | DCHECK(num_read == *n); |
1468 | 14.8k | } |
1469 | | |
1470 | 4.66k | if (is_column_nullable(*dst)) { |
1471 | 4.34k | size_t num_read = *n; |
1472 | 4.34k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1473 | | // in not-null to null linked-schemachange mode, |
1474 | | // actually we do not change dat data include meta in footer, |
1475 | | // so may dst from changed meta which is nullable but old data is not nullable, |
1476 | | // if so, we should set null_map to all null by default |
1477 | 4.34k | if (_null_iterator) { |
1478 | 4.25k | bool null_signs_has_null = false; |
1479 | 4.25k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1480 | 4.25k | RETURN_IF_ERROR( |
1481 | 4.25k | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1482 | 4.25k | } else { |
1483 | 89 | null_map_ptr->insert_many_vals(0, num_read); |
1484 | 89 | } |
1485 | 4.34k | DCHECK(num_read == *n); |
1486 | 4.34k | } |
1487 | | |
1488 | 4.66k | return Status::OK(); |
1489 | 4.66k | } |
1490 | | |
1491 | 4.66k | Status StructFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
1492 | 4.66k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1493 | 0 | DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading."; |
1494 | 0 | return Status::OK(); |
1495 | 0 | } |
1496 | | |
1497 | 4.66k | if (read_null_map_only()) { |
1498 | | // In NULL_MAP_ONLY mode, only seek the null iterator; skip all sub-column iterators |
1499 | 2 | if (_struct_reader->is_nullable() && _null_iterator) { |
1500 | 2 | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1501 | 2 | } |
1502 | 2 | return Status::OK(); |
1503 | 2 | } |
1504 | | |
1505 | 14.8k | for (auto& column_iterator : _sub_column_iterators) { |
1506 | 14.8k | RETURN_IF_ERROR(column_iterator->seek_to_ordinal(ord)); |
1507 | 14.8k | } |
1508 | 4.66k | if (_struct_reader->is_nullable()) { |
1509 | 4.25k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1510 | 4.25k | } |
1511 | 4.66k | return Status::OK(); |
1512 | 4.66k | } |
1513 | | |
1514 | 3.46k | Status StructFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1515 | 10.0k | for (auto& column_iterator : _sub_column_iterators) { |
1516 | 10.0k | RETURN_IF_ERROR(column_iterator->init_prefetcher(params)); |
1517 | 10.0k | } |
1518 | 3.46k | if (_struct_reader->is_nullable()) { |
1519 | 3.09k | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
1520 | 3.09k | } |
1521 | 3.46k | return Status::OK(); |
1522 | 3.46k | } |
1523 | | |
1524 | | void StructFileColumnIterator::collect_prefetchers( |
1525 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1526 | 3.46k | PrefetcherInitMethod init_method) { |
1527 | 10.0k | for (auto& column_iterator : _sub_column_iterators) { |
1528 | 10.0k | column_iterator->collect_prefetchers(prefetchers, init_method); |
1529 | 10.0k | } |
1530 | 3.46k | if (_struct_reader->is_nullable()) { |
1531 | 3.08k | _null_iterator->collect_prefetchers(prefetchers, init_method); |
1532 | 3.08k | } |
1533 | 3.46k | } |
1534 | | |
1535 | | Status StructFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
1536 | 2.74k | MutableColumnPtr& dst) { |
1537 | 2.74k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1538 | 0 | DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading."; |
1539 | 0 | dst->insert_many_defaults(count); |
1540 | 0 | return Status::OK(); |
1541 | 0 | } |
1542 | | |
1543 | 2.74k | if (count == 0) { |
1544 | 0 | return Status::OK(); |
1545 | 0 | } |
1546 | | |
1547 | 2.74k | size_t this_run = 1; |
1548 | 2.74k | auto start_idx = rowids[0]; |
1549 | 2.74k | auto last_idx = rowids[0]; |
1550 | 2.86k | for (size_t i = 1; i < count; ++i) { |
1551 | 117 | if (last_idx == rowids[i] - 1) { |
1552 | 111 | last_idx = rowids[i]; |
1553 | 111 | this_run++; |
1554 | 111 | continue; |
1555 | 111 | } |
1556 | 6 | RETURN_IF_ERROR(seek_to_ordinal(start_idx)); |
1557 | 6 | size_t num_read = this_run; |
1558 | 6 | RETURN_IF_ERROR(next_batch(&num_read, dst)); |
1559 | 6 | DCHECK_EQ(num_read, this_run); |
1560 | | |
1561 | 6 | start_idx = rowids[i]; |
1562 | 6 | last_idx = rowids[i]; |
1563 | 6 | this_run = 1; |
1564 | 6 | } |
1565 | | |
1566 | 2.74k | RETURN_IF_ERROR(seek_to_ordinal(start_idx)); |
1567 | 2.74k | size_t num_read = this_run; |
1568 | 2.74k | RETURN_IF_ERROR(next_batch(&num_read, dst)); |
1569 | 2.74k | DCHECK_EQ(num_read, this_run); |
1570 | 2.74k | return Status::OK(); |
1571 | 2.74k | } |
1572 | | |
1573 | 6.89k | void StructFileColumnIterator::set_need_to_read() { |
1574 | 6.89k | set_reading_flag(ReadingFlag::NEED_TO_READ); |
1575 | 24.9k | for (auto& sub_iterator : _sub_column_iterators) { |
1576 | 24.9k | sub_iterator->set_need_to_read(); |
1577 | 24.9k | } |
1578 | 6.89k | } |
1579 | | |
1580 | 7.06k | void StructFileColumnIterator::remove_pruned_sub_iterators() { |
1581 | 33.3k | for (auto it = _sub_column_iterators.begin(); it != _sub_column_iterators.end();) { |
1582 | 26.2k | auto& sub_iterator = *it; |
1583 | 26.2k | if (sub_iterator->reading_flag() == ReadingFlag::SKIP_READING) { |
1584 | 860 | DLOG(INFO) << "Struct column iterator remove pruned sub-column " |
1585 | 860 | << sub_iterator->column_name(); |
1586 | 860 | it = _sub_column_iterators.erase(it); |
1587 | 25.3k | } else { |
1588 | 25.3k | sub_iterator->remove_pruned_sub_iterators(); |
1589 | 25.3k | ++it; |
1590 | 25.3k | } |
1591 | 26.2k | } |
1592 | 7.06k | } |
1593 | | |
1594 | | Status StructFileColumnIterator::set_access_paths( |
1595 | | const TColumnAccessPaths& all_access_paths, |
1596 | 6.52k | const TColumnAccessPaths& predicate_access_paths) { |
1597 | 6.52k | if (all_access_paths.empty()) { |
1598 | 1.86k | return Status::OK(); |
1599 | 1.86k | } |
1600 | | |
1601 | 4.65k | if (!predicate_access_paths.empty()) { |
1602 | 61 | set_reading_flag(ReadingFlag::READING_FOR_PREDICATE); |
1603 | 61 | DLOG(INFO) << "Struct column iterator set sub-column " << _column_name |
1604 | 61 | << " to READING_FOR_PREDICATE"; |
1605 | 61 | } |
1606 | 4.65k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
1607 | 4.65k | auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths)); |
1608 | | |
1609 | | // Check for NULL_MAP_ONLY mode: only read null map, skip all sub-columns |
1610 | 4.65k | _check_and_set_meta_read_mode(sub_all_access_paths); |
1611 | 4.65k | if (read_null_map_only()) { |
1612 | 6 | for (auto& sub_iterator : _sub_column_iterators) { |
1613 | 6 | sub_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1614 | 6 | } |
1615 | 3 | DLOG(INFO) << "Struct column iterator set column " << _column_name |
1616 | 3 | << " to NULL_MAP_ONLY reading mode, all sub-columns set to SKIP_READING"; |
1617 | 3 | return Status::OK(); |
1618 | 3 | } |
1619 | | |
1620 | 4.65k | const auto no_sub_column_to_skip = sub_all_access_paths.empty(); |
1621 | 4.65k | const auto no_predicate_sub_column = sub_predicate_access_paths.empty(); |
1622 | | |
1623 | 20.7k | for (auto& sub_iterator : _sub_column_iterators) { |
1624 | 20.7k | const auto name = sub_iterator->column_name(); |
1625 | 20.7k | bool need_to_read = no_sub_column_to_skip; |
1626 | 20.7k | TColumnAccessPaths sub_all_access_paths_of_this; |
1627 | 20.7k | if (!need_to_read) { |
1628 | 2.12k | for (const auto& paths : sub_all_access_paths) { |
1629 | 2.12k | if (paths.data_access_path.path[0] == name) { |
1630 | 423 | sub_all_access_paths_of_this.emplace_back(paths); |
1631 | 423 | } |
1632 | 2.12k | } |
1633 | 1.27k | need_to_read = !sub_all_access_paths_of_this.empty(); |
1634 | 1.27k | } |
1635 | | |
1636 | 20.7k | if (!need_to_read) { |
1637 | 857 | sub_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1638 | 857 | DLOG(INFO) << "Struct column iterator set sub-column " << name << " to SKIP_READING"; |
1639 | 857 | continue; |
1640 | 857 | } |
1641 | 19.8k | set_reading_flag(ReadingFlag::NEED_TO_READ); |
1642 | 19.8k | sub_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ); |
1643 | | |
1644 | 19.8k | TColumnAccessPaths sub_predicate_access_paths_of_this; |
1645 | | |
1646 | 19.8k | if (!no_predicate_sub_column) { |
1647 | 59 | for (const auto& paths : sub_predicate_access_paths) { |
1648 | 59 | if (StringCaseEqual()(paths.data_access_path.path[0], name)) { |
1649 | 56 | sub_predicate_access_paths_of_this.emplace_back(paths); |
1650 | 56 | } |
1651 | 59 | } |
1652 | 59 | } |
1653 | | |
1654 | 19.8k | RETURN_IF_ERROR(sub_iterator->set_access_paths(sub_all_access_paths_of_this, |
1655 | 19.8k | sub_predicate_access_paths_of_this)); |
1656 | 19.8k | } |
1657 | 4.65k | return Status::OK(); |
1658 | 4.65k | } |
1659 | | |
1660 | | //////////////////////////////////////////////////////////////////////////////// |
1661 | 100k | Status OffsetFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1662 | 100k | RETURN_IF_ERROR(_offset_iterator->init(opts)); |
1663 | | // allocate peek tmp column once |
1664 | 100k | _peek_tmp_col = ColumnOffset64::create(); |
1665 | 100k | return Status::OK(); |
1666 | 100k | } |
1667 | | |
1668 | 167k | Status OffsetFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1669 | 167k | RETURN_IF_ERROR(_offset_iterator->next_batch(n, dst, has_null)); |
1670 | 167k | return Status::OK(); |
1671 | 167k | } |
1672 | | |
1673 | 319k | Status OffsetFileColumnIterator::_peek_one_offset(ordinal_t* offset) { |
1674 | 319k | if (_offset_iterator->get_current_page()->has_remaining()) { |
1675 | 232k | PageDecoder* offset_page_decoder = _offset_iterator->get_current_page()->data_decoder.get(); |
1676 | 232k | size_t n = 1; |
1677 | 232k | _peek_tmp_col->clear(); |
1678 | 232k | RETURN_IF_ERROR(offset_page_decoder->peek_next_batch(&n, _peek_tmp_col)); // not null |
1679 | 232k | DCHECK(_peek_tmp_col->size() == 1); |
1680 | 232k | *offset = |
1681 | 232k | assert_cast<const ColumnOffset64*, TypeCheckOnRelease::DISABLE>(_peek_tmp_col.get()) |
1682 | 232k | ->get_element(0); |
1683 | 232k | } else { |
1684 | 87.0k | *offset = _offset_iterator->get_current_page()->next_array_item_ordinal; |
1685 | 87.0k | } |
1686 | 319k | return Status::OK(); |
1687 | 319k | } |
1688 | | |
1689 | 52.5k | Status OffsetFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1690 | 52.5k | return _offset_iterator->init_prefetcher(params); |
1691 | 52.5k | } |
1692 | | |
1693 | | void OffsetFileColumnIterator::collect_prefetchers( |
1694 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1695 | 52.3k | PrefetcherInitMethod init_method) { |
1696 | 52.3k | _offset_iterator->collect_prefetchers(prefetchers, init_method); |
1697 | 52.3k | } |
1698 | | |
1699 | | /** |
1700 | | * first_storage_offset read from page should smaller than next_storage_offset which here call _peek_one_offset from page, |
1701 | | and first_column_offset is keep in memory data which is different dimension with (first_storage_offset and next_storage_offset) |
1702 | | eg. step1. read page: first_storage_offset = 16382 |
1703 | | step2. read page below with _peek_one_offset(&last_offset): last_offset = 16387 |
1704 | | step3. first_offset = 126 which is calculate in column offsets |
1705 | | for loop column offsets element in size |
1706 | | we can calculate from first_storage_offset to next_storage_offset one by one to fill with offsets_data in memory column offsets |
1707 | | * @param start |
1708 | | * @param column_offsets |
1709 | | * @return |
1710 | | */ |
1711 | | Status OffsetFileColumnIterator::_calculate_offsets(ssize_t start, |
1712 | 153k | ColumnArray::ColumnOffsets& column_offsets) { |
1713 | 153k | ordinal_t next_storage_offset = 0; |
1714 | 153k | RETURN_IF_ERROR(_peek_one_offset(&next_storage_offset)); |
1715 | | |
1716 | | // calculate real offsets |
1717 | 153k | auto& offsets_data = column_offsets.get_data(); |
1718 | 153k | ordinal_t first_column_offset = offsets_data[start - 1]; // -1 is valid |
1719 | 153k | ordinal_t first_storage_offset = offsets_data[start]; |
1720 | 153k | DCHECK(next_storage_offset >= first_storage_offset); |
1721 | 17.6M | for (ssize_t i = start; i < offsets_data.size() - 1; ++i) { |
1722 | 17.4M | offsets_data[i] = first_column_offset + (offsets_data[i + 1] - first_storage_offset); |
1723 | 17.4M | } |
1724 | | // last offset |
1725 | 153k | offsets_data[offsets_data.size() - 1] = |
1726 | 153k | first_column_offset + (next_storage_offset - first_storage_offset); |
1727 | 153k | return Status::OK(); |
1728 | 153k | } |
1729 | | |
1730 | | //////////////////////////////////////////////////////////////////////////////// |
1731 | | ArrayFileColumnIterator::ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
1732 | | OffsetFileColumnIteratorUPtr offset_reader, |
1733 | | ColumnIteratorUPtr item_iterator, |
1734 | | ColumnIteratorUPtr null_iterator) |
1735 | 64.2k | : _array_reader(reader), |
1736 | 64.2k | _offset_iterator(std::move(offset_reader)), |
1737 | 64.2k | _item_iterator(std::move(item_iterator)) { |
1738 | 64.2k | if (_array_reader->is_nullable()) { |
1739 | 46.2k | _null_iterator = std::move(null_iterator); |
1740 | 46.2k | } |
1741 | 64.2k | } |
1742 | | |
1743 | 64.0k | Status ArrayFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1744 | 64.0k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1745 | 56 | DLOG(INFO) << "Array column iterator column " << _column_name << " skip readking."; |
1746 | 56 | return Status::OK(); |
1747 | 56 | } |
1748 | | |
1749 | 63.9k | RETURN_IF_ERROR(_offset_iterator->init(opts)); |
1750 | 63.9k | RETURN_IF_ERROR(_item_iterator->init(opts)); |
1751 | 63.9k | if (_array_reader->is_nullable()) { |
1752 | 46.2k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
1753 | 46.2k | } |
1754 | 63.9k | return Status::OK(); |
1755 | 63.9k | } |
1756 | | |
1757 | 133k | Status ArrayFileColumnIterator::_seek_by_offsets(ordinal_t ord) { |
1758 | 133k | if (read_offset_only()) { |
1759 | | // In OFFSET_ONLY mode, item iterator is SKIP_READING, no need to seek it |
1760 | 1.25k | return Status::OK(); |
1761 | 1.25k | } |
1762 | | // using offsets info |
1763 | 132k | ordinal_t offset = 0; |
1764 | 132k | RETURN_IF_ERROR(_offset_iterator->_peek_one_offset(&offset)); |
1765 | 132k | RETURN_IF_ERROR(_item_iterator->seek_to_ordinal(offset)); |
1766 | 132k | return Status::OK(); |
1767 | 132k | } |
1768 | | |
1769 | 133k | Status ArrayFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
1770 | 133k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1771 | 0 | DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading."; |
1772 | 0 | return Status::OK(); |
1773 | 0 | } |
1774 | | |
1775 | 133k | if (read_null_map_only()) { |
1776 | | // In NULL_MAP_ONLY mode, only seek the null iterator; skip offset and item iterators |
1777 | 2 | if (_array_reader->is_nullable() && _null_iterator) { |
1778 | 2 | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1779 | 2 | } |
1780 | 2 | return Status::OK(); |
1781 | 2 | } |
1782 | | |
1783 | 133k | RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord)); |
1784 | 133k | if (_array_reader->is_nullable()) { |
1785 | 111k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1786 | 111k | } |
1787 | 133k | return _seek_by_offsets(ord); |
1788 | 133k | } |
1789 | | |
1790 | 133k | Status ArrayFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1791 | 133k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1792 | 0 | DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading."; |
1793 | 0 | dst->insert_many_defaults(*n); |
1794 | 0 | return Status::OK(); |
1795 | 0 | } |
1796 | | |
1797 | 133k | if (read_null_map_only()) { |
1798 | | // NULL_MAP_ONLY mode: read null map, fill nested ColumnArray with empty defaults |
1799 | 2 | DORIS_CHECK(is_column_nullable(*dst)); |
1800 | 2 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
1801 | 2 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1802 | 2 | size_t num_read = *n; |
1803 | 2 | if (_null_iterator) { |
1804 | 2 | bool null_signs_has_null = false; |
1805 | 2 | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1806 | 2 | RETURN_IF_ERROR( |
1807 | 2 | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1808 | 2 | } else { |
1809 | | // schema-change: column became nullable but old segment has no null data |
1810 | 0 | null_map_ptr->insert_many_vals(0, num_read); |
1811 | 0 | } |
1812 | 2 | DCHECK(num_read == *n); |
1813 | | // fill nested ColumnArray with empty (zero-length) arrays |
1814 | 2 | auto& column_array = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>( |
1815 | 2 | nullable_col.get_nested_column()); |
1816 | 2 | column_array.insert_many_defaults(num_read); |
1817 | 2 | *has_null = true; |
1818 | 2 | return Status::OK(); |
1819 | 2 | } |
1820 | | |
1821 | 133k | auto& column_array = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>( |
1822 | 133k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1823 | 133k | : *dst); |
1824 | | |
1825 | 133k | bool offsets_has_null = false; |
1826 | 133k | auto column_offsets_ptr = std::move(*column_array.get_offsets_ptr()).mutate(); |
1827 | 133k | Defer defer_offsets {[&] { |
1828 | 133k | auto typed_column_offsets_ptr = ColumnArray::ColumnOffsets::cast_to_column_mutptr( |
1829 | 133k | assert_cast<ColumnArray::ColumnOffsets*, TypeCheckOnRelease::DISABLE>( |
1830 | 133k | column_offsets_ptr.get())); |
1831 | 133k | column_offsets_ptr = nullptr; |
1832 | 133k | column_array.get_offsets_ptr() = std::move(typed_column_offsets_ptr); |
1833 | 133k | }}; |
1834 | 133k | ssize_t start = column_offsets_ptr->size(); |
1835 | 133k | RETURN_IF_ERROR(_offset_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null)); |
1836 | 133k | if (*n == 0) { |
1837 | 0 | return Status::OK(); |
1838 | 0 | } |
1839 | 133k | auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr); |
1840 | 133k | RETURN_IF_ERROR(_offset_iterator->_calculate_offsets(start, column_offsets)); |
1841 | 133k | size_t num_items = |
1842 | 133k | column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid |
1843 | 133k | if (num_items > 0) { |
1844 | 95.0k | auto column_items_ptr = IColumn::mutate(std::move(column_array.get_data_ptr())); |
1845 | 95.0k | Defer defer_items {[&] { column_array.get_data_ptr() = std::move(column_items_ptr); }}; |
1846 | 95.0k | if (read_offset_only()) { |
1847 | | // OFFSET_ONLY mode: skip reading actual item data, fill with defaults |
1848 | 1.01k | column_items_ptr->insert_many_defaults(num_items); |
1849 | 93.9k | } else { |
1850 | 93.9k | size_t num_read = num_items; |
1851 | 93.9k | bool items_has_null = false; |
1852 | 93.9k | RETURN_IF_ERROR( |
1853 | 93.9k | _item_iterator->next_batch(&num_read, column_items_ptr, &items_has_null)); |
1854 | 93.9k | DCHECK(num_read == num_items); |
1855 | 93.9k | } |
1856 | 95.0k | } |
1857 | | |
1858 | 133k | if (is_column_nullable(*dst)) { |
1859 | 111k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1860 | 111k | size_t num_read = *n; |
1861 | | // in not-null to null linked-schemachange mode, |
1862 | | // actually we do not change dat data include meta in footer, |
1863 | | // so may dst from changed meta which is nullable but old data is not nullable, |
1864 | | // if so, we should set null_map to all null by default |
1865 | 111k | if (_null_iterator) { |
1866 | 111k | bool null_signs_has_null = false; |
1867 | 111k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1868 | 111k | RETURN_IF_ERROR( |
1869 | 111k | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1870 | 18.4E | } else { |
1871 | 18.4E | null_map_ptr->insert_many_vals(0, num_read); |
1872 | 18.4E | } |
1873 | 111k | DCHECK(num_read == *n); |
1874 | 111k | } |
1875 | | |
1876 | 133k | return Status::OK(); |
1877 | 133k | } |
1878 | | |
1879 | 45.9k | Status ArrayFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1880 | 45.9k | RETURN_IF_ERROR(_offset_iterator->init_prefetcher(params)); |
1881 | 45.9k | RETURN_IF_ERROR(_item_iterator->init_prefetcher(params)); |
1882 | 45.9k | if (_array_reader->is_nullable()) { |
1883 | 29.9k | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
1884 | 29.9k | } |
1885 | 45.9k | return Status::OK(); |
1886 | 45.9k | } |
1887 | | |
1888 | | void ArrayFileColumnIterator::collect_prefetchers( |
1889 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1890 | 45.8k | PrefetcherInitMethod init_method) { |
1891 | 45.8k | _offset_iterator->collect_prefetchers(prefetchers, init_method); |
1892 | | // the actual data pages to read of item column depends on the read result of offset column, |
1893 | | // so we can't init prefetch blocks according to rowids, just prefetch all data blocks here. |
1894 | 45.8k | _item_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
1895 | 45.8k | if (_array_reader->is_nullable()) { |
1896 | 29.8k | _null_iterator->collect_prefetchers(prefetchers, init_method); |
1897 | 29.8k | } |
1898 | 45.8k | } |
1899 | | |
1900 | | Status ArrayFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
1901 | 33.3k | MutableColumnPtr& dst) { |
1902 | 33.3k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
1903 | 0 | DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading."; |
1904 | 0 | dst->insert_many_defaults(count); |
1905 | 0 | return Status::OK(); |
1906 | 0 | } |
1907 | | |
1908 | 141k | for (size_t i = 0; i < count; ++i) { |
1909 | | // TODO(cambyszju): now read array one by one, need optimize later |
1910 | 107k | RETURN_IF_ERROR(seek_to_ordinal(rowids[i])); |
1911 | 107k | size_t num_read = 1; |
1912 | 107k | RETURN_IF_ERROR(next_batch(&num_read, dst)); |
1913 | 107k | } |
1914 | 33.3k | return Status::OK(); |
1915 | 33.3k | } |
1916 | | |
1917 | 49.4k | void ArrayFileColumnIterator::set_need_to_read() { |
1918 | 49.4k | set_reading_flag(ReadingFlag::NEED_TO_READ); |
1919 | 49.4k | _item_iterator->set_need_to_read(); |
1920 | 49.4k | } |
1921 | | |
1922 | 51.1k | void ArrayFileColumnIterator::remove_pruned_sub_iterators() { |
1923 | 51.1k | _item_iterator->remove_pruned_sub_iterators(); |
1924 | 51.1k | } |
1925 | | |
1926 | | Status ArrayFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths, |
1927 | 50.5k | const TColumnAccessPaths& predicate_access_paths) { |
1928 | 50.5k | if (all_access_paths.empty()) { |
1929 | 1.75k | return Status::OK(); |
1930 | 1.75k | } |
1931 | | |
1932 | 48.8k | if (!predicate_access_paths.empty()) { |
1933 | 2.70k | set_reading_flag(ReadingFlag::READING_FOR_PREDICATE); |
1934 | 2.70k | DLOG(INFO) << "Array column iterator set sub-column " << _column_name |
1935 | 2.70k | << " to READING_FOR_PREDICATE"; |
1936 | 2.70k | } |
1937 | | |
1938 | 48.8k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
1939 | 48.8k | auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths)); |
1940 | | |
1941 | | // Check for meta-only modes (OFFSET_ONLY or NULL_MAP_ONLY) |
1942 | 48.8k | _check_and_set_meta_read_mode(sub_all_access_paths); |
1943 | 48.8k | if (read_offset_only()) { |
1944 | 1.18k | _item_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1945 | 1.18k | DLOG(INFO) << "Array column iterator set column " << _column_name |
1946 | 1.18k | << " to OFFSET_ONLY reading mode, item column set to SKIP_READING"; |
1947 | 1.18k | return Status::OK(); |
1948 | 1.18k | } |
1949 | 47.6k | if (read_null_map_only()) { |
1950 | 12 | _item_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1951 | 12 | DLOG(INFO) << "Array column iterator set column " << _column_name |
1952 | 12 | << " to NULL_MAP_ONLY reading mode, item column set to SKIP_READING"; |
1953 | 12 | return Status::OK(); |
1954 | 12 | } |
1955 | | |
1956 | 47.6k | const auto no_sub_column_to_skip = sub_all_access_paths.empty(); |
1957 | 47.6k | const auto no_predicate_sub_column = sub_predicate_access_paths.empty(); |
1958 | | |
1959 | 47.6k | if (!no_sub_column_to_skip) { |
1960 | 3.60k | for (auto& path : sub_all_access_paths) { |
1961 | 3.60k | if (path.data_access_path.path[0] == ACCESS_ALL) { |
1962 | 3.59k | path.data_access_path.path[0] = _item_iterator->column_name(); |
1963 | 3.59k | } |
1964 | 3.60k | } |
1965 | 3.58k | } |
1966 | | |
1967 | 47.6k | if (!no_predicate_sub_column) { |
1968 | 202 | for (auto& path : sub_predicate_access_paths) { |
1969 | 202 | if (path.data_access_path.path[0] == ACCESS_ALL) { |
1970 | 202 | path.data_access_path.path[0] = _item_iterator->column_name(); |
1971 | 202 | } |
1972 | 202 | } |
1973 | 202 | } |
1974 | | |
1975 | 47.6k | if (!no_sub_column_to_skip || !no_predicate_sub_column) { |
1976 | 3.60k | _item_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ); |
1977 | 3.60k | RETURN_IF_ERROR( |
1978 | 3.60k | _item_iterator->set_access_paths(sub_all_access_paths, sub_predicate_access_paths)); |
1979 | 3.60k | } |
1980 | 47.6k | return Status::OK(); |
1981 | 47.6k | } |
1982 | | |
1983 | | //////////////////////////////////////////////////////////////////////////////// |
1984 | | // StringFileColumnIterator implementation |
1985 | | //////////////////////////////////////////////////////////////////////////////// |
1986 | | |
1987 | | StringFileColumnIterator::StringFileColumnIterator(std::shared_ptr<ColumnReader> reader) |
1988 | 16.7M | : FileColumnIterator(std::move(reader)) {} |
1989 | | |
1990 | 16.7M | Status StringFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1991 | 16.7M | if (read_offset_only()) { |
1992 | | // Propagate only_read_offsets to the FileColumnIterator's options |
1993 | 157 | auto modified_opts = opts; |
1994 | 157 | modified_opts.only_read_offsets = true; |
1995 | 157 | return FileColumnIterator::init(modified_opts); |
1996 | 157 | } |
1997 | 16.7M | return FileColumnIterator::init(opts); |
1998 | 16.7M | } |
1999 | | |
2000 | | Status StringFileColumnIterator::set_access_paths( |
2001 | | const TColumnAccessPaths& all_access_paths, |
2002 | 4.84k | const TColumnAccessPaths& predicate_access_paths) { |
2003 | 4.84k | if (all_access_paths.empty()) { |
2004 | 3.74k | return Status::OK(); |
2005 | 3.74k | } |
2006 | | |
2007 | 1.09k | if (!predicate_access_paths.empty()) { |
2008 | 193 | set_reading_flag(ReadingFlag::READING_FOR_PREDICATE); |
2009 | 193 | } |
2010 | | |
2011 | | // Strip the column name from path[0] before checking for meta-only modes. |
2012 | | // Raw paths look like ["col_name", "OFFSET"] or ["col_name", "NULL"]. |
2013 | 1.09k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
2014 | 1.09k | _check_and_set_meta_read_mode(sub_all_access_paths); |
2015 | | // OFFSET_ONLY mode is fundamentally incompatible with CHAR columns: |
2016 | | // CHAR is stored padded to its declared length (see |
2017 | | // OlapColumnDataConvertorChar::clone_and_padding), so the per-row length |
2018 | | // recorded in dict word info / page headers is always the padded length |
2019 | | // (e.g. 25 for CHAR(25)) — never the logical length expected by length(). |
2020 | | // Recovering the logical length requires scanning the chars buffer with |
2021 | | // strnlen() (shrink_padding_chars), which OFFSET_ONLY by definition skips. |
2022 | | // There is no partial-benefit path: any optimization that still produces |
2023 | | // the correct length() result must read the chars buffer in full. |
2024 | | // |
2025 | | // FE (NestedColumnPruning) already filters CHAR slots out of the |
2026 | | // OFFSET-only access plan, so reaching this branch means an FE/BE |
2027 | | // contract violation. Fail loudly instead of silently falling back. |
2028 | 1.09k | if (read_offset_only() && get_reader() != nullptr && |
2029 | 1.09k | get_reader()->get_meta_type() == FieldType::OLAP_FIELD_TYPE_CHAR) { |
2030 | 0 | return Status::InternalError( |
2031 | 0 | "OFFSET_ONLY access path is not supported on CHAR column '{}': CHAR is stored " |
2032 | 0 | "padded so the per-row length information available without reading the chars " |
2033 | 0 | "buffer is always the padded length, not the logical length. The FE planner " |
2034 | 0 | "must not emit an OFFSET access path for CHAR columns.", |
2035 | 0 | _column_name); |
2036 | 0 | } |
2037 | 1.09k | if (read_offset_only()) { |
2038 | 156 | DLOG(INFO) << "String column iterator set column " << _column_name |
2039 | 156 | << " to OFFSET_ONLY reading mode"; |
2040 | 940 | } else if (read_null_map_only()) { |
2041 | 213 | DLOG(INFO) << "String column iterator set column " << _column_name |
2042 | 213 | << " to NULL_MAP_ONLY reading mode"; |
2043 | 213 | } |
2044 | | |
2045 | 1.09k | return Status::OK(); |
2046 | 1.09k | } |
2047 | | |
2048 | | //////////////////////////////////////////////////////////////////////////////// |
2049 | | |
2050 | 27.8M | FileColumnIterator::FileColumnIterator(std::shared_ptr<ColumnReader> reader) : _reader(reader) {} |
2051 | | |
2052 | 55.5k | void ColumnIterator::_check_and_set_meta_read_mode(const TColumnAccessPaths& sub_all_access_paths) { |
2053 | 55.5k | for (const auto& path : sub_all_access_paths) { |
2054 | 6.74k | if (!path.data_access_path.path.empty()) { |
2055 | 6.74k | if (StringCaseEqual()(path.data_access_path.path[0], ACCESS_OFFSET)) { |
2056 | 1.80k | _read_mode = ReadMode::OFFSET_ONLY; |
2057 | 1.80k | return; |
2058 | 4.93k | } else if (StringCaseEqual()(path.data_access_path.path[0], ACCESS_NULL)) { |
2059 | 244 | _read_mode = ReadMode::NULL_MAP_ONLY; |
2060 | 244 | return; |
2061 | 244 | } |
2062 | 6.74k | } |
2063 | 6.70k | } |
2064 | 53.5k | _read_mode = ReadMode::DEFAULT; |
2065 | 53.5k | } |
2066 | | |
2067 | 27.7M | Status FileColumnIterator::init(const ColumnIteratorOptions& opts) { |
2068 | 27.7M | if (_reading_flag == ReadingFlag::SKIP_READING) { |
2069 | 2.38k | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2070 | 2.38k | return Status::OK(); |
2071 | 2.38k | } |
2072 | | |
2073 | 27.7M | _opts = opts; |
2074 | 27.7M | if (!_opts.use_page_cache) { |
2075 | 27.7M | _reader->disable_index_meta_cache(); |
2076 | 27.7M | } |
2077 | 27.7M | RETURN_IF_ERROR(get_block_compression_codec(_reader->get_compression(), &_compress_codec)); |
2078 | 27.7M | if (config::enable_low_cardinality_optimize && |
2079 | 27.8M | opts.io_ctx.reader_type == ReaderType::READER_QUERY && |
2080 | 27.7M | _reader->encoding_info()->encoding() == DICT_ENCODING) { |
2081 | 16.5M | auto dict_encoding_type = _reader->get_dict_encoding_type(); |
2082 | | // Only if the column is a predicate column, then we need check the all dict encoding flag |
2083 | | // because we could rewrite the predciate to accelarate query speed. But if it is not a |
2084 | | // predicate column, then it is useless. And it has a bad impact on cold read(first time read) |
2085 | | // because it will load the column's ordinal index and zonemap index and maybe other indices. |
2086 | | // it has bad impact on primary key query. For example, select * from table where pk = 1, and |
2087 | | // the table has 2000 columns. |
2088 | 16.5M | if (dict_encoding_type == ColumnReader::UNKNOWN_DICT_ENCODING && opts.is_predicate_column) { |
2089 | 5.78k | RETURN_IF_ERROR(seek_to_ordinal(_reader->num_rows() - 1)); |
2090 | 5.78k | _is_all_dict_encoding = _page.is_dict_encoding; |
2091 | 5.78k | _reader->set_dict_encoding_type(_is_all_dict_encoding |
2092 | 5.78k | ? ColumnReader::ALL_DICT_ENCODING |
2093 | 5.78k | : ColumnReader::PARTIAL_DICT_ENCODING); |
2094 | 16.5M | } else { |
2095 | 16.5M | _is_all_dict_encoding = dict_encoding_type == ColumnReader::ALL_DICT_ENCODING; |
2096 | 16.5M | } |
2097 | 16.5M | } |
2098 | 27.7M | return Status::OK(); |
2099 | 27.7M | } |
2100 | | |
2101 | 27.9M | FileColumnIterator::~FileColumnIterator() = default; |
2102 | | |
2103 | 1.10M | void FileColumnIterator::_trigger_prefetch_if_eligible(ordinal_t ord) { |
2104 | 1.10M | std::vector<BlockRange> ranges; |
2105 | 1.10M | if (_prefetcher->need_prefetch(cast_set<uint32_t>(ord), &ranges)) { |
2106 | 848k | for (const auto& range : ranges) { |
2107 | 848k | _cached_remote_file_reader->prefetch_range(range.offset, range.size, &_opts.io_ctx); |
2108 | 848k | } |
2109 | 848k | } |
2110 | 1.10M | } |
2111 | | |
2112 | 3.42M | Status FileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
2113 | 3.42M | if (_reading_flag == ReadingFlag::SKIP_READING) { |
2114 | 451 | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2115 | 451 | return Status::OK(); |
2116 | 451 | } |
2117 | | |
2118 | 18.4E | LOG_IF(INFO, config::enable_segment_prefetch_verbose_log) << fmt::format( |
2119 | 18.4E | "[verbose] FileColumnIterator::seek_to_ordinal seek to ordinal {}, enable_prefetch={}", |
2120 | 18.4E | ord, _enable_prefetch); |
2121 | 3.42M | if (_enable_prefetch) { |
2122 | 1.10M | _trigger_prefetch_if_eligible(ord); |
2123 | 1.10M | } |
2124 | | |
2125 | | // if current page contains this row, we don't need to seek |
2126 | 3.42M | if (!_page || !_page.contains(ord) || !_page_iter.valid()) { |
2127 | 1.59M | RETURN_IF_ERROR(_reader->seek_at_or_before(ord, &_page_iter, _opts)); |
2128 | 1.59M | RETURN_IF_ERROR(_read_data_page(_page_iter)); |
2129 | 1.59M | } |
2130 | 3.42M | RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, ord - _page.first_ordinal)); |
2131 | 3.42M | _current_ordinal = ord; |
2132 | 3.42M | return Status::OK(); |
2133 | 3.42M | } |
2134 | | |
2135 | 0 | Status FileColumnIterator::seek_to_page_start() { |
2136 | 0 | return seek_to_ordinal(_page.first_ordinal); |
2137 | 0 | } |
2138 | | |
2139 | 3.49M | Status FileColumnIterator::_seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const { |
2140 | 3.49M | if (page->offset_in_page == offset_in_page) { |
2141 | | // fast path, do nothing |
2142 | 1.91M | return Status::OK(); |
2143 | 1.91M | } |
2144 | | |
2145 | 1.57M | ordinal_t pos_in_data = offset_in_page; |
2146 | 1.57M | if (_page.has_null) { |
2147 | 107k | ordinal_t offset_in_data = 0; |
2148 | 107k | ordinal_t skips = offset_in_page; |
2149 | | |
2150 | 107k | if (offset_in_page > page->offset_in_page) { |
2151 | | // forward, reuse null bitmap |
2152 | 67.2k | skips = offset_in_page - page->offset_in_page; |
2153 | 67.2k | offset_in_data = page->data_decoder->current_index(); |
2154 | 67.2k | } else { |
2155 | | // rewind null bitmap, and |
2156 | 39.8k | page->null_decoder = RleDecoder<bool>((const uint8_t*)page->null_bitmap.data, |
2157 | 39.8k | cast_set<int>(page->null_bitmap.size), 1); |
2158 | 39.8k | } |
2159 | | |
2160 | 107k | auto skip_nulls = page->null_decoder.Skip(skips); |
2161 | 107k | pos_in_data = offset_in_data + skips - skip_nulls; |
2162 | 107k | } |
2163 | | |
2164 | 1.57M | RETURN_IF_ERROR(page->data_decoder->seek_to_position_in_page(pos_in_data)); |
2165 | 1.57M | page->offset_in_page = offset_in_page; |
2166 | 1.57M | return Status::OK(); |
2167 | 1.57M | } |
2168 | | |
2169 | 20.0k | Status FileColumnIterator::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) { |
2170 | 20.0k | return _reader->next_batch_of_zone_map(n, dst); |
2171 | 20.0k | } |
2172 | | |
2173 | 2.60M | Status FileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2174 | 2.60M | if (read_null_map_only()) { |
2175 | 2.95k | DLOG(INFO) << "File column iterator column " << _column_name |
2176 | 2.95k | << " in NULL_MAP_ONLY mode, reading only null map."; |
2177 | 2.95k | DORIS_CHECK(is_column_nullable(*dst)); |
2178 | 2.95k | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
2179 | 2.95k | auto& null_map_data = nullable_col.get_null_map_data(); |
2180 | | |
2181 | 2.95k | size_t remaining = *n; |
2182 | 2.95k | *has_null = false; |
2183 | 5.96k | while (remaining > 0) { |
2184 | 3.01k | if (!_page.has_remaining()) { |
2185 | 51 | bool eos = false; |
2186 | 51 | RETURN_IF_ERROR(_load_next_page(&eos)); |
2187 | 51 | if (eos) { |
2188 | 0 | break; |
2189 | 0 | } |
2190 | 51 | } |
2191 | | |
2192 | 3.01k | size_t nrows_in_page = std::min(remaining, _page.remaining()); |
2193 | 3.01k | size_t nrows_to_read = nrows_in_page; |
2194 | 3.01k | if (_page.has_null) { |
2195 | 198k | while (nrows_to_read > 0) { |
2196 | 195k | bool is_null = false; |
2197 | 195k | size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read); |
2198 | 195k | const size_t cur_size = null_map_data.size(); |
2199 | 195k | null_map_data.resize(cur_size + this_run); |
2200 | 195k | memset(null_map_data.data() + cur_size, is_null ? 1 : 0, this_run); |
2201 | 195k | if (is_null) { |
2202 | 102k | *has_null = true; |
2203 | 102k | } |
2204 | 195k | nrows_to_read -= this_run; |
2205 | 195k | _page.offset_in_page += this_run; |
2206 | 195k | _current_ordinal += this_run; |
2207 | 195k | } |
2208 | 2.93k | } else { |
2209 | 79 | const size_t cur_size = null_map_data.size(); |
2210 | 79 | null_map_data.resize(cur_size + nrows_to_read); |
2211 | 79 | memset(null_map_data.data() + cur_size, 0, nrows_to_read); |
2212 | 79 | _page.offset_in_page += nrows_to_read; |
2213 | 79 | _current_ordinal += nrows_to_read; |
2214 | 79 | } |
2215 | 3.01k | remaining -= nrows_in_page; |
2216 | 3.01k | } |
2217 | 2.95k | *n -= remaining; |
2218 | 2.95k | nullable_col.get_nested_column().insert_many_defaults(*n); |
2219 | 2.95k | return Status::OK(); |
2220 | 2.95k | } |
2221 | | |
2222 | 2.59M | if (_reading_flag == ReadingFlag::SKIP_READING) { |
2223 | 451 | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2224 | 451 | dst->insert_many_defaults(*n); |
2225 | 451 | return Status::OK(); |
2226 | 451 | } |
2227 | | |
2228 | 2.59M | size_t curr_size = dst->byte_size(); |
2229 | 2.59M | dst->reserve(*n); |
2230 | 2.59M | size_t remaining = *n; |
2231 | 2.59M | *has_null = false; |
2232 | 5.26M | while (remaining > 0) { |
2233 | 2.65M | if (!_page.has_remaining()) { |
2234 | 61.6k | bool eos = false; |
2235 | 61.6k | RETURN_IF_ERROR(_load_next_page(&eos)); |
2236 | 61.6k | if (eos) { |
2237 | 0 | break; |
2238 | 0 | } |
2239 | 61.6k | } |
2240 | | |
2241 | | // number of rows to be read from this page |
2242 | 2.65M | size_t nrows_in_page = std::min(remaining, _page.remaining()); |
2243 | 2.65M | size_t nrows_to_read = nrows_in_page; |
2244 | 2.65M | if (_page.has_null) { |
2245 | 881k | while (nrows_to_read > 0) { |
2246 | 675k | bool is_null = false; |
2247 | 675k | size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read); |
2248 | | // we use num_rows only for CHECK |
2249 | 675k | size_t num_rows = this_run; |
2250 | 675k | if (!is_null) { |
2251 | 362k | RETURN_IF_ERROR(_page.data_decoder->next_batch(&num_rows, dst)); |
2252 | 362k | DCHECK_EQ(this_run, num_rows); |
2253 | 362k | } else { |
2254 | 313k | *has_null = true; |
2255 | 313k | auto* null_col = check_and_get_column<ColumnNullable>(dst.get()); |
2256 | 316k | if (null_col != nullptr) { |
2257 | 316k | null_col->insert_many_defaults(this_run); |
2258 | 18.4E | } else { |
2259 | 18.4E | return Status::InternalError("unexpected column type in column reader"); |
2260 | 18.4E | } |
2261 | 313k | } |
2262 | | |
2263 | 678k | nrows_to_read -= this_run; |
2264 | 678k | _page.offset_in_page += this_run; |
2265 | 678k | _current_ordinal += this_run; |
2266 | 678k | } |
2267 | 2.45M | } else { |
2268 | 2.45M | RETURN_IF_ERROR(_page.data_decoder->next_batch(&nrows_to_read, dst)); |
2269 | 2.45M | DCHECK_EQ(nrows_to_read, nrows_in_page); |
2270 | | |
2271 | 2.45M | _page.offset_in_page += nrows_to_read; |
2272 | 2.45M | _current_ordinal += nrows_to_read; |
2273 | 2.45M | } |
2274 | 2.66M | remaining -= nrows_in_page; |
2275 | 2.66M | } |
2276 | 2.60M | *n -= remaining; |
2277 | 2.60M | _opts.stats->bytes_read += (dst->byte_size() - curr_size) + BitmapSize(*n); |
2278 | | |
2279 | | #ifdef BE_TEST |
2280 | | _reader->check_data_by_zone_map_for_test(dst); |
2281 | | #endif |
2282 | 2.60M | return Status::OK(); |
2283 | 2.59M | } |
2284 | | |
2285 | | Status FileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
2286 | 773k | MutableColumnPtr& dst) { |
2287 | 773k | if (read_null_map_only()) { |
2288 | 21 | DLOG(INFO) << "File column iterator column " << _column_name |
2289 | 21 | << " in NULL_MAP_ONLY mode, reading only null map by rowids."; |
2290 | | |
2291 | 21 | DORIS_CHECK(is_column_nullable(*dst)); |
2292 | 21 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
2293 | 21 | auto& null_map_data = nullable_col.get_null_map_data(); |
2294 | 21 | const size_t base_size = null_map_data.size(); |
2295 | 21 | null_map_data.resize(base_size + count); |
2296 | | |
2297 | 21 | size_t remaining = count; |
2298 | 21 | size_t total_read_count = 0; |
2299 | 21 | size_t nrows_to_read = 0; |
2300 | 42 | while (remaining > 0) { |
2301 | 21 | RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count])); |
2302 | | |
2303 | 21 | nrows_to_read = std::min(remaining, _page.remaining()); |
2304 | | |
2305 | 21 | if (_page.has_null) { |
2306 | 4 | size_t already_read = 0; |
2307 | 8 | while ((nrows_to_read - already_read) > 0) { |
2308 | 4 | bool is_null = false; |
2309 | 4 | size_t this_run = std::min(nrows_to_read - already_read, _page.remaining()); |
2310 | 4 | if (UNLIKELY(this_run == 0)) { |
2311 | 0 | break; |
2312 | 0 | } |
2313 | 4 | this_run = _page.null_decoder.GetNextRun(&is_null, this_run); |
2314 | | |
2315 | 4 | size_t offset = total_read_count + already_read; |
2316 | 4 | size_t this_read_count = 0; |
2317 | 4 | rowid_t current_ordinal_in_page = |
2318 | 4 | cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal); |
2319 | 8 | for (size_t i = 0; i < this_run; ++i) { |
2320 | 4 | if (rowids[offset + i] - current_ordinal_in_page >= this_run) { |
2321 | 0 | break; |
2322 | 0 | } |
2323 | 4 | this_read_count++; |
2324 | 4 | } |
2325 | | |
2326 | 4 | if (this_read_count > 0) { |
2327 | 4 | memset(null_map_data.data() + base_size + offset, is_null ? 1 : 0, |
2328 | 4 | this_read_count); |
2329 | 4 | } |
2330 | | |
2331 | 4 | already_read += this_read_count; |
2332 | 4 | _page.offset_in_page += this_run; |
2333 | 4 | } |
2334 | | |
2335 | 4 | nrows_to_read = already_read; |
2336 | 4 | total_read_count += nrows_to_read; |
2337 | 4 | remaining -= nrows_to_read; |
2338 | 17 | } else { |
2339 | 17 | memset(null_map_data.data() + base_size + total_read_count, 0, nrows_to_read); |
2340 | 17 | total_read_count += nrows_to_read; |
2341 | 17 | remaining -= nrows_to_read; |
2342 | 17 | } |
2343 | 21 | } |
2344 | | |
2345 | 21 | null_map_data.resize(base_size + total_read_count); |
2346 | 21 | nullable_col.get_nested_column().insert_many_defaults(total_read_count); |
2347 | 21 | return Status::OK(); |
2348 | 21 | } |
2349 | | |
2350 | 773k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
2351 | 0 | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2352 | 0 | dst->insert_many_defaults(count); |
2353 | 0 | return Status::OK(); |
2354 | 0 | } |
2355 | | |
2356 | 773k | size_t remaining = count; |
2357 | 773k | size_t total_read_count = 0; |
2358 | 773k | size_t nrows_to_read = 0; |
2359 | 1.57M | while (remaining > 0) { |
2360 | 803k | RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count])); |
2361 | | |
2362 | | // number of rows to be read from this page |
2363 | 803k | nrows_to_read = std::min(remaining, _page.remaining()); |
2364 | | |
2365 | 803k | if (_page.has_null) { |
2366 | 50.8k | size_t already_read = 0; |
2367 | 3.15M | while ((nrows_to_read - already_read) > 0) { |
2368 | 3.10M | bool is_null = false; |
2369 | 3.10M | size_t this_run = std::min(nrows_to_read - already_read, _page.remaining()); |
2370 | 3.10M | if (UNLIKELY(this_run == 0)) { |
2371 | 171 | break; |
2372 | 171 | } |
2373 | 3.10M | this_run = _page.null_decoder.GetNextRun(&is_null, this_run); |
2374 | 3.10M | size_t offset = total_read_count + already_read; |
2375 | 3.10M | size_t this_read_count = 0; |
2376 | 3.10M | rowid_t current_ordinal_in_page = |
2377 | 3.10M | cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal); |
2378 | 6.20M | for (size_t i = 0; i < this_run; ++i) { |
2379 | 6.14M | if (rowids[offset + i] - current_ordinal_in_page >= this_run) { |
2380 | 3.04M | break; |
2381 | 3.04M | } |
2382 | 3.10M | this_read_count++; |
2383 | 3.10M | } |
2384 | | |
2385 | 3.10M | auto origin_index = _page.data_decoder->current_index(); |
2386 | 3.10M | if (this_read_count > 0) { |
2387 | 69.8k | if (is_null) { |
2388 | 52.7k | auto* null_col = check_and_get_column<ColumnNullable>(dst.get()); |
2389 | 52.7k | if (UNLIKELY(null_col == nullptr)) { |
2390 | 0 | return Status::InternalError("unexpected column type in column reader"); |
2391 | 0 | } |
2392 | | |
2393 | 52.7k | null_col->insert_many_defaults(this_read_count); |
2394 | 52.7k | } else { |
2395 | 17.0k | size_t read_count = this_read_count; |
2396 | | |
2397 | | // ordinal in nullable columns' data buffer maybe be not continuously(the data doesn't contain null value), |
2398 | | // so we need use `page_start_off_in_decoder` to calculate the actual offset in `data_decoder` |
2399 | 17.0k | size_t page_start_off_in_decoder = |
2400 | 17.0k | _page.first_ordinal + _page.offset_in_page - origin_index; |
2401 | 17.0k | RETURN_IF_ERROR(_page.data_decoder->read_by_rowids( |
2402 | 17.0k | &rowids[offset], page_start_off_in_decoder, &read_count, dst)); |
2403 | 17.0k | DCHECK_EQ(read_count, this_read_count); |
2404 | 17.0k | } |
2405 | 69.8k | } |
2406 | | |
2407 | 3.10M | if (!is_null) { |
2408 | 3.05M | RETURN_IF_ERROR( |
2409 | 3.05M | _page.data_decoder->seek_to_position_in_page(origin_index + this_run)); |
2410 | 3.05M | } |
2411 | | |
2412 | 3.10M | already_read += this_read_count; |
2413 | 3.10M | _page.offset_in_page += this_run; |
2414 | 3.10M | DCHECK(_page.offset_in_page <= _page.num_rows); |
2415 | 3.10M | } |
2416 | | |
2417 | 50.8k | nrows_to_read = already_read; |
2418 | 50.8k | total_read_count += nrows_to_read; |
2419 | 50.8k | remaining -= nrows_to_read; |
2420 | 752k | } else { |
2421 | 752k | RETURN_IF_ERROR(_page.data_decoder->read_by_rowids( |
2422 | 752k | &rowids[total_read_count], _page.first_ordinal, &nrows_to_read, dst)); |
2423 | 752k | total_read_count += nrows_to_read; |
2424 | 752k | remaining -= nrows_to_read; |
2425 | 752k | } |
2426 | 803k | } |
2427 | 773k | return Status::OK(); |
2428 | 773k | } |
2429 | | |
2430 | 61.7k | Status FileColumnIterator::_load_next_page(bool* eos) { |
2431 | 61.7k | _page_iter.next(); |
2432 | 61.7k | if (!_page_iter.valid()) { |
2433 | 0 | *eos = true; |
2434 | 0 | return Status::OK(); |
2435 | 0 | } |
2436 | | |
2437 | 61.7k | RETURN_IF_ERROR(_read_data_page(_page_iter)); |
2438 | 61.7k | RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, 0)); |
2439 | 61.7k | *eos = false; |
2440 | 61.7k | return Status::OK(); |
2441 | 61.7k | } |
2442 | | |
2443 | 1.65M | Status FileColumnIterator::_read_data_page(const OrdinalPageIndexIterator& iter) { |
2444 | 1.65M | PageHandle handle; |
2445 | 1.65M | Slice page_body; |
2446 | 1.65M | PageFooterPB footer; |
2447 | 1.65M | _opts.type = DATA_PAGE; |
2448 | 1.65M | PageDecoderOptions decoder_opts; |
2449 | 1.65M | decoder_opts.only_read_offsets = _opts.only_read_offsets; |
2450 | 1.65M | RETURN_IF_ERROR( |
2451 | 1.65M | _reader->read_page(_opts, iter.page(), &handle, &page_body, &footer, _compress_codec)); |
2452 | | // parse data page |
2453 | 1.65M | auto st = ParsedPage::create(std::move(handle), page_body, footer.data_page_footer(), |
2454 | 1.65M | _reader->encoding_info(), iter.page(), iter.page_index(), &_page, |
2455 | 1.65M | decoder_opts); |
2456 | 1.65M | if (!st.ok()) { |
2457 | 0 | LOG(WARNING) << "failed to create ParsedPage, file=" << _opts.file_reader->path().native() |
2458 | 0 | << ", page_offset=" << iter.page().offset << ", page_size=" << iter.page().size |
2459 | 0 | << ", page_index=" << iter.page_index() << ", error=" << st; |
2460 | 0 | return st; |
2461 | 0 | } |
2462 | | |
2463 | | // dictionary page is read when the first data page that uses it is read, |
2464 | | // this is to optimize the memory usage: when there is no query on one column, we could |
2465 | | // release the memory of dictionary page. |
2466 | | // note that concurrent iterators for the same column won't repeatedly read dictionary page |
2467 | | // because of page cache. |
2468 | 1.65M | if (_reader->encoding_info()->encoding() == DICT_ENCODING) { |
2469 | 436k | auto dict_page_decoder = reinterpret_cast<BinaryDictPageDecoder*>(_page.data_decoder.get()); |
2470 | 436k | if (dict_page_decoder->is_dict_encoding()) { |
2471 | 406k | if (_dict_decoder == nullptr) { |
2472 | 392k | RETURN_IF_ERROR(_read_dict_data()); |
2473 | 392k | CHECK_NOTNULL(_dict_decoder); |
2474 | 392k | } |
2475 | | |
2476 | 406k | dict_page_decoder->set_dict_decoder(cast_set<uint32_t>(_dict_decoder->count()), |
2477 | 406k | _dict_word_info.get()); |
2478 | 406k | } |
2479 | 436k | } |
2480 | 1.65M | return Status::OK(); |
2481 | 1.65M | } |
2482 | | |
2483 | 397k | Status FileColumnIterator::_read_dict_data() { |
2484 | 397k | CHECK_EQ(_reader->encoding_info()->encoding(), DICT_ENCODING); |
2485 | | // read dictionary page |
2486 | 397k | Slice dict_data; |
2487 | 397k | PageFooterPB dict_footer; |
2488 | 397k | _opts.type = INDEX_PAGE; |
2489 | | |
2490 | 397k | RETURN_IF_ERROR(_reader->read_page(_opts, _reader->get_dict_page_pointer(), &_dict_page_handle, |
2491 | 397k | &dict_data, &dict_footer, _compress_codec)); |
2492 | 397k | const EncodingInfo* encoding_info; |
2493 | | // The dict pool stores strings of the outer column's type. Using the |
2494 | | // outer type (CHAR vs VARCHAR/STRING) lets the EncodingInfo pick a |
2495 | | // CHAR-strip pre-decoder so the cached dict page is already unpadded. |
2496 | 397k | RETURN_IF_ERROR(EncodingInfo::get(_reader->get_meta_type(), |
2497 | 397k | dict_footer.dict_page_footer().encoding(), &encoding_info)); |
2498 | 397k | RETURN_IF_ERROR(encoding_info->create_page_decoder(dict_data, {}, _dict_decoder)); |
2499 | 397k | RETURN_IF_ERROR(_dict_decoder->init()); |
2500 | | |
2501 | 397k | _dict_word_info.reset(new StringRef[_dict_decoder->count()]); |
2502 | 397k | RETURN_IF_ERROR(_dict_decoder->get_dict_word_info(_dict_word_info.get())); |
2503 | 397k | return Status::OK(); |
2504 | 397k | } |
2505 | | |
2506 | | Status FileColumnIterator::get_row_ranges_by_zone_map( |
2507 | | const AndBlockColumnPredicate* col_predicates, |
2508 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
2509 | 131k | RowRanges* row_ranges) { |
2510 | 131k | if (_reader->has_zone_map()) { |
2511 | 131k | RETURN_IF_ERROR(_reader->get_row_ranges_by_zone_map(col_predicates, delete_predicates, |
2512 | 131k | row_ranges, _opts)); |
2513 | 131k | } |
2514 | 131k | return Status::OK(); |
2515 | 131k | } |
2516 | | |
2517 | | Status FileColumnIterator::get_row_ranges_by_bloom_filter( |
2518 | 131k | const AndBlockColumnPredicate* col_predicates, RowRanges* row_ranges) { |
2519 | 131k | if ((col_predicates->can_do_bloom_filter(false) && _reader->has_bloom_filter_index(false)) || |
2520 | 131k | (col_predicates->can_do_bloom_filter(true) && _reader->has_bloom_filter_index(true))) { |
2521 | 98 | RETURN_IF_ERROR(_reader->get_row_ranges_by_bloom_filter(col_predicates, row_ranges, _opts)); |
2522 | 98 | } |
2523 | 131k | return Status::OK(); |
2524 | 131k | } |
2525 | | |
2526 | | Status FileColumnIterator::get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
2527 | 134k | RowRanges* row_ranges) { |
2528 | 134k | if (!_is_all_dict_encoding) { |
2529 | 122k | return Status::OK(); |
2530 | 122k | } |
2531 | | |
2532 | 11.3k | if (!_dict_decoder) { |
2533 | 5.67k | RETURN_IF_ERROR(_read_dict_data()); |
2534 | 5.67k | CHECK_NOTNULL(_dict_decoder); |
2535 | 5.67k | } |
2536 | | |
2537 | 11.3k | if (!col_predicates->evaluate_and(_dict_word_info.get(), _dict_decoder->count())) { |
2538 | 1.20k | row_ranges->clear(); |
2539 | 1.20k | } |
2540 | 11.3k | return Status::OK(); |
2541 | 11.3k | } |
2542 | | |
2543 | 884k | Status FileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
2544 | 884k | if (_cached_remote_file_reader = |
2545 | 884k | std::dynamic_pointer_cast<io::CachedRemoteFileReader>(_reader->_file_reader); |
2546 | 884k | !_cached_remote_file_reader) { |
2547 | 0 | return Status::OK(); |
2548 | 0 | } |
2549 | 884k | _enable_prefetch = true; |
2550 | 884k | _prefetcher = std::make_unique<SegmentPrefetcher>(params.config); |
2551 | 884k | RETURN_IF_ERROR(_prefetcher->init(_reader, params.read_options)); |
2552 | 884k | return Status::OK(); |
2553 | 884k | } |
2554 | | |
2555 | | void FileColumnIterator::collect_prefetchers( |
2556 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
2557 | 883k | PrefetcherInitMethod init_method) { |
2558 | 883k | if (_prefetcher) { |
2559 | 883k | prefetchers[init_method].emplace_back(_prefetcher.get()); |
2560 | 883k | } |
2561 | 883k | } |
2562 | | |
2563 | 20.4k | Status DefaultValueColumnIterator::init(const ColumnIteratorOptions& opts) { |
2564 | 20.4k | _opts = opts; |
2565 | | // be consistent with segment v1 |
2566 | | // if _has_default_value, we should create default column iterator for this column, and |
2567 | | // "NULL" is a special default value which means the default value is null. |
2568 | 20.4k | if (_has_default_value) { |
2569 | 1.37k | if (_default_value == "NULL") { |
2570 | 603 | _default_value_field = Field::create_field<TYPE_NULL>(Null {}); |
2571 | 769 | } else { |
2572 | 769 | if (_type == FieldType::OLAP_FIELD_TYPE_ARRAY) { |
2573 | 8 | if (_default_value != "[]") { |
2574 | 0 | return Status::NotSupported("Array default {} is unsupported", _default_value); |
2575 | 8 | } else { |
2576 | 8 | _default_value_field = Field::create_field<TYPE_ARRAY>(Array {}); |
2577 | 8 | return Status::OK(); |
2578 | 8 | } |
2579 | 761 | } else if (_type == FieldType::OLAP_FIELD_TYPE_STRUCT) { |
2580 | 0 | return Status::NotSupported("STRUCT default type is unsupported"); |
2581 | 761 | } else if (_type == FieldType::OLAP_FIELD_TYPE_MAP) { |
2582 | 0 | return Status::NotSupported("MAP default type is unsupported"); |
2583 | 0 | } |
2584 | 761 | const auto t = _type; |
2585 | 761 | const auto serde = DataTypeFactory::instance() |
2586 | 761 | .create_data_type(t, _precision, _scale, _len) |
2587 | 761 | ->get_serde(); |
2588 | 761 | RETURN_IF_ERROR(serde->from_fe_string(_default_value, _default_value_field)); |
2589 | 761 | } |
2590 | 19.1k | } else if (_is_nullable) { |
2591 | 19.1k | _default_value_field = Field::create_field<TYPE_NULL>(Null {}); |
2592 | 18.4E | } else { |
2593 | 18.4E | return Status::InternalError( |
2594 | 18.4E | "invalid default value column for no default value and not nullable"); |
2595 | 18.4E | } |
2596 | 20.5k | return Status::OK(); |
2597 | 20.4k | } |
2598 | | |
2599 | 2.67k | Status DefaultValueColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2600 | 2.67k | *has_null = _default_value_field.is_null(); |
2601 | 2.67k | _insert_many_default(dst, *n); |
2602 | 2.67k | return Status::OK(); |
2603 | 2.67k | } |
2604 | | |
2605 | | Status DefaultValueColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
2606 | 5.19k | MutableColumnPtr& dst) { |
2607 | 5.19k | _insert_many_default(dst, count); |
2608 | 5.19k | return Status::OK(); |
2609 | 5.19k | } |
2610 | | |
2611 | 7.86k | void DefaultValueColumnIterator::_insert_many_default(MutableColumnPtr& dst, size_t n) { |
2612 | 7.86k | if (_default_value_field.is_null()) { |
2613 | 7.43k | dst->insert_many_defaults(n); |
2614 | 7.43k | } else { |
2615 | 426 | dst = dst->convert_to_predicate_column_if_dictionary(); |
2616 | 426 | dst->insert_duplicate_fields(_default_value_field, n); |
2617 | 426 | } |
2618 | 7.86k | } |
2619 | | |
2620 | 4.10k | Status RowIdColumnIteratorV2::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2621 | 4.10k | auto* string_column = assert_cast<ColumnString*, TypeCheckOnRelease::DISABLE>(dst.get()); |
2622 | | |
2623 | 11.4M | for (uint32_t i = 0; i < *n; ++i) { |
2624 | 11.4M | uint32_t row_id = _current_rowid + i; |
2625 | 11.4M | GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id); |
2626 | 11.4M | string_column->insert_data(reinterpret_cast<const char*>(&location), |
2627 | 11.4M | sizeof(GlobalRowLoacationV2)); |
2628 | 11.4M | } |
2629 | 4.10k | _current_rowid += *n; |
2630 | 4.10k | return Status::OK(); |
2631 | 4.10k | } |
2632 | | |
2633 | | Status RowIdColumnIteratorV2::read_by_rowids(const rowid_t* rowids, const size_t count, |
2634 | 43.2k | MutableColumnPtr& dst) { |
2635 | 43.2k | auto* string_column = assert_cast<ColumnString*>(dst.get()); |
2636 | | |
2637 | 32.0M | for (size_t i = 0; i < count; ++i) { |
2638 | 32.0M | uint32_t row_id = rowids[i]; |
2639 | 32.0M | GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id); |
2640 | 32.0M | string_column->insert_data(reinterpret_cast<const char*>(&location), |
2641 | 32.0M | sizeof(GlobalRowLoacationV2)); |
2642 | 32.0M | } |
2643 | 43.2k | return Status::OK(); |
2644 | 43.2k | } |
2645 | | |
2646 | | } // namespace doris::segment_v2 |