You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2022/05/26 08:48:11 UTC

[GitHub] [iotdb] liuminghui233 commented on a diff in pull request #5986: [IOTDB-3081] Implementation of SlidingWindowAggregationOperator

liuminghui233 commented on code in PR #5986:
URL: https://github.com/apache/iotdb/pull/5986#discussion_r882457164


##########
server/src/main/java/org/apache/iotdb/db/mpp/aggregation/slidingwindow/SlidingWindowAggregator.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.mpp.aggregation.slidingwindow;
+
+import org.apache.iotdb.db.mpp.aggregation.Accumulator;
+import org.apache.iotdb.db.mpp.aggregation.Aggregator;
+import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.AggregationStep;
+import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.InputLocation;
+import org.apache.iotdb.tsfile.exception.write.UnSupportedDataTypeException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.TimeRange;
+import org.apache.iotdb.tsfile.read.common.block.TsBlock;
+import org.apache.iotdb.tsfile.read.common.block.TsBlockBuilder;
+import org.apache.iotdb.tsfile.read.common.block.column.Column;
+import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder;
+import org.apache.iotdb.tsfile.read.common.block.column.TimeColumn;
+
+import java.util.Arrays;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+public abstract class SlidingWindowAggregator extends Aggregator {
+
+  // cached partial aggregation result of pre-aggregate windows
+  protected Deque<PartialAggregationResult> deque;
+
+  public SlidingWindowAggregator(
+      Accumulator accumulator, List<InputLocation[]> inputLocationList, AggregationStep step) {
+    super(accumulator, step, inputLocationList);
+    this.deque = new LinkedList<>();
+  }
+
+  @Override
+  public void processTsBlock(TsBlock tsBlock) {
+    checkArgument(
+        step.isInputPartial(),
+        "Step in SlidingWindowAggregationOperator can only process partial result");
+    TimeColumn timeColumn = tsBlock.getTimeColumn();
+    Column[] valueColumn = new Column[inputLocationList.get(0).length];
+    for (int i = 0; i < inputLocationList.get(0).length; i++) {
+      InputLocation inputLocation = inputLocationList.get(0)[i];
+      checkArgument(
+          inputLocation.getTsBlockIndex() == 0,
+          "SlidingWindowAggregationOperator can only process one tsBlock input.");
+      valueColumn[i] = tsBlock.getColumn(inputLocation.getValueColumnIndex());
+    }
+    processPartialResult(new PartialAggregationResult(timeColumn, valueColumn));
+  }
+
+  @Override
+  public void updateTimeRange(TimeRange curTimeRange) {
+    this.curTimeRange = curTimeRange;
+    evictingExpiredValue();
+  }
+
+  /** evicting expired element in queue and reset expired aggregateResult */
+  protected abstract void evictingExpiredValue();
+
+  /** update queue and aggregateResult */
+  public abstract void processPartialResult(PartialAggregationResult partialResult);
+
+  protected static class PartialAggregationResult {
+
+    private final TimeColumn timeColumn;
+    private final Column[] valueColumns;

Review Comment:
   fixed



##########
server/src/main/java/org/apache/iotdb/db/mpp/aggregation/slidingwindow/NormalQueueSlidingWindowAggregator.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.mpp.aggregation.slidingwindow;
+
+import org.apache.iotdb.db.mpp.aggregation.Accumulator;
+import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.AggregationStep;
+import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.InputLocation;
+
+import java.util.List;
+
+/**
+ * When calculating MIN_TIME and FIRST_VALUE (MAX_TIME and LAST_VALUE in descending order), the
+ * aggregated result always appears at the head of the queue. We need to cache all pre-aggregated
+ * results in the queue.
+ */
+public class NormalQueueSlidingWindowAggregator extends SlidingWindowAggregator {
+
+  public NormalQueueSlidingWindowAggregator(
+      Accumulator accumulator, List<InputLocation[]> inputLocationList, AggregationStep step) {
+    super(accumulator, inputLocationList, step);
+  }
+
+  @Override
+  protected void evictingExpiredValue() {
+    while (!deque.isEmpty() && !curTimeRange.contains(deque.getFirst().getTime())) {
+      deque.removeFirst();
+    }
+    this.accumulator.reset();
+    if (!deque.isEmpty()) {
+      this.accumulator.addIntermediate(deque.getFirst().getPartialResult());
+    }

Review Comment:
   fixed



##########
server/src/main/java/org/apache/iotdb/db/mpp/aggregation/slidingwindow/SmoothQueueSlidingWindowAggregator.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.mpp.aggregation.slidingwindow;
+
+import org.apache.iotdb.db.mpp.aggregation.Accumulator;
+import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.AggregationStep;
+import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.InputLocation;
+
+import java.util.List;
+
+/**
+ * The aggregation result is calculated from all pre-aggregation results in the currently maintained
+ * queue when calculating the COUNT, SUM, and AVG.
+ */
+public class SmoothQueueSlidingWindowAggregator extends SlidingWindowAggregator {
+  public SmoothQueueSlidingWindowAggregator(
+      Accumulator accumulator, List<InputLocation[]> inputLocationList, AggregationStep step) {
+    super(accumulator, inputLocationList, step);
+  }
+
+  @Override
+  protected void evictingExpiredValue() {
+    while (!deque.isEmpty() && !curTimeRange.contains(deque.getFirst().getTime())) {
+      PartialAggregationResult partialResult = deque.removeFirst();
+      if (deque.isEmpty()) {
+        this.accumulator.reset();
+      } else {
+        this.accumulator.addIntermediate(partialResult.opposite());
+      }
+    }
+  }

Review Comment:
   fixed



-- 
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-unsubscribe@iotdb.apache.org

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