aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/OptimizeDateOrDateTimeConverterWithPreimageVisitor.h
blob: 778fa462364a081a4aa8bb6f2f1685de8ea0d915 (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
#pragma once

#include <Interpreters/DatabaseAndTableWithAlias.h>
#include <Interpreters/InDepthNodeVisitor.h>

namespace DB
{

class ASTFunction;

/** Replace predicate having Date/DateTime converters with their preimages to improve performance.
 *  Given a Date column c, toYear(c) = 2023 -> c >= '2023-01-01' AND c < '2024-01-01'
 *  Or if c is a DateTime column, toYear(c) = 2023 -> c >= '2023-01-01 00:00:00' AND c < '2024-01-01 00:00:00'.
 *  The similar optimization also applies to other converters.
 */
class OptimizeDateOrDateTimeConverterWithPreimageMatcher
{
public:
    struct Data
    {
        const TablesWithColumns & tables;
        ContextPtr context;
    };

    static void visit(ASTPtr & ast, Data & data)
    {
        if (const auto * ast_function = ast->as<ASTFunction>())
            visit(*ast_function, ast, data);
    }

    static void visit(const ASTFunction & function, ASTPtr & ast, const Data & data);

    static bool needChildVisit(ASTPtr & ast, ASTPtr & child);
};

using OptimizeDateOrDateTimeConverterWithPreimageVisitor = InDepthNodeVisitor<OptimizeDateOrDateTimeConverterWithPreimageMatcher, true>;
}