Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Commit c4112f8

Browse files
authored
Fix iterator invalidation in binding insert statements. (#1413)
1 parent d770eb9 commit c4112f8

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

script/testing/junit/traces/insert.test

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,39 @@ SELECT * from insert12
231231
statement ok
232232
DROP TABLE insert12
233233

234+
# Test inserts that are: out of order, missing values, multiple values, multiple types. Issue #1408
235+
statement ok
236+
CREATE TABLE insert13 (c1 integer, c2 double)
237+
238+
statement ok
239+
INSERT INTO insert13 (c2, c1) VALUES (2.0, 1), (4.0, 2), (8.0, 3)
240+
241+
query IR rowsort
242+
SELECT * FROM insert13
243+
----
244+
1
245+
2.0
246+
2
247+
4.0
248+
3
249+
8.0
250+
251+
statement ok
252+
INSERT INTO insert13 (c2) VALUES (16.0),(32.0),(64.0)
253+
254+
query R rowsort
255+
SELECT c2 FROM insert13 WHERE c1 IS NULL
256+
----
257+
16.0
258+
32.0
259+
64.0
260+
261+
query R rowsort
262+
SELECT c2 FROM insert13 WHERE c1 IS NOT NULL
263+
----
264+
2.0
265+
4.0
266+
8.0
267+
268+
statement ok
269+
DROP TABLE insert13

src/binder/bind_node_visitor.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,8 @@ void BindNodeVisitor::Visit(common::ManagedPointer<parser::InsertStatement> node
398398
}
399399

400400
// We overwrite the original insert columns and values with the schema-ordered versions generated above.
401-
insert_columns->clear();
402401
values.clear();
403402
for (auto &pair : cols) {
404-
insert_columns->emplace_back(pair.first.Name());
405403
values.emplace_back(pair.second);
406404
}
407405
}
@@ -444,6 +442,15 @@ void BindNodeVisitor::Visit(common::ManagedPointer<parser::InsertStatement> node
444442
}
445443
}
446444
}
445+
// The final list of insert columns will always be the full list. Done here to avoid iterator invalidation problems.
446+
{
447+
const auto &cols = table_schema.GetColumns();
448+
node->GetInsertColumns()->clear();
449+
node->GetInsertColumns()->reserve(cols.size());
450+
for (const auto &col : cols) {
451+
node->GetInsertColumns()->emplace_back(col.Name());
452+
}
453+
}
447454
}
448455

449456
context_ = nullptr;

0 commit comments

Comments
 (0)