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

Commit ea30570

Browse files
committed
Review changes. Make various items private
1 parent 0341299 commit ea30570

File tree

2 files changed

+133
-143
lines changed

2 files changed

+133
-143
lines changed

src/include/planner/insert_plan.h

Lines changed: 132 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#pragma once
1414

15+
#include "expression/constant_value_expression.h"
16+
#include "storage/data_table.h"
1517
#include "planner/abstract_plan.h"
1618
#include "planner/abstract_scan_plan.h"
1719
#include "planner/project_info.h"
@@ -29,27 +31,9 @@ class InsertStatement;
2931
}
3032

3133
namespace planner {
32-
33-
// mapping from schema columns to insert columns
34-
struct SchemaColsToInsertCols {
35-
// this schema column is present in the insert columns
36-
bool in_insert_cols;
37-
38-
// For a PS, insert saved value (from constant in insert values list), no
39-
// param value.
40-
bool set_value;
41-
42-
// index of this column in insert columns values
43-
int val_idx;
44-
45-
// schema column type
46-
type::TypeId type;
47-
48-
// set_value refers to this saved value
49-
type::Value value;
50-
};
5134

5235
class InsertPlan : public AbstractPlan {
36+
5337
public:
5438
// Construct when SELECT comes in with it
5539
InsertPlan(storage::DataTable *table, oid_t bulk_insert_count = 1)
@@ -91,38 +75,7 @@ class InsertPlan : public AbstractPlan {
9175
// Get a varlen pool - will construct the pool only if needed
9276
type::AbstractPool *GetPlanPool();
9377

94-
PlanNodeType GetPlanNodeType() const override {
95-
return PlanNodeType::INSERT; };
96-
97-
/**
98-
* Lookup a column name in the schema columns
99-
*
100-
* @param[in] col_name column name, from insert statement
101-
* @param[in] tbl_columns table columns from the schema
102-
* @param[out] index index into schema columns, only if found
103-
*
104-
* @return true if column was found, false otherwise
105-
*/
106-
bool FindSchemaColIndex(std::string col_name,
107-
const std::vector<catalog::Column> &tbl_columns,
108-
uint32_t &index);
109-
110-
/**
111-
* Process column specification supplied in the insert statement.
112-
* Construct a map from insert columns to schema columns. Once
113-
* we know which columns will receive constant inserts, further
114-
* adjustment of the map will be needed.
115-
*
116-
* @param[in] columns Column specification
117-
*/
118-
void ProcessColumnSpec(const std::vector<std::string> *columns);
119-
120-
/**
121-
* Set default value into a schema column
122-
*
123-
* @param[in] idx schema column index
124-
*/
125-
void SetDefaultValue(uint32_t idx);
78+
PlanNodeType GetPlanNodeType() const override { return PlanNodeType::INSERT; };
12679

12780
/**
12881
* @brief Save values for jdbc prepared statement insert.
@@ -132,18 +85,6 @@ class InsertPlan : public AbstractPlan {
13285
*/
13386
void SetParameterValues(std::vector<type::Value> *values) override;
13487

135-
/**
136-
* Process a single expression to be inserted.
137-
*
138-
* @param[in] expr insert expression
139-
* @param[in] schema_idx index into schema columns, where the expr
140-
* will be inserted.
141-
* @return true if values imply a prepared statement
142-
* false if all values are constants. This does not rule
143-
* out the insert being a prepared statement.
144-
*/
145-
bool ProcessValueExpr(expression::AbstractExpression *expr,
146-
uint32_t schema_idx);
14788
/*
14889
* Clear the parameter values of the current insert. The plan may be
14990
* cached in the statement / plan cache and may be reused.
@@ -195,14 +136,33 @@ class InsertPlan : public AbstractPlan {
195136
const std::vector<peloton::type::Value> &values_from_user) override;
196137

197138
private:
139+
// mapping from schema columns to insert columns
140+
struct SchemaColsToInsertCols {
141+
// this schema column is present in the insert columns
142+
bool in_insert_cols;
143+
144+
// For a PS, insert saved value (from constant in insert values list), no
145+
// param value.
146+
bool set_value;
147+
148+
// index of this column in insert columns values
149+
int val_idx;
150+
151+
// schema column type
152+
type::TypeId type;
153+
154+
// set_value refers to this saved value
155+
type::Value value;
156+
};
157+
198158
// Target table
199159
storage::DataTable *target_table_ = nullptr;
200160

201161
// Values
202162
std::vector<type::Value> values_;
203163

204164
// mapping from schema columns to vector of insert columns
205-
std::vector<struct SchemaColsToInsertCols> schema_to_insert_;
165+
std::vector<SchemaColsToInsertCols> schema_to_insert_;
206166
// mapping from insert columns to schema columns
207167
std::vector<uint32_t> insert_to_schema_;
208168

@@ -230,7 +190,114 @@ class InsertPlan : public AbstractPlan {
230190

231191
private:
232192
DISALLOW_COPY_AND_MOVE(InsertPlan);
233-
};
193+
194+
/**
195+
* Lookup a column name in the schema columns
196+
*
197+
* @param[in] col_name column name, from insert statement
198+
* @param[in] tbl_columns table columns from the schema
199+
* @param[out] index index into schema columns, only if found
200+
*
201+
* @return true if column was found, false otherwise
202+
*/
203+
bool FindSchemaColIndex(std::string col_name,
204+
const std::vector<catalog::Column> &tbl_columns,
205+
uint32_t &index) {
206+
for (auto tcol = tbl_columns.begin(); tcol != tbl_columns.end(); tcol++) {
207+
if (tcol->GetName() == col_name) {
208+
index = std::distance(tbl_columns.begin(), tcol);
209+
return true;
210+
}
211+
}
212+
return false;
213+
}
214+
215+
/**
216+
* Process column specification supplied in the insert statement.
217+
* Construct a map from insert columns to schema columns. Once
218+
* we know which columns will receive constant inserts, further
219+
* adjustment of the map will be needed.
220+
*
221+
* @param[in] columns Column specification
222+
*/
223+
void ProcessColumnSpec(const std::vector<std::string> *columns) {
224+
auto *schema = target_table_->GetSchema();
225+
auto &table_columns = schema->GetColumns();
226+
auto usr_col_count = columns->size();
227+
228+
// iterate over supplied columns
229+
for (size_t usr_col_id = 0; usr_col_id < usr_col_count; usr_col_id++) {
230+
uint32_t idx;
231+
auto col_name = columns->at(usr_col_id);
232+
233+
// determine index of column in schema
234+
bool found_col = FindSchemaColIndex(col_name, table_columns, idx);
235+
if (not found_col) {
236+
throw Exception("column " + col_name + " not in table " +
237+
target_table_->GetName() + " columns");
238+
}
239+
// we have values for this column
240+
schema_to_insert_[idx].in_insert_cols = true;
241+
// remember how to map schema col -> value for col in tuple
242+
schema_to_insert_[idx].val_idx = usr_col_id;
243+
// and the reverse
244+
insert_to_schema_[usr_col_id] = idx;
245+
}
246+
}
247+
248+
/**
249+
* Process a single expression to be inserted.
250+
*
251+
* @param[in] expr insert expression
252+
* @param[in] schema_idx index into schema columns, where the expr
253+
* will be inserted.
254+
* @return true if values imply a prepared statement
255+
* false if all values are constants. This does not rule
256+
* out the insert being a prepared statement.
257+
*/
258+
bool ProcessValueExpr(expression::AbstractExpression *expr,
259+
uint32_t schema_idx) {
260+
auto type = schema_to_insert_[schema_idx].type;
261+
262+
if (expr == nullptr) {
263+
SetDefaultValue(schema_idx);
264+
} else if (expr->GetExpressionType() == ExpressionType::VALUE_CONSTANT) {
265+
266+
auto *const_expr =
267+
dynamic_cast<expression::ConstantValueExpression *>(expr);
268+
type::Value value = const_expr->GetValue().CastAs(type);
269+
270+
schema_to_insert_[schema_idx].set_value = true;
271+
schema_to_insert_[schema_idx].value = value;
272+
// save it, in case this is not a PS
273+
values_.push_back(value);
274+
275+
return false;
276+
} else {
277+
PELOTON_ASSERT(expr->GetExpressionType() ==
278+
ExpressionType::VALUE_PARAMETER);
279+
return true;
280+
}
281+
return false;
282+
}
234283

284+
/**
285+
* Set default value into a schema column
286+
*
287+
* @param[in] idx schema column index
288+
*/
289+
void SetDefaultValue(uint32_t idx) {
290+
auto *schema = target_table_->GetSchema();
291+
type::Value *v = schema->GetDefaultValue(idx);
292+
type::TypeId type = schema_to_insert_[idx].type;
293+
294+
if (v == nullptr)
295+
// null default value
296+
values_.push_back(type::ValueFactory::GetNullValueByType(type));
297+
else
298+
// non-null default value
299+
values_.push_back(*v);
300+
}
301+
};
235302
} // namespace planner
236303
} // namespace peloton

