aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/sql_complete.h
blob: ce8a7c5296f3826b76eca76fc3e8ddad38c1944e (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
#pragma once

#include <yql/essentials/sql/v1/complete/core/input.h>
#include <yql/essentials/sql/v1/complete/core/environment.h>
#include <yql/essentials/sql/v1/complete/name/service/name_service.h>
#include <yql/essentials/sql/v1/lexer/lexer.h>

#include <library/cpp/threading/future/future.h>

#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/generic/hash.h>
#include <util/generic/hash_set.h>

namespace NSQLComplete {

    struct TCompletedToken {
        TStringBuf Content;
        size_t SourcePosition = 0;
    };

    enum class ECandidateKind {
        Keyword,
        PragmaName,
        TypeName,
        FunctionName,
        HintName,
        FolderName,
        TableName,
        ClusterName,
        ColumnName,
        BindingName,
        UnknownName,
    };

    struct TCandidate {
        ECandidateKind Kind;
        TString Content;
        size_t CursorShift = 0;

        friend bool operator==(const TCandidate& lhs, const TCandidate& rhs) = default;
    };

    struct TCompletion {
        TCompletedToken CompletedToken;
        TVector<TCandidate> Candidates;
    };

    // TODO(YQL-19747): Make it thread-safe.
    class ISqlCompletionEngine {
    public:
        using TPtr = THolder<ISqlCompletionEngine>;

        struct TConfiguration {
            friend class TSqlCompletionEngine;
            friend ISqlCompletionEngine::TConfiguration MakeYDBConfiguration();
            friend ISqlCompletionEngine::TConfiguration MakeYQLConfiguration();
            friend ISqlCompletionEngine::TConfiguration MakeConfiguration(THashSet<TString> allowedStmts);

        public:
            size_t Limit = 256;

        private:
            THashSet<TString> IgnoredRules_;
            THashMap<TString, THashSet<TString>> DisabledPreviousByToken_;
            THashMap<TString, THashSet<TString>> ForcedPreviousByToken_;
        };

        virtual ~ISqlCompletionEngine() = default;

        virtual TCompletion
        Complete(TCompletionInput input, TEnvironment env = {}) = 0;

        virtual NThreading::TFuture<TCompletion> // TODO(YQL-19747): Migrate YDB CLI to `Complete` method
        CompleteAsync(TCompletionInput input, TEnvironment env = {}) = 0;
    };

    using TLexerSupplier = std::function<NSQLTranslation::ILexer::TPtr(bool ansi)>;

    ISqlCompletionEngine::TConfiguration MakeYDBConfiguration();

    ISqlCompletionEngine::TConfiguration MakeYQLConfiguration();

    ISqlCompletionEngine::TPtr MakeSqlCompletionEngine(
        TLexerSupplier lexer,
        INameService::TPtr names,
        ISqlCompletionEngine::TConfiguration configuration = {});

} // namespace NSQLComplete