JackieTien97 commented on code in PR #13997: URL: https://github.com/apache/iotdb/pull/13997#discussion_r1830739694
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/grouped/StreamingAggregationOperator.java: ########## @@ -0,0 +1,270 @@ +/* + * 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.queryengine.execution.operator.source.relational.aggregation.grouped; + +import org.apache.iotdb.db.queryengine.execution.MemoryEstimationHelper; +import org.apache.iotdb.db.queryengine.execution.operator.AbstractOperator; +import org.apache.iotdb.db.queryengine.execution.operator.Operator; +import org.apache.iotdb.db.queryengine.execution.operator.OperatorContext; +import org.apache.iotdb.db.queryengine.execution.operator.source.relational.TableScanOperator; +import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.TableAggregator; +import org.apache.iotdb.db.queryengine.plan.relational.type.InternalTypeManager; + +import com.google.common.primitives.Ints; +import com.google.common.util.concurrent.ListenableFuture; +import org.apache.tsfile.block.column.Column; +import org.apache.tsfile.block.column.ColumnBuilder; +import org.apache.tsfile.read.common.block.TsBlock; +import org.apache.tsfile.read.common.block.TsBlockBuilder; +import org.apache.tsfile.read.common.block.column.RunLengthEncodedColumn; +import org.apache.tsfile.read.common.type.Type; +import org.apache.tsfile.utils.RamUsageEstimator; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; +import static org.apache.iotdb.db.queryengine.plan.relational.utils.TypeUtil.appendTo; +import static org.apache.iotdb.db.queryengine.plan.relational.utils.TypeUtil.rowNotDistinctFromRow; + +public class StreamingAggregationOperator extends AbstractOperator { + private static final long INSTANCE_SIZE = + RamUsageEstimator.shallowSizeOfInstance(StreamingAggregationOperator.class); + + private final OperatorContext operatorContext; + + private final Operator child; + + private final List<TableAggregator> aggregators; + + private final Type[] groupByTypes; + private final int[] groupByChannels; + + private final TsBlockBuilder resultBuilder; + private final ColumnBuilder[] resultColumnsBuilder; + + private boolean finished = false; + + // cached current group to judge row equality + private TsBlock currentGroup; + + // more than one group in input block + private final Deque<TsBlock> outputs = new LinkedList<>(); + + public StreamingAggregationOperator( + OperatorContext operatorContext, + Operator child, + List<Type> groupByTypes, + List<Integer> groupByChannels, + List<TableAggregator> aggregators, + long maxPartialMemory, + boolean spillEnabled, + long unSpillMemoryLimit) { + this.operatorContext = operatorContext; + this.child = child; + this.groupByTypes = groupByTypes.toArray(new Type[0]); + this.groupByChannels = Ints.toArray(groupByChannels); + this.aggregators = aggregators; + this.resultBuilder = + new TsBlockBuilder( + Stream.concat( + groupByTypes.stream().map(InternalTypeManager::getTSDataType), + aggregators.stream().map(TableAggregator::getType)) + .collect(Collectors.toList())); + this.resultColumnsBuilder = resultBuilder.getValueColumnBuilders(); + checkArgument(!spillEnabled, "spill is not supported"); + } + + @Override + public ListenableFuture<?> isBlocked() { + return child.isBlocked(); + } + + @Override + public boolean hasNext() throws Exception { + return !finished || retainedTsBlock != null || !outputs.isEmpty(); + } + + @Override + public TsBlock next() throws Exception { + if (retainedTsBlock != null) { + return getResultFromRetainedTsBlock(); + } + + if (!outputs.isEmpty()) { + resultTsBlock = outputs.removeFirst(); + return checkTsBlockSizeAndGetResult(); + } + + TsBlock block; + if (child.hasNextWithTimer()) { + block = child.nextWithTimer(); + if (block == null) { + return null; + } + + processInput(block); + + if (outputs.isEmpty()) { + return null; + } + } else { + // last evaluate + if (currentGroup != null) { + evaluateAndFlushGroup(currentGroup, 0, true); + currentGroup = null; + } + finished = true; + } + resultTsBlock = outputs.removeFirst(); Review Comment: add checkArguments. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
