You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by al...@apache.org on 2025/06/12 06:00:27 UTC
(kudu) 01/02: [cfile] type encoding clean-up
This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
commit af92e28f0057b89f1c5edcad2698fc2f2c33ae7c
Author: Alexey Serbin <al...@apache.org>
AuthorDate: Tue Jun 10 11:49:00 2025 -0700
[cfile] type encoding clean-up
Since I'm updating the code in type_encodings.{h,cc} in follow-up
changelists, it's a good idea to clean up the corresponding code first,
and this patch does exactly so:
* fix const-correctness of TypeEncodingResolver's methods
* use std::array instead of std::unordered_map for storing information
on default encoding
* simplify the signature of encoder/decoder factory methods
This changelist doesn't contains any functional modifications.
Change-Id: Ibbb823562e27d7a22c39e471b2bac48785c654b2
Reviewed-on: http://gerrit.cloudera.org:8080/23007
Tested-by: Alexey Serbin <al...@apache.org>
Reviewed-by: Abhishek Chennaka <ac...@cloudera.com>
---
src/kudu/cfile/cfile_reader.cc | 4 +-
src/kudu/cfile/cfile_writer.cc | 11 ++--
src/kudu/cfile/encoding-test.cc | 8 +--
src/kudu/cfile/type_encodings.cc | 100 ++++++++++++++++++++-----------------
src/kudu/cfile/type_encodings.h | 35 +++++++------
src/kudu/master/catalog_manager.cc | 6 +--
6 files changed, 83 insertions(+), 81 deletions(-)
diff --git a/src/kudu/cfile/cfile_reader.cc b/src/kudu/cfile/cfile_reader.cc
index 62be31745..93c8d72fb 100644
--- a/src/kudu/cfile/cfile_reader.cc
+++ b/src/kudu/cfile/cfile_reader.cc
@@ -978,8 +978,8 @@ Status CFileIterator::ReadCurrentDataBlock(const IndexTreeIterator& idx_iter,
prep_block->rle_bitmap.size());
}
- RETURN_NOT_OK(reader_->type_encoding_info()->CreateBlockDecoder(
- &prep_block->dblk_, std::move(data_block), this));
+ prep_block->dblk_ = reader_->type_encoding_info()->CreateBlockDecoder(
+ std::move(data_block), this);
RETURN_NOT_OK_PREPEND(prep_block->dblk_->ParseHeader(),
Substitute("unable to decode data block header in block $0 ($1)",
diff --git a/src/kudu/cfile/cfile_writer.cc b/src/kudu/cfile/cfile_writer.cc
index 54251f195..433d70f3e 100644
--- a/src/kudu/cfile/cfile_writer.cc
+++ b/src/kudu/cfile/cfile_writer.cc
@@ -133,13 +133,13 @@ CFileWriter::CFileWriter(WriterOptions options,
is_nullable_(is_nullable),
typeinfo_(typeinfo),
state_(kWriterInitialized) {
- EncodingType encoding = options_.storage_attributes.encoding;
- Status s = TypeEncodingInfo::Get(typeinfo_, encoding, &type_encoding_info_);
- if (PREDICT_FALSE(!s.ok())) {
+ const EncodingType encoding = options_.storage_attributes.encoding;
+ if (auto s = TypeEncodingInfo::Get(typeinfo_, encoding, &type_encoding_info_);
+ PREDICT_FALSE(!s.ok())) {
// TODO(af): we should somehow pass some contextual info about the
// tablet here.
WARN_NOT_OK(s, "Falling back to default encoding");
- s = TypeEncodingInfo::Get(typeinfo,
+ s = TypeEncodingInfo::Get(typeinfo_,
TypeEncodingInfo::GetDefaultEncoding(typeinfo_),
&type_encoding_info_);
CHECK_OK(s);
@@ -212,8 +212,7 @@ Status CFileWriter::Start() {
}
RETURN_NOT_OK_PREPEND(WriteRawData(header_slices), "Couldn't write header");
-
- RETURN_NOT_OK(type_encoding_info_->CreateBlockBuilder(&data_block_, &options_));
+ data_block_ = type_encoding_info_->CreateBlockBuilder(&options_);
if (is_nullable_) {
size_t nrows = ((options_.storage_attributes.cfile_block_size + typeinfo_->size() - 1) /
diff --git a/src/kudu/cfile/encoding-test.cc b/src/kudu/cfile/encoding-test.cc
index 63df32c09..93b8e631a 100644
--- a/src/kudu/cfile/encoding-test.cc
+++ b/src/kudu/cfile/encoding-test.cc
@@ -99,9 +99,7 @@ class TestEncoding : public KuduTest {
EncodingType encoding) {
const TypeEncodingInfo* tei;
CHECK_OK(TypeEncodingInfo::Get(GetTypeInfo(type), encoding, &tei));
- unique_ptr<BlockBuilder> bb;
- CHECK_OK(tei->CreateBlockBuilder(&bb, &default_write_options_));
- return bb;
+ return tei->CreateBlockBuilder(&default_write_options_);
}
static unique_ptr<BlockDecoder> CreateBlockDecoderOrDie(DataType type,
@@ -109,9 +107,7 @@ class TestEncoding : public KuduTest {
scoped_refptr<BlockHandle> block) {
const TypeEncodingInfo* tei;
CHECK_OK(TypeEncodingInfo::Get(GetTypeInfo(type), encoding, &tei));
- unique_ptr<BlockDecoder> bd;
- CHECK_OK(tei->CreateBlockDecoder(&bd, std::move(block), /*parent_cfile_iter=*/nullptr));
- return bd;
+ return tei->CreateBlockDecoder(std::move(block), /*parent_cfile_iter=*/nullptr);
}
// Insert a given number of strings into the provided BlockBuilder.
diff --git a/src/kudu/cfile/type_encodings.cc b/src/kudu/cfile/type_encodings.cc
index 9aa501b76..25d7f2922 100644
--- a/src/kudu/cfile/type_encodings.cc
+++ b/src/kudu/cfile/type_encodings.cc
@@ -16,11 +16,14 @@
// under the License.
#include "kudu/cfile/type_encodings.h"
+#include <array>
#include <cstddef>
#include <memory>
#include <unordered_map>
#include <utility>
+#include <glog/logging.h>
+
#include "kudu/cfile/binary_dict_block.h" // IWYU pragma: keep
#include "kudu/cfile/binary_plain_block.h" // IWYU pragma: keep
#include "kudu/cfile/binary_prefix_block.h" // IWYU pragma: keep
@@ -31,12 +34,15 @@
#include "kudu/cfile/plain_block.h" // IWYU pragma: keep
#include "kudu/cfile/rle_block.h" // IWYU pragma: keep
#include "kudu/common/types.h"
+#include "kudu/gutil/map-util.h"
#include "kudu/gutil/port.h"
#include "kudu/gutil/ref_counted.h"
#include "kudu/gutil/singleton.h"
#include "kudu/gutil/strings/substitute.h"
+using std::array;
using std::make_pair;
+using std::make_unique;
using std::pair;
using std::unique_ptr;
using std::unordered_map;
@@ -48,18 +54,16 @@ namespace cfile {
// classes.
template<class Builder, class Decoder>
struct EncodingTraits {
- static Status CreateBlockBuilder(unique_ptr<BlockBuilder>* bb, const WriterOptions* options) {
- bb->reset(new Builder(options));
- return Status::OK();
+ static unique_ptr<BlockBuilder> CreateBlockBuilder(const WriterOptions* options) {
+ return make_unique<Builder>(options);
}
- static Status CreateBlockDecoder(unique_ptr<BlockDecoder>* bd,
+ static unique_ptr<BlockDecoder> CreateBlockDecoder(
// https://bugs.llvm.org/show_bug.cgi?id=44598
// NOLINTNEXTLINE(performance-unnecessary-value-param)
scoped_refptr<BlockHandle> block,
CFileIterator* /*parent_cfile_iter*/) {
- bd->reset(new Decoder(std::move(block)));
- return Status::OK();
+ return make_unique<Decoder>(std::move(block));
}
};
@@ -68,9 +72,9 @@ struct DataTypeEncodingTraits {};
// Instantiate this template to get static access to the type traits.
template<DataType Type, EncodingType Encoding> struct TypeEncodingTraits
- : public DataTypeEncodingTraits<Type, Encoding> {
+ : public DataTypeEncodingTraits<Type, Encoding> {
- static const EncodingType kEncodingType = Encoding;
+ static constexpr const EncodingType kEncodingType = Encoding;
};
// Generic, fallback, partial specialization that should work for all
@@ -111,11 +115,9 @@ struct DataTypeEncodingTraits<BINARY, PREFIX_ENCODING>
template<>
struct DataTypeEncodingTraits<BINARY, DICT_ENCODING>
: public EncodingTraits<BinaryDictBlockBuilder, BinaryDictBlockDecoder> {
- static Status CreateBlockDecoder(unique_ptr<BlockDecoder>* bd,
- scoped_refptr<BlockHandle> block,
- CFileIterator* parent_cfile_iter) {
- bd->reset(new BinaryDictBlockDecoder(std::move(block), parent_cfile_iter));
- return Status::OK();
+ static unique_ptr<BlockDecoder> CreateBlockDecoder(
+ scoped_refptr<BlockHandle> block, CFileIterator* parent_cfile_iter) {
+ return make_unique<BinaryDictBlockDecoder>(std::move(block), parent_cfile_iter);
}
};
@@ -124,25 +126,24 @@ struct DataTypeEncodingTraits<IntType, RLE>
: public EncodingTraits<RleIntBlockBuilder<IntType>, RleIntBlockDecoder<IntType>> {};
template<typename TypeEncodingTraitsClass>
-TypeEncodingInfo::TypeEncodingInfo(TypeEncodingTraitsClass /*t*/)
+TypeEncodingInfo::TypeEncodingInfo(TypeEncodingTraitsClass /*unused*/)
: encoding_type_(TypeEncodingTraitsClass::kEncodingType),
create_builder_func_(TypeEncodingTraitsClass::CreateBlockBuilder),
create_decoder_func_(TypeEncodingTraitsClass::CreateBlockDecoder) {
}
-Status TypeEncodingInfo::CreateBlockDecoder(unique_ptr<BlockDecoder>* bd,
- scoped_refptr<BlockHandle> block,
- CFileIterator* parent_cfile_iter) const {
- return create_decoder_func_(bd, std::move(block), parent_cfile_iter);
+unique_ptr<BlockDecoder> TypeEncodingInfo::CreateBlockDecoder(
+ scoped_refptr<BlockHandle> block, CFileIterator* parent_cfile_iter) const {
+ return create_decoder_func_(std::move(block), parent_cfile_iter);
}
-Status TypeEncodingInfo::CreateBlockBuilder(
- unique_ptr<BlockBuilder>* bb, const WriterOptions* options) const {
- return create_builder_func_(bb, options);
+unique_ptr<BlockBuilder> TypeEncodingInfo::CreateBlockBuilder(
+ const WriterOptions* options) const {
+ return create_builder_func_(options);
}
struct EncodingMapHash {
- size_t operator()(pair<DataType, EncodingType> pair) const {
+ size_t operator()(const pair<DataType, EncodingType>& pair) const noexcept {
return (pair.first << 5) + pair.second;
}
};
@@ -152,23 +153,25 @@ struct EncodingMapHash {
// becomes the default encoding for the type.
class TypeEncodingResolver {
public:
- Status GetTypeEncodingInfo(DataType t, EncodingType e,
- const TypeEncodingInfo** out) {
+ Status GetTypeEncodingInfo(DataType t,
+ EncodingType e,
+ const TypeEncodingInfo** out) const {
if (e == AUTO_ENCODING) {
e = GetDefaultEncoding(t);
}
- const TypeEncodingInfo *type_info = mapping_[make_pair(t, e)].get();
- if (PREDICT_FALSE(type_info == nullptr)) {
+ const TypeEncodingInfo* info = FindPointeeOrNull(mapping_, make_pair(t, e));
+ if (PREDICT_FALSE(info == nullptr)) {
return Status::NotSupported(
strings::Substitute("encoding $1 not supported for type $0",
DataType_Name(t),
EncodingType_Name(e)));
}
- *out = type_info;
+ *out = info;
return Status::OK();
}
- const EncodingType GetDefaultEncoding(DataType t) {
+ EncodingType GetDefaultEncoding(DataType t) const {
+ DCHECK_LE(t, kDataTypeMaxIdx);
return default_mapping_[t];
}
@@ -176,7 +179,10 @@ class TypeEncodingResolver {
// the first encoder/decoder to be
// added to the mapping becomes the default
private:
+ friend class Singleton<TypeEncodingResolver>;
+
TypeEncodingResolver() {
+ default_mapping_.fill(UNKNOWN_ENCODING);
AddMapping<UINT8, BIT_SHUFFLE>();
AddMapping<UINT8, PLAIN_ENCODING>();
AddMapping<UINT8, RLE>();
@@ -216,36 +222,38 @@ class TypeEncodingResolver {
// AddMapping<INT128, RLE>();
}
- template<DataType type, EncodingType encoding> void AddMapping() {
- TypeEncodingTraits<type, encoding> traits;
- // The first call to AddMapping() for a given data-type is always the default one.
- // emplace() will no-op if the data-type is already present, so we can blindly
- // call emplace() here(i.e. no need to call find() to check before inserting)
- default_mapping_.emplace(type, encoding);
- mapping_.emplace(make_pair(type, encoding),
- unique_ptr<TypeEncodingInfo>(new TypeEncodingInfo(traits)));
+ template<DataType type, EncodingType encoding>
+ void AddMapping() {
+ static_assert(type <= kDataTypeMaxIdx, "unexpected type for encoder mapping");
+ if (default_mapping_[type] == UNKNOWN_ENCODING) {
+ default_mapping_[type] = encoding;
+ }
+ const auto ins_info = mapping_.emplace(
+ make_pair(type, encoding),
+ new TypeEncodingInfo(TypeEncodingTraits<type, encoding>()));
+ DCHECK(ins_info.second);
}
- unordered_map<pair<DataType, EncodingType>,
- unique_ptr<const TypeEncodingInfo>,
- EncodingMapHash > mapping_;
+ static constexpr const size_t kDataTypeMaxIdx = DataType::INT128;
+ array<EncodingType, kDataTypeMaxIdx + 1> default_mapping_;
- unordered_map<DataType, EncodingType, std::hash<size_t> > default_mapping_;
+ unordered_map<pair<DataType, EncodingType>,
+ unique_ptr<const TypeEncodingInfo>,
+ EncodingMapHash> mapping_;
- friend class Singleton<TypeEncodingResolver>;
DISALLOW_COPY_AND_ASSIGN(TypeEncodingResolver);
};
Status TypeEncodingInfo::Get(const TypeInfo* typeinfo,
EncodingType encoding,
const TypeEncodingInfo** out) {
- return Singleton<TypeEncodingResolver>::get()->GetTypeEncodingInfo(typeinfo->physical_type(),
- encoding,
- out);
+ return Singleton<TypeEncodingResolver>::get()->GetTypeEncodingInfo(
+ typeinfo->physical_type(), encoding, out);
}
-const EncodingType TypeEncodingInfo::GetDefaultEncoding(const TypeInfo* typeinfo) {
- return Singleton<TypeEncodingResolver>::get()->GetDefaultEncoding(typeinfo->physical_type());
+EncodingType TypeEncodingInfo::GetDefaultEncoding(const TypeInfo* typeinfo) {
+ return Singleton<TypeEncodingResolver>::get()->GetDefaultEncoding(
+ typeinfo->physical_type());
}
} // namespace cfile
diff --git a/src/kudu/cfile/type_encodings.h b/src/kudu/cfile/type_encodings.h
index 71e5934a8..ee6911c59 100644
--- a/src/kudu/cfile/type_encodings.h
+++ b/src/kudu/cfile/type_encodings.h
@@ -39,41 +39,40 @@ struct WriterOptions;
// Mimicked after common::TypeInfo et al
class TypeEncodingInfo {
public:
- static Status Get(const TypeInfo* typeinfo, EncodingType encoding, const TypeEncodingInfo** out);
+ static Status Get(const TypeInfo* typeinfo,
+ EncodingType encoding,
+ const TypeEncodingInfo** out);
- static const EncodingType GetDefaultEncoding(const TypeInfo* typeinfo);
+ static EncodingType GetDefaultEncoding(const TypeInfo* typeinfo);
- EncodingType encoding_type() const { return encoding_type_; }
+ EncodingType encoding_type() const {
+ return encoding_type_;
+ }
- Status CreateBlockBuilder(std::unique_ptr<BlockBuilder>* bb, const WriterOptions* options) const;
+ std::unique_ptr<BlockBuilder> CreateBlockBuilder(const WriterOptions* options) const;
- // Create a BlockDecoder. Sets *bd to the newly created decoder,
- // if successful, otherwise returns a non-OK Status.
- //
- // Input parent_cfile_iter parameter will only be used in case of dictionary encoding.
- Status CreateBlockDecoder(std::unique_ptr<BlockDecoder>* bd,
- scoped_refptr<BlockHandle> block,
- CFileIterator* parent_cfile_iter) const;
+ // Create a BlockDecoder. Returns the newly created decoder.
+ // The 'parent_cfile_iter' parameter is only used in case of dictionary encoding.
+ std::unique_ptr<BlockDecoder> CreateBlockDecoder(scoped_refptr<BlockHandle> block,
+ CFileIterator* parent_cfile_iter) const;
private:
friend class TypeEncodingResolver;
template<typename TypeEncodingTraitsClass>
- explicit TypeEncodingInfo(TypeEncodingTraitsClass t);
+ explicit TypeEncodingInfo(TypeEncodingTraitsClass unused);
- EncodingType encoding_type_;
+ const EncodingType encoding_type_;
- typedef Status (*CreateBlockBuilderFunc)(std::unique_ptr<BlockBuilder>*, const WriterOptions*);
+ typedef std::unique_ptr<BlockBuilder> (*CreateBlockBuilderFunc)(const WriterOptions*);
const CreateBlockBuilderFunc create_builder_func_;
- typedef Status (*CreateBlockDecoderFunc)(std::unique_ptr<BlockDecoder>*,
- scoped_refptr<BlockHandle>,
- CFileIterator*);
+ typedef std::unique_ptr<BlockDecoder> (*CreateBlockDecoderFunc)(
+ scoped_refptr<BlockHandle>, CFileIterator*);
const CreateBlockDecoderFunc create_decoder_func_;
DISALLOW_COPY_AND_ASSIGN(TypeEncodingInfo);
};
-
} // namespace cfile
} // namespace kudu
diff --git a/src/kudu/master/catalog_manager.cc b/src/kudu/master/catalog_manager.cc
index 403d91f89..4457aa824 100644
--- a/src/kudu/master/catalog_manager.cc
+++ b/src/kudu/master/catalog_manager.cc
@@ -1887,9 +1887,9 @@ Status ValidateClientSchema(const optional<string>& name,
}
// Check that the encodings are valid for the specified types.
- const TypeEncodingInfo *dummy;
- Status s = TypeEncodingInfo::Get(ti, col.attributes().encoding, &dummy);
- if (!s.ok()) {
+ const TypeEncodingInfo* dummy;
+ if (const auto s = TypeEncodingInfo::Get(ti, col.attributes().encoding, &dummy);
+ PREDICT_FALSE(!s.ok())) {
return s.CloneAndPrepend(Substitute("invalid encoding for column '$0'", col.name()));
}
}