1 | #pragma once |
2 | |
3 | #include <Storages/MergeTree/MergeTreeIndices.h> |
4 | #include <Storages/MergeTree/MergeTreeData.h> |
5 | |
6 | #include <Interpreters/SetVariants.h> |
7 | |
8 | #include <memory> |
9 | #include <set> |
10 | |
11 | |
12 | namespace DB |
13 | { |
14 | |
15 | class MergeTreeIndexSet; |
16 | |
17 | struct MergeTreeIndexGranuleSet : public IMergeTreeIndexGranule |
18 | { |
19 | explicit MergeTreeIndexGranuleSet(const MergeTreeIndexSet & index_); |
20 | MergeTreeIndexGranuleSet(const MergeTreeIndexSet & index_, MutableColumns && columns_); |
21 | |
22 | void serializeBinary(WriteBuffer & ostr) const override; |
23 | void deserializeBinary(ReadBuffer & istr) override; |
24 | |
25 | size_t size() const { return block.rows(); } |
26 | bool empty() const override { return !size(); } |
27 | |
28 | ~MergeTreeIndexGranuleSet() override = default; |
29 | |
30 | const MergeTreeIndexSet & index; |
31 | Block block; |
32 | }; |
33 | |
34 | |
35 | struct MergeTreeIndexAggregatorSet : IMergeTreeIndexAggregator |
36 | { |
37 | explicit MergeTreeIndexAggregatorSet(const MergeTreeIndexSet & index); |
38 | ~MergeTreeIndexAggregatorSet() override = default; |
39 | |
40 | size_t size() const { return data.getTotalRowCount(); } |
41 | bool empty() const override { return !size(); } |
42 | |
43 | MergeTreeIndexGranulePtr getGranuleAndReset() override; |
44 | |
45 | void update(const Block & block, size_t * pos, size_t limit) override; |
46 | |
47 | private: |
48 | /// return true if has new data |
49 | template <typename Method> |
50 | bool buildFilter( |
51 | Method & method, |
52 | const ColumnRawPtrs & column_ptrs, |
53 | IColumn::Filter & filter, |
54 | size_t pos, |
55 | size_t limit, |
56 | ClearableSetVariants & variants) const; |
57 | |
58 | const MergeTreeIndexSet & index; |
59 | ClearableSetVariants data; |
60 | Sizes key_sizes; |
61 | MutableColumns columns; |
62 | }; |
63 | |
64 | |
65 | class MergeTreeIndexConditionSet : public IMergeTreeIndexCondition |
66 | { |
67 | public: |
68 | MergeTreeIndexConditionSet( |
69 | const SelectQueryInfo & query, |
70 | const Context & context, |
71 | const MergeTreeIndexSet & index); |
72 | |
73 | bool alwaysUnknownOrTrue() const override; |
74 | |
75 | bool mayBeTrueOnGranule(MergeTreeIndexGranulePtr idx_granule) const override; |
76 | |
77 | ~MergeTreeIndexConditionSet() override = default; |
78 | private: |
79 | void traverseAST(ASTPtr & node) const; |
80 | bool atomFromAST(ASTPtr & node) const; |
81 | bool operatorFromAST(ASTPtr & node) const; |
82 | |
83 | bool checkASTUseless(const ASTPtr &node, bool atomic = false) const; |
84 | |
85 | const MergeTreeIndexSet & index; |
86 | |
87 | bool useless; |
88 | std::set<String> key_columns; |
89 | ASTPtr expression_ast; |
90 | ExpressionActionsPtr actions; |
91 | }; |
92 | |
93 | |
94 | class MergeTreeIndexSet : public IMergeTreeIndex |
95 | { |
96 | public: |
97 | MergeTreeIndexSet( |
98 | String name_, |
99 | ExpressionActionsPtr expr_, |
100 | const Names & columns_, |
101 | const DataTypes & data_types_, |
102 | const Block & , |
103 | size_t granularity_, |
104 | size_t max_rows_) |
105 | : IMergeTreeIndex(std::move(name_), std::move(expr_), columns_, data_types_, header_, granularity_), max_rows(max_rows_) {} |
106 | |
107 | ~MergeTreeIndexSet() override = default; |
108 | |
109 | MergeTreeIndexGranulePtr createIndexGranule() const override; |
110 | MergeTreeIndexAggregatorPtr createIndexAggregator() const override; |
111 | |
112 | MergeTreeIndexConditionPtr createIndexCondition( |
113 | const SelectQueryInfo & query, const Context & context) const override; |
114 | |
115 | bool mayBenefitFromIndexForIn(const ASTPtr & node) const override; |
116 | |
117 | size_t max_rows = 0; |
118 | }; |
119 | |
120 | } |
121 | |