2018-12-26 12:19:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2018-12-26 17:34:44 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <Core/Block.h>
|
|
|
|
#include <ext/singleton.h>
|
2019-01-02 14:24:26 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataPartChecksum.h>
|
2018-12-29 11:12:41 +00:00
|
|
|
#include <Storages/SelectQueryInfo.h>
|
|
|
|
#include <Storages/MergeTree/MarkRange.h>
|
2018-12-26 17:34:44 +00:00
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2018-12-26 12:19:24 +00:00
|
|
|
#include <Parsers/ASTIndexDeclaration.h>
|
|
|
|
|
2019-01-05 09:26:02 +00:00
|
|
|
constexpr auto INDEX_FILE_PREFIX = "skp_idx_";
|
|
|
|
|
2018-12-26 12:19:24 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-01-07 18:53:51 +00:00
|
|
|
class MergeTreeData;
|
2018-12-28 17:53:41 +00:00
|
|
|
class MergeTreeIndex;
|
|
|
|
|
2019-01-02 14:24:26 +00:00
|
|
|
using MergeTreeIndexPtr = std::shared_ptr<const MergeTreeIndex>;
|
|
|
|
using MutableMergeTreeIndexPtr = std::shared_ptr<MergeTreeIndex>;
|
2018-12-29 11:12:41 +00:00
|
|
|
|
2018-12-29 12:04:00 +00:00
|
|
|
|
2019-01-03 16:47:42 +00:00
|
|
|
struct MergeTreeIndexGranule
|
|
|
|
{
|
2019-01-07 16:49:34 +00:00
|
|
|
virtual ~MergeTreeIndexGranule() = default;
|
2019-01-03 16:47:42 +00:00
|
|
|
|
2019-01-04 14:33:38 +00:00
|
|
|
virtual void serializeBinary(WriteBuffer & ostr) const = 0;
|
2019-01-07 16:49:34 +00:00
|
|
|
virtual void deserializeBinary(ReadBuffer & istr) = 0;
|
2019-01-03 16:47:42 +00:00
|
|
|
|
2019-01-04 14:33:38 +00:00
|
|
|
virtual bool empty() const = 0;
|
|
|
|
virtual void update(const Block & block, size_t * pos, size_t limit) = 0;
|
2019-01-03 16:47:42 +00:00
|
|
|
};
|
|
|
|
|
2019-01-07 12:51:14 +00:00
|
|
|
|
2019-01-03 16:47:42 +00:00
|
|
|
using MergeTreeIndexGranulePtr = std::shared_ptr<MergeTreeIndexGranule>;
|
2019-01-04 14:33:38 +00:00
|
|
|
using MergeTreeIndexGranules = std::vector<MergeTreeIndexGranulePtr>;
|
2019-01-03 16:47:42 +00:00
|
|
|
|
2019-01-07 12:51:14 +00:00
|
|
|
/// Condition on the index.
|
2019-01-26 06:26:49 +00:00
|
|
|
class IndexCondition
|
|
|
|
{
|
2019-01-07 12:51:14 +00:00
|
|
|
public:
|
|
|
|
virtual ~IndexCondition() = default;
|
|
|
|
/// Checks if this index is useful for query.
|
|
|
|
virtual bool alwaysUnknownOrTrue() const = 0;
|
|
|
|
|
2019-01-08 17:27:44 +00:00
|
|
|
virtual bool mayBeTrueOnGranule(MergeTreeIndexGranulePtr granule) const = 0;
|
2019-01-06 15:22:04 +00:00
|
|
|
};
|
|
|
|
|
2019-01-07 12:51:14 +00:00
|
|
|
using IndexConditionPtr = std::shared_ptr<IndexCondition>;
|
|
|
|
|
2019-01-06 15:22:04 +00:00
|
|
|
|
2018-12-29 12:04:00 +00:00
|
|
|
/// Structure for storing basic index info like columns, expression, arguments, ...
|
2018-12-26 17:34:44 +00:00
|
|
|
class MergeTreeIndex
|
2018-12-26 12:19:24 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-01-10 12:57:12 +00:00
|
|
|
MergeTreeIndex(
|
|
|
|
String name,
|
|
|
|
ExpressionActionsPtr expr,
|
|
|
|
const Names & columns,
|
|
|
|
const DataTypes & data_types,
|
2019-01-27 18:02:15 +00:00
|
|
|
const Block & header,
|
2019-01-10 12:57:12 +00:00
|
|
|
size_t granularity)
|
|
|
|
: name(name)
|
|
|
|
, expr(expr)
|
|
|
|
, columns(columns)
|
|
|
|
, data_types(data_types)
|
2019-01-27 18:02:15 +00:00
|
|
|
, header(header)
|
2019-01-10 12:57:12 +00:00
|
|
|
, granularity(granularity) {}
|
2018-12-26 12:19:24 +00:00
|
|
|
|
2019-01-08 17:27:44 +00:00
|
|
|
virtual ~MergeTreeIndex() = default;
|
2018-12-26 12:19:24 +00:00
|
|
|
|
2019-01-05 09:26:02 +00:00
|
|
|
/// gets filename without extension
|
2019-01-26 06:26:49 +00:00
|
|
|
String getFileName() const { return INDEX_FILE_PREFIX + name; }
|
2019-01-05 09:26:02 +00:00
|
|
|
|
2019-01-04 14:33:38 +00:00
|
|
|
virtual MergeTreeIndexGranulePtr createIndexGranule() const = 0;
|
|
|
|
|
2019-01-07 12:51:14 +00:00
|
|
|
virtual IndexConditionPtr createIndexCondition(
|
2019-01-04 15:54:34 +00:00
|
|
|
const SelectQueryInfo & query_info, const Context & context) const = 0;
|
|
|
|
|
2018-12-26 12:19:24 +00:00
|
|
|
String name;
|
|
|
|
ExpressionActionsPtr expr;
|
2019-01-04 14:33:38 +00:00
|
|
|
Names columns;
|
|
|
|
DataTypes data_types;
|
2019-01-27 18:02:15 +00:00
|
|
|
Block header;
|
2019-01-10 12:57:12 +00:00
|
|
|
size_t granularity;
|
2018-12-26 12:19:24 +00:00
|
|
|
};
|
|
|
|
|
2018-12-26 17:34:44 +00:00
|
|
|
|
2019-01-17 12:11:36 +00:00
|
|
|
using MergeTreeIndices = std::vector<MutableMergeTreeIndexPtr>;
|
2019-01-05 18:33:30 +00:00
|
|
|
|
|
|
|
|
2018-12-26 12:19:24 +00:00
|
|
|
class MergeTreeIndexFactory : public ext::singleton<MergeTreeIndexFactory>
|
|
|
|
{
|
|
|
|
friend class ext::singleton<MergeTreeIndexFactory>;
|
|
|
|
|
|
|
|
public:
|
2019-01-07 18:53:51 +00:00
|
|
|
using Creator = std::function<
|
|
|
|
std::unique_ptr<MergeTreeIndex>(
|
2019-01-30 10:18:59 +00:00
|
|
|
const NamesAndTypesList & columns,
|
2019-01-07 18:53:51 +00:00
|
|
|
std::shared_ptr<ASTIndexDeclaration> node,
|
|
|
|
const Context & context)>;
|
|
|
|
|
|
|
|
std::unique_ptr<MergeTreeIndex> get(
|
2019-01-30 10:18:59 +00:00
|
|
|
const NamesAndTypesList & columns,
|
2019-01-09 17:05:52 +00:00
|
|
|
std::shared_ptr<ASTIndexDeclaration> node,
|
|
|
|
const Context & context) const;
|
2018-12-26 12:19:24 +00:00
|
|
|
|
|
|
|
void registerIndex(const std::string & name, Creator creator);
|
|
|
|
|
2019-01-26 06:26:49 +00:00
|
|
|
const auto & getAllIndexes() const { return indexes; }
|
2018-12-26 12:19:24 +00:00
|
|
|
|
2018-12-26 17:34:44 +00:00
|
|
|
protected:
|
2018-12-29 11:12:41 +00:00
|
|
|
MergeTreeIndexFactory() = default;
|
2018-12-26 17:34:44 +00:00
|
|
|
|
2018-12-26 12:19:24 +00:00
|
|
|
private:
|
|
|
|
using Indexes = std::unordered_map<std::string, Creator>;
|
|
|
|
Indexes indexes;
|
|
|
|
};
|
|
|
|
|
2019-01-26 06:26:49 +00:00
|
|
|
}
|