aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/cbo/cbo_optimizer_ut.cpp
blob: 235afe2afd9c72bb93c8a0ec0177b28f1b5f4fa6 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/testing/hook/hook.h>

#include <yql/essentials/parser/pg_wrapper/interface/optimizer.h>

using namespace NYql;

Y_UNIT_TEST_SUITE(CboOptimizer) {

Y_UNIT_TEST(InputToString) {
    IOptimizer::TRel rel1 = {100000, 1000000, {{'a'}}};
    IOptimizer::TRel rel2 = {1000000, 9000009, {{'b'}}};
    IOptimizer::TRel rel3 = {10000, 9009, {{'c'}}};
    IOptimizer::TInput input = {{rel1, rel2, rel3}, {}, {}, {}};

    input.EqClasses.emplace_back(IOptimizer::TEq {
        {{1, 1}, {2, 1}, {3, 1}}
    });

    auto str = input.ToString();

    TString expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
{rows: 1000000,cost: 9000009,vars: [b]},
{rows: 10000,cost: 9009,vars: [c]}]
EqClasses: [[a,b,c]]
)__";
    UNIT_ASSERT_STRINGS_EQUAL(expected, str);
}

Y_UNIT_TEST(OutputToString) {
    IOptimizer::TOutput output;
    auto str = output.ToString();

    TString expected = R"__(Rows: 0.00
TotalCost: 0.00
{
}
)__";
    UNIT_ASSERT_STRINGS_EQUAL(expected, str);
}

Y_UNIT_TEST(InputNormalize) {
    IOptimizer::TRel rel1 = {100000, 1000000, {{'a'}}};
    IOptimizer::TRel rel2 = {1000000, 9000009, {{'b'}}};
    IOptimizer::TRel rel3 = {10000, 9009, {{'c'}}};
    IOptimizer::TInput input = {{rel1, rel2, rel3}, {}, {}, {}};

    input.EqClasses.emplace_back(IOptimizer::TEq {
        {{1, 1}, {2, 1}}
    });
    input.EqClasses.emplace_back(IOptimizer::TEq {
        {{2, 1}, {3, 1}}
    });

    TString expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
{rows: 1000000,cost: 9000009,vars: [b]},
{rows: 10000,cost: 9009,vars: [c]}]
EqClasses: [[a,b],[b,c]]
)__";
    UNIT_ASSERT_STRINGS_EQUAL(expected, input.ToString());

    input.Normalize();

    expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
{rows: 1000000,cost: 9000009,vars: [b]},
{rows: 10000,cost: 9009,vars: [c]}]
EqClasses: [[a,b,c]]
)__";
    UNIT_ASSERT_STRINGS_EQUAL(expected, input.ToString());

    IOptimizer::TRel rel4 = {10001, 9009, {{'d'}}};
    IOptimizer::TInput input2 = {{rel1, rel2, rel3, rel4}, {}, {}, {}};
    input2.EqClasses.emplace_back(IOptimizer::TEq {
        {{1, 1}, {2, 1}}
    });
    input2.EqClasses.emplace_back(IOptimizer::TEq {
        {{4, 1}, {3, 1}}
    });

    expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
{rows: 1000000,cost: 9000009,vars: [b]},
{rows: 10000,cost: 9009,vars: [c]},
{rows: 10001,cost: 9009,vars: [d]}]
EqClasses: [[a,b],[d,c]]
)__";
    UNIT_ASSERT_STRINGS_EQUAL(expected, input2.ToString());

    input2.Normalize();

    expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
{rows: 1000000,cost: 9000009,vars: [b]},
{rows: 10000,cost: 9009,vars: [c]},
{rows: 10001,cost: 9009,vars: [d]}]
EqClasses: [[a,b],[c,d]]
)__";
    UNIT_ASSERT_STRINGS_EQUAL(expected, input2.ToString());
}

} // Y_UNIT_TEST_SUITE(CboOptimizer)