Skip to content

Commit

Permalink
Use CREATE INDEX SCAN rather than regular scan for ALTER TYPE to prev…
Browse files Browse the repository at this point in the history
…ent row offsets from becoming misaligned due to deletions (fixes #622)
  • Loading branch information
Mytherin committed May 5, 2020
1 parent 6ef7f2f commit 289b2ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/storage/data_table.cpp
Expand Up @@ -117,7 +117,9 @@ DataTable::DataTable(ClientContext &context, DataTable &parent, idx_t changed_id
transient_manager(parent.transient_manager), columns(parent.columns), is_root(true) {

// prevent any new tuples from being added to the parent
lock_guard<mutex> parent_lock(parent.append_lock);
CreateIndexScanState scan_state;
parent.InitializeCreateIndexScan(scan_state, bound_columns);

// first check if there are any indexes that exist that point to the changed column
for (auto &index : info->indexes) {
for (auto &column_id : index->column_ids) {
Expand All @@ -140,25 +142,23 @@ DataTable::DataTable(ClientContext &context, DataTable &parent, idx_t changed_id

// scan the original table, and fill the new column with the transformed value
auto &transaction = Transaction::GetTransaction(context);
TableScanState scan_state;

vector<TypeId> types;
for (idx_t i = 0; i < bound_columns.size(); i++) {
types.push_back(parent.types[i]);
}
parent.InitializeScan(transaction, scan_state, bound_columns, nullptr);

DataChunk scan_chunk;
scan_chunk.Initialize(types);
unordered_map<idx_t, vector<TableFilter>> dummy_filters;

ExpressionExecutor executor;
executor.AddExpression(cast_expr);

Vector append_vector(new_type);
while (true) {
// scan the table
parent.Scan(transaction, scan_chunk, scan_state, dummy_filters);
scan_chunk.Reset();
parent.CreateIndexScan(scan_state, scan_chunk);
if (scan_chunk.size() == 0) {
break;
}
Expand Down
8 changes: 8 additions & 0 deletions test/rigger/test_rigger.cpp
Expand Up @@ -695,4 +695,12 @@ TEST_CASE("Tests found by Rigger", "[rigger]") {
REQUIRE(CHECK_COLUMN(result, 0, {-1}));
REQUIRE(CHECK_COLUMN(result, 1, {Value()}));
}
SECTION("622") {
REQUIRE_NO_FAIL(con.Query("CREATE TABLE t0(c0 TIMESTAMP);"));
REQUIRE_NO_FAIL(con.Query("INSERT INTO t0 VALUES(NULL);"));
REQUIRE_NO_FAIL(con.Query("DELETE FROM t0;"));
REQUIRE_NO_FAIL(con.Query("ALTER TABLE t0 ALTER c0 TYPE DATE;"));
REQUIRE_NO_FAIL(con.Query("INSERT INTO t0 VALUES(NULL);"));
REQUIRE_NO_FAIL(con.Query("UPDATE t0 SET c0 = '1969-12-18'; "));
}
}

0 comments on commit 289b2ea

Please sign in to comment.