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 | 639 | inline bool read_as_string(PrimitiveType type) { |
88 | 639 | return type == PrimitiveType::TYPE_STRING || type == PrimitiveType::INVALID_TYPE || |
89 | 639 | type == PrimitiveType::TYPE_BITMAP || type == PrimitiveType::TYPE_FIXED_LENGTH_OBJECT; |
90 | 639 | } |
91 | | |
92 | | Status ColumnReader::create_array(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
93 | | const io::FileReaderSPtr& file_reader, |
94 | 57.1k | std::shared_ptr<ColumnReader>* reader) { |
95 | 57.1k | DCHECK(meta.children_columns_size() == 2 || meta.children_columns_size() == 3); |
96 | | |
97 | 57.1k | std::shared_ptr<ColumnReader> item_reader; |
98 | 57.1k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0), |
99 | 57.1k | meta.children_columns(0).num_rows(), file_reader, |
100 | 57.1k | &item_reader)); |
101 | | |
102 | 57.1k | std::shared_ptr<ColumnReader> offset_reader; |
103 | 57.1k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1), |
104 | 57.1k | meta.children_columns(1).num_rows(), file_reader, |
105 | 57.1k | &offset_reader)); |
106 | | |
107 | 57.1k | std::shared_ptr<ColumnReader> null_reader; |
108 | 57.1k | if (meta.is_nullable()) { |
109 | 41.4k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2), |
110 | 41.4k | meta.children_columns(2).num_rows(), file_reader, |
111 | 41.4k | &null_reader)); |
112 | 41.4k | } |
113 | | |
114 | | // The num rows of the array reader equals to the num rows of the length reader. |
115 | 57.1k | uint64_t array_num_rows = meta.children_columns(1).num_rows(); |
116 | 57.1k | std::shared_ptr<ColumnReader> array_reader( |
117 | 57.1k | new ColumnReader(opts, meta, array_num_rows, file_reader)); |
118 | | // array reader do not need to init |
119 | 57.1k | array_reader->_sub_readers.resize(meta.children_columns_size()); |
120 | 57.1k | array_reader->_sub_readers[0] = std::move(item_reader); |
121 | 57.1k | array_reader->_sub_readers[1] = std::move(offset_reader); |
122 | 57.1k | if (meta.is_nullable()) { |
123 | 41.3k | array_reader->_sub_readers[2] = std::move(null_reader); |
124 | 41.3k | } |
125 | 57.1k | array_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_ARRAY; |
126 | 57.1k | *reader = std::move(array_reader); |
127 | 57.1k | return Status::OK(); |
128 | 57.1k | } |
129 | | |
130 | | Status ColumnReader::create_map(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
131 | | const io::FileReaderSPtr& file_reader, |
132 | 45.7k | std::shared_ptr<ColumnReader>* reader) { |
133 | | // map reader now has 3 sub readers for key, value, offsets(scalar), null(scala) |
134 | 45.7k | DCHECK(meta.children_columns_size() == 3 || meta.children_columns_size() == 4); |
135 | 45.7k | std::shared_ptr<ColumnReader> key_reader; |
136 | 45.7k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0), |
137 | 45.7k | meta.children_columns(0).num_rows(), file_reader, |
138 | 45.7k | &key_reader)); |
139 | 45.7k | std::shared_ptr<ColumnReader> val_reader; |
140 | 45.7k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1), |
141 | 45.7k | meta.children_columns(1).num_rows(), file_reader, |
142 | 45.7k | &val_reader)); |
143 | 45.7k | std::shared_ptr<ColumnReader> offset_reader; |
144 | 45.7k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2), |
145 | 45.7k | meta.children_columns(2).num_rows(), file_reader, |
146 | 45.7k | &offset_reader)); |
147 | 45.7k | std::shared_ptr<ColumnReader> null_reader; |
148 | 45.7k | if (meta.is_nullable()) { |
149 | 14.3k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(3), |
150 | 14.3k | meta.children_columns(3).num_rows(), file_reader, |
151 | 14.3k | &null_reader)); |
152 | 14.3k | } |
153 | | |
154 | | // The num rows of the map reader equals to the num rows of the length reader. |
155 | 45.7k | uint64_t map_num_rows = meta.children_columns(2).num_rows(); |
156 | 45.7k | std::shared_ptr<ColumnReader> map_reader( |
157 | 45.7k | new ColumnReader(opts, meta, map_num_rows, file_reader)); |
158 | 45.7k | map_reader->_sub_readers.resize(meta.children_columns_size()); |
159 | | |
160 | 45.7k | map_reader->_sub_readers[0] = std::move(key_reader); |
161 | 45.7k | map_reader->_sub_readers[1] = std::move(val_reader); |
162 | 45.7k | map_reader->_sub_readers[2] = std::move(offset_reader); |
163 | 45.7k | if (meta.is_nullable()) { |
164 | 14.3k | map_reader->_sub_readers[3] = std::move(null_reader); |
165 | 14.3k | } |
166 | 45.7k | map_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_MAP; |
167 | 45.7k | *reader = std::move(map_reader); |
168 | 45.7k | return Status::OK(); |
169 | 45.7k | } |
170 | | |
171 | | Status ColumnReader::create_struct(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
172 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
173 | 2.87k | std::shared_ptr<ColumnReader>* reader) { |
174 | | // not support empty struct |
175 | 2.87k | DCHECK(meta.children_columns_size() >= 1); |
176 | | // create struct column reader |
177 | 2.87k | std::shared_ptr<ColumnReader> struct_reader( |
178 | 2.87k | new ColumnReader(opts, meta, num_rows, file_reader)); |
179 | 2.87k | 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 | 16.7k | for (int i = 0; i < meta.children_columns_size(); i++) { |
182 | 13.9k | std::shared_ptr<ColumnReader> sub_reader; |
183 | 13.9k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(i), |
184 | 13.9k | meta.children_columns(i).num_rows(), file_reader, |
185 | 13.9k | &sub_reader)); |
186 | 13.9k | struct_reader->_sub_readers.push_back(std::move(sub_reader)); |
187 | 13.9k | } |
188 | 2.87k | struct_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_STRUCT; |
189 | 2.87k | *reader = std::move(struct_reader); |
190 | 2.87k | return Status::OK(); |
191 | 2.87k | } |
192 | | |
193 | | Status ColumnReader::create_agg_state(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
194 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
195 | 638 | std::shared_ptr<ColumnReader>* reader) { |
196 | 638 | 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 | 638 | auto data_type = DataTypeFactory::instance().create_data_type(meta); |
205 | 638 | const auto* agg_state_type = assert_cast<const DataTypeAggState*>(data_type.get()); |
206 | 638 | agg_state_type->check_function_compatibility(opts.be_exec_version); |
207 | 638 | auto type = agg_state_type->get_serialized_type()->get_primitive_type(); |
208 | | |
209 | 638 | if (read_as_string(type)) { |
210 | 594 | std::shared_ptr<ColumnReader> reader_local( |
211 | 594 | new ColumnReader(opts, meta, num_rows, file_reader)); |
212 | 594 | RETURN_IF_ERROR(reader_local->init(&meta)); |
213 | 594 | *reader = std::move(reader_local); |
214 | 594 | return Status::OK(); |
215 | 594 | } else if (type == PrimitiveType::TYPE_MAP) { |
216 | 34 | return create_map(opts, meta, file_reader, reader); |
217 | 34 | } 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 | 0 | return Status::InternalError("Not supported type: {}, serialized type: {}", |
224 | 0 | agg_state_type->get_name(), int(type)); |
225 | 638 | } |
226 | | |
227 | 188k | bool ColumnReader::is_compaction_reader_type(ReaderType type) { |
228 | 188k | return type == ReaderType::READER_BASE_COMPACTION || |
229 | 188k | type == ReaderType::READER_CUMULATIVE_COMPACTION || |
230 | 188k | type == ReaderType::READER_COLD_DATA_COMPACTION || |
231 | 188k | type == ReaderType::READER_SEGMENT_COMPACTION || |
232 | 188k | type == ReaderType::READER_FULL_COMPACTION; |
233 | 188k | } |
234 | | |
235 | | Status ColumnReader::create(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
236 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
237 | 2.37M | std::shared_ptr<ColumnReader>* reader) { |
238 | 2.37M | if (is_scalar_type((FieldType)meta.type())) { |
239 | 2.26M | std::shared_ptr<ColumnReader> reader_local( |
240 | 2.26M | new ColumnReader(opts, meta, num_rows, file_reader)); |
241 | 2.26M | RETURN_IF_ERROR(reader_local->init(&meta)); |
242 | 2.26M | *reader = std::move(reader_local); |
243 | 2.26M | return Status::OK(); |
244 | 2.26M | } else { |
245 | 118k | auto type = (FieldType)meta.type(); |
246 | 118k | switch (type) { |
247 | 643 | case FieldType::OLAP_FIELD_TYPE_AGG_STATE: { |
248 | 643 | return create_agg_state(opts, meta, num_rows, file_reader, reader); |
249 | 0 | } |
250 | 2.87k | case FieldType::OLAP_FIELD_TYPE_STRUCT: { |
251 | 2.87k | return create_struct(opts, meta, num_rows, file_reader, reader); |
252 | 0 | } |
253 | 57.2k | case FieldType::OLAP_FIELD_TYPE_ARRAY: { |
254 | 57.2k | return create_array(opts, meta, file_reader, reader); |
255 | 0 | } |
256 | 45.7k | case FieldType::OLAP_FIELD_TYPE_MAP: { |
257 | 45.7k | return create_map(opts, meta, file_reader, reader); |
258 | 0 | } |
259 | 12.4k | case FieldType::OLAP_FIELD_TYPE_VARIANT: { |
260 | | // Read variant only root data using a single ColumnReader |
261 | 12.4k | std::shared_ptr<ColumnReader> reader_local( |
262 | 12.4k | new ColumnReader(opts, meta, num_rows, file_reader)); |
263 | 12.4k | RETURN_IF_ERROR(reader_local->init(&meta)); |
264 | 12.4k | *reader = std::move(reader_local); |
265 | 12.4k | return Status::OK(); |
266 | 12.4k | } |
267 | 0 | default: |
268 | 0 | return Status::NotSupported("unsupported type for ColumnReader: {}", |
269 | 0 | std::to_string(int(type))); |
270 | 118k | } |
271 | 118k | } |
272 | 2.37M | } |
273 | | |
274 | 12.4k | ColumnReader::ColumnReader() = default; |
275 | | |
276 | | ColumnReader::ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
277 | | uint64_t num_rows, io::FileReaderSPtr file_reader) |
278 | 2.38M | : _use_index_page_cache(!config::disable_storage_page_cache), |
279 | 2.38M | _opts(opts), |
280 | 2.38M | _num_rows(num_rows), |
281 | 2.38M | _file_reader(std::move(file_reader)), |
282 | 2.38M | _dict_encoding_type(UNKNOWN_DICT_ENCODING) { |
283 | 2.38M | _meta_length = meta.length(); |
284 | 2.38M | _meta_type = (FieldType)meta.type(); |
285 | 2.38M | if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) { |
286 | 57.2k | _meta_children_column_type = (FieldType)meta.children_columns(0).type(); |
287 | 57.2k | } |
288 | 2.38M | _data_type = DataTypeFactory::instance().create_data_type(meta); |
289 | 2.38M | _meta_is_nullable = meta.is_nullable(); |
290 | 2.38M | _meta_dict_page = meta.dict_page(); |
291 | 2.38M | _meta_compression = meta.compression(); |
292 | 2.38M | } |
293 | | |
294 | 2.17M | ColumnReader::~ColumnReader() = default; |
295 | | |
296 | 2.27M | int64_t ColumnReader::get_metadata_size() const { |
297 | 2.27M | return sizeof(ColumnReader) + (_segment_zone_map ? _segment_zone_map->ByteSizeLong() : 0); |
298 | 2.27M | } |
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 | 2.27M | Status ColumnReader::init(const ColumnMetaPB* meta) { |
349 | 2.27M | _type = (FieldType)meta->type(); |
350 | | |
351 | 2.27M | if (meta->has_be_exec_version()) { |
352 | 1.95M | _be_exec_version = meta->be_exec_version(); |
353 | 1.95M | } |
354 | | |
355 | 2.27M | 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 | 2.27M | RETURN_IF_ERROR(EncodingInfo::get(_type, meta->encoding(), &_encoding_info)); |
359 | | |
360 | 6.38M | for (int i = 0; i < meta->indexes_size(); i++) { |
361 | 4.11M | const auto& index_meta = meta->indexes(i); |
362 | 4.11M | switch (index_meta.type()) { |
363 | 0 | case BITMAP_INDEX: |
364 | 0 | break; |
365 | 2.21M | case ORDINAL_INDEX: |
366 | 2.21M | _ordinal_index.reset( |
367 | 2.21M | new OrdinalIndexReader(_file_reader, _num_rows, index_meta.ordinal_index())); |
368 | 2.21M | break; |
369 | 1.89M | case ZONE_MAP_INDEX: |
370 | 1.89M | _segment_zone_map = |
371 | 1.89M | std::make_unique<ZoneMapPB>(index_meta.zone_map_index().segment_zone_map()); |
372 | 1.89M | _zone_map_index.reset(new ZoneMapIndexReader( |
373 | 1.89M | _file_reader, index_meta.zone_map_index().page_zone_maps())); |
374 | 1.89M | break; |
375 | 7.87k | case BLOOM_FILTER_INDEX: |
376 | 7.87k | _bloom_filter_index.reset( |
377 | 7.87k | new BloomFilterIndexReader(_file_reader, index_meta.bloom_filter_index())); |
378 | 7.87k | 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 | 4.11M | } |
385 | 4.11M | } |
386 | 2.27M | 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 | 2.27M | 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 | 2.27M | return Status::OK(); |
396 | 2.27M | } |
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 | 125k | std::unique_ptr<IndexIterator>* iterator) { |
402 | 125k | RETURN_IF_ERROR( |
403 | 125k | _load_index(index_file_reader, index_meta, rowset_id, segment_id, rows_of_segment)); |
404 | 125k | { |
405 | 125k | std::shared_lock<std::shared_mutex> rlock(_load_index_lock); |
406 | 125k | auto iter = _index_readers.find(index_meta->index_id()); |
407 | 125k | if (iter != _index_readers.end()) { |
408 | 125k | if (iter->second != nullptr) { |
409 | 125k | RETURN_IF_ERROR(iter->second->new_iterator(iterator)); |
410 | 125k | } |
411 | 125k | } |
412 | 125k | } |
413 | 125k | return Status::OK(); |
414 | 125k | } |
415 | | |
416 | | Status ColumnReader::read_page(const ColumnIteratorOptions& iter_opts, const PagePointer& pp, |
417 | | PageHandle* handle, Slice* page_body, PageFooterPB* footer, |
418 | 2.37M | BlockCompressionCodec* codec) const { |
419 | 2.37M | SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().column_reader_read_page); |
420 | 2.37M | iter_opts.sanity_check(); |
421 | 2.37M | PageReadOptions opts(iter_opts.io_ctx); |
422 | 2.37M | opts.verify_checksum = _opts.verify_checksum; |
423 | 2.37M | opts.use_page_cache = iter_opts.use_page_cache; |
424 | 2.37M | opts.kept_in_memory = _opts.kept_in_memory; |
425 | 2.37M | opts.type = iter_opts.type; |
426 | 2.37M | opts.file_reader = iter_opts.file_reader; |
427 | 2.37M | opts.page_pointer = pp; |
428 | 2.37M | opts.codec = codec; |
429 | 2.37M | opts.stats = iter_opts.stats; |
430 | 2.37M | opts.encoding_info = _encoding_info; |
431 | | |
432 | 2.37M | return PageIO::read_and_decompress_page(opts, handle, page_body, footer); |
433 | 2.37M | } |
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 | 136k | RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts) { |
439 | 136k | std::vector<uint32_t> page_indexes; |
440 | 136k | RETURN_IF_ERROR( |
441 | 136k | _get_filtered_pages(col_predicates, delete_predicates, &page_indexes, iter_opts)); |
442 | 136k | RETURN_IF_ERROR(_calculate_row_ranges(page_indexes, row_ranges, iter_opts)); |
443 | 136k | return Status::OK(); |
444 | 136k | } |
445 | | |
446 | 33.0k | Status ColumnReader::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const { |
447 | 33.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 | 33.0k | ZoneMap zone_map; |
452 | 33.0k | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
453 | | |
454 | 33.0k | dst->reserve(*n); |
455 | 33.0k | if (!zone_map.has_not_null) { |
456 | 1.49k | assert_cast<ColumnNullable&>(*dst).insert_many_defaults(*n); |
457 | 1.49k | return Status::OK(); |
458 | 1.49k | } |
459 | 31.5k | dst->insert(zone_map.max_value); |
460 | 63.0k | for (int i = 1; i < *n; ++i) { |
461 | 31.5k | dst->insert(zone_map.min_value); |
462 | 31.5k | } |
463 | 31.5k | return Status::OK(); |
464 | 33.0k | } |
465 | | |
466 | | Status ColumnReader::match_condition(const AndBlockColumnPredicate* col_predicates, |
467 | 1.87M | bool* matched) const { |
468 | 1.87M | *matched = true; |
469 | 1.87M | if (_zone_map_index == nullptr) { |
470 | 0 | return Status::OK(); |
471 | 0 | } |
472 | 1.87M | ZoneMap zone_map; |
473 | 1.87M | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
474 | | |
475 | 1.87M | *matched = _zone_map_match_condition(zone_map, col_predicates); |
476 | 1.87M | return Status::OK(); |
477 | 1.87M | } |
478 | | |
479 | | Status ColumnReader::prune_predicates_by_zone_map( |
480 | | std::vector<std::shared_ptr<ColumnPredicate>>& predicates, const int column_id, |
481 | 25.3M | bool* pruned) const { |
482 | 25.3M | *pruned = false; |
483 | 25.3M | if (_zone_map_index == nullptr) { |
484 | 33.3k | return Status::OK(); |
485 | 33.3k | } |
486 | | |
487 | 25.3M | ZoneMap zone_map; |
488 | 25.3M | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
489 | 25.3M | if (zone_map.pass_all) { |
490 | 0 | return Status::OK(); |
491 | 0 | } |
492 | | |
493 | 50.9M | for (auto it = predicates.begin(); it != predicates.end();) { |
494 | 25.6M | auto predicate = *it; |
495 | 25.6M | if (predicate->column_id() == column_id && predicate->is_always_true(zone_map)) { |
496 | 13.2k | *pruned = true; |
497 | 13.2k | it = predicates.erase(it); |
498 | 25.6M | } else { |
499 | 25.6M | ++it; |
500 | 25.6M | } |
501 | 25.6M | } |
502 | 25.3M | return Status::OK(); |
503 | 25.3M | } |
504 | | |
505 | | bool ColumnReader::_zone_map_match_condition(const ZoneMap& zone_map, |
506 | 1.96M | const AndBlockColumnPredicate* col_predicates) const { |
507 | 1.96M | if (zone_map.pass_all) { |
508 | 0 | return true; |
509 | 0 | } |
510 | | |
511 | 1.96M | return col_predicates->evaluate_and(zone_map); |
512 | 1.96M | } |
513 | | |
514 | | Status ColumnReader::_get_filtered_pages( |
515 | | const AndBlockColumnPredicate* col_predicates, |
516 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
517 | 136k | std::vector<uint32_t>* page_indexes, const ColumnIteratorOptions& iter_opts) { |
518 | 136k | RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
519 | | |
520 | 136k | const std::vector<ZoneMapPB>& zone_maps = _zone_map_index->page_zone_maps(); |
521 | 136k | size_t page_size = _zone_map_index->num_pages(); |
522 | 320k | for (size_t i = 0; i < page_size; ++i) { |
523 | 184k | if (zone_maps[i].pass_all()) { |
524 | 90.9k | page_indexes->push_back(cast_set<uint32_t>(i)); |
525 | 93.0k | } else { |
526 | 93.0k | segment_v2::ZoneMap zone_map; |
527 | 93.0k | RETURN_IF_ERROR(ZoneMap::from_proto(zone_maps[i], _data_type, zone_map)); |
528 | 93.0k | if (_zone_map_match_condition(zone_map, col_predicates)) { |
529 | 92.3k | bool should_read = true; |
530 | 92.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 | 92.3k | if (should_read) { |
541 | 92.3k | page_indexes->push_back(cast_set<uint32_t>(i)); |
542 | 92.3k | } |
543 | 92.3k | } |
544 | 93.0k | } |
545 | 184k | } |
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 | 136k | return Status::OK(); |
551 | 136k | } |
552 | | |
553 | | Status ColumnReader::_calculate_row_ranges(const std::vector<uint32_t>& page_indexes, |
554 | | RowRanges* row_ranges, |
555 | 136k | const ColumnIteratorOptions& iter_opts) { |
556 | 136k | row_ranges->clear(); |
557 | 136k | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
558 | 183k | for (auto i : page_indexes) { |
559 | 183k | ordinal_t page_first_id = _ordinal_index->get_first_ordinal(i); |
560 | 183k | ordinal_t page_last_id = _ordinal_index->get_last_ordinal(i); |
561 | 183k | RowRanges page_row_ranges(RowRanges::create_single(page_first_id, page_last_id + 1)); |
562 | 183k | RowRanges::ranges_union(*row_ranges, page_row_ranges, row_ranges); |
563 | 183k | } |
564 | 136k | return Status::OK(); |
565 | 136k | } |
566 | | |
567 | | Status ColumnReader::get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
568 | | RowRanges* row_ranges, |
569 | 184 | const ColumnIteratorOptions& iter_opts) { |
570 | 184 | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
571 | 184 | RETURN_IF_ERROR( |
572 | 184 | _load_bloom_filter_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
573 | 184 | RowRanges bf_row_ranges; |
574 | 184 | std::unique_ptr<BloomFilterIndexIterator> bf_iter; |
575 | 184 | RETURN_IF_ERROR(_bloom_filter_index->new_iterator(&bf_iter, iter_opts.stats)); |
576 | 183 | size_t range_size = row_ranges->range_size(); |
577 | | // get covered page ids |
578 | 183 | std::set<uint32_t> page_ids; |
579 | 366 | for (int i = 0; i < range_size; ++i) { |
580 | 183 | int64_t from = row_ranges->get_range_from(i); |
581 | 183 | int64_t idx = from; |
582 | 183 | int64_t to = row_ranges->get_range_to(i); |
583 | 183 | auto iter = _ordinal_index->seek_at_or_before(from); |
584 | 629 | while (idx < to && iter.valid()) { |
585 | 446 | page_ids.insert(iter.page_index()); |
586 | 446 | idx = iter.last_ordinal() + 1; |
587 | 446 | iter.next(); |
588 | 446 | } |
589 | 183 | } |
590 | 446 | for (auto& pid : page_ids) { |
591 | 446 | std::unique_ptr<BloomFilter> bf; |
592 | 446 | RETURN_IF_ERROR(bf_iter->read_bloom_filter(pid, &bf)); |
593 | 446 | if (col_predicates->evaluate_and(bf.get())) { |
594 | 201 | bf_row_ranges.add(RowRange(_ordinal_index->get_first_ordinal(pid), |
595 | 201 | _ordinal_index->get_last_ordinal(pid) + 1)); |
596 | 201 | } |
597 | 446 | } |
598 | 183 | RowRanges::ranges_intersection(*row_ranges, bf_row_ranges, row_ranges); |
599 | 183 | return Status::OK(); |
600 | 183 | } |
601 | | |
602 | | Status ColumnReader::_load_ordinal_index(bool use_page_cache, bool kept_in_memory, |
603 | 1.96M | const ColumnIteratorOptions& iter_opts) { |
604 | 1.96M | if (!_ordinal_index) { |
605 | 0 | return Status::InternalError("ordinal_index not inited"); |
606 | 0 | } |
607 | 1.96M | return _ordinal_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
608 | 1.96M | } |
609 | | |
610 | | Status ColumnReader::_load_zone_map_index(bool use_page_cache, bool kept_in_memory, |
611 | 137k | const ColumnIteratorOptions& iter_opts) { |
612 | 137k | if (_zone_map_index != nullptr) { |
613 | 137k | return _zone_map_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
614 | 137k | } |
615 | 18.4E | return Status::OK(); |
616 | 137k | } |
617 | | |
618 | 2.83k | Status ColumnReader::get_segment_zone_map(segment_v2::ZoneMap* zone_map) const { |
619 | 2.83k | DORIS_CHECK(zone_map != nullptr); |
620 | 2.83k | DORIS_CHECK(_segment_zone_map != nullptr); |
621 | 2.83k | return ZoneMap::from_proto(*_segment_zone_map, _data_type, *zone_map); |
622 | 2.83k | } |
623 | | |
624 | | Status ColumnReader::get_page_zone_maps(const ColumnIteratorOptions& iter_opts, |
625 | 618 | const std::vector<ZoneMapPB>** zone_maps) { |
626 | 618 | DORIS_CHECK(zone_maps != nullptr); |
627 | 618 | if (_zone_map_index == nullptr) { |
628 | 0 | *zone_maps = nullptr; |
629 | 0 | return Status::OK(); |
630 | 0 | } |
631 | 618 | RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
632 | 618 | *zone_maps = &_zone_map_index->page_zone_maps(); |
633 | 618 | return Status::OK(); |
634 | 618 | } |
635 | | |
636 | | Status ColumnReader::get_row_range_for_page(uint32_t page_index, |
637 | | const ColumnIteratorOptions& iter_opts, |
638 | 1.07k | RowRange* row_range) { |
639 | 1.07k | DORIS_CHECK(row_range != nullptr); |
640 | 1.07k | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
641 | 1.07k | DORIS_CHECK(page_index < _ordinal_index->num_data_pages()); |
642 | 1.07k | *row_range = RowRange(_ordinal_index->get_first_ordinal(page_index), |
643 | 1.07k | _ordinal_index->get_last_ordinal(page_index) + 1); |
644 | 1.07k | return Status::OK(); |
645 | 1.07k | } |
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 | 125k | uint32_t segment_id, size_t rows_of_segment) { |
650 | 125k | std::unique_lock<std::shared_mutex> wlock(_load_index_lock); |
651 | | |
652 | 125k | 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 | 125k | auto it = _index_readers.find(index_meta->index_id()); |
658 | 125k | if (it != _index_readers.end()) { |
659 | 64.4k | return Status::OK(); |
660 | 64.4k | } |
661 | | |
662 | 60.6k | bool should_analyzer = |
663 | 60.6k | inverted_index::InvertedIndexAnalyzer::should_analyzer(index_meta->properties()); |
664 | | |
665 | 60.6k | FieldType type; |
666 | 60.6k | if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) { |
667 | 7.29k | type = _meta_children_column_type; |
668 | 53.3k | } else { |
669 | 53.3k | type = _type; |
670 | 53.3k | } |
671 | | |
672 | 60.6k | if (index_meta->index_type() == IndexType::ANN) { |
673 | 92 | _index_readers[index_meta->index_id()] = std::make_shared<AnnIndexReader>( |
674 | 92 | index_meta, index_file_reader, rowset_id, segment_id, rows_of_segment); |
675 | 92 | return Status::OK(); |
676 | 92 | } |
677 | | |
678 | 60.5k | IndexReaderPtr index_reader; |
679 | | |
680 | 60.5k | if (is_string_type(type)) { |
681 | 32.3k | if (should_analyzer) { |
682 | 21.1k | try { |
683 | 21.1k | index_reader = FullTextIndexReader::create_shared(index_meta, index_file_reader); |
684 | 21.1k | } catch (const CLuceneError& e) { |
685 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
686 | 0 | "create FullTextIndexReader error: {}", e.what()); |
687 | 0 | } |
688 | 21.1k | } else { |
689 | 11.1k | try { |
690 | 11.1k | index_reader = |
691 | 11.1k | StringTypeInvertedIndexReader::create_shared(index_meta, index_file_reader); |
692 | 11.1k | } catch (const CLuceneError& e) { |
693 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
694 | 0 | "create StringTypeInvertedIndexReader error: {}", e.what()); |
695 | 0 | } |
696 | 11.1k | } |
697 | 32.3k | } else if (is_numeric_type(type)) { |
698 | 28.5k | try { |
699 | 28.5k | index_reader = BkdIndexReader::create_shared(index_meta, index_file_reader); |
700 | 28.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 | 60.8k | _index_readers[index_meta->index_id()] = index_reader; |
709 | 60.8k | return Status::OK(); |
710 | 60.5k | } |
711 | | |
712 | 112k | bool ColumnReader::has_bloom_filter_index(bool ngram) const { |
713 | 112k | 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 | 184 | const ColumnIteratorOptions& iter_opts) { |
724 | 184 | if (_bloom_filter_index != nullptr) { |
725 | 184 | return _bloom_filter_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
726 | 184 | } |
727 | 0 | return Status::OK(); |
728 | 184 | } |
729 | | |
730 | | Status ColumnReader::seek_at_or_before(ordinal_t ordinal, OrdinalPageIndexIterator* iter, |
731 | 1.83M | const ColumnIteratorOptions& iter_opts) { |
732 | 1.83M | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
733 | 1.83M | *iter = _ordinal_index->seek_at_or_before(ordinal); |
734 | 1.83M | if (!iter->valid()) { |
735 | 0 | return Status::NotFound("Failed to seek to ordinal {}, ", ordinal); |
736 | 0 | } |
737 | 1.83M | return Status::OK(); |
738 | 1.83M | } |
739 | | |
740 | | Status ColumnReader::get_ordinal_index_reader(OrdinalIndexReader*& reader, |
741 | 300k | 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 | 300k | RETURN_IF_ERROR( |
745 | 300k | _ordinal_index->load(_use_index_page_cache, _opts.kept_in_memory, index_load_stats)); |
746 | 300k | reader = _ordinal_index.get(); |
747 | 300k | return Status::OK(); |
748 | 300k | } |
749 | | |
750 | 549k | Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column) { |
751 | 549k | return new_iterator(iterator, tablet_column, nullptr); |
752 | 549k | } |
753 | | |
754 | | Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column, |
755 | 27.5M | const StorageReadOptions* opt) { |
756 | 27.5M | if (is_empty()) { |
757 | 50.4k | *iterator = std::make_unique<EmptyFileColumnIterator>(); |
758 | 50.4k | return Status::OK(); |
759 | 50.4k | } |
760 | 27.4M | if (is_scalar_type(_meta_type)) { |
761 | 27.3M | if (is_string_type(_meta_type)) { |
762 | 16.3M | *iterator = std::make_unique<StringFileColumnIterator>(shared_from_this()); |
763 | 16.3M | } else { |
764 | 11.0M | *iterator = std::make_unique<FileColumnIterator>(shared_from_this()); |
765 | 11.0M | } |
766 | 27.3M | (*iterator)->set_column_name(tablet_column ? tablet_column->name() : ""); |
767 | 27.3M | return Status::OK(); |
768 | 27.3M | } else { |
769 | 121k | auto type = _meta_type; |
770 | 121k | switch (type) { |
771 | 878 | case FieldType::OLAP_FIELD_TYPE_AGG_STATE: { |
772 | 878 | return new_agg_state_iterator(iterator); |
773 | 0 | } |
774 | 8.11k | case FieldType::OLAP_FIELD_TYPE_STRUCT: { |
775 | 8.11k | return new_struct_iterator(iterator, tablet_column); |
776 | 0 | } |
777 | 82.6k | case FieldType::OLAP_FIELD_TYPE_ARRAY: { |
778 | 82.6k | return new_array_iterator(iterator, tablet_column); |
779 | 0 | } |
780 | 43.0k | case FieldType::OLAP_FIELD_TYPE_MAP: { |
781 | 43.0k | 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 | 121k | } |
787 | 121k | } |
788 | 27.4M | } |
789 | | |
790 | 872 | Status ColumnReader::new_agg_state_iterator(ColumnIteratorUPtr* iterator) { |
791 | 872 | *iterator = std::make_unique<FileColumnIterator>(shared_from_this()); |
792 | 872 | return Status::OK(); |
793 | 872 | } |
794 | | |
795 | | Status ColumnReader::new_array_iterator(ColumnIteratorUPtr* iterator, |
796 | 82.3k | const TabletColumn* tablet_column) { |
797 | 82.3k | ColumnIteratorUPtr item_iterator; |
798 | 82.3k | RETURN_IF_ERROR(_sub_readers[0]->new_iterator( |
799 | 82.3k | &item_iterator, tablet_column && tablet_column->get_subtype_count() > 0 |
800 | 82.3k | ? &tablet_column->get_sub_column(0) |
801 | 82.3k | : nullptr)); |
802 | | |
803 | 82.3k | item_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : ""); |
804 | | |
805 | 82.3k | ColumnIteratorUPtr offset_iterator; |
806 | 82.3k | RETURN_IF_ERROR(_sub_readers[1]->new_iterator(&offset_iterator, nullptr)); |
807 | 82.3k | auto* file_iter = static_cast<FileColumnIterator*>(offset_iterator.release()); |
808 | 82.3k | OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>( |
809 | 82.3k | std::unique_ptr<FileColumnIterator>(file_iter)); |
810 | | |
811 | 82.3k | ColumnIteratorUPtr null_iterator; |
812 | 82.3k | if (is_nullable()) { |
813 | 63.8k | RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&null_iterator, nullptr)); |
814 | 63.8k | } |
815 | 82.3k | *iterator = std::make_unique<ArrayFileColumnIterator>(shared_from_this(), std::move(ofcIter), |
816 | 82.3k | std::move(item_iterator), |
817 | 82.3k | std::move(null_iterator)); |
818 | 82.3k | return Status::OK(); |
819 | 82.3k | } |
820 | | |
821 | | Status ColumnReader::new_map_iterator(ColumnIteratorUPtr* iterator, |
822 | 43.0k | const TabletColumn* tablet_column) { |
823 | 43.0k | ColumnIteratorUPtr key_iterator; |
824 | 43.0k | RETURN_IF_ERROR(_sub_readers[0]->new_iterator( |
825 | 43.0k | &key_iterator, tablet_column && tablet_column->get_subtype_count() > 1 |
826 | 43.0k | ? &tablet_column->get_sub_column(0) |
827 | 43.0k | : nullptr)); |
828 | 43.0k | key_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : ""); |
829 | 43.0k | ColumnIteratorUPtr val_iterator; |
830 | 43.0k | RETURN_IF_ERROR(_sub_readers[1]->new_iterator( |
831 | 43.0k | &val_iterator, tablet_column && tablet_column->get_subtype_count() > 1 |
832 | 43.0k | ? &tablet_column->get_sub_column(1) |
833 | 43.0k | : nullptr)); |
834 | 43.0k | val_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(1).name() : ""); |
835 | 43.0k | ColumnIteratorUPtr offsets_iterator; |
836 | 43.0k | RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&offsets_iterator, nullptr)); |
837 | 43.0k | auto* file_iter = static_cast<FileColumnIterator*>(offsets_iterator.release()); |
838 | 43.0k | OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>( |
839 | 43.0k | std::unique_ptr<FileColumnIterator>(file_iter)); |
840 | | |
841 | 43.0k | ColumnIteratorUPtr null_iterator; |
842 | 43.0k | if (is_nullable()) { |
843 | 18.1k | RETURN_IF_ERROR(_sub_readers[3]->new_iterator(&null_iterator, nullptr)); |
844 | 18.1k | } |
845 | 43.0k | *iterator = std::make_unique<MapFileColumnIterator>( |
846 | 43.0k | shared_from_this(), std::move(null_iterator), std::move(ofcIter), |
847 | 43.0k | std::move(key_iterator), std::move(val_iterator)); |
848 | 43.0k | return Status::OK(); |
849 | 43.0k | } |
850 | | |
851 | | Status ColumnReader::new_struct_iterator(ColumnIteratorUPtr* iterator, |
852 | 8.10k | const TabletColumn* tablet_column) { |
853 | 8.10k | std::vector<ColumnIteratorUPtr> sub_column_iterators; |
854 | 8.10k | 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 | 8.10k | sub_column_iterators.reserve(child_size); |
857 | | |
858 | 38.0k | for (uint64_t i = 0; i < child_size; i++) { |
859 | 29.9k | ColumnIteratorUPtr sub_column_iterator; |
860 | 29.9k | RETURN_IF_ERROR(_sub_readers[i]->new_iterator( |
861 | 29.9k | &sub_column_iterator, tablet_column ? &tablet_column->get_sub_column(i) : nullptr)); |
862 | 29.9k | sub_column_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(i).name() |
863 | 18.4E | : ""); |
864 | 29.9k | sub_column_iterators.emplace_back(std::move(sub_column_iterator)); |
865 | 29.9k | } |
866 | | |
867 | | // create default_iterator for schema-change behavior which increase column |
868 | 9.40k | 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 | 8.10k | ColumnIteratorUPtr null_iterator; |
877 | 8.10k | if (is_nullable()) { |
878 | 7.26k | RETURN_IF_ERROR(_sub_readers[child_size]->new_iterator(&null_iterator, nullptr)); |
879 | 7.26k | } |
880 | 8.10k | *iterator = std::make_unique<StructFileColumnIterator>( |
881 | 8.10k | shared_from_this(), std::move(null_iterator), std::move(sub_column_iterators)); |
882 | 8.10k | return Status::OK(); |
883 | 8.10k | } |
884 | | |
885 | | Result<TColumnAccessPaths> ColumnIterator::_get_sub_access_paths( |
886 | 149k | const TColumnAccessPaths& access_paths) { |
887 | 149k | TColumnAccessPaths sub_access_paths = access_paths; |
888 | 233k | for (auto it = sub_access_paths.begin(); it != sub_access_paths.end();) { |
889 | 83.5k | TColumnAccessPath& name_path = *it; |
890 | 83.5k | 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 | 83.5k | 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 | 83.5k | name_path.data_access_path.path.erase(name_path.data_access_path.path.begin()); |
902 | 83.5k | if (!name_path.data_access_path.path.empty()) { |
903 | 10.3k | ++it; |
904 | 73.1k | } else { |
905 | 73.1k | set_need_to_read(); |
906 | 73.1k | it = sub_access_paths.erase(it); |
907 | 73.1k | } |
908 | 83.5k | } |
909 | 149k | return sub_access_paths; |
910 | 149k | } |
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 | 43.0k | : _map_reader(reader), |
919 | 43.0k | _offsets_iterator(std::move(offsets_iterator)), |
920 | 43.0k | _key_iterator(std::move(key_iterator)), |
921 | 43.0k | _val_iterator(std::move(val_iterator)) { |
922 | 43.0k | if (_map_reader->is_nullable()) { |
923 | 18.1k | _null_iterator = std::move(null_iterator); |
924 | 18.1k | } |
925 | 43.0k | } |
926 | | |
927 | 42.9k | Status MapFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
928 | 42.9k | if (_reading_flag == ReadingFlag::SKIP_READING) { |
929 | 32 | DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading."; |
930 | 32 | return Status::OK(); |
931 | 32 | } |
932 | 42.8k | RETURN_IF_ERROR(_key_iterator->init(opts)); |
933 | 42.8k | RETURN_IF_ERROR(_val_iterator->init(opts)); |
934 | 42.8k | RETURN_IF_ERROR(_offsets_iterator->init(opts)); |
935 | 42.8k | if (_map_reader->is_nullable()) { |
936 | 18.1k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
937 | 18.1k | } |
938 | 42.8k | return Status::OK(); |
939 | 42.8k | } |
940 | | |
941 | 22.4k | Status MapFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
942 | 22.4k | 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 | 22.4k | 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 | 22.4k | if (_map_reader->is_nullable()) { |
956 | 12.4k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
957 | 12.4k | } |
958 | 22.4k | RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(ord)); |
959 | 22.4k | 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 | 21.9k | ordinal_t offset = 0; |
965 | 21.9k | RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&offset)); |
966 | 21.9k | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(offset)); |
967 | 21.9k | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(offset)); |
968 | 21.9k | return Status::OK(); |
969 | 21.9k | } |
970 | | |
971 | 1.35k | Status MapFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
972 | 1.35k | RETURN_IF_ERROR(_offsets_iterator->init_prefetcher(params)); |
973 | 1.35k | if (_map_reader->is_nullable()) { |
974 | 1.33k | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
975 | 1.33k | } |
976 | 1.35k | RETURN_IF_ERROR(_key_iterator->init_prefetcher(params)); |
977 | 1.35k | RETURN_IF_ERROR(_val_iterator->init_prefetcher(params)); |
978 | 1.35k | return Status::OK(); |
979 | 1.35k | } |
980 | | |
981 | | void MapFileColumnIterator::collect_prefetchers( |
982 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
983 | 1.35k | PrefetcherInitMethod init_method) { |
984 | 1.35k | _offsets_iterator->collect_prefetchers(prefetchers, init_method); |
985 | 1.35k | if (_map_reader->is_nullable()) { |
986 | 1.33k | _null_iterator->collect_prefetchers(prefetchers, init_method); |
987 | 1.33k | } |
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 | 1.35k | _key_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
991 | 1.35k | _val_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
992 | 1.35k | } |
993 | | |
994 | 22.4k | Status MapFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
995 | 22.4k | 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 | 22.4k | 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 | 22.4k | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1026 | 22.4k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1027 | 22.4k | : *dst); |
1028 | 22.4k | auto column_offsets_ptr = IColumn::mutate(std::move(column_map.get_offsets_ptr())); |
1029 | 22.4k | Defer defer_offsets {[&] { |
1030 | 22.4k | auto typed_column_offsets_ptr = ColumnMap::COffsets::cast_to_column_mutptr( |
1031 | 22.4k | assert_cast<ColumnMap::COffsets*, TypeCheckOnRelease::DISABLE>( |
1032 | 22.4k | column_offsets_ptr.get())); |
1033 | 22.4k | column_offsets_ptr = nullptr; |
1034 | 22.4k | column_map.get_offsets_ptr() = std::move(typed_column_offsets_ptr); |
1035 | 22.4k | }}; |
1036 | 22.4k | bool offsets_has_null = false; |
1037 | 22.4k | ssize_t start = column_offsets_ptr->size(); |
1038 | 22.4k | RETURN_IF_ERROR(_offsets_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null)); |
1039 | 22.4k | if (*n == 0) { |
1040 | 0 | return Status::OK(); |
1041 | 0 | } |
1042 | 22.4k | auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr); |
1043 | 22.4k | RETURN_IF_ERROR(_offsets_iterator->_calculate_offsets(start, column_offsets)); |
1044 | 22.4k | DCHECK(column_offsets.get_data().back() >= column_offsets.get_data()[start - 1]); |
1045 | 22.4k | size_t num_items = |
1046 | 22.4k | column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid |
1047 | | |
1048 | 22.4k | if (num_items > 0) { |
1049 | 15.8k | auto key_ptr = IColumn::mutate(std::move(column_map.get_keys_ptr())); |
1050 | 15.8k | auto val_ptr = IColumn::mutate(std::move(column_map.get_values_ptr())); |
1051 | 15.8k | Defer defer_keys {[&] { column_map.get_keys_ptr() = std::move(key_ptr); }}; |
1052 | 15.8k | Defer defer_values {[&] { column_map.get_values_ptr() = std::move(val_ptr); }}; |
1053 | 15.8k | if (read_offset_only()) { |
1054 | | // OFFSET_ONLY mode: skip reading actual key/value data, fill with defaults |
1055 | 469 | key_ptr->insert_many_defaults(num_items); |
1056 | 469 | val_ptr->insert_many_defaults(num_items); |
1057 | 15.4k | } else { |
1058 | 15.4k | size_t num_read = num_items; |
1059 | 15.4k | bool key_has_null = false; |
1060 | 15.4k | bool val_has_null = false; |
1061 | 15.4k | RETURN_IF_ERROR(_key_iterator->next_batch(&num_read, key_ptr, &key_has_null)); |
1062 | 15.4k | RETURN_IF_ERROR(_val_iterator->next_batch(&num_read, val_ptr, &val_has_null)); |
1063 | 15.4k | DCHECK(num_read == num_items); |
1064 | 15.4k | } |
1065 | 15.8k | } |
1066 | | |
1067 | 22.4k | if (is_column_nullable(*dst)) { |
1068 | 12.4k | size_t num_read = *n; |
1069 | 12.4k | 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 | 12.4k | if (_null_iterator) { |
1075 | 12.4k | bool null_signs_has_null = false; |
1076 | 12.4k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1077 | 12.4k | RETURN_IF_ERROR( |
1078 | 12.4k | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1079 | 12.4k | } else { |
1080 | 0 | null_map_ptr->insert_many_vals(0, num_read); |
1081 | 0 | } |
1082 | 12.4k | DCHECK(num_read == *n); |
1083 | 12.4k | } |
1084 | 22.4k | return Status::OK(); |
1085 | 22.4k | } |
1086 | | |
1087 | | Status MapFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
1088 | 17.5k | MutableColumnPtr& dst) { |
1089 | 17.5k | 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.5k | 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.5k | if (count == 0) { |
1116 | 0 | return Status::OK(); |
1117 | 0 | } |
1118 | | // resolve ColumnMap and nullable wrapper |
1119 | 17.5k | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1120 | 17.5k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1121 | 17.5k | : *dst); |
1122 | 17.5k | auto offsets_ptr = IColumn::mutate(std::move(column_map.get_offsets_ptr())); |
1123 | 17.5k | Defer defer_offsets {[&] { |
1124 | 17.5k | auto typed_offsets_ptr = ColumnMap::COffsets::cast_to_column_mutptr( |
1125 | 17.5k | assert_cast<ColumnMap::COffsets*, TypeCheckOnRelease::DISABLE>(offsets_ptr.get())); |
1126 | 17.5k | offsets_ptr = nullptr; |
1127 | 17.5k | column_map.get_offsets_ptr() = std::move(typed_offsets_ptr); |
1128 | 17.5k | }}; |
1129 | 17.5k | auto& offsets = static_cast<ColumnArray::ColumnOffsets&>(*offsets_ptr); |
1130 | 17.5k | size_t base = offsets.get_data().empty() ? 0 : offsets.get_data().back(); |
1131 | | |
1132 | | // 1. bulk read null-map if nullable |
1133 | 17.5k | std::vector<uint8_t> null_mask; // 0: not null, 1: null |
1134 | 17.5k | if (_map_reader->is_nullable()) { |
1135 | | // For nullable map columns, the destination column must also be nullable. |
1136 | 3.63k | 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.63k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1141 | 3.63k | size_t null_before = null_map_ptr->size(); |
1142 | 3.63k | auto* null_map_col = null_map_ptr.get(); |
1143 | 3.63k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1144 | 3.63k | 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.63k | null_mask.reserve(count); |
1147 | 57.7k | for (size_t i = 0; i < count; ++i) { |
1148 | 54.1k | null_mask.push_back(null_map_col->get_element(null_before + i)); |
1149 | 54.1k | } |
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.5k | MutableColumnPtr starts_col = ColumnOffset64::create(); |
1161 | 17.5k | starts_col->reserve(count); |
1162 | 17.5k | 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.5k | std::vector<rowid_t> next_rowids(count); |
1166 | 233k | for (size_t i = 0; i < count; ++i) { |
1167 | 215k | uint64_t nr = rowids[i] + 1; |
1168 | 215k | next_rowids[i] = nr < _map_reader->num_rows() ? static_cast<rowid_t>(nr) |
1169 | 215k | : static_cast<rowid_t>(0); // placeholder |
1170 | 215k | } |
1171 | 17.5k | MutableColumnPtr next_starts_col = ColumnOffset64::create(); |
1172 | 17.5k | next_starts_col->reserve(count); |
1173 | | // read for all; we'll fix out-of-bound cases below |
1174 | 17.5k | 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 | 233k | for (size_t i = 0; i < count; ++i) { |
1178 | 215k | 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 | 16.3k | RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(rowids[i])); |
1182 | 16.3k | size_t one = 1; |
1183 | 16.3k | bool has_null_unused = false; |
1184 | 16.3k | MutableColumnPtr tmp = ColumnOffset64::create(); |
1185 | 16.3k | RETURN_IF_ERROR(_offsets_iterator->next_batch(&one, tmp, &has_null_unused)); |
1186 | 16.3k | ordinal_t ns = 0; |
1187 | 16.3k | RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&ns)); |
1188 | | // overwrite with sentinel |
1189 | 16.3k | assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*next_starts_col) |
1190 | 16.3k | .get_data()[i] = ns; |
1191 | 16.3k | } |
1192 | 215k | } |
1193 | | |
1194 | | // 5. compute sizes and append offsets prefix-sum |
1195 | 17.5k | auto& starts_data = assert_cast<ColumnOffset64&>(*starts_col).get_data(); |
1196 | 17.5k | auto& next_starts_data = assert_cast<ColumnOffset64&>(*next_starts_col).get_data(); |
1197 | 17.5k | std::vector<size_t> sizes(count, 0); |
1198 | 17.5k | size_t acc = base; |
1199 | 17.5k | const auto original_size = offsets.get_data().back(); |
1200 | 17.5k | offsets.get_data().reserve(offsets.get_data().size() + count); |
1201 | 227k | for (size_t i = 0; i < count; ++i) { |
1202 | 209k | size_t sz = static_cast<size_t>(next_starts_data[i] - starts_data[i]); |
1203 | 209k | if (_map_reader->is_nullable() && !null_mask.empty() && null_mask[i]) { |
1204 | 802 | sz = 0; // null rows do not consume elements |
1205 | 802 | } |
1206 | 209k | sizes[i] = sz; |
1207 | 209k | acc += sz; |
1208 | 209k | offsets.get_data().push_back(acc); |
1209 | 209k | } |
1210 | | |
1211 | | // 6. read key/value elements for non-empty sizes |
1212 | 17.5k | auto keys_ptr = IColumn::mutate(std::move(column_map.get_keys_ptr())); |
1213 | 17.5k | auto vals_ptr = IColumn::mutate(std::move(column_map.get_values_ptr())); |
1214 | 17.5k | Defer defer_keys {[&] { column_map.get_keys_ptr() = std::move(keys_ptr); }}; |
1215 | 17.5k | Defer defer_values {[&] { column_map.get_values_ptr() = std::move(vals_ptr); }}; |
1216 | | |
1217 | 17.5k | size_t this_run = sizes[0]; |
1218 | 17.5k | auto start_idx = starts_data[0]; |
1219 | 17.5k | auto last_idx = starts_data[0] + this_run; |
1220 | 210k | for (size_t i = 1; i < count; ++i) { |
1221 | 193k | size_t sz = sizes[i]; |
1222 | 193k | if (sz == 0) { |
1223 | 98.4k | continue; |
1224 | 98.4k | } |
1225 | 94.6k | auto start = static_cast<ordinal_t>(starts_data[i]); |
1226 | 94.6k | if (start != last_idx) { |
1227 | 341 | size_t n = this_run; |
1228 | 341 | bool dummy_has_null = false; |
1229 | | |
1230 | 341 | if (this_run != 0) { |
1231 | 339 | if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) { |
1232 | 339 | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx)); |
1233 | 339 | RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null)); |
1234 | 339 | DCHECK(n == this_run); |
1235 | 339 | } |
1236 | | |
1237 | 339 | if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) { |
1238 | 339 | n = this_run; |
1239 | 339 | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx)); |
1240 | 339 | RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null)); |
1241 | 339 | DCHECK(n == this_run); |
1242 | 339 | } |
1243 | 339 | } |
1244 | 341 | start_idx = start; |
1245 | 341 | this_run = sz; |
1246 | 341 | last_idx = start + sz; |
1247 | 341 | continue; |
1248 | 341 | } |
1249 | | |
1250 | 94.2k | this_run += sz; |
1251 | 94.2k | last_idx += sz; |
1252 | 94.2k | } |
1253 | | |
1254 | 17.5k | size_t n = this_run; |
1255 | 17.5k | const size_t total_count = offsets.get_data().back() - original_size; |
1256 | 17.5k | bool dummy_has_null = false; |
1257 | 17.5k | if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) { |
1258 | 17.5k | if (this_run != 0) { |
1259 | 8.05k | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx)); |
1260 | 8.05k | RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null)); |
1261 | 8.05k | DCHECK(n == this_run); |
1262 | 8.05k | } |
1263 | 17.5k | } else { |
1264 | 7 | keys_ptr->insert_many_defaults(total_count); |
1265 | 7 | } |
1266 | | |
1267 | 17.5k | if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) { |
1268 | 17.5k | if (this_run != 0) { |
1269 | 8.04k | n = this_run; |
1270 | 8.04k | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx)); |
1271 | 8.04k | RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null)); |
1272 | 8.04k | DCHECK(n == this_run); |
1273 | 8.04k | } |
1274 | 17.5k | } else { |
1275 | 17 | vals_ptr->insert_many_defaults(total_count); |
1276 | 17 | } |
1277 | | |
1278 | 17.5k | return Status::OK(); |
1279 | 17.5k | } |
1280 | | |
1281 | 6.60k | void MapFileColumnIterator::set_need_to_read() { |
1282 | 6.60k | set_reading_flag(ReadingFlag::NEED_TO_READ); |
1283 | 6.60k | _key_iterator->set_need_to_read(); |
1284 | 6.60k | _val_iterator->set_need_to_read(); |
1285 | 6.60k | } |
1286 | | |
1287 | 8.69k | void MapFileColumnIterator::remove_pruned_sub_iterators() { |
1288 | 8.69k | _key_iterator->remove_pruned_sub_iterators(); |
1289 | 8.69k | _val_iterator->remove_pruned_sub_iterators(); |
1290 | 8.69k | } |
1291 | | |
1292 | | Status MapFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths, |
1293 | 8.44k | const TColumnAccessPaths& predicate_access_paths) { |
1294 | 8.44k | if (all_access_paths.empty()) { |
1295 | 1.50k | return Status::OK(); |
1296 | 1.50k | } |
1297 | | |
1298 | 6.93k | if (!predicate_access_paths.empty()) { |
1299 | 201 | set_reading_flag(ReadingFlag::READING_FOR_PREDICATE); |
1300 | 201 | DLOG(INFO) << "Map column iterator set sub-column " << _column_name |
1301 | 201 | << " to READING_FOR_PREDICATE"; |
1302 | 201 | } |
1303 | | |
1304 | 6.93k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
1305 | 6.93k | auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths)); |
1306 | | |
1307 | 6.93k | if (sub_all_access_paths.empty()) { |
1308 | 4.85k | return Status::OK(); |
1309 | 4.85k | } |
1310 | | |
1311 | | // Check for meta-only modes (OFFSET_ONLY or NULL_MAP_ONLY) |
1312 | 2.08k | _check_and_set_meta_read_mode(sub_all_access_paths); |
1313 | 2.08k | if (read_offset_only()) { |
1314 | 463 | _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1315 | 463 | _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1316 | 463 | DLOG(INFO) << "Map column iterator set column " << _column_name |
1317 | 463 | << " to OFFSET_ONLY reading mode, key/value columns set to SKIP_READING"; |
1318 | 463 | return Status::OK(); |
1319 | 463 | } |
1320 | 1.61k | 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 | 1.60k | TColumnAccessPaths key_all_access_paths; |
1329 | 1.60k | TColumnAccessPaths val_all_access_paths; |
1330 | 1.60k | TColumnAccessPaths key_predicate_access_paths; |
1331 | 1.60k | TColumnAccessPaths val_predicate_access_paths; |
1332 | | |
1333 | 2.54k | for (auto paths : sub_all_access_paths) { |
1334 | 2.54k | 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 | 1.08k | TColumnAccessPath key_path; |
1340 | 1.08k | key_path.__set_type(paths.type); |
1341 | 1.08k | TDataAccessPath key_data_path; |
1342 | 1.08k | key_data_path.__set_path({_key_iterator->column_name()}); |
1343 | 1.08k | key_path.__set_data_access_path(key_data_path); |
1344 | 1.08k | key_all_access_paths.emplace_back(std::move(key_path)); |
1345 | | // For value: pass the full sub-path so qualifiers like OFFSET propagate. |
1346 | 1.08k | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1347 | 1.08k | val_all_access_paths.emplace_back(paths); |
1348 | 1.45k | } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) { |
1349 | 1.18k | paths.data_access_path.path[0] = _key_iterator->column_name(); |
1350 | 1.18k | key_all_access_paths.emplace_back(paths); |
1351 | 1.18k | } else if (paths.data_access_path.path[0] == ACCESS_MAP_VALUES) { |
1352 | 271 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1353 | 271 | val_all_access_paths.emplace_back(paths); |
1354 | 271 | } |
1355 | 2.54k | } |
1356 | 1.60k | const auto need_read_keys = !key_all_access_paths.empty(); |
1357 | 1.60k | const auto need_read_values = !val_all_access_paths.empty(); |
1358 | | |
1359 | 1.60k | for (auto paths : sub_predicate_access_paths) { |
1360 | 362 | if (paths.data_access_path.path[0] == ACCESS_ALL) { |
1361 | | // Same logic as above: key needs full data, value gets the sub-path. |
1362 | 189 | TColumnAccessPath key_path; |
1363 | 189 | key_path.__set_type(paths.type); |
1364 | 189 | TDataAccessPath key_data_path; |
1365 | 189 | key_data_path.__set_path({_key_iterator->column_name()}); |
1366 | 189 | key_path.__set_data_access_path(key_data_path); |
1367 | 189 | key_predicate_access_paths.emplace_back(std::move(key_path)); |
1368 | 189 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1369 | 189 | val_predicate_access_paths.emplace_back(paths); |
1370 | 189 | } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) { |
1371 | 172 | paths.data_access_path.path[0] = _key_iterator->column_name(); |
1372 | 172 | key_predicate_access_paths.emplace_back(paths); |
1373 | 172 | } 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 | 362 | } |
1378 | | |
1379 | 1.60k | if (need_read_keys) { |
1380 | 1.38k | _key_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ); |
1381 | 1.38k | RETURN_IF_ERROR( |
1382 | 1.38k | _key_iterator->set_access_paths(key_all_access_paths, key_predicate_access_paths)); |
1383 | 1.38k | } else { |
1384 | 212 | _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1385 | 212 | DLOG(INFO) << "Map column iterator set key column to SKIP_READING"; |
1386 | 212 | } |
1387 | | |
1388 | 1.60k | if (need_read_values) { |
1389 | 1.36k | _val_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ); |
1390 | 1.36k | RETURN_IF_ERROR( |
1391 | 1.36k | _val_iterator->set_access_paths(val_all_access_paths, val_predicate_access_paths)); |
1392 | 1.36k | } else { |
1393 | 240 | _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1394 | 240 | DLOG(INFO) << "Map column iterator set value column to SKIP_READING"; |
1395 | 240 | } |
1396 | 1.60k | return Status::OK(); |
1397 | 1.60k | } |
1398 | | |
1399 | | //////////////////////////////////////////////////////////////////////////////// |
1400 | | |
1401 | | StructFileColumnIterator::StructFileColumnIterator( |
1402 | | std::shared_ptr<ColumnReader> reader, ColumnIteratorUPtr null_iterator, |
1403 | | std::vector<ColumnIteratorUPtr>&& sub_column_iterators) |
1404 | 8.10k | : _struct_reader(reader), _sub_column_iterators(std::move(sub_column_iterators)) { |
1405 | 8.10k | if (_struct_reader->is_nullable()) { |
1406 | 7.25k | _null_iterator = std::move(null_iterator); |
1407 | 7.25k | } |
1408 | 8.10k | } |
1409 | | |
1410 | 8.07k | Status StructFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1411 | 8.07k | 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 | 29.7k | for (auto& column_iterator : _sub_column_iterators) { |
1417 | 29.7k | RETURN_IF_ERROR(column_iterator->init(opts)); |
1418 | 29.7k | } |
1419 | 8.07k | if (_struct_reader->is_nullable()) { |
1420 | 7.24k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
1421 | 7.24k | } |
1422 | 8.07k | return Status::OK(); |
1423 | 8.07k | } |
1424 | | |
1425 | 4.82k | Status StructFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1426 | 4.82k | 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.82k | 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.81k | auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>( |
1457 | 4.81k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1458 | 4.81k | : *dst); |
1459 | 21.6k | for (size_t i = 0; i < column_struct.tuple_size(); i++) { |
1460 | 16.8k | size_t num_read = *n; |
1461 | 16.8k | auto sub_column_ptr = IColumn::mutate(std::move(column_struct.get_column_ptr(i))); |
1462 | 16.8k | Defer defer_sub_column { |
1463 | 16.8k | [&] { column_struct.get_column_ptr(i) = std::move(sub_column_ptr); }}; |
1464 | 16.8k | bool column_has_null = false; |
1465 | 16.8k | RETURN_IF_ERROR( |
1466 | 16.8k | _sub_column_iterators[i]->next_batch(&num_read, sub_column_ptr, &column_has_null)); |
1467 | 16.8k | DCHECK(num_read == *n); |
1468 | 16.8k | } |
1469 | | |
1470 | 4.81k | if (is_column_nullable(*dst)) { |
1471 | 4.49k | size_t num_read = *n; |
1472 | 4.49k | 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.49k | if (_null_iterator) { |
1478 | 4.40k | bool null_signs_has_null = false; |
1479 | 4.40k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1480 | 4.40k | RETURN_IF_ERROR( |
1481 | 4.40k | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1482 | 4.40k | } else { |
1483 | 87 | null_map_ptr->insert_many_vals(0, num_read); |
1484 | 87 | } |
1485 | 4.49k | DCHECK(num_read == *n); |
1486 | 4.49k | } |
1487 | | |
1488 | 4.81k | return Status::OK(); |
1489 | 4.81k | } |
1490 | | |
1491 | 4.82k | Status StructFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
1492 | 4.82k | 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.82k | 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 | 16.8k | for (auto& column_iterator : _sub_column_iterators) { |
1506 | 16.8k | RETURN_IF_ERROR(column_iterator->seek_to_ordinal(ord)); |
1507 | 16.8k | } |
1508 | 4.81k | if (_struct_reader->is_nullable()) { |
1509 | 4.41k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1510 | 4.41k | } |
1511 | 4.81k | return Status::OK(); |
1512 | 4.81k | } |
1513 | | |
1514 | 441 | Status StructFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1515 | 1.32k | for (auto& column_iterator : _sub_column_iterators) { |
1516 | 1.32k | RETURN_IF_ERROR(column_iterator->init_prefetcher(params)); |
1517 | 1.32k | } |
1518 | 441 | if (_struct_reader->is_nullable()) { |
1519 | 435 | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
1520 | 435 | } |
1521 | 441 | return Status::OK(); |
1522 | 441 | } |
1523 | | |
1524 | | void StructFileColumnIterator::collect_prefetchers( |
1525 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1526 | 440 | PrefetcherInitMethod init_method) { |
1527 | 1.32k | for (auto& column_iterator : _sub_column_iterators) { |
1528 | 1.32k | column_iterator->collect_prefetchers(prefetchers, init_method); |
1529 | 1.32k | } |
1530 | 440 | if (_struct_reader->is_nullable()) { |
1531 | 435 | _null_iterator->collect_prefetchers(prefetchers, init_method); |
1532 | 435 | } |
1533 | 440 | } |
1534 | | |
1535 | | Status StructFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
1536 | 2.85k | MutableColumnPtr& dst) { |
1537 | 2.85k | 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.85k | if (count == 0) { |
1544 | 0 | return Status::OK(); |
1545 | 0 | } |
1546 | | |
1547 | 2.85k | size_t this_run = 1; |
1548 | 2.85k | auto start_idx = rowids[0]; |
1549 | 2.85k | auto last_idx = rowids[0]; |
1550 | 3.04k | for (size_t i = 1; i < count; ++i) { |
1551 | 188 | if (last_idx == rowids[i] - 1) { |
1552 | 182 | last_idx = rowids[i]; |
1553 | 182 | this_run++; |
1554 | 182 | continue; |
1555 | 182 | } |
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.85k | RETURN_IF_ERROR(seek_to_ordinal(start_idx)); |
1567 | 2.85k | size_t num_read = this_run; |
1568 | 2.85k | RETURN_IF_ERROR(next_batch(&num_read, dst)); |
1569 | 2.85k | DCHECK_EQ(num_read, this_run); |
1570 | 2.85k | return Status::OK(); |
1571 | 2.85k | } |
1572 | | |
1573 | 7.01k | void StructFileColumnIterator::set_need_to_read() { |
1574 | 7.01k | set_reading_flag(ReadingFlag::NEED_TO_READ); |
1575 | 26.9k | for (auto& sub_iterator : _sub_column_iterators) { |
1576 | 26.9k | sub_iterator->set_need_to_read(); |
1577 | 26.9k | } |
1578 | 7.01k | } |
1579 | | |
1580 | 7.23k | void StructFileColumnIterator::remove_pruned_sub_iterators() { |
1581 | 36.0k | for (auto it = _sub_column_iterators.begin(); it != _sub_column_iterators.end();) { |
1582 | 28.7k | auto& sub_iterator = *it; |
1583 | 28.7k | if (sub_iterator->reading_flag() == ReadingFlag::SKIP_READING) { |
1584 | 1.42k | DLOG(INFO) << "Struct column iterator remove pruned sub-column " |
1585 | 1.42k | << sub_iterator->column_name(); |
1586 | 1.42k | it = _sub_column_iterators.erase(it); |
1587 | 27.3k | } else { |
1588 | 27.3k | sub_iterator->remove_pruned_sub_iterators(); |
1589 | 27.3k | ++it; |
1590 | 27.3k | } |
1591 | 28.7k | } |
1592 | 7.23k | } |
1593 | | |
1594 | | Status StructFileColumnIterator::set_access_paths( |
1595 | | const TColumnAccessPaths& all_access_paths, |
1596 | 6.67k | const TColumnAccessPaths& predicate_access_paths) { |
1597 | 6.67k | if (all_access_paths.empty()) { |
1598 | 1.86k | return Status::OK(); |
1599 | 1.86k | } |
1600 | | |
1601 | 4.81k | 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.81k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
1607 | 4.80k | 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.80k | _check_and_set_meta_read_mode(sub_all_access_paths); |
1611 | 4.80k | 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.80k | const auto no_sub_column_to_skip = sub_all_access_paths.empty(); |
1621 | 4.80k | const auto no_predicate_sub_column = sub_predicate_access_paths.empty(); |
1622 | | |
1623 | 23.3k | for (auto& sub_iterator : _sub_column_iterators) { |
1624 | 23.3k | const auto name = sub_iterator->column_name(); |
1625 | 23.3k | bool need_to_read = no_sub_column_to_skip; |
1626 | 23.3k | TColumnAccessPaths sub_all_access_paths_of_this; |
1627 | 23.3k | if (!need_to_read) { |
1628 | 2.71k | for (const auto& paths : sub_all_access_paths) { |
1629 | 2.71k | if (paths.data_access_path.path[0] == name) { |
1630 | 457 | sub_all_access_paths_of_this.emplace_back(paths); |
1631 | 457 | } |
1632 | 2.71k | } |
1633 | 1.87k | need_to_read = !sub_all_access_paths_of_this.empty(); |
1634 | 1.87k | } |
1635 | | |
1636 | 23.3k | if (!need_to_read) { |
1637 | 1.41k | sub_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1638 | 1.41k | DLOG(INFO) << "Struct column iterator set sub-column " << name << " to SKIP_READING"; |
1639 | 1.41k | continue; |
1640 | 1.41k | } |
1641 | 21.9k | set_reading_flag(ReadingFlag::NEED_TO_READ); |
1642 | 21.9k | sub_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ); |
1643 | | |
1644 | 21.9k | TColumnAccessPaths sub_predicate_access_paths_of_this; |
1645 | | |
1646 | 21.9k | 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 | 21.9k | RETURN_IF_ERROR(sub_iterator->set_access_paths(sub_all_access_paths_of_this, |
1655 | 21.9k | sub_predicate_access_paths_of_this)); |
1656 | 21.9k | } |
1657 | 4.80k | return Status::OK(); |
1658 | 4.80k | } |
1659 | | |
1660 | | //////////////////////////////////////////////////////////////////////////////// |
1661 | 125k | Status OffsetFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1662 | 125k | RETURN_IF_ERROR(_offset_iterator->init(opts)); |
1663 | | // allocate peek tmp column once |
1664 | 125k | _peek_tmp_col = ColumnOffset64::create(); |
1665 | 125k | return Status::OK(); |
1666 | 125k | } |
1667 | | |
1668 | 159k | Status OffsetFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1669 | 159k | RETURN_IF_ERROR(_offset_iterator->next_batch(n, dst, has_null)); |
1670 | 159k | return Status::OK(); |
1671 | 159k | } |
1672 | | |
1673 | 300k | Status OffsetFileColumnIterator::_peek_one_offset(ordinal_t* offset) { |
1674 | 300k | if (_offset_iterator->get_current_page()->has_remaining()) { |
1675 | 196k | PageDecoder* offset_page_decoder = _offset_iterator->get_current_page()->data_decoder.get(); |
1676 | 196k | size_t n = 1; |
1677 | 196k | _peek_tmp_col->clear(); |
1678 | 196k | RETURN_IF_ERROR(offset_page_decoder->peek_next_batch(&n, _peek_tmp_col)); // not null |
1679 | 196k | DCHECK(_peek_tmp_col->size() == 1); |
1680 | 196k | *offset = |
1681 | 196k | assert_cast<const ColumnOffset64*, TypeCheckOnRelease::DISABLE>(_peek_tmp_col.get()) |
1682 | 196k | ->get_element(0); |
1683 | 196k | } else { |
1684 | 104k | *offset = _offset_iterator->get_current_page()->next_array_item_ordinal; |
1685 | 104k | } |
1686 | 300k | return Status::OK(); |
1687 | 300k | } |
1688 | | |
1689 | 6.62k | Status OffsetFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1690 | 6.62k | return _offset_iterator->init_prefetcher(params); |
1691 | 6.62k | } |
1692 | | |
1693 | | void OffsetFileColumnIterator::collect_prefetchers( |
1694 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1695 | 6.62k | PrefetcherInitMethod init_method) { |
1696 | 6.62k | _offset_iterator->collect_prefetchers(prefetchers, init_method); |
1697 | 6.62k | } |
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 | 143k | ColumnArray::ColumnOffsets& column_offsets) { |
1713 | 143k | ordinal_t next_storage_offset = 0; |
1714 | 143k | RETURN_IF_ERROR(_peek_one_offset(&next_storage_offset)); |
1715 | | |
1716 | | // calculate real offsets |
1717 | 143k | auto& offsets_data = column_offsets.get_data(); |
1718 | 143k | ordinal_t first_column_offset = offsets_data[start - 1]; // -1 is valid |
1719 | 143k | ordinal_t first_storage_offset = offsets_data[start]; |
1720 | 143k | DCHECK(next_storage_offset >= first_storage_offset); |
1721 | 13.0M | for (ssize_t i = start; i < offsets_data.size() - 1; ++i) { |
1722 | 12.9M | offsets_data[i] = first_column_offset + (offsets_data[i + 1] - first_storage_offset); |
1723 | 12.9M | } |
1724 | | // last offset |
1725 | 143k | offsets_data[offsets_data.size() - 1] = |
1726 | 143k | first_column_offset + (next_storage_offset - first_storage_offset); |
1727 | 143k | return Status::OK(); |
1728 | 143k | } |
1729 | | |
1730 | | //////////////////////////////////////////////////////////////////////////////// |
1731 | | ArrayFileColumnIterator::ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
1732 | | OffsetFileColumnIteratorUPtr offset_reader, |
1733 | | ColumnIteratorUPtr item_iterator, |
1734 | | ColumnIteratorUPtr null_iterator) |
1735 | 82.5k | : _array_reader(reader), |
1736 | 82.5k | _offset_iterator(std::move(offset_reader)), |
1737 | 82.5k | _item_iterator(std::move(item_iterator)) { |
1738 | 82.5k | if (_array_reader->is_nullable()) { |
1739 | 63.8k | _null_iterator = std::move(null_iterator); |
1740 | 63.8k | } |
1741 | 82.5k | } |
1742 | | |
1743 | 82.1k | Status ArrayFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1744 | 82.1k | 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 | 82.1k | RETURN_IF_ERROR(_offset_iterator->init(opts)); |
1750 | 82.1k | RETURN_IF_ERROR(_item_iterator->init(opts)); |
1751 | 82.1k | if (_array_reader->is_nullable()) { |
1752 | 63.7k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
1753 | 63.7k | } |
1754 | 82.1k | return Status::OK(); |
1755 | 82.1k | } |
1756 | | |
1757 | 120k | Status ArrayFileColumnIterator::_seek_by_offsets(ordinal_t ord) { |
1758 | 120k | if (read_offset_only()) { |
1759 | | // In OFFSET_ONLY mode, item iterator is SKIP_READING, no need to seek it |
1760 | 1.27k | return Status::OK(); |
1761 | 1.27k | } |
1762 | | // using offsets info |
1763 | 119k | ordinal_t offset = 0; |
1764 | 119k | RETURN_IF_ERROR(_offset_iterator->_peek_one_offset(&offset)); |
1765 | 119k | RETURN_IF_ERROR(_item_iterator->seek_to_ordinal(offset)); |
1766 | 119k | return Status::OK(); |
1767 | 119k | } |
1768 | | |
1769 | 120k | Status ArrayFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
1770 | 120k | 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 | 120k | 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 | 120k | RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord)); |
1784 | 120k | if (_array_reader->is_nullable()) { |
1785 | 101k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1786 | 101k | } |
1787 | 120k | return _seek_by_offsets(ord); |
1788 | 120k | } |
1789 | | |
1790 | 120k | Status ArrayFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1791 | 120k | 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 | 120k | 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 | 120k | auto& column_array = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>( |
1822 | 120k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1823 | 120k | : *dst); |
1824 | | |
1825 | 120k | bool offsets_has_null = false; |
1826 | 120k | auto column_offsets_ptr = std::move(*column_array.get_offsets_ptr()).mutate(); |
1827 | 120k | Defer defer_offsets {[&] { |
1828 | 120k | auto typed_column_offsets_ptr = ColumnArray::ColumnOffsets::cast_to_column_mutptr( |
1829 | 120k | assert_cast<ColumnArray::ColumnOffsets*, TypeCheckOnRelease::DISABLE>( |
1830 | 120k | column_offsets_ptr.get())); |
1831 | 120k | column_offsets_ptr = nullptr; |
1832 | 120k | column_array.get_offsets_ptr() = std::move(typed_column_offsets_ptr); |
1833 | 120k | }}; |
1834 | 120k | ssize_t start = column_offsets_ptr->size(); |
1835 | 120k | RETURN_IF_ERROR(_offset_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null)); |
1836 | 120k | if (*n == 0) { |
1837 | 0 | return Status::OK(); |
1838 | 0 | } |
1839 | 120k | auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr); |
1840 | 120k | RETURN_IF_ERROR(_offset_iterator->_calculate_offsets(start, column_offsets)); |
1841 | 120k | size_t num_items = |
1842 | 120k | column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid |
1843 | 120k | if (num_items > 0) { |
1844 | 92.7k | auto column_items_ptr = IColumn::mutate(std::move(column_array.get_data_ptr())); |
1845 | 92.7k | Defer defer_items {[&] { column_array.get_data_ptr() = std::move(column_items_ptr); }}; |
1846 | 92.7k | if (read_offset_only()) { |
1847 | | // OFFSET_ONLY mode: skip reading actual item data, fill with defaults |
1848 | 1.02k | column_items_ptr->insert_many_defaults(num_items); |
1849 | 91.7k | } else { |
1850 | 91.7k | size_t num_read = num_items; |
1851 | 91.7k | bool items_has_null = false; |
1852 | 91.7k | RETURN_IF_ERROR( |
1853 | 91.7k | _item_iterator->next_batch(&num_read, column_items_ptr, &items_has_null)); |
1854 | 91.7k | DCHECK(num_read == num_items); |
1855 | 91.7k | } |
1856 | 92.7k | } |
1857 | | |
1858 | 120k | if (is_column_nullable(*dst)) { |
1859 | 101k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1860 | 101k | 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 | 101k | if (_null_iterator) { |
1866 | 101k | bool null_signs_has_null = false; |
1867 | 101k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1868 | 101k | RETURN_IF_ERROR( |
1869 | 101k | _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 | 101k | DCHECK(num_read == *n); |
1874 | 101k | } |
1875 | | |
1876 | 120k | return Status::OK(); |
1877 | 120k | } |
1878 | | |
1879 | 5.27k | Status ArrayFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1880 | 5.27k | RETURN_IF_ERROR(_offset_iterator->init_prefetcher(params)); |
1881 | 5.27k | RETURN_IF_ERROR(_item_iterator->init_prefetcher(params)); |
1882 | 5.27k | if (_array_reader->is_nullable()) { |
1883 | 4.08k | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
1884 | 4.08k | } |
1885 | 5.27k | return Status::OK(); |
1886 | 5.27k | } |
1887 | | |
1888 | | void ArrayFileColumnIterator::collect_prefetchers( |
1889 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1890 | 5.27k | PrefetcherInitMethod init_method) { |
1891 | 5.27k | _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 | 5.27k | _item_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
1895 | 5.27k | if (_array_reader->is_nullable()) { |
1896 | 4.08k | _null_iterator->collect_prefetchers(prefetchers, init_method); |
1897 | 4.08k | } |
1898 | 5.27k | } |
1899 | | |
1900 | | Status ArrayFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
1901 | 43.0k | MutableColumnPtr& dst) { |
1902 | 43.0k | 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 | 131k | for (size_t i = 0; i < count; ++i) { |
1909 | | // TODO(cambyszju): now read array one by one, need optimize later |
1910 | 88.8k | RETURN_IF_ERROR(seek_to_ordinal(rowids[i])); |
1911 | 88.8k | size_t num_read = 1; |
1912 | 88.8k | RETURN_IF_ERROR(next_batch(&num_read, dst)); |
1913 | 88.8k | } |
1914 | 43.0k | return Status::OK(); |
1915 | 43.0k | } |
1916 | | |
1917 | 64.5k | void ArrayFileColumnIterator::set_need_to_read() { |
1918 | 64.5k | set_reading_flag(ReadingFlag::NEED_TO_READ); |
1919 | 64.5k | _item_iterator->set_need_to_read(); |
1920 | 64.5k | } |
1921 | | |
1922 | 64.8k | void ArrayFileColumnIterator::remove_pruned_sub_iterators() { |
1923 | 64.8k | _item_iterator->remove_pruned_sub_iterators(); |
1924 | 64.8k | } |
1925 | | |
1926 | | Status ArrayFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths, |
1927 | 64.0k | const TColumnAccessPaths& predicate_access_paths) { |
1928 | 64.0k | if (all_access_paths.empty()) { |
1929 | 1.75k | return Status::OK(); |
1930 | 1.75k | } |
1931 | | |
1932 | 62.2k | if (!predicate_access_paths.empty()) { |
1933 | 5.27k | set_reading_flag(ReadingFlag::READING_FOR_PREDICATE); |
1934 | 5.27k | DLOG(INFO) << "Array column iterator set sub-column " << _column_name |
1935 | 5.27k | << " to READING_FOR_PREDICATE"; |
1936 | 5.27k | } |
1937 | | |
1938 | 62.2k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
1939 | 62.2k | 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 | 62.2k | _check_and_set_meta_read_mode(sub_all_access_paths); |
1943 | 62.2k | if (read_offset_only()) { |
1944 | 1.23k | _item_iterator->set_reading_flag(ReadingFlag::SKIP_READING); |
1945 | 1.23k | DLOG(INFO) << "Array column iterator set column " << _column_name |
1946 | 1.23k | << " to OFFSET_ONLY reading mode, item column set to SKIP_READING"; |
1947 | 1.23k | return Status::OK(); |
1948 | 1.23k | } |
1949 | 61.0k | 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 | 61.0k | const auto no_sub_column_to_skip = sub_all_access_paths.empty(); |
1957 | 61.0k | const auto no_predicate_sub_column = sub_predicate_access_paths.empty(); |
1958 | | |
1959 | 61.0k | if (!no_sub_column_to_skip) { |
1960 | 4.21k | for (auto& path : sub_all_access_paths) { |
1961 | 4.21k | if (path.data_access_path.path[0] == ACCESS_ALL) { |
1962 | 4.18k | path.data_access_path.path[0] = _item_iterator->column_name(); |
1963 | 4.18k | } |
1964 | 4.21k | } |
1965 | 4.20k | } |
1966 | | |
1967 | 61.0k | if (!no_predicate_sub_column) { |
1968 | 372 | for (auto& path : sub_predicate_access_paths) { |
1969 | 372 | if (path.data_access_path.path[0] == ACCESS_ALL) { |
1970 | 372 | path.data_access_path.path[0] = _item_iterator->column_name(); |
1971 | 372 | } |
1972 | 372 | } |
1973 | 372 | } |
1974 | | |
1975 | 61.0k | if (!no_sub_column_to_skip || !no_predicate_sub_column) { |
1976 | 4.21k | _item_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ); |
1977 | 4.21k | RETURN_IF_ERROR( |
1978 | 4.21k | _item_iterator->set_access_paths(sub_all_access_paths, sub_predicate_access_paths)); |
1979 | 4.21k | } |
1980 | 61.0k | return Status::OK(); |
1981 | 61.0k | } |
1982 | | |
1983 | | //////////////////////////////////////////////////////////////////////////////// |
1984 | | // StringFileColumnIterator implementation |
1985 | | //////////////////////////////////////////////////////////////////////////////// |
1986 | | |
1987 | | StringFileColumnIterator::StringFileColumnIterator(std::shared_ptr<ColumnReader> reader) |
1988 | 16.3M | : FileColumnIterator(std::move(reader)) {} |
1989 | | |
1990 | 16.3M | Status StringFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1991 | 16.3M | if (read_offset_only()) { |
1992 | | // Propagate only_read_offsets to the FileColumnIterator's options |
1993 | 327 | auto modified_opts = opts; |
1994 | 327 | modified_opts.only_read_offsets = true; |
1995 | 327 | return FileColumnIterator::init(modified_opts); |
1996 | 327 | } |
1997 | 16.3M | return FileColumnIterator::init(opts); |
1998 | 16.3M | } |
1999 | | |
2000 | | Status StringFileColumnIterator::set_access_paths( |
2001 | | const TColumnAccessPaths& all_access_paths, |
2002 | 5.94k | const TColumnAccessPaths& predicate_access_paths) { |
2003 | 5.94k | if (all_access_paths.empty()) { |
2004 | 4.11k | return Status::OK(); |
2005 | 4.11k | } |
2006 | | |
2007 | 1.83k | if (!predicate_access_paths.empty()) { |
2008 | 388 | set_reading_flag(ReadingFlag::READING_FOR_PREDICATE); |
2009 | 388 | } |
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.83k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
2014 | 1.83k | _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.83k | if (read_offset_only() && get_reader() != nullptr && |
2029 | 1.83k | 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.83k | if (read_offset_only()) { |
2038 | 327 | DLOG(INFO) << "String column iterator set column " << _column_name |
2039 | 327 | << " to OFFSET_ONLY reading mode"; |
2040 | 1.50k | } else if (read_null_map_only()) { |
2041 | 284 | DLOG(INFO) << "String column iterator set column " << _column_name |
2042 | 284 | << " to NULL_MAP_ONLY reading mode"; |
2043 | 284 | } |
2044 | | |
2045 | 1.83k | return Status::OK(); |
2046 | 1.83k | } |
2047 | | |
2048 | | //////////////////////////////////////////////////////////////////////////////// |
2049 | | |
2050 | 27.3M | FileColumnIterator::FileColumnIterator(std::shared_ptr<ColumnReader> reader) : _reader(reader) {} |
2051 | | |
2052 | 71.0k | void ColumnIterator::_check_and_set_meta_read_mode(const TColumnAccessPaths& sub_all_access_paths) { |
2053 | 71.0k | for (const auto& path : sub_all_access_paths) { |
2054 | 9.52k | if (!path.data_access_path.path.empty()) { |
2055 | 9.52k | if (StringCaseEqual()(path.data_access_path.path[0], ACCESS_OFFSET)) { |
2056 | 2.02k | _read_mode = ReadMode::OFFSET_ONLY; |
2057 | 2.02k | return; |
2058 | 7.50k | } else if (StringCaseEqual()(path.data_access_path.path[0], ACCESS_NULL)) { |
2059 | 316 | _read_mode = ReadMode::NULL_MAP_ONLY; |
2060 | 316 | return; |
2061 | 316 | } |
2062 | 9.52k | } |
2063 | 9.49k | } |
2064 | 68.7k | _read_mode = ReadMode::DEFAULT; |
2065 | 68.7k | } |
2066 | | |
2067 | 27.2M | Status FileColumnIterator::init(const ColumnIteratorOptions& opts) { |
2068 | 27.2M | if (_reading_flag == ReadingFlag::SKIP_READING) { |
2069 | 2.40k | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2070 | 2.40k | return Status::OK(); |
2071 | 2.40k | } |
2072 | | |
2073 | 27.2M | _opts = opts; |
2074 | 27.2M | if (!_opts.use_page_cache) { |
2075 | 26.2M | _reader->disable_index_meta_cache(); |
2076 | 26.2M | } |
2077 | 27.2M | RETURN_IF_ERROR(get_block_compression_codec(_reader->get_compression(), &_compress_codec)); |
2078 | 27.2M | if (config::enable_low_cardinality_optimize && |
2079 | 27.3M | opts.io_ctx.reader_type == ReaderType::READER_QUERY && |
2080 | 27.2M | _reader->encoding_info()->encoding() == DICT_ENCODING) { |
2081 | 16.1M | 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.1M | if (dict_encoding_type == ColumnReader::UNKNOWN_DICT_ENCODING && opts.is_predicate_column) { |
2089 | 6.29k | RETURN_IF_ERROR(seek_to_ordinal(_reader->num_rows() - 1)); |
2090 | 6.29k | _is_all_dict_encoding = _page.is_dict_encoding; |
2091 | 6.29k | _reader->set_dict_encoding_type(_is_all_dict_encoding |
2092 | 6.29k | ? ColumnReader::ALL_DICT_ENCODING |
2093 | 6.29k | : ColumnReader::PARTIAL_DICT_ENCODING); |
2094 | 16.1M | } else { |
2095 | 16.1M | _is_all_dict_encoding = dict_encoding_type == ColumnReader::ALL_DICT_ENCODING; |
2096 | 16.1M | } |
2097 | 16.1M | } |
2098 | 27.2M | return Status::OK(); |
2099 | 27.2M | } |
2100 | | |
2101 | 27.4M | FileColumnIterator::~FileColumnIterator() = default; |
2102 | | |
2103 | 347k | void FileColumnIterator::_trigger_prefetch_if_eligible(ordinal_t ord) { |
2104 | 347k | std::vector<BlockRange> ranges; |
2105 | 347k | if (_prefetcher->need_prefetch(cast_set<uint32_t>(ord), &ranges)) { |
2106 | 301k | for (const auto& range : ranges) { |
2107 | 301k | _cached_remote_file_reader->prefetch_range(range.offset, range.size, &_opts.io_ctx); |
2108 | 301k | } |
2109 | 300k | } |
2110 | 347k | } |
2111 | | |
2112 | 4.21M | Status FileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
2113 | 4.21M | 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 | 4.21M | if (_enable_prefetch) { |
2122 | 347k | _trigger_prefetch_if_eligible(ord); |
2123 | 347k | } |
2124 | | |
2125 | | // if current page contains this row, we don't need to seek |
2126 | 4.21M | if (!_page || !_page.contains(ord) || !_page_iter.valid()) { |
2127 | 1.83M | RETURN_IF_ERROR(_reader->seek_at_or_before(ord, &_page_iter, _opts)); |
2128 | 1.83M | RETURN_IF_ERROR(_read_data_page(_page_iter)); |
2129 | 1.83M | } |
2130 | 4.21M | RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, ord - _page.first_ordinal)); |
2131 | 4.21M | _current_ordinal = ord; |
2132 | 4.21M | return Status::OK(); |
2133 | 4.21M | } |
2134 | | |
2135 | 0 | Status FileColumnIterator::seek_to_page_start() { |
2136 | 0 | return seek_to_ordinal(_page.first_ordinal); |
2137 | 0 | } |
2138 | | |
2139 | 4.27M | Status FileColumnIterator::_seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const { |
2140 | 4.27M | if (page->offset_in_page == offset_in_page) { |
2141 | | // fast path, do nothing |
2142 | 2.26M | return Status::OK(); |
2143 | 2.26M | } |
2144 | | |
2145 | 2.00M | ordinal_t pos_in_data = offset_in_page; |
2146 | 2.00M | if (_page.has_null) { |
2147 | 164k | ordinal_t offset_in_data = 0; |
2148 | 164k | ordinal_t skips = offset_in_page; |
2149 | | |
2150 | 164k | if (offset_in_page > page->offset_in_page) { |
2151 | | // forward, reuse null bitmap |
2152 | 87.0k | skips = offset_in_page - page->offset_in_page; |
2153 | 87.0k | offset_in_data = page->data_decoder->current_index(); |
2154 | 87.0k | } else { |
2155 | | // rewind null bitmap, and |
2156 | 77.6k | page->null_decoder = RleDecoder<bool>((const uint8_t*)page->null_bitmap.data, |
2157 | 77.6k | cast_set<int>(page->null_bitmap.size), 1); |
2158 | 77.6k | } |
2159 | | |
2160 | 164k | auto skip_nulls = page->null_decoder.Skip(skips); |
2161 | 164k | pos_in_data = offset_in_data + skips - skip_nulls; |
2162 | 164k | } |
2163 | | |
2164 | 2.00M | RETURN_IF_ERROR(page->data_decoder->seek_to_position_in_page(pos_in_data)); |
2165 | 2.00M | page->offset_in_page = offset_in_page; |
2166 | 2.00M | return Status::OK(); |
2167 | 2.00M | } |
2168 | | |
2169 | 33.0k | Status FileColumnIterator::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) { |
2170 | 33.0k | return _reader->next_batch_of_zone_map(n, dst); |
2171 | 33.0k | } |
2172 | | |
2173 | 3.28M | Status FileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2174 | 3.28M | if (read_null_map_only()) { |
2175 | 3.02k | DLOG(INFO) << "File column iterator column " << _column_name |
2176 | 3.02k | << " in NULL_MAP_ONLY mode, reading only null map."; |
2177 | 3.02k | DORIS_CHECK(is_column_nullable(*dst)); |
2178 | 3.02k | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
2179 | 3.02k | auto& null_map_data = nullable_col.get_null_map_data(); |
2180 | | |
2181 | 3.02k | size_t remaining = *n; |
2182 | 3.02k | *has_null = false; |
2183 | 6.09k | while (remaining > 0) { |
2184 | 3.07k | if (!_page.has_remaining()) { |
2185 | 48 | bool eos = false; |
2186 | 48 | RETURN_IF_ERROR(_load_next_page(&eos)); |
2187 | 48 | if (eos) { |
2188 | 0 | break; |
2189 | 0 | } |
2190 | 48 | } |
2191 | | |
2192 | 3.07k | size_t nrows_in_page = std::min(remaining, _page.remaining()); |
2193 | 3.07k | size_t nrows_to_read = nrows_in_page; |
2194 | 3.07k | if (_page.has_null) { |
2195 | 191k | while (nrows_to_read > 0) { |
2196 | 188k | bool is_null = false; |
2197 | 188k | size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read); |
2198 | 188k | const size_t cur_size = null_map_data.size(); |
2199 | 188k | null_map_data.resize(cur_size + this_run); |
2200 | 188k | memset(null_map_data.data() + cur_size, is_null ? 1 : 0, this_run); |
2201 | 188k | if (is_null) { |
2202 | 100k | *has_null = true; |
2203 | 100k | } |
2204 | 188k | nrows_to_read -= this_run; |
2205 | 188k | _page.offset_in_page += this_run; |
2206 | 188k | _current_ordinal += this_run; |
2207 | 188k | } |
2208 | 2.96k | } else { |
2209 | 104 | const size_t cur_size = null_map_data.size(); |
2210 | 104 | null_map_data.resize(cur_size + nrows_to_read); |
2211 | 104 | memset(null_map_data.data() + cur_size, 0, nrows_to_read); |
2212 | 104 | _page.offset_in_page += nrows_to_read; |
2213 | 104 | _current_ordinal += nrows_to_read; |
2214 | 104 | } |
2215 | 3.07k | remaining -= nrows_in_page; |
2216 | 3.07k | } |
2217 | 3.02k | *n -= remaining; |
2218 | 3.02k | nullable_col.get_nested_column().insert_many_defaults(*n); |
2219 | 3.02k | return Status::OK(); |
2220 | 3.02k | } |
2221 | | |
2222 | 3.28M | 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 | 3.28M | size_t curr_size = dst->byte_size(); |
2229 | 3.28M | dst->reserve(*n); |
2230 | 3.28M | size_t remaining = *n; |
2231 | 3.28M | *has_null = false; |
2232 | 6.65M | while (remaining > 0) { |
2233 | 3.34M | if (!_page.has_remaining()) { |
2234 | 57.4k | bool eos = false; |
2235 | 57.4k | RETURN_IF_ERROR(_load_next_page(&eos)); |
2236 | 57.4k | if (eos) { |
2237 | 0 | break; |
2238 | 0 | } |
2239 | 57.4k | } |
2240 | | |
2241 | | // number of rows to be read from this page |
2242 | 3.34M | size_t nrows_in_page = std::min(remaining, _page.remaining()); |
2243 | 3.34M | size_t nrows_to_read = nrows_in_page; |
2244 | 3.34M | if (_page.has_null) { |
2245 | 13.5M | while (nrows_to_read > 0) { |
2246 | 13.1M | bool is_null = false; |
2247 | 13.1M | size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read); |
2248 | | // we use num_rows only for CHECK |
2249 | 13.1M | size_t num_rows = this_run; |
2250 | 13.1M | if (!is_null) { |
2251 | 6.57M | RETURN_IF_ERROR(_page.data_decoder->next_batch(&num_rows, dst)); |
2252 | 6.57M | DCHECK_EQ(this_run, num_rows); |
2253 | 6.57M | } else { |
2254 | 6.55M | *has_null = true; |
2255 | 6.55M | auto* null_col = check_and_get_column<ColumnNullable>(dst.get()); |
2256 | 6.58M | if (null_col != nullptr) { |
2257 | 6.58M | 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 | 6.55M | } |
2262 | | |
2263 | 13.1M | nrows_to_read -= this_run; |
2264 | 13.1M | _page.offset_in_page += this_run; |
2265 | 13.1M | _current_ordinal += this_run; |
2266 | 13.1M | } |
2267 | 2.94M | } else { |
2268 | 2.94M | RETURN_IF_ERROR(_page.data_decoder->next_batch(&nrows_to_read, dst)); |
2269 | 2.94M | DCHECK_EQ(nrows_to_read, nrows_in_page); |
2270 | | |
2271 | 2.94M | _page.offset_in_page += nrows_to_read; |
2272 | 2.94M | _current_ordinal += nrows_to_read; |
2273 | 2.94M | } |
2274 | 3.37M | remaining -= nrows_in_page; |
2275 | 3.37M | } |
2276 | 3.31M | *n -= remaining; |
2277 | 3.31M | _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 | 3.31M | return Status::OK(); |
2283 | 3.28M | } |
2284 | | |
2285 | | Status FileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
2286 | 884k | MutableColumnPtr& dst) { |
2287 | 884k | if (read_null_map_only()) { |
2288 | 26 | DLOG(INFO) << "File column iterator column " << _column_name |
2289 | 26 | << " in NULL_MAP_ONLY mode, reading only null map by rowids."; |
2290 | | |
2291 | 26 | DORIS_CHECK(is_column_nullable(*dst)); |
2292 | 26 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
2293 | 26 | auto& null_map_data = nullable_col.get_null_map_data(); |
2294 | 26 | const size_t base_size = null_map_data.size(); |
2295 | 26 | null_map_data.resize(base_size + count); |
2296 | | |
2297 | 26 | size_t remaining = count; |
2298 | 26 | size_t total_read_count = 0; |
2299 | 26 | size_t nrows_to_read = 0; |
2300 | 52 | while (remaining > 0) { |
2301 | 26 | RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count])); |
2302 | | |
2303 | 26 | nrows_to_read = std::min(remaining, _page.remaining()); |
2304 | | |
2305 | 26 | if (_page.has_null) { |
2306 | 6 | size_t already_read = 0; |
2307 | 12 | while ((nrows_to_read - already_read) > 0) { |
2308 | 6 | bool is_null = false; |
2309 | 6 | size_t this_run = std::min(nrows_to_read - already_read, _page.remaining()); |
2310 | 6 | if (UNLIKELY(this_run == 0)) { |
2311 | 0 | break; |
2312 | 0 | } |
2313 | 6 | this_run = _page.null_decoder.GetNextRun(&is_null, this_run); |
2314 | | |
2315 | 6 | size_t offset = total_read_count + already_read; |
2316 | 6 | size_t this_read_count = 0; |
2317 | 6 | rowid_t current_ordinal_in_page = |
2318 | 6 | cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal); |
2319 | 12 | for (size_t i = 0; i < this_run; ++i) { |
2320 | 6 | if (rowids[offset + i] - current_ordinal_in_page >= this_run) { |
2321 | 0 | break; |
2322 | 0 | } |
2323 | 6 | this_read_count++; |
2324 | 6 | } |
2325 | | |
2326 | 6 | if (this_read_count > 0) { |
2327 | 6 | memset(null_map_data.data() + base_size + offset, is_null ? 1 : 0, |
2328 | 6 | this_read_count); |
2329 | 6 | } |
2330 | | |
2331 | 6 | already_read += this_read_count; |
2332 | 6 | _page.offset_in_page += this_run; |
2333 | 6 | } |
2334 | | |
2335 | 6 | nrows_to_read = already_read; |
2336 | 6 | total_read_count += nrows_to_read; |
2337 | 6 | remaining -= nrows_to_read; |
2338 | 20 | } else { |
2339 | 20 | memset(null_map_data.data() + base_size + total_read_count, 0, nrows_to_read); |
2340 | 20 | total_read_count += nrows_to_read; |
2341 | 20 | remaining -= nrows_to_read; |
2342 | 20 | } |
2343 | 26 | } |
2344 | | |
2345 | 26 | null_map_data.resize(base_size + total_read_count); |
2346 | 26 | nullable_col.get_nested_column().insert_many_defaults(total_read_count); |
2347 | 26 | return Status::OK(); |
2348 | 26 | } |
2349 | | |
2350 | 884k | 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 | 884k | size_t remaining = count; |
2357 | 884k | size_t total_read_count = 0; |
2358 | 884k | size_t nrows_to_read = 0; |
2359 | 1.79M | while (remaining > 0) { |
2360 | 915k | RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count])); |
2361 | | |
2362 | | // number of rows to be read from this page |
2363 | 915k | nrows_to_read = std::min(remaining, _page.remaining()); |
2364 | | |
2365 | 915k | if (_page.has_null) { |
2366 | 87.9k | size_t already_read = 0; |
2367 | 4.26M | while ((nrows_to_read - already_read) > 0) { |
2368 | 4.17M | bool is_null = false; |
2369 | 4.17M | size_t this_run = std::min(nrows_to_read - already_read, _page.remaining()); |
2370 | 4.17M | if (UNLIKELY(this_run == 0)) { |
2371 | 178 | break; |
2372 | 178 | } |
2373 | 4.17M | this_run = _page.null_decoder.GetNextRun(&is_null, this_run); |
2374 | 4.17M | size_t offset = total_read_count + already_read; |
2375 | 4.17M | size_t this_read_count = 0; |
2376 | 4.17M | rowid_t current_ordinal_in_page = |
2377 | 4.17M | cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal); |
2378 | 20.7M | for (size_t i = 0; i < this_run; ++i) { |
2379 | 19.4M | if (rowids[offset + i] - current_ordinal_in_page >= this_run) { |
2380 | 2.90M | break; |
2381 | 2.90M | } |
2382 | 16.5M | this_read_count++; |
2383 | 16.5M | } |
2384 | | |
2385 | 4.17M | auto origin_index = _page.data_decoder->current_index(); |
2386 | 4.17M | if (this_read_count > 0) { |
2387 | 1.29M | if (is_null) { |
2388 | 685k | auto* null_col = check_and_get_column<ColumnNullable>(dst.get()); |
2389 | 685k | if (UNLIKELY(null_col == nullptr)) { |
2390 | 0 | return Status::InternalError("unexpected column type in column reader"); |
2391 | 0 | } |
2392 | | |
2393 | 685k | null_col->insert_many_defaults(this_read_count); |
2394 | 685k | } else { |
2395 | 610k | 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 | 610k | size_t page_start_off_in_decoder = |
2400 | 610k | _page.first_ordinal + _page.offset_in_page - origin_index; |
2401 | 610k | RETURN_IF_ERROR(_page.data_decoder->read_by_rowids( |
2402 | 610k | &rowids[offset], page_start_off_in_decoder, &read_count, dst)); |
2403 | 610k | DCHECK_EQ(read_count, this_read_count); |
2404 | 610k | } |
2405 | 1.29M | } |
2406 | | |
2407 | 4.17M | if (!is_null) { |
2408 | 3.42M | RETURN_IF_ERROR( |
2409 | 3.42M | _page.data_decoder->seek_to_position_in_page(origin_index + this_run)); |
2410 | 3.42M | } |
2411 | | |
2412 | 4.17M | already_read += this_read_count; |
2413 | 4.17M | _page.offset_in_page += this_run; |
2414 | 4.17M | DCHECK(_page.offset_in_page <= _page.num_rows); |
2415 | 4.17M | } |
2416 | | |
2417 | 87.9k | nrows_to_read = already_read; |
2418 | 87.9k | total_read_count += nrows_to_read; |
2419 | 87.9k | remaining -= nrows_to_read; |
2420 | 827k | } else { |
2421 | 827k | RETURN_IF_ERROR(_page.data_decoder->read_by_rowids( |
2422 | 827k | &rowids[total_read_count], _page.first_ordinal, &nrows_to_read, dst)); |
2423 | 827k | total_read_count += nrows_to_read; |
2424 | 827k | remaining -= nrows_to_read; |
2425 | 827k | } |
2426 | 915k | } |
2427 | 884k | return Status::OK(); |
2428 | 884k | } |
2429 | | |
2430 | 57.5k | Status FileColumnIterator::_load_next_page(bool* eos) { |
2431 | 57.5k | _page_iter.next(); |
2432 | 57.5k | if (!_page_iter.valid()) { |
2433 | 0 | *eos = true; |
2434 | 0 | return Status::OK(); |
2435 | 0 | } |
2436 | | |
2437 | 57.5k | RETURN_IF_ERROR(_read_data_page(_page_iter)); |
2438 | 57.5k | RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, 0)); |
2439 | 57.5k | *eos = false; |
2440 | 57.5k | return Status::OK(); |
2441 | 57.5k | } |
2442 | | |
2443 | 1.88M | Status FileColumnIterator::_read_data_page(const OrdinalPageIndexIterator& iter) { |
2444 | 1.88M | PageHandle handle; |
2445 | 1.88M | Slice page_body; |
2446 | 1.88M | PageFooterPB footer; |
2447 | 1.88M | _opts.type = DATA_PAGE; |
2448 | 1.88M | PageDecoderOptions decoder_opts; |
2449 | 1.88M | decoder_opts.only_read_offsets = _opts.only_read_offsets; |
2450 | 1.88M | RETURN_IF_ERROR( |
2451 | 1.88M | _reader->read_page(_opts, iter.page(), &handle, &page_body, &footer, _compress_codec)); |
2452 | | // parse data page |
2453 | 1.88M | auto st = ParsedPage::create(std::move(handle), page_body, footer.data_page_footer(), |
2454 | 1.88M | _reader->encoding_info(), iter.page(), iter.page_index(), &_page, |
2455 | 1.88M | decoder_opts); |
2456 | 1.88M | 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.88M | if (_reader->encoding_info()->encoding() == DICT_ENCODING) { |
2469 | 513k | auto dict_page_decoder = reinterpret_cast<BinaryDictPageDecoder*>(_page.data_decoder.get()); |
2470 | 513k | if (dict_page_decoder->is_dict_encoding()) { |
2471 | 493k | if (_dict_decoder == nullptr) { |
2472 | 484k | RETURN_IF_ERROR(_read_dict_data()); |
2473 | 484k | CHECK_NOTNULL(_dict_decoder); |
2474 | 484k | } |
2475 | | |
2476 | 493k | dict_page_decoder->set_dict_decoder(cast_set<uint32_t>(_dict_decoder->count()), |
2477 | 493k | _dict_word_info.get()); |
2478 | 493k | } |
2479 | 513k | } |
2480 | 1.88M | return Status::OK(); |
2481 | 1.88M | } |
2482 | | |
2483 | 489k | Status FileColumnIterator::_read_dict_data() { |
2484 | 489k | CHECK_EQ(_reader->encoding_info()->encoding(), DICT_ENCODING); |
2485 | | // read dictionary page |
2486 | 489k | Slice dict_data; |
2487 | 489k | PageFooterPB dict_footer; |
2488 | 489k | _opts.type = INDEX_PAGE; |
2489 | | |
2490 | 489k | RETURN_IF_ERROR(_reader->read_page(_opts, _reader->get_dict_page_pointer(), &_dict_page_handle, |
2491 | 489k | &dict_data, &dict_footer, _compress_codec)); |
2492 | 489k | 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 | 489k | RETURN_IF_ERROR(EncodingInfo::get(_reader->get_meta_type(), |
2497 | 489k | dict_footer.dict_page_footer().encoding(), &encoding_info)); |
2498 | 489k | RETURN_IF_ERROR(encoding_info->create_page_decoder(dict_data, {}, _dict_decoder)); |
2499 | 489k | RETURN_IF_ERROR(_dict_decoder->init()); |
2500 | | |
2501 | 489k | _dict_word_info.reset(new StringRef[_dict_decoder->count()]); |
2502 | 489k | RETURN_IF_ERROR(_dict_decoder->get_dict_word_info(_dict_word_info.get())); |
2503 | 489k | return Status::OK(); |
2504 | 489k | } |
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 | 137k | RowRanges* row_ranges) { |
2510 | 137k | if (_reader->has_zone_map()) { |
2511 | 137k | RETURN_IF_ERROR(_reader->get_row_ranges_by_zone_map(col_predicates, delete_predicates, |
2512 | 137k | row_ranges, _opts)); |
2513 | 137k | } |
2514 | 137k | return Status::OK(); |
2515 | 137k | } |
2516 | | |
2517 | | Status FileColumnIterator::get_row_ranges_by_bloom_filter( |
2518 | 137k | const AndBlockColumnPredicate* col_predicates, RowRanges* row_ranges) { |
2519 | 137k | if ((col_predicates->can_do_bloom_filter(false) && _reader->has_bloom_filter_index(false)) || |
2520 | 137k | (col_predicates->can_do_bloom_filter(true) && _reader->has_bloom_filter_index(true))) { |
2521 | 184 | RETURN_IF_ERROR(_reader->get_row_ranges_by_bloom_filter(col_predicates, row_ranges, _opts)); |
2522 | 184 | } |
2523 | 137k | return Status::OK(); |
2524 | 137k | } |
2525 | | |
2526 | | Status FileColumnIterator::get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
2527 | 139k | RowRanges* row_ranges) { |
2528 | 139k | if (!_is_all_dict_encoding) { |
2529 | 128k | return Status::OK(); |
2530 | 128k | } |
2531 | | |
2532 | 11.3k | if (!_dict_decoder) { |
2533 | 5.65k | RETURN_IF_ERROR(_read_dict_data()); |
2534 | 5.65k | CHECK_NOTNULL(_dict_decoder); |
2535 | 5.65k | } |
2536 | | |
2537 | 11.3k | if (!col_predicates->evaluate_and(_dict_word_info.get(), _dict_decoder->count())) { |
2538 | 1.15k | row_ranges->clear(); |
2539 | 1.15k | } |
2540 | 11.3k | return Status::OK(); |
2541 | 11.3k | } |
2542 | | |
2543 | 300k | Status FileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
2544 | 300k | if (_cached_remote_file_reader = |
2545 | 300k | std::dynamic_pointer_cast<io::CachedRemoteFileReader>(_reader->_file_reader); |
2546 | 300k | !_cached_remote_file_reader) { |
2547 | 0 | return Status::OK(); |
2548 | 0 | } |
2549 | 300k | _enable_prefetch = true; |
2550 | 300k | _prefetcher = std::make_unique<SegmentPrefetcher>(params.config); |
2551 | 300k | RETURN_IF_ERROR(_prefetcher->init(_reader, params.read_options)); |
2552 | 300k | return Status::OK(); |
2553 | 300k | } |
2554 | | |
2555 | | void FileColumnIterator::collect_prefetchers( |
2556 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
2557 | 300k | PrefetcherInitMethod init_method) { |
2558 | 300k | if (_prefetcher) { |
2559 | 300k | prefetchers[init_method].emplace_back(_prefetcher.get()); |
2560 | 300k | } |
2561 | 300k | } |
2562 | | |
2563 | 26.3k | Status DefaultValueColumnIterator::init(const ColumnIteratorOptions& opts) { |
2564 | 26.3k | _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 | 26.3k | if (_has_default_value) { |
2569 | 1.72k | if (_default_value == "NULL") { |
2570 | 803 | _default_value_field = Field::create_field<TYPE_NULL>(Null {}); |
2571 | 924 | } else { |
2572 | 924 | 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 | 916 | } else if (_type == FieldType::OLAP_FIELD_TYPE_STRUCT) { |
2580 | 0 | return Status::NotSupported("STRUCT default type is unsupported"); |
2581 | 916 | } else if (_type == FieldType::OLAP_FIELD_TYPE_MAP) { |
2582 | 0 | return Status::NotSupported("MAP default type is unsupported"); |
2583 | 0 | } |
2584 | 916 | const auto t = _type; |
2585 | 916 | const auto serde = DataTypeFactory::instance() |
2586 | 916 | .create_data_type(t, _precision, _scale, _len) |
2587 | 916 | ->get_serde(); |
2588 | 916 | RETURN_IF_ERROR(serde->from_fe_string(_default_value, _default_value_field)); |
2589 | 916 | } |
2590 | 24.6k | } else if (_is_nullable) { |
2591 | 24.6k | _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 | 26.3k | return Status::OK(); |
2597 | 26.3k | } |
2598 | | |
2599 | 5.36k | Status DefaultValueColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2600 | 5.36k | *has_null = _default_value_field.is_null(); |
2601 | 5.36k | _insert_many_default(dst, *n); |
2602 | 5.36k | return Status::OK(); |
2603 | 5.36k | } |
2604 | | |
2605 | | Status DefaultValueColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
2606 | 5.86k | MutableColumnPtr& dst) { |
2607 | 5.86k | _insert_many_default(dst, count); |
2608 | 5.86k | return Status::OK(); |
2609 | 5.86k | } |
2610 | | |
2611 | 11.2k | void DefaultValueColumnIterator::_insert_many_default(MutableColumnPtr& dst, size_t n) { |
2612 | 11.2k | if (_default_value_field.is_null()) { |
2613 | 10.7k | dst->insert_many_defaults(n); |
2614 | 10.7k | } else { |
2615 | 479 | dst = dst->convert_to_predicate_column_if_dictionary(); |
2616 | 479 | dst->insert_duplicate_fields(_default_value_field, n); |
2617 | 479 | } |
2618 | 11.2k | } |
2619 | | |
2620 | 4.95k | Status RowIdColumnIteratorV2::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2621 | 4.95k | auto* string_column = assert_cast<ColumnString*, TypeCheckOnRelease::DISABLE>(dst.get()); |
2622 | | |
2623 | 12.3M | for (uint32_t i = 0; i < *n; ++i) { |
2624 | 12.3M | uint32_t row_id = _current_rowid + i; |
2625 | 12.3M | GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id); |
2626 | 12.3M | string_column->insert_data(reinterpret_cast<const char*>(&location), |
2627 | 12.3M | sizeof(GlobalRowLoacationV2)); |
2628 | 12.3M | } |
2629 | 4.95k | _current_rowid += *n; |
2630 | 4.95k | return Status::OK(); |
2631 | 4.95k | } |
2632 | | |
2633 | | Status RowIdColumnIteratorV2::read_by_rowids(const rowid_t* rowids, const size_t count, |
2634 | 43.3k | MutableColumnPtr& dst) { |
2635 | 43.3k | auto* string_column = assert_cast<ColumnString*>(dst.get()); |
2636 | | |
2637 | 32.0M | for (size_t i = 0; i < count; ++i) { |
2638 | 31.9M | uint32_t row_id = rowids[i]; |
2639 | 31.9M | GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id); |
2640 | 31.9M | string_column->insert_data(reinterpret_cast<const char*>(&location), |
2641 | 31.9M | sizeof(GlobalRowLoacationV2)); |
2642 | 31.9M | } |
2643 | 43.3k | return Status::OK(); |
2644 | 43.3k | } |
2645 | | |
2646 | | } // namespace doris::segment_v2 |