12
12
13
13
#pragma once
14
14
15
+ #include " expression/constant_value_expression.h"
16
+ #include " storage/data_table.h"
15
17
#include " planner/abstract_plan.h"
16
18
#include " planner/abstract_scan_plan.h"
17
19
#include " planner/project_info.h"
@@ -29,27 +31,9 @@ class InsertStatement;
29
31
}
30
32
31
33
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
- };
51
34
52
35
class InsertPlan : public AbstractPlan {
36
+
53
37
public:
54
38
// Construct when SELECT comes in with it
55
39
InsertPlan (storage::DataTable *table, oid_t bulk_insert_count = 1 )
@@ -91,38 +75,7 @@ class InsertPlan : public AbstractPlan {
91
75
// Get a varlen pool - will construct the pool only if needed
92
76
type::AbstractPool *GetPlanPool ();
93
77
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; };
126
79
127
80
/* *
128
81
* @brief Save values for jdbc prepared statement insert.
@@ -132,18 +85,6 @@ class InsertPlan : public AbstractPlan {
132
85
*/
133
86
void SetParameterValues (std::vector<type::Value> *values) override ;
134
87
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);
147
88
/*
148
89
* Clear the parameter values of the current insert. The plan may be
149
90
* cached in the statement / plan cache and may be reused.
@@ -195,14 +136,33 @@ class InsertPlan : public AbstractPlan {
195
136
const std::vector<peloton::type::Value> &values_from_user) override ;
196
137
197
138
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
+
198
158
// Target table
199
159
storage::DataTable *target_table_ = nullptr ;
200
160
201
161
// Values
202
162
std::vector<type::Value> values_;
203
163
204
164
// mapping from schema columns to vector of insert columns
205
- std::vector<struct SchemaColsToInsertCols > schema_to_insert_;
165
+ std::vector<SchemaColsToInsertCols> schema_to_insert_;
206
166
// mapping from insert columns to schema columns
207
167
std::vector<uint32_t > insert_to_schema_;
208
168
@@ -230,7 +190,114 @@ class InsertPlan : public AbstractPlan {
230
190
231
191
private:
232
192
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
+ }
234
283
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
+ };
235
302
} // namespace planner
236
303
} // namespace peloton
0 commit comments