JackieTien97 commented on code in PR #17810:
URL: https://github.com/apache/iotdb/pull/17810#discussion_r3338156389


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/planner/node/NextFillNode.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.commons.queryengine.plan.relational.planner.node;
+
+import 
org.apache.iotdb.commons.queryengine.plan.planner.plan.node.ICoreQueryPlanVisitor;
+import 
org.apache.iotdb.commons.queryengine.plan.planner.plan.node.IPlanVisitor;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeId;
+import 
org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeType;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.Symbol;
+
+import com.google.common.collect.Iterables;
+import org.apache.tsfile.utils.ReadWriteIOUtils;
+import org.apache.tsfile.utils.TimeDuration;
+
+import javax.annotation.Nullable;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+public class NextFillNode extends FillNode {
+
+  @Nullable private final TimeDuration timeBound;
+
+  @Nullable private final Symbol helperColumn;
+
+  @Nullable private final List<Symbol> groupingKeys;
+
+  public NextFillNode(
+      PlanNodeId id,
+      PlanNode child,
+      TimeDuration timeBound,
+      Symbol helperColumn,
+      List<Symbol> groupingKeys) {
+    super(id, child);
+    this.timeBound = timeBound;
+    this.helperColumn = helperColumn;
+    this.groupingKeys = groupingKeys;
+  }
+
+  public Optional<TimeDuration> getTimeBound() {
+    return Optional.ofNullable(timeBound);
+  }
+
+  public Optional<Symbol> getHelperColumn() {
+    return Optional.ofNullable(helperColumn);
+  }
+
+  public Optional<List<Symbol>> getGroupingKeys() {
+    return Optional.ofNullable(groupingKeys);
+  }
+
+  @Override
+  public PlanNode clone() {
+    return new NextFillNode(id, null, timeBound, helperColumn, groupingKeys);
+  }
+
+  @Override
+  public <R, C> R accept(IPlanVisitor<R, C> visitor, C context) {
+    return ((ICoreQueryPlanVisitor<R, C>) visitor).visitNextFill(this, 
context);
+  }
+
+  @Override
+  protected void serializeAttributes(ByteBuffer byteBuffer) {
+    PlanNodeType.TABLE_NEXT_FILL_NODE.serialize(byteBuffer);
+    if (timeBound == null) {
+      ReadWriteIOUtils.write(false, byteBuffer);
+    } else {
+      ReadWriteIOUtils.write(true, byteBuffer);
+      timeBound.serialize(byteBuffer);
+    }
+
+    if (helperColumn == null) {
+      ReadWriteIOUtils.write(false, byteBuffer);
+    } else {
+      ReadWriteIOUtils.write(true, byteBuffer);
+      Symbol.serialize(helperColumn, byteBuffer);
+    }
+
+    if (groupingKeys == null) {
+      ReadWriteIOUtils.write(false, byteBuffer);
+    } else {
+      ReadWriteIOUtils.write(true, byteBuffer);
+      ReadWriteIOUtils.write(groupingKeys.size(), byteBuffer);
+      for (Symbol symbol : groupingKeys) {
+        Symbol.serialize(symbol, byteBuffer);
+      }
+    }
+  }
+
+  @Override
+  protected void serializeAttributes(DataOutputStream stream) throws 
IOException {
+    PlanNodeType.TABLE_NEXT_FILL_NODE.serialize(stream);
+    if (timeBound == null) {
+      ReadWriteIOUtils.write(false, stream);
+    } else {
+      ReadWriteIOUtils.write(true, stream);
+      timeBound.serialize(stream);
+    }
+    if (helperColumn == null) {
+      ReadWriteIOUtils.write(false, stream);
+    } else {
+      ReadWriteIOUtils.write(true, stream);
+      Symbol.serialize(helperColumn, stream);
+    }
+    if (groupingKeys == null) {
+      ReadWriteIOUtils.write(false, stream);
+    } else {
+      ReadWriteIOUtils.write(true, stream);
+      ReadWriteIOUtils.write(groupingKeys.size(), stream);
+      for (Symbol symbol : groupingKeys) {
+        Symbol.serialize(symbol, stream);
+      }
+    }
+  }
+
+  public static NextFillNode deserialize(ByteBuffer byteBuffer) {
+    boolean hasValue = ReadWriteIOUtils.readBool(byteBuffer);
+    TimeDuration timeDuration = null;
+    if (hasValue) {
+      timeDuration = TimeDuration.deserialize(byteBuffer);
+    }
+    hasValue = ReadWriteIOUtils.readBool(byteBuffer);
+    Symbol helperColumn = null;
+    if (hasValue) {
+      helperColumn = Symbol.deserialize(byteBuffer);
+    }
+    hasValue = ReadWriteIOUtils.readBool(byteBuffer);
+    List<Symbol> groupingKeys = null;
+    if (hasValue) {
+      int size = ReadWriteIOUtils.readInt(byteBuffer);
+      groupingKeys = new ArrayList<>(size);
+      while (size-- > 0) {
+        groupingKeys.add(Symbol.deserialize(byteBuffer));
+      }
+    }
+    PlanNodeId planNodeId = PlanNodeId.deserialize(byteBuffer);
+    return new NextFillNode(planNodeId, null, timeDuration, helperColumn, 
groupingKeys);
+  }
+
+  @Override
+  public PlanNode replaceChildren(List<PlanNode> newChildren) {
+    return new NextFillNode(
+        id, Iterables.getOnlyElement(newChildren), timeBound, helperColumn, 
groupingKeys);
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    if (!super.equals(o)) {
+      return false;
+    }
+    NextFillNode that = (NextFillNode) o;
+    return Objects.equals(timeBound, that.timeBound)
+        && Objects.equals(helperColumn, that.helperColumn)
+        && Objects.equals(groupingKeys, that.groupingKeys);

Review Comment:
   `equals`/`hashCode` here correctly include `groupingKeys`. Worth noting the 
sibling `PreviousFillNode` does **not** — its `equals`/`hashCode` only use 
`timeBound` + `helperColumn`, so two PreviousFill nodes that differ only by 
`FILL_GROUP` keys compare equal. Not a problem for this PR (NextFill is the 
correct one), but since the two classes are otherwise identical you may want to 
align `PreviousFillNode` for consistency.
   



##########
iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/TableNextFillWithGroupOperator.java:
##########
@@ -0,0 +1,167 @@
+/*
+ * 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.calc.execution.operator.process;
+
+import org.apache.iotdb.calc.execution.operator.CommonOperatorContext;
+import org.apache.iotdb.calc.execution.operator.Operator;
+import org.apache.iotdb.calc.execution.operator.process.fill.ILinearFill;
+import org.apache.iotdb.calc.plan.planner.CommonOperatorUtils;
+import org.apache.iotdb.calc.utils.datastructure.SortKey;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+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 java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+public class TableNextFillWithGroupOperator extends TableNextFillOperator {
+
+  private final List<Boolean> groupSplitter;
+
+  private final List<Boolean> noMoreTsBlockForCurrentGroup;
+
+  private final Comparator<SortKey> groupKeyComparator;
+
+  private final TsBlockBuilder resultBuilder;
+
+  private SortKey lastRow = null;
+
+  public TableNextFillWithGroupOperator(
+      CommonOperatorContext operatorContext,
+      ILinearFill[] fillArray,
+      Operator child,
+      int helperColumnIndex,
+      boolean hasTimeBound,
+      Comparator<SortKey> groupKeyComparator,
+      List<TSDataType> dataTypes) {
+    super(operatorContext, fillArray, child, helperColumnIndex, hasTimeBound);
+    this.groupSplitter = new ArrayList<>();
+    this.noMoreTsBlockForCurrentGroup = new ArrayList<>();
+    this.groupKeyComparator = groupKeyComparator;
+    this.resultBuilder = new TsBlockBuilder(dataTypes);
+  }
+
+  @Override
+  // we won't build timeColumn in this method
+  TsBlock append(int length, Column timeColumn, Column[] valueColumns) {
+    for (int i = 0; i < outputColumnCount; i++) {
+      Column column = valueColumns[i];
+      ColumnBuilder builder = resultBuilder.getColumnBuilder(i);
+      for (int rowIndex = 0; rowIndex < length; rowIndex++) {
+        if (column.isNull(rowIndex)) {
+          builder.appendNull();
+        } else {
+          builder.write(column, rowIndex);
+        }
+      }
+    }
+    resultBuilder.declarePositions(length);
+    return null;
+  }
+
+  @Override
+  TsBlock buildFinalResult(TsBlock tempResult) {
+    TsBlock result = null;
+    if (!resultBuilder.isEmpty()) {
+      Column timeColumn =
+          new RunLengthEncodedColumn(
+              CommonOperatorUtils.TIME_COLUMN_TEMPLATE, 
resultBuilder.getPositionCount());
+      result = resultBuilder.build(timeColumn);
+      resultBuilder.reset();
+    }
+    return result;
+  }
+
+  @Override
+  boolean noMoreTsBlockForCurrentGroup() {
+    return noMoreTsBlock || noMoreTsBlockForCurrentGroup.get(0);
+  }
+
+  @Override
+  void resetFill() {
+    boolean isNewGroup = Boolean.TRUE.equals(groupSplitter.remove(0));
+    boolean isNoMoreTsBlockForCurrentGroup =
+        Boolean.TRUE.equals(noMoreTsBlockForCurrentGroup.remove(0));
+    if (isNewGroup || isNoMoreTsBlockForCurrentGroup) {

Review Comment:
   This reset condition diverges from 
`TableLinearFillWithGroupOperator#resetFill`, which resets only on 
`isNewGroup`. The extra `|| isNoMoreTsBlockForCurrentGroup` reset is what 
discards a cross-group candidate that Step 1 of 
`AbstractLinearFillOperator#next` may have prepared (via `prepareForNext`) 
*before* `resetFill` runs — 
`testNextFillWithGroupDoesNotUseNextGroupAfterContinuedGroup` covers that case, 
good.
   
   Because `resetFill` runs after `prepareForNext`, could you add the symmetric 
**same-group** test: the first (or continued) block of a group ends in a 
trailing null whose fill value lives in a *later* block of the *same* group, so 
the look-ahead candidate must survive this reset. The existing unit test's 
first block has no trailing null, so this path is currently untested and it's 
the one most sensitive to this condition.
   



##########
iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/TableNextFillOperator.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.calc.execution.operator.process;
+
+import org.apache.iotdb.calc.execution.operator.CommonOperatorContext;
+import org.apache.iotdb.calc.execution.operator.Operator;
+import org.apache.iotdb.calc.execution.operator.process.fill.ILinearFill;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.read.common.block.TsBlock;
+
+public class TableNextFillOperator extends AbstractLinearFillOperator {
+
+  // start from 0; -1 means plain NEXT has no helper column.
+  private final int helperColumnIndex;
+
+  private final boolean hasTimeBound;
+
+  public TableNextFillOperator(
+      CommonOperatorContext operatorContext,
+      ILinearFill[] fillArray,
+      Operator child,
+      int helperColumnIndex,
+      boolean hasTimeBound) {
+    super(operatorContext, fillArray, child);
+    this.helperColumnIndex = helperColumnIndex;
+    this.hasTimeBound = hasTimeBound;
+  }
+
+  @Override
+  protected Column getHelperColumn(TsBlock tsBlock) {
+    return helperColumnIndex == -1 ? tsBlock.getTimeColumn() : 
tsBlock.getColumn(helperColumnIndex);
+  }
+
+  @Override
+  protected Integer getLastRowIndexForNonNullHelperColumn(TsBlock tsBlock) {

Review Comment:
   Minor / defensive: when `hasTimeBound` is true this calls `getHelperColumn`, 
which does `tsBlock.getColumn(helperColumnIndex)` and would throw for 
`helperColumnIndex == -1`. This relies on the invariant "TIME_BOUND present ⇒ 
analyzer always resolves a helper column" (so the index is never -1 here), 
which holds today. A one-line comment — or a `checkArgument` in the constructor 
— documenting that invariant would prevent a confusing `IndexOutOfBounds` if a 
future caller ever builds this operator with `hasTimeBound=true, 
helperColumnIndex=-1`.
   



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/NextFillNodeSerdeTest.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.plan.relational.planner;
+
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeId;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.Symbol;
+import 
org.apache.iotdb.commons.queryengine.plan.relational.planner.node.NextFillNode;
+import 
org.apache.iotdb.db.queryengine.plan.planner.node.PlanNodeDeserializeHelper;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.tsfile.utils.TimeDuration;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+
+public class NextFillNodeSerdeTest {
+
+  @Test
+  public void testNextFillNodeSerde() throws Exception {

Review Comment:
   This only exercises the all-fields-present path. 
`serializeAttributes`/`deserialize` each have three `false` branches (null 
`timeBound` / `helperColumn` / `groupingKeys`), and the all-null case is 
actually the most common one — plain `FILL METHOD NEXT`. Could you add a case 
with all three null, and ideally one partial combination, so those null 
branches are covered?
   



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