|
6 | 6 | //
|
7 | 7 | // Identification: src/planner/seq_scan_plan.cpp
|
8 | 8 | //
|
9 |
| -// Copyright (c) 2015-2018, Carnegie Mellon University Database Group |
| 9 | +// Copyright (c) 2015-17, Carnegie Mellon University Database Group |
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | #include "planner/seq_scan_plan.h"
|
14 | 14 |
|
| 15 | +#include "parser/select_statement.h" |
| 16 | +#include "catalog/manager.h" |
| 17 | +#include "catalog/schema.h" |
15 | 18 | #include "common/logger.h"
|
16 | 19 | #include "common/macros.h"
|
17 | 20 | #include "expression/abstract_expression.h"
|
| 21 | +#include "expression/expression_util.h" |
18 | 22 | #include "storage/data_table.h"
|
| 23 | +#include "storage/storage_manager.h" |
19 | 24 | #include "common/internal_types.h"
|
20 | 25 |
|
21 | 26 | namespace peloton {
|
22 | 27 | namespace planner {
|
23 | 28 |
|
| 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 | + |
24 | 249 | void SeqScanPlan::SetParameterValues(std::vector<type::Value> *values) {
|
25 | 250 | LOG_TRACE("Setting parameter values in Sequential Scan");
|
26 | 251 |
|
|
0 commit comments