diff --git a/dbms/src/Dictionaries/DictionaryBlockInputStream.h b/dbms/src/Dictionaries/DictionaryBlockInputStream.h index 1b7b536a15c..28459e1c067 100644 --- a/dbms/src/Dictionaries/DictionaryBlockInputStream.h +++ b/dbms/src/Dictionaries/DictionaryBlockInputStream.h @@ -114,6 +114,15 @@ private: Columns data_columns; GetColumnsFunction get_key_columns_function; GetColumnsFunction get_view_columns_function; + + enum class DictionaryKeyType + { + Id, + ComplexKey, + Callback + }; + + DictionaryKeyType key_type; }; template @@ -124,7 +133,8 @@ DictionaryBlockInputStream::DictionaryBlockInputStream( dictionary(std::static_pointer_cast(dictionary)), column_names(column_names), ids(std::move(ids)), logger(&Poco::Logger::get("DictionaryBlockInputStream")), - fillBlockFunction(&DictionaryBlockInputStream::fillBlock) + fillBlockFunction(&DictionaryBlockInputStream::fillBlock), + key_type(DictionaryKeyType::Id) { } @@ -135,7 +145,8 @@ DictionaryBlockInputStream::DictionaryBlockInputStream( : DictionaryBlockInputStreamBase(keys.size(), max_block_size), dictionary(std::static_pointer_cast(dictionary)), column_names(column_names), logger(&Poco::Logger::get("DictionaryBlockInputStream")), - fillBlockFunction(&DictionaryBlockInputStream::fillBlock) + fillBlockFunction(&DictionaryBlockInputStream::fillBlock), + key_type(DictionaryKeyType::ComplexKey) { const DictionaryStructure & dictionaty_structure = dictionary->getStructure(); fillKeyColumns(keys, 0, keys.size(), dictionaty_structure, key_columns); @@ -152,50 +163,55 @@ DictionaryBlockInputStream::DictionaryBlockInputStream( logger(&Poco::Logger::get("DictionaryBlockInputStream")), fillBlockFunction(&DictionaryBlockInputStream::fillBlock), data_columns(data_columns), - get_key_columns_function(get_key_columns_function), get_view_columns_function(get_view_columns_function) + get_key_columns_function(get_key_columns_function), get_view_columns_function(get_view_columns_function), + key_type(DictionaryKeyType::Callback) { } template Block DictionaryBlockInputStream::getBlock(size_t start, size_t length) const { - if (!key_columns.empty()) + switch (key_type) { - Columns columns; - ColumnsWithTypeAndName view_columns; - columns.reserve(key_columns.size()); - for (const auto & key_column : key_columns) + case DictionaryKeyType::ComplexKey: { - ColumnPtr column = key_column.column->cut(start, length); - columns.emplace_back(column); - view_columns.emplace_back(column, key_column.type, key_column.name); + Columns columns; + ColumnsWithTypeAndName view_columns; + columns.reserve(key_columns.size()); + for (const auto & key_column : key_columns) + { + ColumnPtr column = key_column.column->cut(start, length); + columns.emplace_back(column); + view_columns.emplace_back(column, key_column.type, key_column.name); + } + return (this->*fillBlockFunction)({}, columns, {}, std::move(view_columns)); } - return (this->*fillBlockFunction)({}, columns, {}, std::move(view_columns)); - } - else if (!ids.empty()) - { - PaddedPODArray block_ids(ids.begin() + start, ids.begin() + start + length); - return (this->*fillBlockFunction)(block_ids, {}, {}, {}); - } - else - { - Columns columns; - columns.reserve(data_columns.size()); - for (const auto & data_column : data_columns) - columns.push_back(data_column->cut(start, length)); - const DictionaryStructure & dictionaty_structure = dictionary->getStructure(); - const auto & attributes = *dictionaty_structure.key; - ColumnsWithTypeAndName keys_with_type_and_name = get_key_columns_function(columns, attributes); - ColumnsWithTypeAndName view_with_type_and_name = get_view_columns_function(columns, attributes); - DataTypes types; - columns.clear(); - for (const auto & key_column : keys_with_type_and_name) + case DictionaryKeyType::Id: { - columns.push_back(key_column.column); - types.push_back(key_column.type); + PaddedPODArray block_ids(ids.begin() + start, ids.begin() + start + length); + return (this->*fillBlockFunction)(block_ids, {}, {}, {}); + } + case DictionaryKeyType::Callback: + { + Columns columns; + columns.reserve(data_columns.size()); + for (const auto & data_column : data_columns) + columns.push_back(data_column->cut(start, length)); + const DictionaryStructure & dictionaty_structure = dictionary->getStructure(); + const auto & attributes = *dictionaty_structure.key; + ColumnsWithTypeAndName keys_with_type_and_name = get_key_columns_function(columns, attributes); + ColumnsWithTypeAndName view_with_type_and_name = get_view_columns_function(columns, attributes); + DataTypes types; + columns.clear(); + for (const auto & key_column : keys_with_type_and_name) + { + columns.push_back(key_column.column); + types.push_back(key_column.type); + } + return (this->*fillBlockFunction)({}, columns, types, std::move(view_with_type_and_name)); } - return (this->*fillBlockFunction)({}, columns, types, std::move(view_with_type_and_name)); } + throw Exception("Unexpected DictionaryKeyType.", ErrorCodes::LOGICAL_ERROR); } template