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:24:03 UTC

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

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


##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/process/SlidingWindowAggregationOperator.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.execution.operator.process;
+
+import org.apache.iotdb.db.mpp.aggregation.Aggregator;
+import org.apache.iotdb.db.mpp.aggregation.slidingwindow.SlidingWindowAggregator;
+import org.apache.iotdb.db.mpp.aggregation.timerangeiterator.ITimeRangeIterator;
+import org.apache.iotdb.db.mpp.execution.operator.Operator;
+import org.apache.iotdb.db.mpp.execution.operator.OperatorContext;
+import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.GroupByTimeParameter;
+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 com.google.common.util.concurrent.ListenableFuture;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.iotdb.db.mpp.execution.operator.process.AggregationOperator.updateResultTsBlockFromAggregators;
+import static org.apache.iotdb.db.mpp.execution.operator.process.RawDataAggregationOperator.satisfied;
+import static org.apache.iotdb.db.mpp.execution.operator.process.RawDataAggregationOperator.skipOutOfTimeRangePoints;
+import static org.apache.iotdb.db.mpp.execution.operator.source.SeriesAggregationScanOperator.initTimeRangeIterator;
+
+public class SlidingWindowAggregationOperator implements ProcessOperator {
+
+  private final OperatorContext operatorContext;
+  private final Operator child;
+
+  private TsBlock cachedTsBlock;
+
+  private final List<SlidingWindowAggregator> aggregators;
+
+  private final ITimeRangeIterator timeRangeIterator;
+
+  private final boolean ascending;
+
+  private final TsBlockBuilder tsBlockBuilder;
+
+  public SlidingWindowAggregationOperator(
+      OperatorContext operatorContext,
+      List<SlidingWindowAggregator> aggregators,
+      Operator child,
+      boolean ascending,
+      GroupByTimeParameter groupByTimeParameter) {
+    checkArgument(
+        groupByTimeParameter != null,
+        "GroupByTimeParameter cannot be null in SlidingWindowAggregationOperator");
+
+    this.operatorContext = operatorContext;
+    this.aggregators = aggregators;
+    this.child = child;
+    List<TSDataType> outputDataTypes = new ArrayList<>();
+    for (Aggregator aggregator : aggregators) {
+      outputDataTypes.addAll(Arrays.asList(aggregator.getOutputType()));
+    }
+    this.tsBlockBuilder = new TsBlockBuilder(outputDataTypes);
+    this.timeRangeIterator = initTimeRangeIterator(groupByTimeParameter, ascending, false);
+    this.ascending = ascending;
+  }
+
+  @Override
+  public boolean hasNext() {
+    return timeRangeIterator.hasNextTimeRange();
+  }
+
+  @Override
+  public TsBlock next() {
+    // 1. Clear previous aggregation result
+    TimeRange curTimeRange = timeRangeIterator.nextTimeRange();
+    for (SlidingWindowAggregator aggregator : aggregators) {
+      aggregator.updateTimeRange(curTimeRange);
+    }
+
+    // 2. Calculate aggregation result based on current time window
+    TsBlock inputTsBlock = cachedTsBlock;
+    while (!calcFromTsBlock(inputTsBlock, curTimeRange)) {
+      if (child.hasNext()) {
+        inputTsBlock = child.next();
+      } else {
+        break;
+      }
+    }
+    if (inputTsBlock != null && !satisfied(inputTsBlock, curTimeRange, ascending)) {
+      cachedTsBlock = inputTsBlock;

Review Comment:
   ```suggestion
       if (inputTsBlock != null && satisfied(inputTsBlock, curTimeRange, ascending)) {
         cachedTsBlock = inputTsBlock;
   ```



##########
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:
   ```suggestion
       if (!deque.isEmpty() && !curTimeRange.contains(deque.getLast().getTime())) {
         this.accumulator.reset();
         return;
       }
       while (!deque.isEmpty() && !curTimeRange.contains(deque.getFirst().getTime())) {
         PartialAggregationResult partialResult = deque.removeFirst();
         this.accumulator.addIntermediate(partialResult.opposite());
       }
   ```



##########
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:
   Is it necessary to update the value here?



##########
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:
   Maybe it should be renamed to `partialResultColumns`. When I read the code, I always thought the valueColumns means the value of origin time series.



-- 
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