@@ -25,7 +25,98 @@ namespace codegen {
25
25
26
26
// //////////////////////////////////////////////////////////////////////////////
27
27
// /
28
- // / OrderByTranslator
28
+ // / Sorter Attribute Access
29
+ // /
30
+ // //////////////////////////////////////////////////////////////////////////////
31
+
32
+ /* *
33
+ * This class is used as a deferred accessor for an attribute from the sorter..
34
+ */
35
+ class OrderByTranslator ::SorterAttributeAccess
36
+ : public RowBatch::AttributeAccess {
37
+ public:
38
+ SorterAttributeAccess (Sorter::SorterAccess &sorter_access,
39
+ uint32_t col_index);
40
+ // Access the configured attributes in the provided row
41
+ Value Access (CodeGen &codegen, RowBatch::Row &row) override ;
42
+
43
+ private:
44
+ // A random access interface to the underlying sorter
45
+ Sorter::SorterAccess &sorter_access_;
46
+ // The column index of the column/attribute we want to access
47
+ uint32_t col_index_;
48
+ };
49
+
50
+ // / Constructor
51
+ OrderByTranslator::SorterAttributeAccess::SorterAttributeAccess (
52
+ Sorter::SorterAccess &sorter_access, uint32_t col_index)
53
+ : sorter_access_(sorter_access), col_index_(col_index) {}
54
+
55
+ // / Access
56
+ Value OrderByTranslator::SorterAttributeAccess::Access (CodeGen &codegen,
57
+ RowBatch::Row &row) {
58
+ auto &sorted_row = sorter_access_.GetRow (row.GetTID (codegen));
59
+ return sorted_row.LoadColumn (codegen, col_index_);
60
+ }
61
+
62
+ // //////////////////////////////////////////////////////////////////////////////
63
+ // /
64
+ // / Produce Results
65
+ // /
66
+ // //////////////////////////////////////////////////////////////////////////////
67
+
68
+ /* *
69
+ * This is the callback used when we're iterating over the sorted results.
70
+ */
71
+ class OrderByTranslator ::ProduceResults
72
+ : public Sorter::VectorizedIterateCallback {
73
+ public:
74
+ ProduceResults (ConsumerContext &ctx, const planner::OrderByPlan &plan,
75
+ Vector &position_list);
76
+ // The callback function providing the current tuple in the sorter instance
77
+ void ProcessEntries (CodeGen &codegen, llvm::Value *start_index,
78
+ llvm::Value *end_index,
79
+ Sorter::SorterAccess &access) const override ;
80
+
81
+ private:
82
+ // The translator
83
+ ConsumerContext &ctx_;
84
+ // The plan node
85
+ const planner::OrderByPlan &plan_;
86
+ // The selection vector when producing rows
87
+ Vector &position_list_;
88
+ };
89
+
90
+ // / Constructor
91
+ OrderByTranslator::ProduceResults::ProduceResults (
92
+ ConsumerContext &ctx, const planner::OrderByPlan &plan,
93
+ Vector &position_list)
94
+ : ctx_(ctx), plan_(plan), position_list_(position_list) {}
95
+
96
+ // / Callback to process a batch of rows from the sorter
97
+ void OrderByTranslator::ProduceResults::ProcessEntries (
98
+ CodeGen &, llvm::Value *start_index, llvm::Value *end_index,
99
+ Sorter::SorterAccess &access) const {
100
+ // Construct the row batch we're producing
101
+ auto &comp_ctx = ctx_.GetCompilationContext ();
102
+ RowBatch batch (comp_ctx, start_index, end_index, position_list_, false );
103
+
104
+ // Add the attribute accessors for rows in this batch
105
+ std::vector<SorterAttributeAccess> accessors;
106
+ auto &output_ais = plan_.GetOutputColumnAIs ();
107
+ for (oid_t col_id = 0 ; col_id < output_ais.size (); col_id++) {
108
+ accessors.emplace_back (access, col_id);
109
+ }
110
+ for (oid_t col_id = 0 ; col_id < output_ais.size (); col_id++) {
111
+ batch.AddAttribute (output_ais[col_id], &accessors[col_id]);
112
+ }
113
+
114
+ ctx_.Consume (batch);
115
+ }
116
+
117
+ // //////////////////////////////////////////////////////////////////////////////
118
+ // /
119
+ // / Order By Translator
29
120
// /
30
121
// //////////////////////////////////////////////////////////////////////////////
31
122
@@ -218,7 +309,7 @@ void OrderByTranslator::Produce() const {
218
309
auto *i32_type = codegen.Int32Type ();
219
310
auto vec_size = Vector::kDefaultVectorSize .load ();
220
311
auto *raw_vec = codegen.AllocateBuffer (i32_type, vec_size, " obPosList" );
221
- Vector position_list{ raw_vec, vec_size, i32_type} ;
312
+ Vector position_list ( raw_vec, vec_size, i32_type) ;
222
313
223
314
const auto &plan = GetPlanAs<planner::OrderByPlan>();
224
315
ProduceResults callback (ctx, plan, position_list);
@@ -302,52 +393,5 @@ void OrderByTranslator::TearDownPipelineState(PipelineContext &pipeline_ctx) {
302
393
}
303
394
}
304
395
305
- // //////////////////////////////////////////////////////////////////////////////
306
- // /
307
- // / PRODUCE RESULTS
308
- // /
309
- // //////////////////////////////////////////////////////////////////////////////
310
-
311
- OrderByTranslator::ProduceResults::ProduceResults (
312
- ConsumerContext &ctx, const planner::OrderByPlan &plan,
313
- Vector &position_list)
314
- : ctx_(ctx), plan_(plan), position_list_(position_list) {}
315
-
316
- void OrderByTranslator::ProduceResults::ProcessEntries (
317
- CodeGen &, llvm::Value *start_index, llvm::Value *end_index,
318
- Sorter::SorterAccess &access) const {
319
- // Construct the row batch we're producing
320
- auto &comp_ctx = ctx_.GetCompilationContext ();
321
- RowBatch batch (comp_ctx, start_index, end_index, position_list_, false );
322
-
323
- // Add the attribute accessors for rows in this batch
324
- std::vector<SorterAttributeAccess> accessors;
325
- auto &output_ais = plan_.GetOutputColumnAIs ();
326
- for (oid_t col_id = 0 ; col_id < output_ais.size (); col_id++) {
327
- accessors.emplace_back (access, col_id);
328
- }
329
- for (oid_t col_id = 0 ; col_id < output_ais.size (); col_id++) {
330
- batch.AddAttribute (output_ais[col_id], &accessors[col_id]);
331
- }
332
-
333
- ctx_.Consume (batch);
334
- }
335
-
336
- // //////////////////////////////////////////////////////////////////////////////
337
- // /
338
- // / SORTER TUPLE ATTRIBUTE ACCESS
339
- // /
340
- // //////////////////////////////////////////////////////////////////////////////
341
-
342
- OrderByTranslator::SorterAttributeAccess::SorterAttributeAccess (
343
- Sorter::SorterAccess &sorter_access, uint32_t col_index)
344
- : sorter_access_(sorter_access), col_index_(col_index) {}
345
-
346
- Value OrderByTranslator::SorterAttributeAccess::Access (CodeGen &codegen,
347
- RowBatch::Row &row) {
348
- auto &sorted_row = sorter_access_.GetRow (row.GetTID (codegen));
349
- return sorted_row.LoadColumn (codegen, col_index_);
350
- }
351
-
352
396
} // namespace codegen
353
397
} // namespace peloton
0 commit comments