@Test
  public void queryWithDeletedDeviceByTTL()
      throws IOException, MetadataException, WriteProcessException, QueryProcessException {
    createFiles(5, 5, 10, 50, 0, 0, 0, 0, false, true);
    createFiles(5, 5, 10, 50, 1707137815000L, 0, 0, 0, false, true);
    createFiles(5, 6, 10, 100, 0, 10000, 0, 50, false, false);
    createFiles(5, 6, 10, 100, 1707137815000L, 10000, 0, 50, false, false);

    DataRegion dataRegion = new DataRegion(COMPACTION_TEST_SG, "0");
    dataRegion.getTsFileManager().addAll(seqResources, true);
    dataRegion.getTsFileManager().addAll(unseqResources, false);
    tsFileManager = dataRegion.getTsFileManager();

    List<IFullPath> pathList = new ArrayList<>();
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d1"),
            new MeasurementSchema("s0", getDataType(0))));
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d3"),
            new MeasurementSchema("s1", getDataType(1))));
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d5"),
            new MeasurementSchema("s2", getDataType(2))));

    QueryDataSource queryDataSource =
        dataRegion.query(pathList, null, EnvironmentUtils.TEST_QUERY_CONTEXT, null, null);
    Assert.assertEquals(10, queryDataSource.getSeqResources().size());
    Assert.assertEquals(10, queryDataSource.getUnseqResources().size());
    Map<IFullPath, List<TimeValuePair>> sourceDatas =
        readSourceFiles(createTimeseries(6, 10, false), Collections.emptyList());
    Assert.assertEquals(1000, sourceDatas.get(pathList.get(0)).size());
    Assert.assertEquals(1000, sourceDatas.get(pathList.get(1)).size());
    Assert.assertEquals(1000, sourceDatas.get(pathList.get(2)).size());
    Assert.assertTrue(sourceDatas.get(pathList.get(0)).get(0).getTimestamp() < 1707137815000L);
    Assert.assertTrue(sourceDatas.get(pathList.get(1)).get(0).getTimestamp() < 1707137815000L);
    Assert.assertTrue(sourceDatas.get(pathList.get(2)).get(0).getTimestamp() < 1707137815000L);

    // set ttl
    DataNodeTTLCache.getInstance()
        .setTTLForTree(COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d1", 315360000000L);
    DataNodeTTLCache.getInstance()
        .setTTLForTree(COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d3", 315360000000L);
    DataNodeTTLCache.getInstance()
        .setTTLForTree(COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d5", 315360000000L);

    queryDataSource =
        dataRegion.query(pathList, null, EnvironmentUtils.TEST_QUERY_CONTEXT, null, null);
    Assert.assertEquals(10, queryDataSource.getSeqResources().size());
    Assert.assertEquals(10, queryDataSource.getUnseqResources().size());
    sourceDatas = readSourceFiles(createTimeseries(6, 10, false), Collections.emptyList());
    Assert.assertEquals(500, sourceDatas.get(pathList.get(0)).size());
    Assert.assertEquals(500, sourceDatas.get(pathList.get(1)).size());
    Assert.assertEquals(500, sourceDatas.get(pathList.get(2)).size());
    Assert.assertTrue(sourceDatas.get(pathList.get(0)).get(0).getTimestamp() >= 1707137815000L);
    Assert.assertTrue(sourceDatas.get(pathList.get(1)).get(0).getTimestamp() >= 1707137815000L);
    Assert.assertTrue(sourceDatas.get(pathList.get(2)).get(0).getTimestamp() >= 1707137815000L);

    pathList.clear();
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d5"),
            new MeasurementSchema("s1", getDataType(1))));
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d5"),
            new MeasurementSchema("s2", getDataType(2))));
    queryDataSource =
        dataRegion.query(
            pathList,
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d5"),
            EnvironmentUtils.TEST_QUERY_CONTEXT,
            null,
            null);
    Assert.assertEquals(0, queryDataSource.getSeqResources().size());
    Assert.assertEquals(10, queryDataSource.getUnseqResources().size());

    // reset ttl to 1
    pathList.clear();
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d1"),
            new MeasurementSchema("s0", getDataType(0))));
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d3"),
            new MeasurementSchema("s1", getDataType(1))));
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d5"),
            new MeasurementSchema("s2", getDataType(2))));
    pathList.add(
        new NonAlignedFullPath(
            IDeviceID.Factory.DEFAULT_FACTORY.create(
                COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d0"),
            new MeasurementSchema("s2", getDataType(2))));

    DataNodeTTLCache.getInstance()
        .setTTLForTree(COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d1", 1L);
    DataNodeTTLCache.getInstance()
        .setTTLForTree(COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d3", 1);
    DataNodeTTLCache.getInstance()
        .setTTLForTree(COMPACTION_TEST_SG + IoTDBConstant.PATH_SEPARATOR + "d5", 1);
    queryDataSource =
        dataRegion.query(pathList, null, EnvironmentUtils.TEST_QUERY_CONTEXT, null, null);
    Assert.assertEquals(10, queryDataSource.getSeqResources().size());
    Assert.assertEquals(10, queryDataSource.getUnseqResources().size());
    sourceDatas = readSourceFiles(createTimeseries(6, 10, false), Collections.emptyList());
    Assert.assertEquals(0, sourceDatas.get(pathList.get(0)).size());
    Assert.assertEquals(0, sourceDatas.get(pathList.get(1)).size());
    Assert.assertEquals(0, sourceDatas.get(pathList.get(2)).size());
    Assert.assertEquals(1000, sourceDatas.get(pathList.get(3)).size());
  }
Device d1, d3 and d5 is deleted by TTL.
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/TTLQueryTest.java