1#pragma once
2
3#include <DataStreams/IBlockInputStream.h>
4
5
6namespace DB
7{
8
9class Arena;
10using ArenaPtr = std::shared_ptr<Arena>;
11
12class ExpressionActions;
13enum class TotalsMode;
14
15/** Takes blocks after grouping, with non-finalized aggregate functions.
16 * Calculates total values according to totals_mode.
17 * If necessary, evaluates the expression from HAVING and filters rows. Returns the finalized and filtered blocks.
18 */
19class TotalsHavingBlockInputStream : public IBlockInputStream
20{
21private:
22 using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
23
24public:
25 /// expression may be nullptr
26 TotalsHavingBlockInputStream(
27 const BlockInputStreamPtr & input_,
28 bool overflow_row_, const ExpressionActionsPtr & expression_,
29 const std::string & filter_column_, TotalsMode totals_mode_, double auto_include_threshold_, bool final_);
30
31 String getName() const override { return "TotalsHaving"; }
32
33 Block getTotals() override;
34
35 Block getHeader() const override;
36
37protected:
38 Block readImpl() override;
39
40private:
41 bool overflow_row;
42 ExpressionActionsPtr expression;
43 String filter_column_name;
44 TotalsMode totals_mode;
45 double auto_include_threshold;
46 bool final;
47 size_t passed_keys = 0;
48 size_t total_keys = 0;
49
50 /** Here are the values that did not pass max_rows_to_group_by.
51 * They are added or not added to the current_totals, depending on the totals_mode.
52 */
53 Block overflow_aggregates;
54
55 /// Here, total values are accumulated. After the work is finished, they will be placed in IBlockInputStream::totals.
56 MutableColumns current_totals;
57
58 /// If filter == nullptr - add all rows. Otherwise, only the rows that pass the filter (HAVING).
59 void addToTotals(const Block & block, const IColumn::Filter * filter);
60};
61
62}
63