src/planner/insert_plan.cpp

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ InsertPlan::InsertPlan(storage::DataTable *table,
9090
if (ret_bool) {
9191
is_prepared_stmt = true;
9292
}
93-
}
94-
else {
93+
} else {
9594
// schema column not present in insert columns spec. Set
9695
// column to its default value
9796
SetDefaultValue(idx);
@@ -127,82 +126,6 @@ InsertPlan::InsertPlan(storage::DataTable *table,
127126
}
128127
}
129128

130-
void InsertPlan::ProcessColumnSpec(const std::vector<std::string> *columns) {
131-
auto *schema = target_table_->GetSchema();
132-
auto &table_columns = schema->GetColumns();
133-
auto usr_col_count = columns->size();
134-
135-
// iterate over supplied columns
136-
for (size_t usr_col_id = 0; usr_col_id < usr_col_count; usr_col_id++) {
137-
uint32_t idx;
138-
auto col_name = columns->at(usr_col_id);
139-
140-
// determine index of column in schema
141-
bool found_col = FindSchemaColIndex(col_name, table_columns, idx);
142-
if (not found_col) {
143-
throw Exception("column " + col_name + " not in table " +
144-
target_table_->GetName() + " columns");
145-
}
146-
// we have values for this column
147-
schema_to_insert_[idx].in_insert_cols = true;
148-
// remember how to map schema col -> value for col in tuple
149-
schema_to_insert_[idx].val_idx = usr_col_id;
150-
// and the reverse
151-
insert_to_schema_[usr_col_id] = idx;
152-
}
153-
}
154-
155-
bool InsertPlan::ProcessValueExpr(expression::AbstractExpression *expr,
156-
uint32_t schema_idx) {
157-
auto type = schema_to_insert_[schema_idx].type;
158-
159-
if (expr == nullptr) {
160-
SetDefaultValue(schema_idx);
161-
} else if (expr->GetExpressionType() == ExpressionType::VALUE_CONSTANT) {
162-
163-
auto *const_expr =
164-
dynamic_cast<expression::ConstantValueExpression *>(expr);
165-
type::Value value = const_expr->GetValue().CastAs(type);
166-
167-
schema_to_insert_[schema_idx].set_value = true;
168-
schema_to_insert_[schema_idx].value = value;
169-
// save it, in case this is not a PS
170-
values_.push_back(value);
171-
172-
return false;
173-
} else {
174-
PELOTON_ASSERT(expr->GetExpressionType() ==
175-
ExpressionType::VALUE_PARAMETER);
176-
return true;
177-
}
178-
return false;
179-
}
180-
181-
void InsertPlan::SetDefaultValue(uint32_t idx) {
182-
auto *schema = target_table_->GetSchema();
183-
type::Value *v = schema->GetDefaultValue(idx);
184-
type::TypeId type = schema_to_insert_[idx].type;
185-
186-
if (v == nullptr)
187-
// null default value
188-
values_.push_back(type::ValueFactory::GetNullValueByType(type));
189-
else
190-
// non-null default value
191-
values_.push_back(*v);
192-
}
193-
194-
bool InsertPlan::FindSchemaColIndex(std::string col_name,
195-
const std::vector<catalog::Column> &tbl_columns,
196-
uint32_t &index) {
197-
for (auto tcol = tbl_columns.begin(); tcol != tbl_columns.end(); tcol++) {
198-
if (tcol->GetName() == col_name) {
199-
index = std::distance(tbl_columns.begin(), tcol);
200-
return true;
201-
}
202-
}
203-
return false;
204-
}
205-
206129
type::AbstractPool *InsertPlan::GetPlanPool() {
207130
if (pool_.get() == nullptr)
208131
pool_.reset(new type::EphemeralPool());

0 commit comments

Comments
 (0)