2021-01-21 09:01:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class ASTFunction;
|
|
|
|
|
|
|
|
/// Rewrite 'sum(if())' and 'sumIf' functions to counIf.
|
|
|
|
/// sumIf(1, cond) -> countIf(1, cond)
|
2022-12-30 06:48:58 +00:00
|
|
|
/// sumIf(123, cond) -> 123 * countIf(1, cond)
|
2021-01-21 09:01:35 +00:00
|
|
|
/// sum(if(cond, 1, 0)) -> countIf(cond)
|
2022-12-30 06:48:58 +00:00
|
|
|
/// sum(if(cond, 123, 0)) -> 123 * countIf(cond)
|
2021-01-21 09:01:35 +00:00
|
|
|
/// sum(if(cond, 0, 1)) -> countIf(not(cond))
|
2022-12-30 06:48:58 +00:00
|
|
|
/// sum(if(cond, 0, 123)) -> 123 * countIf(not(cond))
|
2021-01-21 09:01:35 +00:00
|
|
|
class RewriteSumIfFunctionMatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
static void visit(ASTPtr & ast, Data &);
|
|
|
|
static void visit(const ASTFunction &, ASTPtr & ast, Data &);
|
|
|
|
static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
using RewriteSumIfFunctionVisitor = InDepthNodeVisitor<RewriteSumIfFunctionMatcher, false>;
|
|
|
|
}
|