Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix non-partition table truncate not working correctly alter table add column #4109

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 27 additions & 9 deletions core/src/main/java/io/questdb/cairo/ColumnVersionWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
public class ColumnVersionWriter extends ColumnVersionReader {
private final CairoConfiguration configuration;
private final MemoryCMARW mem;
private final boolean partitioned;
private boolean hasChanges;
private long size;
private long version;
private final boolean partitioned;


// size should be read from the transaction file
Expand Down Expand Up @@ -133,20 +133,38 @@ public void removePartition(long partitionTimestamp) {
}

public void truncate() {
if (cachedColumnVersionList.size() > 0 && partitioned) {
if (cachedColumnVersionList.size() > 0) {

final long defaultPartitionTimestamp = COL_TOP_DEFAULT_PARTITION;
int from = cachedColumnVersionList.binarySearchBlock(BLOCK_SIZE_MSB, defaultPartitionTimestamp + 1, BinarySearch.SCAN_UP);
if (from < 0) {
from = -from - 1;
}
// Remove all partitions after COL_TOP_DEFAULT_PARTITION
if (from < cachedColumnVersionList.size()) {
cachedColumnVersionList.setPos(from);
}
// Keep default column version but reset the added timestamp to min
for (int i = 0, n = cachedColumnVersionList.size(); i < n; i += BLOCK_SIZE) {
cachedColumnVersionList.setQuick(i + TIMESTAMP_ADDED_PARTITION_OFFSET, defaultPartitionTimestamp);

if (partitioned) {
// Remove all partitions after COL_TOP_DEFAULT_PARTITION
if (from < cachedColumnVersionList.size()) {
cachedColumnVersionList.setPos(from);
}
// Keep default column version but reset the added timestamp to min
for (int i = 0, n = cachedColumnVersionList.size(); i < n; i += BLOCK_SIZE) {
cachedColumnVersionList.setQuick(i + TIMESTAMP_ADDED_PARTITION_OFFSET, defaultPartitionTimestamp);
}
} else {
// We have to keep all the column name txns because the files are truncated but not re-created.
// But we want to remove all the column tops.
// The column name txn can be added when the column is added via alter table or when column is updated.
// When ALTER table add column is executed it creates a record in the NaN partition with the column name txn
// and a record in 0 (default) partition with the column top.
// When the column is changed using UPDATE SQL, the column name txn is only set in 0 (default) partition.
// These 2 scenarios are test covered in TruncateTest.

// Result action is to remove all column tops and keep all column name txns.
for (int i = from; i < cachedColumnVersionList.size(); i += BLOCK_SIZE) {
cachedColumnVersionList.setQuick(i + COLUMN_TOP_OFFSET, 0);
}
}

hasChanges = true;
commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5228,6 +5228,8 @@ public void testSelectCharInNull() throws Exception {
@Test
public void testSelectConcurrentDDL() throws Exception {

// On Windows CI this test can fail with Metadata read timeout with small timeout.
node1.getConfigurationOverrides().setSpinLockTimeout(30000);
ddl("create table x (a int, b int, c int)");

final AtomicBoolean ddlError = new AtomicBoolean(false);
Expand Down
43 changes: 43 additions & 0 deletions core/src/test/java/io/questdb/test/griffin/TruncateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,49 @@

public class TruncateTest extends AbstractCairoTest {

@Test
public void testAddColumnTruncate() throws Exception {
assertMemoryLeak(() -> {
ddl(
"create table y as (" +
"select timestamp_sequence(0, 1000000000) timestamp," +
" x " +
" from long_sequence(10)" +
") timestamp (timestamp)"
);


ddl("alter table y add column new_x int", sqlExecutionContext);
ddl("truncate table y");

insert("insert into y values('2022-02-24', 1, 2)");

assertSql("timestamp\tx\tnew_x\n" +
"2022-02-24T00:00:00.000000Z\t1\t2\n", "select * from y");
});
}

@Test
public void testUpdateThenTruncate() throws Exception {
assertMemoryLeak(() -> {
ddl(
"create table y as (" +
"select timestamp_sequence(0, 1000000000) timestamp," +
" x " +
" from long_sequence(10)" +
") timestamp (timestamp)"
);

update("update y set x = 10");
ddl("truncate table y");

insert("insert into y values('2022-02-24', 1)");

assertSql("timestamp\tx\n" +
"2022-02-24T00:00:00.000000Z\t1\n", "select * from y");
});
}

@Test
public void testDropColumnTruncatePartitionByNone() throws Exception {
assertMemoryLeak(() -> {
Expand Down