1#include "duckdb/catalog/catalog.hpp"
2#include "duckdb/parser/expression/constant_expression.hpp"
3#include "duckdb/parser/statement/insert_statement.hpp"
4#include "duckdb/parser/query_node/select_node.hpp"
5#include "duckdb/parser/tableref/expressionlistref.hpp"
6#include "duckdb/planner/binder.hpp"
7#include "duckdb/planner/expression_binder/insert_binder.hpp"
8#include "duckdb/planner/operator/logical_insert.hpp"
9#include "duckdb/common/string_util.hpp"
10
11using namespace std;
12
13namespace duckdb {
14
15static void CheckInsertColumnCountMismatch(int64_t expected_columns, int64_t result_columns, bool columns_provided,
16 const char *tname) {
17 if (result_columns != expected_columns) {
18 string msg = StringUtil::Format(!columns_provided ? "table %s has %lld columns but %lld values were supplied"
19 : "Column name/value mismatch for insert on %s: "
20 "expected %lld columns but %lld values were supplied",
21 tname, expected_columns, result_columns);
22 throw BinderException(msg);
23 }
24}
25
26BoundStatement Binder::Bind(InsertStatement &stmt) {
27 BoundStatement result;
28 result.names = {"Count"};
29 result.types = {SQLType::BIGINT};
30
31 auto table = Catalog::GetCatalog(context).GetEntry<TableCatalogEntry>(context, stmt.schema, stmt.table);
32 assert(table);
33 if (!table->temporary) {
34 // inserting into a non-temporary table: alters underlying database
35 this->read_only = false;
36 }
37
38 auto insert = make_unique<LogicalInsert>(table);
39
40 vector<idx_t> named_column_map;
41 if (stmt.columns.size() > 0) {
42 // insertion statement specifies column list
43
44 // create a mapping of (list index) -> (column index)
45 unordered_map<string, idx_t> column_name_map;
46 for (idx_t i = 0; i < stmt.columns.size(); i++) {
47 column_name_map[stmt.columns[i]] = i;
48 auto entry = table->name_map.find(stmt.columns[i]);
49 if (entry == table->name_map.end()) {
50 throw BinderException("Column %s not found in table %s", stmt.columns[i].c_str(), table->name.c_str());
51 }
52 if (entry->second == COLUMN_IDENTIFIER_ROW_ID) {
53 throw BinderException("Cannot explicitly insert values into rowid column");
54 }
55 insert->expected_types.push_back(table->columns[entry->second].type);
56 named_column_map.push_back(entry->second);
57 }
58 for (idx_t i = 0; i < table->columns.size(); i++) {
59 auto &col = table->columns[i];
60 auto entry = column_name_map.find(col.name);
61 if (entry == column_name_map.end()) {
62 // column not specified, set index to INVALID_INDEX
63 insert->column_index_map.push_back(INVALID_INDEX);
64 } else {
65 // column was specified, set to the index
66 insert->column_index_map.push_back(entry->second);
67 }
68 }
69 } else {
70 for (idx_t i = 0; i < table->columns.size(); i++) {
71 insert->expected_types.push_back(table->columns[i].type);
72 }
73 }
74
75 // bind the default values
76 BindDefaultValues(table->columns, insert->bound_defaults);
77 if (!stmt.select_statement) {
78 result.plan = move(insert);
79 return result;
80 }
81
82 idx_t expected_columns = stmt.columns.size() == 0 ? table->columns.size() : stmt.columns.size();
83 // special case: check if we are inserting from a VALUES statement
84 if (stmt.select_statement->node->type == QueryNodeType::SELECT_NODE) {
85 auto &node = (SelectNode &)*stmt.select_statement->node;
86 if (node.from_table->type == TableReferenceType::EXPRESSION_LIST) {
87 auto &expr_list = (ExpressionListRef &)*node.from_table;
88 expr_list.expected_types.resize(expected_columns);
89 expr_list.expected_names.resize(expected_columns);
90
91 assert(expr_list.values.size() > 0);
92 CheckInsertColumnCountMismatch(expected_columns, expr_list.values[0].size(), stmt.columns.size() != 0,
93 table->name.c_str());
94
95 // VALUES list!
96 for (idx_t col_idx = 0; col_idx < expected_columns; col_idx++) {
97 idx_t table_col_idx = stmt.columns.size() == 0 ? col_idx : named_column_map[col_idx];
98 assert(table_col_idx < table->columns.size());
99
100 // set the expected types as the types for the INSERT statement
101 auto &column = table->columns[table_col_idx];
102 expr_list.expected_types[col_idx] = column.type;
103 expr_list.expected_names[col_idx] = column.name;
104
105 // now replace any DEFAULT values with the corresponding default expression
106 for (idx_t list_idx = 0; list_idx < expr_list.values.size(); list_idx++) {
107 if (expr_list.values[list_idx][col_idx]->type == ExpressionType::VALUE_DEFAULT) {
108 // DEFAULT value! replace the entry
109 if (column.default_value) {
110 expr_list.values[list_idx][col_idx] = column.default_value->Copy();
111 } else {
112 expr_list.values[list_idx][col_idx] =
113 make_unique<ConstantExpression>(column.type, Value(GetInternalType(column.type)));
114 }
115 }
116 }
117 }
118 }
119 }
120
121 // insert from select statement
122 // parse select statement and add to logical plan
123 auto root_select = Bind(*stmt.select_statement);
124 CheckInsertColumnCountMismatch(expected_columns, root_select.types.size(), stmt.columns.size() != 0,
125 table->name.c_str());
126
127 auto root = CastLogicalOperatorToTypes(root_select.types, insert->expected_types, move(root_select.plan));
128 insert->AddChild(move(root));
129
130 result.plan = move(insert);
131 return result;
132}
133
134} // namespace duckdb
135