-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.hpp
More file actions
419 lines (354 loc) · 12.8 KB
/
operator.hpp
File metadata and controls
419 lines (354 loc) · 12.8 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* @file operator.hpp
* @brief Operator classes for Volcano-style execution model
*/
#ifndef CLOUDSQL_EXECUTOR_OPERATOR_HPP
#define CLOUDSQL_EXECUTOR_OPERATOR_HPP
#include <memory>
#include <memory_resource>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "executor/types.hpp"
#include "parser/expression.hpp"
#include "storage/btree_index.hpp"
#include "storage/heap_table.hpp"
#include "transaction/lock_manager.hpp"
#include "transaction/transaction.hpp"
namespace cloudsql::executor {
using namespace cloudsql::transaction;
/**
* @brief Operator types
*/
enum class OperatorType : uint8_t {
SeqScan,
IndexScan,
Filter,
Project,
NestedLoopJoin,
HashJoin,
Sort,
Aggregate,
HashAggregate,
Limit,
Materialize,
Result,
BufferScan
};
/**
* @brief Base operator class (Volcano iterator model)
*/
class Operator {
private:
OperatorType type_;
ExecState state_ = ExecState::Init;
std::string error_message_;
Transaction* txn_;
LockManager* lock_manager_;
std::pmr::memory_resource* mr_ = nullptr;
const std::vector<common::Value>* params_ = nullptr;
public:
explicit Operator(OperatorType type, Transaction* txn = nullptr,
LockManager* lock_manager = nullptr)
: type_(type), txn_(txn), lock_manager_(lock_manager) {}
virtual ~Operator() = default;
// Disable copy/move for base operator
Operator(const Operator&) = delete;
Operator& operator=(const Operator&) = delete;
Operator(Operator&&) = delete;
Operator& operator=(Operator&&) = delete;
[[nodiscard]] OperatorType type() const { return type_; }
[[nodiscard]] ExecState state() const { return state_; }
[[nodiscard]] const std::string& error() const { return error_message_; }
[[nodiscard]] Transaction* get_txn() const { return txn_; }
[[nodiscard]] LockManager* get_lock_manager() const { return lock_manager_; }
virtual void set_memory_resource(std::pmr::memory_resource* mr) { mr_ = mr; }
[[nodiscard]] std::pmr::memory_resource* get_memory_resource() const {
return mr_ ? mr_ : std::pmr::get_default_resource();
}
virtual void set_params(const std::vector<common::Value>* params) { params_ = params; }
[[nodiscard]] const std::vector<common::Value>* get_params() const { return params_; }
virtual bool init() { return true; }
virtual bool open() { return true; }
virtual bool next(Tuple& out_tuple) {
(void)out_tuple;
state_ = ExecState::Done;
return false;
}
// Forward declare TupleView inside Operator pointer context
virtual bool next_view(storage::HeapTable::TupleView& out_view) {
(void)out_view;
state_ = ExecState::Done;
return false;
}
virtual void close() {}
[[nodiscard]] virtual Schema& output_schema() = 0;
virtual void add_child(std::unique_ptr<Operator> child) { (void)child; }
[[nodiscard]] virtual const std::vector<std::unique_ptr<Operator>>& children() const {
static const std::vector<std::unique_ptr<Operator>> empty;
return empty;
}
[[nodiscard]] bool is_done() const { return state_ == ExecState::Done; }
[[nodiscard]] bool has_error() const { return state_ == ExecState::Error; }
protected:
void set_state(ExecState s) { state_ = s; }
void set_error(std::string msg) {
error_message_ = std::move(msg);
state_ = ExecState::Error;
}
};
/**
* @brief Sequential scan operator
*/
class SeqScanOperator : public Operator {
private:
std::string table_name_;
std::shared_ptr<storage::HeapTable> table_;
std::unique_ptr<storage::HeapTable::Iterator> iterator_;
Schema schema_;
bool no_txn_ = false;
public:
explicit SeqScanOperator(std::shared_ptr<storage::HeapTable> table, Transaction* txn = nullptr,
LockManager* lock_manager = nullptr);
bool init() override;
bool open() override;
bool next(Tuple& out_tuple) override;
virtual bool next_view(storage::HeapTable::TupleView& out_view) override;
void close() override;
[[nodiscard]] Schema& output_schema() override;
[[nodiscard]] const std::string& table_name() const { return table_name_; }
};
/**
* @brief Buffer scan operator (for shuffled/broadcasted data)
*/
class BufferScanOperator : public Operator {
private:
std::string context_id_;
std::string table_name_;
std::vector<Tuple> data_;
size_t current_index_ = 0;
Schema schema_;
public:
BufferScanOperator(std::string context_id, std::string table_name, std::vector<Tuple> data,
Schema schema);
bool init() override { return true; }
bool open() override {
current_index_ = 0;
return true;
}
bool next(Tuple& out_tuple) override;
void close() override {}
[[nodiscard]] Schema& output_schema() override;
};
/**
* @brief Index scan operator (point lookup)
*/
class IndexScanOperator : public Operator {
private:
std::string table_name_;
std::string index_name_;
std::shared_ptr<storage::HeapTable> table_;
std::unique_ptr<storage::BTreeIndex> index_;
common::Value search_key_;
std::vector<storage::HeapTable::TupleId> matching_ids_;
size_t current_match_index_ = 0;
Schema schema_;
public:
IndexScanOperator(std::shared_ptr<storage::HeapTable> table,
std::unique_ptr<storage::BTreeIndex> index, common::Value search_key,
Transaction* txn = nullptr, LockManager* lock_manager = nullptr);
bool init() override;
bool open() override;
bool next(Tuple& out_tuple) override;
void close() override;
[[nodiscard]] Schema& output_schema() override;
};
/**
* @brief Filter operator (WHERE clause)
*/
class FilterOperator : public Operator {
private:
std::unique_ptr<Operator> child_;
std::unique_ptr<parser::Expression> condition_;
Schema schema_;
public:
FilterOperator(std::unique_ptr<Operator> child, std::unique_ptr<parser::Expression> condition);
bool init() override;
bool open() override;
bool next(Tuple& out_tuple) override;
virtual bool next_view(storage::HeapTable::TupleView& out_view) override;
void close() override;
[[nodiscard]] Schema& output_schema() override;
void add_child(std::unique_ptr<Operator> child) override;
void set_memory_resource(std::pmr::memory_resource* mr) override;
void set_params(const std::vector<common::Value>* params) override;
};
/**
* @brief Project operator (SELECT columns)
*/
class ProjectOperator : public Operator {
private:
std::unique_ptr<Operator> child_;
std::vector<std::unique_ptr<parser::Expression>> columns_;
Schema schema_;
std::vector<size_t> column_mapping_;
bool is_simple_projection_ = false;
public:
ProjectOperator(std::unique_ptr<Operator> child,
std::vector<std::unique_ptr<parser::Expression>> columns);
bool init() override;
bool open() override;
bool next(Tuple& out_tuple) override;
virtual bool next_view(storage::HeapTable::TupleView& out_view) override;
void close() override;
[[nodiscard]] Schema& output_schema() override;
void add_child(std::unique_ptr<Operator> child) override;
void set_memory_resource(std::pmr::memory_resource* mr) override;
void set_params(const std::vector<common::Value>* params) override;
};
/**
* @brief Sort operator (ORDER BY)
*/
class SortOperator : public Operator {
private:
std::unique_ptr<Operator> child_;
std::vector<std::unique_ptr<parser::Expression>> sort_keys_;
std::vector<bool> ascending_;
std::vector<Tuple> sorted_tuples_;
size_t current_index_ = 0;
Schema schema_;
public:
SortOperator(std::unique_ptr<Operator> child,
std::vector<std::unique_ptr<parser::Expression>> sort_keys,
std::vector<bool> ascending);
bool init() override;
bool open() override;
bool next(Tuple& out_tuple) override;
void close() override;
[[nodiscard]] Schema& output_schema() override;
void set_memory_resource(std::pmr::memory_resource* mr) override;
void set_params(const std::vector<common::Value>* params) override;
};
/**
* @brief Aggregate specification
*/
struct AggregateInfo {
AggregateType type = AggregateType::Count;
std::unique_ptr<parser::Expression> expr;
std::string name;
bool is_distinct = false;
};
/**
* @brief Aggregate operator (GROUP BY)
*/
class AggregateOperator : public Operator {
private:
std::unique_ptr<Operator> child_;
std::vector<std::unique_ptr<parser::Expression>> group_by_;
std::vector<AggregateInfo> aggregates_;
std::vector<Tuple> groups_;
size_t current_group_ = 0;
Schema schema_;
public:
AggregateOperator(std::unique_ptr<Operator> child,
std::vector<std::unique_ptr<parser::Expression>> group_by,
std::vector<AggregateInfo> aggregates);
bool init() override;
bool open() override;
bool next(Tuple& out_tuple) override;
void close() override;
[[nodiscard]] Schema& output_schema() override;
void set_memory_resource(std::pmr::memory_resource* mr) override;
void set_params(const std::vector<common::Value>* params) override;
};
/**
* @brief Hash join operator
*/
class HashJoinOperator : public Operator {
public:
using JoinType = cloudsql::executor::JoinType;
private:
struct BuildTuple {
Tuple tuple;
bool matched = false;
};
std::unique_ptr<Operator> left_;
std::unique_ptr<Operator> right_;
std::unique_ptr<parser::Expression> left_key_;
std::unique_ptr<parser::Expression> right_key_;
JoinType join_type_;
Schema schema_;
/* In-memory hash table for the right side */
std::unordered_multimap<std::string, BuildTuple> hash_table_;
/* Probe phase state */
std::optional<Tuple> left_tuple_;
bool left_had_match_ = false;
struct MatchIterator {
std::unordered_multimap<std::string, BuildTuple>::iterator current;
std::unordered_multimap<std::string, BuildTuple>::iterator end;
};
std::optional<MatchIterator> match_iter_;
/* Final phase for RIGHT/FULL joins */
std::optional<std::unordered_multimap<std::string, BuildTuple>::iterator> right_idx_iter_;
/* Storage for unmatched LEFT tuples (for FULL JOIN distributed collection) */
std::vector<Tuple> unmatched_left_rows_;
std::vector<std::string> unmatched_left_keys_;
public:
HashJoinOperator(std::unique_ptr<Operator> left, std::unique_ptr<Operator> right,
std::unique_ptr<parser::Expression> left_key,
std::unique_ptr<parser::Expression> right_key,
JoinType join_type = JoinType::Inner);
bool init() override;
bool open() override;
bool next(Tuple& out_tuple) override;
void close() override;
[[nodiscard]] Schema& output_schema() override;
void add_child(std::unique_ptr<Operator> child) override;
void set_memory_resource(std::pmr::memory_resource* mr) override;
void set_params(const std::vector<common::Value>* params) override;
/**
* @brief Get unmatched right rows after join execution
* @return Vector of tuples - the right-side rows that had no match
*/
[[nodiscard]] std::vector<Tuple> get_unmatched_right_rows() const;
/**
* @brief Get join key values of unmatched right rows
* @return Vector of strings - the join key values for unmatched right rows
*/
[[nodiscard]] std::vector<std::string> get_unmatched_right_keys() const;
/**
* @brief Get unmatched left rows after join execution
* @return Vector of tuples - the left-side rows that had no match
*/
[[nodiscard]] std::vector<Tuple> get_unmatched_left_rows() const;
/**
* @brief Get join key values of unmatched left rows
* @return Vector of strings - the join key values for unmatched left rows
*/
[[nodiscard]] std::vector<std::string> get_unmatched_left_keys() const;
};
/**
* @brief Limit operator
*/
class LimitOperator : public Operator {
private:
std::unique_ptr<Operator> child_;
int64_t limit_;
int64_t offset_;
uint64_t current_offset_ = 0;
uint64_t count_ = 0;
public:
LimitOperator(std::unique_ptr<Operator> child, int64_t limit, int64_t offset = 0);
bool init() override;
bool open() override;
bool next(Tuple& out_tuple) override;
virtual bool next_view(storage::HeapTable::TupleView& out_view) override;
void close() override;
[[nodiscard]] Schema& output_schema() override;
void add_child(std::unique_ptr<Operator> child) override;
void set_memory_resource(std::pmr::memory_resource* mr) override;
void set_params(const std::vector<common::Value>* params) override;
};
} // namespace cloudsql::executor
#endif // CLOUDSQL_EXECUTOR_OPERATOR_HPP