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
|
#include "yql_configuration_transformer.h"
#include <yql/essentials/core/yql_graph_transformer.h>
#include <yql/essentials/core/yql_expr_optimize.h>
#include <yql/essentials/core/yql_expr_type_annotation.h>
#include <yql/essentials/core/expr_nodes/yql_expr_nodes.h>
#include <util/generic/maybe.h>
#include <util/string/vector.h>
namespace NYql {
namespace NCommon {
using namespace NNodes;
TProviderConfigurationTransformer::TProviderConfigurationTransformer(TSettingDispatcher::TPtr dispatcher,
const TTypeAnnotationContext& types, const TString& provider, const THashSet<TStringBuf>& configureCallables)
: Dispatcher(dispatcher)
, Types(types)
, Provider(provider)
, ConfigureCallables(configureCallables)
{
if (ConfigureCallables.empty()) {
ConfigureCallables.insert(TCoConfigure::CallableName());
}
}
IGraphTransformer::TStatus TProviderConfigurationTransformer::DoTransform(TExprNode::TPtr input,
TExprNode::TPtr& output, TExprContext& ctx)
{
output = input;
if (ctx.Step.IsDone(TExprStep::Configure)) {
return TStatus::Ok;
}
TOptimizeExprSettings settings(nullptr);
settings.VisitChanges = true;
auto status = OptimizeExpr(input, output, [&](const TExprNode::TPtr& node, TExprContext& ctx) -> TExprNode::TPtr {
if (node->IsCallable(ConfigureCallables)) {
if (!EnsureMinArgsCount(*node, 2, ctx)) {
return nullptr;
}
if (!TCoDataSource::Match(node->Child(TCoConfigure::idx_DataSource))) {
return node;
}
auto ds = node->Child(TCoConfigure::idx_DataSource);
if (ds->Child(TCoDataSource::idx_Category)->Content() != Provider) {
return node;
}
if (!EnsureMinArgsCount(*ds, 2, ctx)) {
return nullptr;
}
if (!EnsureAtom(*ds->Child(1), ctx)) {
return nullptr;
}
auto clusterName = TString(ds->Child(1)->Content());
if (!EnsureMinArgsCount(*node, 3, ctx)) {
return nullptr;
}
if (!EnsureAtom(*node->Child(2), ctx)) {
return nullptr;
}
auto atom = node->Child(2)->Content();
if (atom == TStringBuf("Attr")) {
if (!EnsureMinArgsCount(*node, 4, ctx)) {
return nullptr;
}
if (!EnsureMaxArgsCount(*node, 5, ctx)) {
return nullptr;
}
if (!EnsureAtom(*node->Child(3), ctx)) {
return nullptr;
}
auto name = TString(node->Child(3)->Content());
if (name.StartsWith('_')) {
ctx.AddError(TIssue(ctx.GetPosition(node->Child(3)->Pos()),
TStringBuilder() << "Failed to override system setting: " << name));
return nullptr;
}
TMaybe<TString> value;
if (node->ChildrenSize() == 5) {
if (node->Child(4)->IsCallable("EvaluateAtom")) {
return node;
}
if (!EnsureAtom(*node->Child(4), ctx)) {
return nullptr;
}
value = TString(node->Child(4)->Content());
}
if (!HandleAttr(node->Child(3)->Pos(), clusterName, name, value, ctx)) {
return nullptr;
}
} else if (atom == TStringBuf("Auth")) {
if (!EnsureArgsCount(*node, 4, ctx)) {
return nullptr;
}
if (!EnsureAtom(*node->Child(3), ctx)) {
return nullptr;
}
auto credAlias = TString(node->Child(3)->Content());
if (!HandleAuth(node->Child(3)->Pos(), clusterName, credAlias, ctx)) {
return nullptr;
}
} else {
ctx.AddError(TIssue(ctx.GetPosition(node->Child(2)->Pos()), TStringBuilder()
<< "Unsupported configuration option: " << atom));
return nullptr;
}
}
return node;
}, ctx, settings);
return status;
}
bool TProviderConfigurationTransformer::HandleAttr(TPositionHandle pos, const TString& cluster, const TString& name,
const TMaybe<TString>& value, TExprContext& ctx)
{
Y_UNUSED(pos);
Y_UNUSED(ctx);
return Dispatcher->Dispatch(cluster, name, value, TSettingDispatcher::EStage::STATIC, TSettingDispatcher::GetErrorCallback(pos, ctx));
}
bool TProviderConfigurationTransformer::HandleAuth(TPositionHandle pos, const TString& cluster, const TString& alias,
TExprContext& ctx)
{
auto cred = Types.Credentials->FindCredential(alias);
if (!cred) {
ctx.AddError(TIssue(ctx.GetPosition(pos), TStringBuilder() << "Unknown credential: " << alias));
return false;
}
if (cred->Category != Provider) {
ctx.AddError(TIssue(ctx.GetPosition(pos), TStringBuilder()
<< "Mismatch credential category, expected: "
<< Provider << ", but found: " << cred->Category));
return false;
}
return Dispatcher->Dispatch(cluster, "Auth", cred->Content, TSettingDispatcher::EStage::STATIC, TSettingDispatcher::GetErrorCallback(pos, ctx));
}
THolder<IGraphTransformer> CreateProviderConfigurationTransformer(
TSettingDispatcher::TPtr dispatcher,
const TTypeAnnotationContext& types,
const TString& provider) {
return THolder(new TProviderConfigurationTransformer(dispatcher, types, provider));
}
} // namespace NCommon
} // namespace NYql
|