chaoyli commented on a change in pull request #1515: Add storage rowwise iterator URL: https://github.com/apache/incubator-doris/pull/1515#discussion_r305620857
########## File path: be/src/olap/generic_iterators.cpp ########## @@ -0,0 +1,346 @@ +// 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/iterators.h" + +#include <queue> + +#include "olap/row_block2.h" +#include "util/arena.h" + +namespace doris { + +// This iterator will generate ordered data. For example for schema +// (int, int) this iterator will generator data like +// (0, 1), (1, 2), (2, 4), (3, 4)... +// +// Usage: +// SchemaV2 schema; +// AutoIncrementIterator iter(schema, 1000); +// StorageReadOptions opts; +// RETURN_IF_ERROR(iter.init(opts)); +// RowBlockV2 block; +// do { +// st = iter.next_batch(&block); +// } while (st.ok()); +class AutoIncrementIterator : public RowwiseIterator { +public: + // Will generate num_rows rows in total + AutoIncrementIterator(const SchemaV2& schema, size_t num_rows) + : _schema(schema), _num_rows(num_rows), _rows_returned(0) { + } + ~AutoIncrementIterator() override { } + + // NOTE: Currently, this function will ignore StorageReadOptions + Status init(const StorageReadOptions& opts) override; + Status next_batch(RowBlockV2* block) override; + + const SchemaV2& schema() const override { return _schema; } +private: + SchemaV2 _schema; + size_t _num_rows; + size_t _rows_returned; +}; + +Status AutoIncrementIterator::init(const StorageReadOptions& opts) { + return Status::OK(); +} + +Status AutoIncrementIterator::next_batch(RowBlockV2* block) { + int row_idx = 0; + while (row_idx < block->capacity() && _rows_returned < _num_rows) { + RowBlockRow row = block->row(row_idx); + + for (int i = 0; i < _schema.column_schemas().size(); ++i) { + row.set_is_null(i, false); + auto& col_schema = _schema.column_schemas()[i]; + switch (col_schema.type()) { + case OLAP_FIELD_TYPE_SMALLINT: + *(int16_t*)row.cell_ptr(i) = _rows_returned + i; + break; + case OLAP_FIELD_TYPE_INT: + *(int32_t*)row.cell_ptr(i) = _rows_returned + i; + break; + case OLAP_FIELD_TYPE_BIGINT: + *(int64_t*)row.cell_ptr(i) = _rows_returned + i; + break; + case OLAP_FIELD_TYPE_FLOAT: + *(float*)row.cell_ptr(i) = _rows_returned + i; + break; + case OLAP_FIELD_TYPE_DOUBLE: + *(double*)row.cell_ptr(i) = _rows_returned + i; + break; + default: + break; + } + } + row_idx++; + _rows_returned++; + } + block->resize(row_idx); + if (row_idx > 0) { + return Status::OK(); + } + return Status::EndOfFile("End of AutoIncrementIterator"); +} + +// Used to store merge state for a MergeIterator input. +// This class will iterate all data from internal iterator +// through client call advance(). +// Usage: +// MergeContext ctx(iter); +// RETURN_IF_ERROR(ctx.init()); +// while (ctx.valid()) { +// visit(ctx.current_row()); +// RETURN_IF_ERROR(ctx.advance()); +// } +class MergeContext { +public: + // This class don't take iter's ownership, client should delete it + MergeContext(RowwiseIterator* iter) + : _iter(iter), _block(iter->schema(), 1024, &_arena) { + } + + // Intialize this context and will prepare data for current_row() + Status init(const StorageReadOptions& opts); + + // Return current row which internal row index points to + // And this function won't make internal index advance. + // Before call this function, Client must assure that + // has_remaining() return true + RowBlockRow current_row() const { + return RowBlockRow(&_block, _index_in_block); + } + + // Advance internal row index to next valid row + // Return error if error happens + // Don't call this when valid() is false, action is undefined + Status advance(); + + // Return if has remaining data in this context. + // Only when this function return true, current_row() + // will return a valid row + bool valid() const { return _valid; } + +private: + // Load next block into _block + Status _load_next_block(); + +private: + RowwiseIterator* _iter; + Arena _arena; + // used to store data load from iterator + RowBlockV2 _block; + + bool _valid = false; + size_t _index_in_block = 0; Review comment: Block has the position, _index_in_block may be redundant. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
