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 <string> |
30 | | #include <utility> |
31 | | |
32 | | #include "common/compiler_util.h" // IWYU pragma: keep |
33 | | #include "common/status.h" |
34 | | #include "core/assert_cast.h" |
35 | | #include "core/binary_cast.hpp" |
36 | | #include "core/column/column.h" |
37 | | #include "core/column/column_array.h" |
38 | | #include "core/column/column_map.h" |
39 | | #include "core/column/column_nullable.h" |
40 | | #include "core/column/column_struct.h" |
41 | | #include "core/column/column_vector.h" |
42 | | #include "core/data_type/data_type_agg_state.h" |
43 | | #include "core/data_type/data_type_factory.hpp" |
44 | | #include "core/data_type/data_type_nullable.h" |
45 | | #include "core/data_type/define_primitive_type.h" |
46 | | #include "core/decimal12.h" |
47 | | #include "core/string_ref.h" |
48 | | #include "core/types.h" |
49 | | #include "core/value/decimalv2_value.h" |
50 | | #include "core/value/vdatetime_value.h" //for VecDateTime |
51 | | #include "io/fs/file_reader.h" |
52 | | #include "storage/index/ann/ann_index_reader.h" |
53 | | #include "storage/index/bloom_filter/bloom_filter.h" |
54 | | #include "storage/index/bloom_filter/bloom_filter_index_reader.h" |
55 | | #include "storage/index/index_file_reader.h" |
56 | | #include "storage/index/index_reader.h" |
57 | | #include "storage/index/inverted/analyzer/analyzer.h" |
58 | | #include "storage/index/inverted/inverted_index_reader.h" |
59 | | #include "storage/index/zone_map/zone_map_index.h" |
60 | | #include "storage/iterators.h" |
61 | | #include "storage/olap_common.h" |
62 | | #include "storage/predicate/block_column_predicate.h" |
63 | | #include "storage/predicate/column_predicate.h" |
64 | | #include "storage/segment/binary_dict_page.h" // for BinaryDictPageDecoder |
65 | | #include "storage/segment/binary_plain_page.h" |
66 | | #include "storage/segment/column_meta_accessor.h" |
67 | | #include "storage/segment/encoding_info.h" // for EncodingInfo |
68 | | #include "storage/segment/page_decoder.h" |
69 | | #include "storage/segment/page_handle.h" // for PageHandle |
70 | | #include "storage/segment/page_io.h" |
71 | | #include "storage/segment/page_pointer.h" // for PagePointer |
72 | | #include "storage/segment/row_ranges.h" |
73 | | #include "storage/segment/segment.h" |
74 | | #include "storage/segment/segment_prefetcher.h" |
75 | | #include "storage/segment/variant/variant_column_reader.h" |
76 | | #include "storage/tablet/tablet_schema.h" |
77 | | #include "storage/types.h" // for TypeInfo |
78 | | #include "util/bitmap.h" |
79 | | #include "util/block_compression.h" |
80 | | #include "util/concurrency_stats.h" |
81 | | #include "util/defer_op.h" |
82 | | #include "util/rle_encoding.h" // for RleDecoder |
83 | | #include "util/slice.h" |
84 | | |
85 | | namespace doris::segment_v2 { |
86 | | #include "storage/segment/column_reader.h" |
87 | | |
88 | 644 | inline bool read_as_string(PrimitiveType type) { |
89 | 644 | return type == PrimitiveType::TYPE_STRING || type == PrimitiveType::INVALID_TYPE || |
90 | 644 | type == PrimitiveType::TYPE_BITMAP || type == PrimitiveType::TYPE_FIXED_LENGTH_OBJECT; |
91 | 644 | } |
92 | | |
93 | 15.0k | bool is_current_level_meta_access_path(const TColumnAccessPath& path) { |
94 | 15.0k | if (path.data_access_path.path.size() != 1) { |
95 | 1.58k | return false; |
96 | 1.58k | } |
97 | 13.4k | const auto& component = path.data_access_path.path[0]; |
98 | 13.4k | return StringCaseEqual()(component, ColumnIterator::ACCESS_OFFSET) || |
99 | 13.4k | StringCaseEqual()(component, ColumnIterator::ACCESS_NULL); |
100 | 15.0k | } |
101 | | |
102 | | bool is_current_level_data_access_path(const TColumnAccessPath& path, |
103 | 65.7k | const std::string& column_name) { |
104 | 65.7k | return path.data_access_path.path.size() == 1 && |
105 | 65.7k | StringCaseEqual()(path.data_access_path.path[0], column_name); |
106 | 65.7k | } |
107 | | |
108 | 116k | void remove_current_level_meta_access_paths(TColumnAccessPaths& paths) { |
109 | 116k | auto removed = std::ranges::remove_if(paths, is_current_level_meta_access_path); |
110 | 116k | paths.erase(removed.begin(), removed.end()); |
111 | 116k | } |
112 | | |
113 | | Status ColumnReader::create_array(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
114 | | const io::FileReaderSPtr& file_reader, |
115 | 51.8k | std::shared_ptr<ColumnReader>* reader) { |
116 | 51.8k | DCHECK(meta.children_columns_size() == 2 || meta.children_columns_size() == 3); |
117 | | |
118 | 51.8k | std::shared_ptr<ColumnReader> item_reader; |
119 | 51.8k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0), |
120 | 51.8k | meta.children_columns(0).num_rows(), file_reader, |
121 | 51.8k | &item_reader)); |
122 | | |
123 | 51.8k | std::shared_ptr<ColumnReader> offset_reader; |
124 | 51.8k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1), |
125 | 51.8k | meta.children_columns(1).num_rows(), file_reader, |
126 | 51.8k | &offset_reader)); |
127 | | |
128 | 51.8k | std::shared_ptr<ColumnReader> null_reader; |
129 | 51.8k | if (meta.is_nullable()) { |
130 | 35.9k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2), |
131 | 35.9k | meta.children_columns(2).num_rows(), file_reader, |
132 | 35.9k | &null_reader)); |
133 | 35.9k | } |
134 | | |
135 | | // The num rows of the array reader equals to the num rows of the length reader. |
136 | 51.8k | uint64_t array_num_rows = meta.children_columns(1).num_rows(); |
137 | 51.8k | std::shared_ptr<ColumnReader> array_reader( |
138 | 51.8k | new ColumnReader(opts, meta, array_num_rows, file_reader)); |
139 | | // array reader do not need to init |
140 | 51.8k | array_reader->_sub_readers.resize(meta.children_columns_size()); |
141 | 51.8k | array_reader->_sub_readers[0] = std::move(item_reader); |
142 | 51.8k | array_reader->_sub_readers[1] = std::move(offset_reader); |
143 | 51.8k | if (meta.is_nullable()) { |
144 | 35.9k | array_reader->_sub_readers[2] = std::move(null_reader); |
145 | 35.9k | } |
146 | 51.8k | array_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_ARRAY; |
147 | 51.8k | *reader = std::move(array_reader); |
148 | 51.8k | return Status::OK(); |
149 | 51.8k | } |
150 | | |
151 | | Status ColumnReader::create_map(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
152 | | const io::FileReaderSPtr& file_reader, |
153 | 33.8k | std::shared_ptr<ColumnReader>* reader) { |
154 | | // map reader now has 3 sub readers for key, value, offsets(scalar), null(scala) |
155 | 33.8k | DCHECK(meta.children_columns_size() == 3 || meta.children_columns_size() == 4); |
156 | 33.8k | std::shared_ptr<ColumnReader> key_reader; |
157 | 33.8k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0), |
158 | 33.8k | meta.children_columns(0).num_rows(), file_reader, |
159 | 33.8k | &key_reader)); |
160 | 33.8k | std::shared_ptr<ColumnReader> val_reader; |
161 | 33.8k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1), |
162 | 33.8k | meta.children_columns(1).num_rows(), file_reader, |
163 | 33.8k | &val_reader)); |
164 | 33.8k | std::shared_ptr<ColumnReader> offset_reader; |
165 | 33.8k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2), |
166 | 33.8k | meta.children_columns(2).num_rows(), file_reader, |
167 | 33.8k | &offset_reader)); |
168 | 33.8k | std::shared_ptr<ColumnReader> null_reader; |
169 | 33.8k | if (meta.is_nullable()) { |
170 | 13.3k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(3), |
171 | 13.3k | meta.children_columns(3).num_rows(), file_reader, |
172 | 13.3k | &null_reader)); |
173 | 13.3k | } |
174 | | |
175 | | // The num rows of the map reader equals to the num rows of the length reader. |
176 | 33.8k | uint64_t map_num_rows = meta.children_columns(2).num_rows(); |
177 | 33.8k | std::shared_ptr<ColumnReader> map_reader( |
178 | 33.8k | new ColumnReader(opts, meta, map_num_rows, file_reader)); |
179 | 33.8k | map_reader->_sub_readers.resize(meta.children_columns_size()); |
180 | | |
181 | 33.8k | map_reader->_sub_readers[0] = std::move(key_reader); |
182 | 33.8k | map_reader->_sub_readers[1] = std::move(val_reader); |
183 | 33.8k | map_reader->_sub_readers[2] = std::move(offset_reader); |
184 | 33.8k | if (meta.is_nullable()) { |
185 | 13.2k | map_reader->_sub_readers[3] = std::move(null_reader); |
186 | 13.2k | } |
187 | 33.8k | map_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_MAP; |
188 | 33.8k | *reader = std::move(map_reader); |
189 | 33.8k | return Status::OK(); |
190 | 33.8k | } |
191 | | |
192 | | Status ColumnReader::create_struct(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
193 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
194 | 2.87k | std::shared_ptr<ColumnReader>* reader) { |
195 | | // not support empty struct |
196 | 2.87k | DCHECK(meta.children_columns_size() >= 1); |
197 | | // create struct column reader |
198 | 2.87k | std::shared_ptr<ColumnReader> struct_reader( |
199 | 2.87k | new ColumnReader(opts, meta, num_rows, file_reader)); |
200 | 2.87k | struct_reader->_sub_readers.reserve(meta.children_columns_size()); |
201 | | // now we support struct column can add the children columns according to the schema-change behavior |
202 | 16.8k | for (int i = 0; i < meta.children_columns_size(); i++) { |
203 | 13.9k | std::shared_ptr<ColumnReader> sub_reader; |
204 | 13.9k | RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(i), |
205 | 13.9k | meta.children_columns(i).num_rows(), file_reader, |
206 | 13.9k | &sub_reader)); |
207 | 13.9k | struct_reader->_sub_readers.push_back(std::move(sub_reader)); |
208 | 13.9k | } |
209 | 2.87k | struct_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_STRUCT; |
210 | 2.87k | *reader = std::move(struct_reader); |
211 | 2.87k | return Status::OK(); |
212 | 2.87k | } |
213 | | |
214 | | Status ColumnReader::create_agg_state(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
215 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
216 | 645 | std::shared_ptr<ColumnReader>* reader) { |
217 | 645 | if (!meta.has_function_name()) { // meet old version ColumnMetaPB |
218 | 0 | std::shared_ptr<ColumnReader> reader_local( |
219 | 0 | new ColumnReader(opts, meta, num_rows, file_reader)); |
220 | 0 | RETURN_IF_ERROR(reader_local->init(&meta)); |
221 | 0 | *reader = std::move(reader_local); |
222 | 0 | return Status::OK(); |
223 | 0 | } |
224 | | |
225 | 645 | auto data_type = DataTypeFactory::instance().create_data_type(meta); |
226 | 645 | const auto* agg_state_type = assert_cast<const DataTypeAggState*>(data_type.get()); |
227 | 645 | agg_state_type->check_function_compatibility(opts.be_exec_version); |
228 | 645 | auto type = agg_state_type->get_serialized_type()->get_primitive_type(); |
229 | | |
230 | 645 | if (read_as_string(type)) { |
231 | 599 | std::shared_ptr<ColumnReader> reader_local( |
232 | 599 | new ColumnReader(opts, meta, num_rows, file_reader)); |
233 | 599 | RETURN_IF_ERROR(reader_local->init(&meta)); |
234 | 599 | *reader = std::move(reader_local); |
235 | 599 | return Status::OK(); |
236 | 599 | } else if (type == PrimitiveType::TYPE_MAP) { |
237 | 34 | return create_map(opts, meta, file_reader, reader); |
238 | 34 | } else if (type == PrimitiveType::TYPE_ARRAY) { |
239 | 10 | return create_array(opts, meta, file_reader, reader); |
240 | 10 | } else if (type == PrimitiveType::TYPE_STRUCT) { |
241 | 0 | return create_struct(opts, meta, num_rows, file_reader, reader); |
242 | 0 | } |
243 | | |
244 | 2 | return Status::InternalError("Not supported type: {}, serialized type: {}", |
245 | 2 | agg_state_type->get_name(), int(type)); |
246 | 645 | } |
247 | | |
248 | 119k | bool ColumnReader::is_compaction_reader_type(ReaderType type) { |
249 | 119k | return type == ReaderType::READER_BASE_COMPACTION || |
250 | 119k | type == ReaderType::READER_CUMULATIVE_COMPACTION || |
251 | 119k | type == ReaderType::READER_COLD_DATA_COMPACTION || |
252 | 119k | type == ReaderType::READER_SEGMENT_COMPACTION || |
253 | 119k | type == ReaderType::READER_FULL_COMPACTION; |
254 | 119k | } |
255 | | |
256 | | Status ColumnReader::create(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
257 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
258 | 3.79M | std::shared_ptr<ColumnReader>* reader) { |
259 | 3.79M | if (is_scalar_type((FieldType)meta.type())) { |
260 | 3.70M | std::shared_ptr<ColumnReader> reader_local( |
261 | 3.70M | new ColumnReader(opts, meta, num_rows, file_reader)); |
262 | 3.70M | RETURN_IF_ERROR(reader_local->init(&meta)); |
263 | 3.70M | *reader = std::move(reader_local); |
264 | 3.70M | return Status::OK(); |
265 | 3.70M | } else { |
266 | 96.8k | auto type = (FieldType)meta.type(); |
267 | 96.8k | switch (type) { |
268 | 646 | case FieldType::OLAP_FIELD_TYPE_AGG_STATE: { |
269 | 646 | return create_agg_state(opts, meta, num_rows, file_reader, reader); |
270 | 0 | } |
271 | 2.87k | case FieldType::OLAP_FIELD_TYPE_STRUCT: { |
272 | 2.87k | return create_struct(opts, meta, num_rows, file_reader, reader); |
273 | 0 | } |
274 | 51.8k | case FieldType::OLAP_FIELD_TYPE_ARRAY: { |
275 | 51.8k | return create_array(opts, meta, file_reader, reader); |
276 | 0 | } |
277 | 33.8k | case FieldType::OLAP_FIELD_TYPE_MAP: { |
278 | 33.8k | return create_map(opts, meta, file_reader, reader); |
279 | 0 | } |
280 | 7.58k | case FieldType::OLAP_FIELD_TYPE_VARIANT: { |
281 | | // Read variant only root data using a single ColumnReader |
282 | 7.58k | std::shared_ptr<ColumnReader> reader_local( |
283 | 7.58k | new ColumnReader(opts, meta, num_rows, file_reader)); |
284 | 7.58k | RETURN_IF_ERROR(reader_local->init(&meta)); |
285 | 7.58k | *reader = std::move(reader_local); |
286 | 7.58k | return Status::OK(); |
287 | 7.58k | } |
288 | 0 | default: |
289 | 0 | return Status::NotSupported("unsupported type for ColumnReader: {}", |
290 | 0 | std::to_string(int(type))); |
291 | 96.8k | } |
292 | 96.8k | } |
293 | 3.79M | } |
294 | | |
295 | 8.02k | ColumnReader::ColumnReader() = default; |
296 | | |
297 | | ColumnReader::ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
298 | | uint64_t num_rows, io::FileReaderSPtr file_reader) |
299 | 3.80M | : _use_index_page_cache(!config::disable_storage_page_cache), |
300 | 3.80M | _opts(opts), |
301 | 3.80M | _num_rows(num_rows), |
302 | 3.80M | _file_reader(std::move(file_reader)), |
303 | 3.80M | _dict_encoding_type(UNKNOWN_DICT_ENCODING) { |
304 | 3.80M | _meta_length = meta.length(); |
305 | 3.80M | _meta_type = (FieldType)meta.type(); |
306 | 3.80M | if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) { |
307 | 51.8k | _meta_children_column_type = (FieldType)meta.children_columns(0).type(); |
308 | 51.8k | } |
309 | 3.80M | _data_type = DataTypeFactory::instance().create_data_type(meta); |
310 | 3.80M | _meta_is_nullable = meta.is_nullable(); |
311 | 3.80M | _meta_dict_page = meta.dict_page(); |
312 | 3.80M | _meta_compression = meta.compression(); |
313 | 3.80M | } |
314 | | |
315 | 3.60M | ColumnReader::~ColumnReader() = default; |
316 | | |
317 | 3.71M | int64_t ColumnReader::get_metadata_size() const { |
318 | 3.71M | return sizeof(ColumnReader) + (_segment_zone_map ? _segment_zone_map->ByteSizeLong() : 0); |
319 | 3.71M | } |
320 | | |
321 | | #ifdef BE_TEST |
322 | | /// This function is only used in UT to verify the correctness of data read from zone map |
323 | | /// See UT case 'SegCompactionMoWTest.SegCompactionInterleaveWithBig_ooooOOoOooooooooO' |
324 | | /// be/test/olap/segcompaction_mow_test.cpp |
325 | | void ColumnReader::check_data_by_zone_map_for_test(const MutableColumnPtr& dst) const { |
326 | | if (!_segment_zone_map) { |
327 | | return; |
328 | | } |
329 | | |
330 | | const auto rows = dst->size(); |
331 | | if (rows == 0) { |
332 | | return; |
333 | | } |
334 | | |
335 | | FieldType type = _type; |
336 | | |
337 | | if (type != FieldType::OLAP_FIELD_TYPE_INT) { |
338 | | return; |
339 | | } |
340 | | |
341 | | auto* non_nullable_column = |
342 | | is_column_nullable(*dst) |
343 | | ? assert_cast<ColumnNullable*>(dst.get())->get_nested_column_ptr().get() |
344 | | : dst.get(); |
345 | | |
346 | | /// Only verify when the destination column carries Field-accessible TYPE_INT data. |
347 | | if (check_and_get_column<ColumnVector<TYPE_INT>>(non_nullable_column) == nullptr) { |
348 | | return; |
349 | | } |
350 | | |
351 | | ZoneMap zone_map; |
352 | | THROW_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
353 | | |
354 | | if (zone_map.has_null) { |
355 | | return; |
356 | | } |
357 | | |
358 | | for (size_t i = 0; i != rows; ++i) { |
359 | | Field field; |
360 | | dst->get(i, field); |
361 | | DCHECK(!field.is_null()); |
362 | | const auto v = field.get<TYPE_INT>(); |
363 | | DCHECK_GE(v, zone_map.min_value.get<TYPE_INT>()); |
364 | | DCHECK_LE(v, zone_map.max_value.get<TYPE_INT>()); |
365 | | } |
366 | | } |
367 | | #endif |
368 | | |
369 | 3.70M | Status ColumnReader::init(const ColumnMetaPB* meta) { |
370 | 3.70M | _type = (FieldType)meta->type(); |
371 | | |
372 | 3.70M | if (meta->has_be_exec_version()) { |
373 | 3.46M | _be_exec_version = meta->be_exec_version(); |
374 | 3.46M | } |
375 | | |
376 | 3.71M | if (_type == FieldType::OLAP_FIELD_TYPE_NONE || _type == FieldType::OLAP_FIELD_TYPE_UNKNOWN) { |
377 | 0 | return Status::NotSupported("unsupported typeinfo, type={}", meta->type()); |
378 | 0 | } |
379 | 3.70M | RETURN_IF_ERROR(EncodingInfo::get(_type, meta->encoding(), &_encoding_info)); |
380 | | |
381 | 10.7M | for (int i = 0; i < meta->indexes_size(); i++) { |
382 | 7.05M | const auto& index_meta = meta->indexes(i); |
383 | 7.05M | switch (index_meta.type()) { |
384 | 0 | case BITMAP_INDEX: |
385 | 0 | break; |
386 | 3.66M | case ORDINAL_INDEX: |
387 | 3.66M | _ordinal_index.reset( |
388 | 3.66M | new OrdinalIndexReader(_file_reader, _num_rows, index_meta.ordinal_index())); |
389 | 3.66M | break; |
390 | 3.38M | case ZONE_MAP_INDEX: |
391 | 3.38M | _segment_zone_map = |
392 | 3.38M | std::make_unique<ZoneMapPB>(index_meta.zone_map_index().segment_zone_map()); |
393 | 3.38M | _zone_map_index.reset(new ZoneMapIndexReader( |
394 | 3.38M | _file_reader, index_meta.zone_map_index().page_zone_maps())); |
395 | 3.38M | break; |
396 | 4.77k | case BLOOM_FILTER_INDEX: |
397 | 4.77k | _bloom_filter_index.reset( |
398 | 4.77k | new BloomFilterIndexReader(_file_reader, index_meta.bloom_filter_index())); |
399 | 4.77k | break; |
400 | 0 | case NESTED_OFFSETS_INDEX: |
401 | 0 | break; |
402 | 0 | default: |
403 | 0 | return Status::Corruption("Bad file {}: invalid column index type {}", |
404 | 0 | _file_reader->path().native(), index_meta.type()); |
405 | 7.05M | } |
406 | 7.05M | } |
407 | 3.70M | update_metadata_size(); |
408 | | |
409 | | // ArrayColumnWriter writes a single empty array and flushes. In this scenario, |
410 | | // the item writer doesn't write any data and the corresponding ordinal index is empty. |
411 | 3.70M | if (_ordinal_index == nullptr && !is_empty()) { |
412 | 0 | return Status::Corruption("Bad file {}: missing ordinal index for column {}", |
413 | 0 | _file_reader->path().native(), meta->column_id()); |
414 | 0 | } |
415 | | |
416 | 3.70M | return Status::OK(); |
417 | 3.70M | } |
418 | | |
419 | | Status ColumnReader::new_index_iterator(const std::shared_ptr<IndexFileReader>& index_file_reader, |
420 | | const TabletIndex* index_meta, const std::string& rowset_id, |
421 | | uint32_t segment_id, size_t rows_of_segment, |
422 | 77.8k | std::unique_ptr<IndexIterator>* iterator) { |
423 | 77.8k | RETURN_IF_ERROR( |
424 | 77.8k | _load_index(index_file_reader, index_meta, rowset_id, segment_id, rows_of_segment)); |
425 | 77.8k | { |
426 | 77.8k | std::shared_lock<std::shared_mutex> rlock(_load_index_lock); |
427 | 77.8k | auto iter = _index_readers.find(index_meta->index_id()); |
428 | 78.0k | if (iter != _index_readers.end()) { |
429 | 78.0k | if (iter->second != nullptr) { |
430 | 78.0k | RETURN_IF_ERROR(iter->second->new_iterator(iterator)); |
431 | 78.0k | } |
432 | 78.0k | } |
433 | 77.8k | } |
434 | 77.8k | return Status::OK(); |
435 | 77.8k | } |
436 | | |
437 | | Status ColumnReader::read_page(const ColumnIteratorOptions& iter_opts, const PagePointer& pp, |
438 | | PageHandle* handle, Slice* page_body, PageFooterPB* footer, |
439 | 2.16M | BlockCompressionCodec* codec) const { |
440 | 2.16M | SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().column_reader_read_page); |
441 | 2.16M | iter_opts.sanity_check(); |
442 | 2.16M | PageReadOptions opts(iter_opts.io_ctx); |
443 | 2.16M | opts.verify_checksum = _opts.verify_checksum; |
444 | 2.16M | opts.use_page_cache = iter_opts.use_page_cache; |
445 | 2.16M | opts.kept_in_memory = _opts.kept_in_memory; |
446 | 2.16M | opts.type = iter_opts.type; |
447 | 2.16M | opts.file_reader = iter_opts.file_reader; |
448 | 2.16M | opts.page_pointer = pp; |
449 | 2.16M | opts.codec = codec; |
450 | 2.16M | opts.stats = iter_opts.stats; |
451 | 2.16M | opts.encoding_info = _encoding_info; |
452 | | |
453 | 2.16M | return PageIO::read_and_decompress_page(opts, handle, page_body, footer); |
454 | 2.16M | } |
455 | | |
456 | | Status ColumnReader::get_row_ranges_by_zone_map( |
457 | | const AndBlockColumnPredicate* col_predicates, |
458 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
459 | 129k | RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts) { |
460 | 129k | std::vector<uint32_t> page_indexes; |
461 | 129k | RETURN_IF_ERROR( |
462 | 129k | _get_filtered_pages(col_predicates, delete_predicates, &page_indexes, iter_opts)); |
463 | 129k | RETURN_IF_ERROR(_calculate_row_ranges(page_indexes, row_ranges, iter_opts)); |
464 | 129k | return Status::OK(); |
465 | 129k | } |
466 | | |
467 | 22.1k | Status ColumnReader::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const { |
468 | 22.1k | if (_segment_zone_map == nullptr) { |
469 | 0 | return Status::InternalError("segment zonemap not exist"); |
470 | 0 | } |
471 | | // TODO: this work to get min/max value seems should only do once |
472 | 22.1k | ZoneMap zone_map; |
473 | 22.1k | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
474 | | |
475 | 22.1k | dst->reserve(*n); |
476 | 22.1k | if (!zone_map.has_not_null) { |
477 | 1.19k | assert_cast<ColumnNullable&>(*dst).insert_many_defaults(*n); |
478 | 1.19k | return Status::OK(); |
479 | 1.19k | } |
480 | 20.9k | dst->insert(zone_map.max_value); |
481 | 41.9k | for (int i = 1; i < *n; ++i) { |
482 | 20.9k | dst->insert(zone_map.min_value); |
483 | 20.9k | } |
484 | 20.9k | return Status::OK(); |
485 | 22.1k | } |
486 | | |
487 | | Status ColumnReader::match_condition(const AndBlockColumnPredicate* col_predicates, |
488 | 1.97M | bool* matched) const { |
489 | 1.97M | *matched = true; |
490 | 1.97M | if (_zone_map_index == nullptr) { |
491 | 0 | return Status::OK(); |
492 | 0 | } |
493 | 1.97M | ZoneMap zone_map; |
494 | 1.97M | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
495 | | |
496 | 1.97M | *matched = _zone_map_match_condition(zone_map, col_predicates); |
497 | 1.97M | return Status::OK(); |
498 | 1.97M | } |
499 | | |
500 | | Status ColumnReader::prune_predicates_by_zone_map( |
501 | | std::vector<std::shared_ptr<ColumnPredicate>>& predicates, const int column_id, |
502 | 26.4M | bool* pruned) const { |
503 | 26.4M | *pruned = false; |
504 | 26.4M | if (_zone_map_index == nullptr) { |
505 | 28.1k | return Status::OK(); |
506 | 28.1k | } |
507 | | |
508 | 26.4M | ZoneMap zone_map; |
509 | 26.4M | RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map)); |
510 | 26.4M | if (zone_map.pass_all) { |
511 | 0 | return Status::OK(); |
512 | 0 | } |
513 | | |
514 | 53.2M | for (auto it = predicates.begin(); it != predicates.end();) { |
515 | 26.8M | auto predicate = *it; |
516 | 26.8M | if (predicate->column_id() == column_id && predicate->is_always_true(zone_map)) { |
517 | 14.1k | *pruned = true; |
518 | 14.1k | it = predicates.erase(it); |
519 | 26.8M | } else { |
520 | 26.8M | ++it; |
521 | 26.8M | } |
522 | 26.8M | } |
523 | 26.4M | return Status::OK(); |
524 | 26.4M | } |
525 | | |
526 | | bool ColumnReader::_zone_map_match_condition(const ZoneMap& zone_map, |
527 | 2.06M | const AndBlockColumnPredicate* col_predicates) const { |
528 | 2.06M | if (zone_map.pass_all) { |
529 | 0 | return true; |
530 | 0 | } |
531 | | |
532 | 2.06M | return col_predicates->evaluate_and(zone_map); |
533 | 2.06M | } |
534 | | |
535 | | Status ColumnReader::_get_filtered_pages( |
536 | | const AndBlockColumnPredicate* col_predicates, |
537 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
538 | 129k | std::vector<uint32_t>* page_indexes, const ColumnIteratorOptions& iter_opts) { |
539 | 129k | RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
540 | | |
541 | 129k | const std::vector<ZoneMapPB>& zone_maps = _zone_map_index->page_zone_maps(); |
542 | 129k | size_t page_size = _zone_map_index->num_pages(); |
543 | 303k | for (size_t i = 0; i < page_size; ++i) { |
544 | 174k | if (zone_maps[i].pass_all()) { |
545 | 85.0k | page_indexes->push_back(cast_set<uint32_t>(i)); |
546 | 89.5k | } else { |
547 | 89.5k | segment_v2::ZoneMap zone_map; |
548 | 89.5k | RETURN_IF_ERROR(ZoneMap::from_proto(zone_maps[i], _data_type, zone_map)); |
549 | 89.5k | if (_zone_map_match_condition(zone_map, col_predicates)) { |
550 | 89.1k | bool should_read = true; |
551 | 89.1k | if (delete_predicates != nullptr) { |
552 | 4 | for (auto del_pred : *delete_predicates) { |
553 | | // TODO: Both `min_value` and `max_value` should be 0 or neither should be 0. |
554 | | // So nullable only need to judge once. |
555 | 4 | if (del_pred->evaluate_del(zone_map)) { |
556 | 1 | should_read = false; |
557 | 1 | break; |
558 | 1 | } |
559 | 4 | } |
560 | 3 | } |
561 | 89.1k | if (should_read) { |
562 | 89.1k | page_indexes->push_back(cast_set<uint32_t>(i)); |
563 | 89.1k | } |
564 | 89.1k | } |
565 | 89.5k | } |
566 | 174k | } |
567 | 18.4E | VLOG(1) << "total-pages: " << page_size << " not-filtered-pages: " << page_indexes->size() |
568 | 18.4E | << " filtered-percent:" |
569 | 18.4E | << 1.0 - (static_cast<double>(page_indexes->size()) / |
570 | 18.4E | (static_cast<double>(page_size) * 1.0)); |
571 | 129k | return Status::OK(); |
572 | 129k | } |
573 | | |
574 | | Status ColumnReader::_calculate_row_ranges(const std::vector<uint32_t>& page_indexes, |
575 | | RowRanges* row_ranges, |
576 | 129k | const ColumnIteratorOptions& iter_opts) { |
577 | 129k | row_ranges->clear(); |
578 | 129k | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
579 | 174k | for (auto i : page_indexes) { |
580 | 174k | ordinal_t page_first_id = _ordinal_index->get_first_ordinal(i); |
581 | 174k | ordinal_t page_last_id = _ordinal_index->get_last_ordinal(i); |
582 | 174k | RowRanges page_row_ranges(RowRanges::create_single(page_first_id, page_last_id + 1)); |
583 | 174k | RowRanges::ranges_union(*row_ranges, page_row_ranges, row_ranges); |
584 | 174k | } |
585 | 129k | return Status::OK(); |
586 | 129k | } |
587 | | |
588 | | Status ColumnReader::get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
589 | | RowRanges* row_ranges, |
590 | 115 | const ColumnIteratorOptions& iter_opts) { |
591 | 115 | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
592 | 115 | RETURN_IF_ERROR( |
593 | 115 | _load_bloom_filter_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
594 | 115 | RowRanges bf_row_ranges; |
595 | 115 | std::unique_ptr<BloomFilterIndexIterator> bf_iter; |
596 | 115 | RETURN_IF_ERROR(_bloom_filter_index->new_iterator(&bf_iter, iter_opts.stats)); |
597 | 115 | size_t range_size = row_ranges->range_size(); |
598 | | // get covered page ids |
599 | 115 | std::set<uint32_t> page_ids; |
600 | 230 | for (int i = 0; i < range_size; ++i) { |
601 | 115 | int64_t from = row_ranges->get_range_from(i); |
602 | 115 | int64_t idx = from; |
603 | 115 | int64_t to = row_ranges->get_range_to(i); |
604 | 115 | auto iter = _ordinal_index->seek_at_or_before(from); |
605 | 427 | while (idx < to && iter.valid()) { |
606 | 312 | page_ids.insert(iter.page_index()); |
607 | 312 | idx = iter.last_ordinal() + 1; |
608 | 312 | iter.next(); |
609 | 312 | } |
610 | 115 | } |
611 | 325 | for (auto& pid : page_ids) { |
612 | 325 | std::unique_ptr<BloomFilter> bf; |
613 | 325 | RETURN_IF_ERROR(bf_iter->read_bloom_filter(pid, &bf)); |
614 | 325 | if (col_predicates->evaluate_and(bf.get())) { |
615 | 36 | bf_row_ranges.add(RowRange(_ordinal_index->get_first_ordinal(pid), |
616 | 36 | _ordinal_index->get_last_ordinal(pid) + 1)); |
617 | 36 | } |
618 | 325 | } |
619 | 115 | RowRanges::ranges_intersection(*row_ranges, bf_row_ranges, row_ranges); |
620 | 115 | return Status::OK(); |
621 | 115 | } |
622 | | |
623 | | Status ColumnReader::_load_ordinal_index(bool use_page_cache, bool kept_in_memory, |
624 | 1.81M | const ColumnIteratorOptions& iter_opts) { |
625 | 1.81M | if (!_ordinal_index) { |
626 | 0 | return Status::InternalError("ordinal_index not inited"); |
627 | 0 | } |
628 | 1.81M | return _ordinal_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
629 | 1.81M | } |
630 | | |
631 | | Status ColumnReader::_load_zone_map_index(bool use_page_cache, bool kept_in_memory, |
632 | 130k | const ColumnIteratorOptions& iter_opts) { |
633 | 130k | if (_zone_map_index != nullptr) { |
634 | 130k | return _zone_map_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
635 | 130k | } |
636 | 18.4E | return Status::OK(); |
637 | 130k | } |
638 | | |
639 | 2.85k | Status ColumnReader::get_segment_zone_map(segment_v2::ZoneMap* zone_map) const { |
640 | 2.85k | DORIS_CHECK(zone_map != nullptr); |
641 | 2.85k | DORIS_CHECK(_segment_zone_map != nullptr); |
642 | 2.85k | return ZoneMap::from_proto(*_segment_zone_map, _data_type, *zone_map); |
643 | 2.85k | } |
644 | | |
645 | | Status ColumnReader::get_page_zone_maps(const ColumnIteratorOptions& iter_opts, |
646 | 633 | const std::vector<ZoneMapPB>** zone_maps) { |
647 | 633 | DORIS_CHECK(zone_maps != nullptr); |
648 | 633 | if (_zone_map_index == nullptr) { |
649 | 0 | *zone_maps = nullptr; |
650 | 0 | return Status::OK(); |
651 | 0 | } |
652 | 633 | RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
653 | 633 | *zone_maps = &_zone_map_index->page_zone_maps(); |
654 | 633 | return Status::OK(); |
655 | 633 | } |
656 | | |
657 | | Status ColumnReader::get_row_range_for_page(uint32_t page_index, |
658 | | const ColumnIteratorOptions& iter_opts, |
659 | 4.19k | RowRange* row_range) { |
660 | 4.19k | DORIS_CHECK(row_range != nullptr); |
661 | 4.19k | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
662 | 4.19k | DORIS_CHECK(page_index < _ordinal_index->num_data_pages()); |
663 | 4.19k | *row_range = RowRange(_ordinal_index->get_first_ordinal(page_index), |
664 | 4.19k | _ordinal_index->get_last_ordinal(page_index) + 1); |
665 | 4.19k | return Status::OK(); |
666 | 4.19k | } |
667 | | |
668 | | Status ColumnReader::_load_index(const std::shared_ptr<IndexFileReader>& index_file_reader, |
669 | | const TabletIndex* index_meta, const std::string& rowset_id, |
670 | 77.8k | uint32_t segment_id, size_t rows_of_segment) { |
671 | 77.8k | std::unique_lock<std::shared_mutex> wlock(_load_index_lock); |
672 | | |
673 | 77.8k | if (index_meta == nullptr) { |
674 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
675 | 0 | "Failed to load inverted index: index metadata is null"); |
676 | 0 | } |
677 | | |
678 | 77.8k | auto it = _index_readers.find(index_meta->index_id()); |
679 | 77.8k | if (it != _index_readers.end()) { |
680 | 38.1k | return Status::OK(); |
681 | 38.1k | } |
682 | | |
683 | 39.7k | bool should_analyzer = |
684 | 39.7k | inverted_index::InvertedIndexAnalyzer::should_analyzer(index_meta->properties()); |
685 | | |
686 | 39.7k | FieldType type; |
687 | 39.7k | if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) { |
688 | 2.16k | type = _meta_children_column_type; |
689 | 37.5k | } else { |
690 | 37.5k | type = _type; |
691 | 37.5k | } |
692 | | |
693 | 39.7k | if (index_meta->index_type() == IndexType::ANN) { |
694 | 64 | _index_readers[index_meta->index_id()] = std::make_shared<AnnIndexReader>( |
695 | 64 | index_meta, index_file_reader, rowset_id, segment_id, rows_of_segment); |
696 | 64 | return Status::OK(); |
697 | 64 | } |
698 | | |
699 | 39.6k | IndexReaderPtr index_reader; |
700 | | |
701 | 39.6k | if (is_string_type(type)) { |
702 | 13.1k | if (should_analyzer) { |
703 | 6.15k | try { |
704 | 6.15k | index_reader = FullTextIndexReader::create_shared(index_meta, index_file_reader); |
705 | 6.15k | } catch (const CLuceneError& e) { |
706 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
707 | 0 | "create FullTextIndexReader error: {}", e.what()); |
708 | 0 | } |
709 | 7.01k | } else { |
710 | 7.01k | try { |
711 | 7.01k | index_reader = |
712 | 7.01k | StringTypeInvertedIndexReader::create_shared(index_meta, index_file_reader); |
713 | 7.01k | } catch (const CLuceneError& e) { |
714 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
715 | 0 | "create StringTypeInvertedIndexReader error: {}", e.what()); |
716 | 0 | } |
717 | 7.01k | } |
718 | 26.6k | } else if (is_numeric_type(type)) { |
719 | 26.6k | try { |
720 | 26.6k | index_reader = BkdIndexReader::create_shared(index_meta, index_file_reader); |
721 | 26.6k | } catch (const CLuceneError& e) { |
722 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
723 | 0 | "create BkdIndexReader error: {}", e.what()); |
724 | 0 | } |
725 | 18.4E | } else { |
726 | 18.4E | return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>( |
727 | 18.4E | "Field type {} is not supported for inverted index", type); |
728 | 18.4E | } |
729 | 39.8k | _index_readers[index_meta->index_id()] = index_reader; |
730 | 39.8k | return Status::OK(); |
731 | 39.6k | } |
732 | | |
733 | 106k | bool ColumnReader::has_bloom_filter_index(bool ngram) const { |
734 | 106k | if (_bloom_filter_index == nullptr) return false; |
735 | | |
736 | 104 | if (ngram) { |
737 | 77 | return _bloom_filter_index->algorithm() == BloomFilterAlgorithmPB::NGRAM_BLOOM_FILTER; |
738 | 77 | } else { |
739 | 27 | return _bloom_filter_index->algorithm() != BloomFilterAlgorithmPB::NGRAM_BLOOM_FILTER; |
740 | 27 | } |
741 | 104 | } |
742 | | |
743 | | Status ColumnReader::_load_bloom_filter_index(bool use_page_cache, bool kept_in_memory, |
744 | 116 | const ColumnIteratorOptions& iter_opts) { |
745 | 116 | if (_bloom_filter_index != nullptr) { |
746 | 115 | return _bloom_filter_index->load(use_page_cache, kept_in_memory, iter_opts.stats); |
747 | 115 | } |
748 | 1 | return Status::OK(); |
749 | 116 | } |
750 | | |
751 | | Status ColumnReader::seek_at_or_before(ordinal_t ordinal, OrdinalPageIndexIterator* iter, |
752 | 1.68M | const ColumnIteratorOptions& iter_opts) { |
753 | 1.68M | RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts)); |
754 | 1.68M | *iter = _ordinal_index->seek_at_or_before(ordinal); |
755 | 1.68M | if (!iter->valid()) { |
756 | 0 | return Status::NotFound("Failed to seek to ordinal {}, ", ordinal); |
757 | 0 | } |
758 | 1.68M | return Status::OK(); |
759 | 1.68M | } |
760 | | |
761 | | Status ColumnReader::get_ordinal_index_reader(OrdinalIndexReader*& reader, |
762 | 1.01M | OlapReaderStatistics* index_load_stats) { |
763 | 18.4E | CHECK(_ordinal_index) << fmt::format("ordinal index is null for column reader of type {}", |
764 | 18.4E | std::to_string(int(_meta_type))); |
765 | 1.01M | RETURN_IF_ERROR( |
766 | 1.01M | _ordinal_index->load(_use_index_page_cache, _opts.kept_in_memory, index_load_stats)); |
767 | 1.01M | reader = _ordinal_index.get(); |
768 | 1.01M | return Status::OK(); |
769 | 1.01M | } |
770 | | |
771 | 457k | Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column) { |
772 | 457k | return new_iterator(iterator, tablet_column, nullptr); |
773 | 457k | } |
774 | | |
775 | | Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column, |
776 | 29.0M | const StorageReadOptions* opt) { |
777 | 29.0M | if (is_empty()) { |
778 | 50.3k | *iterator = std::make_unique<EmptyFileColumnIterator>(); |
779 | 50.3k | return Status::OK(); |
780 | 50.3k | } |
781 | 28.9M | if (is_scalar_type(_meta_type)) { |
782 | 28.8M | if (is_string_type(_meta_type)) { |
783 | 17.3M | *iterator = std::make_unique<StringFileColumnIterator>(shared_from_this()); |
784 | 17.3M | } else { |
785 | 11.5M | *iterator = std::make_unique<FileColumnIterator>(shared_from_this()); |
786 | 11.5M | } |
787 | 28.8M | (*iterator)->set_column_name(tablet_column ? tablet_column->name() : ""); |
788 | 28.8M | return Status::OK(); |
789 | 28.8M | } else { |
790 | 104k | auto type = _meta_type; |
791 | 104k | switch (type) { |
792 | 853 | case FieldType::OLAP_FIELD_TYPE_AGG_STATE: { |
793 | 853 | return new_agg_state_iterator(iterator); |
794 | 0 | } |
795 | 8.24k | case FieldType::OLAP_FIELD_TYPE_STRUCT: { |
796 | 8.24k | return new_struct_iterator(iterator, tablet_column); |
797 | 0 | } |
798 | 66.7k | case FieldType::OLAP_FIELD_TYPE_ARRAY: { |
799 | 66.7k | return new_array_iterator(iterator, tablet_column); |
800 | 0 | } |
801 | 37.6k | case FieldType::OLAP_FIELD_TYPE_MAP: { |
802 | 37.6k | return new_map_iterator(iterator, tablet_column); |
803 | 0 | } |
804 | 0 | default: |
805 | 0 | return Status::NotSupported("unsupported type to create iterator: {}", |
806 | 0 | std::to_string(int(type))); |
807 | 104k | } |
808 | 104k | } |
809 | 28.9M | } |
810 | | |
811 | 849 | Status ColumnReader::new_agg_state_iterator(ColumnIteratorUPtr* iterator) { |
812 | 849 | *iterator = std::make_unique<FileColumnIterator>(shared_from_this()); |
813 | 849 | return Status::OK(); |
814 | 849 | } |
815 | | |
816 | | Status ColumnReader::new_array_iterator(ColumnIteratorUPtr* iterator, |
817 | 66.7k | const TabletColumn* tablet_column) { |
818 | 66.7k | ColumnIteratorUPtr item_iterator; |
819 | 66.7k | RETURN_IF_ERROR(_sub_readers[0]->new_iterator( |
820 | 66.7k | &item_iterator, tablet_column && tablet_column->get_subtype_count() > 0 |
821 | 66.7k | ? &tablet_column->get_sub_column(0) |
822 | 66.7k | : nullptr)); |
823 | | |
824 | 66.7k | item_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : ""); |
825 | | |
826 | 66.7k | ColumnIteratorUPtr offset_iterator; |
827 | 66.7k | RETURN_IF_ERROR(_sub_readers[1]->new_iterator(&offset_iterator, nullptr)); |
828 | 66.7k | auto* file_iter = static_cast<FileColumnIterator*>(offset_iterator.release()); |
829 | 66.7k | OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>( |
830 | 66.7k | std::unique_ptr<FileColumnIterator>(file_iter)); |
831 | | |
832 | 66.7k | ColumnIteratorUPtr null_iterator; |
833 | 66.7k | if (is_nullable()) { |
834 | 47.9k | RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&null_iterator, nullptr)); |
835 | 47.9k | } |
836 | 66.7k | *iterator = std::make_unique<ArrayFileColumnIterator>(shared_from_this(), std::move(ofcIter), |
837 | 66.7k | std::move(item_iterator), |
838 | 66.7k | std::move(null_iterator)); |
839 | 66.7k | return Status::OK(); |
840 | 66.7k | } |
841 | | |
842 | | Status ColumnReader::new_map_iterator(ColumnIteratorUPtr* iterator, |
843 | 37.5k | const TabletColumn* tablet_column) { |
844 | 37.5k | ColumnIteratorUPtr key_iterator; |
845 | 37.5k | RETURN_IF_ERROR(_sub_readers[0]->new_iterator( |
846 | 37.5k | &key_iterator, tablet_column && tablet_column->get_subtype_count() > 1 |
847 | 37.5k | ? &tablet_column->get_sub_column(0) |
848 | 37.5k | : nullptr)); |
849 | 37.5k | key_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : ""); |
850 | 37.5k | ColumnIteratorUPtr val_iterator; |
851 | 37.5k | RETURN_IF_ERROR(_sub_readers[1]->new_iterator( |
852 | 37.5k | &val_iterator, tablet_column && tablet_column->get_subtype_count() > 1 |
853 | 37.5k | ? &tablet_column->get_sub_column(1) |
854 | 37.5k | : nullptr)); |
855 | 37.5k | val_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(1).name() : ""); |
856 | 37.5k | ColumnIteratorUPtr offsets_iterator; |
857 | 37.5k | RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&offsets_iterator, nullptr)); |
858 | 37.5k | auto* file_iter = static_cast<FileColumnIterator*>(offsets_iterator.release()); |
859 | 37.5k | OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>( |
860 | 37.5k | std::unique_ptr<FileColumnIterator>(file_iter)); |
861 | | |
862 | 37.5k | ColumnIteratorUPtr null_iterator; |
863 | 37.5k | if (is_nullable()) { |
864 | 17.1k | RETURN_IF_ERROR(_sub_readers[3]->new_iterator(&null_iterator, nullptr)); |
865 | 17.1k | } |
866 | 37.5k | *iterator = std::make_unique<MapFileColumnIterator>( |
867 | 37.5k | shared_from_this(), std::move(null_iterator), std::move(ofcIter), |
868 | 37.5k | std::move(key_iterator), std::move(val_iterator)); |
869 | 37.5k | return Status::OK(); |
870 | 37.5k | } |
871 | | |
872 | | Status ColumnReader::new_struct_iterator(ColumnIteratorUPtr* iterator, |
873 | 8.24k | const TabletColumn* tablet_column) { |
874 | 8.24k | std::vector<ColumnIteratorUPtr> sub_column_iterators; |
875 | 8.24k | size_t child_size = is_nullable() ? _sub_readers.size() - 1 : _sub_readers.size(); |
876 | 8.24k | size_t tablet_column_size = tablet_column ? tablet_column->get_sub_columns().size() : 0; |
877 | 8.24k | sub_column_iterators.reserve(child_size); |
878 | | |
879 | 38.5k | for (uint64_t i = 0; i < child_size; i++) { |
880 | 30.3k | ColumnIteratorUPtr sub_column_iterator; |
881 | 30.3k | RETURN_IF_ERROR(_sub_readers[i]->new_iterator( |
882 | 30.3k | &sub_column_iterator, tablet_column ? &tablet_column->get_sub_column(i) : nullptr)); |
883 | 30.3k | sub_column_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(i).name() |
884 | 30.3k | : ""); |
885 | 30.3k | sub_column_iterators.emplace_back(std::move(sub_column_iterator)); |
886 | 30.3k | } |
887 | | |
888 | | // create default_iterator for schema-change behavior which increase column |
889 | 9.56k | for (size_t i = child_size; i < tablet_column_size; i++) { |
890 | 1.31k | TabletColumn column = tablet_column->get_sub_column(i); |
891 | 1.31k | ColumnIteratorUPtr it; |
892 | 1.31k | RETURN_IF_ERROR(Segment::new_default_iterator(column, &it)); |
893 | 1.31k | it->set_column_name(column.name()); |
894 | 1.31k | sub_column_iterators.emplace_back(std::move(it)); |
895 | 1.31k | } |
896 | | |
897 | 8.24k | ColumnIteratorUPtr null_iterator; |
898 | 8.24k | if (is_nullable()) { |
899 | 7.39k | RETURN_IF_ERROR(_sub_readers[child_size]->new_iterator(&null_iterator, nullptr)); |
900 | 7.39k | } |
901 | 8.24k | *iterator = std::make_unique<StructFileColumnIterator>( |
902 | 8.24k | shared_from_this(), std::move(null_iterator), std::move(sub_column_iterators)); |
903 | 8.24k | return Status::OK(); |
904 | 8.24k | } |
905 | | |
906 | 4.56k | void ColumnIterator::_convert_to_place_holder_column(MutableColumnPtr& dst, size_t count) { |
907 | 4.56k | if (_read_phase == ReadPhase::LAZY) { |
908 | 2.86k | return; |
909 | 2.86k | } else if (_read_requirement == ReadRequirement::LAZY_OUTPUT && |
910 | 1.70k | _read_phase == ReadPhase::PREDICATE) { |
911 | | // This branch is for non-predicate columns that still have to appear in the |
912 | | // predicate-phase block so row filtering can keep all block columns aligned. |
913 | | // Columns already marked PREDICATE are read normally, and SKIP/NORMAL |
914 | | // columns do not participate in lazy materialization. |
915 | 1.22k | _has_place_holder_column = true; |
916 | 1.22k | } |
917 | | |
918 | 1.70k | dst->insert_many_defaults(count); |
919 | 1.70k | } |
920 | | |
921 | 3.51M | void ColumnIterator::_recovery_from_place_holder_column(MutableColumnPtr& dst) { |
922 | 3.51M | if (_read_phase == ReadPhase::LAZY && _has_place_holder_column) { |
923 | 1.22k | dst->clear(); |
924 | 1.22k | _has_place_holder_column = false; |
925 | 1.22k | } |
926 | 3.51M | } |
927 | | |
928 | | Result<TColumnAccessPaths> ColumnIterator::_get_sub_access_paths( |
929 | 126k | TColumnAccessPaths sub_access_paths, bool is_predicate) { |
930 | | // Access paths passed to a complex iterator always start with the current |
931 | | // column name. Strip that component and return the remaining child-relative |
932 | | // paths to the caller. For example, when this iterator is for column `s`, |
933 | | // path `s.a.b` is converted to `a.b` and then dispatched to child `a`. |
934 | | // |
935 | | // If stripping the current column consumes the whole path, the current |
936 | | // iterator itself is requested rather than one of its children. Mark the |
937 | | // current iterator according to the path source: predicate paths must be read |
938 | | // in the predicate phase, while all/output paths become lazy output targets. |
939 | | // Empty or mismatched paths indicate an FE/BE access-path contract violation. |
940 | 196k | for (auto it = sub_access_paths.begin(); it != sub_access_paths.end();) { |
941 | 69.9k | TColumnAccessPath& name_path = *it; |
942 | 69.9k | if (name_path.data_access_path.path.empty()) { |
943 | 2 | return ResultError(Status::InternalError( |
944 | 2 | "Invalid access path for column '{}': path is empty", _column_name)); |
945 | 2 | } |
946 | | |
947 | 69.9k | if (!StringCaseEqual()(name_path.data_access_path.path[0], _column_name)) { |
948 | 3 | return ResultError(Status::InternalError( |
949 | 3 | R"(Invalid access path for column: expected name "{}", got "{}")", _column_name, |
950 | 3 | name_path.data_access_path.path[0])); |
951 | 3 | } |
952 | | |
953 | 69.9k | name_path.data_access_path.path.erase(name_path.data_access_path.path.begin()); |
954 | 69.9k | if (!name_path.data_access_path.path.empty()) { |
955 | 11.3k | ++it; |
956 | 58.6k | } else { |
957 | 58.6k | if (is_predicate) { |
958 | 2.87k | set_read_requirement(ReadRequirement::PREDICATE); |
959 | 55.7k | } else { |
960 | 55.7k | set_lazy_output_requirement(); |
961 | 55.7k | } |
962 | 58.6k | it = sub_access_paths.erase(it); |
963 | 58.6k | } |
964 | 69.9k | } |
965 | 126k | return sub_access_paths; |
966 | 126k | } |
967 | | |
968 | | ///====================== MapFileColumnIterator ============================//// |
969 | | MapFileColumnIterator::MapFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
970 | | ColumnIteratorUPtr null_iterator, |
971 | | OffsetFileColumnIteratorUPtr offsets_iterator, |
972 | | ColumnIteratorUPtr key_iterator, |
973 | | ColumnIteratorUPtr val_iterator) |
974 | 37.6k | : _map_reader(reader), |
975 | 37.6k | _offsets_iterator(std::move(offsets_iterator)), |
976 | 37.6k | _key_iterator(std::move(key_iterator)), |
977 | 37.6k | _val_iterator(std::move(val_iterator)) { |
978 | 37.6k | if (_map_reader->is_nullable()) { |
979 | 17.2k | _null_iterator = std::move(null_iterator); |
980 | 17.2k | } |
981 | 37.6k | } |
982 | | |
983 | 37.5k | Status MapFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
984 | 37.5k | if (_read_requirement == ReadRequirement::SKIP) { |
985 | 32 | DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading."; |
986 | 32 | return Status::OK(); |
987 | 32 | } |
988 | 37.5k | RETURN_IF_ERROR(_key_iterator->init(opts)); |
989 | 37.5k | RETURN_IF_ERROR(_val_iterator->init(opts)); |
990 | 37.5k | RETURN_IF_ERROR(_offsets_iterator->init(opts)); |
991 | 37.5k | if (_map_reader->is_nullable()) { |
992 | 17.1k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
993 | 17.1k | } |
994 | 37.5k | return Status::OK(); |
995 | 37.5k | } |
996 | | |
997 | 21.0k | Status MapFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
998 | 21.0k | if (!need_to_read()) { |
999 | 0 | DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading."; |
1000 | 0 | return Status::OK(); |
1001 | 0 | } |
1002 | | |
1003 | 21.0k | if (read_null_map_only()) { |
1004 | | // In NULL_MAP_ONLY mode, only seek the null iterator; skip offset/key/val iterators |
1005 | 11 | if (_map_reader->is_nullable() && _null_iterator) { |
1006 | 11 | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1007 | 11 | } |
1008 | 11 | return Status::OK(); |
1009 | 11 | } |
1010 | | |
1011 | 21.0k | if (_map_reader->is_nullable()) { |
1012 | 14.2k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1013 | 14.2k | } |
1014 | 21.0k | RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(ord)); |
1015 | 21.0k | if (read_offset_only()) { |
1016 | | // In OFFSET_ONLY mode, key/value iterators are SKIP, no need to seek them |
1017 | 468 | return Status::OK(); |
1018 | 468 | } |
1019 | | // here to use offset info |
1020 | 20.6k | ordinal_t offset = 0; |
1021 | 20.6k | RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&offset)); |
1022 | 20.6k | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(offset)); |
1023 | 20.6k | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(offset)); |
1024 | 20.6k | return Status::OK(); |
1025 | 20.6k | } |
1026 | | |
1027 | 7.27k | Status MapFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1028 | 7.27k | RETURN_IF_ERROR(_offsets_iterator->init_prefetcher(params)); |
1029 | 7.27k | if (_map_reader->is_nullable()) { |
1030 | 5.41k | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
1031 | 5.41k | } |
1032 | 7.27k | RETURN_IF_ERROR(_key_iterator->init_prefetcher(params)); |
1033 | 7.27k | RETURN_IF_ERROR(_val_iterator->init_prefetcher(params)); |
1034 | 7.27k | return Status::OK(); |
1035 | 7.27k | } |
1036 | | |
1037 | | void MapFileColumnIterator::collect_prefetchers( |
1038 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1039 | 7.21k | PrefetcherInitMethod init_method) { |
1040 | 7.21k | if (!need_to_read()) { |
1041 | 0 | return; |
1042 | 0 | } |
1043 | 7.21k | if (!read_null_map_only()) { |
1044 | 7.18k | _offsets_iterator->collect_prefetchers(prefetchers, init_method); |
1045 | 7.18k | } |
1046 | 7.21k | if (_map_reader->is_nullable()) { |
1047 | 5.33k | _null_iterator->collect_prefetchers(prefetchers, init_method); |
1048 | 5.33k | } |
1049 | 7.21k | if (read_offset_only() || read_null_map_only()) { |
1050 | 464 | return; |
1051 | 464 | } |
1052 | | // the actual data pages to read of key/value column depends on the read result of offset column, |
1053 | | // so we can't init prefetch blocks according to rowids, just prefetch all data blocks here. |
1054 | 6.74k | if (_key_iterator->need_to_read()) { |
1055 | 6.45k | _key_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
1056 | 6.45k | } |
1057 | 6.74k | if (_val_iterator->need_to_read()) { |
1058 | 6.46k | _val_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
1059 | 6.46k | } |
1060 | 6.74k | } |
1061 | | |
1062 | 21.0k | Status MapFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1063 | 21.0k | if (!need_to_read()) { |
1064 | 0 | DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading."; |
1065 | 0 | _convert_to_place_holder_column(dst, *n); |
1066 | 0 | return Status::OK(); |
1067 | 0 | } |
1068 | | |
1069 | 21.0k | _recovery_from_place_holder_column(dst); |
1070 | | |
1071 | 21.0k | if (read_null_map_only()) { |
1072 | | // NULL_MAP_ONLY mode: read null map, fill nested ColumnMap with empty defaults |
1073 | 12 | DORIS_CHECK(is_column_nullable(*dst)); |
1074 | 12 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
1075 | 12 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1076 | 12 | size_t num_read = *n; |
1077 | 12 | if (_null_iterator) { |
1078 | 12 | bool null_signs_has_null = false; |
1079 | 12 | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1080 | 12 | RETURN_IF_ERROR( |
1081 | 12 | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1082 | 12 | } else { |
1083 | | // schema-change: column became nullable but old segment has no null data |
1084 | 0 | null_map_ptr->insert_many_vals(0, num_read); |
1085 | 0 | } |
1086 | 12 | DCHECK(num_read == *n); |
1087 | | // fill nested ColumnMap with empty (zero-element) maps |
1088 | 12 | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1089 | 12 | nullable_col.get_nested_column()); |
1090 | 12 | column_map.insert_many_defaults(num_read); |
1091 | 12 | *has_null = true; |
1092 | 12 | return Status::OK(); |
1093 | 12 | } |
1094 | | |
1095 | 21.0k | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1096 | 21.0k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1097 | 21.0k | : *dst); |
1098 | 21.0k | const bool read_meta_columns = need_to_read_meta_columns(); |
1099 | 21.0k | MutableColumnPtr column_offsets_ptr; |
1100 | 21.0k | if (read_meta_columns) { |
1101 | 20.5k | column_offsets_ptr = IColumn::mutate(std::move(column_map.get_offsets_ptr())); |
1102 | 20.5k | } else { |
1103 | | // The parent offsets were already materialized in the predicate phase, so |
1104 | | // they must not be appended to dst again. We still read offsets into a |
1105 | | // temporary column here: this sequential path may be serving a nested |
1106 | | // lazy read after seek_to_ordinal(), and the storage offsets are needed to |
1107 | | // compute how many key/value elements to read from the current source |
1108 | | // ordinal. The existing dst offsets only describe the filtered output |
1109 | | // shape and do not track the current source ordinal consumed by this |
1110 | | // iterator call. |
1111 | 466 | const auto base_offset = |
1112 | 466 | column_map.get_offsets().empty() ? 0 : column_map.get_offsets().back(); |
1113 | 466 | column_offsets_ptr = ColumnMap::COffsets::create(); |
1114 | 466 | assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*column_offsets_ptr) |
1115 | 466 | .insert_value(base_offset); |
1116 | 466 | } |
1117 | 21.0k | Defer defer_offsets {[&] { |
1118 | 21.0k | if (read_meta_columns) { |
1119 | 20.6k | auto typed_column_offsets_ptr = ColumnMap::COffsets::cast_to_column_mutptr( |
1120 | 20.6k | assert_cast<ColumnMap::COffsets*, TypeCheckOnRelease::DISABLE>( |
1121 | 20.6k | column_offsets_ptr.get())); |
1122 | 20.6k | column_offsets_ptr = nullptr; |
1123 | 20.6k | column_map.get_offsets_ptr() = std::move(typed_column_offsets_ptr); |
1124 | 20.6k | } |
1125 | 21.0k | }}; |
1126 | 21.0k | bool offsets_has_null = false; |
1127 | 21.0k | ssize_t start = column_offsets_ptr->size(); |
1128 | 21.0k | RETURN_IF_ERROR(_offsets_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null)); |
1129 | 21.0k | if (*n == 0) { |
1130 | 0 | return Status::OK(); |
1131 | 0 | } |
1132 | 21.0k | auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr); |
1133 | 21.0k | RETURN_IF_ERROR(_offsets_iterator->_calculate_offsets(start, column_offsets)); |
1134 | 21.0k | DCHECK(column_offsets.get_data().back() >= column_offsets.get_data()[start - 1]); |
1135 | 21.0k | size_t num_items = |
1136 | 21.0k | column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid |
1137 | | |
1138 | 21.0k | if (num_items > 0) { |
1139 | 16.4k | auto key_ptr = IColumn::mutate(std::move(column_map.get_keys_ptr())); |
1140 | 16.4k | auto val_ptr = IColumn::mutate(std::move(column_map.get_values_ptr())); |
1141 | 16.4k | Defer defer_keys {[&] { column_map.get_keys_ptr() = std::move(key_ptr); }}; |
1142 | 16.4k | Defer defer_values {[&] { column_map.get_values_ptr() = std::move(val_ptr); }}; |
1143 | 16.4k | if (read_offset_only()) { |
1144 | | // OFFSET_ONLY mode: skip reading actual key/value data, fill with defaults |
1145 | 468 | key_ptr->insert_many_defaults(num_items); |
1146 | 468 | val_ptr->insert_many_defaults(num_items); |
1147 | 15.9k | } else { |
1148 | 15.9k | size_t num_read = num_items; |
1149 | 15.9k | bool key_has_null = false; |
1150 | 15.9k | bool val_has_null = false; |
1151 | 15.9k | RETURN_IF_ERROR(_key_iterator->next_batch(&num_read, key_ptr, &key_has_null)); |
1152 | 15.9k | RETURN_IF_ERROR(_val_iterator->next_batch(&num_read, val_ptr, &val_has_null)); |
1153 | 15.9k | DCHECK(num_read == num_items); |
1154 | 15.9k | } |
1155 | 16.4k | } |
1156 | | |
1157 | 21.0k | if (is_column_nullable(*dst) && read_meta_columns) { |
1158 | 13.7k | size_t num_read = *n; |
1159 | 13.7k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1160 | | // in not-null to null linked-schemachange mode, |
1161 | | // actually we do not change dat data include meta in footer, |
1162 | | // so may dst from changed meta which is nullable but old data is not nullable, |
1163 | | // if so, we should set null_map to all null by default |
1164 | 13.7k | if (_null_iterator) { |
1165 | 13.7k | bool null_signs_has_null = false; |
1166 | 13.7k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1167 | 13.7k | RETURN_IF_ERROR( |
1168 | 13.7k | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1169 | 18.4E | } else { |
1170 | 18.4E | null_map_ptr->insert_many_vals(0, num_read); |
1171 | 18.4E | } |
1172 | 13.7k | DCHECK(num_read == *n); |
1173 | 13.7k | } |
1174 | 21.0k | return Status::OK(); |
1175 | 21.0k | } |
1176 | | |
1177 | | Status MapFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
1178 | 16.1k | MutableColumnPtr& dst) { |
1179 | 16.1k | if (!need_to_read()) { |
1180 | 1 | DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading."; |
1181 | 1 | _convert_to_place_holder_column(dst, count); |
1182 | 1 | return Status::OK(); |
1183 | 1 | } |
1184 | | |
1185 | 16.1k | _recovery_from_place_holder_column(dst); |
1186 | | |
1187 | 16.1k | if (read_null_map_only()) { |
1188 | | // NULL_MAP_ONLY mode: read null map by rowids, fill nested ColumnMap with empty defaults |
1189 | 8 | DORIS_CHECK(is_column_nullable(*dst)); |
1190 | 8 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
1191 | 8 | if (_null_iterator) { |
1192 | 8 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1193 | 8 | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1194 | 8 | RETURN_IF_ERROR(_null_iterator->read_by_rowids(rowids, count, null_map_column)); |
1195 | 8 | } else { |
1196 | | // schema-change: column became nullable but old segment has no null data |
1197 | 0 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1198 | 0 | null_map_ptr->insert_many_vals(0, count); |
1199 | 0 | } |
1200 | | // fill nested ColumnMap with empty (zero-element) maps |
1201 | 8 | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1202 | 8 | nullable_col.get_nested_column()); |
1203 | 8 | column_map.insert_many_defaults(count); |
1204 | 8 | return Status::OK(); |
1205 | 8 | } |
1206 | | |
1207 | 16.1k | if (count == 0) { |
1208 | 0 | return Status::OK(); |
1209 | 0 | } |
1210 | | |
1211 | | // resolve ColumnMap and nullable wrapper |
1212 | 16.1k | auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1213 | 16.1k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1214 | 16.1k | : *dst); |
1215 | 16.1k | const bool read_meta_columns = need_to_read_meta_columns(); |
1216 | 16.1k | MutableColumnPtr offsets_ptr; |
1217 | 16.1k | if (read_meta_columns) { |
1218 | 16.0k | offsets_ptr = IColumn::mutate(std::move(column_map.get_offsets_ptr())); |
1219 | 16.0k | } else { |
1220 | 32 | const auto base_offset = |
1221 | 32 | column_map.get_offsets().empty() ? 0 : column_map.get_offsets().back(); |
1222 | 32 | offsets_ptr = ColumnMap::COffsets::create(); |
1223 | 32 | assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*offsets_ptr) |
1224 | 32 | .insert_value(base_offset); |
1225 | 32 | } |
1226 | 16.1k | Defer defer_offsets {[&] { |
1227 | 16.1k | if (read_meta_columns) { |
1228 | 16.0k | auto typed_offsets_ptr = ColumnMap::COffsets::cast_to_column_mutptr( |
1229 | 16.0k | assert_cast<ColumnMap::COffsets*, TypeCheckOnRelease::DISABLE>( |
1230 | 16.0k | offsets_ptr.get())); |
1231 | 16.0k | offsets_ptr = nullptr; |
1232 | 16.0k | column_map.get_offsets_ptr() = std::move(typed_offsets_ptr); |
1233 | 16.0k | } |
1234 | 16.1k | }}; |
1235 | 16.1k | auto& offsets = static_cast<ColumnArray::ColumnOffsets&>(*offsets_ptr); |
1236 | 16.1k | size_t base = offsets.get_data().empty() ? 0 : offsets.get_data().back(); |
1237 | | |
1238 | | // 1. bulk read null-map if nullable |
1239 | 16.1k | std::vector<uint8_t> null_mask; // 0: not null, 1: null |
1240 | 16.1k | if (read_meta_columns) { |
1241 | 16.0k | if (_map_reader->is_nullable()) { |
1242 | | // For nullable map columns, the destination column must also be nullable. |
1243 | 3.57k | if (UNLIKELY(!is_column_nullable(*dst))) { |
1244 | 0 | return Status::InternalError( |
1245 | 0 | "unexpected non-nullable destination column for nullable map reader"); |
1246 | 0 | } |
1247 | 3.57k | MutableColumnPtr null_map_ptr = |
1248 | 3.57k | static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1249 | 3.57k | size_t null_before = null_map_ptr->size(); |
1250 | 3.57k | RETURN_IF_ERROR(_null_iterator->read_by_rowids(rowids, count, null_map_ptr)); |
1251 | | // extract a light-weight view to decide element reads |
1252 | 3.57k | auto& null_map_col = assert_cast<ColumnUInt8&>(*null_map_ptr); |
1253 | 3.57k | const auto* src = null_map_col.get_data().data() + null_before; |
1254 | 3.57k | null_mask.assign(src, src + count); |
1255 | 12.4k | } else if (is_column_nullable(*dst)) { |
1256 | | // in not-null to null linked-schemachange mode, |
1257 | | // actually we do not change dat data include meta in footer, |
1258 | | // so may dst from changed meta which is nullable but old data is not nullable, |
1259 | | // if so, we should set null_map to all null by default |
1260 | 1 | MutableColumnPtr null_map_ptr = |
1261 | 1 | static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1262 | 1 | auto& null_map = assert_cast<ColumnUInt8&>(*null_map_ptr); |
1263 | 1 | null_map.insert_many_vals(0, count); |
1264 | 1 | } |
1265 | 16.0k | } else if (_map_reader->is_nullable()) { |
1266 | | // In lazy mode the parent null map has already been materialized during |
1267 | | // predicate read and filtered together with the block. Reuse that dst |
1268 | | // null map to avoid re-reading the same meta column from storage. |
1269 | 25 | if (UNLIKELY(!is_column_nullable(*dst))) { |
1270 | 0 | return Status::InternalError( |
1271 | 0 | "unexpected non-nullable destination column for nullable map reader"); |
1272 | 0 | } |
1273 | 25 | const auto& null_map_col = static_cast<const ColumnNullable&>(*dst).get_null_map_column(); |
1274 | 25 | DORIS_CHECK(null_map_col.size() == count); |
1275 | 25 | const auto* src = null_map_col.get_data().data(); |
1276 | 25 | null_mask.assign(src, src + count); |
1277 | 25 | } |
1278 | | |
1279 | | // 2. Bulk read source start ordinals for requested rows. The offsets stored |
1280 | | // in dst already describe the filtered output shape when read_meta_columns is |
1281 | | // false, but they do not contain the source key/value ordinal for each |
1282 | | // selected rowid. We still need the storage offsets here to seek child |
1283 | | // iterators to the correct source element ranges. |
1284 | 16.1k | MutableColumnPtr starts_col = ColumnOffset64::create(); |
1285 | 16.1k | starts_col->reserve(count); |
1286 | 16.1k | RETURN_IF_ERROR(_offsets_iterator->read_by_rowids(rowids, count, starts_col)); |
1287 | | |
1288 | | // 3. bulk read next-start ordinals for rowid+1 (within bounds) |
1289 | 16.1k | std::vector<rowid_t> next_rowids(count); |
1290 | 224k | for (size_t i = 0; i < count; ++i) { |
1291 | 208k | uint64_t nr = rowids[i] + 1; |
1292 | 208k | next_rowids[i] = nr < _map_reader->num_rows() ? static_cast<rowid_t>(nr) |
1293 | 208k | : static_cast<rowid_t>(0); // placeholder |
1294 | 208k | } |
1295 | 16.1k | MutableColumnPtr next_starts_col = ColumnOffset64::create(); |
1296 | 16.1k | next_starts_col->reserve(count); |
1297 | | // read for all; we'll fix out-of-bound cases below |
1298 | 16.1k | RETURN_IF_ERROR(_offsets_iterator->read_by_rowids(next_rowids.data(), count, next_starts_col)); |
1299 | | |
1300 | | // 4. fix next_start for rows whose next_rowid is out-of-bound (rowid == num_rows-1) |
1301 | 224k | for (size_t i = 0; i < count; ++i) { |
1302 | 208k | if (rowids[i] + 1 >= _map_reader->num_rows()) { |
1303 | | // seek to the last row and consume one to move decoder to end-of-page, |
1304 | | // then peek page-tail sentinel next_array_item_ordinal as next_start |
1305 | 14.8k | RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(rowids[i])); |
1306 | 14.8k | size_t one = 1; |
1307 | 14.8k | bool has_null_unused = false; |
1308 | 14.8k | MutableColumnPtr tmp = ColumnOffset64::create(); |
1309 | 14.8k | RETURN_IF_ERROR(_offsets_iterator->next_batch(&one, tmp, &has_null_unused)); |
1310 | 14.8k | ordinal_t ns = 0; |
1311 | 14.8k | RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&ns)); |
1312 | | // overwrite with sentinel |
1313 | 14.8k | assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*next_starts_col) |
1314 | 14.8k | .get_data()[i] = ns; |
1315 | 14.8k | } |
1316 | 208k | } |
1317 | | |
1318 | | // 5. compute sizes and append offsets prefix-sum |
1319 | 16.1k | auto& starts_data = assert_cast<ColumnOffset64&>(*starts_col).get_data(); |
1320 | 16.1k | auto& next_starts_data = assert_cast<ColumnOffset64&>(*next_starts_col).get_data(); |
1321 | 16.1k | std::vector<size_t> sizes(count, 0); |
1322 | 16.1k | size_t acc = base; |
1323 | 16.1k | if (read_meta_columns) { |
1324 | 16.0k | offsets.get_data().reserve(offsets.get_data().size() + count); |
1325 | 16.0k | } |
1326 | 222k | for (size_t i = 0; i < count; ++i) { |
1327 | 206k | auto sz = static_cast<size_t>(next_starts_data[i] - starts_data[i]); |
1328 | 206k | if (_map_reader->is_nullable() && !null_mask.empty() && null_mask[i]) { |
1329 | 738 | sz = 0; // null rows do not consume elements |
1330 | 738 | } |
1331 | 206k | sizes[i] = sz; |
1332 | 206k | acc += sz; |
1333 | 206k | if (read_meta_columns) { |
1334 | 181k | offsets.get_data().push_back(acc); |
1335 | 181k | } |
1336 | 206k | } |
1337 | | |
1338 | | // 6. read key/value elements for non-empty sizes |
1339 | 16.1k | auto keys_ptr = IColumn::mutate(std::move(column_map.get_keys_ptr())); |
1340 | 16.1k | auto vals_ptr = IColumn::mutate(std::move(column_map.get_values_ptr())); |
1341 | 16.1k | Defer defer_keys {[&] { column_map.get_keys_ptr() = std::move(keys_ptr); }}; |
1342 | 16.1k | Defer defer_values {[&] { column_map.get_values_ptr() = std::move(vals_ptr); }}; |
1343 | | |
1344 | 16.1k | size_t this_run = sizes[0]; |
1345 | 16.1k | auto start_idx = starts_data[0]; |
1346 | 16.1k | auto last_idx = starts_data[0] + this_run; |
1347 | 208k | for (size_t i = 1; i < count; ++i) { |
1348 | 192k | size_t sz = sizes[i]; |
1349 | 192k | if (sz == 0) { |
1350 | 29.5k | continue; |
1351 | 29.5k | } |
1352 | 162k | auto start = static_cast<ordinal_t>(starts_data[i]); |
1353 | 162k | if (start != last_idx) { |
1354 | 221 | size_t n = this_run; |
1355 | 221 | bool dummy_has_null = false; |
1356 | | |
1357 | 221 | if (this_run != 0) { |
1358 | 221 | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx)); |
1359 | 221 | RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null)); |
1360 | 221 | DCHECK(n == this_run); |
1361 | | |
1362 | 221 | n = this_run; |
1363 | 221 | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx)); |
1364 | 221 | RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null)); |
1365 | 221 | DCHECK(n == this_run); |
1366 | 221 | } |
1367 | 221 | start_idx = start; |
1368 | 221 | this_run = sz; |
1369 | 221 | last_idx = start + sz; |
1370 | 221 | continue; |
1371 | 221 | } |
1372 | | |
1373 | 162k | this_run += sz; |
1374 | 162k | last_idx += sz; |
1375 | 162k | } |
1376 | | |
1377 | 16.1k | size_t n = this_run; |
1378 | 16.1k | bool dummy_has_null = false; |
1379 | 16.1k | if (this_run != 0) { |
1380 | 5.21k | RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx)); |
1381 | 5.21k | RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null)); |
1382 | 5.21k | DCHECK(n == this_run); |
1383 | | |
1384 | 5.21k | n = this_run; |
1385 | 5.21k | RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx)); |
1386 | 5.21k | RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null)); |
1387 | 5.21k | DCHECK(n == this_run); |
1388 | 5.21k | } |
1389 | 16.1k | return Status::OK(); |
1390 | 16.1k | } |
1391 | | |
1392 | 5.76k | void MapFileColumnIterator::set_lazy_output_requirement() { |
1393 | 5.76k | set_read_requirement_self(ReadRequirement::LAZY_OUTPUT); |
1394 | 5.76k | _key_iterator->set_lazy_output_requirement(); |
1395 | 5.76k | _val_iterator->set_lazy_output_requirement(); |
1396 | 5.76k | } |
1397 | | |
1398 | 7.91k | void MapFileColumnIterator::remove_pruned_sub_iterators() { |
1399 | 7.91k | _key_iterator->remove_pruned_sub_iterators(); |
1400 | 7.91k | _val_iterator->remove_pruned_sub_iterators(); |
1401 | 7.91k | } |
1402 | | |
1403 | | Status MapFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths, |
1404 | 7.70k | const TColumnAccessPaths& predicate_access_paths) { |
1405 | 7.70k | if (all_access_paths.empty() && predicate_access_paths.empty()) { |
1406 | 1.50k | return Status::OK(); |
1407 | 1.50k | } |
1408 | | |
1409 | 6.19k | const auto requirement_before_access_path = _read_requirement; |
1410 | 6.19k | if (!predicate_access_paths.empty()) { |
1411 | 285 | set_read_requirement_self(ReadRequirement::PREDICATE); |
1412 | 285 | DLOG(INFO) << "Map column iterator set sub-column " << _column_name << " to PREDICATE"; |
1413 | 285 | } |
1414 | | |
1415 | 6.19k | const bool has_current_level_data_path = |
1416 | 7.15k | std::ranges::any_of(all_access_paths, [this](const TColumnAccessPath& path) { |
1417 | 7.15k | return is_current_level_data_access_path(path, _column_name); |
1418 | 7.15k | }); |
1419 | 6.19k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
1420 | 6.19k | auto sub_predicate_access_paths = |
1421 | 6.19k | DORIS_TRY(_get_sub_access_paths(predicate_access_paths, true)); |
1422 | 6.19k | if (has_current_level_data_path) { |
1423 | 3.97k | remove_current_level_meta_access_paths(sub_all_access_paths); |
1424 | 3.97k | } |
1425 | 6.19k | const bool has_current_level_predicate_meta_path = |
1426 | 6.19k | std::ranges::any_of(sub_predicate_access_paths, is_current_level_meta_access_path); |
1427 | | // Current-level predicate metadata paths are consumed by this map iterator and must not be |
1428 | | // forwarded to key/value children. The FE keeps all_access_paths as a superset of predicate |
1429 | | // paths, so meta-only mode is still decided from sub_all_access_paths below. |
1430 | 6.19k | remove_current_level_meta_access_paths(sub_predicate_access_paths); |
1431 | | |
1432 | 6.19k | if (sub_predicate_access_paths.empty() && _read_requirement == ReadRequirement::PREDICATE && |
1433 | 6.19k | !has_current_level_predicate_meta_path) { |
1434 | | // if no sub-column in predicate_access_paths, but current column is PREDICATE, |
1435 | | // then we should set key/value iterator to PREDICATE too. |
1436 | 1 | _key_iterator->set_read_requirement(ReadRequirement::PREDICATE); |
1437 | 1 | _val_iterator->set_read_requirement(ReadRequirement::PREDICATE); |
1438 | 1 | } |
1439 | | |
1440 | 6.19k | if (sub_predicate_access_paths.empty()) { |
1441 | | // Check for meta-only modes (OFFSET_ONLY or NULL_MAP_ONLY). Only skip key/value |
1442 | | // iterators when no predicate sub-path needs them in the predicate phase. |
1443 | 5.93k | _check_and_set_meta_read_mode(requirement_before_access_path, sub_all_access_paths); |
1444 | 5.93k | if (read_offset_only()) { |
1445 | 457 | _key_iterator->set_read_requirement(ReadRequirement::SKIP); |
1446 | 457 | _val_iterator->set_read_requirement(ReadRequirement::SKIP); |
1447 | 457 | DLOG(INFO) << "Map column iterator set column " << _column_name |
1448 | 457 | << " to OFFSET_ONLY meta read mode, key/value columns set to SKIP"; |
1449 | 457 | return Status::OK(); |
1450 | 457 | } |
1451 | 5.47k | if (read_null_map_only()) { |
1452 | 20 | _key_iterator->set_read_requirement(ReadRequirement::SKIP); |
1453 | 20 | _val_iterator->set_read_requirement(ReadRequirement::SKIP); |
1454 | 20 | DLOG(INFO) << "Map column iterator set column " << _column_name |
1455 | 20 | << " to NULL_MAP_ONLY meta read mode, key/value columns set to SKIP"; |
1456 | 20 | return Status::OK(); |
1457 | 20 | } |
1458 | 5.47k | } |
1459 | | |
1460 | | // A current-level data path is consumed by _get_sub_access_paths() and leaves |
1461 | | // sub_all_access_paths empty after marking key/value as lazy-read targets. Predicate |
1462 | | // sub-paths still have to be routed to child iterators for the predicate phase. |
1463 | 5.72k | if (sub_all_access_paths.empty() && sub_predicate_access_paths.empty()) { |
1464 | 3.98k | return Status::OK(); |
1465 | 3.98k | } |
1466 | | |
1467 | 1.73k | TColumnAccessPaths key_all_access_paths; |
1468 | 1.73k | TColumnAccessPaths val_all_access_paths; |
1469 | 1.73k | TColumnAccessPaths key_predicate_access_paths; |
1470 | 1.73k | TColumnAccessPaths val_predicate_access_paths; |
1471 | | |
1472 | 2.66k | for (auto paths : sub_all_access_paths) { |
1473 | 2.66k | if (paths.data_access_path.path[0] == ACCESS_ALL) { |
1474 | | // ACCESS_ALL means element_at(map, key) style access: the key column must be |
1475 | | // fully read so that the runtime can match the requested key, while any sub-path |
1476 | | // qualifiers (e.g. OFFSET) apply only to the value column. |
1477 | | // For key: create a path with just the column name (= full data access). |
1478 | 1.14k | TColumnAccessPath key_path; |
1479 | 1.14k | key_path.__set_type(paths.type); |
1480 | 1.14k | TDataAccessPath key_data_path; |
1481 | 1.14k | key_data_path.__set_path({_key_iterator->column_name()}); |
1482 | 1.14k | key_path.__set_data_access_path(key_data_path); |
1483 | 1.14k | key_all_access_paths.emplace_back(std::move(key_path)); |
1484 | | // For value: pass the full sub-path so qualifiers like OFFSET propagate. |
1485 | 1.14k | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1486 | 1.14k | val_all_access_paths.emplace_back(paths); |
1487 | 1.52k | } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) { |
1488 | 1.21k | paths.data_access_path.path[0] = _key_iterator->column_name(); |
1489 | 1.21k | key_all_access_paths.emplace_back(paths); |
1490 | 1.21k | } else if (paths.data_access_path.path[0] == ACCESS_MAP_VALUES) { |
1491 | 315 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1492 | 315 | val_all_access_paths.emplace_back(paths); |
1493 | 315 | } |
1494 | 2.66k | } |
1495 | 1.73k | for (auto paths : sub_predicate_access_paths) { |
1496 | 413 | if (paths.data_access_path.path[0] == ACCESS_ALL) { |
1497 | | // Same logic as above: key needs full data, value gets the sub-path. |
1498 | 208 | TColumnAccessPath key_path; |
1499 | 208 | key_path.__set_type(paths.type); |
1500 | 208 | TDataAccessPath key_data_path; |
1501 | 208 | key_data_path.__set_path({_key_iterator->column_name()}); |
1502 | 208 | key_path.__set_data_access_path(key_data_path); |
1503 | 208 | key_predicate_access_paths.emplace_back(std::move(key_path)); |
1504 | 208 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1505 | 208 | val_predicate_access_paths.emplace_back(paths); |
1506 | 208 | } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) { |
1507 | 190 | paths.data_access_path.path[0] = _key_iterator->column_name(); |
1508 | 190 | key_predicate_access_paths.emplace_back(paths); |
1509 | 190 | } else if (paths.data_access_path.path[0] == ACCESS_MAP_VALUES) { |
1510 | 15 | paths.data_access_path.path[0] = _val_iterator->column_name(); |
1511 | 15 | val_predicate_access_paths.emplace_back(paths); |
1512 | 15 | } |
1513 | 413 | } |
1514 | | |
1515 | 1.73k | const auto need_read_keys = |
1516 | 1.73k | !key_all_access_paths.empty() || !key_predicate_access_paths.empty(); |
1517 | 1.73k | const auto need_read_values = |
1518 | 1.73k | !val_all_access_paths.empty() || !val_predicate_access_paths.empty(); |
1519 | | |
1520 | 1.73k | if (need_read_keys) { |
1521 | 1.46k | RETURN_IF_ERROR( |
1522 | 1.46k | _key_iterator->set_access_paths(key_all_access_paths, key_predicate_access_paths)); |
1523 | | // Apply LAZY_OUTPUT after child predicate paths have been handled. Read requirements are |
1524 | | // monotonic, so a predicate-only child already promoted to PREDICATE will not |
1525 | | // be downgraded, while a non-predicate child becomes a lazy materialization target. |
1526 | 1.46k | _key_iterator->set_read_requirement_self(ReadRequirement::LAZY_OUTPUT); |
1527 | 1.46k | } else { |
1528 | 272 | _key_iterator->set_read_requirement(ReadRequirement::SKIP); |
1529 | 272 | DLOG(INFO) << "Map column iterator set key column to SKIP"; |
1530 | 272 | } |
1531 | | |
1532 | 1.73k | if (need_read_values) { |
1533 | 1.45k | RETURN_IF_ERROR( |
1534 | 1.45k | _val_iterator->set_access_paths(val_all_access_paths, val_predicate_access_paths)); |
1535 | | // Same as keys: predicate-only value paths stay PREDICATE because this |
1536 | | // post-processing update cannot lower a stronger child requirement. |
1537 | 1.45k | _val_iterator->set_read_requirement_self(ReadRequirement::LAZY_OUTPUT); |
1538 | 1.45k | } else { |
1539 | 277 | _val_iterator->set_read_requirement(ReadRequirement::SKIP); |
1540 | 277 | DLOG(INFO) << "Map column iterator set value column to SKIP"; |
1541 | 277 | } |
1542 | 1.73k | return Status::OK(); |
1543 | 1.73k | } |
1544 | | |
1545 | 80.5k | void MapFileColumnIterator::set_read_phase(ReadPhase mode) { |
1546 | 80.5k | ColumnIterator::set_read_phase(mode); |
1547 | 80.5k | _key_iterator->set_read_phase(mode); |
1548 | 80.5k | _val_iterator->set_read_phase(mode); |
1549 | 80.5k | } |
1550 | | |
1551 | 791 | void MapFileColumnIterator::finalize_lazy_phase(MutableColumnPtr& dst) { |
1552 | 791 | _recovery_from_place_holder_column(dst); |
1553 | 791 | auto& map_column = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>( |
1554 | 791 | dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst); |
1555 | 791 | auto keys_ptr = IColumn::mutate(std::move(map_column.get_keys_ptr())); |
1556 | 791 | auto vals_ptr = IColumn::mutate(std::move(map_column.get_values_ptr())); |
1557 | 791 | _key_iterator->finalize_lazy_phase(keys_ptr); |
1558 | 791 | _val_iterator->finalize_lazy_phase(vals_ptr); |
1559 | 791 | map_column.get_keys_ptr() = std::move(keys_ptr); |
1560 | 791 | map_column.get_values_ptr() = std::move(vals_ptr); |
1561 | 791 | } |
1562 | | |
1563 | 53 | void MapFileColumnIterator::set_read_requirement(ReadRequirement requirement) { |
1564 | 53 | set_read_requirement_self(requirement); |
1565 | 53 | _key_iterator->set_read_requirement(requirement); |
1566 | 53 | _val_iterator->set_read_requirement(requirement); |
1567 | 53 | } |
1568 | | |
1569 | 7.03k | bool MapFileColumnIterator::has_lazy_read_target() const { |
1570 | 7.03k | return _read_requirement == ReadRequirement::LAZY_OUTPUT || |
1571 | 7.03k | _key_iterator->has_lazy_read_target() || _val_iterator->has_lazy_read_target(); |
1572 | 7.03k | } |
1573 | | |
1574 | | //////////////////////////////////////////////////////////////////////////////// |
1575 | | |
1576 | | StructFileColumnIterator::StructFileColumnIterator( |
1577 | | std::shared_ptr<ColumnReader> reader, ColumnIteratorUPtr null_iterator, |
1578 | | std::vector<ColumnIteratorUPtr>&& sub_column_iterators) |
1579 | 8.29k | : _struct_reader(reader), _sub_column_iterators(std::move(sub_column_iterators)) { |
1580 | 8.29k | if (_struct_reader->is_nullable()) { |
1581 | 7.38k | _null_iterator = std::move(null_iterator); |
1582 | 7.38k | } |
1583 | 8.29k | } |
1584 | | |
1585 | 8.19k | Status StructFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1586 | 8.19k | if (_read_requirement == ReadRequirement::SKIP) { |
1587 | 0 | DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading."; |
1588 | 0 | return Status::OK(); |
1589 | 0 | } |
1590 | | |
1591 | 29.9k | for (auto& column_iterator : _sub_column_iterators) { |
1592 | 29.9k | RETURN_IF_ERROR(column_iterator->init(opts)); |
1593 | 29.9k | } |
1594 | 8.19k | if (_struct_reader->is_nullable()) { |
1595 | 7.36k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
1596 | 7.36k | } |
1597 | 8.19k | return Status::OK(); |
1598 | 8.19k | } |
1599 | | |
1600 | 9.99k | Status StructFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1601 | 9.99k | if (!need_to_read()) { |
1602 | 2 | DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading."; |
1603 | 2 | _convert_to_place_holder_column(dst, *n); |
1604 | 2 | return Status::OK(); |
1605 | 2 | } |
1606 | | |
1607 | 9.98k | _recovery_from_place_holder_column(dst); |
1608 | | |
1609 | 9.98k | if (read_null_map_only()) { |
1610 | | // NULL_MAP_ONLY mode: read null map, fill nested ColumnStruct with empty defaults |
1611 | 3 | DORIS_CHECK(is_column_nullable(*dst)); |
1612 | 3 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
1613 | 3 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
1614 | 3 | size_t num_read = *n; |
1615 | 3 | if (_null_iterator) { |
1616 | 3 | bool null_signs_has_null = false; |
1617 | 3 | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1618 | 3 | RETURN_IF_ERROR( |
1619 | 3 | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1620 | 3 | } else { |
1621 | | // schema-change: column became nullable but old segment has no null data |
1622 | 0 | null_map_ptr->insert_many_vals(0, num_read); |
1623 | 0 | } |
1624 | 3 | DCHECK(num_read == *n); |
1625 | | // fill nested ColumnStruct with defaults to maintain consistent column sizes |
1626 | 3 | auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>( |
1627 | 3 | nullable_col.get_nested_column()); |
1628 | 3 | column_struct.insert_many_defaults(num_read); |
1629 | 3 | *has_null = true; |
1630 | 3 | return Status::OK(); |
1631 | 3 | } |
1632 | | |
1633 | 9.98k | auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>( |
1634 | 9.98k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
1635 | 9.98k | : *dst); |
1636 | 35.8k | for (size_t i = 0; i < column_struct.tuple_size(); i++) { |
1637 | 25.8k | size_t num_read = *n; |
1638 | 25.8k | auto sub_column_ptr = IColumn::mutate(std::move(column_struct.get_column_ptr(i))); |
1639 | 25.8k | Defer defer_sub_column { |
1640 | 25.8k | [&] { column_struct.get_column_ptr(i) = std::move(sub_column_ptr); }}; |
1641 | 25.8k | bool column_has_null = false; |
1642 | 25.8k | RETURN_IF_ERROR( |
1643 | 25.8k | _sub_column_iterators[i]->next_batch(&num_read, sub_column_ptr, &column_has_null)); |
1644 | 25.8k | DCHECK(num_read == *n); |
1645 | 25.8k | } |
1646 | | |
1647 | 9.98k | if (is_column_nullable(*dst) && need_to_read_meta_columns()) { |
1648 | 6.80k | size_t num_read = *n; |
1649 | 6.80k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
1650 | | // in not-null to null linked-schemachange mode, |
1651 | | // actually we do not change dat data include meta in footer, |
1652 | | // so may dst from changed meta which is nullable but old data is not nullable, |
1653 | | // if so, we should set null_map to all null by default |
1654 | 6.80k | if (_null_iterator) { |
1655 | 6.71k | bool null_signs_has_null = false; |
1656 | 6.71k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
1657 | 6.71k | RETURN_IF_ERROR( |
1658 | 6.71k | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
1659 | 6.71k | } else { |
1660 | 88 | null_map_ptr->insert_many_vals(0, num_read); |
1661 | 88 | } |
1662 | 6.80k | DCHECK(num_read == *n); |
1663 | 6.80k | } |
1664 | | |
1665 | 9.98k | return Status::OK(); |
1666 | 9.98k | } |
1667 | | |
1668 | 9.98k | Status StructFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
1669 | 9.98k | if (!need_to_read()) { |
1670 | 2 | DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading."; |
1671 | 2 | return Status::OK(); |
1672 | 2 | } |
1673 | | |
1674 | 9.98k | if (read_null_map_only()) { |
1675 | | // In NULL_MAP_ONLY mode, only seek the null iterator; skip all sub-column iterators |
1676 | 2 | if (_struct_reader->is_nullable() && _null_iterator) { |
1677 | 2 | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1678 | 2 | } |
1679 | 2 | return Status::OK(); |
1680 | 2 | } |
1681 | | |
1682 | 25.8k | for (auto& column_iterator : _sub_column_iterators) { |
1683 | 25.8k | RETURN_IF_ERROR(column_iterator->seek_to_ordinal(ord)); |
1684 | 25.8k | } |
1685 | | |
1686 | 9.98k | if (_struct_reader->is_nullable() && need_to_read_meta_columns()) { |
1687 | 6.71k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
1688 | 6.71k | } |
1689 | 9.98k | return Status::OK(); |
1690 | 9.98k | } |
1691 | | |
1692 | 3.59k | Status StructFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1693 | 10.5k | for (auto& column_iterator : _sub_column_iterators) { |
1694 | 10.5k | RETURN_IF_ERROR(column_iterator->init_prefetcher(params)); |
1695 | 10.5k | } |
1696 | 3.59k | if (_struct_reader->is_nullable()) { |
1697 | 3.22k | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
1698 | 3.22k | } |
1699 | 3.59k | return Status::OK(); |
1700 | 3.59k | } |
1701 | | |
1702 | | void StructFileColumnIterator::collect_prefetchers( |
1703 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1704 | 3.56k | PrefetcherInitMethod init_method) { |
1705 | 3.56k | if (!need_to_read()) { |
1706 | 0 | return; |
1707 | 0 | } |
1708 | 3.56k | if (_struct_reader->is_nullable()) { |
1709 | 3.19k | _null_iterator->collect_prefetchers(prefetchers, init_method); |
1710 | 3.19k | } |
1711 | 3.56k | if (read_null_map_only()) { |
1712 | 2 | return; |
1713 | 2 | } |
1714 | 10.4k | for (auto& column_iterator : _sub_column_iterators) { |
1715 | 10.4k | if (column_iterator->need_to_read()) { |
1716 | 10.1k | column_iterator->collect_prefetchers(prefetchers, init_method); |
1717 | 10.1k | } |
1718 | 10.4k | } |
1719 | 3.56k | } |
1720 | | |
1721 | | Status StructFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
1722 | 3.63k | MutableColumnPtr& dst) { |
1723 | 3.63k | if (!need_to_read()) { |
1724 | 0 | DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading."; |
1725 | 0 | _convert_to_place_holder_column(dst, count); |
1726 | 0 | return Status::OK(); |
1727 | 0 | } |
1728 | | |
1729 | 3.63k | _recovery_from_place_holder_column(dst); |
1730 | | |
1731 | 3.63k | if (count == 0) { |
1732 | 0 | return Status::OK(); |
1733 | 0 | } |
1734 | | |
1735 | 3.63k | size_t this_run = 1; |
1736 | 3.63k | auto start_idx = rowids[0]; |
1737 | 3.63k | auto last_idx = rowids[0]; |
1738 | 5.60k | for (size_t i = 1; i < count; ++i) { |
1739 | 1.96k | if (last_idx == rowids[i] - 1) { |
1740 | 320 | last_idx = rowids[i]; |
1741 | 320 | this_run++; |
1742 | 320 | continue; |
1743 | 320 | } |
1744 | 1.64k | RETURN_IF_ERROR(seek_to_ordinal(start_idx)); |
1745 | 1.64k | size_t num_read = this_run; |
1746 | 1.64k | RETURN_IF_ERROR(next_batch(&num_read, dst)); |
1747 | 1.64k | DCHECK_EQ(num_read, this_run); |
1748 | | |
1749 | 1.64k | start_idx = rowids[i]; |
1750 | 1.64k | last_idx = rowids[i]; |
1751 | 1.64k | this_run = 1; |
1752 | 1.64k | } |
1753 | | |
1754 | 3.63k | RETURN_IF_ERROR(seek_to_ordinal(start_idx)); |
1755 | 3.63k | size_t num_read = this_run; |
1756 | 3.63k | RETURN_IF_ERROR(next_batch(&num_read, dst)); |
1757 | 3.63k | DCHECK_EQ(num_read, this_run); |
1758 | 3.63k | return Status::OK(); |
1759 | 3.63k | } |
1760 | | |
1761 | 7.03k | void StructFileColumnIterator::set_lazy_output_requirement() { |
1762 | 7.03k | set_read_requirement_self(ReadRequirement::LAZY_OUTPUT); |
1763 | 27.0k | for (auto& sub_iterator : _sub_column_iterators) { |
1764 | 27.0k | sub_iterator->set_lazy_output_requirement(); |
1765 | 27.0k | } |
1766 | 7.03k | } |
1767 | | |
1768 | 7.33k | void StructFileColumnIterator::remove_pruned_sub_iterators() { |
1769 | 36.4k | for (auto it = _sub_column_iterators.begin(); it != _sub_column_iterators.end();) { |
1770 | 29.0k | auto& sub_iterator = *it; |
1771 | 29.0k | if (sub_iterator->read_requirement() == ReadRequirement::SKIP) { |
1772 | 1.55k | DLOG(INFO) << "Struct column iterator remove pruned sub-column " |
1773 | 1.55k | << sub_iterator->column_name(); |
1774 | 1.55k | it = _sub_column_iterators.erase(it); |
1775 | 27.5k | } else { |
1776 | 27.5k | sub_iterator->remove_pruned_sub_iterators(); |
1777 | 27.5k | ++it; |
1778 | 27.5k | } |
1779 | 29.0k | } |
1780 | 7.33k | } |
1781 | | |
1782 | | Status StructFileColumnIterator::set_access_paths( |
1783 | | const TColumnAccessPaths& all_access_paths, |
1784 | 6.82k | const TColumnAccessPaths& predicate_access_paths) { |
1785 | 6.82k | if (all_access_paths.empty() && predicate_access_paths.empty()) { |
1786 | 1.86k | return Status::OK(); |
1787 | 1.86k | } |
1788 | | |
1789 | 4.96k | const auto requirement_before_access_path = _read_requirement; |
1790 | 4.96k | if (!predicate_access_paths.empty()) { |
1791 | 177 | set_read_requirement_self(ReadRequirement::PREDICATE); |
1792 | 177 | DLOG(INFO) << "Struct column iterator set sub-column " << _column_name << " to PREDICATE"; |
1793 | 177 | } |
1794 | | |
1795 | 4.96k | const bool has_current_level_data_path = |
1796 | 5.22k | std::ranges::any_of(all_access_paths, [this](const TColumnAccessPath& path) { |
1797 | 5.22k | return is_current_level_data_access_path(path, _column_name); |
1798 | 5.22k | }); |
1799 | 4.96k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
1800 | 4.95k | auto sub_predicate_access_paths = |
1801 | 4.95k | DORIS_TRY(_get_sub_access_paths(predicate_access_paths, true)); |
1802 | 4.95k | if (has_current_level_data_path) { |
1803 | 4.57k | remove_current_level_meta_access_paths(sub_all_access_paths); |
1804 | 4.57k | } |
1805 | 4.95k | const bool has_current_level_predicate_meta_path = |
1806 | 4.95k | std::ranges::any_of(sub_predicate_access_paths, is_current_level_meta_access_path); |
1807 | | // Current-level predicate metadata paths are consumed by this struct iterator and must not be |
1808 | | // forwarded to child fields. The FE keeps all_access_paths as a superset of predicate paths, so |
1809 | | // NULL_MAP_ONLY is still decided from sub_all_access_paths below. |
1810 | 4.95k | remove_current_level_meta_access_paths(sub_predicate_access_paths); |
1811 | | |
1812 | 4.95k | if (sub_predicate_access_paths.empty()) { |
1813 | | // Check for NULL_MAP_ONLY mode: only read null map, skip all sub-columns. |
1814 | | // Do not take this early return when predicate child paths must still be read. |
1815 | 4.79k | _check_and_set_meta_read_mode(requirement_before_access_path, sub_all_access_paths); |
1816 | 4.79k | if (read_null_map_only()) { |
1817 | 9 | for (auto& sub_iterator : _sub_column_iterators) { |
1818 | 9 | sub_iterator->set_read_requirement(ReadRequirement::SKIP); |
1819 | 9 | } |
1820 | 5 | DLOG(INFO) << "Struct column iterator set column " << _column_name |
1821 | 5 | << " to NULL_MAP_ONLY meta read mode, all sub-columns set to SKIP"; |
1822 | 5 | return Status::OK(); |
1823 | 5 | } |
1824 | 4.79k | } |
1825 | | |
1826 | 4.95k | const auto no_sub_column_to_skip = sub_all_access_paths.empty(); |
1827 | 4.95k | const auto no_predicate_sub_column = sub_predicate_access_paths.empty(); |
1828 | | |
1829 | 23.6k | for (auto& sub_iterator : _sub_column_iterators) { |
1830 | 23.6k | const auto name = sub_iterator->column_name(); |
1831 | 23.6k | TColumnAccessPaths sub_all_access_paths_of_this; |
1832 | 23.6k | if (!no_sub_column_to_skip) { |
1833 | 3.16k | for (const auto& paths : sub_all_access_paths) { |
1834 | 3.16k | if (paths.data_access_path.path[0] == name) { |
1835 | 633 | sub_all_access_paths_of_this.emplace_back(paths); |
1836 | 633 | } |
1837 | 3.16k | } |
1838 | 2.19k | } |
1839 | | |
1840 | 23.6k | TColumnAccessPaths sub_predicate_access_paths_of_this; |
1841 | 23.6k | if (!no_predicate_sub_column) { |
1842 | 1.22k | for (const auto& paths : sub_predicate_access_paths) { |
1843 | 1.22k | if (StringCaseEqual()(paths.data_access_path.path[0], name)) { |
1844 | 165 | sub_predicate_access_paths_of_this.emplace_back(paths); |
1845 | 165 | } |
1846 | 1.22k | } |
1847 | 1.21k | } |
1848 | | |
1849 | | // Predicate-only child paths still need to be routed to the child iterator |
1850 | | // even when the child is not requested by ordinary projection access paths. |
1851 | 23.6k | const bool need_to_read = no_sub_column_to_skip || !sub_all_access_paths_of_this.empty() || |
1852 | 23.6k | !sub_predicate_access_paths_of_this.empty(); |
1853 | 23.6k | if (!need_to_read) { |
1854 | 1.57k | set_read_requirement_self(ReadRequirement::SKIP); |
1855 | 1.57k | sub_iterator->set_read_requirement(ReadRequirement::SKIP); |
1856 | 1.57k | DLOG(INFO) << "Struct column iterator set sub-column " << name << " to SKIP"; |
1857 | 1.57k | continue; |
1858 | 1.57k | } |
1859 | | |
1860 | 22.1k | if (no_predicate_sub_column && _read_requirement == ReadRequirement::PREDICATE && |
1861 | 22.1k | !has_current_level_predicate_meta_path) { |
1862 | | // if no sub-column in predicate_access_paths, but current column is PREDICATE, |
1863 | | // then we should set sub iterator to PREDICATE too. |
1864 | 6 | sub_iterator->set_read_requirement(ReadRequirement::PREDICATE); |
1865 | 6 | } |
1866 | | |
1867 | 22.1k | RETURN_IF_ERROR(sub_iterator->set_access_paths(sub_all_access_paths_of_this, |
1868 | 22.1k | sub_predicate_access_paths_of_this)); |
1869 | | // Set LAZY_OUTPUT after routing child predicate paths. If the child was needed only for |
1870 | | // predicate evaluation, set_access_paths() has already promoted it to |
1871 | | // PREDICATE and this monotonic update will not downgrade it. Otherwise, this |
1872 | | // marks the child as a lazy materialization target. |
1873 | 22.1k | set_read_requirement_self(ReadRequirement::LAZY_OUTPUT); |
1874 | 22.1k | sub_iterator->set_read_requirement_self(ReadRequirement::LAZY_OUTPUT); |
1875 | 22.1k | } |
1876 | 4.95k | return Status::OK(); |
1877 | 4.95k | } |
1878 | | |
1879 | 34.7k | void StructFileColumnIterator::set_read_phase(ReadPhase mode) { |
1880 | 34.7k | ColumnIterator::set_read_phase(mode); |
1881 | 103k | for (auto& sub_iterator : _sub_column_iterators) { |
1882 | 103k | sub_iterator->set_read_phase(mode); |
1883 | 103k | } |
1884 | 34.7k | } |
1885 | | |
1886 | 1.52k | void StructFileColumnIterator::finalize_lazy_phase(MutableColumnPtr& dst) { |
1887 | 1.52k | _recovery_from_place_holder_column(dst); |
1888 | 1.52k | auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>( |
1889 | 1.52k | dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst); |
1890 | | |
1891 | 4.18k | for (size_t i = 0; i < _sub_column_iterators.size(); ++i) { |
1892 | 2.66k | auto& sub_column = column_struct.get_column_ptr(i); |
1893 | 2.66k | MutableColumnPtr mutable_sub_column = IColumn::mutate(std::move(sub_column)); |
1894 | 2.66k | _sub_column_iterators[i]->finalize_lazy_phase(mutable_sub_column); |
1895 | 2.66k | sub_column = std::move(mutable_sub_column); |
1896 | 2.66k | } |
1897 | 1.52k | } |
1898 | | |
1899 | 23 | void StructFileColumnIterator::set_read_requirement(ReadRequirement requirement) { |
1900 | 23 | set_read_requirement_self(requirement); |
1901 | 43 | for (const auto& sub_column_iterator : _sub_column_iterators) { |
1902 | 43 | sub_column_iterator->set_read_requirement(requirement); |
1903 | 43 | } |
1904 | 23 | } |
1905 | | |
1906 | 13.3k | bool StructFileColumnIterator::has_lazy_read_target() const { |
1907 | 13.3k | if (_read_requirement == ReadRequirement::LAZY_OUTPUT) { |
1908 | 2.04k | return true; |
1909 | 2.04k | } |
1910 | 11.3k | return std::any_of(_sub_column_iterators.begin(), _sub_column_iterators.end(), |
1911 | 13.8k | [](const auto& sub_column_iterator) { |
1912 | 13.8k | return sub_column_iterator->has_lazy_read_target(); |
1913 | 13.8k | }); |
1914 | 13.3k | } |
1915 | | |
1916 | | //////////////////////////////////////////////////////////////////////////////// |
1917 | 104k | Status OffsetFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
1918 | 104k | RETURN_IF_ERROR(_offset_iterator->init(opts)); |
1919 | | // allocate peek tmp column once |
1920 | 104k | _peek_tmp_col = ColumnOffset64::create(); |
1921 | 104k | return Status::OK(); |
1922 | 104k | } |
1923 | | |
1924 | 141k | Status OffsetFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
1925 | 141k | RETURN_IF_ERROR(_offset_iterator->next_batch(n, dst, has_null)); |
1926 | 141k | return Status::OK(); |
1927 | 141k | } |
1928 | | |
1929 | 266k | Status OffsetFileColumnIterator::_peek_one_offset(ordinal_t* offset) { |
1930 | 266k | if (_offset_iterator->get_current_page()->has_remaining()) { |
1931 | 176k | PageDecoder* offset_page_decoder = _offset_iterator->get_current_page()->data_decoder.get(); |
1932 | 176k | size_t n = 1; |
1933 | 176k | _peek_tmp_col->clear(); |
1934 | 176k | RETURN_IF_ERROR(offset_page_decoder->peek_next_batch(&n, _peek_tmp_col)); // not null |
1935 | 176k | DCHECK(_peek_tmp_col->size() == 1); |
1936 | 176k | *offset = |
1937 | 176k | assert_cast<const ColumnOffset64*, TypeCheckOnRelease::DISABLE>(_peek_tmp_col.get()) |
1938 | 176k | ->get_element(0); |
1939 | 176k | } else { |
1940 | 90.7k | *offset = _offset_iterator->get_current_page()->next_array_item_ordinal; |
1941 | 90.7k | } |
1942 | 266k | return Status::OK(); |
1943 | 266k | } |
1944 | | |
1945 | 54.9k | Status OffsetFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
1946 | 54.9k | return _offset_iterator->init_prefetcher(params); |
1947 | 54.9k | } |
1948 | | |
1949 | | void OffsetFileColumnIterator::collect_prefetchers( |
1950 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
1951 | 54.7k | PrefetcherInitMethod init_method) { |
1952 | 54.7k | _offset_iterator->collect_prefetchers(prefetchers, init_method); |
1953 | 54.7k | } |
1954 | | |
1955 | | /** |
1956 | | * first_storage_offset read from page should smaller than next_storage_offset which here call _peek_one_offset from page, |
1957 | | and first_column_offset is keep in memory data which is different dimension with (first_storage_offset and next_storage_offset) |
1958 | | eg. step1. read page: first_storage_offset = 16382 |
1959 | | step2. read page below with _peek_one_offset(&last_offset): last_offset = 16387 |
1960 | | step3. first_offset = 126 which is calculate in column offsets |
1961 | | for loop column offsets element in size |
1962 | | we can calculate from first_storage_offset to next_storage_offset one by one to fill with offsets_data in memory column offsets |
1963 | | * @param start |
1964 | | * @param column_offsets |
1965 | | * @return |
1966 | | */ |
1967 | | Status OffsetFileColumnIterator::_calculate_offsets(ssize_t start, |
1968 | 126k | ColumnArray::ColumnOffsets& column_offsets) { |
1969 | 126k | ordinal_t next_storage_offset = 0; |
1970 | 126k | RETURN_IF_ERROR(_peek_one_offset(&next_storage_offset)); |
1971 | | |
1972 | | // calculate real offsets |
1973 | 126k | auto& offsets_data = column_offsets.get_data(); |
1974 | 126k | ordinal_t first_column_offset = offsets_data[start - 1]; // -1 is valid |
1975 | 126k | ordinal_t first_storage_offset = offsets_data[start]; |
1976 | 126k | DCHECK(next_storage_offset >= first_storage_offset); |
1977 | 6.05M | for (ssize_t i = start; i < offsets_data.size() - 1; ++i) { |
1978 | 5.92M | offsets_data[i] = first_column_offset + (offsets_data[i + 1] - first_storage_offset); |
1979 | 5.92M | } |
1980 | | // last offset |
1981 | 126k | offsets_data[offsets_data.size() - 1] = |
1982 | 126k | first_column_offset + (next_storage_offset - first_storage_offset); |
1983 | 126k | return Status::OK(); |
1984 | 126k | } |
1985 | | |
1986 | | //////////////////////////////////////////////////////////////////////////////// |
1987 | | ArrayFileColumnIterator::ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
1988 | | OffsetFileColumnIteratorUPtr offset_reader, |
1989 | | ColumnIteratorUPtr item_iterator, |
1990 | | ColumnIteratorUPtr null_iterator) |
1991 | 66.8k | : _array_reader(reader), |
1992 | 66.8k | _offset_iterator(std::move(offset_reader)), |
1993 | 66.8k | _item_iterator(std::move(item_iterator)) { |
1994 | 66.8k | if (_array_reader->is_nullable()) { |
1995 | 47.9k | _null_iterator = std::move(null_iterator); |
1996 | 47.9k | } |
1997 | 66.8k | } |
1998 | | |
1999 | 66.6k | Status ArrayFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
2000 | 66.6k | if (_read_requirement == ReadRequirement::SKIP) { |
2001 | 56 | DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading."; |
2002 | 56 | return Status::OK(); |
2003 | 56 | } |
2004 | | |
2005 | 66.6k | RETURN_IF_ERROR(_offset_iterator->init(opts)); |
2006 | 66.6k | RETURN_IF_ERROR(_item_iterator->init(opts)); |
2007 | 66.6k | if (_array_reader->is_nullable()) { |
2008 | 47.9k | RETURN_IF_ERROR(_null_iterator->init(opts)); |
2009 | 47.9k | } |
2010 | 66.6k | return Status::OK(); |
2011 | 66.6k | } |
2012 | | |
2013 | 105k | Status ArrayFileColumnIterator::_seek_by_offsets(ordinal_t ord) { |
2014 | 105k | if (read_offset_only()) { |
2015 | | // In OFFSET_ONLY mode, item iterator is SKIP, no need to seek it |
2016 | 1.34k | return Status::OK(); |
2017 | 1.34k | } |
2018 | | // using offsets info |
2019 | 104k | ordinal_t offset = 0; |
2020 | 104k | RETURN_IF_ERROR(_offset_iterator->_peek_one_offset(&offset)); |
2021 | 104k | RETURN_IF_ERROR(_item_iterator->seek_to_ordinal(offset)); |
2022 | 104k | return Status::OK(); |
2023 | 104k | } |
2024 | | |
2025 | 107k | Status ArrayFileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
2026 | 107k | if (!need_to_read()) { |
2027 | 1.22k | DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading."; |
2028 | 1.22k | return Status::OK(); |
2029 | 1.22k | } |
2030 | | |
2031 | 105k | if (read_null_map_only()) { |
2032 | | // In NULL_MAP_ONLY mode, only seek the null iterator; skip offset and item iterators |
2033 | 4 | if (_array_reader->is_nullable() && _null_iterator) { |
2034 | 4 | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
2035 | 4 | } |
2036 | 4 | return Status::OK(); |
2037 | 4 | } |
2038 | | |
2039 | 105k | RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord)); |
2040 | 105k | if (_array_reader->is_nullable()) { |
2041 | 86.4k | RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord)); |
2042 | 86.4k | } |
2043 | 105k | return _seek_by_offsets(ord); |
2044 | 105k | } |
2045 | | |
2046 | 107k | Status ArrayFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2047 | 107k | if (!need_to_read()) { |
2048 | 1.22k | DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading, read phase" |
2049 | 1.22k | << static_cast<int>(_read_phase) |
2050 | 1.22k | << ", read requirement: " << static_cast<int>(_read_requirement); |
2051 | 1.22k | _convert_to_place_holder_column(dst, *n); |
2052 | 1.22k | return Status::OK(); |
2053 | 1.22k | } |
2054 | | |
2055 | 105k | _recovery_from_place_holder_column(dst); |
2056 | | |
2057 | 105k | if (read_null_map_only()) { |
2058 | | // NULL_MAP_ONLY mode: read null map, fill nested ColumnArray with empty defaults |
2059 | 5 | DORIS_CHECK(is_column_nullable(*dst)); |
2060 | 5 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
2061 | 5 | auto null_map_ptr = nullable_col.get_null_map_column_ptr(); |
2062 | 5 | size_t num_read = *n; |
2063 | 5 | if (_null_iterator) { |
2064 | 5 | bool null_signs_has_null = false; |
2065 | 5 | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
2066 | 5 | RETURN_IF_ERROR( |
2067 | 5 | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
2068 | 5 | } else { |
2069 | | // schema-change: column became nullable but old segment has no null data |
2070 | 0 | null_map_ptr->insert_many_vals(0, num_read); |
2071 | 0 | } |
2072 | 5 | DCHECK(num_read == *n); |
2073 | | // fill nested ColumnArray with empty (zero-length) arrays |
2074 | 5 | auto& column_array = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>( |
2075 | 5 | nullable_col.get_nested_column()); |
2076 | 5 | column_array.insert_many_defaults(num_read); |
2077 | 5 | *has_null = true; |
2078 | 5 | return Status::OK(); |
2079 | 5 | } |
2080 | | |
2081 | 105k | auto& column_array = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>( |
2082 | 105k | is_column_nullable(*dst) ? static_cast<ColumnNullable&>(*dst).get_nested_column() |
2083 | 105k | : *dst); |
2084 | | |
2085 | 105k | bool offsets_has_null = false; |
2086 | 105k | const bool read_meta_columns = need_to_read_meta_columns(); |
2087 | 105k | MutableColumnPtr column_offsets_ptr; |
2088 | 105k | if (read_meta_columns) { |
2089 | 105k | column_offsets_ptr = IColumn::mutate(std::move(column_array.get_offsets_ptr())); |
2090 | 105k | } else { |
2091 | 576 | const auto base_offset = |
2092 | 576 | column_array.get_offsets().empty() ? 0 : column_array.get_offsets().back(); |
2093 | 576 | column_offsets_ptr = ColumnArray::ColumnOffsets::create(); |
2094 | 576 | assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*column_offsets_ptr) |
2095 | 576 | .insert_value(base_offset); |
2096 | 576 | } |
2097 | 105k | Defer defer_offsets {[&] { |
2098 | 105k | if (read_meta_columns) { |
2099 | 105k | auto typed_column_offsets_ptr = ColumnArray::ColumnOffsets::cast_to_column_mutptr( |
2100 | 105k | assert_cast<ColumnArray::ColumnOffsets*, TypeCheckOnRelease::DISABLE>( |
2101 | 105k | column_offsets_ptr.get())); |
2102 | 105k | column_offsets_ptr = nullptr; |
2103 | 105k | column_array.get_offsets_ptr() = std::move(typed_column_offsets_ptr); |
2104 | 105k | } |
2105 | 105k | }}; |
2106 | 105k | ssize_t start = column_offsets_ptr->size(); |
2107 | 105k | RETURN_IF_ERROR(_offset_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null)); |
2108 | 105k | if (*n == 0) { |
2109 | 0 | return Status::OK(); |
2110 | 0 | } |
2111 | 105k | auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr); |
2112 | 105k | RETURN_IF_ERROR(_offset_iterator->_calculate_offsets(start, column_offsets)); |
2113 | 105k | size_t num_items = |
2114 | 105k | column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid |
2115 | 105k | if (num_items > 0) { |
2116 | 77.8k | auto column_items_ptr = IColumn::mutate(std::move(column_array.get_data_ptr())); |
2117 | 77.8k | Defer defer_items {[&] { column_array.get_data_ptr() = std::move(column_items_ptr); }}; |
2118 | 77.8k | if (read_offset_only()) { |
2119 | | // OFFSET_ONLY mode: skip reading actual item data, fill with defaults |
2120 | 1.01k | column_items_ptr->insert_many_defaults(num_items); |
2121 | 76.8k | } else { |
2122 | 76.8k | size_t num_read = num_items; |
2123 | 76.8k | bool items_has_null = false; |
2124 | 76.8k | RETURN_IF_ERROR( |
2125 | 76.8k | _item_iterator->next_batch(&num_read, column_items_ptr, &items_has_null)); |
2126 | 76.8k | DCHECK(num_read == num_items); |
2127 | 76.8k | } |
2128 | 77.8k | } |
2129 | | |
2130 | 105k | if (is_column_nullable(*dst) && read_meta_columns) { |
2131 | 85.9k | auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr(); |
2132 | 85.9k | size_t num_read = *n; |
2133 | | // in not-null to null linked-schemachange mode, |
2134 | | // actually we do not change dat data include meta in footer, |
2135 | | // so may dst from changed meta which is nullable but old data is not nullable, |
2136 | | // if so, we should set null_map to all null by default |
2137 | 85.9k | if (_null_iterator) { |
2138 | 85.9k | bool null_signs_has_null = false; |
2139 | 85.9k | MutableColumnPtr null_map_column = std::move(null_map_ptr); |
2140 | 85.9k | RETURN_IF_ERROR( |
2141 | 85.9k | _null_iterator->next_batch(&num_read, null_map_column, &null_signs_has_null)); |
2142 | 18.4E | } else { |
2143 | 18.4E | null_map_ptr->insert_many_vals(0, num_read); |
2144 | 18.4E | } |
2145 | 85.9k | DCHECK(num_read == *n); |
2146 | 85.9k | } |
2147 | | |
2148 | 105k | return Status::OK(); |
2149 | 105k | } |
2150 | | |
2151 | 47.6k | Status ArrayFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
2152 | 47.6k | RETURN_IF_ERROR(_offset_iterator->init_prefetcher(params)); |
2153 | 47.6k | RETURN_IF_ERROR(_item_iterator->init_prefetcher(params)); |
2154 | 47.6k | if (_array_reader->is_nullable()) { |
2155 | 30.7k | RETURN_IF_ERROR(_null_iterator->init_prefetcher(params)); |
2156 | 30.7k | } |
2157 | 47.6k | return Status::OK(); |
2158 | 47.6k | } |
2159 | | |
2160 | | void ArrayFileColumnIterator::collect_prefetchers( |
2161 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
2162 | 47.5k | PrefetcherInitMethod init_method) { |
2163 | 47.5k | if (!need_to_read()) { |
2164 | 0 | return; |
2165 | 0 | } |
2166 | 47.5k | if (!read_null_map_only()) { |
2167 | 47.5k | _offset_iterator->collect_prefetchers(prefetchers, init_method); |
2168 | 47.5k | } |
2169 | 47.5k | if (_array_reader->is_nullable()) { |
2170 | 30.6k | _null_iterator->collect_prefetchers(prefetchers, init_method); |
2171 | 30.6k | } |
2172 | 47.5k | if (read_offset_only() || read_null_map_only()) { |
2173 | 1.34k | return; |
2174 | 1.34k | } |
2175 | | // the actual data pages to read of item column depends on the read result of offset column, |
2176 | | // so we can't init prefetch blocks according to rowids, just prefetch all data blocks here. |
2177 | 46.1k | if (_item_iterator->need_to_read()) { |
2178 | 46.1k | _item_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS); |
2179 | 46.1k | } |
2180 | 46.1k | } |
2181 | | |
2182 | | Status ArrayFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
2183 | 34.0k | MutableColumnPtr& dst) { |
2184 | 34.0k | if (!need_to_read()) { |
2185 | 0 | DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading."; |
2186 | 0 | _convert_to_place_holder_column(dst, count); |
2187 | 0 | return Status::OK(); |
2188 | 0 | } |
2189 | | |
2190 | 34.0k | _recovery_from_place_holder_column(dst); |
2191 | | |
2192 | 110k | for (size_t i = 0; i < count; ++i) { |
2193 | | // TODO(cambyszju): now read array one by one, need optimize later |
2194 | 76.8k | RETURN_IF_ERROR(seek_to_ordinal(rowids[i])); |
2195 | 76.8k | size_t num_read = 1; |
2196 | 76.8k | RETURN_IF_ERROR(next_batch(&num_read, dst)); |
2197 | 76.8k | } |
2198 | 34.0k | return Status::OK(); |
2199 | 34.0k | } |
2200 | | |
2201 | 48.0k | void ArrayFileColumnIterator::set_lazy_output_requirement() { |
2202 | 48.0k | set_read_requirement_self(ReadRequirement::LAZY_OUTPUT); |
2203 | 48.0k | _item_iterator->set_lazy_output_requirement(); |
2204 | 48.0k | } |
2205 | | |
2206 | 53.8k | void ArrayFileColumnIterator::remove_pruned_sub_iterators() { |
2207 | 53.8k | _item_iterator->remove_pruned_sub_iterators(); |
2208 | 53.8k | } |
2209 | | |
2210 | 306k | void ArrayFileColumnIterator::set_read_phase(ReadPhase mode) { |
2211 | 306k | ColumnIterator::set_read_phase(mode); |
2212 | 306k | _item_iterator->set_read_phase(mode); |
2213 | 306k | } |
2214 | | |
2215 | 824 | void ArrayFileColumnIterator::finalize_lazy_phase(MutableColumnPtr& dst) { |
2216 | 824 | _recovery_from_place_holder_column(dst); |
2217 | 824 | auto& column_array = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>( |
2218 | 824 | dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst); |
2219 | 824 | auto item_column_ptr = IColumn::mutate(std::move(column_array.get_data_ptr())); |
2220 | 824 | _item_iterator->finalize_lazy_phase(item_column_ptr); |
2221 | 824 | column_array.get_data_ptr() = std::move(item_column_ptr); |
2222 | 824 | } |
2223 | | |
2224 | 2.94k | void ArrayFileColumnIterator::set_read_requirement(ReadRequirement requirement) { |
2225 | 2.94k | set_read_requirement_self(requirement); |
2226 | 2.94k | _item_iterator->set_read_requirement(requirement); |
2227 | 2.94k | } |
2228 | | |
2229 | 10.0k | bool ArrayFileColumnIterator::has_lazy_read_target() const { |
2230 | 10.0k | return _read_requirement == ReadRequirement::LAZY_OUTPUT || |
2231 | 10.0k | _item_iterator->has_lazy_read_target(); |
2232 | 10.0k | } |
2233 | | |
2234 | | Status ArrayFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths, |
2235 | 53.3k | const TColumnAccessPaths& predicate_access_paths) { |
2236 | 53.3k | if (all_access_paths.empty() && predicate_access_paths.empty()) { |
2237 | 1.75k | return Status::OK(); |
2238 | 1.75k | } |
2239 | | |
2240 | 51.5k | const auto requirement_before_access_path = _read_requirement; |
2241 | 51.5k | if (!predicate_access_paths.empty()) { |
2242 | 3.39k | set_read_requirement_self(ReadRequirement::PREDICATE); |
2243 | 3.39k | DLOG(INFO) << "Array column iterator set sub-column " << _column_name << " to PREDICATE"; |
2244 | 3.39k | } |
2245 | | |
2246 | 51.5k | const bool has_current_level_data_path = |
2247 | 51.6k | std::ranges::any_of(all_access_paths, [this](const TColumnAccessPath& path) { |
2248 | 51.6k | return is_current_level_data_access_path(path, _column_name); |
2249 | 51.6k | }); |
2250 | 51.5k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
2251 | 51.5k | auto sub_predicate_access_paths = |
2252 | 51.5k | DORIS_TRY(_get_sub_access_paths(predicate_access_paths, true)); |
2253 | 51.5k | if (has_current_level_data_path) { |
2254 | | // A current-level data path already reads the array offsets while materializing the array. |
2255 | | // Do not let a redundant current-level OFFSET/NULL path switch this iterator into a |
2256 | | // meta-only mode that would skip item data. |
2257 | 45.7k | remove_current_level_meta_access_paths(sub_all_access_paths); |
2258 | 45.7k | } |
2259 | 51.5k | const bool has_current_level_predicate_meta_path = |
2260 | 51.5k | std::ranges::any_of(sub_predicate_access_paths, is_current_level_meta_access_path); |
2261 | | // Current-level predicate metadata paths are consumed by this array iterator and must not be |
2262 | | // forwarded to the item iterator. The FE keeps all_access_paths as a superset of predicate |
2263 | | // paths, so meta-only mode is still decided from sub_all_access_paths below. |
2264 | 51.5k | auto removed = |
2265 | 51.5k | std::ranges::remove_if(sub_predicate_access_paths, is_current_level_meta_access_path); |
2266 | 51.5k | sub_predicate_access_paths.erase(removed.begin(), removed.end()); |
2267 | | |
2268 | 51.5k | if (sub_predicate_access_paths.empty()) { |
2269 | | // Check for meta-only modes (OFFSET_ONLY or NULL_MAP_ONLY). Only skip the item |
2270 | | // iterator when no predicate sub-path needs it in the predicate phase. |
2271 | 51.1k | _check_and_set_meta_read_mode(requirement_before_access_path, sub_all_access_paths); |
2272 | 51.1k | if (read_offset_only()) { |
2273 | 1.34k | _item_iterator->set_read_requirement(ReadRequirement::SKIP); |
2274 | 1.34k | DLOG(INFO) << "Array column iterator set column " << _column_name |
2275 | 1.34k | << " to OFFSET_ONLY meta read mode, item column set to SKIP"; |
2276 | 1.34k | return Status::OK(); |
2277 | 1.34k | } |
2278 | 49.8k | if (read_null_map_only()) { |
2279 | 14 | _item_iterator->set_read_requirement(ReadRequirement::SKIP); |
2280 | 14 | DLOG(INFO) << "Array column iterator set column " << _column_name |
2281 | 14 | << " to NULL_MAP_ONLY meta read mode, item column set to SKIP"; |
2282 | 14 | return Status::OK(); |
2283 | 14 | } |
2284 | 49.8k | } |
2285 | | // OFFSET/NULL at the current array level is consumed by this iterator. After deciding that |
2286 | | // the array is not in a meta-only mode, do not forward those paths to the item iterator. |
2287 | 50.1k | remove_current_level_meta_access_paths(sub_all_access_paths); |
2288 | | |
2289 | 50.1k | const auto no_sub_column_to_skip = sub_all_access_paths.empty(); |
2290 | 50.1k | const auto no_predicate_sub_column = sub_predicate_access_paths.empty(); |
2291 | | |
2292 | 50.1k | if (!no_sub_column_to_skip) { |
2293 | 4.55k | for (auto& path : sub_all_access_paths) { |
2294 | 4.55k | if (path.data_access_path.path[0] == ACCESS_ALL) { |
2295 | 4.55k | path.data_access_path.path[0] = _item_iterator->column_name(); |
2296 | 4.55k | } |
2297 | 4.55k | } |
2298 | 4.49k | } |
2299 | | |
2300 | 50.1k | if (no_predicate_sub_column) { |
2301 | | // Current-level predicate meta paths (OFFSET/NULL) are consumed by the array itself and |
2302 | | // removed before forwarding paths to the item iterator. If they are the only predicate |
2303 | | // paths, the item iterator may still be needed later for lazy materialization, but it must |
2304 | | // not be promoted to PREDICATE. Only propagate the predicate requirement when the |
2305 | | // parent predicate really applies to the item/whole value instead of array metadata only. |
2306 | 49.8k | if (_read_requirement == ReadRequirement::PREDICATE && |
2307 | 49.8k | !has_current_level_predicate_meta_path) { |
2308 | 2.87k | _item_iterator->set_read_requirement(ReadRequirement::PREDICATE); |
2309 | 2.87k | } |
2310 | 49.8k | } else { |
2311 | 422 | for (auto& path : sub_predicate_access_paths) { |
2312 | 422 | if (path.data_access_path.path[0] == ACCESS_ALL) { |
2313 | 422 | path.data_access_path.path[0] = _item_iterator->column_name(); |
2314 | 422 | } |
2315 | 422 | } |
2316 | 376 | } |
2317 | | |
2318 | 50.1k | if (!no_sub_column_to_skip || !no_predicate_sub_column) { |
2319 | 4.51k | RETURN_IF_ERROR( |
2320 | 4.51k | _item_iterator->set_access_paths(sub_all_access_paths, sub_predicate_access_paths)); |
2321 | | // Predicate-only item paths stay PREDICATE because this update runs after |
2322 | | // child set_access_paths() and read requirements are monotonic. Non-predicate item paths are |
2323 | | // marked as lazy materialization targets. |
2324 | 4.50k | _item_iterator->set_read_requirement_self(ReadRequirement::LAZY_OUTPUT); |
2325 | 4.50k | } |
2326 | 50.1k | return Status::OK(); |
2327 | 50.1k | } |
2328 | | |
2329 | | //////////////////////////////////////////////////////////////////////////////// |
2330 | | // StringFileColumnIterator implementation |
2331 | | //////////////////////////////////////////////////////////////////////////////// |
2332 | | |
2333 | | StringFileColumnIterator::StringFileColumnIterator(std::shared_ptr<ColumnReader> reader) |
2334 | 17.3M | : FileColumnIterator(std::move(reader)) {} |
2335 | | |
2336 | 17.3M | Status StringFileColumnIterator::init(const ColumnIteratorOptions& opts) { |
2337 | 17.3M | if (read_offset_only()) { |
2338 | | // Propagate only_read_offsets to the FileColumnIterator's options |
2339 | 176 | auto modified_opts = opts; |
2340 | 176 | modified_opts.only_read_offsets = true; |
2341 | 176 | return FileColumnIterator::init(modified_opts); |
2342 | 176 | } |
2343 | 17.3M | return FileColumnIterator::init(opts); |
2344 | 17.3M | } |
2345 | | |
2346 | | Status StringFileColumnIterator::set_access_paths( |
2347 | | const TColumnAccessPaths& all_access_paths, |
2348 | 5.85k | const TColumnAccessPaths& predicate_access_paths) { |
2349 | 5.85k | if (all_access_paths.empty() && predicate_access_paths.empty()) { |
2350 | 4.10k | return Status::OK(); |
2351 | 4.10k | } |
2352 | | |
2353 | 1.74k | const auto requirement_before_access_path = _read_requirement; |
2354 | 1.74k | if (!predicate_access_paths.empty()) { |
2355 | 385 | set_read_requirement(ReadRequirement::PREDICATE); |
2356 | 385 | } |
2357 | | |
2358 | 1.74k | const bool has_current_level_data_path = |
2359 | 1.74k | std::ranges::any_of(all_access_paths, [this](const TColumnAccessPath& path) { |
2360 | 1.71k | return is_current_level_data_access_path(path, _column_name); |
2361 | 1.71k | }); |
2362 | | // Strip the column name from path[0] before checking for meta-only modes. |
2363 | | // Raw paths look like ["col_name", "OFFSET"] or ["col_name", "NULL"]. |
2364 | 1.74k | auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths)); |
2365 | 1.74k | if (has_current_level_data_path) { |
2366 | 1.27k | remove_current_level_meta_access_paths(sub_all_access_paths); |
2367 | 1.27k | } |
2368 | 1.74k | _check_and_set_meta_read_mode(requirement_before_access_path, sub_all_access_paths); |
2369 | | // OFFSET_ONLY mode is fundamentally incompatible with CHAR columns: |
2370 | | // CHAR is stored padded to its declared length (see |
2371 | | // OlapColumnDataConvertorChar::clone_and_padding), so the per-row length |
2372 | | // recorded in dict word info / page headers is always the padded length |
2373 | | // (e.g. 25 for CHAR(25)) — never the logical length expected by length(). |
2374 | | // Recovering the logical length requires scanning the chars buffer with |
2375 | | // strnlen() (shrink_padding_chars), which OFFSET_ONLY by definition skips. |
2376 | | // There is no partial-benefit path: any optimization that still produces |
2377 | | // the correct length() result must read the chars buffer in full. |
2378 | | // |
2379 | | // FE (NestedColumnPruning) already filters CHAR slots out of the |
2380 | | // OFFSET-only access plan, so reaching this branch means an FE/BE |
2381 | | // contract violation. Fail loudly instead of silently falling back. |
2382 | 1.74k | if (read_offset_only() && get_reader() != nullptr && |
2383 | 1.74k | get_reader()->get_meta_type() == FieldType::OLAP_FIELD_TYPE_CHAR) { |
2384 | 0 | return Status::InternalError( |
2385 | 0 | "OFFSET_ONLY access path is not supported on CHAR column '{}': CHAR is stored " |
2386 | 0 | "padded so the per-row length information available without reading the chars " |
2387 | 0 | "buffer is always the padded length, not the logical length. The FE planner " |
2388 | 0 | "must not emit an OFFSET access path for CHAR columns.", |
2389 | 0 | _column_name); |
2390 | 0 | } |
2391 | 1.74k | if (read_offset_only()) { |
2392 | 177 | DLOG(INFO) << "String column iterator set column " << _column_name |
2393 | 177 | << " to OFFSET_ONLY meta read mode"; |
2394 | 1.56k | } else if (read_null_map_only()) { |
2395 | 246 | DLOG(INFO) << "String column iterator set column " << _column_name |
2396 | 246 | << " to NULL_MAP_ONLY meta read mode"; |
2397 | 246 | } |
2398 | | |
2399 | 1.74k | return Status::OK(); |
2400 | 1.74k | } |
2401 | | |
2402 | | //////////////////////////////////////////////////////////////////////////////// |
2403 | | |
2404 | 28.8M | FileColumnIterator::FileColumnIterator(std::shared_ptr<ColumnReader> reader) : _reader(reader) {} |
2405 | | |
2406 | | void ColumnIterator::_check_and_set_meta_read_mode(ReadRequirement requirement_before_access_path, |
2407 | 63.3k | const TColumnAccessPaths& sub_all_access_paths) { |
2408 | 63.3k | _meta_read_mode = MetaReadMode::DEFAULT; |
2409 | 63.3k | if (requirement_before_access_path != ReadRequirement::NORMAL && |
2410 | 63.3k | requirement_before_access_path != ReadRequirement::SKIP) { |
2411 | | // A stronger requirement means a parent/full-data path already required this iterator |
2412 | | // to materialize data. In that case a later predicate NULL/OFFSET path is only |
2413 | | // an additional predicate requirement and must not downgrade the read to |
2414 | | // meta-only. |
2415 | 44 | return; |
2416 | 44 | } |
2417 | | |
2418 | 63.3k | bool has_offset_path = false; |
2419 | 63.3k | bool has_null_path = false; |
2420 | 63.3k | for (const auto& path : sub_all_access_paths) { |
2421 | 8.00k | if (!is_current_level_meta_access_path(path)) { |
2422 | 5.78k | _meta_read_mode = MetaReadMode::DEFAULT; |
2423 | 5.78k | return; |
2424 | 5.78k | } |
2425 | 2.22k | const auto& component = path.data_access_path.path[0]; |
2426 | 2.22k | if (StringCaseEqual()(component, ACCESS_OFFSET)) { |
2427 | 1.98k | has_offset_path = true; |
2428 | 1.98k | } else { |
2429 | 237 | has_null_path = true; |
2430 | 237 | } |
2431 | 2.22k | } |
2432 | 57.5k | if (has_offset_path) { |
2433 | | // OFFSET_ONLY skips actual child/string data, but nullable complex iterators still |
2434 | | // materialize the current-level null map. So OFFSET covers OFFSET+NULL metadata. |
2435 | 1.98k | _meta_read_mode = MetaReadMode::OFFSET_ONLY; |
2436 | 55.5k | } else if (has_null_path) { |
2437 | 287 | _meta_read_mode = MetaReadMode::NULL_MAP_ONLY; |
2438 | 55.2k | } else { |
2439 | 55.2k | _meta_read_mode = MetaReadMode::DEFAULT; |
2440 | 55.2k | } |
2441 | 57.5k | } |
2442 | | |
2443 | 28.7M | Status FileColumnIterator::init(const ColumnIteratorOptions& opts) { |
2444 | 28.7M | if (_read_requirement == ReadRequirement::SKIP) { |
2445 | 2.40k | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2446 | 2.40k | return Status::OK(); |
2447 | 2.40k | } |
2448 | | |
2449 | 28.7M | _opts = opts; |
2450 | 28.7M | if (!_opts.use_page_cache) { |
2451 | 28.7M | _reader->disable_index_meta_cache(); |
2452 | 28.7M | } |
2453 | 28.7M | RETURN_IF_ERROR(get_block_compression_codec(_reader->get_compression(), &_compress_codec)); |
2454 | 28.7M | if (config::enable_low_cardinality_optimize && |
2455 | 28.8M | opts.io_ctx.reader_type == ReaderType::READER_QUERY && |
2456 | 28.7M | _reader->encoding_info()->encoding() == DICT_ENCODING) { |
2457 | 17.1M | auto dict_encoding_type = _reader->get_dict_encoding_type(); |
2458 | | // Only if the column is a predicate column, then we need check the all dict encoding flag |
2459 | | // because we could rewrite the predciate to accelarate query speed. But if it is not a |
2460 | | // predicate column, then it is useless. And it has a bad impact on cold read(first time read) |
2461 | | // because it will load the column's ordinal index and zonemap index and maybe other indices. |
2462 | | // it has bad impact on primary key query. For example, select * from table where pk = 1, and |
2463 | | // the table has 2000 columns. |
2464 | 17.1M | if (dict_encoding_type == ColumnReader::UNKNOWN_DICT_ENCODING && opts.is_predicate_column) { |
2465 | 6.33k | RETURN_IF_ERROR(seek_to_ordinal(_reader->num_rows() - 1)); |
2466 | 6.33k | _is_all_dict_encoding = _page.is_dict_encoding; |
2467 | 6.33k | _reader->set_dict_encoding_type(_is_all_dict_encoding |
2468 | 6.33k | ? ColumnReader::ALL_DICT_ENCODING |
2469 | 6.33k | : ColumnReader::PARTIAL_DICT_ENCODING); |
2470 | 17.1M | } else { |
2471 | 17.1M | _is_all_dict_encoding = dict_encoding_type == ColumnReader::ALL_DICT_ENCODING; |
2472 | 17.1M | } |
2473 | 17.1M | } |
2474 | 28.7M | return Status::OK(); |
2475 | 28.7M | } |
2476 | | |
2477 | 28.9M | FileColumnIterator::~FileColumnIterator() = default; |
2478 | | |
2479 | 1.18M | void FileColumnIterator::_trigger_prefetch_if_eligible(ordinal_t ord) { |
2480 | 1.18M | std::vector<BlockRange> ranges; |
2481 | 1.18M | if (_prefetcher->need_prefetch(cast_set<uint32_t>(ord), &ranges)) { |
2482 | 905k | for (const auto& range : ranges) { |
2483 | 905k | _cached_remote_file_reader->prefetch_range(range.offset, range.size, &_opts.io_ctx); |
2484 | 905k | } |
2485 | 904k | } |
2486 | 1.18M | } |
2487 | | |
2488 | 3.36M | Status FileColumnIterator::seek_to_ordinal(ordinal_t ord) { |
2489 | 3.36M | if (!need_to_read()) { |
2490 | 3.33k | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2491 | 3.33k | return Status::OK(); |
2492 | 3.33k | } |
2493 | | |
2494 | 18.4E | LOG_IF(INFO, config::enable_segment_prefetch_verbose_log) << fmt::format( |
2495 | 18.4E | "[verbose] FileColumnIterator::seek_to_ordinal seek to ordinal {}, enable_prefetch={}", |
2496 | 18.4E | ord, _enable_prefetch); |
2497 | 3.36M | if (_enable_prefetch) { |
2498 | 1.18M | _trigger_prefetch_if_eligible(ord); |
2499 | 1.18M | } |
2500 | | |
2501 | | // if current page contains this row, we don't need to seek |
2502 | 3.36M | if (!_page || !_page.contains(ord) || !_page_iter.valid()) { |
2503 | 1.68M | RETURN_IF_ERROR(_reader->seek_at_or_before(ord, &_page_iter, _opts)); |
2504 | 1.68M | RETURN_IF_ERROR(_read_data_page(_page_iter)); |
2505 | 1.68M | } |
2506 | 3.36M | RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, ord - _page.first_ordinal)); |
2507 | 3.36M | _current_ordinal = ord; |
2508 | 3.36M | return Status::OK(); |
2509 | 3.36M | } |
2510 | | |
2511 | 0 | Status FileColumnIterator::seek_to_page_start() { |
2512 | 0 | return seek_to_ordinal(_page.first_ordinal); |
2513 | 0 | } |
2514 | | |
2515 | 3.41M | Status FileColumnIterator::_seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const { |
2516 | 3.41M | if (page->offset_in_page == offset_in_page) { |
2517 | | // fast path, do nothing |
2518 | 1.90M | return Status::OK(); |
2519 | 1.90M | } |
2520 | | |
2521 | 1.50M | ordinal_t pos_in_data = offset_in_page; |
2522 | 1.50M | if (_page.has_null) { |
2523 | 134k | ordinal_t offset_in_data = 0; |
2524 | 134k | ordinal_t skips = offset_in_page; |
2525 | | |
2526 | 134k | if (offset_in_page > page->offset_in_page) { |
2527 | | // forward, reuse null bitmap |
2528 | 92.5k | skips = offset_in_page - page->offset_in_page; |
2529 | 92.5k | offset_in_data = page->data_decoder->current_index(); |
2530 | 92.5k | } else { |
2531 | | // rewind null bitmap, and |
2532 | 41.6k | page->null_decoder = RleDecoder<bool>((const uint8_t*)page->null_bitmap.data, |
2533 | 41.6k | cast_set<int>(page->null_bitmap.size), 1); |
2534 | 41.6k | } |
2535 | | |
2536 | 134k | auto skip_nulls = page->null_decoder.Skip(skips); |
2537 | 134k | pos_in_data = offset_in_data + skips - skip_nulls; |
2538 | 134k | } |
2539 | | |
2540 | 1.50M | RETURN_IF_ERROR(page->data_decoder->seek_to_position_in_page(pos_in_data)); |
2541 | 1.50M | page->offset_in_page = offset_in_page; |
2542 | 1.50M | return Status::OK(); |
2543 | 1.50M | } |
2544 | | |
2545 | 22.1k | Status FileColumnIterator::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) { |
2546 | 22.1k | return _reader->next_batch_of_zone_map(n, dst); |
2547 | 22.1k | } |
2548 | | |
2549 | 2.50M | Status FileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2550 | 2.50M | if (!need_to_read()) { |
2551 | 3.33k | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2552 | 3.33k | _convert_to_place_holder_column(dst, *n); |
2553 | 3.33k | return Status::OK(); |
2554 | 3.33k | } |
2555 | | |
2556 | 2.49M | _recovery_from_place_holder_column(dst); |
2557 | | |
2558 | 2.49M | if (read_null_map_only()) { |
2559 | 3.00k | DLOG(INFO) << "File column iterator column " << _column_name |
2560 | 3.00k | << " in NULL_MAP_ONLY mode, reading only null map."; |
2561 | 3.00k | DORIS_CHECK(is_column_nullable(*dst)); |
2562 | 3.00k | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
2563 | 3.00k | auto& null_map_data = nullable_col.get_null_map_data(); |
2564 | | |
2565 | 3.00k | size_t remaining = *n; |
2566 | 3.00k | *has_null = false; |
2567 | 6.06k | while (remaining > 0) { |
2568 | 3.05k | if (!_page.has_remaining()) { |
2569 | 48 | bool eos = false; |
2570 | 48 | RETURN_IF_ERROR(_load_next_page(&eos)); |
2571 | 48 | if (eos) { |
2572 | 0 | break; |
2573 | 0 | } |
2574 | 48 | } |
2575 | | |
2576 | 3.05k | size_t nrows_in_page = std::min(remaining, _page.remaining()); |
2577 | 3.05k | size_t nrows_to_read = nrows_in_page; |
2578 | 3.05k | if (_page.has_null) { |
2579 | 211k | while (nrows_to_read > 0) { |
2580 | 209k | bool is_null = false; |
2581 | 209k | size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read); |
2582 | 209k | const size_t cur_size = null_map_data.size(); |
2583 | 209k | null_map_data.resize(cur_size + this_run); |
2584 | 209k | memset(null_map_data.data() + cur_size, is_null ? 1 : 0, this_run); |
2585 | 209k | if (is_null) { |
2586 | 106k | *has_null = true; |
2587 | 106k | } |
2588 | 209k | nrows_to_read -= this_run; |
2589 | 209k | _page.offset_in_page += this_run; |
2590 | 209k | _current_ordinal += this_run; |
2591 | 209k | } |
2592 | 2.96k | } else { |
2593 | 93 | const size_t cur_size = null_map_data.size(); |
2594 | 93 | null_map_data.resize(cur_size + nrows_to_read); |
2595 | 93 | memset(null_map_data.data() + cur_size, 0, nrows_to_read); |
2596 | 93 | _page.offset_in_page += nrows_to_read; |
2597 | 93 | _current_ordinal += nrows_to_read; |
2598 | 93 | } |
2599 | 3.05k | remaining -= nrows_in_page; |
2600 | 3.05k | } |
2601 | 3.00k | *n -= remaining; |
2602 | 3.00k | nullable_col.get_nested_column().insert_many_defaults(*n); |
2603 | 3.00k | return Status::OK(); |
2604 | 3.00k | } |
2605 | | |
2606 | 2.49M | size_t curr_size = dst->byte_size(); |
2607 | 2.49M | dst->reserve(*n); |
2608 | 2.49M | size_t remaining = *n; |
2609 | 2.49M | *has_null = false; |
2610 | 5.04M | while (remaining > 0) { |
2611 | 2.54M | if (!_page.has_remaining()) { |
2612 | 49.4k | bool eos = false; |
2613 | 49.4k | RETURN_IF_ERROR(_load_next_page(&eos)); |
2614 | 49.4k | if (eos) { |
2615 | 0 | break; |
2616 | 0 | } |
2617 | 49.4k | } |
2618 | | |
2619 | | // number of rows to be read from this page |
2620 | 2.54M | size_t nrows_in_page = std::min(remaining, _page.remaining()); |
2621 | 2.54M | size_t nrows_to_read = nrows_in_page; |
2622 | 2.54M | if (_page.has_null) { |
2623 | 992k | while (nrows_to_read > 0) { |
2624 | 750k | bool is_null = false; |
2625 | 750k | size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read); |
2626 | | // we use num_rows only for CHECK |
2627 | 750k | size_t num_rows = this_run; |
2628 | 750k | if (!is_null) { |
2629 | 382k | RETURN_IF_ERROR(_page.data_decoder->next_batch(&num_rows, dst)); |
2630 | 382k | DCHECK_EQ(this_run, num_rows); |
2631 | 382k | } else { |
2632 | 367k | *has_null = true; |
2633 | 367k | auto* null_col = check_and_get_column<ColumnNullable>(dst.get()); |
2634 | 368k | if (null_col != nullptr) { |
2635 | 368k | null_col->insert_many_defaults(this_run); |
2636 | 18.4E | } else { |
2637 | 18.4E | return Status::InternalError("unexpected column type in column reader"); |
2638 | 18.4E | } |
2639 | 367k | } |
2640 | | |
2641 | 751k | nrows_to_read -= this_run; |
2642 | 751k | _page.offset_in_page += this_run; |
2643 | 751k | _current_ordinal += this_run; |
2644 | 751k | } |
2645 | 2.30M | } else { |
2646 | 2.30M | RETURN_IF_ERROR(_page.data_decoder->next_batch(&nrows_to_read, dst)); |
2647 | 2.30M | DCHECK_EQ(nrows_to_read, nrows_in_page); |
2648 | | |
2649 | 2.30M | _page.offset_in_page += nrows_to_read; |
2650 | 2.30M | _current_ordinal += nrows_to_read; |
2651 | 2.30M | } |
2652 | 2.54M | remaining -= nrows_in_page; |
2653 | 2.54M | } |
2654 | 2.49M | *n -= remaining; |
2655 | 2.49M | _opts.stats->bytes_read += (dst->byte_size() - curr_size) + BitmapSize(*n); |
2656 | | |
2657 | | #ifdef BE_TEST |
2658 | | _reader->check_data_by_zone_map_for_test(dst); |
2659 | | #endif |
2660 | 2.49M | return Status::OK(); |
2661 | 2.49M | } |
2662 | | |
2663 | | Status FileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
2664 | 815k | MutableColumnPtr& dst) { |
2665 | 815k | if (!need_to_read()) { |
2666 | 0 | DLOG(INFO) << "File column iterator column " << _column_name << " skip reading."; |
2667 | 0 | _convert_to_place_holder_column(dst, count); |
2668 | 0 | return Status::OK(); |
2669 | 0 | } |
2670 | | |
2671 | 815k | _recovery_from_place_holder_column(dst); |
2672 | | |
2673 | 815k | if (read_null_map_only()) { |
2674 | 22 | DLOG(INFO) << "File column iterator column " << _column_name |
2675 | 22 | << " in NULL_MAP_ONLY mode, reading only null map by rowids."; |
2676 | | |
2677 | 22 | DORIS_CHECK(is_column_nullable(*dst)); |
2678 | 22 | auto& nullable_col = assert_cast<ColumnNullable&>(*dst); |
2679 | 22 | auto& null_map_data = nullable_col.get_null_map_data(); |
2680 | 22 | const size_t base_size = null_map_data.size(); |
2681 | 22 | null_map_data.resize(base_size + count); |
2682 | | |
2683 | 22 | size_t remaining = count; |
2684 | 22 | size_t total_read_count = 0; |
2685 | 22 | size_t nrows_to_read = 0; |
2686 | 45 | while (remaining > 0) { |
2687 | 23 | RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count])); |
2688 | | |
2689 | 23 | nrows_to_read = std::min(remaining, _page.remaining()); |
2690 | | |
2691 | 23 | if (_page.has_null) { |
2692 | 5 | size_t already_read = 0; |
2693 | 10 | while ((nrows_to_read - already_read) > 0) { |
2694 | 5 | bool is_null = false; |
2695 | 5 | size_t this_run = std::min(nrows_to_read - already_read, _page.remaining()); |
2696 | 5 | if (UNLIKELY(this_run == 0)) { |
2697 | 0 | break; |
2698 | 0 | } |
2699 | 5 | this_run = _page.null_decoder.GetNextRun(&is_null, this_run); |
2700 | | |
2701 | 5 | size_t offset = total_read_count + already_read; |
2702 | 5 | size_t this_read_count = 0; |
2703 | 5 | rowid_t current_ordinal_in_page = |
2704 | 5 | cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal); |
2705 | 10 | for (size_t i = 0; i < this_run; ++i) { |
2706 | 5 | if (rowids[offset + i] - current_ordinal_in_page >= this_run) { |
2707 | 0 | break; |
2708 | 0 | } |
2709 | 5 | this_read_count++; |
2710 | 5 | } |
2711 | | |
2712 | 5 | if (this_read_count > 0) { |
2713 | 5 | memset(null_map_data.data() + base_size + offset, is_null ? 1 : 0, |
2714 | 5 | this_read_count); |
2715 | 5 | } |
2716 | | |
2717 | 5 | already_read += this_read_count; |
2718 | 5 | _page.offset_in_page += this_run; |
2719 | 5 | } |
2720 | | |
2721 | 5 | nrows_to_read = already_read; |
2722 | 5 | total_read_count += nrows_to_read; |
2723 | 5 | remaining -= nrows_to_read; |
2724 | 18 | } else { |
2725 | 18 | rowid_t current_ordinal_in_page = |
2726 | 18 | cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal); |
2727 | 18 | size_t rows_in_current_page = 0; |
2728 | 36 | for (size_t i = 0; i < nrows_to_read; ++i) { |
2729 | 19 | if (rowids[total_read_count + i] - current_ordinal_in_page >= nrows_to_read) { |
2730 | 1 | break; |
2731 | 1 | } |
2732 | 18 | ++rows_in_current_page; |
2733 | 18 | } |
2734 | 18 | DCHECK_GT(rows_in_current_page, 0); |
2735 | 18 | memset(null_map_data.data() + base_size + total_read_count, 0, |
2736 | 18 | rows_in_current_page); |
2737 | 18 | _page.offset_in_page += rows_in_current_page; |
2738 | 18 | total_read_count += rows_in_current_page; |
2739 | 18 | remaining -= rows_in_current_page; |
2740 | 18 | } |
2741 | 23 | } |
2742 | | |
2743 | 22 | null_map_data.resize(base_size + total_read_count); |
2744 | 22 | nullable_col.get_nested_column().insert_many_defaults(total_read_count); |
2745 | 22 | return Status::OK(); |
2746 | 22 | } |
2747 | | |
2748 | 815k | size_t remaining = count; |
2749 | 815k | size_t total_read_count = 0; |
2750 | 815k | size_t nrows_to_read = 0; |
2751 | 1.66M | while (remaining > 0) { |
2752 | 845k | RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count])); |
2753 | | |
2754 | | // number of rows to be read from this page |
2755 | 845k | nrows_to_read = std::min(remaining, _page.remaining()); |
2756 | | |
2757 | 845k | if (_page.has_null) { |
2758 | 88.3k | size_t already_read = 0; |
2759 | 4.33M | while ((nrows_to_read - already_read) > 0) { |
2760 | 4.24M | bool is_null = false; |
2761 | 4.24M | size_t this_run = std::min(nrows_to_read - already_read, _page.remaining()); |
2762 | 4.24M | if (UNLIKELY(this_run == 0)) { |
2763 | 175 | break; |
2764 | 175 | } |
2765 | 4.24M | this_run = _page.null_decoder.GetNextRun(&is_null, this_run); |
2766 | 4.24M | size_t offset = total_read_count + already_read; |
2767 | 4.24M | size_t this_read_count = 0; |
2768 | 4.24M | rowid_t current_ordinal_in_page = |
2769 | 4.24M | cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal); |
2770 | 20.9M | for (size_t i = 0; i < this_run; ++i) { |
2771 | 19.6M | if (rowids[offset + i] - current_ordinal_in_page >= this_run) { |
2772 | 2.95M | break; |
2773 | 2.95M | } |
2774 | 16.7M | this_read_count++; |
2775 | 16.7M | } |
2776 | | |
2777 | 4.24M | auto origin_index = _page.data_decoder->current_index(); |
2778 | 4.24M | if (this_read_count > 0) { |
2779 | 1.29M | if (is_null) { |
2780 | 681k | auto* null_col = check_and_get_column<ColumnNullable>(dst.get()); |
2781 | 681k | if (UNLIKELY(null_col == nullptr)) { |
2782 | 0 | return Status::InternalError("unexpected column type in column reader"); |
2783 | 0 | } |
2784 | | |
2785 | 681k | null_col->insert_many_defaults(this_read_count); |
2786 | 681k | } else { |
2787 | 616k | size_t read_count = this_read_count; |
2788 | | |
2789 | | // ordinal in nullable columns' data buffer maybe be not continuously(the data doesn't contain null value), |
2790 | | // so we need use `page_start_off_in_decoder` to calculate the actual offset in `data_decoder` |
2791 | 616k | size_t page_start_off_in_decoder = |
2792 | 616k | _page.first_ordinal + _page.offset_in_page - origin_index; |
2793 | 616k | RETURN_IF_ERROR(_page.data_decoder->read_by_rowids( |
2794 | 616k | &rowids[offset], page_start_off_in_decoder, &read_count, dst)); |
2795 | 616k | DCHECK_EQ(read_count, this_read_count); |
2796 | 616k | } |
2797 | 1.29M | } |
2798 | | |
2799 | 4.24M | if (!is_null) { |
2800 | 3.54M | RETURN_IF_ERROR( |
2801 | 3.54M | _page.data_decoder->seek_to_position_in_page(origin_index + this_run)); |
2802 | 3.54M | } |
2803 | | |
2804 | 4.24M | already_read += this_read_count; |
2805 | 4.24M | _page.offset_in_page += this_run; |
2806 | 4.24M | DCHECK(_page.offset_in_page <= _page.num_rows); |
2807 | 4.24M | } |
2808 | | |
2809 | 88.3k | nrows_to_read = already_read; |
2810 | 88.3k | total_read_count += nrows_to_read; |
2811 | 88.3k | remaining -= nrows_to_read; |
2812 | 757k | } else { |
2813 | 757k | RETURN_IF_ERROR(_page.data_decoder->read_by_rowids( |
2814 | 757k | &rowids[total_read_count], _page.first_ordinal, &nrows_to_read, dst)); |
2815 | 757k | total_read_count += nrows_to_read; |
2816 | 757k | remaining -= nrows_to_read; |
2817 | 757k | } |
2818 | 845k | } |
2819 | 815k | return Status::OK(); |
2820 | 815k | } |
2821 | | |
2822 | 49.5k | Status FileColumnIterator::_load_next_page(bool* eos) { |
2823 | 49.5k | _page_iter.next(); |
2824 | 49.5k | if (!_page_iter.valid()) { |
2825 | 0 | *eos = true; |
2826 | 0 | return Status::OK(); |
2827 | 0 | } |
2828 | | |
2829 | 49.5k | RETURN_IF_ERROR(_read_data_page(_page_iter)); |
2830 | 49.5k | RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, 0)); |
2831 | 49.5k | *eos = false; |
2832 | 49.5k | return Status::OK(); |
2833 | 49.5k | } |
2834 | | |
2835 | 1.73M | Status FileColumnIterator::_read_data_page(const OrdinalPageIndexIterator& iter) { |
2836 | 1.73M | PageHandle handle; |
2837 | 1.73M | Slice page_body; |
2838 | 1.73M | PageFooterPB footer; |
2839 | 1.73M | _opts.type = DATA_PAGE; |
2840 | 1.73M | PageDecoderOptions decoder_opts; |
2841 | 1.73M | decoder_opts.only_read_offsets = _opts.only_read_offsets; |
2842 | 1.73M | RETURN_IF_ERROR( |
2843 | 1.73M | _reader->read_page(_opts, iter.page(), &handle, &page_body, &footer, _compress_codec)); |
2844 | | // parse data page |
2845 | 1.73M | auto st = ParsedPage::create(std::move(handle), page_body, footer.data_page_footer(), |
2846 | 1.73M | _reader->encoding_info(), iter.page(), iter.page_index(), &_page, |
2847 | 1.73M | decoder_opts); |
2848 | 1.73M | if (!st.ok()) { |
2849 | 0 | LOG(WARNING) << "failed to create ParsedPage, file=" << _opts.file_reader->path().native() |
2850 | 0 | << ", page_offset=" << iter.page().offset << ", page_size=" << iter.page().size |
2851 | 0 | << ", page_index=" << iter.page_index() << ", error=" << st; |
2852 | 0 | return st; |
2853 | 0 | } |
2854 | | |
2855 | | // dictionary page is read when the first data page that uses it is read, |
2856 | | // this is to optimize the memory usage: when there is no query on one column, we could |
2857 | | // release the memory of dictionary page. |
2858 | | // note that concurrent iterators for the same column won't repeatedly read dictionary page |
2859 | | // because of page cache. |
2860 | 1.73M | if (_reader->encoding_info()->encoding() == DICT_ENCODING) { |
2861 | 454k | auto dict_page_decoder = reinterpret_cast<BinaryDictPageDecoder*>(_page.data_decoder.get()); |
2862 | 454k | if (dict_page_decoder->is_dict_encoding()) { |
2863 | 438k | if (_dict_decoder == nullptr) { |
2864 | 430k | RETURN_IF_ERROR(_read_dict_data()); |
2865 | 430k | CHECK_NOTNULL(_dict_decoder); |
2866 | 430k | } |
2867 | | |
2868 | 438k | dict_page_decoder->set_dict_decoder(cast_set<uint32_t>(_dict_decoder->count()), |
2869 | 438k | _dict_word_info.get()); |
2870 | 438k | } |
2871 | 454k | } |
2872 | 1.73M | return Status::OK(); |
2873 | 1.73M | } |
2874 | | |
2875 | 435k | Status FileColumnIterator::_read_dict_data() { |
2876 | 435k | CHECK_EQ(_reader->encoding_info()->encoding(), DICT_ENCODING); |
2877 | | // read dictionary page |
2878 | 435k | Slice dict_data; |
2879 | 435k | PageFooterPB dict_footer; |
2880 | 435k | _opts.type = INDEX_PAGE; |
2881 | | |
2882 | 435k | RETURN_IF_ERROR(_reader->read_page(_opts, _reader->get_dict_page_pointer(), &_dict_page_handle, |
2883 | 435k | &dict_data, &dict_footer, _compress_codec)); |
2884 | 435k | const EncodingInfo* encoding_info; |
2885 | | // The dict pool stores strings of the outer column's type. Using the |
2886 | | // outer type (CHAR vs VARCHAR/STRING) lets the EncodingInfo pick a |
2887 | | // CHAR-strip pre-decoder so the cached dict page is already unpadded. |
2888 | 435k | RETURN_IF_ERROR(EncodingInfo::get(_reader->get_meta_type(), |
2889 | 435k | dict_footer.dict_page_footer().encoding(), &encoding_info)); |
2890 | 435k | RETURN_IF_ERROR(encoding_info->create_page_decoder(dict_data, {}, _dict_decoder)); |
2891 | 435k | RETURN_IF_ERROR(_dict_decoder->init()); |
2892 | | |
2893 | 435k | _dict_word_info.reset(new StringRef[_dict_decoder->count()]); |
2894 | 435k | RETURN_IF_ERROR(_dict_decoder->get_dict_word_info(_dict_word_info.get())); |
2895 | 435k | return Status::OK(); |
2896 | 435k | } |
2897 | | |
2898 | | Status FileColumnIterator::get_row_ranges_by_zone_map( |
2899 | | const AndBlockColumnPredicate* col_predicates, |
2900 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
2901 | 129k | RowRanges* row_ranges) { |
2902 | 129k | if (_reader->has_zone_map()) { |
2903 | 129k | RETURN_IF_ERROR(_reader->get_row_ranges_by_zone_map(col_predicates, delete_predicates, |
2904 | 129k | row_ranges, _opts)); |
2905 | 129k | } |
2906 | 129k | return Status::OK(); |
2907 | 129k | } |
2908 | | |
2909 | | Status FileColumnIterator::get_row_ranges_by_bloom_filter( |
2910 | 130k | const AndBlockColumnPredicate* col_predicates, RowRanges* row_ranges) { |
2911 | 130k | if ((col_predicates->can_do_bloom_filter(false) && _reader->has_bloom_filter_index(false)) || |
2912 | 130k | (col_predicates->can_do_bloom_filter(true) && _reader->has_bloom_filter_index(true))) { |
2913 | 116 | RETURN_IF_ERROR(_reader->get_row_ranges_by_bloom_filter(col_predicates, row_ranges, _opts)); |
2914 | 116 | } |
2915 | 130k | return Status::OK(); |
2916 | 130k | } |
2917 | | |
2918 | | Status FileColumnIterator::get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
2919 | 132k | RowRanges* row_ranges) { |
2920 | 132k | if (!_is_all_dict_encoding) { |
2921 | 121k | return Status::OK(); |
2922 | 121k | } |
2923 | | |
2924 | 10.4k | if (!_dict_decoder) { |
2925 | 4.92k | RETURN_IF_ERROR(_read_dict_data()); |
2926 | 4.92k | CHECK_NOTNULL(_dict_decoder); |
2927 | 4.92k | } |
2928 | | |
2929 | 10.4k | if (!col_predicates->evaluate_and(_dict_word_info.get(), _dict_decoder->count())) { |
2930 | 1.25k | row_ranges->clear(); |
2931 | 1.25k | } |
2932 | 10.4k | return Status::OK(); |
2933 | 10.4k | } |
2934 | | |
2935 | 1.01M | Status FileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) { |
2936 | 1.01M | if (_cached_remote_file_reader = |
2937 | 1.01M | std::dynamic_pointer_cast<io::CachedRemoteFileReader>(_reader->_file_reader); |
2938 | 1.01M | !_cached_remote_file_reader) { |
2939 | 0 | return Status::OK(); |
2940 | 0 | } |
2941 | 1.01M | _enable_prefetch = true; |
2942 | 1.01M | _prefetcher = std::make_unique<SegmentPrefetcher>(params.config); |
2943 | 1.01M | RETURN_IF_ERROR(_prefetcher->init(_reader, params.read_options)); |
2944 | 1.01M | return Status::OK(); |
2945 | 1.01M | } |
2946 | | |
2947 | | void FileColumnIterator::collect_prefetchers( |
2948 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
2949 | 1.00M | PrefetcherInitMethod init_method) { |
2950 | 1.00M | if (_prefetcher) { |
2951 | 1.00M | prefetchers[init_method].emplace_back(_prefetcher.get()); |
2952 | 1.00M | } |
2953 | 1.00M | } |
2954 | | |
2955 | 25.8k | Status DefaultValueColumnIterator::init(const ColumnIteratorOptions& opts) { |
2956 | 25.8k | _opts = opts; |
2957 | | // be consistent with segment v1 |
2958 | | // if _has_default_value, we should create default column iterator for this column, and |
2959 | | // "NULL" is a special default value which means the default value is null. |
2960 | 25.8k | if (_has_default_value) { |
2961 | 1.50k | if (_default_value == "NULL") { |
2962 | 699 | _default_value_field = Field::create_field<TYPE_NULL>(Null {}); |
2963 | 802 | } else { |
2964 | 802 | if (_type == FieldType::OLAP_FIELD_TYPE_ARRAY) { |
2965 | 8 | if (_default_value != "[]") { |
2966 | 0 | return Status::NotSupported("Array default {} is unsupported", _default_value); |
2967 | 8 | } else { |
2968 | 8 | _default_value_field = Field::create_field<TYPE_ARRAY>(Array {}); |
2969 | 8 | return Status::OK(); |
2970 | 8 | } |
2971 | 794 | } else if (_type == FieldType::OLAP_FIELD_TYPE_STRUCT) { |
2972 | 0 | return Status::NotSupported("STRUCT default type is unsupported"); |
2973 | 794 | } else if (_type == FieldType::OLAP_FIELD_TYPE_MAP) { |
2974 | 0 | return Status::NotSupported("MAP default type is unsupported"); |
2975 | 0 | } |
2976 | 794 | const auto t = _type; |
2977 | 794 | const auto serde = DataTypeFactory::instance() |
2978 | 794 | .create_data_type(t, _precision, _scale, _len) |
2979 | 794 | ->get_serde(); |
2980 | 794 | RETURN_IF_ERROR(serde->from_fe_string(_default_value, _default_value_field)); |
2981 | 794 | } |
2982 | 24.3k | } else if (_is_nullable) { |
2983 | 24.3k | _default_value_field = Field::create_field<TYPE_NULL>(Null {}); |
2984 | 18.4E | } else { |
2985 | 18.4E | return Status::InternalError( |
2986 | 18.4E | "invalid default value column for no default value and not nullable"); |
2987 | 18.4E | } |
2988 | 25.8k | return Status::OK(); |
2989 | 25.8k | } |
2990 | | |
2991 | 3.20k | Status DefaultValueColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
2992 | 3.20k | *has_null = _default_value_field.is_null(); |
2993 | 3.20k | _insert_many_default(dst, *n); |
2994 | 3.20k | return Status::OK(); |
2995 | 3.20k | } |
2996 | | |
2997 | | Status DefaultValueColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count, |
2998 | 7.30k | MutableColumnPtr& dst) { |
2999 | 7.30k | _insert_many_default(dst, count); |
3000 | 7.30k | return Status::OK(); |
3001 | 7.30k | } |
3002 | | |
3003 | 10.5k | void DefaultValueColumnIterator::_insert_many_default(MutableColumnPtr& dst, size_t n) { |
3004 | 10.5k | if (_default_value_field.is_null()) { |
3005 | 10.0k | dst->insert_many_defaults(n); |
3006 | 10.0k | } else { |
3007 | 440 | dst = dst->convert_to_predicate_column_if_dictionary(); |
3008 | 440 | dst->insert_duplicate_fields(_default_value_field, n); |
3009 | 440 | } |
3010 | 10.5k | } |
3011 | | |
3012 | 4.83k | Status RowIdColumnIteratorV2::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
3013 | 4.83k | auto* string_column = assert_cast<ColumnString*, TypeCheckOnRelease::DISABLE>(dst.get()); |
3014 | | |
3015 | 12.1M | for (uint32_t i = 0; i < *n; ++i) { |
3016 | 12.1M | uint32_t row_id = _current_rowid + i; |
3017 | 12.1M | GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id); |
3018 | 12.1M | string_column->insert_data(reinterpret_cast<const char*>(&location), |
3019 | 12.1M | sizeof(GlobalRowLoacationV2)); |
3020 | 12.1M | } |
3021 | 4.83k | _current_rowid += *n; |
3022 | 4.83k | return Status::OK(); |
3023 | 4.83k | } |
3024 | | |
3025 | | Status RowIdColumnIteratorV2::read_by_rowids(const rowid_t* rowids, const size_t count, |
3026 | 36.5k | MutableColumnPtr& dst) { |
3027 | 36.5k | auto* string_column = assert_cast<ColumnString*>(dst.get()); |
3028 | | |
3029 | 28.3M | for (size_t i = 0; i < count; ++i) { |
3030 | 28.2M | uint32_t row_id = rowids[i]; |
3031 | 28.2M | GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id); |
3032 | 28.2M | string_column->insert_data(reinterpret_cast<const char*>(&location), |
3033 | 28.2M | sizeof(GlobalRowLoacationV2)); |
3034 | 28.2M | } |
3035 | 36.5k | return Status::OK(); |
3036 | 36.5k | } |
3037 | | |
3038 | | } // namespace doris::segment_v2 |