yiguolei commented on code in PR #66472:
URL: https://github.com/apache/doris/pull/66472#discussion_r3765349114


##########
be/src/storage/segment/segment_iterator.cpp:
##########
@@ -751,63 +736,56 @@ Status SegmentIterator::_get_row_ranges_by_keys() {
 
 // Set up environment for the following seek.
 Status SegmentIterator::_prepare_seek(const StorageReadOptions::KeyRange& 
key_range) {
-    std::vector<const TabletColumn*> key_columns;
-    std::set<uint32_t> column_set;
-    if (key_range.lower_key != nullptr) {
-        for (auto cid : key_range.lower_key->schema()->column_ids()) {
-            column_set.emplace(cid);
-            key_columns.emplace_back(key_range.lower_key->column(cid));
-        }
-    }
-    if (key_range.upper_key != nullptr) {
-        for (auto cid : key_range.upper_key->schema()->column_ids()) {
-            if (column_set.count(cid) == 0) {
-                key_columns.emplace_back(key_range.upper_key->column(cid));
-                column_set.emplace(cid);
-            }
-        }
+    // Both bound keys carry a dense prefix of the tablet key columns,
+    const doris::ReadSchema* key_schema =
+            key_range.lower_key != nullptr ? key_range.lower_key->schema() : 
nullptr;
+    if (key_range.upper_key != nullptr &&
+        (key_schema == nullptr ||
+         key_range.upper_key->schema()->num_read_columns() > 
key_schema->num_read_columns())) {
+        key_schema = key_range.upper_key->schema();
+    }
+    if (key_schema == nullptr) {
+        return Status::OK();
     }
     if (!_seek_schema) {
-        std::vector<TabletColumnPtr> cols;
-        cols.reserve(key_columns.size());
-        for (const TabletColumn* col : key_columns) {
-            cols.emplace_back(std::make_shared<TabletColumn>(*col));
-        }
-        std::vector<uint32_t> column_ids(cols.size());
-        std::iota(column_ids.begin(), column_ids.end(), 0);
-        _seek_schema = std::make_unique<Schema>(cols, column_ids);
+        _seek_schema = std::make_unique<ReadSchema>(*key_schema);
     }
     // todo(wb) need refactor here, when using pk to search, _seek_block is 
useless
-    if (_seek_block.size() == 0) {
-        _seek_block.resize(_seek_schema->num_column_ids());
-        int i = 0;
-        for (auto cid : _seek_schema->column_ids()) {
-            auto column_desc = _seek_schema->column(cid);
-            _seek_block[i] = 
Schema::get_data_type_ptr(*column_desc)->create_column();
-            i++;
+    if (_seek_block.empty()) {
+        _seek_block.resize(_seek_schema->num_read_columns());
+        for (size_t i = 0; i < _seek_schema->num_read_columns(); ++i) {
+            _seek_block[i] = _seek_schema->data_type(i)->create_column();
         }
     }
 
-    // create used column iterator
-    for (auto cid : _seek_schema->column_ids()) {
-        if (_column_iterators[cid] == nullptr) {
-            // TODO: Do we need this?
-            if (_virtual_column_exprs.contains(cid)) {
-                _column_iterators[cid] = 
std::make_unique<VirtualColumnIterator>();
-                continue;
-            }
-
-            
RETURN_IF_ERROR(_segment->new_column_iterator(_opts.tablet_schema->column(cid),
-                                                          
&_column_iterators[cid], &_opts,
-                                                          
&_variant_sparse_column_cache));
-            ColumnIteratorOptions iter_opts {
-                    .use_page_cache = _opts.use_page_cache,
-                    .file_reader = _file_reader.get(),
-                    .stats = _opts.stats,
-                    .io_ctx = _opts.io_ctx,
-            };
-            RETURN_IF_ERROR(_column_iterators[cid]->init(iter_opts));
+    // The seek key schema covers the leading key columns of the TABLET schema;
+    // a key column may not be part of the read schema at all. Reuse the shared
+    // iterator when it is, otherwise the seek path owns a dedicated one.
+    if (_seek_column_iterators.empty()) {
+        _seek_column_iterators.resize(_seek_schema->num_read_columns(), 
nullptr);
+    }
+    for (size_t i = 0; i < _seek_schema->num_read_columns(); ++i) {
+        if (_seek_column_iterators[i] != nullptr) {
+            continue;
+        }
+        const TabletColumn* col = _seek_schema->column(i);
+        int32_t read_ordinal = _schema->ordinal_by_column(*col);
+        if (read_ordinal >= 0 && _column_iterators[read_ordinal] != nullptr) {

Review Comment:
   如果read ordinal < 0  怎么杨?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to