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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#include "yql_library_compiler.h"
#include "yql_expr_optimize.h"
#include <yql/essentials/sql/sql.h>
#include <util/system/file.h>
#include <unordered_set>
#include <unordered_map>
namespace NYql {
namespace {
bool ReplaceNodes(TExprNode& node, const TNodeOnNodeOwnedMap& replaces, bool& hasChanges, TNodeSet& visited, TNodeSet& parents)
{
if (!node.ChildrenSize()) {
return true;
}
const auto pair = parents.emplace(&node);
if (!pair.second) {
return false;
}
if (!visited.emplace(&node).second) {
parents.erase(pair.first);
return true;
}
for (ui32 i = 0U; i < node.ChildrenSize(); ++i) {
auto& child = node.ChildRef(i);
if (const auto it = replaces.find(node.Child(i)); replaces.cend() != it) {
child = it->second;
hasChanges = true;
}
if (!ReplaceNodes(*node.Child(i), replaces, hasChanges, visited, parents)) {
child.Reset();
return false;
}
}
parents.erase(pair.first);
return true;
}
bool ReplaceNodes(TExprNode& node, const TNodeOnNodeOwnedMap& replaces, bool& hasChanges) {
TNodeSet visited;
TNodeSet parents;
return ReplaceNodes(node, replaces, hasChanges, visited, parents);
}
TString Load(const TString& path)
{
TFile file(path, EOpenModeFlag::RdOnly);
if (file.GetLength() <= 0)
return TString();
std::vector<TString::value_type> buffer(file.GetLength());
file.Load(buffer.data(), buffer.size());
return TString(buffer.data(), buffer.size());
}
}
bool OptimizeLibrary(TLibraryCohesion& cohesion, TExprContext& ctx) {
TExprNode::TListType tupleItems;
for (const auto& x : cohesion.Exports.Symbols()) {
tupleItems.push_back(x.second);
}
auto root = ctx.NewList(TPositionHandle(), std::move(tupleItems));
for (;;) {
auto status = ExpandApply(root, root, ctx);
if (status == IGraphTransformer::TStatus::Error) {
return false;
}
if (status == IGraphTransformer::TStatus::Ok) {
ctx.Step.Repeat(TExprStep::ExpandApplyForLambdas);
break;
}
}
ui32 index = 0;
for (auto& x : cohesion.Exports.Symbols(ctx)) {
x.second = root->ChildPtr(index++);
}
return true;
}
bool CompileLibrary(const TString& alias, const TString& script, TExprContext& ctx, TLibraryCohesion& cohesion, bool optimize)
{
TAstParseResult res;
if (alias.EndsWith(".sql")) {
NSQLTranslation::TTranslationSettings translationSettings;
translationSettings.SyntaxVersion = 1;
translationSettings.Mode = NSQLTranslation::ESqlMode::LIBRARY;
res = NSQLTranslation::SqlToYql(script, translationSettings);
} else {
res = ParseAst(script, nullptr, alias);
}
if (!res.IsOk()) {
for (const auto& originalError : res.Issues) {
TIssue error(originalError);
TStringBuilder message;
message << error.GetMessage() << " (at " << alias << ")";
error.SetMessage(message);
ctx.AddError(error);
}
return false;
}
if (!CompileExpr(*res.Root, cohesion, ctx))
return false;
if (!optimize) {
return true;
}
return OptimizeLibrary(cohesion, ctx);
}
bool LinkLibraries(THashMap<TString, TLibraryCohesion>& libs, TExprContext& ctx, TExprContext& ctxToClone, const TModulesTable* loadedModules) {
std::function<const TExportTable*(const TString&)> f = [loadedModules](const TString& normalizedModuleName) -> const TExportTable* {
if (!loadedModules) {
return nullptr;
}
return loadedModules->FindPtr(normalizedModuleName);
};
return LinkLibraries(libs, ctx, ctxToClone, f);
}
bool LinkLibraries(THashMap<TString, TLibraryCohesion>& libs, TExprContext& ctx, TExprContext& ctxToClone, const std::function<const TExportTable*(const TString&)>& module2ExportTable)
{
TNodeOnNodeOwnedMap clones, replaces;
for (const auto& lib : libs) {
for (const auto& import : lib.second.Imports) {
if (import.first->Dead()) {
continue;
}
if (import.second.first == lib.first) {
ctx.AddError(TIssue(ctxToClone.GetPosition(import.first->Pos()),
TStringBuilder() << "Library '" << lib.first << "' tries to import itself."));
return false;
}
const auto* exportTable = module2ExportTable(TModuleResolver::NormalizeModuleName(import.second.first));
const bool externalModule = exportTable;
if (!exportTable) {
if (const auto it = libs.find(import.second.first); libs.cend() != it) {
exportTable = &it->second.Exports;
}
}
if (!exportTable) {
ctx.AddError(TIssue(ctxToClone.GetPosition(import.first->Pos()),
TStringBuilder() << "Library '" << lib.first << "' has unresolved dependency from '" << import.second.first << "'."));
return false;
}
if (const auto ex = exportTable->Symbols().find(import.second.second); exportTable->Symbols().cend() != ex) {
replaces[import.first] = externalModule ? ctxToClone.DeepCopy(*ex->second, exportTable->ExprCtx(), clones, true, false) : ex->second;
} else {
ctx.AddError(TIssue(ctxToClone.GetPosition(import.first->Pos()),
TStringBuilder() << "Library '" << lib.first << "' has unresolved symbol '" << import.second.second << "' from '" << import.second.first << "'."));
return false;
}
}
}
if (!replaces.empty()) {
for (auto& lib : libs) {
for (auto& expo : lib.second.Exports.Symbols(lib.second.Exports.ExprCtx())) {
if (const auto find = replaces.find(expo.second.Get()); replaces.cend() != find)
expo.second = find->second;
}
}
}
for (bool hasChanges = !replaces.empty(); hasChanges;) {
hasChanges = false;
for (const auto& lib : libs) {
for (const auto& expo : lib.second.Exports.Symbols()) {
if (!ReplaceNodes(*expo.second, replaces, hasChanges)) {
ctx.AddError(TIssue(ctxToClone.GetPosition(expo.second->Pos()),
TStringBuilder() << "Cross reference detected under '" << expo.first << "' in '" << lib.first << "'."));
return false;
}
}
}
}
return true;
}
bool CompileLibraries(const TUserDataTable& userData, TExprContext& ctx, TModulesTable& modules, bool optimize)
{
THashMap<TString, TLibraryCohesion> libs;
for (const auto& data : userData) {
if (data.first.IsFile() && data.second.Usage.Test(EUserDataBlockUsage::Library)) {
TString libraryData;
const TString& alias = data.first.Alias();
if (data.second.Type == EUserDataType::PATH) {
libraryData = Load(data.second.Data);
} else if (data.second.Type == EUserDataType::RAW_INLINE_DATA) {
libraryData = data.second.Data;
}
if (!libraryData.empty()) {
if (CompileLibrary(alias, libraryData, ctx, libs[alias], optimize))
modules[TModuleResolver::NormalizeModuleName(alias)] = libs[alias].Exports;
else
return false;
}
}
}
return LinkLibraries(libs, ctx, ctx);
}
}
|