aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/pg_wrapper/interface/optimizer.h
blob: 689d2f622f9da027236f8e6f8917d542a3944f23 (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
#pragma once

#include <yql/essentials/core/cbo/cbo_optimizer_new.h>

#include <functional>

namespace NYql {

struct TExprContext;

struct IOptimizer {
    using TVarId = std::tuple<int, int>;

    struct TVar {
        char Name = 0; // debug name: 'a', 'b', 'c', ...
    };

    struct TRel {
        double Rows = 0;
        double TotalCost = 0;

        std::vector<TVar> TargetVars;
    };

    struct TEq {
        std::vector<TVarId> Vars;
    };

    struct TInput {
        std::vector<TRel> Rels;
        std::vector<TEq> EqClasses;
        std::vector<TEq> Left;
        std::vector<TEq> Right;

        TString ToString() const;
        void Normalize();
    };

    enum class EJoinType {
        Unknown,
        Inner,
        Left,
        Right,
    };

    enum class EJoinStrategy {
        Unknown,
        Hash,
        Loop
    };

    struct TJoinNode {
        EJoinType Mode = EJoinType::Unknown;
        EJoinStrategy Strategy = EJoinStrategy::Unknown;
        // only a = b && c = d ...  supported yet
        std::vector<TVarId> LeftVars = {};
        std::vector<TVarId> RightVars = {};
        std::vector<int> Rels = {};
        int Outer = -1; // index in Nodes
        int Inner = -1; // index in Nodes
    };

    struct TOutput {
        std::vector<TJoinNode> Nodes;
        TInput* Input = nullptr;
        double Rows = 0;
        double TotalCost = 0;

        TString ToString(bool printCost = true) const;
    };

    virtual ~IOptimizer() = default;
    virtual TOutput JoinSearch() = 0;
};

IOptimizer* MakePgOptimizerInternal(const IOptimizer::TInput& input, const std::function<void(const TString&)>& log = {});

IOptimizerNew* MakePgOptimizerNew(IProviderContext& pctx, TExprContext& ctx, const std::function<void(const TString&)>& log = {});

} // namespace NYql