summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/proto_parser/parse_tree.cpp
blob: a93183684361a53f9d6aa16f2025be624b4d0189 (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
100
101
102
103
104
105
106
107
108
109
110
#include "parse_tree.h"

namespace NSQLTranslationV1 {

    const TRule_select_or_expr* GetSelectOrExpr(const TRule_smart_parenthesis& msg) {
        if (!msg.GetBlock2().HasAlt1()) {
            return nullptr;
        }

        return &msg.GetBlock2()
                    .GetAlt1()
                    .GetRule_select_subexpr1()
                    .GetRule_select_subexpr_intersect1()
                    .GetRule_select_or_expr1();
    }

    const TRule_tuple_or_expr* GetTupleOrExpr(const TRule_smart_parenthesis& msg) {
        const auto* select_or_expr = GetSelectOrExpr(msg);
        if (!select_or_expr) {
            return nullptr;
        }

        if (!select_or_expr->HasAlt_select_or_expr2()) {
            return nullptr;
        }

        return &select_or_expr
                    ->GetAlt_select_or_expr2()
                    .GetRule_tuple_or_expr1();
    }

    const TRule_smart_parenthesis* GetParenthesis(const TRule_expr& msg) {
        if (!msg.HasAlt_expr1()) {
            return nullptr;
        }

        const auto& con = msg.GetAlt_expr1()
                              .GetRule_or_subexpr1()
                              .GetRule_and_subexpr1()
                              .GetRule_xor_subexpr1()
                              .GetRule_eq_subexpr1()
                              .GetRule_neq_subexpr1()
                              .GetRule_bit_subexpr1()
                              .GetRule_add_subexpr1()
                              .GetRule_mul_subexpr1()
                              .GetRule_con_subexpr1();

        if (!con.HasAlt_con_subexpr1()) {
            return nullptr;
        }

        const auto& unary_subexpr = con.GetAlt_con_subexpr1()
                                        .GetRule_unary_subexpr1();

        if (!unary_subexpr.HasAlt_unary_subexpr1()) {
            return nullptr;
        }

        const auto& block = unary_subexpr.GetAlt_unary_subexpr1()
                                .GetRule_unary_casual_subexpr1()
                                .GetBlock1();

        if (!block.HasAlt2()) {
            return nullptr;
        }

        const auto& atom = block.GetAlt2()
                               .GetRule_atom_expr1();

        if (!atom.HasAlt_atom_expr3()) {
            return nullptr;
        }

        return &atom.GetAlt_atom_expr3()
                    .GetRule_lambda1()
                    .GetRule_smart_parenthesis1();
    }

    bool IsSelect(const TRule_smart_parenthesis& msg) {
        const auto* select_or_expr = GetSelectOrExpr(msg);
        if (!select_or_expr) {
            return false;
        }

        if (select_or_expr->HasAlt_select_or_expr1()) {
            return true;
        }

        return IsSelect(
            select_or_expr
                ->GetAlt_select_or_expr2()
                .GetRule_tuple_or_expr1()
                .GetRule_expr1());
    }

    bool IsSelect(const TRule_expr& msg) {
        const auto* parenthesis = GetParenthesis(msg);
        if (!parenthesis) {
            return false;
        }

        return IsSelect(*parenthesis);
    }

    bool IsOnlySubExpr(const TRule_select_subexpr& node) {
        return node.GetBlock2().size() == 0 &&
               node.GetRule_select_subexpr_intersect1().GetBlock2().size() == 0;
    }

} // namespace NSQLTranslationV1