Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;

import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.read.common.RowRecord;
import org.apache.tsfile.read.common.TimeRange;
import org.apache.tsfile.write.record.Tablet;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.AfterClass;
Expand All @@ -60,6 +63,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Random;
Expand All @@ -74,6 +78,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.apache.iotdb.relational.it.session.IoTDBSessionRelationalIT.genValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -2289,6 +2294,159 @@ public void testMultiDeviceCompletelyDeleteTable() throws SQLException {
cleanData(testNum);
}

@Test
public void testDeleteDataByTag() throws IoTDBConnectionException, StatementExecutionException {
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnectionWithDB("test")) {
session.executeNonQueryStatement(
"CREATE TABLE IF NOT EXISTS delete_by_tag (deviceId STRING TAG, s1 INT32 FIELD)");

session.executeNonQueryStatement(
"insert into delete_by_tag (time, deviceId, s1) values (1, 'sensor', 1)");
session.executeNonQueryStatement(
"insert into delete_by_tag (time, deviceId, s1) values (2, 'sensor', 2)");
session.executeNonQueryStatement(
"insert into delete_by_tag (time, deviceId, s1) values (3, 'sensor', 3)");
session.executeNonQueryStatement(
"insert into delete_by_tag (time, deviceId, s1) values (4, 'sensor', 4)");

session.executeNonQueryStatement("DELETE FROM delete_by_tag WHERE deviceId = 'sensor'");

SessionDataSet dataSet =
session.executeQueryStatement("select * from delete_by_tag order by time");
assertFalse(dataSet.hasNext());

session.executeNonQueryStatement(
"insert into delete_by_tag (time, deviceId, s1) values (1, 'sensor', 1)");
session.executeNonQueryStatement(
"insert into delete_by_tag (time, deviceId, s1) values (2, 'sensor', 2)");
session.executeNonQueryStatement(
"insert into delete_by_tag (time, deviceId, s1) values (3, 'sensor', 3)");
session.executeNonQueryStatement(
"insert into delete_by_tag (time, deviceId, s1) values (4, 'sensor', 4)");
session.executeNonQueryStatement("FLUSH");

session.executeNonQueryStatement("DELETE FROM delete_by_tag WHERE deviceId = 'sensor'");

dataSet = session.executeQueryStatement("select * from delete_by_tag order by time");
assertFalse(dataSet.hasNext());
} finally {
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnectionWithDB("test")) {
session.executeNonQueryStatement("DROP TABLE IF EXISTS delete_by_tag");
}
}
}

@Test
public void testDropAndAlter() throws IoTDBConnectionException, StatementExecutionException {
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnectionWithDB("test")) {
session.executeNonQueryStatement("CREATE TABLE IF NOT EXISTS drop_and_alter (s1 int32)");

// time=1 and time=2 are INT32 and deleted by drop column
Tablet tablet =
new Tablet(
"drop_and_alter",
Collections.singletonList("s1"),
Collections.singletonList(TSDataType.INT32),
Collections.singletonList(ColumnCategory.FIELD));
tablet.addTimestamp(0, 1);
tablet.addValue("s1", 0, genValue(TSDataType.INT32, 1));
session.insert(tablet);
tablet.reset();

session.executeNonQueryStatement("FLUSH");

tablet =
new Tablet(
"drop_and_alter",
Collections.singletonList("s1"),
Collections.singletonList(TSDataType.INT32),
Collections.singletonList(ColumnCategory.FIELD));
tablet.addTimestamp(0, 2);
tablet.addValue("s1", 0, genValue(TSDataType.INT32, 2));
session.insert(tablet);
tablet.reset();

session.executeNonQueryStatement("ALTER TABLE drop_and_alter DROP COLUMN s1");

// time=3 and time=4 are STRING
tablet =
new Tablet(
"drop_and_alter",
Collections.singletonList("s1"),
Collections.singletonList(TSDataType.STRING),
Collections.singletonList(ColumnCategory.FIELD));
tablet.addTimestamp(0, 3);
tablet.addValue("s1", 0, genValue(TSDataType.STRING, 3));
session.insert(tablet);
tablet.reset();

session.executeNonQueryStatement("FLUSH");

tablet =
new Tablet(
"drop_and_alter",
Collections.singletonList("s1"),
Collections.singletonList(TSDataType.STRING),
Collections.singletonList(ColumnCategory.FIELD));
tablet.addTimestamp(0, 4);
tablet.addValue("s1", 0, genValue(TSDataType.STRING, 4));
session.insert(tablet);
tablet.reset();

session.executeNonQueryStatement("ALTER TABLE drop_and_alter DROP COLUMN s1");
session.executeNonQueryStatement("ALTER TABLE drop_and_alter ADD COLUMN s1 TEXT");

// time=5 and time=6 are TEXT
tablet =
new Tablet(
"drop_and_alter",
Collections.singletonList("s1"),
Collections.singletonList(TSDataType.TEXT),
Collections.singletonList(ColumnCategory.FIELD));
tablet.addTimestamp(0, 5);
tablet.addValue("s1", 0, genValue(TSDataType.STRING, 5));
session.insert(tablet);
tablet.reset();

session.executeNonQueryStatement("FLUSH");

tablet =
new Tablet(
"drop_and_alter",
Collections.singletonList("s1"),
Collections.singletonList(TSDataType.TEXT),
Collections.singletonList(ColumnCategory.FIELD));
tablet.addTimestamp(0, 6);
tablet.addValue("s1", 0, genValue(TSDataType.STRING, 6));
session.insert(tablet);
tablet.reset();

SessionDataSet dataSet =
session.executeQueryStatement("select * from drop_and_alter order by time");
// s1 is dropped but the time should remain
RowRecord rec;
int cnt = 0;
for (int i = 1; i < 7; i++) {
rec = dataSet.next();
assertEquals(i, rec.getFields().get(0).getLongV());
LOGGER.error(
"time is {}, value is {}, value type is {}",
rec.getFields().get(0).getLongV(),
rec.getFields().get(1),
rec.getFields().get(1).getDataType());
// assertNull(rec.getFields().get(1).getDataType());
// Assert.assertEquals(TSDataType.TEXT, rec.getFields().get(1).getDataType());
cnt++;
}
Assert.assertEquals(6, cnt);
assertFalse(dataSet.hasNext());
} finally {
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnectionWithDB("test")) {
session.executeNonQueryStatement("DROP TABLE IF EXISTS drop_and_alter");
}
}
}

