aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/Passes/SumIfToCountIfPass.h
blob: f3ba47f1c2cfd94de7e49546d0969395211f2208 (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
#pragma once

#include <Analyzer/IQueryTreePass.h>

namespace DB
{

/** Rewrite `sum(if(cond, value_1, value_2))` and `sumIf` functions to `countIf`.
  *
  * Example: SELECT sumIf(1, cond);
  * Result: SELECT countIf(cond);
  *
  * Example: SELECT sum(if(cond, 1, 0));
  * Result: SELECT countIf(cond);
  *
  * Example: SELECT sum(if(cond, 0, 1));
  * Result: SELECT countIf(not(cond));
  */
class SumIfToCountIfPass final : public IQueryTreePass
{
public:
    String getName() override { return "SumIfToCountIf"; }

    String getDescription() override { return "Rewrite sum(if) and sumIf into countIf"; }

    void run(QueryTreeNodePtr query_tree_node, ContextPtr context) override;

};

}