2022-04-01 18:47:48 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-05-08 21:24:58 +00:00
|
|
|
|
2022-05-12 12:30:18 +00:00
|
|
|
#include <Storages/MergeTree/CommonANNIndexes.h>
|
2022-04-01 18:47:48 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeIndices.h>
|
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
|
|
|
#include <Storages/MergeTree/KeyCondition.h>
|
|
|
|
|
|
|
|
#include <annoylib.h>
|
|
|
|
#include <kissrandom.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annoy
|
|
|
|
{
|
|
|
|
using AnnoyIndexThreadedBuildPolicy = ::Annoy::AnnoyIndexSingleThreadedBuildPolicy;
|
|
|
|
template <typename Dist = ::Annoy::Euclidean>
|
|
|
|
class AnnoyIndexSerialize : public ::Annoy::AnnoyIndex<Int32, Float32, Dist, ::Annoy::Kiss64Random, AnnoyIndexThreadedBuildPolicy>
|
|
|
|
{
|
|
|
|
using Base = ::Annoy::AnnoyIndex<Int32, Float32, Dist, ::Annoy::Kiss64Random, AnnoyIndexThreadedBuildPolicy>;
|
|
|
|
public:
|
|
|
|
AnnoyIndexSerialize() = delete;
|
|
|
|
AnnoyIndexSerialize(const int dim) : Base::AnnoyIndex(dim) {}
|
|
|
|
void serialize(WriteBuffer& ostr) const;
|
|
|
|
void deserialize(ReadBuffer& istr);
|
2022-05-08 19:30:33 +00:00
|
|
|
float getSpaceDim() const;
|
2022-04-01 18:47:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MergeTreeIndexGranuleAnnoy final : public IMergeTreeIndexGranule
|
|
|
|
{
|
|
|
|
using AnnoyIndex = Annoy::AnnoyIndexSerialize<>;
|
|
|
|
using AnnoyIndexPtr = std::shared_ptr<AnnoyIndex>;
|
|
|
|
|
|
|
|
MergeTreeIndexGranuleAnnoy(const String & index_name_, const Block & index_sample_block_);
|
|
|
|
MergeTreeIndexGranuleAnnoy(
|
|
|
|
const String & index_name_,
|
|
|
|
const Block & index_sample_block_,
|
|
|
|
AnnoyIndexPtr index_base_);
|
|
|
|
|
|
|
|
~MergeTreeIndexGranuleAnnoy() override = default;
|
|
|
|
|
|
|
|
void serializeBinary(WriteBuffer & ostr) const override;
|
|
|
|
void deserializeBinary(ReadBuffer & istr, MergeTreeIndexVersion version) override;
|
|
|
|
|
|
|
|
bool empty() const override;
|
|
|
|
|
|
|
|
String index_name;
|
|
|
|
Block index_sample_block;
|
|
|
|
AnnoyIndexPtr index_base;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct MergeTreeIndexAggregatorAnnoy final : IMergeTreeIndexAggregator
|
|
|
|
{
|
|
|
|
using AnnoyIndex = Annoy::AnnoyIndexSerialize<>;
|
|
|
|
using AnnoyIndexPtr = std::shared_ptr<AnnoyIndex>;
|
|
|
|
|
2022-05-12 12:30:18 +00:00
|
|
|
MergeTreeIndexAggregatorAnnoy(const String & index_name_, const Block & index_sample_block, int index_param);
|
2022-04-01 18:47:48 +00:00
|
|
|
~MergeTreeIndexAggregatorAnnoy() override = default;
|
|
|
|
|
|
|
|
bool empty() const override;
|
|
|
|
MergeTreeIndexGranulePtr getGranuleAndReset() override;
|
|
|
|
void update(const Block & block, size_t * pos, size_t limit) override;
|
|
|
|
|
|
|
|
String index_name;
|
|
|
|
Block index_sample_block;
|
2022-05-12 12:30:18 +00:00
|
|
|
int index_param;
|
2022-04-01 18:47:48 +00:00
|
|
|
AnnoyIndexPtr index_base;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-04-15 15:45:59 +00:00
|
|
|
class MergeTreeIndexConditionAnnoy final : public IMergeTreeIndexCondition
|
2022-04-01 18:47:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-04-15 12:08:54 +00:00
|
|
|
MergeTreeIndexConditionAnnoy(
|
|
|
|
const IndexDescription & index,
|
|
|
|
const SelectQueryInfo & query,
|
|
|
|
ContextPtr context);
|
2022-04-01 18:47:48 +00:00
|
|
|
|
2022-04-15 12:08:54 +00:00
|
|
|
bool alwaysUnknownOrTrue() const override;
|
2022-04-01 18:47:48 +00:00
|
|
|
|
|
|
|
bool mayBeTrueOnGranule(MergeTreeIndexGranulePtr idx_granule) const override;
|
|
|
|
|
|
|
|
~MergeTreeIndexConditionAnnoy() override = default;
|
2022-04-15 12:08:54 +00:00
|
|
|
|
2022-05-04 21:38:11 +00:00
|
|
|
private:
|
2022-05-08 21:24:58 +00:00
|
|
|
ANNCondition::ANNCondition condition;
|
2022-04-01 18:47:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MergeTreeIndexAnnoy : public IMergeTreeIndex
|
|
|
|
{
|
|
|
|
public:
|
2022-05-12 12:30:18 +00:00
|
|
|
explicit MergeTreeIndexAnnoy(const IndexDescription & index_, int index_param_)
|
2022-04-01 18:47:48 +00:00
|
|
|
: IMergeTreeIndex(index_)
|
2022-05-12 12:30:18 +00:00
|
|
|
, index_param(index_param_)
|
2022-04-01 18:47:48 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
~MergeTreeIndexAnnoy() override = default;
|
|
|
|
|
|
|
|
MergeTreeIndexGranulePtr createIndexGranule() const override;
|
|
|
|
MergeTreeIndexAggregatorPtr createIndexAggregator() const override;
|
|
|
|
|
|
|
|
MergeTreeIndexConditionPtr createIndexCondition(
|
|
|
|
const SelectQueryInfo & query, ContextPtr context) const override;
|
|
|
|
|
|
|
|
bool mayBenefitFromIndexForIn(const ASTPtr & /*node*/) const override { return true; }
|
|
|
|
|
|
|
|
const char* getSerializedFileExtension() const override { return ".idx2"; }
|
|
|
|
MergeTreeIndexFormat getDeserializedFormat(const DiskPtr disk, const std::string & path_prefix) const override;
|
2022-05-12 12:30:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int index_param;
|
2022-04-01 18:47:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|