1 | #include <Processors/ISimpleTransform.h> |
2 | |
3 | #include <Common/Arena.h> |
4 | |
5 | namespace DB |
6 | { |
7 | |
8 | class Arena; |
9 | using ArenaPtr = std::shared_ptr<Arena>; |
10 | |
11 | class ExpressionActions; |
12 | using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>; |
13 | |
14 | enum class TotalsMode; |
15 | |
16 | /** Takes blocks after grouping, with non-finalized aggregate functions. |
17 | * Calculates total values according to totals_mode. |
18 | * If necessary, evaluates the expression from HAVING and filters rows. Returns the finalized and filtered blocks. |
19 | */ |
20 | class TotalsHavingTransform : public ISimpleTransform |
21 | { |
22 | public: |
23 | TotalsHavingTransform( |
24 | const Block & , |
25 | bool overflow_row_, |
26 | const ExpressionActionsPtr & expression_, |
27 | const std::string & filter_column_, |
28 | TotalsMode totals_mode_, |
29 | double auto_include_threshold_, |
30 | bool final_); |
31 | |
32 | String getName() const override { return "TotalsHavingTransform" ; } |
33 | |
34 | OutputPort & getTotalsPort() { return outputs.back(); } |
35 | |
36 | Status prepare() override; |
37 | void work() override; |
38 | |
39 | protected: |
40 | void transform(Chunk & chunk) override; |
41 | |
42 | bool finished_transform = false; |
43 | Chunk totals; |
44 | |
45 | private: |
46 | void addToTotals(const Chunk & block, const IColumn::Filter * filter); |
47 | void prepareTotals(); |
48 | |
49 | /// Params |
50 | bool overflow_row; |
51 | ExpressionActionsPtr expression; |
52 | String filter_column_name; |
53 | TotalsMode totals_mode; |
54 | double auto_include_threshold; |
55 | bool final; |
56 | |
57 | size_t passed_keys = 0; |
58 | size_t total_keys = 0; |
59 | |
60 | size_t filter_column_pos = 0; |
61 | |
62 | Block ; |
63 | |
64 | /// Here are the values that did not pass max_rows_to_group_by. |
65 | /// They are added or not added to the current_totals, depending on the totals_mode. |
66 | Chunk overflow_aggregates; |
67 | |
68 | /// Here, total values are accumulated. After the work is finished, they will be placed in IBlockInputStream::totals. |
69 | MutableColumns current_totals; |
70 | }; |
71 | |
72 | void finalizeChunk(Chunk & chunk); |
73 | |
74 | } |
75 | |