private static void prepareDatabase() {
try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
Statement statement = connection.createStatement()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,14 @@ public ITimeSeriesMetadata generateTimeSeriesMetadata(
boolean isModified = false;
for (IChunkMetadata chunkMetadata : chunkMetadataList) {
isModified = (isModified || chunkMetadata.isModified());
TSDataType targetDataType = fullPath.getMeasurementSchema().getType();
if (targetDataType.equals(TSDataType.STRING)
&& (chunkMetadata.getDataType() != targetDataType)) {
// create new statistics object via new data type, and merge statistics information
SchemaUtils.rewriteNonAlignedChunkMetadataStatistics(
(ChunkMetadata) chunkMetadata, targetDataType);
chunkMetadata.setModified(true);
}
if (!useFakeStatistics) {
seriesStatistics.mergeStatistics(chunkMetadata.getStatistics());
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import org.apache.iotdb.db.storageengine.dataregion.memtable.IMemTable;
import org.apache.iotdb.db.storageengine.dataregion.memtable.TsFileProcessor;
import org.apache.iotdb.db.storageengine.dataregion.memtable.TsFileProcessorInfo;
import org.apache.iotdb.db.storageengine.dataregion.modification.IDPredicate;
import org.apache.iotdb.db.storageengine.dataregion.modification.ModEntry;
import org.apache.iotdb.db.storageengine.dataregion.modification.ModificationFile;
import org.apache.iotdb.db.storageengine.dataregion.modification.TableDeletionEntry;
Expand Down Expand Up @@ -3235,18 +3236,14 @@ private void deleteDataInSealedFiles(Collection<TsFileResource> sealedTsFiles, M
List<TsFileResource> deletedByMods = new ArrayList<>();
List<TsFileResource> deletedByFiles = new ArrayList<>();
boolean isDropMeasurementExist = false;
boolean isDropTagExist = false;
IDPredicate.IDPredicateType idPredicateType = null;

if (deletion instanceof TableDeletionEntry) {
TableDeletionEntry entry = (TableDeletionEntry) deletion;
isDropMeasurementExist = !entry.getPredicate().getMeasurementNames().isEmpty();
} else {
TreeDeletionEntry entry = (TreeDeletionEntry) deletion;
if (entry.getPathPattern() instanceof MeasurementPath) {
Map<String, String> tagMap = ((MeasurementPath) entry.getPathPattern()).getTagMap();
isDropTagExist = (tagMap != null) && !tagMap.isEmpty();
}
TableDeletionEntry tableDeletionEntry = (TableDeletionEntry) deletion;
isDropMeasurementExist = !tableDeletionEntry.getPredicate().getMeasurementNames().isEmpty();
idPredicateType = tableDeletionEntry.getPredicate().getIdPredicateType();
}

for (TsFileResource sealedTsFile : sealedTsFiles) {
if (canSkipDelete(sealedTsFile, deletion)) {
continue;
Expand Down Expand Up @@ -3310,14 +3307,15 @@ private void deleteDataInSealedFiles(Collection<TsFileResource> sealedTsFiles, M
fileStartTime,
fileEndTime);
}
if (isFileFullyMatchedByTime(deletion, fileStartTime, fileEndTime)) {
if (isFileFullyMatchedByTime(deletion, fileStartTime, fileEndTime)
&& idPredicateType.equals(IDPredicate.IDPredicateType.NOP)) {
++matchSize;
} else {
deletedByMods.add(sealedTsFile);
break;
}
}
if (matchSize == devicesInFile.size()) {
if (matchSize == devicesInFile.size() && !isDropMeasurementExist) {
deletedByFiles.add(sealedTsFile);
}

Expand All @@ -3343,7 +3341,7 @@ private void deleteDataInSealedFiles(Collection<TsFileResource> sealedTsFiles, M
} // else do nothing
}

if (!deletedByFiles.isEmpty() && !isDropMeasurementExist && !isDropTagExist) {
if (!deletedByFiles.isEmpty()) {
deleteTsFileCompletely(deletedByFiles);
if (logger.isDebugEnabled()) {
logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public IDPredicate getIdPredicate() {
return idPredicate;
}

public IDPredicate.IDPredicateType getIdPredicateType() {
return this.idPredicate.type;
}

public String getTableName() {
return tableName;
}
Expand Down
Loading
Loading