decster commented on a change in pull request #3580: URL: https://github.com/apache/incubator-doris/pull/3580#discussion_r426446742
########## File path: be/src/olap/memory/typed_column_reader.h ########## @@ -0,0 +1,221 @@ +// 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. + +#pragma once + +#include "olap/memory/column_reader.h" +#include "util/hash_util.hpp" + +namespace doris { +namespace memory { + +// Those methods need to be shared by reader/writer, so extract them as template functions + +// Get Column's storage cell address by rowid. +template <class RW, class T, bool Nullable, class ST> Review comment: fixed ########## File path: be/src/olap/memory/typed_column_reader.h ########## @@ -0,0 +1,221 @@ +// 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. + +#pragma once + +#include "olap/memory/column_reader.h" +#include "util/hash_util.hpp" + +namespace doris { +namespace memory { + +// Those methods need to be shared by reader/writer, so extract them as template functions + +// Get Column's storage cell address by rowid. +template <class RW, class T, bool Nullable, class ST> +inline const void* TypedColumnGet(const RW& rw, const uint32_t rid) { + for (ssize_t i = rw._deltas.size() - 1; i >= 0; i--) { + ColumnDelta* pdelta = rw._deltas[i]; + uint32_t pos = pdelta->find_idx(rid); + if (pos != DeltaIndex::npos) { + if (Nullable) { + bool isnull = pdelta->nulls() && pdelta->nulls().as<bool>()[pos]; + if (isnull) { + return nullptr; + } else { + return &(pdelta->data().template as<ST>()[pos]); + } + } else { + return &(pdelta->data().template as<ST>()[pos]); + } + } + } + uint32_t bid = rid >> 16; + DCHECK(bid < rw._base->size()); + uint32_t idx = rid & 0xffff; + DCHECK(idx * sizeof(ST) < (*rw._base)[bid]->data().bsize()); + if (Nullable) { + bool isnull = (*rw._base)[bid]->is_null(idx); + if (isnull) { + return nullptr; + } else { + return &((*rw._base)[bid]->data().template as<ST>()[idx]); + } + } else { + return &((*rw._base)[bid]->data().template as<ST>()[idx]); + } +} + +// Get a cell's hashcode of a typed array +template <class T, class ST> +inline uint64_t TypedColumnHashcode(const void* rhs, size_t rhs_idx) { + if (std::is_same<T, ST>::value) { + const T* prhs = ((const T*)rhs) + rhs_idx; + return HashUtil::fnv_hash64(prhs, sizeof(T), 0); + } else { + // TODO: support other type's hash + return 0; + } +} + +// Compare equality of a typed array's cell with this column's cell +template <class RW, class T, bool Nullable, class ST> +bool TypedColumnEquals(const RW& rw, const uint32_t rid, const void* rhs, size_t rhs_idx) { + const T& rhs_value = ((const T*)rhs)[rhs_idx]; + for (ssize_t i = rw._deltas.size() - 1; i >= 0; i--) { + ColumnDelta* pdelta = rw._deltas[i]; + uint32_t pos = pdelta->find_idx(rid); + if (pos != DeltaIndex::npos) { + if (Nullable) { + CHECK(false) << "only used for key column"; + return false; + } else { + return (pdelta->data().template as<T>()[pos]) == rhs_value; + } + } + } + uint32_t bid = rid >> 16; + DCHECK(bid < rw._base->size()); + uint32_t idx = rid & 0xffff; + DCHECK(idx * sizeof(ST) < (*rw._base)[bid]->data().bsize()); + if (Nullable) { + CHECK(false) << "only used for key column"; + return false; + } else { + DCHECK(rhs); + return ((*rw._base)[bid]->data().template as<T>()[idx]) == rhs_value; + } +} + +// ColumnReader typed implementations +// currently only works for int8/int16/int32/int64/int128/float/double +// +// Template type meanings: +// T: column type used for interface +// Nullable: whether column type is nullable +// ST: column type used for internal storage +// +// For fixed size scalar types(int/float), T should be the same as ST +// For var size typse(string), when using dict encoding, ST may be integer +// +// TODO: add string and other varlen type support +template <class T, bool Nullable = false, class ST = T> +class TypedColumnReader : public ColumnReader { +public: + TypedColumnReader(Column* column, uint64_t version, uint64_t real_version, + vector<ColumnDelta*>&& deltas) + : _column(column), + _base(&_column->_base), + _version(version), + _real_version(real_version), + _deltas(std::move(deltas)) {} + + const void* get(const uint32_t rid) const { + return TypedColumnGet<TypedColumnReader<T, Nullable, ST>, T, Nullable, ST>(*this, rid); + } + + Status get_block(size_t nrows, size_t bid, ColumnBlockHolder* cbh) const { Review comment: fixed ---------------------------------------------------------------- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
