imay commented on a change in pull request #1577: Support Segment for BetaRowset
URL: https://github.com/apache/incubator-doris/pull/1577#discussion_r310451747
 
 

 ##########
 File path: be/src/olap/rowset/segment_v2/segment_iterator.cpp
 ##########
 @@ -0,0 +1,259 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "olap/rowset/segment_v2/segment_iterator.h"
+
+#include <set>
+
+#include "gutil/strings/substitute.h"
+#include "olap/rowset/segment_v2/segment.h"
+#include "olap/rowset/segment_v2/column_reader.h"
+#include "olap/row_block2.h"
+#include "olap/row_cursor.h"
+#include "olap/short_key_index.h"
+
+using strings::Substitute;
+
+namespace doris {
+namespace segment_v2 {
+
+SegmentIterator::SegmentIterator(std::shared_ptr<Segment> segment,
+                                 const Schema& schema)
+        : _segment(std::move(segment)),
+        _schema(schema),
+        _column_iterators(_schema.num_columns(), nullptr) {
+}
+
+SegmentIterator::~SegmentIterator() {
+    for (auto iter : _column_iterators) {
+        delete iter;
+    }
+}
+
+Status SegmentIterator::init(const StorageReadOptions& opts) {
+    _opts = opts;
+    RETURN_IF_ERROR(_init_short_key_range());
+    RETURN_IF_ERROR(_init_column_iterators());
+    return Status::OK();
+}
+
+// This function will use input key bounds to get a row range.
+Status SegmentIterator::_init_short_key_range() {
+    _lower_rowid = 0;
+    _upper_rowid = num_rows();
+
+    // fast path for empty segment
+    if (_upper_rowid == 0) {
+        return Status::OK();
+    }
+
+    if (_opts.lower_bound == nullptr && _opts.upper_bound == nullptr) {
+        return Status::OK();
+    }
+
+    RETURN_IF_ERROR(_prepare_seek());
+
+    // init row range with short key range
+    if (_opts.upper_bound != nullptr) {
+        // If client want to read upper_bound, the include_upper_bound is 
true. So we
+        // should get the first ordinal at which key is larger than 
upper_bound.
+        // So we call _lookup_ordinal with include_upper_bound's negate
+        RETURN_IF_ERROR(_lookup_ordinal(
+                *_opts.upper_bound, !_opts.include_upper_bound, num_rows(), 
&_upper_rowid));
+    }
+    if (_upper_rowid > 0 && _opts.lower_bound != nullptr) {
+        RETURN_IF_ERROR(_lookup_ordinal(
+                *_opts.lower_bound, _opts.include_lower_bound, _upper_rowid, 
&_lower_rowid));
+    }
+
+    return Status::OK();
+}
+
+// Set up environment for the following seek.
+Status SegmentIterator::_prepare_seek() {
+    std::vector<Field> key_fields;
+    std::set<uint32_t> column_set;
+    if (_opts.lower_bound != nullptr) {
+        for (auto cid : _opts.lower_bound->schema()->column_ids()) {
+            column_set.emplace(cid);
+            key_fields.push_back(*_opts.lower_bound->schema()->column(cid));
+        }
+    }
+    if (_opts.upper_bound != nullptr) {
+        for (auto cid : _opts.upper_bound->schema()->column_ids()) {
+            if (column_set.count(cid) == 0) {
+                
key_fields.push_back(*_opts.upper_bound->schema()->column(cid));
+                column_set.emplace(cid);
+            }
+        }
+    }
+    _seek_schema.reset(new Schema(key_fields, key_fields.size()));
+    _seek_block.reset(new RowBlockV2(*_seek_schema, 1, &_arena));
+
+    // create used column iterator
+    for (auto cid : _seek_schema->column_ids()) {
+        if (_column_iterators[cid] == nullptr) {
+            RETURN_IF_ERROR(_create_column_iterator(cid, 
&_column_iterators[cid]));
+        }
+    }
+
+    return Status::OK();
+}
+
+Status SegmentIterator::_init_column_iterators() {
+    _cur_rowid = _lower_rowid;
+    if (_cur_rowid >= num_rows()) {
+        return Status::OK();
+    }
+    for (auto cid : _schema.column_ids()) {
+        if (_column_iterators[cid] == nullptr) {
+            RETURN_IF_ERROR(_create_column_iterator(cid, 
&_column_iterators[cid]));
+        }
+
+        _column_iterators[cid]->seek_to_ordinal(_cur_rowid);
+    }
+    return Status::OK();
+}
+
+Status SegmentIterator::_create_column_iterator(uint32_t cid, ColumnIterator** 
iter) {
+    _segment->new_column_iterator(cid, iter);
+    return Status::OK();
+}
+
+// Schema of lhs and rhs are different.
+// callers should assure that rhs' schema has all columns in lhs schema
+template<typename LhsRowType, typename RhsRowType>
+int compare_row_with_lhs_columns(const LhsRowType& lhs, const RhsRowType& rhs) 
{
+    for (auto cid : lhs.schema()->column_ids()) {
+        auto res = lhs.schema()->column(cid)->compare_cell(lhs.cell(cid), 
rhs.cell(cid));
+        if (res != 0) {
+            return res;
+        }
+    }
+    return 0;
+}
+
+// look up one key to get its ordinal at which can get data. 
+// 'upper_bound' is defined the max ordinal the function will search.
+// We use upper_bound to reduce search times.
+// If we find a valid ordinal, it will be set in rowid and with Status::OK()
+// If we can not find a valid key in this segment, we will set rowid to 
upper_bound
+// Otherwise return error.
+// 1. get [start, end) ordinal through short key index
+// 2. binary search to find exact ordinal that match the input condition
+// Make is_include template to reduce branch
+Status SegmentIterator::_lookup_ordinal(const RowCursor& key, bool is_include,
+                                        rowid_t upper_bound, rowid_t* rowid) {
+    std::string index_key;
+    encode_key_with_padding(&index_key, key, _segment->num_short_keys(), 
is_include);
+
+    uint32_t start_block_id = 0;
+    auto start_iter = _segment->lower_bound(index_key);
+    if (start_iter.valid()) {
+        // Because previous block may contain this key, so we should set rowid 
to
+        // last block's first row.
 
 Review comment:
   This is for all key models.
   
   What we store is **short** key, full key will be truncated. If we see an 
equal short key, there may be some same full key in previous block.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@doris.apache.org
For additional commands, e-mail: dev-h...@doris.apache.org

Reply via email to