From 3f6fdc1ddb31d226824efb0631d812e00316f54d Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 13 Mar 2020 21:42:19 +0300 Subject: [PATCH] Remove global variable with nontrivial ctor/dtor --- .../MergeTree/MergeTreeIndexFullText.cpp | 218 ++++++++---------- .../MergeTree/MergeTreeIndexFullText.h | 4 - 2 files changed, 96 insertions(+), 126 deletions(-) diff --git a/dbms/src/Storages/MergeTree/MergeTreeIndexFullText.cpp b/dbms/src/Storages/MergeTree/MergeTreeIndexFullText.cpp index 6d3b830a3f4..4b3bd954496 100644 --- a/dbms/src/Storages/MergeTree/MergeTreeIndexFullText.cpp +++ b/dbms/src/Storages/MergeTree/MergeTreeIndexFullText.cpp @@ -120,121 +120,6 @@ void MergeTreeIndexAggregatorFullText::update(const Block & block, size_t * pos, } -const MergeTreeConditionFullText::AtomMap MergeTreeConditionFullText::atom_map -{ - { - "notEquals", - [] (RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx) - { - out.function = RPNElement::FUNCTION_NOT_EQUALS; - out.bloom_filter = std::make_unique( - idx.bloom_filter_size, idx.bloom_filter_hashes, idx.seed); - - const auto & str = value.get(); - stringToBloomFilter(str.c_str(), str.size(), idx.token_extractor_func, *out.bloom_filter); - return true; - } - }, - { - "equals", - [] (RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx) - { - return createFunctionEqualsCondition(out, value, idx); - } - }, - { - "like", - [] (RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx) - { - out.function = RPNElement::FUNCTION_EQUALS; - out.bloom_filter = std::make_unique( - idx.bloom_filter_size, idx.bloom_filter_hashes, idx.seed); - - const auto & str = value.get(); - likeStringToBloomFilter(str, idx.token_extractor_func, *out.bloom_filter); - return true; - } - }, - { - "notLike", - [] (RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx) - { - out.function = RPNElement::FUNCTION_NOT_EQUALS; - out.bloom_filter = std::make_unique( - idx.bloom_filter_size, idx.bloom_filter_hashes, idx.seed); - - const auto & str = value.get(); - likeStringToBloomFilter(str, idx.token_extractor_func, *out.bloom_filter); - return true; - } - }, - { - "hasToken", - [] (RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx) - { - out.function = RPNElement::FUNCTION_EQUALS; - out.bloom_filter = std::make_unique( - idx.bloom_filter_size, idx.bloom_filter_hashes, idx.seed); - - const auto & str = value.get(); - stringToBloomFilter(str.c_str(), str.size(), idx.token_extractor_func, *out.bloom_filter); - return true; - } - }, - { - "startsWith", - [] (RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx) - { - return createFunctionEqualsCondition(out, value, idx); - } - }, - { - "endsWith", - [] (RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx) - { - return createFunctionEqualsCondition(out, value, idx); - } - }, - { - "multiSearchAny", - [] (RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx) - { - out.function = RPNElement::FUNCTION_MULTI_SEARCH; - - /// 2d vector is not needed here but is used because already exists for FUNCTION_IN - std::vector> bloom_filters; - bloom_filters.emplace_back(); - for (const auto & element : value.get()) - { - if (element.getType() != Field::Types::String) - return false; - - bloom_filters.back().emplace_back(idx.bloom_filter_size, idx.bloom_filter_hashes, idx.seed); - const auto & str = element.get(); - stringToBloomFilter(str.c_str(), str.size(), idx.token_extractor_func, bloom_filters.back().back()); - } - out.set_bloom_filters = std::move(bloom_filters); - return true; - } - }, - { - "notIn", - [] (RPNElement & out, const Field &, const MergeTreeIndexFullText &) - { - out.function = RPNElement::FUNCTION_NOT_IN; - return true; - } - }, - { - "in", - [] (RPNElement & out, const Field &, const MergeTreeIndexFullText &) - { - out.function = RPNElement::FUNCTION_IN; - return true; - } - }, -}; - MergeTreeConditionFullText::MergeTreeConditionFullText( const SelectQueryInfo & query_info, const Context & context, @@ -429,21 +314,110 @@ bool MergeTreeConditionFullText::atomFromAST( return false; if (const_type && const_type->getTypeId() != TypeIndex::String - && const_type->getTypeId() != TypeIndex::FixedString - && const_type->getTypeId() != TypeIndex::Array) + && const_type->getTypeId() != TypeIndex::FixedString + && const_type->getTypeId() != TypeIndex::Array) + { return false; + } if (key_arg_pos == 1 && (func_name != "equals" || func_name != "notEquals")) return false; else if (!index.token_extractor_func->supportLike() && (func_name == "like" || func_name == "notLike")) return false; - const auto atom_it = atom_map.find(func_name); - if (atom_it == std::end(atom_map)) - return false; + if (func_name == "notEquals") + { + out.key_column = key_column_num; + out.function = RPNElement::FUNCTION_NOT_EQUALS; + out.bloom_filter = std::make_unique( + index.bloom_filter_size, index.bloom_filter_hashes, index.seed); - out.key_column = key_column_num; - return atom_it->second(out, const_value, index); + const auto & str = const_value.get(); + stringToBloomFilter(str.c_str(), str.size(), index.token_extractor_func, *out.bloom_filter); + return true; + } + else if (func_name == "equals") + { + out.key_column = key_column_num; + return createFunctionEqualsCondition(out, const_value, index); + } + else if (func_name == "like") + { + out.key_column = key_column_num; + out.function = RPNElement::FUNCTION_EQUALS; + out.bloom_filter = std::make_unique( + index.bloom_filter_size, index.bloom_filter_hashes, index.seed); + + const auto & str = const_value.get(); + likeStringToBloomFilter(str, index.token_extractor_func, *out.bloom_filter); + return true; + } + else if (func_name == "notLike") + { + out.key_column = key_column_num; + out.function = RPNElement::FUNCTION_NOT_EQUALS; + out.bloom_filter = std::make_unique( + index.bloom_filter_size, index.bloom_filter_hashes, index.seed); + + const auto & str = const_value.get(); + likeStringToBloomFilter(str, index.token_extractor_func, *out.bloom_filter); + return true; + } + else if (func_name == "hasToken") + { + out.key_column = key_column_num; + out.function = RPNElement::FUNCTION_EQUALS; + out.bloom_filter = std::make_unique( + index.bloom_filter_size, index.bloom_filter_hashes, index.seed); + + const auto & str = const_value.get(); + stringToBloomFilter(str.c_str(), str.size(), index.token_extractor_func, *out.bloom_filter); + return true; + } + else if (func_name == "startsWith") + { + out.key_column = key_column_num; + return createFunctionEqualsCondition(out, const_value, index); + } + else if (func_name == "endsWith") + { + out.key_column = key_column_num; + return createFunctionEqualsCondition(out, const_value, index); + } + else if (func_name == "multiSearchAny") + { + out.key_column = key_column_num; + out.function = RPNElement::FUNCTION_MULTI_SEARCH; + + /// 2d vector is not needed here but is used because already exists for FUNCTION_IN + std::vector> bloom_filters; + bloom_filters.emplace_back(); + for (const auto & element : const_value.get()) + { + if (element.getType() != Field::Types::String) + return false; + + bloom_filters.back().emplace_back(index.bloom_filter_size, index.bloom_filter_hashes, index.seed); + const auto & str = element.get(); + stringToBloomFilter(str.c_str(), str.size(), index.token_extractor_func, bloom_filters.back().back()); + } + out.set_bloom_filters = std::move(bloom_filters); + return true; + } + else if (func_name == "notIn") + { + out.key_column = key_column_num; + out.function = RPNElement::FUNCTION_NOT_IN; + return true; + } + else if (func_name == "in") + { + out.key_column = key_column_num; + out.function = RPNElement::FUNCTION_IN; + return true; + } + + return false; } else if (KeyCondition::getConstant(node, block_with_constants, const_value, const_type)) { diff --git a/dbms/src/Storages/MergeTree/MergeTreeIndexFullText.h b/dbms/src/Storages/MergeTree/MergeTreeIndexFullText.h index 196b4df9eda..1e7f2753a93 100644 --- a/dbms/src/Storages/MergeTree/MergeTreeIndexFullText.h +++ b/dbms/src/Storages/MergeTree/MergeTreeIndexFullText.h @@ -60,7 +60,6 @@ public: ~MergeTreeConditionFullText() override = default; bool alwaysUnknownOrTrue() const override; - bool mayBeTrueOnGranule(MergeTreeIndexGranulePtr idx_granule) const override; private: struct KeyTuplePositionMapping @@ -109,7 +108,6 @@ private: std::vector set_key_position; }; - using AtomMap = std::unordered_map; using RPN = std::vector; bool atomFromAST(const ASTPtr & node, Block & block_with_constants, RPNElement & out); @@ -119,8 +117,6 @@ private: static bool createFunctionEqualsCondition(RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx); - static const AtomMap atom_map; - const MergeTreeIndexFullText & index; RPN rpn; /// Sets from syntax analyzer.