• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #9629

pending completion
#9629

push

travis_ci

web-flow
Add executeGroupByQueryIntervalQuery rpc interface (#10571)

260 of 260 new or added lines in 6 files covered. (100.0%)

79015 of 165199 relevant lines covered (47.83%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.32
/iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/read/filter/TimeFilter.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.iotdb.tsfile.read.filter;
21

22
import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics;
23
import org.apache.iotdb.tsfile.read.common.TimeRange;
24
import org.apache.iotdb.tsfile.read.filter.basic.Filter;
25
import org.apache.iotdb.tsfile.read.filter.factory.FilterSerializeId;
26
import org.apache.iotdb.tsfile.read.filter.factory.FilterType;
27
import org.apache.iotdb.tsfile.read.filter.operator.Between;
28
import org.apache.iotdb.tsfile.read.filter.operator.Eq;
29
import org.apache.iotdb.tsfile.read.filter.operator.Gt;
30
import org.apache.iotdb.tsfile.read.filter.operator.GtEq;
31
import org.apache.iotdb.tsfile.read.filter.operator.In;
32
import org.apache.iotdb.tsfile.read.filter.operator.Lt;
33
import org.apache.iotdb.tsfile.read.filter.operator.LtEq;
34
import org.apache.iotdb.tsfile.read.filter.operator.NotEq;
35
import org.apache.iotdb.tsfile.read.filter.operator.OrFilter;
36

37
import java.io.DataOutputStream;
38
import java.io.IOException;
39
import java.nio.ByteBuffer;
40
import java.util.ArrayList;
41
import java.util.Arrays;
42
import java.util.Collections;
43
import java.util.List;
44
import java.util.Set;
45
import java.util.stream.Collectors;
46

47
public class TimeFilter {
48

49
  private TimeFilter() {}
50

51
  public static TimeGt gt(long value) {
52
    return new TimeGt(value);
1✔
53
  }
54

55
  public static TimeGtEq gtEq(long value) {
56
    return new TimeGtEq(value);
1✔
57
  }
58

59
  public static TimeLt lt(long value) {
60
    return new TimeLt(value);
1✔
61
  }
62

63
  public static TimeLtEq ltEq(long value) {
64
    return new TimeLtEq(value);
1✔
65
  }
66

67
  public static TimeEq eq(long value) {
68
    return new TimeEq(value);
1✔
69
  }
70

71
  public static TimeNotEq notEq(long value) {
72
    return new TimeNotEq(value);
1✔
73
  }
74

75
  public static TimeBetween between(long value1, long value2) {
76
    return new TimeBetween(value1, value2, false);
1✔
77
  }
78

79
  public static TimeBetween notBetween(long value1, long value2) {
80
    return new TimeBetween(value1, value2, true);
1✔
81
  }
82

83
  public static TimeIn in(Set<Long> values) {
84
    return new TimeIn(values, false);
1✔
85
  }
86

87
  public static TimeIn notIn(Set<Long> values) {
88
    return new TimeIn(values, true);
1✔
89
  }
90

91
  public static class TimeGt extends Gt<Long> {
92

93
    private TimeGt(long value) {
94
      super(value, FilterType.TIME_FILTER);
1✔
95
    }
1✔
96

97
    @Override
98
    public List<TimeRange> getTimeRanges() {
99
      long left = value;
1✔
100
      if (left != Long.MAX_VALUE) {
1✔
101
        return Collections.singletonList(new TimeRange(left + 1, Long.MAX_VALUE));
1✔
102
      } else {
103
        return Collections.emptyList();
×
104
      }
105
    }
106
  }
107

108
  public static class TimeGtEq extends GtEq<Long> {
109

110
    private TimeGtEq(long value) {
111
      super(value, FilterType.TIME_FILTER);
1✔
112
    }
1✔
113

114
    @Override
115
    public List<TimeRange> getTimeRanges() {
116
      return Collections.singletonList(new TimeRange(value, Long.MAX_VALUE));
1✔
117
    }
118
  }
119

120
  public static class TimeLt extends Lt<Long> {
121

122
    private TimeLt(long value) {
123
      super(value, FilterType.TIME_FILTER);
1✔
124
    }
1✔
125

126
    @Override
127
    public List<TimeRange> getTimeRanges() {
128
      long right = value;
1✔
129
      if (right != Long.MIN_VALUE) {
1✔
130
        return Collections.singletonList(new TimeRange(Long.MIN_VALUE, right - 1));
1✔
131
      } else {
132
        return Collections.emptyList();
×
133
      }
134
    }
135
  }
136

137
  public static class TimeLtEq extends LtEq<Long> {
138

139
    private TimeLtEq(long value) {
140
      super(value, FilterType.TIME_FILTER);
1✔
141
    }
1✔
142

143
    @Override
144
    public List<TimeRange> getTimeRanges() {
145
      return Collections.singletonList(new TimeRange(Long.MIN_VALUE, value));
1✔
146
    }
147
  }
148

149
  public static class TimeEq extends Eq<Long> {
150

151
    private TimeEq(long value) {
152
      super(value, FilterType.TIME_FILTER);
1✔
153
    }
1✔
154

155
    @Override
156
    public List<TimeRange> getTimeRanges() {
157
      return Collections.singletonList(new TimeRange(value, value));
1✔
158
    }
159
  }
160

161
  public static class TimeNotEq extends NotEq<Long> {
162

163
    private TimeNotEq(long value) {
164
      super(value, FilterType.TIME_FILTER);
1✔
165
    }
1✔
166

167
    @Override
168
    public List<TimeRange> getTimeRanges() {
169
      long time = value;
1✔
170
      if (time == Long.MIN_VALUE) {
1✔
171
        return Collections.singletonList(new TimeRange(time + 1, Long.MAX_VALUE));
×
172
      } else if (time == Long.MAX_VALUE) {
1✔
173
        return Collections.singletonList(new TimeRange(Long.MIN_VALUE, time - 1));
×
174
      } else {
175
        return Arrays.asList(
1✔
176
            new TimeRange(Long.MIN_VALUE, time - 1), new TimeRange(time + 1, Long.MAX_VALUE));
177
      }
178
    }
179
  }
180

181
  public static class TimeBetween extends Between<Long> {
182

183
    private TimeBetween(long value1, long value2, boolean not) {
184
      super(value1, value2, FilterType.TIME_FILTER, not);
1✔
185
    }
1✔
186

187
    @Override
188
    public List<TimeRange> getTimeRanges() {
189
      long left = value1;
1✔
190
      long right = value2;
1✔
191
      if (not) {
1✔
192
        List<TimeRange> res = new ArrayList<>();
1✔
193
        if (left != Long.MIN_VALUE) {
1✔
194
          res.add(new TimeRange(Long.MIN_VALUE, left - 1));
1✔
195
        }
196
        if (right != Long.MAX_VALUE) {
1✔
197
          res.add(new TimeRange(right + 1, Long.MAX_VALUE));
1✔
198
        }
199
        return res;
1✔
200
      } else {
201
        return Collections.singletonList(new TimeRange(left, right));
1✔
202
      }
203
    }
204
  }
205

206
  public static class TimeIn extends In<Long> {
207

208
    private TimeIn(Set<Long> values, boolean not) {
209
      super(values, FilterType.TIME_FILTER, not);
1✔
210
    }
1✔
211

212
    @Override
213
    public List<TimeRange> getTimeRanges() {
214
      if (not) {
×
215
        return Collections.singletonList(new TimeRange(Long.MIN_VALUE, Long.MAX_VALUE));
×
216
      } else {
217
        return values.stream()
×
218
            .map(
×
219
                l -> {
220
                  long time = l;
×
221
                  return new TimeRange(time, time);
×
222
                })
223
            .collect(Collectors.toList());
×
224
      }
225
    }
226
  }
227

228
  /**
229
   * returns a default time filter by whether it's an ascending query.
230
   *
231
   * <p>If the data is read in descending order, we use the largest timestamp to set to the filter,
232
   * so the filter should be TimeLtEq. If the data is read in ascending order, we use the smallest
233
   * timestamp to set to the filter, so the filter should be TimeGtEq.
234
   */
235
  public static Filter defaultTimeFilter(boolean ascending) {
236
    return ascending ? TimeFilter.gtEq(Long.MIN_VALUE) : TimeFilter.ltEq(Long.MAX_VALUE);
×
237
  }
238

239
  public static class TimeGtEqAndLt implements Filter {
240

241
    private long startTime;
242

243
    private long endTime;
244

245
    public TimeGtEqAndLt() {}
×
246

247
    public TimeGtEqAndLt(long startTime, long endTime) {
×
248
      this.startTime = startTime;
×
249
      this.endTime = endTime;
×
250
    }
×
251

252
    @Override
253
    public boolean satisfy(Statistics statistics) {
254
      return !(statistics.getEndTime() < startTime || statistics.getStartTime() >= endTime);
×
255
    }
256

257
    @Override
258
    public boolean allSatisfy(Statistics statistics) {
259
      return startTime <= statistics.getStartTime() && statistics.getEndTime() < endTime;
×
260
    }
261

262
    @Override
263
    public boolean satisfy(long time, Object value) {
264
      return startTime <= time && time < endTime;
×
265
    }
266

267
    @Override
268
    public boolean satisfyStartEndTime(long startTime, long endTime) {
269
      return !(endTime < this.startTime || startTime >= this.endTime);
×
270
    }
271

272
    @Override
273
    public boolean containStartEndTime(long startTime, long endTime) {
274
      return this.startTime <= startTime && endTime < this.endTime;
×
275
    }
276

277
    @Override
278
    public Filter copy() {
279
      return new TimeGtEqAndLt(startTime, endTime);
×
280
    }
281

282
    @Override
283
    public String toString() {
284
      return "TimeGtEqAndLt{" + "startTime=" + startTime + ", endTime=" + endTime + '}';
×
285
    }
286

287
    @Override
288
    public void serialize(DataOutputStream outputStream) {
289
      try {
290
        outputStream.write(getSerializeId().ordinal());
×
291
        outputStream.writeLong(startTime);
×
292
        outputStream.writeLong(endTime);
×
293
      } catch (IOException ignored) {
×
294
        // ignored
295
      }
×
296
    }
×
297

298
    @Override
299
    public void deserialize(ByteBuffer buffer) {
300
      startTime = buffer.getLong();
×
301
      endTime = buffer.getLong();
×
302
    }
×
303

304
    @Override
305
    public FilterSerializeId getSerializeId() {
306
      return FilterSerializeId.TIME_GTEQ_AND_LT;
×
307
    }
308

309
    @Override
310
    public List<TimeRange> getTimeRanges() {
311
      return startTime >= endTime
×
312
          ? Collections.emptyList()
×
313
          : Collections.singletonList(new TimeRange(startTime, endTime - 1));
×
314
    }
315

316
    @Override
317
    public Filter reverse() {
318
      return new OrFilter(new TimeLt(startTime), new TimeGtEq(endTime));
×
319
    }
320
  }
321
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc