aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/name/service/name_service.h
blob: 672d2d62c46a7d8b0c9f3c7ffce5400b8c68b5c8 (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
154
155
156
157
158
159
#pragma once

#include <yql/essentials/sql/v1/complete/core/name.h>
#include <yql/essentials/sql/v1/complete/core/statement.h>

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

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

namespace NSQLComplete {

    struct TIdentifier {
        TString Identifier;
    };

    struct TNamespaced {
        TString Namespace;
    };

    struct TDescribed {
        TMaybe<TString> Description;
    };

    struct TKeyword {
        TString Content;
    };

    struct TPragmaName: TIdentifier {
        struct TConstraints: TNamespaced {};
    };

    struct TTypeName: TIdentifier, TDescribed {
        struct TConstraints {};

        enum class EKind {
            Simple,
            Container,
            Parameterized,
        };

        EKind Kind = EKind::Simple;
    };

    struct TFunctionName: TIdentifier, TDescribed {
        struct TConstraints: TNamespaced {
            ENodeKind ReturnType;
        };
    };

    struct THintName: TIdentifier {
        struct TConstraints {
            EStatementKind Statement;
        };
    };

    struct TObjectNameConstraints {
        TString Provider;
        TString Cluster;
        THashSet<EObjectKind> Kinds;
    };

    struct TFolderName: TIdentifier {
    };

    struct TTableName: TIdentifier {
    };

    struct TClusterName: TIdentifier {
        struct TConstraints: TNamespaced {};
    };

    struct TColumnName: TIdentifier {
        struct TConstraints {
            TVector<TAliased<TTableId>> Tables;
            THashMap<TString, THashSet<TString>> WithoutByTableAlias;
        };

        TString TableAlias;
    };

    struct TBindingName: TIdentifier {
    };

    struct TUnknownName {
        TString Content;
        TString Type;
    };

    using TGenericName = std::variant<
        TKeyword,
        TPragmaName,
        TTypeName,
        TFunctionName,
        THintName,
        TFolderName,
        TTableName,
        TClusterName,
        TColumnName,
        TBindingName,
        TUnknownName>;

    struct TNameConstraints {
        TMaybe<TPragmaName::TConstraints> Pragma;
        TMaybe<TTypeName::TConstraints> Type;
        TMaybe<TFunctionName::TConstraints> Function;
        TMaybe<THintName::TConstraints> Hint;
        TMaybe<TObjectNameConstraints> Object;
        TMaybe<TClusterName::TConstraints> Cluster;
        TMaybe<TColumnName::TConstraints> Column;

        bool IsEmpty() const {
            return !Pragma &&
                   !Type &&
                   !Function &&
                   !Hint &&
                   !Object &&
                   !Cluster &&
                   !Column;
        }

        TGenericName Qualified(TGenericName unqualified) const;
        TGenericName Unqualified(TGenericName qualified) const;
        TVector<TGenericName> Qualified(TVector<TGenericName> unqualified) const;
        TVector<TGenericName> Unqualified(TVector<TGenericName> qualified) const;
    };

    struct TNameRequest {
        TVector<TString> Keywords;
        TNameConstraints Constraints;
        TString Prefix = "";
        size_t Limit = 128;

        bool IsEmpty() const {
            return Keywords.empty() && Constraints.IsEmpty();
        }
    };

    struct TNameResponse {
        TVector<TGenericName> RankedNames;
        TMaybe<size_t> NameHintLength = Nothing();

        bool IsEmpty() const {
            return RankedNames.empty();
        }
    };

    class INameService: public TThrRefBase {
    public:
        using TPtr = TIntrusivePtr<INameService>;

        virtual NThreading::TFuture<TNameResponse> Lookup(const TNameRequest& request) const = 0;
    };

    TString NormalizeName(TStringBuf name);

} // namespace NSQLComplete