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

Commit ec280f9

Browse files
committed
Revert "Removed unused serialization stuff from plan nodes"
This reverts commit 74427c7.
1 parent e7fc857 commit ec280f9

File tree

5 files changed

+267
-11
lines changed

5 files changed

+267
-11
lines changed

src/include/planner/abstract_plan.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "codegen/query_parameters_map.h"
2121
#include "common/printable.h"
2222
#include "planner/binding_context.h"
23+
#include "type/serializeio.h"
24+
#include "type/serializer.h"
2325
#include "common/internal_types.h"
2426
#include "type/value.h"
2527
#include "util/hash_util.h"
@@ -64,6 +66,8 @@ class AbstractPlan : public Printable {
6466

6567
const AbstractPlan *GetChild(uint32_t child_index) const;
6668

69+
const AbstractPlan *GetParent() const;
70+
6771
//===--------------------------------------------------------------------===//
6872
// Accessors
6973
//===--------------------------------------------------------------------===//
@@ -107,6 +111,23 @@ class AbstractPlan : public Printable {
107111

108112
virtual std::unique_ptr<AbstractPlan> Copy() const = 0;
109113

114+
// A plan will be sent to anther node via serialization
115+
// So serialization should be implemented by the derived classes
116+
117+
//===--------------------------------------------------------------------===//
118+
// Serialization/Deserialization
119+
// Each sub-class will have to implement these functions
120+
// After the implementation for each sub-class, we should set these to pure
121+
// virtual
122+
//===--------------------------------------------------------------------===//
123+
virtual bool SerializeTo(SerializeOutput &output UNUSED_ATTRIBUTE) const {
124+
return false;
125+
}
126+
virtual bool DeserializeFrom(SerializeInput &input UNUSED_ATTRIBUTE) {
127+
return false;
128+
}
129+
virtual int SerializeSize() const { return 0; }
130+
110131
virtual hash_t Hash() const;
111132

112133
virtual bool operator==(const AbstractPlan &rhs) const;
@@ -122,10 +143,16 @@ class AbstractPlan : public Printable {
122143
}
123144
}
124145

146+
protected:
147+
// only used by its derived classes (when deserialization)
148+
AbstractPlan *Parent() const { return parent_; }
149+
125150
private:
126151
// A plan node can have multiple children
127152
std::vector<std::unique_ptr<AbstractPlan>> children_;
128153

154+
AbstractPlan *parent_ = nullptr;
155+
129156
// TODO: This field is harded coded now. This needs to be changed when
130157
// optimizer has the cost model and cardinality estimation
131158
int estimated_cardinality_ = 500000;

src/include/planner/abstract_scan_plan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class AbstractScan : public AbstractPlan {
7171
protected:
7272
void SetTargetTable(storage::DataTable *table) { target_table_ = table; }
7373

74+
void AddColumnId(oid_t col_id) { column_ids_.push_back(col_id); }
75+
7476
void SetPredicate(expression::AbstractExpression *predicate) {
7577
predicate_ = std::unique_ptr<expression::AbstractExpression>(predicate);
7678
}

src/include/planner/seq_scan_plan.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,10 @@
1818

1919
#include "common/internal_types.h"
2020
#include "common/logger.h"
21-
#include "expression/abstract_expression.h"
2221
#include "planner/abstract_scan_plan.h"
2322
#include "type/serializer.h"
2423

2524
namespace peloton {
26-
27-
namespace expression {
28-
class Parameter;
29-
} // namespace expression
30-
31-
namespace storage {
32-
class DataTable;
33-
} // namespace storage
34-
3525
namespace planner {
3626

3727
class SeqScanPlan : public AbstractScan {
@@ -58,6 +48,15 @@ class SeqScanPlan : public AbstractScan {
5848

5949
void SetParameterValues(std::vector<type::Value> *values) override;
6050

51+
//===--------------------------------------------------------------------===//
52+
// Serialization/Deserialization
53+
//===--------------------------------------------------------------------===//
54+
bool SerializeTo(SerializeOutput &output) const override;
55+
bool DeserializeFrom(SerializeInput &input) override;
56+
57+
/* For init SerializeOutput */
58+
int SerializeSize() const override;
59+
6160
std::unique_ptr<AbstractPlan> Copy() const override {
6261
auto *new_plan =
6362
new SeqScanPlan(GetTable(), GetPredicate()->Copy(), GetColumnIds());

src/planner/abstract_plan.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "common/logger.h"
1616
#include "common/macros.h"
17+
#include "expression/expression_util.h"
1718
#include "util/hash_util.h"
1819

1920
namespace peloton {
@@ -37,6 +38,8 @@ const AbstractPlan *AbstractPlan::GetChild(uint32_t child_index) const {
3738
return children_[child_index].get();
3839
}
3940

41+
const AbstractPlan *AbstractPlan::GetParent() const { return parent_; }
42+
4043
// Get a string representation of this plan
4144
std::ostream &operator<<(std::ostream &os, const AbstractPlan &plan) {
4245
os << PlanNodeTypeToString(plan.GetPlanNodeType());

src/planner/seq_scan_plan.cpp

Lines changed: 226 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,246 @@
66
//
77
// Identification: src/planner/seq_scan_plan.cpp
88
//
9-
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "planner/seq_scan_plan.h"
1414

15+
#include "parser/select_statement.h"
16+
#include "catalog/manager.h"
17+
#include "catalog/schema.h"
1518
#include "common/logger.h"
1619
#include "common/macros.h"
1720
#include "expression/abstract_expression.h"
21+
#include "expression/expression_util.h"
1822
#include "storage/data_table.h"
23+
#include "storage/storage_manager.h"
1924
#include "common/internal_types.h"
2025

2126
namespace peloton {
2227
namespace planner {
2328

29+
//===--------------------------------------------------------------------===//
30+
// Serialization/Deserialization
31+
//===--------------------------------------------------------------------===//
32+
33+
/**
34+
* The SeqScanPlan has the following members:
35+
* database_id, table_id, predicate, column_id, parent(might be NULL)
36+
* TODO: SeqScanPlan doesn't have children, so we don't need to handle it
37+
*
38+
* Therefore a SeqScanPlan is serialized as:
39+
* [(int) total size]
40+
* [(int8_t) plan type]
41+
* [(int) database_id]
42+
* [(int) table_id]
43+
* [(int) num column_id]
44+
* [(int) column id...]
45+
* [(int8_t) expr type] : if invalid, predicate is null
46+
* [(bytes) predicate] : predicate is Expression
47+
* [(int8_t) plan type] : if invalid, parent is null
48+
* [(bytes) parent] : parent is also a plan
49+
*
50+
* TODO: parent_ seems never be set or used
51+
*/
52+
53+
bool SeqScanPlan::SerializeTo(SerializeOutput &output) const {
54+
// A placeholder for the total size written at the end
55+
int start = output.Position();
56+
output.WriteInt(-1);
57+
58+
// Write the SeqScanPlan type
59+
PlanNodeType plan_type = GetPlanNodeType();
60+
output.WriteByte(static_cast<int8_t>(plan_type));
61+
62+
// Write database id and table id
63+
if (!GetTable()) {
64+
// The plan is not completed
65+
return false;
66+
}
67+
oid_t database_id = GetTable()->GetDatabaseOid();
68+
oid_t table_id = GetTable()->GetOid();
69+
70+
output.WriteInt(static_cast<int>(database_id));
71+
output.WriteInt(static_cast<int>(table_id));
72+
73+
// If column has 0 item, just write the columnid_count with 0
74+
int columnid_count = GetColumnIds().size();
75+
output.WriteInt(columnid_count);
76+
77+
// If column has 0 item, nothing happens here
78+
for (int it = 0; it < columnid_count; it++) {
79+
oid_t col_id = GetColumnIds()[it];
80+
output.WriteInt(static_cast<int>(col_id));
81+
}
82+
83+
// Write predicate
84+
if (GetPredicate() == nullptr) {
85+
// Write the type
86+
output.WriteByte(static_cast<int8_t>(ExpressionType::INVALID));
87+
} else {
88+
// Write the expression type
89+
ExpressionType expr_type = GetPredicate()->GetExpressionType();
90+
output.WriteByte(static_cast<int8_t>(expr_type));
91+
}
92+
93+
// Write parent, but parent seems never be set or used right now
94+
if (GetParent() == nullptr) {
95+
// Write the type
96+
output.WriteByte(static_cast<int8_t>(PlanNodeType::INVALID));
97+
} else {
98+
// Write the parent type
99+
PlanNodeType parent_type = GetParent()->GetPlanNodeType();
100+
output.WriteByte(static_cast<int8_t>(parent_type));
101+
102+
// Write parent
103+
GetParent()->SerializeTo(output);
104+
}
105+
106+
// Write the total length
107+
int32_t sz = static_cast<int32_t>(output.Position() - start - sizeof(int));
108+
PELOTON_ASSERT(sz > 0);
109+
output.WriteIntAt(start, sz);
110+
111+
return true;
112+
}
113+
114+
/**
115+
* Therefore a SeqScanPlan is serialized as:
116+
* [(int) total size]
117+
* [(int8_t) plan type]
118+
* [(int) database_id]
119+
* [(int) table_id]
120+
* [(int) num column_id]
121+
* [(int) column id...]
122+
* [(int8_t) expr type] : if invalid, predicate is null
123+
* [(bytes) predicate] : predicate is Expression
124+
* [(int8_t) plan type] : if invalid, parent is null
125+
* [(bytes) parent] : parent is also a plan
126+
*/
127+
bool SeqScanPlan::DeserializeFrom(SerializeInput &input) {
128+
// Read the size of SeqScanPlan class
129+
input.ReadInt();
130+
131+
// Read the type
132+
UNUSED_ATTRIBUTE PlanNodeType plan_type =
133+
(PlanNodeType)input.ReadEnumInSingleByte();
134+
PELOTON_ASSERT(plan_type == GetPlanNodeType());
135+
136+
// Read database id
137+
oid_t database_oid = input.ReadInt();
138+
139+
// Read table id
140+
oid_t table_oid = input.ReadInt();
141+
142+
// Get table and set it to the member
143+
storage::DataTable *target_table = nullptr;
144+
try{
145+
target_table = static_cast<storage::DataTable *>(
146+
storage::StorageManager::GetInstance()->GetTableWithOid(
147+
database_oid, table_oid));
148+
} catch (CatalogException &e) {
149+
LOG_TRACE("Can't find table %d! Return false", table_oid);
150+
return false;
151+
}
152+
SetTargetTable(target_table);
153+
154+
// Read the number of column_id and set them to column_ids_
155+
oid_t columnid_count = input.ReadInt();
156+
for (oid_t it = 0; it < columnid_count; it++) {
157+
oid_t column_id = input.ReadInt();
158+
AddColumnId(column_id);
159+
}
160+
161+
// Read the type
162+
ExpressionType expr_type = (ExpressionType)input.ReadEnumInSingleByte();
163+
164+
// Predicate deserialization
165+
if (expr_type != ExpressionType::INVALID) {
166+
switch (expr_type) {
167+
// case ExpressionType::COMPARE_IN:
168+
// predicate_ =
169+
// std::unique_ptr<ExpressionType::COMPARE_IN>(new
170+
// ComparisonExpression (101));
171+
// predicate_.DeserializeFrom(input);
172+
// break;
173+
174+
default: {
175+
LOG_ERROR(
176+
"Expression deserialization :: Unsupported EXPRESSION_TYPE: %s",
177+
ExpressionTypeToString(expr_type).c_str());
178+
break;
179+
}
180+
}
181+
}
182+
183+
// Read the type of parent
184+
PlanNodeType parent_type = (PlanNodeType)input.ReadEnumInSingleByte();
185+
186+
// Parent deserialization
187+
if (parent_type != PlanNodeType::INVALID) {
188+
switch (expr_type) {
189+
// case ExpressionType::COMPARE_IN:
190+
// predicate_ =
191+
// std::unique_ptr<ExpressionType::COMPARE_IN>(new
192+
// ComparisonExpression (101));
193+
// predicate_.DeserializeFrom(input);
194+
// break;
195+
196+
default: {
197+
LOG_ERROR("Parent deserialization :: Unsupported PlanNodeType: %s",
198+
ExpressionTypeToString(expr_type).c_str());
199+
break;
200+
}
201+
}
202+
}
203+
204+
return true;
205+
}
206+
/**
207+
*
208+
* SeqScanPlan is serialized as:
209+
* [(int) total size]
210+
* [(int8_t) plan type]
211+
* [(int) database_id]
212+
* [(int) table_id]
213+
* [(int) num column_id]
214+
* [(int) column id...]
215+
* [(int8_t) expr type] : if invalid, predicate is null
216+
* [(bytes) predicate] : predicate is Expression
217+
* [(int8_t) plan type] : if invalid, parent is null
218+
* [(bytes) parent] : parent is also a plan
219+
*
220+
* So, the fixed size part is:
221+
* [(int) total size] 4 +
222+
* [(int8_t) plan type] 1 +
223+
* [(int) database_id] 4 +
224+
* [(int) table_id] 4 +
225+
* [(int) num column_id]4 +
226+
* [(int8_t) expr type] 1 +
227+
* [(int8_t) plan type] 1 =
228+
* the variant part is :
229+
* [(int) column id...]: num column_id * 4
230+
* [(bytes) predicate] : predicate->GetSerializeSize()
231+
* [(bytes) parent] : parent->GetSerializeSize()
232+
*/
233+
int SeqScanPlan::SerializeSize() const {
234+
// Fixed size. see the detail above
235+
int size_fix = sizeof(int) * 4 + 3;
236+
int size_column_ids = GetColumnIds().size() * sizeof(int);
237+
int size = size_fix + size_column_ids;
238+
239+
if (GetPredicate() != nullptr) {
240+
size = size + GetPredicate()->SerializeSize();
241+
}
242+
if (Parent()) {
243+
size = size + Parent()->SerializeSize();
244+
}
245+
246+
return size;
247+
}
248+
24249
void SeqScanPlan::SetParameterValues(std::vector<type::Value> *values) {
25250
LOG_TRACE("Setting parameter values in Sequential Scan");
26251

0 commit comments

Comments
 (0)