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
|
#include "ranking.h"
#include <yql/essentials/sql/v1/complete/name/service/name_service.h>
#include <util/charset/utf8.h>
namespace NSQLComplete {
class TRanking: public IRanking {
private:
struct TRow {
TGenericName Name;
size_t Weight;
};
public:
explicit TRanking(TFrequencyData frequency)
: Frequency_(std::move(frequency))
{
}
void CropToSortedPrefix(
TVector<TGenericName>& names,
const TNameConstraints& constraints,
size_t limit) const override {
limit = Min(limit, names.size());
TVector<TRow> rows;
rows.reserve(names.size());
for (TGenericName& name : names) {
name = constraints.Qualified(std::move(name));
size_t weight = Weight(name);
rows.emplace_back(std::move(name), weight);
}
::PartialSort(
std::begin(rows), std::begin(rows) + limit, std::end(rows),
[this](const TRow& lhs, const TRow& rhs) {
const size_t lhs_weight = ReversedWeight(lhs.Weight);
const auto lhs_content = ContentView(lhs.Name);
const size_t rhs_weight = ReversedWeight(rhs.Weight);
const auto rhs_content = ContentView(rhs.Name);
return std::tie(lhs_weight, lhs_content) <
std::tie(rhs_weight, rhs_content);
});
names.crop(limit);
rows.crop(limit);
for (size_t i = 0; i < limit; ++i) {
names[i] = constraints.Unqualified(std::move(rows[i].Name));
}
}
private:
size_t Weight(const TGenericName& name) const {
return std::visit([this](const auto& name) -> size_t {
using T = std::decay_t<decltype(name)>;
auto content = NormalizeName(ContentView(name));
if constexpr (std::is_same_v<T, TKeyword>) {
if (auto weight = Frequency_.Keywords.FindPtr(content)) {
return *weight;
}
}
if constexpr (std::is_same_v<T, TPragmaName>) {
if (auto weight = Frequency_.Pragmas.FindPtr(content)) {
return *weight;
}
}
if constexpr (std::is_same_v<T, TFunctionName>) {
if (auto weight = Frequency_.Functions.FindPtr(content)) {
return *weight;
}
}
if constexpr (std::is_same_v<T, TTypeName>) {
if (auto weight = Frequency_.Types.FindPtr(content)) {
return *weight;
}
}
if constexpr (std::is_same_v<T, THintName>) {
if (auto weight = Frequency_.Hints.FindPtr(content)) {
return *weight;
}
}
if constexpr (std::is_same_v<T, TFolderName> ||
std::is_same_v<T, TTableName>) {
return std::numeric_limits<size_t>::max();
}
if constexpr (std::is_same_v<T, TClusterName>) {
return std::numeric_limits<size_t>::max() - 8;
}
return 0;
}, name);
}
static size_t ReversedWeight(size_t weight) {
return std::numeric_limits<size_t>::max() - weight;
}
TStringBuf ContentView(const TGenericName& name Y_LIFETIME_BOUND) const {
return std::visit([](const auto& name) -> TStringBuf {
using T = std::decay_t<decltype(name)>;
if constexpr (std::is_base_of_v<TKeyword, T>) {
return name.Content;
}
if constexpr (std::is_base_of_v<TIndentifier, T>) {
return name.Indentifier;
}
if constexpr (std::is_base_of_v<TUnkownName, T>) {
return name.Content;
}
}, name);
}
TFrequencyData Frequency_;
};
IRanking::TPtr MakeDefaultRanking() {
return MakeDefaultRanking(LoadFrequencyData());
}
IRanking::TPtr MakeDefaultRanking(const TFrequencyData& frequency) {
return MakeIntrusive<TRanking>(Pruned(frequency));
}
} // namespace NSQLComplete
|