Sh-Zh-7 commented on code in PR #16953:
URL: https://github.com/apache/iotdb/pull/16953#discussion_r2709508436


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/window/RowNumberOperator.java:
##########
@@ -0,0 +1,214 @@
+/*
+ * 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.source.relational.aggregation.grouped.UpdateMemory;
+import 
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.hash.GroupByHash;
+import 
org.apache.iotdb.db.queryengine.plan.relational.type.InternalTypeManager;
+
+import com.google.common.collect.ImmutableList;
+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.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.read.common.type.Type;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static 
org.apache.iotdb.db.queryengine.execution.operator.source.relational.AbstractTableScanOperator.TIME_COLUMN_TEMPLATE;
+import static 
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.hash.GroupByHash.createGroupByHash;
+
+public class RowNumberOperator implements ProcessOperator {
+  private static final long INSTANCE_SIZE =
+      RamUsageEstimator.shallowSizeOfInstance(RowNumberOperator.class);
+
+  private final OperatorContext operatorContext;
+  private final Operator inputOperator;
+  private final List<Integer> outputChannels;
+  private final List<Integer> partitionChannels;
+  private final TsBlockBuilder tsBlockBuilder;
+
+  private final Optional<GroupByHash> groupByHash;
+  private final Optional<Integer> maxRowsPerPartition;
+  private final Map<Integer, Long> partitionRowCounts;
+
+  public RowNumberOperator(
+      OperatorContext operatorContext,
+      Operator inputOperator,
+      List<TSDataType> inputDataTypes,
+      List<Integer> outputChannels,
+      List<Integer> partitionChannels,
+      Optional<Integer> maxRowsPerPartition,
+      int expectedPositions) {
+    this.operatorContext = operatorContext;
+    this.inputOperator = inputOperator;
+    this.outputChannels = ImmutableList.copyOf(outputChannels);
+    this.partitionChannels = ImmutableList.copyOf(partitionChannels);
+    this.maxRowsPerPartition = maxRowsPerPartition;
+
+    // Output data types
+    // original output channels + row number column
+    List<TSDataType> outputDataTypes = new ArrayList<>();
+    for (int channel : outputChannels) {
+      outputDataTypes.add(inputDataTypes.get(channel));
+    }
+    outputDataTypes.add(TSDataType.INT64);
+    this.tsBlockBuilder = new TsBlockBuilder(outputDataTypes);
+
+    if (partitionChannels.isEmpty()) {
+      this.groupByHash = Optional.empty();
+    } else {
+      // Partition data types
+      List<Type> partitionDataTypes = new ArrayList<>();
+      for (int channel : partitionChannels) {
+        TSDataType tsDataType = inputDataTypes.get(channel);
+        Type convertType = InternalTypeManager.fromTSDataType(tsDataType);
+        partitionDataTypes.add(convertType);
+      }
+      this.groupByHash =
+          Optional.of(
+              createGroupByHash(partitionDataTypes, false, expectedPositions, 
UpdateMemory.NOOP));
+    }
+
+    this.partitionRowCounts = new HashMap<>(expectedPositions);
+  }
+
+  @Override
+  public OperatorContext getOperatorContext() {
+    return operatorContext;
+  }
+
+  @Override
+  public TsBlock next() throws Exception {
+    TsBlock tsBlock = inputOperator.nextWithTimer();
+    if (tsBlock == null) {
+      return null;
+    }
+
+    int[] partitionIds = getTsBlockPartitionIds(tsBlock);
+    for (int position = 0; position < tsBlock.getPositionCount(); position++) {
+      int partitionId = groupByHash.isPresent() ? partitionIds[position] : 0;
+      long rowCount = partitionRowCounts.getOrDefault(partitionId, 0L);
+      processRow(tsBlock, partitionId, rowCount + 1);
+      partitionRowCounts.put(partitionId, rowCount + 1);
+    }
+
+    TsBlock result =
+        tsBlockBuilder.build(
+            new RunLengthEncodedColumn(TIME_COLUMN_TEMPLATE, 
tsBlockBuilder.getPositionCount()));
+    tsBlockBuilder.reset();
+    return result;
+  }
+
+  private void processRow(TsBlock tsBlock, int position, long rowNumber) {
+    // Check max rows per partition limit
+    if (maxRowsPerPartition.isPresent() && rowNumber >= 
maxRowsPerPartition.get()) {

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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to