Skip to content

Commit f9b7f9d

Browse files
committed
Only delete data, but don't delete tsfile when performing a drop column statement on the table model or a drop tag statement on the tree model.
1 parent d9fafc9 commit f9b7f9d

File tree

4 files changed

+176
-2
lines changed

4 files changed

+176
-2
lines changed

integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBDeletionIT.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,58 @@ public void testDeleteByRangeComparison() throws SQLException {
488488
}
489489
}
490490

491+
@Test
492+
public void testDropAndAlter() throws SQLException {
493+
try (Connection connection = EnvFactory.getEnv().getConnection();
494+
Statement statement = connection.createStatement()) {
495+
statement.execute("CREATE DATABASE root.test");
496+
statement.execute(
497+
"CREATE TIMESERIES root.test.g_0.d3.s_10 with datatype=INT32 tags(tag1=v1, tag2=v2)");
498+
499+
// time=1 and time=2 are INT32 and deleted by drop column
500+
statement.execute("INSERT INTO root.test.g_0.d3(timestamp, s_10) VALUES(1, 1)");
501+
502+
statement.execute("FLUSH");
503+
504+
statement.execute("INSERT INTO root.test.g_0.d3(timestamp, s_10) VALUES(2, 2)");
505+
506+
statement.execute("ALTER TIMESERIES root.test.g_0.d3.s_10 DROP tag1");
507+
508+
// time=3 and time=4 are STRING
509+
statement.execute("INSERT INTO root.test.g_0.d3(timestamp, s_10) VALUES(3, 3)");
510+
511+
statement.execute("FLUSH");
512+
513+
statement.execute("INSERT INTO root.test.g_0.d3(timestamp, s_10) VALUES(4, 4)");
514+
515+
statement.execute("ALTER TIMESERIES root.test.g_0.d3.s_10 ADD TAGS tag1=v1");
516+
517+
// time=5 and time=6 are TEXT
518+
statement.execute("INSERT INTO root.test.g_0.d3(timestamp, s_10) VALUES(5, 5)");
519+
520+
statement.execute("FLUSH");
521+
522+
statement.execute("INSERT INTO root.test.g_0.d3(timestamp, s_10) VALUES(6, 6)");
523+
524+
try (ResultSet dataSet =
525+
statement.executeQuery("select * from root.test.g_0.d3 order by time")) {
526+
// s1 is dropped but the time should remain
527+
int i = 1;
528+
while (dataSet.next()) {
529+
assertEquals(i, dataSet.getLong(1));
530+
i++;
531+
}
532+
Assert.assertEquals(6, i - 1);
533+
assertFalse(dataSet.next());
534+
}
535+
} finally {
536+
try (Connection connection = EnvFactory.getEnv().getConnection();
537+
Statement statement = connection.createStatement()) {
538+
statement.execute("DROP DATABASE root.test");
539+
}
540+
}
541+
}
542+
491543
private static void prepareSeries() {
492544
try (Connection connection = EnvFactory.getEnv().getConnection();
493545
Statement statement = connection.createStatement()) {

integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBDeletionTableIT.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
import org.apache.iotdb.rpc.IoTDBConnectionException;
3434
import org.apache.iotdb.rpc.StatementExecutionException;
3535

36+
import org.apache.tsfile.enums.ColumnCategory;
37+
import org.apache.tsfile.enums.TSDataType;
3638
import org.apache.tsfile.read.common.RowRecord;
3739
import org.apache.tsfile.read.common.TimeRange;
40+
import org.apache.tsfile.write.record.Tablet;
3841
import org.awaitility.Awaitility;
3942
import org.junit.After;
4043
import org.junit.AfterClass;
@@ -60,6 +63,7 @@
6063
import java.sql.SQLException;
6164
import java.sql.Statement;
6265
import java.util.ArrayList;
66+
import java.util.Collections;
6367
import java.util.List;
6468
import java.util.Locale;
6569
import java.util.Random;
@@ -74,6 +78,7 @@
7478
import java.util.stream.Collectors;
7579
import java.util.stream.Stream;
7680

81+
import static org.apache.iotdb.relational.it.session.IoTDBSessionRelationalIT.genValue;
7782
import static org.junit.Assert.assertEquals;
7883
import static org.junit.Assert.assertFalse;
7984
import static org.junit.Assert.assertTrue;
@@ -2268,6 +2273,110 @@ public void testMultiDeviceCompletelyDeleteTable() throws SQLException {
22682273
cleanData(testNum);
22692274
}
22702275

2276+
@Test
2277+
public void testDropAndAlter() throws IoTDBConnectionException, StatementExecutionException {
2278+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnectionWithDB("test")) {
2279+
session.executeNonQueryStatement("CREATE TABLE IF NOT EXISTS drop_and_alter (s1 int32)");
2280+
2281+
// time=1 and time=2 are INT32 and deleted by drop column
2282+
Tablet tablet =
2283+
new Tablet(
2284+
"drop_and_alter",
2285+
Collections.singletonList("s1"),
2286+
Collections.singletonList(TSDataType.INT32),
2287+
Collections.singletonList(ColumnCategory.FIELD));
2288+
tablet.addTimestamp(0, 1);
2289+
tablet.addValue("s1", 0, genValue(TSDataType.INT32, 1));
2290+
session.insert(tablet);
2291+
tablet.reset();
2292+
2293+
session.executeNonQueryStatement("FLUSH");
2294+
2295+
tablet =
2296+
new Tablet(
2297+
"drop_and_alter",
2298+
Collections.singletonList("s1"),
2299+
Collections.singletonList(TSDataType.INT32),
2300+
Collections.singletonList(ColumnCategory.FIELD));
2301+
tablet.addTimestamp(0, 2);
2302+
tablet.addValue("s1", 0, genValue(TSDataType.INT32, 2));
2303+
session.insert(tablet);
2304+
tablet.reset();
2305+
2306+
session.executeNonQueryStatement("ALTER TABLE drop_and_alter DROP COLUMN s1");
2307+
2308+
// time=3 and time=4 are STRING
2309+
tablet =
2310+
new Tablet(
2311+
"drop_and_alter",
2312+
Collections.singletonList("s1"),
2313+
Collections.singletonList(TSDataType.STRING),
2314+
Collections.singletonList(ColumnCategory.FIELD));
2315+
tablet.addTimestamp(0, 3);
2316+
tablet.addValue("s1", 0, genValue(TSDataType.STRING, 3));
2317+
session.insert(tablet);
2318+
tablet.reset();
2319+
2320+
session.executeNonQueryStatement("FLUSH");
2321+
2322+
tablet =
2323+
new Tablet(
2324+
"drop_and_alter",
2325+
Collections.singletonList("s1"),
2326+
Collections.singletonList(TSDataType.STRING),
2327+
Collections.singletonList(ColumnCategory.FIELD));
2328+
tablet.addTimestamp(0, 4);
2329+
tablet.addValue("s1", 0, genValue(TSDataType.STRING, 4));
2330+
session.insert(tablet);
2331+
tablet.reset();
2332+
2333+
session.executeNonQueryStatement("ALTER TABLE drop_and_alter DROP COLUMN s1");
2334+
session.executeNonQueryStatement("ALTER TABLE drop_and_alter ADD COLUMN s1 TEXT");
2335+
2336+
// time=5 and time=6 are TEXT
2337+
tablet =
2338+
new Tablet(
2339+
"drop_and_alter",
2340+
Collections.singletonList("s1"),
2341+
Collections.singletonList(TSDataType.TEXT),
2342+
Collections.singletonList(ColumnCategory.FIELD));
2343+
tablet.addTimestamp(0, 5);
2344+
tablet.addValue("s1", 0, genValue(TSDataType.STRING, 5));
2345+
session.insert(tablet);
2346+
tablet.reset();
2347+
2348+
session.executeNonQueryStatement("FLUSH");
2349+
2350+
tablet =
2351+
new Tablet(
2352+
"drop_and_alter",
2353+
Collections.singletonList("s1"),
2354+
Collections.singletonList(TSDataType.TEXT),
2355+
Collections.singletonList(ColumnCategory.FIELD));
2356+
tablet.addTimestamp(0, 6);
2357+
tablet.addValue("s1", 0, genValue(TSDataType.STRING, 6));
2358+
session.insert(tablet);
2359+
tablet.reset();
2360+
2361+
SessionDataSet dataSet =
2362+
session.executeQueryStatement("select * from drop_and_alter order by time");
2363+
// s1 is dropped but the time should remain
2364+
RowRecord rec;
2365+
int cnt = 0;
2366+
for (int i = 1; i < 7; i++) {
2367+
rec = dataSet.next();
2368+
assertEquals(i, rec.getFields().get(0).getLongV());
2369+
cnt++;
2370+
}
2371+
Assert.assertEquals(6, cnt);
2372+
assertFalse(dataSet.hasNext());
2373+
} finally {
2374+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnectionWithDB("test")) {
2375+
session.executeNonQueryStatement("DROP TABLE IF EXISTS drop_and_alter");
2376+
}
2377+
}
2378+
}
2379+
22712380
private static void prepareDatabase() {
22722381
try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
22732382
Statement statement = connection.createStatement()) {

integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBSessionRelationalIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ private void testOneCastWithRow(
15961596
}
15971597

15981598
@SuppressWarnings("SameParameterValue")
1599-
private Object genValue(TSDataType dataType, int i) {
1599+
public static Object genValue(TSDataType dataType, int i) {
16001600
switch (dataType) {
16011601
case INT32:
16021602
return i;

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3030,6 +3030,19 @@ private void deleteDataInSealedFiles(Collection<TsFileResource> sealedTsFiles, M
30303030
Set<ModificationFile> involvedModificationFiles = new HashSet<>();
30313031
List<TsFileResource> deletedByMods = new ArrayList<>();
30323032
List<TsFileResource> deletedByFiles = new ArrayList<>();
3033+
boolean isDropMeasurementExist = false;
3034+
boolean isDropTagExist = false;
3035+
3036+
if (deletion instanceof TableDeletionEntry) {
3037+
TableDeletionEntry entry = (TableDeletionEntry) deletion;
3038+
isDropMeasurementExist = !entry.getPredicate().getMeasurementNames().isEmpty();
3039+
} else {
3040+
TreeDeletionEntry entry = (TreeDeletionEntry) deletion;
3041+
if (entry.getPathPattern() instanceof MeasurementPath) {
3042+
isDropTagExist = !((MeasurementPath) entry.getPathPattern()).getTagMap().isEmpty();
3043+
}
3044+
}
3045+
30333046
for (TsFileResource sealedTsFile : sealedTsFiles) {
30343047
if (canSkipDelete(sealedTsFile, deletion)) {
30353048
continue;
@@ -3126,7 +3139,7 @@ private void deleteDataInSealedFiles(Collection<TsFileResource> sealedTsFiles, M
31263139
} // else do nothing
31273140
}
31283141

3129-
if (!deletedByFiles.isEmpty()) {
3142+
if (!deletedByFiles.isEmpty() && !isDropMeasurementExist && !isDropTagExist) {
31303143
deleteTsFileCompletely(deletedByFiles);
31313144
if (logger.isDebugEnabled()) {
31323145
logger.debug(

0 commit comments

Comments
 (0)