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/segment.h" |
19 | | |
20 | | #include <crc32c/crc32c.h> |
21 | | #include <gen_cpp/Descriptors_types.h> |
22 | | #include <gen_cpp/PlanNodes_types.h> |
23 | | #include <gen_cpp/olap_file.pb.h> |
24 | | #include <gen_cpp/segment_v2.pb.h> |
25 | | |
26 | | #include <algorithm> |
27 | | #include <cstring> |
28 | | #include <memory> |
29 | | #include <set> |
30 | | #include <sstream> |
31 | | #include <utility> |
32 | | |
33 | | #include "cloud/config.h" |
34 | | #include "common/config.h" |
35 | | #include "common/exception.h" |
36 | | #include "common/logging.h" |
37 | | #include "common/status.h" |
38 | | #include "core/column/column.h" |
39 | | #include "core/data_type/data_type.h" |
40 | | #include "core/data_type/data_type_factory.hpp" |
41 | | #include "core/data_type/data_type_nullable.h" |
42 | | #include "core/data_type/data_type_variant.h" |
43 | | #include "core/field.h" |
44 | | #include "core/string_ref.h" |
45 | | #include "cpp/sync_point.h" |
46 | | #include "exprs/expr_zonemap_filter.h" |
47 | | #include "exprs/vexpr_context.h" |
48 | | #include "io/cache/block_file_cache.h" |
49 | | #include "io/cache/block_file_cache_factory.h" |
50 | | #include "io/cache/cached_remote_file_reader.h" |
51 | | #include "io/fs/file_reader.h" |
52 | | #include "io/fs/file_system.h" |
53 | | #include "io/io_common.h" |
54 | | #include "runtime/exec_env.h" |
55 | | #include "runtime/query_context.h" |
56 | | #include "runtime/runtime_predicate.h" |
57 | | #include "runtime/runtime_state.h" |
58 | | #include "storage/index/index_file_reader.h" |
59 | | #include "storage/index/indexed_column_reader.h" |
60 | | #include "storage/index/primary_key_index.h" |
61 | | #include "storage/index/short_key_index.h" |
62 | | #include "storage/index/zone_map/zonemap_eval_context.h" |
63 | | #include "storage/iterator/vgeneric_iterators.h" |
64 | | #include "storage/iterators.h" |
65 | | #include "storage/key_coder.h" |
66 | | #include "storage/olap_common.h" |
67 | | #include "storage/predicate/block_column_predicate.h" |
68 | | #include "storage/predicate/column_predicate.h" |
69 | | #include "storage/rowset/rowset_reader_context.h" |
70 | | #include "storage/schema.h" |
71 | | #include "storage/segment/column_meta_accessor.h" |
72 | | #include "storage/segment/column_reader.h" |
73 | | #include "storage/segment/column_reader_cache.h" |
74 | | #include "storage/segment/empty_segment_iterator.h" |
75 | | #include "storage/segment/page_io.h" |
76 | | #include "storage/segment/page_pointer.h" |
77 | | #include "storage/segment/segment_iterator.h" |
78 | | #include "storage/segment/segment_writer.h" // k_segment_magic_length |
79 | | #include "storage/segment/stream_reader.h" |
80 | | #include "storage/segment/variant/variant_column_reader.h" |
81 | | #include "storage/tablet/tablet_schema.h" |
82 | | #include "storage/types.h" |
83 | | #include "storage/utils.h" |
84 | | #include "util/coding.h" |
85 | | #include "util/json/path_in_data.h" |
86 | | #include "util/slice.h" // Slice |
87 | | |
88 | | namespace doris::segment_v2 { |
89 | | |
90 | | class InvertedIndexIterator; |
91 | | |
92 | | namespace { |
93 | | |
94 | | Status build_segment_zonemap_context(Segment* segment, const Schema& schema, |
95 | | const StorageReadOptions& read_options, |
96 | 17.7k | const VExprContextSPtrs& conjuncts, ZoneMapEvalContext* ctx) { |
97 | 17.7k | DORIS_CHECK(segment != nullptr); |
98 | 17.7k | DORIS_CHECK(ctx != nullptr); |
99 | 17.7k | std::set<int> slot_indexes; |
100 | 20.5k | for (const auto& conjunct : conjuncts) { |
101 | 20.5k | DORIS_CHECK(conjunct != nullptr); |
102 | 20.5k | const auto& root = conjunct->root(); |
103 | 20.5k | DORIS_CHECK(root != nullptr); |
104 | 20.5k | if (!root->can_evaluate_zonemap_filter()) { |
105 | 18.7k | continue; |
106 | 18.7k | } |
107 | | // Segment zone maps have one min/max/null summary per column for the whole segment, so a |
108 | | // segment-level context can safely hold every slot referenced by a compound expression. |
109 | | // Page zone maps are page-aligned per column and still use single-slot filtering in |
110 | | // SegmentIterator. |
111 | 1.86k | root->collect_slot_column_ids(slot_indexes); |
112 | 1.86k | } |
113 | 17.7k | for (const int slot_index : slot_indexes) { |
114 | 3.13k | if (slot_index < 0 || cast_set<size_t>(slot_index) >= schema.num_column_ids()) { |
115 | 0 | continue; |
116 | 0 | } |
117 | 3.13k | const auto column_id = schema.column_id(cast_set<size_t>(slot_index)); |
118 | 3.13k | const auto* tablet_column = schema.column(column_id); |
119 | 3.13k | DORIS_CHECK(tablet_column != nullptr); |
120 | 3.13k | if (!segment->can_apply_predicate_safely( |
121 | 3.13k | column_id, schema, read_options.target_cast_type_for_variants, read_options)) { |
122 | 9 | continue; |
123 | 9 | } |
124 | 3.12k | auto data_type = segment->get_data_type_of(*tablet_column, read_options); |
125 | 3.12k | if (data_type == nullptr) { |
126 | 0 | continue; |
127 | 0 | } |
128 | 3.12k | ZoneMapEvalContext::SlotZoneMap slot_zone_map; |
129 | 3.12k | slot_zone_map.data_type = data_type; |
130 | 3.12k | std::shared_ptr<ColumnReader> reader; |
131 | 3.12k | Status st = segment->get_column_reader(*tablet_column, &reader, read_options.stats); |
132 | 3.12k | if (st.is<ErrorCode::NOT_FOUND>()) { |
133 | 26 | ctx->slots.emplace(slot_index, std::move(slot_zone_map)); |
134 | 26 | continue; |
135 | 26 | } |
136 | 3.10k | RETURN_IF_ERROR(st); |
137 | 3.10k | if (reader != nullptr && reader->has_zone_map()) { |
138 | 2.82k | ZoneMap zone_map; |
139 | 2.82k | RETURN_IF_ERROR(reader->get_segment_zone_map(&zone_map)); |
140 | 2.82k | slot_zone_map.zone_map = std::make_shared<ZoneMap>(std::move(zone_map)); |
141 | 2.82k | } |
142 | 3.10k | ctx->slots.emplace(slot_index, std::move(slot_zone_map)); |
143 | 3.10k | } |
144 | 17.7k | return Status::OK(); |
145 | 17.7k | } |
146 | | |
147 | | } // namespace |
148 | | |
149 | | Status Segment::open(io::FileSystemSPtr fs, const std::string& path, int64_t tablet_id, |
150 | | uint32_t segment_id, RowsetId rowset_id, TabletSchemaSPtr tablet_schema, |
151 | | const io::FileReaderOptions& reader_options, std::shared_ptr<Segment>* output, |
152 | 170k | InvertedIndexFileInfo idx_file_info, OlapReaderStatistics* stats) { |
153 | | // Ensure tablet_id is available in reader_options for CachedRemoteFileReader peer read. |
154 | 170k | io::FileReaderOptions opts_with_tablet = reader_options; |
155 | 170k | opts_with_tablet.tablet_id = tablet_id; |
156 | | |
157 | 170k | auto s = _open(fs, path, segment_id, rowset_id, tablet_schema, opts_with_tablet, output, |
158 | 170k | idx_file_info, stats); |
159 | 172k | if (s.ok() && output && *output) { |
160 | 172k | (*output)->_tablet_id = tablet_id; |
161 | 172k | } |
162 | 170k | if (!s.ok()) { |
163 | 5 | if (!config::is_cloud_mode()) { |
164 | 5 | auto res = ExecEnv::get_tablet(tablet_id); |
165 | 5 | TabletSharedPtr tablet = |
166 | 5 | res.has_value() ? std::dynamic_pointer_cast<Tablet>(res.value()) : nullptr; |
167 | 5 | if (tablet) { |
168 | 0 | tablet->report_error(s); |
169 | 0 | } |
170 | 5 | } |
171 | 5 | } |
172 | | |
173 | 170k | return s; |
174 | 170k | } |
175 | | |
176 | | Status Segment::_open(io::FileSystemSPtr fs, const std::string& path, uint32_t segment_id, |
177 | | RowsetId rowset_id, TabletSchemaSPtr tablet_schema, |
178 | | const io::FileReaderOptions& reader_options, std::shared_ptr<Segment>* output, |
179 | 170k | InvertedIndexFileInfo idx_file_info, OlapReaderStatistics* stats) { |
180 | 170k | io::FileReaderSPtr file_reader; |
181 | 170k | auto st = fs->open_file(path, &file_reader, &reader_options); |
182 | 170k | TEST_INJECTION_POINT_CALLBACK("Segment::open:corruption", &st); |
183 | 170k | std::shared_ptr<Segment> segment( |
184 | 170k | new Segment(segment_id, rowset_id, std::move(tablet_schema), idx_file_info)); |
185 | 170k | segment->_seg_path = path; |
186 | 172k | if (st) { |
187 | 172k | segment->_fs = fs; |
188 | 172k | segment->_file_reader = std::move(file_reader); |
189 | 172k | st = segment->_open(stats); |
190 | 172k | } |
191 | | |
192 | | // Three-tier retry for CORRUPTION errors when file cache is enabled. |
193 | | // This handles CORRUPTION from both open_file() and _parse_footer() (via _open()). |
194 | 170k | if (st.is<ErrorCode::CORRUPTION>() && |
195 | 170k | reader_options.cache_type == io::FileCachePolicy::FILE_BLOCK_CACHE) { |
196 | | // Tier 1: Clear file cache and retry with cache support (re-downloads from remote). |
197 | 2 | LOG(WARNING) << "bad segment file may be read from file cache, try to read remote source " |
198 | 2 | "file directly, file path: " |
199 | 2 | << path << " cache_key: " << file_cache_key_str(path); |
200 | 2 | auto file_key = file_cache_key_from_path(path); |
201 | 2 | auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
202 | 2 | file_cache->remove_if_cached(file_key); |
203 | | |
204 | 2 | st = fs->open_file(path, &file_reader, &reader_options); |
205 | 2 | if (st) { |
206 | 2 | segment->_fs = fs; |
207 | 2 | segment->_file_reader = std::move(file_reader); |
208 | 2 | st = segment->_open(stats); |
209 | 2 | } |
210 | 2 | TEST_INJECTION_POINT_CALLBACK("Segment::open:corruption1", &st); |
211 | 2 | if (st.is<ErrorCode::CORRUPTION>()) { // corrupt again |
212 | | // Tier 2: Bypass cache entirely and read directly from remote storage. |
213 | 0 | LOG(WARNING) << "failed to try to read remote source file again with cache support," |
214 | 0 | << " try to read from remote directly, " |
215 | 0 | << " file path: " << path << " cache_key: " << file_cache_key_str(path); |
216 | 0 | file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
217 | 0 | file_cache->remove_if_cached(file_key); |
218 | |
|
219 | 0 | io::FileReaderOptions opt = reader_options; |
220 | 0 | opt.cache_type = io::FileCachePolicy::NO_CACHE; // skip cache |
221 | 0 | RETURN_IF_ERROR(fs->open_file(path, &file_reader, &opt)); |
222 | 0 | segment->_fs = fs; |
223 | 0 | segment->_file_reader = std::move(file_reader); |
224 | 0 | st = segment->_open(stats); |
225 | 0 | if (!st.ok()) { |
226 | | // Tier 3: Remote source itself is corrupt. |
227 | 0 | LOG(WARNING) << "failed to try to read remote source file directly," |
228 | 0 | << " file path: " << path |
229 | 0 | << " cache_key: " << file_cache_key_str(path); |
230 | 0 | } |
231 | 0 | } |
232 | 2 | } |
233 | 170k | RETURN_IF_ERROR(st); |
234 | 18.4E | DCHECK(segment->_fs != nullptr) << "file system is nullptr after segment open"; |
235 | 170k | *output = std::move(segment); |
236 | 170k | return Status::OK(); |
237 | 170k | } |
238 | | |
239 | | Segment::Segment(uint32_t segment_id, RowsetId rowset_id, TabletSchemaSPtr tablet_schema, |
240 | | InvertedIndexFileInfo idx_file_info) |
241 | 172k | : _segment_id(segment_id), |
242 | 172k | _meta_mem_usage(0), |
243 | 172k | _rowset_id(rowset_id), |
244 | 172k | _tablet_schema(std::move(tablet_schema)), |
245 | 172k | _idx_file_info(std::move(idx_file_info)) {} |
246 | | |
247 | 137k | Segment::~Segment() { |
248 | 137k | g_segment_estimate_mem_bytes << -_tracked_meta_mem_usage; |
249 | | // if failed, fix `_tracked_meta_mem_usage` accuracy |
250 | 137k | DCHECK(_tracked_meta_mem_usage == meta_mem_usage()); |
251 | 137k | } |
252 | | |
253 | 51.1k | io::UInt128Wrapper Segment::file_cache_key(std::string_view rowset_id, uint32_t seg_id) { |
254 | 51.1k | return io::BlockFileCache::hash(fmt::format("{}_{}.dat", rowset_id, seg_id)); |
255 | 51.1k | } |
256 | | |
257 | 170k | int64_t Segment::get_metadata_size() const { |
258 | 170k | std::shared_ptr<SegmentFooterPB> footer_pb_shared = _footer_pb.lock(); |
259 | 170k | return sizeof(Segment) + (_pk_index_meta ? _pk_index_meta->ByteSizeLong() : 0) + |
260 | 18.4E | (footer_pb_shared ? footer_pb_shared->ByteSizeLong() : 0); |
261 | 170k | } |
262 | | |
263 | 170k | void Segment::update_metadata_size() { |
264 | 170k | MetadataAdder::update_metadata_size(); |
265 | 170k | g_segment_estimate_mem_bytes << _meta_mem_usage - _tracked_meta_mem_usage; |
266 | 170k | _tracked_meta_mem_usage = _meta_mem_usage; |
267 | 170k | } |
268 | | |
269 | 172k | Status Segment::_open(OlapReaderStatistics* stats) { |
270 | 172k | std::shared_ptr<SegmentFooterPB> footer_pb_shared; |
271 | 172k | RETURN_IF_ERROR(_get_segment_footer(footer_pb_shared, stats)); |
272 | | |
273 | 172k | _pk_index_meta.reset( |
274 | 172k | footer_pb_shared->has_primary_key_index_meta() |
275 | 172k | ? new PrimaryKeyIndexMetaPB(footer_pb_shared->primary_key_index_meta()) |
276 | 172k | : nullptr); |
277 | | // delete_bitmap_calculator_test.cpp |
278 | | // DCHECK(footer.has_short_key_index_page()); |
279 | 172k | _sk_index_page = footer_pb_shared->short_key_index_page(); |
280 | 172k | _num_rows = footer_pb_shared->num_rows(); |
281 | | |
282 | | // An estimated memory usage of a segment |
283 | | // Footer is seperated to StoragePageCache so we don't need to add it to _meta_mem_usage |
284 | | // _meta_mem_usage += footer_pb_shared->ByteSizeLong(); |
285 | 172k | if (_pk_index_meta != nullptr) { |
286 | 101k | _meta_mem_usage += _pk_index_meta->ByteSizeLong(); |
287 | 101k | } |
288 | | |
289 | 172k | _meta_mem_usage += sizeof(*this); |
290 | 172k | _meta_mem_usage += std::min(static_cast<int>(_tablet_schema->num_columns()), |
291 | 172k | config::max_segment_partial_column_cache_size) * |
292 | 172k | config::estimated_mem_per_column_reader; |
293 | | |
294 | | // 1024 comes from SegmentWriterOptions |
295 | 172k | _meta_mem_usage += (_num_rows + 1023) / 1024 * (36 + 4); |
296 | | // 0.01 comes from PrimaryKeyIndexBuilder::init |
297 | 172k | _meta_mem_usage += BloomFilter::optimal_bit_num(_num_rows, 0.01) / 8; |
298 | | |
299 | 172k | update_metadata_size(); |
300 | | |
301 | 172k | return Status::OK(); |
302 | 172k | } |
303 | | |
304 | 5.68k | Status Segment::_open_index_file_reader() { |
305 | | // Derive the index path from `_seg_path`, not `_file_reader->path()`: remote FS normalizes the |
306 | | // latter to an absolute path that won't match the relative keys in PackedFileSystem's index map. |
307 | 5.68k | _index_file_reader = std::make_shared<IndexFileReader>( |
308 | 5.68k | _fs, std::string {InvertedIndexDescriptor::get_index_file_path_prefix(_seg_path)}, |
309 | 5.68k | _tablet_schema->get_inverted_index_storage_format(), _idx_file_info, _tablet_id); |
310 | 5.68k | return Status::OK(); |
311 | 5.68k | } |
312 | | |
313 | | bool Segment::is_tso_placeholder_col(int cid, const Schema& schema, |
314 | 1.33M | const StorageReadOptions& read_options) const { |
315 | 1.33M | if (read_options.version.first != read_options.version.second) { |
316 | 702k | return false; |
317 | 702k | } |
318 | 630k | if (read_options.io_ctx.reader_type != ReaderType::READER_BINLOG && |
319 | 631k | read_options.io_ctx.reader_type != ReaderType::READER_BINLOG_COMPACTION) { |
320 | 631k | return false; |
321 | 631k | } |
322 | | // tso_col_idx() is -1 for non-binlog schemas, so this returns false there. |
323 | 18.4E | return cid == schema.tso_col_idx(); |
324 | 630k | } |
325 | | |
326 | | Status Segment::new_iterator(SchemaSPtr schema, const StorageReadOptions& read_options, |
327 | 1.50M | std::unique_ptr<RowwiseIterator>* iter) { |
328 | 1.50M | if (read_options.runtime_state != nullptr) { |
329 | 1.42M | _be_exec_version = read_options.runtime_state->be_exec_version(); |
330 | 1.42M | } |
331 | 1.50M | RETURN_IF_ERROR(_create_column_meta_once(read_options.stats)); |
332 | | |
333 | 1.50M | read_options.stats->total_segment_number++; |
334 | | // trying to prune the current segment by segment-level zone map |
335 | 1.50M | for (const auto& entry : read_options.col_id_to_predicates) { |
336 | 1.25M | int32_t column_id = entry.first; |
337 | | // schema change |
338 | 1.25M | if (_tablet_schema->num_columns() <= column_id) { |
339 | 1.43k | continue; |
340 | 1.43k | } |
341 | 1.25M | const TabletColumn& col = read_options.tablet_schema->column(column_id); |
342 | 1.25M | std::shared_ptr<ColumnReader> reader; |
343 | | // __DORIS_COMMIT_TSO_COL__ on a single-version segment stores a 0 placeholder on disk |
344 | | // (replaced with the rowset's real commit_tso at read time). Its on-disk zonemap [0,0] |
345 | | // must not drive segment-level pruning, so build a ConstantColumnReader carrying the real |
346 | | // commit_tso to prune against the real value instead. |
347 | 1.25M | std::optional<Field> const_value; |
348 | 1.25M | if (read_options.version.first == read_options.version.second && |
349 | 1.25M | column_id == schema->commit_tso_col_idx() && read_options.commit_tso.end_tso() != -1) { |
350 | 1 | const_value = Field::create_field<TYPE_BIGINT>(read_options.commit_tso.end_tso()); |
351 | 1 | } |
352 | 1.25M | Status st = get_column_reader(col, &reader, read_options.stats, std::move(const_value)); |
353 | | // not found in this segment, skip |
354 | 1.25M | if (st.is<ErrorCode::NOT_FOUND>()) { |
355 | 123 | continue; |
356 | 123 | } |
357 | 1.25M | RETURN_IF_ERROR(st); |
358 | | // should be OK |
359 | 1.25M | DCHECK(reader != nullptr); |
360 | 1.25M | if (!reader->has_zone_map()) { |
361 | 0 | continue; |
362 | 0 | } |
363 | | // Placeholder tso column on a single-version binlog segment: its zonemap reflects the |
364 | | // NULL placeholder (replaced with commit_tso at read time), so skip pruning by |
365 | | // zonemap (min == max == commit_tso) and reuse the predicate's own zonemap matching: |
366 | | // evaluate_and() returns false iff no value in [min, max] can satisfy the predicates, |
367 | | // i.e. commit_tso fails them and the whole segment can be pruned. Predicates that don't |
368 | | // support zonemap return true (conservative: not pruned, row-level eval handles them). |
369 | 1.25M | if (read_options.col_id_to_predicates.contains(column_id) && |
370 | 1.25M | is_tso_placeholder_col(column_id, *schema, read_options)) { |
371 | 0 | const Int64 commit_tso = |
372 | 0 | read_options.commit_tso.end_tso() == -1 ? 0 : read_options.commit_tso.end_tso(); |
373 | 0 | ZoneMap zone_map; |
374 | 0 | zone_map.min_value = Field::create_field<TYPE_BIGINT>(commit_tso); |
375 | 0 | zone_map.max_value = Field::create_field<TYPE_BIGINT>(commit_tso); |
376 | 0 | zone_map.has_not_null = true; |
377 | 0 | if (!entry.second->evaluate_and(zone_map)) { |
378 | | // any condition not satisfied, return. |
379 | 0 | *iter = std::make_unique<EmptySegmentIterator>(*schema); |
380 | 0 | read_options.stats->filtered_segment_number++; |
381 | 0 | return Status::OK(); |
382 | 0 | } |
383 | 0 | continue; |
384 | 0 | } |
385 | 1.25M | if (read_options.col_id_to_predicates.contains(column_id) && |
386 | 1.25M | can_apply_predicate_safely(column_id, *schema, |
387 | 1.25M | read_options.target_cast_type_for_variants, read_options)) { |
388 | 1.25M | bool matched = true; |
389 | 1.25M | RETURN_IF_ERROR(reader->match_condition(entry.second.get(), &matched)); |
390 | 1.25M | if (!matched) { |
391 | | // any condition not satisfied, return. |
392 | 73.8k | *iter = std::make_unique<EmptySegmentIterator>(*schema); |
393 | 73.8k | read_options.stats->filtered_segment_number++; |
394 | 73.8k | read_options.stats->rows_stats_filtered += num_rows(); |
395 | 73.8k | return Status::OK(); |
396 | 73.8k | } |
397 | 1.25M | } |
398 | 1.25M | } |
399 | | |
400 | | // Segment-level expr-zonemap runs before SegmentIterator can rebind storage expressions to |
401 | | // the reader schema. Only apply it when scan tuple slot ordinals already match this schema. |
402 | 1.43M | if (expr_zonemap::is_expr_zonemap_filter_enabled(read_options.runtime_state) && |
403 | 1.43M | !read_options.common_expr_ctxs_push_down.empty()) { |
404 | 17.9k | ZoneMapEvalContext ctx; |
405 | 17.9k | RETURN_IF_ERROR(build_segment_zonemap_context( |
406 | 17.9k | this, *schema, read_options, read_options.common_expr_ctxs_push_down, &ctx)); |
407 | 17.9k | const auto result = |
408 | 17.9k | VExprContext::evaluate_zonemap_filter(read_options.common_expr_ctxs_push_down, ctx); |
409 | 17.9k | ctx.stats.accumulate_to(read_options.stats); |
410 | 17.9k | if (result == ZoneMapFilterResult::kNoMatch) { |
411 | 356 | *iter = std::make_unique<EmptySegmentIterator>(*schema); |
412 | 356 | read_options.stats->filtered_segment_number++; |
413 | 356 | read_options.stats->expr_zonemap_filtered_segments++; |
414 | 356 | return Status::OK(); |
415 | 356 | } |
416 | 17.9k | } |
417 | | |
418 | 1.43M | { |
419 | 1.43M | SCOPED_RAW_TIMER(&read_options.stats->segment_load_index_timer_ns); |
420 | 1.43M | RETURN_IF_ERROR(load_index(read_options.stats)); |
421 | 1.43M | } |
422 | | |
423 | 1.43M | if (read_options.delete_condition_predicates->num_of_column_predicate() == 0 && |
424 | 1.43M | read_options.push_down_agg_type_opt != TPushAggOp::NONE && |
425 | 1.43M | read_options.push_down_agg_type_opt != TPushAggOp::COUNT_ON_INDEX) { |
426 | 10.6k | iter->reset(new_vstatistics_iterator(this->shared_from_this(), *schema)); |
427 | 1.42M | } else { |
428 | 1.42M | *iter = std::make_unique<SegmentIterator>(this->shared_from_this(), schema); |
429 | 1.42M | } |
430 | | |
431 | | // TODO: Valid the opt not only in ReaderType::READER_QUERY |
432 | 1.43M | if (read_options.io_ctx.reader_type == ReaderType::READER_QUERY && |
433 | 1.43M | !read_options.column_predicates.empty()) { |
434 | 1.14M | auto pruned_predicates = read_options.column_predicates; |
435 | 1.14M | auto pruned = false; |
436 | 16.3M | for (auto& it : _column_reader_cache->get_available_readers(false)) { |
437 | 16.3M | const auto uid = it.first; |
438 | 16.3M | const auto column_id = read_options.tablet_schema->field_index(uid); |
439 | 16.3M | bool tmp_pruned = false; |
440 | 16.3M | RETURN_IF_ERROR(it.second->prune_predicates_by_zone_map(pruned_predicates, column_id, |
441 | 16.3M | &tmp_pruned)); |
442 | 16.3M | pruned |= tmp_pruned; |
443 | 16.3M | } |
444 | | |
445 | 1.14M | if (pruned) { |
446 | 7.85k | auto options_with_pruned_predicates = read_options; |
447 | 7.85k | options_with_pruned_predicates.column_predicates = pruned_predicates; |
448 | 7.85k | std::set<uint32_t> cols_with_pred_after_prune; |
449 | 14.5k | for (const auto& pred : pruned_predicates) { |
450 | 14.5k | cols_with_pred_after_prune.insert(pred->column_id()); |
451 | 14.5k | } |
452 | 26.6k | for (const auto& pred : read_options.column_predicates) { |
453 | 26.6k | const auto pred_cid = pred->column_id(); |
454 | | // Key columns may still be required by key range seeks even if the segment zone |
455 | | // map proves their predicates always true. Only mark non-key columns as safe for |
456 | | // the no-need-read path. |
457 | 26.6k | if (!read_options.tablet_schema->column(pred_cid).is_key() && |
458 | 26.6k | !cols_with_pred_after_prune.contains(pred_cid)) { |
459 | 6.00k | options_with_pruned_predicates.zonemap_always_true_pred_cols.insert(pred_cid); |
460 | 6.00k | } |
461 | 26.6k | } |
462 | | //because column_predicates is changed, we need to rebuild col_id_to_predicates so that inverted index will not go through it. |
463 | 7.85k | options_with_pruned_predicates.col_id_to_predicates.clear(); |
464 | 14.5k | for (auto pred : options_with_pruned_predicates.column_predicates) { |
465 | 14.5k | if (!options_with_pruned_predicates.col_id_to_predicates.contains( |
466 | 14.5k | pred->column_id())) { |
467 | 8.88k | options_with_pruned_predicates.col_id_to_predicates.insert( |
468 | 8.88k | {pred->column_id(), AndBlockColumnPredicate::create_shared()}); |
469 | 8.88k | } |
470 | 14.5k | options_with_pruned_predicates.col_id_to_predicates[pred->column_id()] |
471 | 14.5k | ->add_column_predicate(SingleColumnBlockPredicate::create_unique(pred)); |
472 | 14.5k | } |
473 | 7.85k | return iter->get()->init(options_with_pruned_predicates); |
474 | 7.85k | } |
475 | 1.14M | } |
476 | 1.42M | return iter->get()->init(read_options); |
477 | 1.43M | } |
478 | | |
479 | | Status Segment::_write_error_file(size_t file_size, size_t offset, size_t bytes_read, char* data, |
480 | 1 | io::IOContext& io_ctx) { |
481 | 1 | if (!config::enbale_dump_error_file || !doris::config::is_cloud_mode()) { |
482 | 1 | return Status::OK(); |
483 | 1 | } |
484 | | |
485 | 0 | std::string file_name = _rowset_id.to_string() + "_" + std::to_string(_segment_id) + ".dat"; |
486 | 0 | std::string dir_path = io::FileCacheFactory::instance()->get_base_paths()[0] + "/error_file/"; |
487 | 0 | Status create_st = io::global_local_filesystem()->create_directory(dir_path, true); |
488 | 0 | if (!create_st.ok() && !create_st.is<ErrorCode::ALREADY_EXIST>()) { |
489 | 0 | LOG(WARNING) << "failed to create error file dir: " << create_st.to_string(); |
490 | 0 | return create_st; |
491 | 0 | } |
492 | 0 | size_t dir_size = 0; |
493 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->directory_size(dir_path, &dir_size)); |
494 | 0 | if (dir_size > config::file_cache_error_log_limit_bytes) { |
495 | 0 | LOG(WARNING) << "error file dir size is too large: " << dir_size; |
496 | 0 | return Status::OK(); |
497 | 0 | } |
498 | | |
499 | 0 | std::string error_part; |
500 | 0 | error_part.resize(bytes_read); |
501 | 0 | std::string part_path = dir_path + file_name + ".part_offset_" + std::to_string(offset); |
502 | 0 | LOG(WARNING) << "writer error part to " << part_path; |
503 | 0 | bool is_part_exist = false; |
504 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->exists(part_path, &is_part_exist)); |
505 | 0 | if (is_part_exist) { |
506 | 0 | LOG(WARNING) << "error part already exists: " << part_path; |
507 | 0 | } else { |
508 | 0 | std::unique_ptr<io::FileWriter> part_writer; |
509 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->create_file(part_path, &part_writer)); |
510 | 0 | RETURN_IF_ERROR(part_writer->append(Slice(data, bytes_read))); |
511 | 0 | RETURN_IF_ERROR(part_writer->close()); |
512 | 0 | } |
513 | | |
514 | 0 | std::string error_file; |
515 | 0 | error_file.resize(file_size); |
516 | 0 | auto* cached_reader = dynamic_cast<io::CachedRemoteFileReader*>(_file_reader.get()); |
517 | 0 | if (cached_reader == nullptr) { |
518 | 0 | return Status::InternalError("file reader is not CachedRemoteFileReader"); |
519 | 0 | } |
520 | 0 | size_t error_file_bytes_read = 0; |
521 | 0 | RETURN_IF_ERROR(cached_reader->get_remote_reader()->read_at( |
522 | 0 | 0, Slice(error_file.data(), file_size), &error_file_bytes_read, &io_ctx)); |
523 | 0 | DCHECK(error_file_bytes_read == file_size); |
524 | | //std::string file_path = dir_path + std::to_string(cur_time) + "_" + ss.str(); |
525 | 0 | std::string file_path = dir_path + file_name; |
526 | 0 | LOG(WARNING) << "writer error file to " << file_path; |
527 | 0 | bool is_file_exist = false; |
528 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->exists(file_path, &is_file_exist)); |
529 | 0 | if (is_file_exist) { |
530 | 0 | LOG(WARNING) << "error file already exists: " << part_path; |
531 | 0 | } else { |
532 | 0 | std::unique_ptr<io::FileWriter> writer; |
533 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->create_file(file_path, &writer)); |
534 | 0 | RETURN_IF_ERROR(writer->append(Slice(error_file.data(), file_size))); |
535 | 0 | RETURN_IF_ERROR(writer->close()); |
536 | 0 | } |
537 | 0 | return Status::OK(); // already exists |
538 | 0 | }; |
539 | | |
540 | | Status Segment::_parse_footer(std::shared_ptr<SegmentFooterPB>& footer, |
541 | 75.0k | OlapReaderStatistics* stats) { |
542 | | // Footer := SegmentFooterPB, FooterPBSize(4), FooterPBChecksum(4), MagicNumber(4) |
543 | 75.0k | auto file_size = _file_reader->size(); |
544 | 75.0k | if (file_size < 12) { |
545 | 0 | return Status::Corruption("Bad segment file {}: file size {} < 12, cache_key: {}", |
546 | 0 | _file_reader->path().native(), file_size, |
547 | 0 | file_cache_key_str(_file_reader->path().native())); |
548 | 0 | } |
549 | | |
550 | 75.0k | uint8_t fixed_buf[12]; |
551 | 75.0k | size_t bytes_read = 0; |
552 | | // TODO(plat1ko): Support session variable `enable_file_cache` |
553 | 75.0k | io::IOContext io_ctx {.is_index_data = true, |
554 | 75.0k | .file_cache_stats = stats ? &stats->file_cache_stats : nullptr}; |
555 | 75.0k | RETURN_IF_ERROR( |
556 | 75.0k | _file_reader->read_at(file_size - 12, Slice(fixed_buf, 12), &bytes_read, &io_ctx)); |
557 | 75.0k | DCHECK_EQ(bytes_read, 12); |
558 | 75.0k | TEST_SYNC_POINT_CALLBACK("Segment::parse_footer:magic_number_corruption", fixed_buf); |
559 | 75.0k | TEST_INJECTION_POINT_CALLBACK("Segment::parse_footer:magic_number_corruption_inj", fixed_buf); |
560 | 75.0k | if (memcmp(fixed_buf + 8, k_segment_magic, k_segment_magic_length) != 0) { |
561 | 1 | Status st = |
562 | 1 | _write_error_file(file_size, file_size - 12, bytes_read, (char*)fixed_buf, io_ctx); |
563 | 1 | if (!st.ok()) { |
564 | 0 | LOG(WARNING) << "failed to write error file: " << st.to_string(); |
565 | 0 | } |
566 | 1 | return Status::Corruption( |
567 | 1 | "Bad segment file {}: file_size: {}, magic number not match, cache_key: {}", |
568 | 1 | _file_reader->path().native(), file_size, |
569 | 1 | file_cache_key_str(_file_reader->path().native())); |
570 | 1 | } |
571 | | |
572 | | // read footer PB |
573 | 75.0k | uint32_t footer_length = decode_fixed32_le(fixed_buf); |
574 | 75.0k | if (file_size < 12 + footer_length) { |
575 | 0 | Status st = |
576 | 0 | _write_error_file(file_size, file_size - 12, bytes_read, (char*)fixed_buf, io_ctx); |
577 | 0 | if (!st.ok()) { |
578 | 0 | LOG(WARNING) << "failed to write error file: " << st.to_string(); |
579 | 0 | } |
580 | 0 | return Status::Corruption("Bad segment file {}: file size {} < {}, cache_key: {}", |
581 | 0 | _file_reader->path().native(), file_size, 12 + footer_length, |
582 | 0 | file_cache_key_str(_file_reader->path().native())); |
583 | 0 | } |
584 | | |
585 | 75.0k | std::string footer_buf; |
586 | 75.0k | footer_buf.resize(footer_length); |
587 | 75.0k | RETURN_IF_ERROR(_file_reader->read_at(file_size - 12 - footer_length, footer_buf, &bytes_read, |
588 | 75.0k | &io_ctx)); |
589 | 75.0k | DCHECK_EQ(bytes_read, footer_length); |
590 | | |
591 | | // validate footer PB's checksum |
592 | 75.0k | uint32_t expect_checksum = decode_fixed32_le(fixed_buf + 4); |
593 | 75.0k | uint32_t actual_checksum = crc32c::Crc32c(footer_buf.data(), footer_buf.size()); |
594 | 75.0k | if (actual_checksum != expect_checksum) { |
595 | 0 | Status st = _write_error_file(file_size, file_size - 12 - footer_length, bytes_read, |
596 | 0 | footer_buf.data(), io_ctx); |
597 | 0 | if (!st.ok()) { |
598 | 0 | LOG(WARNING) << "failed to write error file: " << st.to_string(); |
599 | 0 | } |
600 | 0 | return Status::Corruption( |
601 | 0 | "Bad segment file {}: file_size = {}, footer checksum not match, actual={} " |
602 | 0 | "vs expect={}, cache_key: {}", |
603 | 0 | _file_reader->path().native(), file_size, actual_checksum, expect_checksum, |
604 | 0 | file_cache_key_str(_file_reader->path().native())); |
605 | 0 | } |
606 | | |
607 | | // deserialize footer PB |
608 | 75.0k | footer = std::make_shared<SegmentFooterPB>(); |
609 | 75.0k | if (!footer->ParseFromString(footer_buf)) { |
610 | 0 | Status st = _write_error_file(file_size, file_size - 12 - footer_length, bytes_read, |
611 | 0 | footer_buf.data(), io_ctx); |
612 | 0 | if (!st.ok()) { |
613 | 0 | LOG(WARNING) << "failed to write error file: " << st.to_string(); |
614 | 0 | } |
615 | 0 | return Status::Corruption( |
616 | 0 | "Bad segment file {}: file_size = {}, failed to parse SegmentFooterPB, " |
617 | 0 | "cache_key: ", |
618 | 0 | _file_reader->path().native(), file_size, |
619 | 0 | file_cache_key_str(_file_reader->path().native())); |
620 | 0 | } |
621 | | |
622 | 18.4E | VLOG_DEBUG << fmt::format("Loading segment footer from {} finished", |
623 | 18.4E | _file_reader->path().native()); |
624 | 75.0k | return Status::OK(); |
625 | 75.0k | } |
626 | | |
627 | 3.81M | Status Segment::_load_pk_bloom_filter(OlapReaderStatistics* stats) { |
628 | | #ifdef BE_TEST |
629 | | if (_pk_index_meta == nullptr) { |
630 | | // for BE UT "segment_cache_test" |
631 | | return _load_pk_bf_once.call([this] { |
632 | | _meta_mem_usage += 100; |
633 | | update_metadata_size(); |
634 | | return Status::OK(); |
635 | | }); |
636 | | } |
637 | | #endif |
638 | 3.81M | DCHECK(_tablet_schema->keys_type() == UNIQUE_KEYS); |
639 | 3.81M | DCHECK(_pk_index_meta != nullptr); |
640 | 3.81M | DCHECK(_pk_index_reader != nullptr); |
641 | | |
642 | 3.81M | return _load_pk_bf_once.call([this, stats] { |
643 | 37.1k | RETURN_IF_ERROR(_pk_index_reader->parse_bf(_file_reader, *_pk_index_meta, stats)); |
644 | | // _meta_mem_usage += _pk_index_reader->get_bf_memory_size(); |
645 | 37.1k | return Status::OK(); |
646 | 37.1k | }); |
647 | 3.81M | } |
648 | | |
649 | 3.81M | Status Segment::load_pk_index_and_bf(OlapReaderStatistics* index_load_stats) { |
650 | | // `DorisCallOnce` may catch exception in calling stack A and re-throw it in |
651 | | // a different calling stack B which doesn't have catch block. So we add catch block here |
652 | | // to prevent coreudmp |
653 | 3.81M | RETURN_IF_CATCH_EXCEPTION({ |
654 | 3.81M | RETURN_IF_ERROR(load_index(index_load_stats)); |
655 | 3.81M | RETURN_IF_ERROR(_load_pk_bloom_filter(index_load_stats)); |
656 | 3.81M | }); |
657 | 3.82M | return Status::OK(); |
658 | 3.81M | } |
659 | | |
660 | 5.27M | Status Segment::load_index(OlapReaderStatistics* stats) { |
661 | 5.27M | return _load_index_once.call([this, stats] { |
662 | 87.2k | if (_tablet_schema->keys_type() == UNIQUE_KEYS && _pk_index_meta != nullptr) { |
663 | 45.6k | _pk_index_reader = std::make_unique<PrimaryKeyIndexReader>(); |
664 | 45.6k | RETURN_IF_ERROR(_pk_index_reader->parse_index(_file_reader, *_pk_index_meta, stats)); |
665 | | // _meta_mem_usage += _pk_index_reader->get_memory_size(); |
666 | 45.6k | return Status::OK(); |
667 | 45.6k | } else { |
668 | | // read and parse short key index page |
669 | 41.6k | OlapReaderStatistics tmp_stats; |
670 | 41.6k | OlapReaderStatistics* stats_ptr = stats != nullptr ? stats : &tmp_stats; |
671 | 41.6k | PageReadOptions opts(io::IOContext {.is_index_data = true, |
672 | 41.6k | .file_cache_stats = &stats_ptr->file_cache_stats}); |
673 | 41.6k | opts.use_page_cache = true; |
674 | 41.6k | opts.type = INDEX_PAGE; |
675 | 41.6k | opts.file_reader = _file_reader.get(); |
676 | 41.6k | opts.page_pointer = PagePointer(_sk_index_page); |
677 | | // short key index page uses NO_COMPRESSION for now |
678 | 41.6k | opts.codec = nullptr; |
679 | 41.6k | opts.stats = &tmp_stats; |
680 | | |
681 | 41.6k | Slice body; |
682 | 41.6k | PageFooterPB footer; |
683 | 41.6k | RETURN_IF_ERROR( |
684 | 41.6k | PageIO::read_and_decompress_page(opts, &_sk_index_handle, &body, &footer)); |
685 | 41.6k | DCHECK_EQ(footer.type(), SHORT_KEY_PAGE); |
686 | 41.6k | DCHECK(footer.has_short_key_page_footer()); |
687 | | |
688 | | // _meta_mem_usage += body.get_size(); |
689 | 41.6k | _sk_index_decoder = std::make_unique<ShortKeyIndexDecoder>(); |
690 | 41.6k | return _sk_index_decoder->parse(body, footer.short_key_page_footer()); |
691 | 41.6k | } |
692 | 87.2k | }); |
693 | 5.27M | } |
694 | | |
695 | 1.55M | Status Segment::healthy_status() { |
696 | 1.55M | try { |
697 | 1.55M | if (_load_index_once.has_called()) { |
698 | 1.51M | RETURN_IF_ERROR(_load_index_once.stored_result()); |
699 | 1.51M | } |
700 | 1.55M | if (_load_pk_bf_once.has_called()) { |
701 | 842k | RETURN_IF_ERROR(_load_pk_bf_once.stored_result()); |
702 | 842k | } |
703 | 1.55M | if (_create_column_meta_once_call.has_called()) { |
704 | 1.50M | RETURN_IF_ERROR(_create_column_meta_once_call.stored_result()); |
705 | 1.50M | } |
706 | 1.55M | if (_index_file_reader_open.has_called()) { |
707 | 26.3k | RETURN_IF_ERROR(_index_file_reader_open.stored_result()); |
708 | 26.3k | } |
709 | | // This status is set by running time, for example, if there is something wrong during read segment iterator. |
710 | 1.55M | return _healthy_status.status(); |
711 | 1.55M | } catch (const doris::Exception& e) { |
712 | | // If there is an exception during load_xxx, should not throw exception directly because |
713 | | // the caller may not exception safe. |
714 | 0 | return e.to_status(); |
715 | 0 | } catch (const std::exception& e) { |
716 | | // The exception is not thrown by doris code. |
717 | 0 | return Status::InternalError("Unexcepted error during load segment: {}", e.what()); |
718 | 0 | } |
719 | 1.55M | } |
720 | | |
721 | | // Return the storage datatype of related column to field. |
722 | | DataTypePtr Segment::get_data_type_of(const TabletColumn& column, |
723 | 21.2M | const StorageReadOptions& read_options) { |
724 | 21.2M | const PathInDataPtr path = column.path_info_ptr(); |
725 | | |
726 | | // none variant column |
727 | 21.2M | if (path == nullptr || path->empty()) { |
728 | 21.2M | return DataTypeFactory::instance().create_data_type(column); |
729 | 21.2M | } |
730 | | |
731 | | // Path exists, proceed with variant logic. |
732 | 18.4E | PathInData relative_path = path->copy_pop_front(); |
733 | 18.4E | int32_t unique_id = column.unique_id() >= 0 ? column.unique_id() : column.parent_unique_id(); |
734 | | |
735 | | // If this uid does not exist in segment meta, fallback to schema type. |
736 | 18.4E | if (!_column_meta_accessor->has_column_uid(unique_id)) { |
737 | 685 | return DataTypeFactory::instance().create_data_type(column); |
738 | 685 | } |
739 | | |
740 | 18.4E | std::shared_ptr<ColumnReader> v_reader; |
741 | | |
742 | | // Get the parent variant column reader |
743 | 18.4E | OlapReaderStatistics stats; |
744 | | // If status is not ok, it will throw exception(data corruption) |
745 | 18.4E | THROW_IF_ERROR(get_column_reader(unique_id, &v_reader, &stats)); |
746 | 18.4E | DCHECK(v_reader != nullptr); |
747 | 18.4E | auto* variant_reader = static_cast<VariantColumnReader*>(v_reader.get()); |
748 | | // Delegate type inference for variant paths to VariantColumnReader. |
749 | 18.4E | DataTypePtr type; |
750 | 18.4E | THROW_IF_ERROR(variant_reader->infer_data_type_for_path(&type, column, read_options, |
751 | 18.4E | _column_reader_cache.get())); |
752 | 18.4E | return type; |
753 | 18.4E | } |
754 | | |
755 | 39.8M | Status Segment::_create_column_meta_once(OlapReaderStatistics* stats) { |
756 | 39.8M | SCOPED_RAW_TIMER(&stats->segment_create_column_readers_timer_ns); |
757 | 39.8M | return _create_column_meta_once_call.call([&] { |
758 | 110k | std::shared_ptr<SegmentFooterPB> footer_pb_shared; |
759 | 110k | RETURN_IF_ERROR(_get_segment_footer(footer_pb_shared, stats)); |
760 | 110k | return _create_column_meta(*footer_pb_shared); |
761 | 110k | }); |
762 | 39.8M | } |
763 | | |
764 | 109k | Status Segment::_create_column_meta(const SegmentFooterPB& footer) { |
765 | | // Initialize column meta accessor which internally maintains uid -> column_ordinal mapping. |
766 | 109k | _column_meta_accessor = std::make_unique<ColumnMetaAccessor>(); |
767 | 109k | RETURN_IF_ERROR(_column_meta_accessor->init(footer, _file_reader)); |
768 | | |
769 | 109k | if (config::enable_adaptive_batch_size) { |
770 | | // Cache raw_data_bytes per column uid for adaptive batch size prediction. |
771 | | // This runs under call_once, so no thread-safety concerns. |
772 | 1.34M | auto st = _column_meta_accessor->traverse_metas(footer, [this](const ColumnMetaPB& meta) { |
773 | 1.35M | if (meta.has_unique_id() && meta.unique_id() != -1 && meta.has_raw_data_bytes()) { |
774 | 1.03M | _column_uid_to_raw_bytes[meta.unique_id()] = meta.raw_data_bytes(); |
775 | 1.03M | } |
776 | 1.34M | }); |
777 | | |
778 | 105k | if (!st.ok()) { |
779 | 0 | LOG(WARNING) << "Failed to traverse column metas to cache raw_data_bytes, error: " |
780 | 0 | << st.to_string(); |
781 | 0 | } |
782 | 105k | } |
783 | | |
784 | 109k | _column_reader_cache = std::make_unique<ColumnReaderCache>( |
785 | 109k | _column_meta_accessor.get(), _tablet_schema, _file_reader, _num_rows, |
786 | 3.93M | [this](std::shared_ptr<SegmentFooterPB>& footer_pb, OlapReaderStatistics* stats) { |
787 | 3.93M | return _get_segment_footer(footer_pb, stats); |
788 | 3.93M | }); |
789 | 109k | return Status::OK(); |
790 | 109k | } |
791 | | |
792 | | Status Segment::new_default_iterator(const TabletColumn& tablet_column, |
793 | 11.6k | std::unique_ptr<ColumnIterator>* iter) { |
794 | 11.6k | if (!tablet_column.has_default_value() && !tablet_column.is_nullable()) { |
795 | 0 | return Status::InternalError( |
796 | 0 | "invalid nonexistent column without default value. column_uid={}, " |
797 | 0 | "column_name={}, " |
798 | 0 | "column_type={}", |
799 | 0 | tablet_column.unique_id(), tablet_column.name(), tablet_column.type()); |
800 | 0 | } |
801 | 11.6k | std::unique_ptr<DefaultValueColumnIterator> default_value_iter(new DefaultValueColumnIterator( |
802 | 11.6k | tablet_column.has_default_value(), tablet_column.default_value(), |
803 | 11.6k | tablet_column.is_nullable(), tablet_column.type(), tablet_column.precision(), |
804 | 11.6k | tablet_column.frac(), tablet_column.length())); |
805 | 11.6k | ColumnIteratorOptions iter_opts; |
806 | | |
807 | 11.6k | RETURN_IF_ERROR(default_value_iter->init(iter_opts)); |
808 | 11.6k | *iter = std::move(default_value_iter); |
809 | 11.6k | return Status::OK(); |
810 | 11.6k | } |
811 | | |
812 | | // Not use cid anymore, for example original table schema is colA int, then user do following actions |
813 | | // 1.add column b |
814 | | // 2. drop column b |
815 | | // 3. add column c |
816 | | // in the new schema column c's cid == 2 |
817 | | // but in the old schema column b's cid == 2 |
818 | | // but they are not the same column |
819 | | Status Segment::new_column_iterator(const TabletColumn& tablet_column, |
820 | | std::unique_ptr<ColumnIterator>* iter, |
821 | | const StorageReadOptions* opt, |
822 | | const std::unordered_map<int32_t, PathToBinaryColumnCacheUPtr>* |
823 | 18.4M | variant_sparse_column_cache) { |
824 | 18.4M | if (opt->runtime_state != nullptr) { |
825 | 18.1M | _be_exec_version = opt->runtime_state->be_exec_version(); |
826 | 18.1M | } |
827 | 18.4M | RETURN_IF_ERROR(_create_column_meta_once(opt->stats)); |
828 | | |
829 | | // For compability reason unique_id may less than 0 for variant extracted column |
830 | 18.5M | int32_t unique_id = tablet_column.unique_id() >= 0 ? tablet_column.unique_id() |
831 | 18.4E | : tablet_column.parent_unique_id(); |
832 | | |
833 | | // If column meta for this uid is not found in this segment, use default iterator. |
834 | 18.4M | if (!_column_meta_accessor->has_column_uid(unique_id)) { |
835 | 3.43k | RETURN_IF_ERROR(new_default_iterator(tablet_column, iter)); |
836 | 3.43k | return Status::OK(); |
837 | 3.43k | } |
838 | | |
839 | | // __DORIS_COMMIT_TSO_COL__ on a single-version segment stores a 0 placeholder on disk (its |
840 | | // real value is the rowset's commit_tso, filled at read time). Pass the real commit_tso as a |
841 | | // const value so the cache returns a ConstantColumnReader, whose iterator yields the real value |
842 | | // on every read path (projection / predicate / MIN-MAX zone-map) instead of the placeholder 0. |
843 | | // commit_tso == -1 means it is not assigned yet (before publish); keep the on-disk value then. |
844 | | // The value is constant per segment (a segment belongs to a single rowset), so caching the |
845 | | // ConstantColumnReader does not cross-pollute other queries. Some internal read paths (e.g. MOW |
846 | | // partial-update row fetch) build a bare StorageReadOptions without tablet_schema, so guard it. |
847 | 18.4M | std::optional<Field> const_value; |
848 | 18.5M | if (opt->tablet_schema != nullptr && opt->version.first == opt->version.second && |
849 | 18.4M | opt->commit_tso.end_tso() != -1) { |
850 | 1 | int32_t tso_idx = opt->tablet_schema->commit_tso_col_idx(); |
851 | 1 | if (tso_idx != -1 && opt->tablet_schema->column(tso_idx).unique_id() == unique_id) { |
852 | 1 | const_value = Field::create_field<TYPE_BIGINT>(opt->commit_tso.end_tso()); |
853 | 1 | } |
854 | 1 | } |
855 | | |
856 | | // init iterator by unique id |
857 | 18.4M | std::shared_ptr<ColumnReader> reader; |
858 | 18.4M | RETURN_IF_ERROR(get_column_reader(unique_id, &reader, opt->stats, std::move(const_value))); |
859 | 18.4M | if (reader == nullptr) { |
860 | 0 | return Status::InternalError("column reader is nullptr, unique_id={}", unique_id); |
861 | 0 | } |
862 | 18.4M | if (reader->get_meta_type() == FieldType::OLAP_FIELD_TYPE_VARIANT) { |
863 | | // if sparse_column_cache_ptr is nullptr, means the sparse column cache is not used |
864 | 33.0k | PathToBinaryColumnCache* sparse_column_cache_ptr = nullptr; |
865 | 33.0k | if (variant_sparse_column_cache) { |
866 | 32.4k | auto it = variant_sparse_column_cache->find(unique_id); |
867 | 32.5k | if (it != variant_sparse_column_cache->end()) { |
868 | 32.5k | sparse_column_cache_ptr = it->second.get(); |
869 | 18.4E | } else { |
870 | 18.4E | DCHECK(false) << "sparse column cache is not found, unique_id=" << unique_id; |
871 | 18.4E | } |
872 | 32.4k | } |
873 | | // use _column_reader_cache to get variant subcolumn(path column) reader |
874 | 33.0k | RETURN_IF_ERROR(assert_cast<VariantColumnReader*>(reader.get()) |
875 | 33.0k | ->new_iterator(iter, &tablet_column, opt, |
876 | 33.0k | _column_reader_cache.get(), |
877 | 33.0k | sparse_column_cache_ptr)); |
878 | 18.4M | } else { |
879 | 18.4M | RETURN_IF_ERROR(reader->new_iterator(iter, &tablet_column, opt)); |
880 | 18.4M | if (opt->all_access_paths.contains(unique_id) || |
881 | 18.4M | opt->predicate_access_paths.contains(unique_id)) { |
882 | 59.4k | const auto& all_access_paths = opt->all_access_paths.contains(unique_id) |
883 | 59.4k | ? opt->all_access_paths.at(unique_id) |
884 | 59.4k | : TColumnAccessPaths {}; |
885 | 59.4k | const auto& predicate_access_paths = opt->predicate_access_paths.contains(unique_id) |
886 | 59.4k | ? opt->predicate_access_paths.at(unique_id) |
887 | 59.4k | : TColumnAccessPaths {}; |
888 | | |
889 | | // set column name to apply access paths. |
890 | 59.4k | (*iter)->set_column_name(tablet_column.name()); |
891 | 59.4k | RETURN_IF_ERROR((*iter)->set_access_paths(all_access_paths, predicate_access_paths)); |
892 | 59.4k | (*iter)->remove_pruned_sub_iterators(); |
893 | 59.4k | } |
894 | 18.4M | } |
895 | | |
896 | 18.5M | if (config::enable_column_type_check && !tablet_column.has_path_info() && |
897 | 18.5M | !tablet_column.is_agg_state_type() && tablet_column.type() != reader->get_meta_type()) { |
898 | 0 | LOG(WARNING) << "different type between schema and column reader," |
899 | 0 | << " column schema name: " << tablet_column.name() |
900 | 0 | << " column schema type: " << int(tablet_column.type()) |
901 | 0 | << " column reader meta type: " << int(reader->get_meta_type()); |
902 | 0 | return Status::InternalError("different type between schema and column reader"); |
903 | 0 | } |
904 | 18.4M | return Status::OK(); |
905 | 18.4M | } |
906 | | |
907 | | Status Segment::get_column_reader(int32_t col_uid, std::shared_ptr<ColumnReader>* column_reader, |
908 | 18.5M | OlapReaderStatistics* stats, std::optional<Field> const_value) { |
909 | 18.5M | RETURN_IF_ERROR(_create_column_meta_once(stats)); |
910 | 18.5M | SCOPED_RAW_TIMER(&stats->segment_create_column_readers_timer_ns); |
911 | | // The column is not in this segment, return nullptr |
912 | 18.5M | if (!_tablet_schema->has_column_unique_id(col_uid)) { |
913 | 0 | *column_reader = nullptr; |
914 | 0 | return Status::Error<ErrorCode::NOT_FOUND, false>("column not found in segment, col_uid={}", |
915 | 0 | col_uid); |
916 | 0 | } |
917 | 18.5M | return _column_reader_cache->get_column_reader(col_uid, column_reader, stats, |
918 | 18.5M | std::move(const_value)); |
919 | 18.5M | } |
920 | | |
921 | 43.4k | Status Segment::traverse_column_meta_pbs(const std::function<void(const ColumnMetaPB&)>& visitor) { |
922 | | // Ensure column meta accessor and reader cache are initialized once. |
923 | 43.4k | OlapReaderStatistics dummy_stats; |
924 | 43.4k | RETURN_IF_ERROR(_create_column_meta_once(&dummy_stats)); |
925 | 43.4k | std::shared_ptr<SegmentFooterPB> footer_pb_shared; |
926 | 43.4k | RETURN_IF_ERROR(_get_segment_footer(footer_pb_shared, &dummy_stats)); |
927 | 43.4k | return _column_meta_accessor->traverse_metas(*footer_pb_shared, visitor); |
928 | 43.4k | } |
929 | | |
930 | | Status Segment::get_column_reader(const TabletColumn& col, |
931 | | std::shared_ptr<ColumnReader>* column_reader, |
932 | 1.33M | OlapReaderStatistics* stats, std::optional<Field> const_value) { |
933 | 1.33M | RETURN_IF_ERROR(_create_column_meta_once(stats)); |
934 | 1.33M | SCOPED_RAW_TIMER(&stats->segment_create_column_readers_timer_ns); |
935 | 18.4E | int col_uid = col.unique_id() >= 0 ? col.unique_id() : col.parent_unique_id(); |
936 | | // The column is not in this segment, return nullptr |
937 | 1.33M | if (!_tablet_schema->has_column_unique_id(col_uid)) { |
938 | 55 | *column_reader = nullptr; |
939 | 55 | return Status::Error<ErrorCode::NOT_FOUND, false>("column not found in segment, col_uid={}", |
940 | 55 | col_uid); |
941 | 55 | } |
942 | 1.33M | if (col.has_path_info()) { |
943 | 2.61k | PathInData relative_path = col.path_info_ptr()->copy_pop_front(); |
944 | 2.61k | return _column_reader_cache->get_path_column_reader(col_uid, relative_path, column_reader, |
945 | 2.61k | stats); |
946 | 2.61k | } |
947 | 1.33M | return _column_reader_cache->get_column_reader(col_uid, column_reader, stats, |
948 | 1.33M | std::move(const_value)); |
949 | 1.33M | } |
950 | | |
951 | | Status Segment::new_index_iterator(const TabletColumn& tablet_column, const TabletIndex* index_meta, |
952 | | const StorageReadOptions& read_options, |
953 | 75.6k | std::unique_ptr<IndexIterator>* iter) { |
954 | 75.6k | if (read_options.runtime_state != nullptr) { |
955 | 66.4k | _be_exec_version = read_options.runtime_state->be_exec_version(); |
956 | 66.4k | } |
957 | 75.6k | RETURN_IF_ERROR(_create_column_meta_once(read_options.stats)); |
958 | 75.6k | std::shared_ptr<ColumnReader> reader; |
959 | 75.6k | auto st = get_column_reader(tablet_column, &reader, read_options.stats); |
960 | 75.6k | if (st.is<ErrorCode::NOT_FOUND>()) { |
961 | 493 | return Status::OK(); |
962 | 493 | } |
963 | 75.1k | RETURN_IF_ERROR(st); |
964 | 75.1k | DCHECK(reader != nullptr); |
965 | 75.3k | if (index_meta) { |
966 | | // call DorisCallOnce.call without check if _index_file_reader is nullptr |
967 | | // to avoid data race during parallel method calls |
968 | 75.3k | RETURN_IF_ERROR(_index_file_reader_open.call([&] { return _open_index_file_reader(); })); |
969 | | // after DorisCallOnce.call, _index_file_reader is guaranteed to be not nullptr |
970 | 75.3k | const std::string rowset_id = |
971 | 75.3k | index_meta->index_type() == IndexType::ANN ? _rowset_id.to_string() : ""; |
972 | 75.3k | const bool need_binding_diagnostic = tablet_column.is_variant_type() || |
973 | 75.3k | tablet_column.is_extracted_column() || |
974 | 75.3k | !index_meta->get_index_suffix().empty(); |
975 | 75.3k | bool index_file_exists = false; |
976 | 75.3k | Status probe_status; |
977 | 75.3k | if (need_binding_diagnostic) { |
978 | 1.78k | probe_status = _index_file_reader->init(config::inverted_index_read_buffer_size, |
979 | 1.78k | &read_options.io_ctx); |
980 | 1.78k | if (probe_status.ok()) { |
981 | 1.78k | probe_status = _index_file_reader->index_file_exist(index_meta, &index_file_exists); |
982 | 1.78k | } |
983 | 1.78k | const auto diagnostic = fmt::format( |
984 | 1.78k | "[VariantSearchBinding] phase=index_file_probe tablet_id={} rowset_id={} " |
985 | 1.78k | "segment_id={} column={} logical_path={} index_id={} suffix={} exists={} " |
986 | 1.78k | "status={}", |
987 | 1.78k | read_options.tablet_id, _rowset_id.to_string(), _segment_id, |
988 | 1.78k | tablet_column.name(), |
989 | 1.78k | tablet_column.has_path_info() ? tablet_column.path_info_ptr()->get_path() |
990 | 18.4E | : tablet_column.name(), |
991 | 1.78k | index_meta->index_id(), index_meta->get_index_suffix(), index_file_exists, |
992 | 18.4E | probe_status.ok() ? "OK" : probe_status.to_string()); |
993 | 18.4E | VLOG_DEBUG << diagnostic; |
994 | 1.78k | if (read_options.stats != nullptr) { |
995 | 1.78k | read_options.stats->inverted_index_stats.add_binding_diagnostic(diagnostic); |
996 | 1.78k | } |
997 | 1.78k | } |
998 | 75.3k | Status iter_status = reader->new_index_iterator(_index_file_reader, index_meta, rowset_id, |
999 | 75.3k | _segment_id, _num_rows, iter); |
1000 | 75.3k | if (!iter_status.ok()) { |
1001 | 0 | if (need_binding_diagnostic) { |
1002 | 0 | const auto diagnostic = fmt::format( |
1003 | 0 | "[VariantSearchBinding] phase=index_iterator_create result=reject " |
1004 | 0 | "tablet_id={} rowset_id={} segment_id={} column={} logical_path={} " |
1005 | 0 | "index_id={} suffix={} reason={}", |
1006 | 0 | read_options.tablet_id, _rowset_id.to_string(), _segment_id, |
1007 | 0 | tablet_column.name(), |
1008 | 0 | tablet_column.has_path_info() ? tablet_column.path_info_ptr()->get_path() |
1009 | 0 | : tablet_column.name(), |
1010 | 0 | index_meta->index_id(), index_meta->get_index_suffix(), |
1011 | 0 | iter_status.to_string()); |
1012 | 0 | VLOG_DEBUG << diagnostic; |
1013 | 0 | if (read_options.stats != nullptr) { |
1014 | 0 | read_options.stats->inverted_index_stats.add_binding_diagnostic(diagnostic); |
1015 | 0 | } |
1016 | 0 | } |
1017 | 0 | return iter_status; |
1018 | 0 | } |
1019 | 75.3k | return Status::OK(); |
1020 | 75.3k | } |
1021 | 18.4E | return Status::OK(); |
1022 | 75.1k | } |
1023 | | |
1024 | | Status Segment::lookup_row_key(const Slice& key, const TabletSchema* latest_schema, |
1025 | | bool with_seq_col, bool with_rowid, RowLocation* row_location, |
1026 | 3.79M | OlapReaderStatistics* stats, std::string* encoded_seq_value) { |
1027 | 3.79M | RETURN_IF_ERROR(load_pk_index_and_bf(stats)); |
1028 | 3.79M | bool has_seq_col = latest_schema->has_sequence_col(); |
1029 | 3.79M | bool has_rowid = !latest_schema->cluster_key_uids().empty(); |
1030 | 3.79M | size_t seq_col_length = 0; |
1031 | 3.79M | if (has_seq_col) { |
1032 | 25.2k | seq_col_length = latest_schema->column(latest_schema->sequence_col_idx()).length() + 1; |
1033 | 25.2k | } |
1034 | 3.79M | size_t rowid_length = has_rowid ? PrimaryKeyIndexReader::ROW_ID_LENGTH : 0; |
1035 | | |
1036 | 3.79M | Slice key_without_seq = |
1037 | 3.79M | Slice(key.get_data(), key.get_size() - (with_seq_col ? seq_col_length : 0) - |
1038 | 3.79M | (with_rowid ? rowid_length : 0)); |
1039 | | |
1040 | 3.79M | DCHECK(_pk_index_reader != nullptr); |
1041 | 3.79M | if (!_pk_index_reader->check_present(key_without_seq)) { |
1042 | 159k | return Status::Error<ErrorCode::KEY_NOT_FOUND, false>(""); |
1043 | 159k | } |
1044 | 3.63M | bool exact_match = false; |
1045 | 3.63M | std::unique_ptr<segment_v2::IndexedColumnIterator> index_iterator; |
1046 | 3.63M | RETURN_IF_ERROR(_pk_index_reader->new_iterator(&index_iterator, stats)); |
1047 | 3.63M | auto st = index_iterator->seek_at_or_after(&key_without_seq, &exact_match); |
1048 | 3.63M | if (!st.ok() && !st.is<ErrorCode::ENTRY_NOT_FOUND>()) { |
1049 | 0 | return st; |
1050 | 0 | } |
1051 | 3.65M | if (st.is<ErrorCode::ENTRY_NOT_FOUND>() || (!has_seq_col && !has_rowid && !exact_match)) { |
1052 | 81 | return Status::Error<ErrorCode::KEY_NOT_FOUND, false>(""); |
1053 | 81 | } |
1054 | 3.63M | row_location->row_id = cast_set<uint32_t>(index_iterator->get_current_ordinal()); |
1055 | 3.63M | row_location->segment_id = _segment_id; |
1056 | 3.63M | row_location->rowset_id = _rowset_id; |
1057 | | |
1058 | 3.63M | size_t num_to_read = 1; |
1059 | 3.63M | auto index_type = DataTypeFactory::instance().create_data_type(_pk_index_reader->type(), 1, 0); |
1060 | 3.63M | auto index_column = index_type->create_column(); |
1061 | 3.63M | size_t num_read = num_to_read; |
1062 | 3.63M | RETURN_IF_ERROR(index_iterator->next_batch(&num_read, index_column)); |
1063 | 3.63M | DCHECK(num_to_read == num_read); |
1064 | | |
1065 | 3.63M | Slice sought_key = Slice(index_column->get_data_at(0).data, index_column->get_data_at(0).size); |
1066 | | |
1067 | | // user may use "ALTER TABLE tbl ENABLE FEATURE "SEQUENCE_LOAD" WITH ..." to add a hidden sequence column |
1068 | | // for a merge-on-write table which doesn't have sequence column, so `has_seq_col == true` doesn't mean |
1069 | | // data in segment has sequence column value |
1070 | 3.63M | bool segment_has_seq_col = _tablet_schema->has_sequence_col(); |
1071 | 3.63M | Slice sought_key_without_seq = Slice( |
1072 | 3.63M | sought_key.get_data(), |
1073 | 3.63M | sought_key.get_size() - (segment_has_seq_col ? seq_col_length : 0) - rowid_length); |
1074 | | |
1075 | 3.63M | if (has_seq_col) { |
1076 | | // compare key |
1077 | 25.2k | if (key_without_seq.compare(sought_key_without_seq) != 0) { |
1078 | 0 | return Status::Error<ErrorCode::KEY_NOT_FOUND, false>(""); |
1079 | 0 | } |
1080 | | |
1081 | 25.2k | if (with_seq_col && segment_has_seq_col) { |
1082 | | // compare sequence id |
1083 | 23.3k | Slice sequence_id = |
1084 | 23.3k | Slice(key.get_data() + key_without_seq.get_size() + 1, seq_col_length - 1); |
1085 | 23.3k | Slice previous_sequence_id = |
1086 | 23.3k | Slice(sought_key.get_data() + sought_key_without_seq.get_size() + 1, |
1087 | 23.3k | seq_col_length - 1); |
1088 | 23.3k | if (sequence_id.compare(previous_sequence_id) < 0) { |
1089 | 333 | return Status::Error<ErrorCode::KEY_ALREADY_EXISTS>( |
1090 | 333 | "key with higher sequence id exists"); |
1091 | 333 | } |
1092 | 23.3k | } |
1093 | 3.61M | } else if (has_rowid) { |
1094 | 59.1k | Slice sought_key_without_rowid = |
1095 | 59.1k | Slice(sought_key.get_data(), sought_key.get_size() - rowid_length); |
1096 | | // compare key |
1097 | 59.1k | if (key_without_seq.compare(sought_key_without_rowid) != 0) { |
1098 | 0 | return Status::Error<ErrorCode::KEY_NOT_FOUND, false>(""); |
1099 | 0 | } |
1100 | 59.1k | } |
1101 | | // found the key, use rowid in pk index if necessary. |
1102 | 3.63M | if (has_rowid) { |
1103 | 71.0k | Slice rowid_slice = Slice(sought_key.get_data() + sought_key_without_seq.get_size() + |
1104 | 71.0k | (segment_has_seq_col ? seq_col_length : 0) + 1, |
1105 | 71.0k | rowid_length - 1); |
1106 | 71.0k | const auto* rowid_coder = get_key_coder(FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT); |
1107 | 71.0k | RETURN_IF_ERROR(rowid_coder->decode_ascending(&rowid_slice, rowid_length, |
1108 | 71.0k | (uint8_t*)&row_location->row_id)); |
1109 | 71.0k | } |
1110 | | |
1111 | 3.63M | if (encoded_seq_value) { |
1112 | 78 | if (!segment_has_seq_col) { |
1113 | 0 | *encoded_seq_value = std::string {}; |
1114 | 78 | } else { |
1115 | | // include marker |
1116 | 78 | *encoded_seq_value = |
1117 | 78 | Slice(sought_key.get_data() + sought_key_without_seq.get_size(), seq_col_length) |
1118 | 78 | .to_string(); |
1119 | 78 | } |
1120 | 78 | } |
1121 | 3.63M | return Status::OK(); |
1122 | 3.63M | } |
1123 | | |
1124 | 0 | Status Segment::read_key_by_rowid(uint32_t row_id, std::string* key) { |
1125 | 0 | OlapReaderStatistics* null_stat = nullptr; |
1126 | 0 | RETURN_IF_ERROR(load_pk_index_and_bf(null_stat)); |
1127 | 0 | std::unique_ptr<segment_v2::IndexedColumnIterator> iter; |
1128 | 0 | RETURN_IF_ERROR(_pk_index_reader->new_iterator(&iter, null_stat)); |
1129 | | |
1130 | 0 | auto index_type = DataTypeFactory::instance().create_data_type(_pk_index_reader->type(), 1, 0); |
1131 | 0 | auto index_column = index_type->create_column(); |
1132 | 0 | RETURN_IF_ERROR(iter->seek_to_ordinal(row_id)); |
1133 | 0 | size_t num_read = 1; |
1134 | 0 | RETURN_IF_ERROR(iter->next_batch(&num_read, index_column)); |
1135 | 0 | CHECK(num_read == 1); |
1136 | | // trim row id |
1137 | 0 | if (_tablet_schema->cluster_key_uids().empty()) { |
1138 | 0 | *key = index_column->get_data_at(0).to_string(); |
1139 | 0 | } else { |
1140 | 0 | Slice sought_key = |
1141 | 0 | Slice(index_column->get_data_at(0).data, index_column->get_data_at(0).size); |
1142 | 0 | Slice sought_key_without_rowid = |
1143 | 0 | Slice(sought_key.get_data(), |
1144 | 0 | sought_key.get_size() - PrimaryKeyIndexReader::ROW_ID_LENGTH); |
1145 | 0 | *key = sought_key_without_rowid.to_string(); |
1146 | 0 | } |
1147 | 0 | return Status::OK(); |
1148 | 0 | } |
1149 | | |
1150 | | Status Segment::seek_and_read_by_rowid(const TabletSchema& schema, SlotDescriptor* slot, |
1151 | | const std::vector<uint32_t>& row_ids, |
1152 | | MutableColumnPtr& result, |
1153 | | StorageReadOptions& storage_read_options, |
1154 | 12.9k | std::unique_ptr<ColumnIterator>& iterator_hint) { |
1155 | 12.9k | if (row_ids.empty()) { |
1156 | 0 | return Status::OK(); |
1157 | 0 | } |
1158 | 12.9k | DORIS_CHECK(std::is_sorted(row_ids.begin(), row_ids.end())); |
1159 | 12.9k | DORIS_CHECK(std::adjacent_find(row_ids.begin(), row_ids.end()) == row_ids.end()); |
1160 | | // ColumnIterator::seek_and_read expects monotonically increasing row_ids without |
1161 | | // duplicates for correct ordinal scanning. Enforce this contract at the entry point. |
1162 | 12.9k | segment_v2::ColumnIteratorOptions opt { |
1163 | 12.9k | .use_page_cache = !config::disable_storage_page_cache, |
1164 | 12.9k | .file_reader = file_reader().get(), |
1165 | 12.9k | .stats = storage_read_options.stats, |
1166 | 12.9k | .io_ctx = io::IOContext {.reader_type = ReaderType::READER_QUERY, |
1167 | 12.9k | .file_cache_stats = |
1168 | 12.9k | &storage_read_options.stats->file_cache_stats}, |
1169 | 12.9k | }; |
1170 | | |
1171 | 12.9k | if (!slot->column_paths().empty()) { |
1172 | | // here need create column readers to make sure column reader is created before seek_and_read_by_rowid |
1173 | | // if segment cache miss, column reader will be created to make sure the variant column result not coredump |
1174 | 321 | RETURN_IF_ERROR(_create_column_meta_once(storage_read_options.stats)); |
1175 | | |
1176 | 321 | const auto& dt_variant = |
1177 | 321 | assert_cast<const DataTypeVariant&>(*remove_nullable(slot->type())); |
1178 | 321 | TabletColumn column = TabletColumn::create_materialized_variant_column( |
1179 | 321 | schema.column_by_uid(slot->col_unique_id()).name_lower_case(), slot->column_paths(), |
1180 | 321 | slot->col_unique_id(), dt_variant.variant_max_subcolumns_count(), |
1181 | 321 | dt_variant.enable_doc_mode()); |
1182 | 321 | auto storage_type = get_data_type_of(column, storage_read_options); |
1183 | 321 | MutableColumnPtr file_storage_column = storage_type->create_column(); |
1184 | 321 | DCHECK(storage_type != nullptr); |
1185 | | |
1186 | 321 | if (iterator_hint == nullptr) { |
1187 | 321 | RETURN_IF_ERROR(new_column_iterator(column, &iterator_hint, &storage_read_options)); |
1188 | 321 | RETURN_IF_ERROR(iterator_hint->init(opt)); |
1189 | 321 | } |
1190 | 321 | RETURN_IF_ERROR( |
1191 | 321 | iterator_hint->read_by_rowids(row_ids.data(), row_ids.size(), file_storage_column)); |
1192 | 321 | ColumnPtr source_ptr; |
1193 | | // storage may have different type with schema, so we need to cast the column |
1194 | 321 | RETURN_IF_ERROR(variant_util::cast_column( |
1195 | 321 | ColumnWithTypeAndName(file_storage_column->get_ptr(), storage_type, column.name()), |
1196 | 321 | slot->type(), &source_ptr)); |
1197 | 321 | RETURN_IF_CATCH_EXCEPTION(result->insert_range_from(*source_ptr, 0, row_ids.size())); |
1198 | 12.6k | } else { |
1199 | 12.6k | int index = (slot->col_unique_id() >= 0) ? schema.field_index(slot->col_unique_id()) |
1200 | 12.6k | : schema.field_index(slot->col_name()); |
1201 | 12.6k | if (index < 0) { |
1202 | 0 | std::stringstream ss; |
1203 | 0 | ss << "field name is invalid. field=" << slot->col_name() |
1204 | 0 | << ", field_name_to_index=" << schema.get_all_field_names(); |
1205 | 0 | return Status::InternalError(ss.str()); |
1206 | 0 | } |
1207 | 12.6k | if (iterator_hint == nullptr) { |
1208 | 12.6k | RETURN_IF_ERROR(new_column_iterator(schema.column(index), &iterator_hint, |
1209 | 12.6k | &storage_read_options)); |
1210 | 12.6k | RETURN_IF_ERROR(iterator_hint->init(opt)); |
1211 | 12.6k | } |
1212 | 12.6k | RETURN_IF_ERROR(iterator_hint->read_by_rowids(row_ids.data(), row_ids.size(), result)); |
1213 | 12.6k | } |
1214 | 12.9k | return Status::OK(); |
1215 | 12.9k | } |
1216 | | |
1217 | | Status Segment::_get_segment_footer(std::shared_ptr<SegmentFooterPB>& footer_pb, |
1218 | 4.25M | OlapReaderStatistics* stats) { |
1219 | 4.25M | std::shared_ptr<SegmentFooterPB> footer_pb_shared = _footer_pb.lock(); |
1220 | 4.25M | if (footer_pb_shared != nullptr) { |
1221 | 4.08M | footer_pb = footer_pb_shared; |
1222 | 4.08M | return Status::OK(); |
1223 | 4.08M | } |
1224 | | |
1225 | 18.4E | VLOG_DEBUG << fmt::format("Segment footer of {}:{}:{} is missing, try to load it", |
1226 | 18.4E | _file_reader->path().native(), _file_reader->size(), |
1227 | 18.4E | _file_reader->size() - 12); |
1228 | | |
1229 | 171k | StoragePageCache* segment_footer_cache = ExecEnv::GetInstance()->get_storage_page_cache(); |
1230 | 171k | DCHECK(segment_footer_cache != nullptr); |
1231 | | |
1232 | 171k | auto cache_key = get_segment_footer_cache_key(); |
1233 | | |
1234 | 171k | PageCacheHandle cache_handle; |
1235 | | |
1236 | | // Put segment footer into index page cache. |
1237 | | // Rationale: |
1238 | | // - Footer is metadata (small, parsed with indexes), not data page payload. |
1239 | | // - Using PageTypePB::INDEX_PAGE keeps it under the same eviction policy/shards |
1240 | | // as other index/metadata pages and avoids competing with DATA_PAGE budget. |
1241 | 171k | if (!segment_footer_cache->lookup(cache_key, &cache_handle, |
1242 | 171k | segment_v2::PageTypePB::INDEX_PAGE)) { |
1243 | 74.8k | RETURN_IF_ERROR(_parse_footer(footer_pb_shared, stats)); |
1244 | 74.8k | segment_footer_cache->insert(cache_key, footer_pb_shared, footer_pb_shared->ByteSizeLong(), |
1245 | 74.8k | &cache_handle, segment_v2::PageTypePB::INDEX_PAGE); |
1246 | 96.2k | } else { |
1247 | 18.4E | VLOG_DEBUG << fmt::format("Segment footer of {}:{}:{} is found in cache", |
1248 | 18.4E | _file_reader->path().native(), _file_reader->size(), |
1249 | 18.4E | _file_reader->size() - 12); |
1250 | 96.2k | } |
1251 | 171k | footer_pb_shared = cache_handle.get<std::shared_ptr<SegmentFooterPB>>(); |
1252 | 171k | _footer_pb = footer_pb_shared; |
1253 | 171k | footer_pb = footer_pb_shared; |
1254 | 171k | return Status::OK(); |
1255 | 171k | } |
1256 | | |
1257 | 179k | StoragePageCache::CacheKey Segment::get_segment_footer_cache_key() const { |
1258 | 179k | DCHECK(_file_reader != nullptr); |
1259 | | // The footer is always at the end of the segment file. |
1260 | | // The size of footer is 12. |
1261 | | // So we use the size of file minus 12 as the cache key, which is unique for each segment file. |
1262 | 179k | return get_segment_footer_cache_key(_file_reader); |
1263 | 179k | } |
1264 | | |
1265 | | StoragePageCache::CacheKey Segment::get_segment_footer_cache_key( |
1266 | 179k | const io::FileReaderSPtr& file_reader) { |
1267 | 179k | return {file_reader->path().native(), file_reader->size(), |
1268 | 179k | static_cast<int64_t>(file_reader->size() - 12)}; |
1269 | 179k | } |
1270 | | |
1271 | | } // namespace doris::segment_v2 |