Beyyes commented on code in PR #14213: URL: https://github.com/apache/iotdb/pull/14213#discussion_r1922031734
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/window/TableWindowOperator.java: ########## @@ -0,0 +1,366 @@ +/* + * 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.process.window; + +import org.apache.iotdb.db.queryengine.execution.MemoryEstimationHelper; +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.process.ProcessOperator; +import org.apache.iotdb.db.queryengine.execution.operator.process.window.function.WindowFunction; +import org.apache.iotdb.db.queryengine.execution.operator.process.window.partition.PartitionExecutor; +import org.apache.iotdb.db.queryengine.execution.operator.process.window.partition.frame.FrameInfo; +import org.apache.iotdb.db.queryengine.execution.operator.process.window.utils.RowComparator; + +import com.google.common.collect.ImmutableList; +import org.apache.tsfile.block.column.Column; +import org.apache.tsfile.common.conf.TSFileDescriptor; +import org.apache.tsfile.enums.TSDataType; +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.utils.RamUsageEstimator; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import static org.apache.iotdb.db.queryengine.execution.operator.source.relational.TableScanOperator.TIME_COLUMN_TEMPLATE; + +public class TableWindowOperator implements ProcessOperator { + private static final long INSTANCE_SIZE = + RamUsageEstimator.shallowSizeOfInstance(TableWindowOperator.class); + + // Common fields + private final OperatorContext operatorContext; + private final Operator inputOperator; + private final List<TSDataType> inputDataTypes; + private final TsBlockBuilder tsBlockBuilder; + + // Basic information about window operator + private final List<WindowFunction> windowFunctions; + private final List<FrameInfo> frameInfoList; + + // Partition + private final List<Integer> partitionChannels; + private final RowComparator partitionComparator; + private final List<TsBlock> cachedTsBlocks; + private int startIndexInFirstBlock; + + // Sort + private final List<Integer> sortChannels; + + // Transformation + private LinkedList<PartitionExecutor> cachedPartitionExecutors; + + public TableWindowOperator( + OperatorContext operatorContext, + Operator inputOperator, + List<TSDataType> inputDataTypes, + List<TSDataType> outputDataTypes, + List<WindowFunction> windowFunctions, + List<FrameInfo> frameInfoList, + List<Integer> partitionChannels, + List<Integer> sortChannels) { + // Common part(among all other operators) + this.operatorContext = operatorContext; + this.inputOperator = inputOperator; + this.inputDataTypes = ImmutableList.copyOf(inputDataTypes); + this.tsBlockBuilder = new TsBlockBuilder(outputDataTypes); + + // Basic information part + this.windowFunctions = windowFunctions; + this.frameInfoList = frameInfoList; + + // Partition Part + this.partitionChannels = ImmutableList.copyOf(partitionChannels); + // Acquire partition channels' data types + List<TSDataType> partitionDataTypes = new ArrayList<>(); + for (Integer channel : partitionChannels) { + partitionDataTypes.add(inputDataTypes.get(channel)); + } + this.partitionComparator = new RowComparator(partitionDataTypes); + + // Ordering part + this.sortChannels = ImmutableList.copyOf(sortChannels); + + // Transformation part + this.cachedPartitionExecutors = new LinkedList<>(); + + // Misc + this.cachedTsBlocks = new ArrayList<>(); + this.startIndexInFirstBlock = -1; + } + + @Override + public OperatorContext getOperatorContext() { + return operatorContext; + } + + @Override + public TsBlock next() throws Exception { + // Transform is not finished + if (!cachedPartitionExecutors.isEmpty()) { + TsBlock tsBlock = transform(); + if (tsBlock != null) { + return tsBlock; + } + // Receive more data when result TsBlock builder is not full + // In this case, all partition executors are done + } + + if (inputOperator.hasNextWithTimer()) { + // This TsBlock is pre-sorted with PARTITION BY and ORDER BY channels + TsBlock preSortedBlock = inputOperator.next(); + // StreamSort Operator sometimes returns null + if (preSortedBlock == null || preSortedBlock.isEmpty()) { + return null; + } + + cachedPartitionExecutors = partition(preSortedBlock); + if (cachedPartitionExecutors.isEmpty()) { + // No partition found + // i.e., partition crosses multiple TsBlocks + return null; + } + + // May return null if builder is not full + return transform(); + } else if (!cachedTsBlocks.isEmpty()) { Review Comment: consider memory control of cachedBlocks ########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/window/TableWindowOperator.java: ########## @@ -0,0 +1,366 @@ +/* + * 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.process.window; + +import org.apache.iotdb.db.queryengine.execution.MemoryEstimationHelper; +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.process.ProcessOperator; +import org.apache.iotdb.db.queryengine.execution.operator.process.window.function.WindowFunction; +import org.apache.iotdb.db.queryengine.execution.operator.process.window.partition.PartitionExecutor; +import org.apache.iotdb.db.queryengine.execution.operator.process.window.partition.frame.FrameInfo; +import org.apache.iotdb.db.queryengine.execution.operator.process.window.utils.RowComparator; + +import com.google.common.collect.ImmutableList; +import org.apache.tsfile.block.column.Column; +import org.apache.tsfile.common.conf.TSFileDescriptor; +import org.apache.tsfile.enums.TSDataType; +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.utils.RamUsageEstimator; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import static org.apache.iotdb.db.queryengine.execution.operator.source.relational.TableScanOperator.TIME_COLUMN_TEMPLATE; + +public class TableWindowOperator implements ProcessOperator { + private static final long INSTANCE_SIZE = + RamUsageEstimator.shallowSizeOfInstance(TableWindowOperator.class); + + // Common fields + private final OperatorContext operatorContext; + private final Operator inputOperator; + private final List<TSDataType> inputDataTypes; + private final TsBlockBuilder tsBlockBuilder; + + // Basic information about window operator + private final List<WindowFunction> windowFunctions; + private final List<FrameInfo> frameInfoList; + + // Partition + private final List<Integer> partitionChannels; + private final RowComparator partitionComparator; + private final List<TsBlock> cachedTsBlocks; + private int startIndexInFirstBlock; + + // Sort + private final List<Integer> sortChannels; + + // Transformation + private LinkedList<PartitionExecutor> cachedPartitionExecutors; + + public TableWindowOperator( + OperatorContext operatorContext, + Operator inputOperator, + List<TSDataType> inputDataTypes, + List<TSDataType> outputDataTypes, + List<WindowFunction> windowFunctions, + List<FrameInfo> frameInfoList, + List<Integer> partitionChannels, + List<Integer> sortChannels) { + // Common part(among all other operators) + this.operatorContext = operatorContext; + this.inputOperator = inputOperator; + this.inputDataTypes = ImmutableList.copyOf(inputDataTypes); + this.tsBlockBuilder = new TsBlockBuilder(outputDataTypes); + + // Basic information part + this.windowFunctions = windowFunctions; + this.frameInfoList = frameInfoList; + + // Partition Part + this.partitionChannels = ImmutableList.copyOf(partitionChannels); + // Acquire partition channels' data types + List<TSDataType> partitionDataTypes = new ArrayList<>(); + for (Integer channel : partitionChannels) { + partitionDataTypes.add(inputDataTypes.get(channel)); + } + this.partitionComparator = new RowComparator(partitionDataTypes); + + // Ordering part + this.sortChannels = ImmutableList.copyOf(sortChannels); + + // Transformation part + this.cachedPartitionExecutors = new LinkedList<>(); + + // Misc + this.cachedTsBlocks = new ArrayList<>(); + this.startIndexInFirstBlock = -1; + } + + @Override + public OperatorContext getOperatorContext() { + return operatorContext; + } + + @Override + public TsBlock next() throws Exception { Review Comment: consider time slice calculation -- 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]
