Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 4d1ebee

Browse files
committed
Cleaned up order-by-translator interface by moving implementation classes to cpp file.
1 parent e4fa959 commit 4d1ebee

File tree

2 files changed

+101
-88
lines changed

2 files changed

+101
-88
lines changed

src/codegen/operator/order_by_translator.cpp

Lines changed: 93 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,98 @@ namespace codegen {
2525

2626
////////////////////////////////////////////////////////////////////////////////
2727
///
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
29120
///
30121
////////////////////////////////////////////////////////////////////////////////
31122

@@ -218,7 +309,7 @@ void OrderByTranslator::Produce() const {
218309
auto *i32_type = codegen.Int32Type();
219310
auto vec_size = Vector::kDefaultVectorSize.load();
220311
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);
222313

223314
const auto &plan = GetPlanAs<planner::OrderByPlan>();
224315
ProduceResults callback(ctx, plan, position_list);
@@ -302,52 +393,5 @@ void OrderByTranslator::TearDownPipelineState(PipelineContext &pipeline_ctx) {
302393
}
303394
}
304395

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-
352396
} // namespace codegen
353397
} // namespace peloton

src/include/codegen/operator/order_by_translator.h

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Identification: src/include/codegen/operator/order_by_translator.h
88
//
9-
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -25,6 +25,9 @@ class OrderByPlan;
2525

2626
namespace codegen {
2727

28+
/**
29+
* Translator for sorting/order-by operators.
30+
*/
2831
class OrderByTranslator : public OperatorTranslator {
2932
public:
3033
OrderByTranslator(const planner::OrderByPlan &plan,
@@ -46,43 +49,9 @@ class OrderByTranslator : public OperatorTranslator {
4649
void Consume(ConsumerContext &context, RowBatch::Row &row) const override;
4750

4851
private:
49-
//===--------------------------------------------------------------------===//
50-
// The call back used when iterating over the results in the sorter instance
51-
//===--------------------------------------------------------------------===//
52-
class ProduceResults : public Sorter::VectorizedIterateCallback {
53-
public:
54-
ProduceResults(ConsumerContext &ctx, const planner::OrderByPlan &plan,
55-
Vector &position_list);
56-
// The callback function providing the current tuple in the sorter instance
57-
void ProcessEntries(CodeGen &codegen, llvm::Value *start_index,
58-
llvm::Value *end_index,
59-
Sorter::SorterAccess &access) const override;
60-
61-
private:
62-
// The translator
63-
ConsumerContext &ctx_;
64-
// The plan node
65-
const planner::OrderByPlan &plan_;
66-
// The selection vector when producing rows
67-
Vector &position_list_;
68-
};
69-
70-
//===--------------------------------------------------------------------===//
71-
// An attribute accessor from a row in the sorter
72-
//===--------------------------------------------------------------------===//
73-
class SorterAttributeAccess : public RowBatch::AttributeAccess {
74-
public:
75-
SorterAttributeAccess(Sorter::SorterAccess &sorter_access,
76-
uint32_t col_index);
77-
// Access the configured attributes in the provided row
78-
Value Access(CodeGen &codegen, RowBatch::Row &row) override;
79-
80-
private:
81-
// A random access interface to the underlying sorter
82-
Sorter::SorterAccess &sorter_access_;
83-
// The column index of the column/attribute we want to access
84-
uint32_t col_index_;
85-
};
52+
// Helper class declarations
53+
class ProduceResults;
54+
class SorterAttributeAccess;
8655

8756
private:
8857
// The child pipeline
@@ -95,7 +64,7 @@ class OrderByTranslator : public OperatorTranslator {
9564
// The sorter translator instance
9665
Sorter sorter_;
9766

98-
// The comparison function
67+
// The (generated) comparison function
9968
llvm::Function *compare_func_;
10069

10170
struct SortKeyInfo {

0 commit comments

Comments
 (0)