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