summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/ide/completion/name/service/schema/name_service.cpp
blob: a2cb07fd7191f2b375cb4af87c5d4c4f6c24bc1c (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "name_service.h"

#include <library/cpp/threading/future/wait/wait.h>
#include <library/cpp/iterator/iterate_values.h>

namespace NSQLComplete {

namespace {

class TNameService: public INameService {
public:
    explicit TNameService(ISchema::TPtr schema)
        : Schema_(std::move(schema))
    {
    }

    NThreading::TFuture<TNameResponse> Lookup(const TNameRequest& request) const override {
        if (request.Constraints.Object) {
            return Schema_
                ->List(ToListRequest(request))
                .Apply(ToListNameResponse);
        }

        if (request.Constraints.Column && !request.Constraints.Column->Tables.empty()) {
            return BatchDescribe(
                request.Constraints.Column->Tables,
                request.Constraints.Column->WithoutByTableAlias,
                request.Prefix,
                request.Limit);
        }

        return NThreading::MakeFuture<TNameResponse>({});
    }

private:
    NThreading::TFuture<TNameResponse> BatchDescribe(
        TVector<TAliased<TTableId>> tables,
        THashMap<TString, THashSet<TString>> withoutByTableAlias,
        TString prefix,
        ui64 limit) const {
        THashMap<TTableId, TVector<TString>> aliasesByTable;
        for (TAliased<TTableId> table : std::move(tables)) {
            aliasesByTable[std::move(static_cast<TTableId&>(table))]
                .emplace_back(std::move(table.Alias));
        }

        THashMap<TTableId, NThreading::TFuture<TDescribeTableResponse>> futuresByTable;
        for (const auto& [table, aliases] : aliasesByTable) {
            TString columnPrefix = prefix;
            for (const auto& alias : aliases) {
                if (alias.StartsWith(prefix)) {
                    columnPrefix = "";
                    break;
                }
            }

            TDescribeTableRequest request = {
                .TableCluster = table.Cluster,
                .TablePath = table.Path,
                .ColumnPrefix = columnPrefix,
                .ColumnsLimit = limit,
            };

            futuresByTable.emplace(table, Schema_->Describe(request));
        }

        auto futuresIt = IterateValues(futuresByTable);
        TVector<NThreading::TFuture<TDescribeTableResponse>> futures(begin(futuresIt), end(futuresIt));

        return NThreading::WaitAll(std::move(futures))
            .Apply([aliasesByTable = std::move(aliasesByTable),
                    futuresByTable = std::move(futuresByTable),
                    withoutByTableAlias = std::move(withoutByTableAlias)](auto) mutable {
                TNameResponse response;

                for (auto [table, f] : futuresByTable) {
                    TDescribeTableResponse description = f.ExtractValue();
                    for (const TString& column : description.Columns) {
                        const auto& aliases = aliasesByTable[table];
                        for (const TString& alias : aliases) {
                            if ((withoutByTableAlias[alias].contains(column)) ||
                                (alias.empty() && 1 < aliases.size())) {
                                continue;
                            }

                            TColumnName name;
                            name.Identifier = column;
                            name.TableAlias = alias;

                            response.RankedNames.emplace_back(std::move(name));
                        }
                    }
                }

                return response;
            });
    }

    static TListRequest ToListRequest(const TNameRequest& request) {
        return {
            .Cluster = ClusterName(*request.Constraints.Object),
            .Path = request.Prefix,
            .Filter = ToListFilter(request.Constraints),
            .Limit = request.Limit,
        };
    }

    static TString ClusterName(const TObjectNameConstraints& constraints) {
        return constraints.Cluster;
    }

    static TListFilter ToListFilter(const TNameConstraints& constraints) {
        const auto& kinds = constraints.Object->Kinds;

        TListFilter filter;
        filter.Types = THashSet<TString>();
        filter.IsUnknownAllowed = kinds.contains(EObjectKind::Unknown);
        for (EObjectKind kind : kinds) {
            if (kind == EObjectKind::Unknown) {
                continue;
            }

            filter.Types->emplace(ToFolderEntry(kind));
        }
        return filter;
    }

    static TString ToFolderEntry(EObjectKind kind) {
        switch (kind) {
            case EObjectKind::Folder:
                return TFolderEntry::Folder;
            case EObjectKind::Table:
                return TFolderEntry::Table;
            case EObjectKind::Unknown:
                ythrow yexception()
                    << "Unknown is a type class and "
                    << "can not be a folder entry";
        }
    }

    static TNameResponse ToListNameResponse(NThreading::TFuture<TListResponse> f) {
        TListResponse list = f.ExtractValue();

        TNameResponse response;
        for (auto& entry : list.Entries) {
            response.RankedNames.emplace_back(ToGenericName(std::move(entry)));
        }
        response.NameHintLength = list.NameHintLength;
        return response;
    }

    static TGenericName ToGenericName(TFolderEntry entry) {
        TGenericName name;
        if (entry.Type == TFolderEntry::Folder) {
            TFolderName local;
            local.Identifier = std::move(entry.Name);
            name = std::move(local);
        } else if (entry.Type == TFolderEntry::Table) {
            TTableName local;
            local.Identifier = std::move(entry.Name);
            name = std::move(local);
        } else {
            TUnknownName local;
            local.Content = std::move(entry.Name);
            local.Type = std::move(entry.Type);
            name = std::move(local);
        }
        return name;
    }

    ISchema::TPtr Schema_;
};

} // namespace

INameService::TPtr MakeSchemaNameService(ISchema::TPtr schema) {
    return new TNameService(std::move(schema));
}

} // namespace NSQLComplete