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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
#pragma once
#include <util/generic/vector.h>
#include <util/generic/string.h>
#include <yql/essentials/core/yql_statistics.h>
#include <yql/essentials/core/yql_cost_function.h>
#include <unordered_map>
#include <memory>
#include <map>
#include <sstream>
namespace NYql {
/**
* OptimizerNodes are the internal representations of operators inside the
* Cost-based optimizer. Currently we only support RelOptimizerNode - a node that
* is an input relation to the equi-join, and JoinOptimizerNode - an inner join
* that connects two sets of relations.
*/
enum EOptimizerNodeKind: ui32
{
RelNodeType,
JoinNodeType
};
/**
* BaseOptimizerNode is a base class for the internal optimizer nodes
* It records a pointer to statistics and records the current cost of the
* operator tree, rooted at this node
*/
struct IBaseOptimizerNode {
EOptimizerNodeKind Kind;
TOptimizerStatistics Stats;
IBaseOptimizerNode(EOptimizerNodeKind k) : Kind(k) {}
IBaseOptimizerNode(EOptimizerNodeKind k, TOptimizerStatistics s) :
Kind(k), Stats(std::move(s)) {}
virtual TVector<TString> Labels()=0;
virtual void Print(std::stringstream& stream, int ntabs=0)=0;
};
enum EJoinKind: ui32
{
InnerJoin,
LeftJoin,
RightJoin,
OuterJoin,
LeftOnly /* == LeftAntiJoin */,
RightOnly /* == RightAntiJoin */,
LeftSemi,
RightSemi,
Cross,
Exclusion
};
EJoinKind ConvertToJoinKind(const TString& joinString);
TString ConvertToJoinString(const EJoinKind kind);
struct TCardinalityHints {
enum ECardOperation : ui32 {
Add,
Subtract,
Multiply,
Divide,
Replace
};
struct TCardinalityHint {
TVector<TString> JoinLabels;
ECardOperation Operation;
double Value;
TString StringRepr;
bool Applied = false;
double ApplyHint(double originalValue) {
Applied = true;
switch (Operation) {
case Add:
return originalValue + Value;
case Subtract:
return originalValue - Value;
case Multiply:
return originalValue * Value;
case Divide:
return originalValue / Value;
case Replace:
return Value;
}
}
};
TVector<TCardinalityHint> Hints;
void PushBack(TVector<TString> labels, ECardOperation op, double value, TString stringRepr) {
Hints.push_back({.JoinLabels = std::move(labels), .Operation = op, .Value = value, .StringRepr = std::move(stringRepr)});
}
};
struct TJoinAlgoHints {
struct TJoinAlgoHint {
TVector<TString> JoinLabels;
EJoinAlgoType Algo;
TString StringRepr;
bool Applied = false;
};
TVector<TJoinAlgoHint> Hints;
void PushBack(TVector<TString> labels, EJoinAlgoType algo, TString stringRepr) {
Hints.push_back({.JoinLabels = std::move(labels), .Algo = algo, .StringRepr = std::move(stringRepr)});
}
};
struct TJoinOrderHints {
struct ITreeNode {
enum _ : ui32 {
Relation,
Join
};
virtual TVector<TString> Labels() = 0;
bool IsRelation() { return Type == Relation; }
bool IsJoin() { return Type == Join; }
virtual ~ITreeNode() = default;
ui32 Type;
};
struct TJoinNode: public ITreeNode {
TJoinNode(std::shared_ptr<ITreeNode> lhs, std::shared_ptr<ITreeNode> rhs)
: Lhs(std::move(lhs))
, Rhs(std::move(rhs))
{
this->Type = ITreeNode::Join;
}
TVector<TString> Labels() override {
auto labels = Lhs->Labels();
auto rhsLabels = Rhs->Labels();
labels.insert(labels.end(), std::make_move_iterator(rhsLabels.begin()), std::make_move_iterator(rhsLabels.end()));
return labels;
}
std::shared_ptr<ITreeNode> Lhs;
std::shared_ptr<ITreeNode> Rhs;
};
struct TRelationNode: public ITreeNode {
TRelationNode(TString label)
: Label(std::move(label))
{
this->Type = ITreeNode::Relation;
}
TVector<TString> Labels() override { return {Label}; }
TString Label;
};
struct TJoinOrderHint {
std::shared_ptr<ITreeNode> Tree;
TString StringRepr;
bool Applied = false;
};
TVector<TJoinOrderHint> Hints;
void PushBack(std::shared_ptr<ITreeNode> hintTree, TString stringRepr) {
Hints.push_back({.Tree = std::move(hintTree), .StringRepr = std::move(stringRepr)});
}
};
struct TOptimizerHints {
std::shared_ptr<TCardinalityHints> CardinalityHints = std::make_shared<TCardinalityHints>();
std::shared_ptr<TJoinAlgoHints> JoinAlgoHints = std::make_shared<TJoinAlgoHints>();
std::shared_ptr<TJoinOrderHints> JoinOrderHints = std::make_shared<TJoinOrderHints>();
TVector<TString> GetUnappliedString();
/*
* The function accepts string with three type of expressions: array of (JoinAlgo | Card | JoinOrder):
* 1) JoinAlgo(t1 t2 ... tn Map | Grace | Lookup) to change join algo for join, where these labels take part
* 2) Card(t1 t2 ... tn (*|/|+|-) Number) to change cardinality for join, where these labels take part or labels only
* 3) JoinOrder( (t1 t2) (t3 (t4 ...)) ) - fixate this join subtree in the general join tree
*/
static TOptimizerHints Parse(const TString&);
};
/**
* This is a temporary structure for KQP provider
* We will soon be supporting multiple providers and we will need to design
* some interfaces to pass provider-specific context to the optimizer
*/
struct IProviderContext {
virtual ~IProviderContext() = default;
virtual double ComputeJoinCost(const TOptimizerStatistics& leftStats, const TOptimizerStatistics& rightStats, const double outputRows, const double outputByteSize, EJoinAlgoType joinAlgol) const = 0;
virtual TOptimizerStatistics ComputeJoinStats(
const TOptimizerStatistics& leftStats,
const TOptimizerStatistics& rightStats,
const TVector<NDq::TJoinColumn>& leftJoinKeys,
const TVector<NDq::TJoinColumn>& rightJoinKeys,
EJoinAlgoType joinAlgo,
EJoinKind joinKind,
TCardinalityHints::TCardinalityHint* maybeHint = nullptr) const = 0;
virtual bool IsJoinApplicable(const std::shared_ptr<IBaseOptimizerNode>& left,
const std::shared_ptr<IBaseOptimizerNode>& right,
const TVector<NDq::TJoinColumn>& leftJoinKeys,
const TVector<NDq::TJoinColumn>& rightJoinKeys,
EJoinAlgoType joinAlgo,
EJoinKind joinKin) = 0;
};
/**
* Default provider context with default cost and stats computation.
*/
struct TBaseProviderContext : public IProviderContext {
TBaseProviderContext() {}
double ComputeJoinCost(const TOptimizerStatistics& leftStats, const TOptimizerStatistics& rightStats, const double outputRows, const double outputByteSize, EJoinAlgoType joinAlgo) const override;
bool IsJoinApplicable(
const std::shared_ptr<IBaseOptimizerNode>& leftStats,
const std::shared_ptr<IBaseOptimizerNode>& rightStats,
const TVector<NDq::TJoinColumn>& leftJoinKeys,
const TVector<NDq::TJoinColumn>& rightJoinKeys,
EJoinAlgoType joinAlgo,
EJoinKind joinKind) override;
virtual TOptimizerStatistics ComputeJoinStats(
const TOptimizerStatistics& leftStats,
const TOptimizerStatistics& rightStats,
const TVector<NDq::TJoinColumn>& leftJoinKeys,
const TVector<NDq::TJoinColumn>& rightJoinKeys,
EJoinAlgoType joinAlgo,
EJoinKind joinKind,
TCardinalityHints::TCardinalityHint* maybeHint = nullptr) const override;
static const TBaseProviderContext& Instance();
};
/**
* RelOptimizerNode adds a label to base class
* This is the label assinged to the input by equi-Join
*/
struct TRelOptimizerNode : public IBaseOptimizerNode {
TString Label;
// Temporary solution to check if a LookupJoin is possible in KQP
//void* Expr;
TRelOptimizerNode(TString label, TOptimizerStatistics stats) :
IBaseOptimizerNode(RelNodeType, std::move(stats)), Label(label) { }
//TRelOptimizerNode(TString label, std::shared_ptr<TOptimizerStatistics> stats, const TExprNode::TPtr expr) :
// IBaseOptimizerNode(RelNodeType, stats), Label(label), Expr(expr) { }
virtual ~TRelOptimizerNode() {}
virtual TVector<TString> Labels();
virtual void Print(std::stringstream& stream, int ntabs=0);
};
/**
* JoinOptimizerNode records the left and right arguments of the join
* as well as the set of join conditions.
* It also has methods to compute the statistics and cost of a join,
* based on pre-computed costs and statistics of the children.
*/
struct TJoinOptimizerNode : public IBaseOptimizerNode {
std::shared_ptr<IBaseOptimizerNode> LeftArg;
std::shared_ptr<IBaseOptimizerNode> RightArg;
TVector<NDq::TJoinColumn> LeftJoinKeys;
TVector<NDq::TJoinColumn> RightJoinKeys;
EJoinKind JoinType;
EJoinAlgoType JoinAlgo;
/////////////////// 'ANY' flag means leaving only one row from the join side.
bool LeftAny;
bool RightAny;
///////////////////
bool IsReorderable;
TJoinOptimizerNode(const std::shared_ptr<IBaseOptimizerNode>& left,
const std::shared_ptr<IBaseOptimizerNode>& right,
TVector<NDq::TJoinColumn> leftKeys,
TVector<NDq::TJoinColumn> rightKeys,
const EJoinKind joinType,
const EJoinAlgoType joinAlgo,
bool leftAny,
bool rightAny,
bool nonReorderable = false
);
virtual ~TJoinOptimizerNode() {}
virtual TVector<TString> Labels();
virtual void Print(std::stringstream& stream, int ntabs=0);
};
struct IOptimizerNew {
using TPtr = std::shared_ptr<IOptimizerNew>;
IProviderContext& Pctx;
IOptimizerNew(IProviderContext& ctx) : Pctx(ctx) {}
virtual ~IOptimizerNew() = default;
virtual std::shared_ptr<TJoinOptimizerNode> JoinSearch(
const std::shared_ptr<TJoinOptimizerNode>& joinTree,
const TOptimizerHints& hints = {}
) = 0;
};
struct TExprContext;
class IOptimizerFactory : private TNonCopyable {
public:
using TPtr = std::shared_ptr<IOptimizerFactory>;
using TLogger = std::function<void(const TString&)>;
virtual ~IOptimizerFactory() = default;
struct TNativeSettings {
ui32 MaxDPhypDPTableSize = 100000;
};
virtual IOptimizerNew::TPtr MakeJoinCostBasedOptimizerNative(IProviderContext& pctx, TExprContext& ctx, const TNativeSettings& settings) const = 0;
struct TPGSettings {
TLogger Logger = [](const TString&){};
};
virtual IOptimizerNew::TPtr MakeJoinCostBasedOptimizerPG(IProviderContext& pctx, TExprContext& ctx, const TPGSettings& settings) const = 0;
};
} // namespace NYql
|