-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptimizerTree.hpp
333 lines (293 loc) · 11.4 KB
/
OptimizerTree.hpp
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_QUERY_OPTIMIZER_OPTIMIZER_TREE_HPP_
#define QUICKSTEP_QUERY_OPTIMIZER_OPTIMIZER_TREE_HPP_
#include <cstddef>
#include <memory>
#include <string>
#include <vector>
#include "catalog/Catalog.pb.h"
#include "storage/StorageBlockLayout.pb.h"
#include "utility/Macros.hpp"
#include "utility/TreeStringSerializable.hpp"
#include "glog/logging.h"
namespace quickstep {
namespace optimizer {
/** \addtogroup QueryOptimizer
* @{
*/
/**
* @brief Base class of OptimizerTree. This class is needed so that we may
* attach non-child type nodes to a tree in the string representation
* as long as the non-child type is a subclass of OptimizerTreeBase.
* For example, a Logical node may have a child tree node of an
* Expression type.
*/
class OptimizerTreeBase
: public TreeStringSerializable<std::shared_ptr<const OptimizerTreeBase>> {
public:
typedef std::shared_ptr<const OptimizerTreeBase> OptimizerTreeBaseNodePtr;
/**
* @brief Destructor.
*/
~OptimizerTreeBase() override {}
protected:
/**
* @brief Constructor.
*/
OptimizerTreeBase() {}
private:
DISALLOW_COPY_AND_ASSIGN(OptimizerTreeBase);
};
/**
* @brief Base class for expressions, logical/physical trees.
*/
template <class NodeType>
class OptimizerTree : public OptimizerTreeBase {
public:
typedef std::shared_ptr<const NodeType> OptimizerTreeNodePtr;
/**
* @brief Destructor.
*/
~OptimizerTree() override {}
/**
* @brief Returns the child node list.
*
* @return A vector of children.
*/
const std::vector<OptimizerTreeNodePtr>& children() const {
return children_;
}
/**
* @brief Returns the number of child nodes.
*
* @return The number of child nodes.
*/
std::size_t getNumChildren() const { return children_.size(); }
/**
* @brief Creates a copy with the child nodes replaced by \p new_children.
*
* @param new_children The children to be substituted for the existing ones.
* @return A copy with \p new_children as child nodes.
*/
virtual OptimizerTreeNodePtr copyWithNewChildren(
const std::vector<OptimizerTreeNodePtr> &new_children) const = 0;
protected:
/**
* @brief Constructor.
*/
OptimizerTree() {}
/**
* @brief Adds a new child node to this tree node->
*
* @param child The node to add as a new child.
*/
void addChild(const OptimizerTreeNodePtr &child) {
children_.push_back(child);
}
private:
std::vector<OptimizerTreeNodePtr> children_;
DISALLOW_COPY_AND_ASSIGN(OptimizerTree);
};
/**
* @brief A helper class for printing Protobuf messages in a simple flat-format.
* @details Holds key-value pairs of a proto message. Key values are added as strings.
* To use, create a representation and then add properties:
* auto foo = new OptimizerProtoRepresentation<OptimizerPtrType>();
* foo.addProperty("friendly_name", proto.GetExtension(ProtoType::friendly_name));
*/
template<class TreeNodeType>
class OptimizerProtoRepresentation : public OptimizerTreeBase {
/**
* @brief Inner class which represents key-value properties of the given proto.
*/
class OptimizerProtoPropertyRepresentation : public OptimizerTreeBase {
public:
OptimizerProtoPropertyRepresentation(std::string key, std::string value)
: key_(key), value_(value) { }
std::string getName() const {
return "ProtoProperty";
}
void getFieldStringItems(std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<TreeNodeType> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<TreeNodeType>> *container_child_fields) const {
inline_field_names->push_back(std::string("Property"));
inline_field_values->push_back(key_);
inline_field_names->push_back(std::string("Value"));
inline_field_values->push_back(value_);
}
private:
std::string key_;
std::string value_;
}; // class OptimizerProtoRepresentation
public:
OptimizerProtoRepresentation() : properties_() { }
std::string getName() const {
return "ProtoDescription";
}
/**
* @brief Add a key-value type property to this proto representation.
* @details Internally, the instance creates another node to represent the
* given values.
*
* @param key A string key.
* @param value A string value.
*/
void addProperty(std::string key, std::string value) {
std::shared_ptr<const OptimizerProtoPropertyRepresentation> property(
new OptimizerProtoPropertyRepresentation(key, value));
properties_.push_back(property);
}
/**
* @brief Add a key-value type property to this proto representation.
* @details Internally, the instance creates another node to represent the
* given values.
*
* @param key A string key.
* @param value An int value which is converted into a string.
*/
void addProperty(std::string key, int value) {
std::shared_ptr<const OptimizerProtoPropertyRepresentation> property(
new OptimizerProtoPropertyRepresentation(key, std::to_string(value)));
properties_.push_back(property);
}
void getFieldStringItems(std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<TreeNodeType> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<TreeNodeType>> *container_child_fields) const {
for (auto property : properties_) {
non_container_child_field_names->push_back("Property");
non_container_child_fields->push_back(property);
}
}
// A list of managed properties.
std::vector<std::shared_ptr<const OptimizerProtoPropertyRepresentation> > properties_;
};
/**
* @brief Returns an optimizer node representation of a LayoutDescription.
*
* @param description A valid StorageBlockLayoutDescription.
* @return A caller-managed optimizer tree node of the proto message.
*/
template<class TreeNodeType>
OptimizerProtoRepresentation<TreeNodeType>* getOptimizerRepresentationForProto(
const StorageBlockLayoutDescription *description) {
if (description == nullptr) {
return nullptr;
}
std::unique_ptr<OptimizerProtoRepresentation<TreeNodeType> >
node(new OptimizerProtoRepresentation<TreeNodeType>());
// Add properties based on the tuple storage block type.
const ::quickstep::TupleStorageSubBlockDescription &storage_block_description
= description->tuple_store_description();
switch (storage_block_description.sub_block_type()) {
case TupleStorageSubBlockDescription::SPLIT_ROW_STORE: {
node->addProperty("blocktype", "split_rowstore");
break;
}
case TupleStorageSubBlockDescription::BASIC_COLUMN_STORE: {
node->addProperty("blocktype", "columnstore");
if (storage_block_description.HasExtension(
quickstep::BasicColumnStoreTupleStorageSubBlockDescription::sort_attribute_id)) {
node->addProperty("sort",
storage_block_description.GetExtension(
quickstep::BasicColumnStoreTupleStorageSubBlockDescription::sort_attribute_id));
}
break;
}
case TupleStorageSubBlockDescription::COMPRESSED_COLUMN_STORE: {
node->addProperty("blocktype", "compressed_columnstore");
node->addProperty("sort",
storage_block_description.GetExtension(
quickstep::CompressedColumnStoreTupleStorageSubBlockDescription::sort_attribute_id));
for (int compressed_attribute = 0;
compressed_attribute < storage_block_description.ExtensionSize(
quickstep::CompressedColumnStoreTupleStorageSubBlockDescription::compressed_attribute_id);
++compressed_attribute) {
node->addProperty("compress",
storage_block_description.GetExtension(
quickstep::CompressedColumnStoreTupleStorageSubBlockDescription::compressed_attribute_id,
compressed_attribute));
}
break;
}
case TupleStorageSubBlockDescription::COMPRESSED_PACKED_ROW_STORE: {
node->addProperty("blocktype", "compressed_packed_rowstore");
for (int compressed_attribute = 0;
compressed_attribute < storage_block_description.ExtensionSize(
quickstep::CompressedPackedRowStoreTupleStorageSubBlockDescription::compressed_attribute_id);
++compressed_attribute) {
node->addProperty("compress",
storage_block_description.GetExtension(
quickstep::CompressedPackedRowStoreTupleStorageSubBlockDescription::compressed_attribute_id,
compressed_attribute));
}
break;
}
default: {
LOG(WARNING) << "Unrecognized block type in protobuf message.";
break;
}
}
// Every case will specify a slots number.
node->addProperty("slots", description->num_slots());
return node.release();
}
template<class TreeNodeType>
OptimizerProtoRepresentation<TreeNodeType>* getOptimizerRepresentationForProto(
const serialization::PartitionSchemeHeader *partition_header) {
if (partition_header == nullptr) {
return nullptr;
}
auto node = std::make_unique<OptimizerProtoRepresentation<TreeNodeType>>();
// Add properties based on the partition type.
switch (partition_header->partition_type()) {
case serialization::PartitionSchemeHeader::HASH: {
node->addProperty("partition_type", "hash");
break;
}
case serialization::PartitionSchemeHeader::RANDOM: {
node->addProperty("partition_type", "random");
break;
}
case serialization::PartitionSchemeHeader::RANGE: {
node->addProperty("partition_type", "range");
// TODO(quickstep-team): display the range boundaries.
node->addProperty("range_boundaries", "TODO");
break;
}
default:
LOG(FATAL) << "Unrecognized partition type in protobuf message.";
}
// Every case will specify a partition number and a partition attributes.
node->addProperty("num_partitions", partition_header->num_partitions());
for (int i = 0; i < partition_header->partition_attribute_ids_size(); ++i) {
node->addProperty("partition_attr_id", partition_header->partition_attribute_ids(i));
}
return node.release();
}
/** @} */
} // namespace optimizer
} // namespace quickstep
#endif /* QUICKSTEP_QUERY_OPTIMIZER_OPTIMIZER_TREE_HPP_ */