| 1 | #include <Storages/MergeTree/MergeTreeIndices.h> |
| 2 | #include <Parsers/parseQuery.h> |
| 3 | #include <Parsers/ParserCreateQuery.h> |
| 4 | #include <IO/WriteHelpers.h> |
| 5 | #include <IO/ReadHelpers.h> |
| 6 | |
| 7 | #include <numeric> |
| 8 | |
| 9 | #include <boost/algorithm/string.hpp> |
| 10 | |
| 11 | |
| 12 | namespace DB |
| 13 | { |
| 14 | |
| 15 | namespace ErrorCodes |
| 16 | { |
| 17 | extern const int LOGICAL_ERROR; |
| 18 | extern const int INCORRECT_QUERY; |
| 19 | extern const int UNKNOWN_EXCEPTION; |
| 20 | } |
| 21 | |
| 22 | void MergeTreeIndexFactory::registerIndex(const std::string & name, Creator creator) |
| 23 | { |
| 24 | if (!indexes.emplace(name, std::move(creator)).second) |
| 25 | throw Exception("MergeTreeIndexFactory: the Index creator name '" + name + "' is not unique" , |
| 26 | ErrorCodes::LOGICAL_ERROR); |
| 27 | } |
| 28 | |
| 29 | std::unique_ptr<IMergeTreeIndex> MergeTreeIndexFactory::get( |
| 30 | const NamesAndTypesList & columns, |
| 31 | std::shared_ptr<ASTIndexDeclaration> node, |
| 32 | const Context & context) const |
| 33 | { |
| 34 | if (!node->type) |
| 35 | throw Exception( |
| 36 | "for index TYPE is required" , ErrorCodes::INCORRECT_QUERY); |
| 37 | if (node->type->parameters && !node->type->parameters->children.empty()) |
| 38 | throw Exception( |
| 39 | "Index type can not have parameters" , ErrorCodes::INCORRECT_QUERY); |
| 40 | |
| 41 | boost::algorithm::to_lower(node->type->name); |
| 42 | auto it = indexes.find(node->type->name); |
| 43 | if (it == indexes.end()) |
| 44 | throw Exception( |
| 45 | "Unknown Index type '" + node->type->name + "'. Available index types: " + |
| 46 | std::accumulate(indexes.cbegin(), indexes.cend(), std::string{}, |
| 47 | [] (auto && lft, const auto & rht) -> std::string { |
| 48 | if (lft == "" ) |
| 49 | return rht.first; |
| 50 | else |
| 51 | return lft + ", " + rht.first; |
| 52 | }), |
| 53 | ErrorCodes::INCORRECT_QUERY); |
| 54 | |
| 55 | return it->second(columns, node, context); |
| 56 | } |
| 57 | |
| 58 | MergeTreeIndexFactory::MergeTreeIndexFactory() |
| 59 | { |
| 60 | registerIndex("minmax" , minmaxIndexCreator); |
| 61 | registerIndex("set" , setIndexCreator); |
| 62 | registerIndex("ngrambf_v1" , bloomFilterIndexCreator); |
| 63 | registerIndex("tokenbf_v1" , bloomFilterIndexCreator); |
| 64 | registerIndex("bloom_filter" , bloomFilterIndexCreatorNew); |
| 65 | } |
| 66 | |
| 67 | MergeTreeIndexFactory & MergeTreeIndexFactory::instance() |
| 68 | { |
| 69 | static MergeTreeIndexFactory instance; |
| 70 | return instance; |
| 71 | } |
| 72 | |
| 73 | } |
| 74 | |