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
|
#include "yql_mounts.h"
#include <yql/essentials/core/yql_library_compiler.h>
#include <library/cpp/resource/resource.h>
#include <util/folder/path.h>
#include <util/stream/file.h>
namespace NYql {
namespace {
using namespace NUserData;
void AddLibraryFromResource(TUserDataTable& userDataTable, const TString& resourceName) {
auto& block = userDataTable[TUserDataKey::File(resourceName)];
block.Data = NResource::Find(resourceName);
block.Type = EUserDataType::RAW_INLINE_DATA;
block.Usage.Set(EUserDataBlockUsage::Library, true);
}
TUserDataKey CreateKey(const NUserData::TUserData& item) {
TString name = (item.Disposition_ == EDisposition::RESOURCE) ? item.Content_ : item.Name_;
if (!name.StartsWith('/')) {
name = GetDefaultFilePrefix() + name;
}
if (item.Type_ == EType::UDF) {
return TUserDataKey::Udf(name);
} else {
return TUserDataKey::File(name);
}
}
void AddUserDataToTable(
TUserDataTable& userDataTable,
const NUserData::TUserData& item) {
auto& block = userDataTable[CreateKey(item)];
switch (item.Disposition_) {
case EDisposition::INLINE:
block.Data = item.Content_;
block.Type = EUserDataType::RAW_INLINE_DATA;
break;
case EDisposition::RESOURCE:
case EDisposition::RESOURCE_FILE:
block.Data = NResource::Find(item.Content_);
block.Type = EUserDataType::RAW_INLINE_DATA;
break;
case EDisposition::FILESYSTEM:
block.Data = item.Content_;
block.Type = EUserDataType::PATH;
break;
case EDisposition::URL:
block.Data = item.Content_;
block.Type = EUserDataType::URL;
break;
default:
ythrow yexception() << "Unknown disposition for user data \"" << item.Name_ << "\": " << item.Disposition_;
}
switch (item.Type_) {
case EType::LIBRARY:
block.Usage.Set(EUserDataBlockUsage::Library, true);
break;
case EType::FILE:
block.Usage.Set(EUserDataBlockUsage::Content, true);
break;
case EType::UDF:
block.Usage.Set(EUserDataBlockUsage::Udf, true);
break;
default:
ythrow yexception() << "Unknown type for user data \"" << item.Name_ << "\": " << item.Type_;
}
if (item.Type_ == EType::LIBRARY) {
switch (item.Disposition_) {
case EDisposition::INLINE:
case EDisposition::RESOURCE:
case EDisposition::RESOURCE_FILE:
case EDisposition::FILESYSTEM: {
if (item.Disposition_ == EDisposition::FILESYSTEM) {
TFsPath path(block.Data);
if (path.Exists() && path.IsFile()) {
TFileInput input(path);
block.Data = input.ReadAll();
} else {
ythrow yexception() << "File for user data \"" << item.Name_ << "\" does not exist: " << block.Data;
}
block.Type = EUserDataType::RAW_INLINE_DATA;
}
} break;
default:
ythrow yexception() << item.Disposition_ << " disposition is not yet supported for libraries (specified for \"" << item.Name_ << "\")";
}
}
}
}
void LoadYqlDefaultMounts(TUserDataTable& userData) {
AddLibraryFromResource(userData, "/lib/yql/aggregate.yql");
AddLibraryFromResource(userData, "/lib/yql/window.yql");
AddLibraryFromResource(userData, "/lib/yql/id.yql");
AddLibraryFromResource(userData, "/lib/yql/sqr.yql");
AddLibraryFromResource(userData, "/lib/yql/core.yql");
AddLibraryFromResource(userData, "/lib/yql/walk_folders.yql");
}
TUserDataTable GetYqlModuleResolverImpl(
TExprContext* rawCtx,
IModuleResolver::TPtr& moduleResolver,
const TVector<NUserData::TUserData>& userData,
const THashMap<TString, TString>& clusterMapping,
const THashSet<TString>& sqlFlags,
bool optimizeLibraries,
THolder<TExprContext> ownedCtx)
{
auto ctx = rawCtx ? rawCtx : ownedCtx.Get();
Y_ENSURE(ctx);
TUserDataTable mounts;
LoadYqlDefaultMounts(mounts);
TModulesTable modulesTable;
if (!CompileLibraries(mounts, *ctx, modulesTable, optimizeLibraries)) {
return {};
}
for (const auto& item : userData) {
AddUserDataToTable(mounts, item);
}
moduleResolver = std::make_shared<TModuleResolver>(std::move(modulesTable), ctx->NextUniqueId,
clusterMapping, sqlFlags, optimizeLibraries, std::move(ownedCtx));
return mounts;
}
TUserDataTable GetYqlModuleResolver(
TExprContext& ctx,
IModuleResolver::TPtr& moduleResolver,
const TVector<NUserData::TUserData>& userData,
const THashMap<TString, TString>& clusterMapping,
const THashSet<TString>& sqlFlags,
bool optimizeLibraries) {
return GetYqlModuleResolverImpl(&ctx, moduleResolver, userData, clusterMapping, sqlFlags, optimizeLibraries, nullptr);
}
bool GetYqlDefaultModuleResolver(
TExprContext& ctx,
IModuleResolver::TPtr& moduleResolver,
const THashMap<TString, TString>& clusterMapping,
bool optimizeLibraries) {
return !GetYqlModuleResolverImpl(&ctx, moduleResolver, {}, clusterMapping, {}, optimizeLibraries, nullptr).empty();
}
bool GetYqlDefaultModuleResolverWithContext(
IModuleResolver::TPtr& moduleResolver,
const THashMap<TString, TString>& clusterMapping,
bool optimizeLibraries) {
return !GetYqlModuleResolverImpl(nullptr, moduleResolver, {}, clusterMapping, {}, optimizeLibraries, MakeHolder<TExprContext>()).empty();
}
}
|