ClickHouse/dbms/Storages/MergeTree/MergeTreeIndexFullText.h

210 lines
6.8 KiB
C++
Raw Normal View History

2019-02-20 11:22:07 +00:00
#pragma once
#include <Interpreters/BloomFilter.h>
#include <Storages/MergeTree/MergeTreeIndices.h>
2019-02-20 16:24:46 +00:00
#include <Storages/MergeTree/KeyCondition.h>
2019-02-20 11:22:07 +00:00
#include <memory>
namespace DB
{
2019-05-10 03:42:28 +00:00
class MergeTreeIndexFullText;
2019-02-20 11:22:07 +00:00
2019-05-10 03:42:28 +00:00
struct MergeTreeIndexGranuleFullText : public IMergeTreeIndexGranule
2019-02-20 11:22:07 +00:00
{
2019-05-10 03:42:28 +00:00
explicit MergeTreeIndexGranuleFullText(
2019-08-03 11:02:40 +00:00
const MergeTreeIndexFullText & index_);
2019-02-20 11:22:07 +00:00
2019-05-10 03:42:28 +00:00
~MergeTreeIndexGranuleFullText() override = default;
2019-02-20 11:22:07 +00:00
void serializeBinary(WriteBuffer & ostr) const override;
void deserializeBinary(ReadBuffer & istr) override;
2019-02-26 20:36:15 +00:00
bool empty() const override { return !has_elems; }
2019-02-20 11:22:07 +00:00
2019-05-10 03:42:28 +00:00
const MergeTreeIndexFullText & index;
std::vector<BloomFilter> bloom_filters;
2019-02-20 12:48:50 +00:00
bool has_elems;
2019-02-20 11:22:07 +00:00
};
2019-05-10 03:42:28 +00:00
using MergeTreeIndexGranuleFullTextPtr = std::shared_ptr<MergeTreeIndexGranuleFullText>;
2019-03-11 17:59:36 +00:00
2019-05-10 03:42:28 +00:00
struct MergeTreeIndexAggregatorFullText : IMergeTreeIndexAggregator
2019-03-11 17:59:36 +00:00
{
2019-05-10 03:42:28 +00:00
explicit MergeTreeIndexAggregatorFullText(const MergeTreeIndexFullText & index);
2019-03-11 17:59:36 +00:00
2019-05-10 03:42:28 +00:00
~MergeTreeIndexAggregatorFullText() override = default;
2019-03-11 17:59:36 +00:00
bool empty() const override { return !granule || granule->empty(); }
MergeTreeIndexGranulePtr getGranuleAndReset() override;
void update(const Block & block, size_t * pos, size_t limit) override;
2019-05-10 03:42:28 +00:00
const MergeTreeIndexFullText & index;
MergeTreeIndexGranuleFullTextPtr granule;
2019-03-11 17:59:36 +00:00
};
2019-02-20 11:22:07 +00:00
2019-06-19 15:30:48 +00:00
class MergeTreeConditionFullText : public IMergeTreeIndexCondition
2019-02-20 11:22:07 +00:00
{
public:
2019-05-10 03:42:28 +00:00
MergeTreeConditionFullText(
2019-02-20 16:24:46 +00:00
const SelectQueryInfo & query_info,
const Context & context,
2019-05-10 03:42:28 +00:00
const MergeTreeIndexFullText & index_);
2019-02-20 11:22:07 +00:00
2019-05-10 03:42:28 +00:00
~MergeTreeConditionFullText() override = default;
2019-02-20 11:22:07 +00:00
2019-02-20 16:24:46 +00:00
bool alwaysUnknownOrTrue() const override;
bool mayBeTrueOnGranule(MergeTreeIndexGranulePtr idx_granule) const override;
2019-02-20 11:22:07 +00:00
private:
2019-02-24 21:17:52 +00:00
struct KeyTuplePositionMapping
{
KeyTuplePositionMapping(size_t tuple_index_, size_t key_index_) : tuple_index(tuple_index_), key_index(key_index_) {}
size_t tuple_index;
size_t key_index;
};
2019-02-20 16:24:46 +00:00
/// Uses RPN like KeyCondition
struct RPNElement
{
enum Function
{
/// Atoms of a Boolean expression.
FUNCTION_EQUALS,
FUNCTION_NOT_EQUALS,
2019-02-24 21:17:52 +00:00
FUNCTION_IN,
FUNCTION_NOT_IN,
FUNCTION_MULTI_SEARCH,
2019-02-20 16:24:46 +00:00
FUNCTION_UNKNOWN, /// Can take any value.
/// Operators of the logical expression.
FUNCTION_NOT,
FUNCTION_AND,
FUNCTION_OR,
/// Constants
ALWAYS_FALSE,
ALWAYS_TRUE,
};
RPNElement(
Function function_ = FUNCTION_UNKNOWN, size_t key_column_ = 0, std::unique_ptr<BloomFilter> && const_bloom_filter_ = nullptr)
: function(function_), key_column(key_column_), bloom_filter(std::move(const_bloom_filter_)) {}
2019-02-20 16:24:46 +00:00
Function function = FUNCTION_UNKNOWN;
/// For FUNCTION_EQUALS, FUNCTION_NOT_EQUALS and FUNCTION_MULTI_SEARCH
2019-02-20 16:24:46 +00:00
size_t key_column;
/// For FUNCTION_EQUALS, FUNCTION_NOT_EQUALS
2019-05-10 03:42:28 +00:00
std::unique_ptr<BloomFilter> bloom_filter;
/// For FUNCTION_IN, FUNCTION_NOT_IN and FUNCTION_MULTI_SEARCH
2019-05-10 03:42:28 +00:00
std::vector<std::vector<BloomFilter>> set_bloom_filters;
/// For FUNCTION_IN and FUNCTION_NOT_IN
2019-02-25 18:38:57 +00:00
std::vector<size_t> set_key_position;
2019-02-20 16:24:46 +00:00
};
using RPN = std::vector<RPNElement>;
2019-02-25 18:23:21 +00:00
bool atomFromAST(const ASTPtr & node, Block & block_with_constants, RPNElement & out);
2019-02-20 16:24:46 +00:00
bool getKey(const ASTPtr & node, size_t & key_column_num);
2019-02-25 18:23:21 +00:00
bool tryPrepareSetBloomFilter(const ASTs & args, RPNElement & out);
2019-02-20 16:24:46 +00:00
2019-07-16 11:40:11 +00:00
static bool createFunctionEqualsCondition(RPNElement & out, const Field & value, const MergeTreeIndexFullText & idx);
2019-05-10 03:42:28 +00:00
const MergeTreeIndexFullText & index;
2019-02-20 16:24:46 +00:00
RPN rpn;
2019-02-22 19:59:40 +00:00
/// Sets from syntax analyzer.
PreparedSets prepared_sets;
2019-02-20 11:22:07 +00:00
};
2019-02-23 13:06:23 +00:00
/// Interface for string parsers.
struct ITokenExtractor
2019-02-21 20:32:36 +00:00
{
2019-02-23 13:06:23 +00:00
virtual ~ITokenExtractor() = default;
2019-02-21 20:32:36 +00:00
/// Fast inplace implementation for regular use.
2019-02-23 13:06:23 +00:00
/// Gets string (data ptr and len) and start position for extracting next token (state of extractor).
/// Returns false if parsing is finished, otherwise returns true.
2019-02-21 21:29:24 +00:00
virtual bool next(const char * data, size_t len, size_t * pos, size_t * token_start, size_t * token_len) const = 0;
2019-02-21 20:32:36 +00:00
/// Special implementation for creating bloom filter for LIKE function.
/// It skips unescaped `%` and `_` and supports escaping symbols, but it is less lightweight.
2019-02-21 21:29:24 +00:00
virtual bool nextLike(const String & str, size_t * pos, String & out) const = 0;
2019-02-25 18:04:25 +00:00
virtual bool supportLike() const = 0;
2019-02-21 21:29:24 +00:00
};
2019-02-23 13:06:23 +00:00
/// Parser extracting all ngrams from string.
struct NgramTokenExtractor : public ITokenExtractor
2019-02-21 21:29:24 +00:00
{
NgramTokenExtractor(size_t n_) : n(n_) {}
2019-03-20 14:52:05 +00:00
static String getName() { return "ngrambf_v1"; }
2019-02-21 21:29:24 +00:00
bool next(const char * data, size_t len, size_t * pos, size_t * token_start, size_t * token_len) const override;
bool nextLike(const String & str, size_t * pos, String & token) const override;
2019-02-26 19:37:07 +00:00
bool supportLike() const override { return true; }
2019-02-25 18:04:25 +00:00
2019-02-21 21:29:24 +00:00
size_t n;
2019-02-21 20:32:36 +00:00
};
2019-02-20 11:22:07 +00:00
2019-02-25 14:23:19 +00:00
/// Parser extracting tokens (sequences of numbers and ascii letters).
2019-02-24 18:55:56 +00:00
struct SplitTokenExtractor : public ITokenExtractor
2019-02-22 10:51:19 +00:00
{
2019-03-20 14:52:05 +00:00
static String getName() { return "tokenbf_v1"; }
2019-02-22 10:51:19 +00:00
bool next(const char * data, size_t len, size_t * pos, size_t * token_start, size_t * token_len) const override;
bool nextLike(const String & str, size_t * pos, String & token) const override;
2019-02-25 18:04:25 +00:00
2019-03-06 15:30:27 +00:00
bool supportLike() const override { return true; }
2019-02-25 14:23:19 +00:00
};
2019-02-22 10:51:19 +00:00
2019-02-24 18:55:56 +00:00
2019-05-10 03:42:28 +00:00
class MergeTreeIndexFullText : public IMergeTreeIndex
2019-02-20 11:22:07 +00:00
{
public:
2019-05-10 03:42:28 +00:00
MergeTreeIndexFullText(
2019-02-20 11:22:07 +00:00
String name_,
ExpressionActionsPtr expr_,
const Names & columns_,
const DataTypes & data_types_,
const Block & header_,
size_t granularity_,
size_t bloom_filter_size_,
size_t bloom_filter_hashes_,
2019-02-20 12:12:41 +00:00
size_t seed_,
2019-02-23 13:06:23 +00:00
std::unique_ptr<ITokenExtractor> && token_extractor_func_)
2019-02-20 11:22:07 +00:00
: IMergeTreeIndex(name_, expr_, columns_, data_types_, header_, granularity_)
, bloom_filter_size(bloom_filter_size_)
, bloom_filter_hashes(bloom_filter_hashes_)
2019-02-20 12:12:41 +00:00
, seed(seed_)
2019-02-22 10:51:19 +00:00
, token_extractor_func(std::move(token_extractor_func_)) {}
2019-02-20 11:22:07 +00:00
2019-05-10 03:42:28 +00:00
~MergeTreeIndexFullText() override = default;
2019-02-20 11:22:07 +00:00
MergeTreeIndexGranulePtr createIndexGranule() const override;
2019-03-11 17:59:36 +00:00
MergeTreeIndexAggregatorPtr createIndexAggregator() const override;
2019-02-20 11:22:07 +00:00
2019-06-19 15:30:48 +00:00
MergeTreeIndexConditionPtr createIndexCondition(
2019-02-20 11:22:07 +00:00
const SelectQueryInfo & query, const Context & context) const override;
2019-02-25 08:43:19 +00:00
bool mayBenefitFromIndexForIn(const ASTPtr & node) const override;
2019-02-20 11:22:07 +00:00
/// Bloom filter size in bytes.
size_t bloom_filter_size;
/// Number of bloom filter hash functions.
size_t bloom_filter_hashes;
/// Bloom filter seed.
size_t seed;
2019-10-20 04:43:54 +00:00
/// Function for selecting next token.
2019-02-23 13:06:23 +00:00
std::unique_ptr<ITokenExtractor> token_extractor_func;
2019-02-20 11:22:07 +00:00
};
}