aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/analysis/global/use.cpp
blob: da7dc6a5751ae3c8bfbcf1f8ed0a1389fe7322f7 (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
#include "use.h"

namespace NSQLComplete {

    namespace {

        class TVisitor: public SQLv1Antlr4BaseVisitor {
        public:
            TVisitor(antlr4::TokenStream* tokens, size_t cursorPosition)
                : Tokens_(tokens)
                , CursorPosition_(cursorPosition)
            {
            }

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

            std::any visitUse_stmt(SQLv1::Use_stmtContext* ctx) override {
                SQLv1::Cluster_exprContext* expr = ctx->cluster_expr();
                if (!expr) {
                    return {};
                }

                std::string provider;
                std::string cluster;

                if (SQLv1::An_idContext* ctx = expr->an_id()) {
                    provider = ctx->getText();
                }

                if (SQLv1::Pure_column_or_namedContext* ctx = expr->pure_column_or_named()) {
                    cluster = ctx->getText();
                }

                if (cluster.empty()) {
                    return {};
                }

                return TUseContext{
                    .Provider = std::move(provider),
                    .Cluster = std::move(cluster),
                };
            }

            std::any aggregateResult(std::any aggregate, std::any nextResult) override {
                if (nextResult.has_value()) {
                    return nextResult;
                }
                return aggregate;
            }

            bool shouldVisitNextChild(antlr4::tree::ParseTree* node, const std::any& /*currentResult*/) override {
                return TextInterval(node).a < static_cast<ssize_t>(CursorPosition_);
            }

        private:
            bool IsEnclosing(antlr4::tree::ParseTree* tree) const {
                return TextInterval(tree).properlyContains(CursorInterval());
            }

            antlr4::misc::Interval TextInterval(antlr4::tree::ParseTree* tree) const {
                auto tokens = tree->getSourceInterval();
                if (tokens.b == -1) {
                    tokens.b = tokens.a;
                }
                return antlr4::misc::Interval(
                    Tokens_->get(tokens.a)->getStartIndex(),
                    Tokens_->get(tokens.b)->getStopIndex());
            }

            antlr4::misc::Interval CursorInterval() const {
                return antlr4::misc::Interval(CursorPosition_, CursorPosition_);
            }

            antlr4::TokenStream* Tokens_;
            size_t CursorPosition_;
        };

    } // namespace

    TMaybe<TUseContext> FindUseStatement(
        SQLv1::Sql_queryContext* ctx,
        antlr4::TokenStream* tokens,
        size_t cursorPosition) {
        std::any result = TVisitor(tokens, cursorPosition).visit(ctx);
        if (!result.has_value()) {
            return Nothing();
        }
        return std::any_cast<TUseContext>(result);
    }

} // namespace NSQLComplete