summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/analysis/global/column.cpp
blob: 8eed8a8dfd948b7bf38bdbd287770f2946690c59 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "column.h"

#include "narrowing_visitor.h"

#include <yql/essentials/sql/v1/complete/syntax/format.h>

namespace NSQLComplete {

    namespace {

        // TODO: Extract it to `identifier.cpp` and reuse it also at `use.cpp`
        //       and replace `GetId` at `parse_tree.cpp`.
        class TIdentifierVisitor: public SQLv1Antlr4BaseVisitor {
        public:
            std::any visitCluster_expr(SQLv1::Cluster_exprContext* ctx) override {
                if (auto* x = ctx->pure_column_or_named()) {
                    return visit(x);
                }
                return {};
            }

            std::any visitTable_key(SQLv1::Table_keyContext* ctx) override {
                if (auto* x = ctx->id_table_or_type()) {
                    return visit(x);
                }
                return {};
            }

            std::any visitTerminal(antlr4::tree::TerminalNode* node) override {
                TString text = GetText(node);
                switch (node->getSymbol()->getType()) {
                    case SQLv1::TOKEN_ID_QUOTED: {
                        text = Unquoted(std::move(text));
                    } break;
                }
                return text;
            }

        private:
            TString GetText(antlr4::tree::ParseTree* tree) const {
                return TString(tree->getText());
            }
        };

        TMaybe<TString> GetId(antlr4::ParserRuleContext* ctx) {
            if (ctx == nullptr) {
                return Nothing();
            }

            std::any result = TIdentifierVisitor().visit(ctx);
            if (!result.has_value()) {
                return Nothing();
            }
            return std::any_cast<TString>(result);
        }

        class TInferenceVisitor: public SQLv1Antlr4BaseVisitor {
        public:
            std::any visitNamed_single_source(SQLv1::Named_single_sourceContext* ctx) override {
                SQLv1::Single_sourceContext* singleSource = ctx->single_source();
                if (singleSource == nullptr) {
                    return {};
                }

                std::any any = visit(singleSource);
                if (!any.has_value()) {
                    return {};
                }
                TColumnContext context = std::move(std::any_cast<TColumnContext>(any));

                TMaybe<TString> alias = GetAlias(ctx);
                if (alias.Empty()) {
                    return context;
                }

                return Renamed(std::move(context), *alias);
            }

            std::any visitTable_ref(SQLv1::Table_refContext* ctx) override {
                TString cluster = GetId(ctx->cluster_expr()).GetOrElse("");

                TMaybe<TString> path = GetId(ctx->table_key());
                if (path.Empty()) {
                    return {};
                }

                return TColumnContext{
                    .Tables = {
                        TTableId{std::move(cluster), std::move(*path)},
                    },
                };
            }

        private:
            TMaybe<TString> GetAlias(SQLv1::Named_single_sourceContext* ctx) const {
                TMaybe<TString> alias = GetId(ctx->an_id());
                alias = alias.Defined() ? alias : GetId(ctx->an_id_as_compat());
                return alias;
            }

            TColumnContext Renamed(TColumnContext context, TString alias) {
                Y_ENSURE(!alias.empty());

                for (TAliased<TTableId>& table : context.Tables) {
                    table.Alias = alias;
                }

                return context;
            }
        };

        class TVisitor: public TSQLv1NarrowingVisitor {
        public:
            TVisitor(antlr4::TokenStream* tokens, size_t cursorPosition)
                : TSQLv1NarrowingVisitor(tokens, cursorPosition)
            {
            }

            std::any visitSql_stmt_core(SQLv1::Sql_stmt_coreContext* ctx) override {
                if (IsEnclosing(ctx)) {
                    return visitChildren(ctx);
                }
                return {};
            }

            std::any visitSelect_core(SQLv1::Select_coreContext* ctx) override {
                SQLv1::Join_sourceContext* source = ctx->join_source(0);
                if (source == nullptr) {
                    source = ctx->join_source(1);
                }
                if (source == nullptr) {
                    return {};
                }

                return TInferenceVisitor().visit(ctx);
            }
        };

    } // namespace

    TMaybe<TColumnContext> InferColumnContext(
        SQLv1::Sql_queryContext* ctx,
        antlr4::TokenStream* tokens,
        size_t cursorPosition) {
        // TODO: add utility `auto ToMaybe<T>(std::any any) -> TMaybe<T>`
        std::any result = TVisitor(tokens, cursorPosition).visit(ctx);
        if (!result.has_value()) {
            return Nothing();
        }
        return std::any_cast<TColumnContext>(result);
    }

} // namespace NSQLComplete