From 81f4bc7b4c937703db0a6ea47ad7f22a29155c62 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 27 Dec 2020 20:44:45 +0300 Subject: [PATCH] Updated SSDCacheDictionary to new interface --- src/Dictionaries/CacheDictionary.h | 2 - src/Dictionaries/SSDCacheDictionary.cpp | 266 ++++++++++++++---------- src/Dictionaries/SSDCacheDictionary.h | 79 +------ 3 files changed, 163 insertions(+), 184 deletions(-) diff --git a/src/Dictionaries/CacheDictionary.h b/src/Dictionaries/CacheDictionary.h index 5833f623517..70bea884ae4 100644 --- a/src/Dictionaries/CacheDictionary.h +++ b/src/Dictionaries/CacheDictionary.h @@ -133,8 +133,6 @@ public: template using ResultArrayType = std::conditional_t, DecimalPaddedPODArray, PaddedPODArray>; - void getString(const std::string & attribute_name, const PaddedPODArray & ids, const String & def, ColumnString * const out) const; - BlockInputStreamPtr getBlockInputStream(const Names & column_names, size_t max_block_size) const override; private: diff --git a/src/Dictionaries/SSDCacheDictionary.cpp b/src/Dictionaries/SSDCacheDictionary.cpp index 030f95da2ab..23507792f7e 100644 --- a/src/Dictionaries/SSDCacheDictionary.cpp +++ b/src/Dictionaries/SSDCacheDictionary.cpp @@ -23,6 +23,7 @@ #include #include +#include namespace ProfileEvents { @@ -1327,93 +1328,146 @@ SSDCacheDictionary::SSDCacheDictionary( createAttributes(); } -#define DECLARE(TYPE) \ - void SSDCacheDictionary::get##TYPE( \ - const std::string & attribute_name, const PaddedPODArray & ids, ResultArrayType & out) const \ - { \ - const auto index = getAttributeIndex(attribute_name); \ - checkAttributeType(this, attribute_name, dict_struct.attributes[index].underlying_type, AttributeUnderlyingType::ut##TYPE); \ - const auto null_value = std::get(null_values[index]); /* NOLINT */ \ - getItemsNumberImpl(index, ids, out, [&](const size_t) { return null_value; }); /* NOLINT */ \ - } +ColumnPtr SSDCacheDictionary::getColumn( + const std::string & attribute_name, + const DataTypePtr &, + const Columns & key_columns, + const DataTypes &, + const ColumnPtr default_untyped) const +{ + ColumnPtr result; - DECLARE(UInt8) - DECLARE(UInt16) - DECLARE(UInt32) - DECLARE(UInt64) - DECLARE(UInt128) - DECLARE(Int8) - DECLARE(Int16) - DECLARE(Int32) - DECLARE(Int64) - DECLARE(Float32) - DECLARE(Float64) - DECLARE(Decimal32) - DECLARE(Decimal64) - DECLARE(Decimal128) -#undef DECLARE + PaddedPODArray backup_storage; + const auto& ids = getColumnDataAsPaddedPODArray(this, key_columns.front(), backup_storage); + + const auto index = getAttributeIndex(attribute_name); -#define DECLARE(TYPE) \ - void SSDCacheDictionary::get##TYPE( \ - const std::string & attribute_name, \ - const PaddedPODArray & ids, \ - const PaddedPODArray & def, \ - ResultArrayType & out) const \ - { \ - const auto index = getAttributeIndex(attribute_name); \ - checkAttributeType(this, attribute_name, dict_struct.attributes[index].underlying_type, AttributeUnderlyingType::ut##TYPE); \ - getItemsNumberImpl( \ - index, \ - ids, \ - out, \ - [&](const size_t row) { return def[row]; }); \ - } - DECLARE(UInt8) - DECLARE(UInt16) - DECLARE(UInt32) - DECLARE(UInt64) - DECLARE(UInt128) - DECLARE(Int8) - DECLARE(Int16) - DECLARE(Int32) - DECLARE(Int64) - DECLARE(Float32) - DECLARE(Float64) - DECLARE(Decimal32) - DECLARE(Decimal64) - DECLARE(Decimal128) -#undef DECLARE + /// TODO: Check that attribute type is same as result type + /// TODO: Check if const will work as expected -#define DECLARE(TYPE) \ - void SSDCacheDictionary::get##TYPE( \ - const std::string & attribute_name, \ - const PaddedPODArray & ids, \ - const TYPE def, \ - ResultArrayType & out) const \ - { \ - const auto index = getAttributeIndex(attribute_name); \ - checkAttributeType(this, attribute_name, dict_struct.attributes[index].underlying_type, AttributeUnderlyingType::ut##TYPE); \ - getItemsNumberImpl( \ - index, \ - ids, \ - out, \ - [&](const size_t) { return def; }); \ - } - DECLARE(UInt8) - DECLARE(UInt16) - DECLARE(UInt32) - DECLARE(UInt64) - DECLARE(UInt128) - DECLARE(Int8) - DECLARE(Int16) - DECLARE(Int32) - DECLARE(Int64) - DECLARE(Float32) - DECLARE(Float64) - DECLARE(Decimal32) - DECLARE(Decimal64) - DECLARE(Decimal128) -#undef DECLARE + auto type_call = [&](const auto &dictionary_attribute_type) + { + using Type = std::decay_t; + using AttributeType = typename Type::AttributeType; + + auto identifiers_size = ids.size(); + + if constexpr (std::is_same_v) + { + auto column_string = ColumnString::create(); + + if (default_untyped != nullptr) + { + if (const auto default_col = checkAndGetColumn(*default_untyped)) + { + getItemsStringImpl(index, ids, column_string.get(), [&](const size_t row) { return default_col->getDataAt(row); }); + } + else if (const auto default_col_const = checkAndGetColumnConst(default_untyped.get())) + { + const auto & def = default_col_const->template getValue(); + + getItemsStringImpl(index, ids, column_string.get(), [&](const size_t) { return StringRef{def}; }); + } + } + else + { + const auto null_value = StringRef{std::get(null_values[index])}; + + getItemsStringImpl(index, ids, column_string.get(), [&](const size_t) { return null_value; }); + } + + result = std::move(column_string); + } + else if constexpr (IsNumber) + { + auto column = ColumnVector::create(identifiers_size); + auto& out = column->getData(); + + if (default_untyped != nullptr) + { + if (const auto default_col = checkAndGetColumn>(*default_untyped)) + { + getItemsNumberImpl( + index, + ids, + out, + [&](const size_t row) { return default_col->getData()[row]; } + ); + } + else if (const auto default_col_const = checkAndGetColumnConst>(default_untyped.get())) + { + const auto & def = default_col_const->template getValue(); + + getItemsNumberImpl( + index, + ids, + out, + [&](const size_t) { return def; } + ); + } + } + else + { + const auto null_value = std::get(null_values[index]);; + + getItemsNumberImpl( + index, + ids, + out, + [&](const size_t) { return null_value; }); + } + + result = std::move(column); + } + else if constexpr (IsDecimalNumber) + { + // auto scale = getDecimalScale(*attribute.type); + auto column = ColumnDecimal::create(identifiers_size, 0); + auto& out = column->getData(); + + if (default_untyped != nullptr) + { + if (const auto default_col = checkAndGetColumn>(*default_untyped)) + { + getItemsNumberImpl( + index, + ids, + out, + [&](const size_t row) { return default_col->getData()[row]; } + ); + } + else if (const auto default_col_const = checkAndGetColumnConst>(default_untyped.get())) + { + const auto & def = default_col_const->template getValue(); + + getItemsNumberImpl( + index, + ids, + out, + [&](const size_t) { return def; } + ); + } + } + else + { + const auto null_value = std::get(null_values[index]);; + + getItemsNumberImpl( + index, + ids, + out, + [&](const size_t) { return null_value; } + ); + } + + result = std::move(column); + } + }; + + callOnDictionaryAttributeType(dict_struct.attributes[index].underlying_type, type_call); + + return result; +} template void SSDCacheDictionary::getItemsNumberImpl( @@ -1445,34 +1499,6 @@ void SSDCacheDictionary::getItemsNumberImpl( getLifetime()); } -void SSDCacheDictionary::getString(const std::string & attribute_name, const PaddedPODArray & ids, ColumnString * out) const -{ - const auto index = getAttributeIndex(attribute_name); - checkAttributeType(this, attribute_name, dict_struct.attributes[index].underlying_type, AttributeUnderlyingType::utString); - - const auto null_value = StringRef{std::get(null_values[index])}; - - getItemsStringImpl(index, ids, out, [&](const size_t) { return null_value; }); -} - -void SSDCacheDictionary::getString( - const std::string & attribute_name, const PaddedPODArray & ids, const ColumnString * const def, ColumnString * const out) const -{ - const auto index = getAttributeIndex(attribute_name); - checkAttributeType(this, attribute_name, dict_struct.attributes[index].underlying_type, AttributeUnderlyingType::utString); - - getItemsStringImpl(index, ids, out, [&](const size_t row) { return def->getDataAt(row); }); -} - -void SSDCacheDictionary::getString( - const std::string & attribute_name, const PaddedPODArray & ids, const String & def, ColumnString * const out) const -{ - const auto index = getAttributeIndex(attribute_name); - checkAttributeType(this, attribute_name, dict_struct.attributes[index].underlying_type, AttributeUnderlyingType::utString); - - getItemsStringImpl(index, ids, out, [&](const size_t) { return StringRef{def}; }); -} - template void SSDCacheDictionary::getItemsStringImpl(const size_t attribute_index, const PaddedPODArray & ids, ColumnString * out, DefaultGetter && get_default) const @@ -1545,14 +1571,24 @@ void SSDCacheDictionary::getItemsStringImpl(const size_t attribute_index, const } } -void SSDCacheDictionary::has(const PaddedPODArray & ids, PaddedPODArray & out) const +ColumnUInt8::Ptr SSDCacheDictionary::has(const Columns & key_columns, const DataTypes &) const { + PaddedPODArray backup_storage; + const auto& ids = getColumnDataAsPaddedPODArray(this, key_columns.front(), backup_storage); + + auto result = ColumnUInt8::create(ext::size(ids)); + auto& out = result->getData(); + + const auto rows = ext::size(ids); + for (const auto row : ext::range(0, rows)) + out[row] = false; + const auto now = std::chrono::system_clock::now(); std::unordered_map> not_found_ids; storage.has(ids, out, not_found_ids, now); if (not_found_ids.empty()) - return; + return result; std::vector required_ids(not_found_ids.size()); std::transform(std::begin(not_found_ids), std::end(not_found_ids), std::begin(required_ids), [](const auto & pair) { return pair.first; }); @@ -1571,6 +1607,8 @@ void SSDCacheDictionary::has(const PaddedPODArray & ids, PaddedPODArray using ResultArrayType = SSDCacheStorage::ResultArrayType; -#define DECLARE(TYPE) \ - void get##TYPE(const std::string & attribute_name, const PaddedPODArray & ids, ResultArrayType & out) const; - DECLARE(UInt8) - DECLARE(UInt16) - DECLARE(UInt32) - DECLARE(UInt64) - DECLARE(UInt128) - DECLARE(Int8) - DECLARE(Int16) - DECLARE(Int32) - DECLARE(Int64) - DECLARE(Float32) - DECLARE(Float64) - DECLARE(Decimal32) - DECLARE(Decimal64) - DECLARE(Decimal128) -#undef DECLARE - - void getString(const std::string & attribute_name, const PaddedPODArray & ids, ColumnString * out) const; - -#define DECLARE(TYPE) \ - void get##TYPE( \ - const std::string & attribute_name, \ - const PaddedPODArray & ids, \ - const PaddedPODArray & def, \ - ResultArrayType & out) const; - DECLARE(UInt8) - DECLARE(UInt16) - DECLARE(UInt32) - DECLARE(UInt64) - DECLARE(UInt128) - DECLARE(Int8) - DECLARE(Int16) - DECLARE(Int32) - DECLARE(Int64) - DECLARE(Float32) - DECLARE(Float64) - DECLARE(Decimal32) - DECLARE(Decimal64) - DECLARE(Decimal128) -#undef DECLARE - - void - getString(const std::string & attribute_name, const PaddedPODArray & ids, const ColumnString * def, ColumnString * out) - const; - -#define DECLARE(TYPE) \ - void get##TYPE(const std::string & attribute_name, const PaddedPODArray & ids, const TYPE def, ResultArrayType & out) const; - DECLARE(UInt8) - DECLARE(UInt16) - DECLARE(UInt32) - DECLARE(UInt64) - DECLARE(UInt128) - DECLARE(Int8) - DECLARE(Int16) - DECLARE(Int32) - DECLARE(Int64) - DECLARE(Float32) - DECLARE(Float64) - DECLARE(Decimal32) - DECLARE(Decimal64) - DECLARE(Decimal128) -#undef DECLARE - - void getString(const std::string & attribute_name, const PaddedPODArray & ids, const String & def, ColumnString * out) const; - - void has(const PaddedPODArray & ids, PaddedPODArray & out) const; - BlockInputStreamPtr getBlockInputStream(const Names & column_names, size_t max_block_size) const override; private: