aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/MergeTree/MergeTreeIndexHypothesis.cpp
blob: 2b7a40b429a33c26c45ff9d5b843d8ccfbb0f802 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <Storages/MergeTree/MergeTreeIndexHypothesis.h>
#include <Storages/MergeTree/MergeTreeIndexHypothesisMergedCondition.h>

#include <Interpreters/ExpressionActions.h>
#include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/TreeRewriter.h>

namespace DB
{
namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
}


MergeTreeIndexGranuleHypothesis::MergeTreeIndexGranuleHypothesis(const String & index_name_)
    : index_name(index_name_), is_empty(true), met(false)
{
}

MergeTreeIndexGranuleHypothesis::MergeTreeIndexGranuleHypothesis(const String & index_name_, bool met_)
    : index_name(index_name_), is_empty(false), met(met_)
{
}

void MergeTreeIndexGranuleHypothesis::serializeBinary(WriteBuffer & ostr) const
{
    const auto & size_type = DataTypePtr(std::make_shared<DataTypeUInt8>());
    size_type->getDefaultSerialization()->serializeBinary(static_cast<UInt8>(met), ostr, {});
}

void MergeTreeIndexGranuleHypothesis::deserializeBinary(ReadBuffer & istr, MergeTreeIndexVersion version)
{
    if (version != 1)
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Unknown index version {}.", version);

    Field field_met;
    const auto & size_type = DataTypePtr(std::make_shared<DataTypeUInt8>());
    size_type->getDefaultSerialization()->deserializeBinary(field_met, istr, {});
    met = field_met.get<UInt8>();
    is_empty = false;
}

MergeTreeIndexAggregatorHypothesis::MergeTreeIndexAggregatorHypothesis(const String & index_name_, const String & column_name_)
    : index_name(index_name_), column_name(column_name_)
{
}

MergeTreeIndexGranulePtr MergeTreeIndexAggregatorHypothesis::getGranuleAndReset()
{
    const auto granule = std::make_shared<MergeTreeIndexGranuleHypothesis>(index_name, met);
    met = true;
    is_empty = true;
    return granule;
}

void MergeTreeIndexAggregatorHypothesis::update(const Block & block, size_t * pos, size_t limit)
{
    size_t rows_read = std::min(limit, block.rows() - *pos);
    if (rows_read == 0)
        return;
    const auto & column = block.getByName(column_name).column->cut(*pos, rows_read);

    if (!column->hasEqualValues() || column->get64(0) == 0)
        met = false;

    is_empty = false;
    *pos += rows_read;
}

MergeTreeIndexGranulePtr MergeTreeIndexHypothesis::createIndexGranule() const
{
    return std::make_shared<MergeTreeIndexGranuleHypothesis>(index.name);
}

MergeTreeIndexAggregatorPtr MergeTreeIndexHypothesis::createIndexAggregator() const
{
    return std::make_shared<MergeTreeIndexAggregatorHypothesis>(index.name, index.sample_block.getNames().front());
}

MergeTreeIndexConditionPtr MergeTreeIndexHypothesis::createIndexCondition(
    const SelectQueryInfo &, ContextPtr) const
{
    throw Exception(ErrorCodes::LOGICAL_ERROR, "Not supported");
}

MergeTreeIndexMergedConditionPtr MergeTreeIndexHypothesis::createIndexMergedCondition(
    const SelectQueryInfo & query_info, StorageMetadataPtr storage_metadata) const
{
    return std::make_shared<MergeTreeIndexhypothesisMergedCondition>(
        query_info, storage_metadata->getConstraints(), index.granularity);
}

bool MergeTreeIndexHypothesis::mayBenefitFromIndexForIn(const ASTPtr &) const
{
    return false;
}

MergeTreeIndexPtr hypothesisIndexCreator(const IndexDescription & index)
{
    return std::make_shared<MergeTreeIndexHypothesis>(index);
}

void hypothesisIndexValidator(const IndexDescription & index, bool /*attach*/)
{
    if (index.expression_list_ast->children.size() != 1)
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Hypothesis index needs exactly one expression");
}

}