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