6
6
//
7
7
// Identification: src/include/planner/insert_plan.h
8
8
//
9
- // Copyright (c) 2015-17 , Carnegie Mellon University Database Group
9
+ // Copyright (c) 2015-2018 , Carnegie Mellon University Database Group
10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
@@ -29,11 +29,12 @@ class InsertStatement;
29
29
}
30
30
31
31
namespace planner {
32
+
32
33
class InsertPlan : public AbstractPlan {
33
34
public:
34
35
// Construct when SELECT comes in with it
35
36
InsertPlan (storage::DataTable *table, oid_t bulk_insert_count = 1 )
36
- : target_table_(table), bulk_insert_count_(bulk_insert_count) {
37
+ : target_table_(table), bulk_insert_count_(bulk_insert_count) {
37
38
LOG_TRACE (" Creating an Insert Plan with SELECT as a child" );
38
39
}
39
40
@@ -42,35 +43,50 @@ class InsertPlan : public AbstractPlan {
42
43
InsertPlan (storage::DataTable *table,
43
44
std::unique_ptr<const planner::ProjectInfo> &&project_info,
44
45
oid_t bulk_insert_count = 1 )
45
- : target_table_(table), project_info_(std::move(project_info)),
46
- bulk_insert_count_ (bulk_insert_count) {
46
+ : target_table_(table),
47
+ project_info_ (std::move(project_info)),
48
+ bulk_insert_count_(bulk_insert_count) {
47
49
LOG_TRACE (" Creating an Insert Plan with a projection" );
48
50
}
49
51
50
52
// Construct with a tuple
51
53
// This can only be handled by the interpreted exeuctor
52
- InsertPlan (storage::DataTable *table,
53
- std::unique_ptr<storage::Tuple> &&tuple,
54
+ InsertPlan (storage::DataTable *table, std::unique_ptr<storage::Tuple> &&tuple,
54
55
oid_t bulk_insert_count = 1 )
55
- : target_table_(table), bulk_insert_count_(bulk_insert_count) {
56
+ : target_table_(table), bulk_insert_count_(bulk_insert_count) {
56
57
LOG_TRACE (" Creating an Insert Plan for one tuple" );
57
58
tuples_.push_back (std::move (tuple));
58
59
}
59
60
60
- // Construct with specific values
61
+ /* *
62
+ * @brief Create an insert plan with specific values
63
+ *
64
+ * @param[in] table Table to insert into
65
+ * @param[in] columns Columns to insert into
66
+ * @param[in] insert_values Values
67
+ */
61
68
InsertPlan (storage::DataTable *table, const std::vector<std::string> *columns,
62
- const std::vector<std::vector<std::unique_ptr<
63
- expression::AbstractExpression>>> *insert_values);
69
+ const std::vector<
70
+ std::vector<std::unique_ptr<expression::AbstractExpression>>> *
71
+ insert_values);
64
72
65
73
// Get a varlen pool - will construct the pool only if needed
66
74
type::AbstractPool *GetPlanPool ();
67
75
68
- PlanNodeType GetPlanNodeType () const override { return PlanNodeType::INSERT; }
76
+ PlanNodeType GetPlanNodeType () const override {
77
+ return PlanNodeType::INSERT;
78
+ };
69
79
80
+ /* *
81
+ * @brief Save values for jdbc prepared statement insert.
82
+ * Only a single tuple is presented to this function.
83
+ *
84
+ * @param[in] values Values for insertion
85
+ */
70
86
void SetParameterValues (std::vector<type::Value> *values) override ;
71
87
72
- /*
73
- * Clear the parameter values of the current insert. The plan may be
88
+ /*
89
+ * Clear the parameter values of the current insert. The plan may be
74
90
* cached in the statement / plan cache and may be reused.
75
91
*/
76
92
void ClearParameterValues () override { values_.clear (); }
@@ -115,17 +131,86 @@ class InsertPlan : public AbstractPlan {
115
131
return !(*this == rhs);
116
132
}
117
133
118
- virtual void VisitParameters (codegen::QueryParametersMap &map,
134
+ virtual void VisitParameters (
135
+ codegen::QueryParametersMap &map,
119
136
std::vector<peloton::type::Value> &values,
120
137
const std::vector<peloton::type::Value> &values_from_user) override ;
121
138
122
139
private:
140
+ /* *
141
+ * Lookup a column name in the schema columns
142
+ *
143
+ * @param[in] col_name column name, from insert statement
144
+ * @param[in] tbl_columns table columns from the schema
145
+ * @param[out] index index into schema columns, only if found
146
+ *
147
+ * @return true if column was found, false otherwise
148
+ */
149
+ bool FindSchemaColIndex (std::string col_name,
150
+ const std::vector<catalog::Column> &tbl_columns,
151
+ uint32_t &index);
152
+
153
+ /* *
154
+ * Process column specification supplied in the insert statement.
155
+ * Construct a map from insert columns to schema columns. Once
156
+ * we know which columns will receive constant inserts, further
157
+ * adjustment of the map will be needed.
158
+ *
159
+ * @param[in] columns Column specification
160
+ */
161
+ void ProcessColumnSpec (const std::vector<std::string> *columns);
162
+
163
+ /* *
164
+ * Process a single expression to be inserted.
165
+ *
166
+ * @param[in] expr insert expression
167
+ * @param[in] schema_idx index into schema columns, where the expr
168
+ * will be inserted.
169
+ * @return true if values imply a prepared statement
170
+ * false if all values are constants. This does not rule
171
+ * out the insert being a prepared statement.
172
+ */
173
+ bool ProcessValueExpr (expression::AbstractExpression *expr,
174
+ uint32_t schema_idx);
175
+
176
+ /* *
177
+ * Set default value into a schema column
178
+ *
179
+ * @param[in] idx schema column index
180
+ */
181
+ void SetDefaultValue (uint32_t idx);
182
+
183
+ private:
184
+ // mapping from schema columns to insert columns
185
+ struct SchemaColsToInsertCols {
186
+ // this schema column is present in the insert columns
187
+ bool in_insert_cols;
188
+
189
+ // For a PS, insert saved value (from constant in insert values list), no
190
+ // param value.
191
+ bool set_value;
192
+
193
+ // index of this column in insert columns values
194
+ int val_idx;
195
+
196
+ // schema column type
197
+ type::TypeId type;
198
+
199
+ // set_value refers to this saved value
200
+ type::Value value;
201
+ };
202
+
123
203
// Target table
124
204
storage::DataTable *target_table_ = nullptr ;
125
205
126
206
// Values
127
207
std::vector<type::Value> values_;
128
208
209
+ // mapping from schema columns to vector of insert columns
210
+ std::vector<SchemaColsToInsertCols> schema_to_insert_;
211
+ // mapping from insert columns to schema columns
212
+ std::vector<uint32_t > insert_to_schema_;
213
+
129
214
// Projection Info
130
215
std::unique_ptr<const planner::ProjectInfo> project_info_;
131
216
@@ -148,9 +233,7 @@ class InsertPlan : public AbstractPlan {
148
233
// Pool for variable length types
149
234
std::unique_ptr<type::AbstractPool> pool_;
150
235
151
- private:
152
236
DISALLOW_COPY_AND_MOVE (InsertPlan);
153
237
};
154
-
155
238
} // namespace planner
156
239
} // namespace peloton
0 commit comments