3535import org .apache .iotdb .db .storageengine .dataregion .read .reader .chunk .MemPageReader ;
3636import org .apache .iotdb .db .storageengine .dataregion .read .reader .common .DescPriorityMergeReader ;
3737import org .apache .iotdb .db .storageengine .dataregion .read .reader .common .MergeReaderPriority ;
38+ import org .apache .iotdb .db .storageengine .dataregion .read .reader .common .NoDataPointReader ;
3839import org .apache .iotdb .db .storageengine .dataregion .read .reader .common .PriorityMergeReader ;
3940import org .apache .iotdb .db .storageengine .dataregion .tsfile .TsFileResource ;
4041import org .apache .iotdb .db .utils .datastructure .MemPointIterator ;
@@ -134,7 +135,10 @@ public class SeriesScanUtil implements Accountable {
134135 + RamUsageEstimator .shallowSizeOfInstance (IDeviceID .class )
135136 + RamUsageEstimator .shallowSizeOfInstance (TimeOrderUtils .class )
136137 + RamUsageEstimator .shallowSizeOfInstance (PaginationController .class )
137- + RamUsageEstimator .shallowSizeOfInstance (SeriesScanOptions .class );
138+ + RamUsageEstimator .shallowSizeOfInstance (SeriesScanOptions .class )
139+ + RamUsageEstimator .shallowSizeOfInstance (TimeRange .class );
140+
141+ protected TimeRange satisfiedTimeRange ;
138142
139143 public SeriesScanUtil (
140144 IFullPath seriesPath ,
@@ -214,6 +218,20 @@ public void initQueryDataSource(QueryDataSource dataSource) {
214218 // init file index
215219 orderUtils .setCurSeqFileIndex (dataSource );
216220 curUnseqFileIndex = 0 ;
221+
222+ if (satisfiedTimeRange == null ) {
223+ long startTime = Long .MAX_VALUE ;
224+ long endTime = Long .MIN_VALUE ;
225+ if (scanOptions .getGlobalTimeFilter () == null ) {
226+ satisfiedTimeRange = new TimeRange (Long .MIN_VALUE , Long .MAX_VALUE );
227+ return ;
228+ }
229+ for (TimeRange timeRange : context .getGlobalTimeFilterTimeRanges ()) {
230+ startTime = Math .min (startTime , timeRange .getMin ());
231+ endTime = Math .max (endTime , timeRange .getMax ());
232+ }
233+ satisfiedTimeRange = new TimeRange (startTime , endTime );
234+ }
217235 }
218236
219237 protected PriorityMergeReader getPriorityMergeReader () {
@@ -681,6 +699,13 @@ private void unpackOneFakeMemChunkMetaData(
681699 readOnlyMemChunk .createMemPointIterator (
682700 orderUtils .getScanOrder (), scanOptions .getGlobalTimeFilter ());
683701 for (Statistics <? extends Serializable > statistics : statisticsList ) {
702+ long orderTime = orderUtils .getOrderTime (statistics );
703+ boolean canSkip =
704+ (orderUtils .getAscending () && orderTime > satisfiedTimeRange .getMax ())
705+ || (!orderUtils .getAscending () && orderTime < satisfiedTimeRange .getMin ());
706+ if (canSkip ) {
707+ break ;
708+ }
684709 IVersionPageReader versionPageReader =
685710 new LazyMemVersionPageReader (
686711 context ,
@@ -1458,6 +1483,7 @@ protected static class LazyMemVersionPageReader implements IVersionPageReader {
14581483 protected final boolean isSeq ;
14591484 protected final boolean isAligned ;
14601485 private boolean inited = false ;
1486+ private boolean hasData = true ;
14611487
14621488 LazyMemVersionPageReader (
14631489 QueryContext context ,
@@ -1477,11 +1503,14 @@ protected static class LazyMemVersionPageReader implements IVersionPageReader {
14771503 }
14781504
14791505 public IPointReader getPointReader () {
1506+ if (!hasData ) {
1507+ return NoDataPointReader .getInstance ();
1508+ }
14801509 return memPointIterator ;
14811510 }
14821511
14831512 public boolean hasNextBatch () {
1484- return memPointIterator .hasNextBatch ();
1513+ return hasData && memPointIterator .hasNextBatch ();
14851514 }
14861515
14871516 public void setCurrentPageTimeRangeToMemPointIterator () {
@@ -1490,10 +1519,34 @@ public void setCurrentPageTimeRangeToMemPointIterator() {
14901519 }
14911520 if (statistics .getStartTime () > statistics .getEndTime ()) {
14921521 // empty
1522+ hasData = false ;
14931523 return ;
14941524 }
1495- this .memPointIterator .setCurrentPageTimeRange (
1496- new TimeRange (statistics .getStartTime (), statistics .getEndTime ()));
1525+ Filter globalTimeFilter = ((FragmentInstanceContext ) context ).getGlobalTimeFilter ();
1526+ if (globalTimeFilter == null ) {
1527+ this .memPointIterator .setCurrentPageTimeRange (
1528+ new TimeRange (statistics .getStartTime (), statistics .getEndTime ()));
1529+ return ;
1530+ }
1531+
1532+ long startTime = statistics .getStartTime ();
1533+ long endTime = statistics .getEndTime ();
1534+ long minStart = Long .MAX_VALUE ;
1535+ long maxEnd = Long .MIN_VALUE ;
1536+ for (TimeRange timeRange :
1537+ ((FragmentInstanceContext ) context ).getGlobalTimeFilterTimeRanges ()) {
1538+ if (timeRange .overlaps (new TimeRange (startTime , endTime ))) {
1539+ minStart = Math .min (minStart , Math .max (timeRange .getMin (), startTime ));
1540+ maxEnd = Math .max (maxEnd , Math .min (timeRange .getMax (), endTime ));
1541+ }
1542+ }
1543+
1544+ if (minStart > maxEnd ) {
1545+ hasData = false ;
1546+ return ;
1547+ }
1548+
1549+ this .memPointIterator .setCurrentPageTimeRange (new TimeRange (minStart , maxEnd ));
14971550 }
14981551
14991552 public TsBlock nextBatch () {
@@ -1629,7 +1682,7 @@ public long getOrderTime(Statistics statistics) {
16291682 @ SuppressWarnings ("squid:S3740" )
16301683 @ Override
16311684 public long getOverlapCheckTime (Statistics range ) {
1632- return range .getStartTime ();
1685+ return Math . max ( satisfiedTimeRange . getMin (), range .getStartTime () );
16331686 }
16341687
16351688 @ SuppressWarnings ("squid:S3740" )
@@ -1758,7 +1811,7 @@ public long getOrderTime(Statistics statistics) {
17581811 @ SuppressWarnings ("squid:S3740" )
17591812 @ Override
17601813 public long getOverlapCheckTime (Statistics range ) {
1761- return range .getEndTime ();
1814+ return Math . min ( satisfiedTimeRange . getMax (), range .getEndTime () );
17621815 }
17631816
17641817 @ SuppressWarnings ("squid:S3740" )
0 commit comments