CRZbulabula commented on a change in pull request #4632:
URL: https://github.com/apache/iotdb/pull/4632#discussion_r775253093



##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,333 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import 
org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+  private GroupByEngineDataSet dataSet;
+
+  private final Map<TSDataType, IFill> fillTypes;
+  private final IFill singleFill;
+  private final List<String> aggregations;
+
+  // the result datatype for each time series
+  private final TSDataType[] resultDataType;
+
+  // the last value and time for each time series
+  private long[] previousTimes;
+  private Object[] previousValues;
+
+  // the next not null and unused rowId for each time series
+  private int[] nextIds;
+  // the next value and time for each time series
+  private List<ElasticSerializableTVList> nextTVLists;
+
+  public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan 
groupByTimeFillPlan)
+      throws QueryProcessException {
+    super(context, groupByTimeFillPlan);
+    this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+    this.fillTypes = groupByTimeFillPlan.getFillType();
+    this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+    this.resultDataType = new TSDataType[aggregations.size()];
+    initArrays(context);
+  }
+
+  private void initArrays(QueryContext context) throws QueryProcessException {
+    for (int i = 0; i < aggregations.size(); i++) {
+      switch (aggregations.get(i)) {
+        case "avg":
+        case "sum":
+          resultDataType[i] = TSDataType.DOUBLE;
+          break;
+        case "count":
+        case "max_time":
+        case "min_time":
+          resultDataType[i] = TSDataType.INT64;
+          break;
+        case "first_value":
+        case "last_value":
+        case "max_value":
+        case "min_value":
+          resultDataType[i] = dataTypes.get(i);
+          break;
+        default:
+          throw new QueryProcessException("unknown aggregation type, please 
update this code!");
+      }
+    }
+
+    previousTimes = new long[aggregations.size()];
+    previousValues = new Object[aggregations.size()];
+    nextIds = new int[aggregations.size()];
+    Arrays.fill(previousTimes, Long.MAX_VALUE);
+    Arrays.fill(previousValues, null);
+    Arrays.fill(nextIds, 0);
+
+    nextTVLists = new ArrayList<>();
+    for (int i = 0; i < aggregations.size(); i++) {
+      nextTVLists.add(
+          ElasticSerializableTVList.newElasticSerializableTVList(
+              resultDataType[i], context.getQueryId(), 10, 2));
+    }
+  }
+
+  public void setDataSet(GroupByEngineDataSet dataSet) {
+    this.dataSet = dataSet;
+  }
+
+  public void initCache() throws IOException, QueryProcessException {
+    BitSet cacheSet = new BitSet(aggregations.size());
+    cacheSet.clear();
+    while (cacheSet.cardinality() < aggregations.size() && 
dataSet.hasNextWithoutConstraint()) {
+      RowRecord record = dataSet.nextWithoutConstraint();
+      long timestamp = record.getTimestamp();
+      List<Field> fields = record.getFields();
+      for (int i = 0; i < fields.size(); i++) {
+        Field field = fields.get(i);
+        if (field == null) {
+          continue;
+        }
+
+        if (ascending && timestamp < startTime) {
+          previousTimes[i] = timestamp;
+          previousValues[i] = field.getObjectValue(resultDataType[i]);
+        } else if (!ascending && timestamp >= endTime) {
+          previousTimes[i] = timestamp;
+          previousValues[i] = field.getObjectValue(resultDataType[i]);
+        } else {
+          nextTVLists.get(i).put(timestamp, 
field.getObjectValue(resultDataType[i]));
+          cacheSet.set(i);
+        }
+      }
+    }
+  }
+
+  @Override
+  public RowRecord nextWithoutConstraint() throws IOException {
+    if (!hasCachedTimeInterval) {
+      throw new IOException(
+          "need to call hasNext() before calling next() " + "in 
GroupByFillDataSet.");
+    }
+
+    hasCachedTimeInterval = false;
+    RowRecord record;
+    long curTimestamp;
+    if (leftCRightO) {
+      curTimestamp = curStartTime;
+      record = new RowRecord(curStartTime);
+    } else {
+      curTimestamp = curEndTime - 1;
+      record = new RowRecord(curEndTime - 1);
+    }
+
+    for (int i = 0; i < aggregations.size(); i++) {
+      if (nextTVLists.get(i).size() == nextIds[i]) {
+        fillRecord(i, record);
+        continue;
+      }
+
+      long cacheTime = nextTVLists.get(i).getTime(nextIds[i]);
+      if (cacheTime == curTimestamp) {
+        record.addField(getNextCacheValue(i), resultDataType[i]);
+      } else {
+        fillRecord(i, record);
+      }
+    }
+
+    try {
+      slideCache(record.getTimestamp());
+    } catch (QueryProcessException e) {
+      // ignored
+    }
+
+    return record;
+  }
+
+  private void fillRecord(int index, RowRecord record) throws IOException {
+    // Don't fill count aggregation
+    if (Objects.equals(aggregations.get(index), "count")) {

Review comment:
       Yes, it is. I'll remove it tomorrow. And for `sum` aggregation, `0` and 
`null` have different meanings. The former can be a sum of 0 in the time range, 
while the latter means that there is no data in the time range. So currently, 
we distinguish `0` and `null` in `sum` aggregation, and we fill `null` in 
`sum`. You can see this on 
[IoTDB-1884](https://github.com/apache/iotdb/pull/4233).




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to