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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#include "yql_dq_state.h"
#include "yql_dq_provider.h"
#include <ydb/library/yql/providers/common/provider/yql_provider_names.h>
#include <ydb/library/yql/providers/dq/expr_nodes/dqs_expr_nodes.h>
#include <ydb/library/yql/providers/dq/interface/yql_dq_integration.h>
#include <ydb/library/yql/core/expr_nodes/yql_expr_nodes.h>
#include <ydb/library/yql/core/yql_expr_optimize.h>
#include <ydb/library/yql/core/yql_graph_transformer.h>
#include <ydb/library/yql/core/yql_expr_type_annotation.h>
#include <ydb/library/yql/core/yql_opt_utils.h>
#include <ydb/library/yql/ast/yql_expr.h>
#include <ydb/library/yql/utils/log/log.h>
#include <ydb/library/yql/dq/opt/dq_opt.h>
#include <util/generic/scope.h>
namespace NYql {
using namespace NNodes;
namespace {
const THashSet<TStringBuf> VALID_SOURCES = {DqProviderName, ConfigProviderName, YtProviderName, ClickHouseProviderName, YdbProviderName};
const THashSet<TStringBuf> VALID_SINKS = {ResultProviderName, YtProviderName};
const THashSet<TStringBuf> UNSUPPORTED_CALLABLE = { TCoForwardList::CallableName() };
}
namespace NDq {
bool CheckJoinColumns(const TExprBase& node);
bool CheckJoinLinkSettings(const TExprBase& node);
} // namespace NDq
class TDqsRecaptureTransformer : public TSyncTransformerBase {
public:
TDqsRecaptureTransformer(TDqStatePtr state)
: State_(state)
{
}
TStatus DoTransform(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) final {
output = input;
if (ctx.Step.IsDone(TExprStep::Recapture)) {
return TStatus::Ok;
}
Y_SCOPE_EXIT(&) {
FlushStatistics();
};
if (State_->ExternalUser) {
Statistics_["DqExternalUser"]++;
return TStatus::Ok;
}
if (State_->TypeCtx->ForceDq) {
Statistics_["DqForce"]++;
}
if (!State_->TypeCtx->ForceDq) {
if (!State_->Settings->AnalyzeQuery.Get().GetOrElse(false)) {
Statistics_["DqAnalyzerOff"]++;
}
if (State_->TypeCtx->PureResultDataSource != DqProviderName) {
Statistics_["DqPureResultDataSourceMismatch"]++;
}
if (State_->TypeCtx->PureResultDataSource != DqProviderName || !State_->Settings->AnalyzeQuery.Get().GetOrElse(false)) {
return TStatus::Ok;
}
Statistics_["DqAnalyzerOn"]++;
ui64 dataSize = 0;
bool good = true;
bool hasJoin = false;
TNodeSet visited;
Scan(*input, ctx, good, dataSize, visited, hasJoin);
if (good) {
Statistics_["DqAnalyzerOk"]++;
} else {
Statistics_["DqAnalyzerFail"] ++;
}
if ((hasJoin && dataSize > State_->Settings->MaxDataSizePerQuery.Get().GetOrElse(10_GB))) {
Statistics_["DqAnalyzerBigJoin"]++;
}
if (!good || (hasJoin && dataSize > State_->Settings->MaxDataSizePerQuery.Get().GetOrElse(10_GB))) {
YQL_LOG(DEBUG) << "good: " << good << " hasJoin: " << hasJoin << " dataSize: " << dataSize;
return TStatus::Ok;
}
}
State_->TypeCtx->DqFallbackPolicy = State_->Settings->FallbackPolicy.Get().GetOrElse("default");
auto status = OptimizeExpr(input, output, [&](const TExprNode::TPtr& node, TExprContext& ctx) {
if (auto maybeRead = TMaybeNode<TCoRight>(node).Input()) {
if (maybeRead.Raw()->ChildrenSize() > 1 && TCoDataSource::Match(maybeRead.Raw()->Child(1))) {
auto dataSourceName = maybeRead.Raw()->Child(1)->Child(0)->Content();
auto dataSource = State_->TypeCtx->DataSourceMap.FindPtr(dataSourceName);
YQL_ENSURE(dataSource);
if (auto dqIntegration = (*dataSource)->GetDqIntegration()) {
auto newRead = dqIntegration->WrapRead(*State_->Settings, maybeRead.Cast().Ptr(), ctx);
if (newRead.Get() != maybeRead.Raw()) {
return newRead;
}
}
}
}
return node;
}, ctx, TOptimizeExprSettings{State_->TypeCtx});
if (input != output) {
// TODO: Add before/after recapture transformers
State_->TypeCtx->DqCaptured = true;
// TODO: drop this after implementing DQS ConstraintTransformer
State_->TypeCtx->ExpectedConstraints.clear();
}
return status;
}
void Rewind() final {
}
private:
void AddInfo(TExprContext& ctx, const TString& message) const {
YQL_LOG(DEBUG) << message;
TIssue info("DQ cannot execute the query. Cause: " + message);
info.Severity = TSeverityIds::S_INFO;
ctx.IssueManager.RaiseIssue(info);
}
void Scan(const TExprNode& node, TExprContext& ctx, bool& good, ui64& dataSize, TNodeSet& visited, bool& hasJoin) const {
if (!visited.insert(&node).second) {
return;
}
TExprBase expr(&node);
if (TMaybeNode<TCoEquiJoin>(&node)) {
hasJoin = true;
}
if (TCoCommit::Match(&node)) {
for (size_t i = 0; i != node.ChildrenSize() && good; ++i) {
if (i != TCoCommit::idx_DataSink) {
Scan(*node.Child(i), ctx, good, dataSize, visited, hasJoin);
}
}
} else if (node.IsCallable(UNSUPPORTED_CALLABLE)) {
AddInfo(ctx, TStringBuilder() << "unsupported callable '" << node.Content() << "'");
good = false;
} else if (node.IsCallable(TCoCollect::CallableName())) {
if (ETypeAnnotationKind::List != node.Head().GetTypeAnn()->GetKind()) {
AddInfo(ctx, TStringBuilder() << "unsupported callable '" << node.Content() << "' over stream/flow");
good = false;
}
} else if (auto datasource = TMaybeNode<TCoDataSource>(&node).Category()) {
if (!VALID_SOURCES.contains(datasource.Cast().Value())) {
AddInfo(ctx, TStringBuilder() << "source '" << datasource.Cast().Value() << "' is not supported by DQ");
good = false;
}
} else if (auto datasink = TMaybeNode<TCoDataSink>(&node).Category()) {
if (!VALID_SINKS.contains(datasink.Cast().Value())) {
AddInfo(ctx, TStringBuilder() << "sink '" << datasink.Cast().Value() << "' is not supported by DQ");
good = false;
}
} else if (TMaybeNode<TCoEquiJoin>(&node) && !NDq::CheckJoinColumns(expr)) {
AddInfo(ctx, TStringBuilder() << "unsupported join column");
good = false;
} else if (node.ChildrenSize() > 1 && TCoDataSource::Match(node.Child(1))) {
auto dataSourceName = node.Child(1)->Child(0)->Content();
if (dataSourceName != DqProviderName && !node.IsCallable(ConfigureName)) {
auto datasource = State_->TypeCtx->DataSourceMap.FindPtr(dataSourceName);
YQL_ENSURE(datasource);
auto dqIntegration = (*datasource)->GetDqIntegration();
if (dqIntegration) {
TMaybe<ui64> size;
bool pragmas = true;
if ((pragmas = dqIntegration->CheckPragmas(node, ctx, false)) && (size = dqIntegration->CanRead(*State_->Settings, node, ctx, /*skipIssues = */ false))) {
dataSize += *size;
} else {
good = false;
if (!pragmas) {
State_->TypeCtx->PureResultDataSource.clear();
std::erase_if(State_->TypeCtx->AvailablePureResultDataSources,
[&](const auto& name) { return name == DqProviderName; });
}
}
} else {
AddInfo(ctx, TStringBuilder() << "source '" << dataSourceName << "' is not supported by DQ");
good = false;
}
}
if (good) {
Scan(node.Head(), ctx,good, dataSize, visited, hasJoin);
}
} else if (node.GetTypeAnn()->GetKind() == ETypeAnnotationKind::World
&& !TCoCommit::Match(&node)
&& node.ChildrenSize() > 1
&& TCoDataSink::Match(node.Child(1))) {
auto dataSinkName = node.Child(1)->Child(0)->Content();
auto dataSink = State_->TypeCtx->DataSinkMap.FindPtr(dataSinkName);
YQL_ENSURE(dataSink);
if (auto dqIntegration = dataSink->Get()->GetDqIntegration()) {
if (auto canWrite = dqIntegration->CanWrite(*State_->Settings, node, ctx)) {
if (!canWrite.GetRef()) {
good = false;
} else if (!State_->Settings->EnableInsert.Get().GetOrElse(false)) {
AddInfo(ctx, TStringBuilder() << "'insert' support is disabled. Use PRAGMA dq.EnableInsert to explicitly enable it");
good = false;
}
}
}
if (good) {
for (size_t i = 0; i != node.ChildrenSize() && good; ++i) {
Scan(*node.Child(i), ctx, good, dataSize, visited, hasJoin);
}
}
}
else if (!State_->TypeCtx->UdfSupportsYield && TCoScriptUdf::Match(&node)) {
if (IsCallableTypeHasStreams(node.GetTypeAnn()->Cast<TCallableExprType>())) {
AddInfo(ctx, TStringBuilder() << "script udf with streams");
good = false;
}
if (good) {
for (size_t i = 0; i != node.ChildrenSize() && good; ++i) {
Scan(*node.Child(i), ctx, good, dataSize, visited, hasJoin);
}
}
}
else {
for (size_t i = 0; i != node.ChildrenSize() && good; ++i) {
Scan(*node.Child(i), ctx, good, dataSize, visited, hasJoin);
}
}
}
private:
TDqStatePtr State_;
THashMap<TString, int> Statistics_;
void FlushStatistics() {
TOperationStatistics statistics;
for (const auto& [k, v] : Statistics_) {
if (v == 1) {
statistics.Entries.push_back(TOperationStatistics::TEntry(k, 0, 0, 0, 0, 1));
}
}
TGuard<TMutex> lock(State_->Mutex);
if (!statistics.Entries.empty()) {
State_->Statistics[State_->MetricId++] = statistics;
}
}
};
THolder<IGraphTransformer> CreateDqsRecaptureTransformer(TDqStatePtr state) {
return THolder(new TDqsRecaptureTransformer(state));
}
} // NYql
|