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

Commit 6ad5cde

Browse files
committed
Changes from review
1 parent f48ad6e commit 6ad5cde

File tree

2 files changed

+79
-78
lines changed

2 files changed

+79
-78
lines changed

src/include/planner/insert_plan.h

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class InsertStatement;
3131
namespace planner {
3232

3333
// mapping from schema columns to insert columns
34-
struct schema_cols_to_insert_cols {
34+
struct SchemaColsToInsertCols {
3535
// this schema column is present in the insert columns
3636
bool in_insert_cols;
3737

@@ -77,25 +77,71 @@ class InsertPlan : public AbstractPlan {
7777
tuples_.push_back(std::move(tuple));
7878
}
7979

80-
// Construct with specific values
81-
InsertPlan(storage::DataTable *table, const std::vector<std::string> *columns,
82-
const std::vector<std::vector<std::unique_ptr<
83-
expression::AbstractExpression>>> *insert_values);
80+
/**
81+
* @brief Create an insert plan with specific values
82+
*
83+
* @param[in] table Table to insert into
84+
* @param[in] columns Columns to insert into
85+
* @param[in] insert_values Values
86+
*/
87+
InsertPlan(storage::DataTable *table,
88+
const std::vector<std::string> *columns,
89+
const std::vector<std::vector<std::unique_ptr<expression::AbstractExpression>>> *insert_values);
8490

8591
// Get a varlen pool - will construct the pool only if needed
8692
type::AbstractPool *GetPlanPool();
8793

8894
PlanNodeType GetPlanNodeType() const override {
8995
return PlanNodeType::INSERT; };
9096

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+
*/
91106
bool FindSchemaColIndex(std::string col_name,
92107
const std::vector<catalog::Column> &tbl_columns,
93108
uint32_t &index);
94109

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+
*/
95118
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+
*/
96125
void SetDefaultValue(uint32_t idx);
126+
127+
/**
128+
* @brief Save values for jdbc prepared statement insert.
129+
* Only a single tuple is presented to this function.
130+
*
131+
* @param[in] values Values for insertion
132+
*/
97133
void SetParameterValues(std::vector<type::Value> *values) override;
98134

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+
*/
99145
bool ProcessValueExpr(expression::AbstractExpression *expr,
100146
uint32_t schema_idx);
101147
/*
@@ -156,9 +202,9 @@ class InsertPlan : public AbstractPlan {
156202
std::vector<type::Value> values_;
157203

158204
// mapping from schema columns to vector of insert columns
159-
std::vector<struct schema_cols_to_insert_cols> stov_;
205+
std::vector<struct SchemaColsToInsertCols> schema_to_insert_;
160206
// mapping from insert columns to schema columns
161-
std::vector<uint32_t> vtos_;
207+
std::vector<uint32_t> insert_to_schema_;
162208

163209
// Projection Info
164210
std::unique_ptr<const planner::ProjectInfo> project_info_;

src/planner/insert_plan.cpp

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@
2121
namespace peloton {
2222
namespace planner {
2323

24-
/**
25-
* @brief Create an insert plan
26-
*
27-
* @param[in] table Table to insert into
28-
* @param[in] columns Columns to insert into
29-
* @param[in] insert_values Values
30-
*/
3124
InsertPlan::InsertPlan(storage::DataTable *table,
3225
const std::vector<std::string> *columns,
3326
const std::vector<std::vector<
@@ -42,16 +35,16 @@ InsertPlan::InsertPlan(storage::DataTable *table,
4235
bool is_prepared_stmt = false;
4336
auto *schema = target_table_->GetSchema();
4437
auto schema_col_count = schema->GetColumnCount();
45-
vtos_.resize(columns->size());
38+
insert_to_schema_.resize(columns->size());
4639
// initialize mapping from schema cols to insert values vector.
4740
// will be updated later based on insert columns and values
48-
stov_.resize(schema_col_count);
41+
schema_to_insert_.resize(schema_col_count);
4942
for (uint32_t idx = 0; idx < schema_col_count; idx++) {
50-
stov_[idx].in_insert_cols = false;
51-
stov_[idx].set_value = false;
52-
stov_[idx].val_idx = 0;
43+
schema_to_insert_[idx].in_insert_cols = false;
44+
schema_to_insert_[idx].set_value = false;
45+
schema_to_insert_[idx].val_idx = 0;
5346
// remember the column types
54-
stov_[idx].type = schema->GetType(idx);
47+
schema_to_insert_[idx].type = schema->GetType(idx);
5548
}
5649

5750
if (columns->empty()) {
@@ -68,8 +61,8 @@ InsertPlan::InsertPlan(storage::DataTable *table,
6861
auto ret_bool = ProcessValueExpr(exp_ptr, column_id);
6962
// there is no column specification, so we have a
7063
// direct mapping between schema cols and the value vector
71-
stov_[column_id].in_insert_cols = true;
72-
stov_[column_id].val_idx = column_id;
64+
schema_to_insert_[column_id].in_insert_cols = true;
65+
schema_to_insert_[column_id].val_idx = column_id;
7366
if (ret_bool == true) {
7467
is_prepared_stmt = true;
7568
}
@@ -87,10 +80,10 @@ InsertPlan::InsertPlan(storage::DataTable *table,
8780
PL_ASSERT(values.size() <= schema_col_count);
8881

8982
for (uint32_t idx = 0; idx < schema_col_count; idx++) {
90-
if (stov_[idx].in_insert_cols) {
83+
if (schema_to_insert_[idx].in_insert_cols) {
9184
// this schema column is present in the insert columns spec.
9285
// get index into values
93-
auto val_idx = stov_[idx].val_idx;
86+
auto val_idx = schema_to_insert_[idx].val_idx;
9487
auto &exp = values[val_idx];
9588
auto exp_ptr = exp.get();
9689
bool ret_bool = ProcessValueExpr(exp_ptr, idx);
@@ -114,13 +107,13 @@ InsertPlan::InsertPlan(storage::DataTable *table,
114107
// the constant columns. If there are no constants, this is a no-op.
115108
uint32_t adjust = 0;
116109
for (uint32_t idx=0; idx < columns->size(); idx++) {
117-
uint32_t stov_idx = vtos_[idx];
118-
if (stov_[stov_idx].set_value) {
110+
uint32_t stov_idx = insert_to_schema_[idx];
111+
if (schema_to_insert_[stov_idx].set_value) {
119112
// constant, not present in PS values
120113
adjust++;
121114
} else {
122115
// adjust the index
123-
stov_[stov_idx].val_idx -= adjust;
116+
schema_to_insert_[stov_idx].val_idx -= adjust;
124117
}
125118
}
126119
}
@@ -133,15 +126,7 @@ InsertPlan::InsertPlan(storage::DataTable *table,
133126
ClearParameterValues();
134127
}
135128
}
136-
137-
/**
138-
* Process column specification supplied in the insert statement.
139-
* Construct a map from insert columns to schema columns. Once
140-
* we know which columns will receive constant inserts, further
141-
* adjustment of the map will be needed.
142-
*
143-
* @param[in] columns Column specification
144-
*/
129+
145130
void InsertPlan::ProcessColumnSpec(const std::vector<std::string> *columns) {
146131
auto *schema = target_table_->GetSchema();
147132
auto &table_columns = schema->GetColumns();
@@ -159,27 +144,17 @@ void InsertPlan::ProcessColumnSpec(const std::vector<std::string> *columns) {
159144
target_table_->GetName() + " columns");
160145
}
161146
// we have values for this column
162-
stov_[idx].in_insert_cols = true;
147+
schema_to_insert_[idx].in_insert_cols = true;
163148
// remember how to map schema col -> value for col in tuple
164-
stov_[idx].val_idx = usr_col_id;
149+
schema_to_insert_[idx].val_idx = usr_col_id;
165150
// and the reverse
166-
vtos_[usr_col_id] = idx;
151+
insert_to_schema_[usr_col_id] = idx;
167152
}
168153
}
169154

170-
/**
171-
* Process a single expression to be inserted.
172-
*
173-
* @param[in] expr insert expression
174-
* @param[in] schema_idx index into schema columns, where the expr
175-
* will be inserted.
176-
* @return true if values imply a prepared statement
177-
* false if all values are constants. This does not rule
178-
* out the insert being a prepared statement.
179-
*/
180155
bool InsertPlan::ProcessValueExpr(expression::AbstractExpression *expr,
181156
uint32_t schema_idx) {
182-
auto type = stov_[schema_idx].type;
157+
auto type = schema_to_insert_[schema_idx].type;
183158

184159
if (expr == nullptr) {
185160
SetDefaultValue(schema_idx);
@@ -189,8 +164,8 @@ bool InsertPlan::ProcessValueExpr(expression::AbstractExpression *expr,
189164
dynamic_cast<expression::ConstantValueExpression *>(expr);
190165
type::Value value = const_expr->GetValue().CastAs(type);
191166

192-
stov_[schema_idx].set_value = true;
193-
stov_[schema_idx].value = value;
167+
schema_to_insert_[schema_idx].set_value = true;
168+
schema_to_insert_[schema_idx].value = value;
194169
// save it, in case this is not a PS
195170
values_.push_back(value);
196171

@@ -202,15 +177,10 @@ bool InsertPlan::ProcessValueExpr(expression::AbstractExpression *expr,
202177
return false;
203178
}
204179

205-
/**
206-
* Set default value into a schema column
207-
*
208-
* @param[in] idx schema column index
209-
*/
210180
void InsertPlan::SetDefaultValue(uint32_t idx) {
211181
auto *schema = target_table_->GetSchema();
212182
type::Value *v = schema->GetDefaultValue(idx);
213-
type::TypeId type = stov_[idx].type;
183+
type::TypeId type = schema_to_insert_[idx].type;
214184

215185
if (v == nullptr)
216186
// null default value
@@ -220,15 +190,6 @@ void InsertPlan::SetDefaultValue(uint32_t idx) {
220190
values_.push_back(*v);
221191
}
222192

223-
/**
224-
* Lookup a column name in the schema columns
225-
*
226-
* @param[in] col_name column name, from insert statement
227-
* @param[in] tbl_columns table columns from the schema
228-
* @param[out] index index into schema columns, only if found
229-
*
230-
* @return true if column was found, false otherwise
231-
*/
232193
bool InsertPlan::FindSchemaColIndex(std::string col_name,
233194
const std::vector<catalog::Column> &tbl_columns,
234195
uint32_t &index) {
@@ -247,25 +208,19 @@ type::AbstractPool *InsertPlan::GetPlanPool() {
247208
return pool_.get();
248209
}
249210

250-
/**
251-
* @brief Save values for jdbc prepared statement insert.
252-
* Only a single tuple is presented to this function.
253-
*
254-
* @param[in] values Values for insertion
255-
*/
256211
void InsertPlan::SetParameterValues(std::vector<type::Value> *values) {
257212
LOG_TRACE("Set Parameter Values in Insert");
258213
auto *schema = target_table_->GetSchema();
259214
auto schema_col_count = schema->GetColumnCount();
260215

261216
PL_ASSERT(values->size() <= schema_col_count);
262217
for (uint32_t idx = 0; idx < schema_col_count; idx++) {
263-
if (stov_[idx].set_value) {
264-
values_.push_back(stov_[idx].value);
265-
} else if (stov_[idx].in_insert_cols) {
218+
if (schema_to_insert_[idx].set_value) {
219+
values_.push_back(schema_to_insert_[idx].value);
220+
} else if (schema_to_insert_[idx].in_insert_cols) {
266221
// get index into values
267-
auto val_idx = stov_[idx].val_idx;
268-
auto type = stov_[idx].type;
222+
auto val_idx = schema_to_insert_[idx].val_idx;
223+
auto type = schema_to_insert_[idx].type;
269224
type::Value value = values->at(val_idx).CastAs(type);
270225
values_.push_back(value);
271226
} else {

0 commit comments

Comments
 (0)