aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/Transforms/TotalsHavingTransform.h
blob: 350956c9c6b53a7124758193947fd35e5b830370 (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
#pragma once

#include <Processors/ISimpleTransform.h>
#include <Processors/Transforms/finalizeChunk.h>

namespace DB
{

class ExpressionActions;
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;

class ActionsDAG;

enum class TotalsMode;

/** Takes blocks after grouping, with non-finalized aggregate functions.
  * Calculates total values according to totals_mode.
  * If necessary, evaluates the expression from HAVING and filters rows. Returns the finalized and filtered blocks.
  */
class TotalsHavingTransform : public ISimpleTransform
{
public:
    TotalsHavingTransform(
        const Block & header,
        const ColumnsMask & aggregates_mask_,
        bool overflow_row_,
        const ExpressionActionsPtr & expression_,
        const std::string & filter_column_,
        bool remove_filter_,
        TotalsMode totals_mode_,
        double auto_include_threshold_,
        bool final_);

    String getName() const override { return "TotalsHavingTransform"; }

    OutputPort & getTotalsPort() { return outputs.back(); }

    Status prepare() override;
    void work() override;

    bool hasFilter() const { return !filter_column_name.empty(); }

    static Block transformHeader(Block block, const ActionsDAG * expression, const std::string & filter_column_name, bool remove_filter, bool final, const ColumnsMask & aggregates_mask);

protected:
    void transform(Chunk & chunk) override;

    bool finished_transform = false;
    bool total_prepared = false;
    Chunk totals;

private:
    void addToTotals(const Chunk & chunk, const IColumn::Filter * filter);
    void prepareTotals();

    /// Params
    const ColumnsMask aggregates_mask;
    bool overflow_row;
    ExpressionActionsPtr expression;
    String filter_column_name;
    bool remove_filter;
    TotalsMode totals_mode;
    double auto_include_threshold;
    bool final;

    size_t passed_keys = 0;
    size_t total_keys = 0;

    size_t filter_column_pos = 0;

    Block finalized_header;

    /// Here are the values that did not pass max_rows_to_group_by.
    /// They are added or not added to the current_totals, depending on the totals_mode.
    Chunk overflow_aggregates;

    /// Here, total values are accumulated. After the work is finished, they will be placed in totals.
    MutableColumns current_totals;
};

}