This is an automated email from the ASF dual-hosted git repository.

JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 15122483c4b fix: fix the missing final window in date_bin_gapfill 
queries
15122483c4b is described below

commit 15122483c4b304741d404ed4ec9cb4795d84bad9
Author: Zeyu Zhang <[email protected]>
AuthorDate: Tue Dec 1 19:55:53 2026 +0800

    fix: fix the missing final window in date_bin_gapfill queries
---
 .../process/gapfill/AbstractGapFillOperator.java   | 22 ++++---
 .../gapfill/GapFillWoGroupWoMoOperatorTest.java    | 67 ++++++++++++++++++++++
 2 files changed, 81 insertions(+), 8 deletions(-)

diff --git 
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/gapfill/AbstractGapFillOperator.java
 
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/gapfill/AbstractGapFillOperator.java
index 6c948657b7e..a5ea840a123 100644
--- 
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/gapfill/AbstractGapFillOperator.java
+++ 
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/gapfill/AbstractGapFillOperator.java
@@ -59,6 +59,7 @@ abstract class AbstractGapFillOperator implements 
ProcessOperator {
   private final long endTime;
 
   protected long currentTime;
+  private boolean timeIteratorExhausted;
   private SortKey lastGroupKey = null;
 
   AbstractGapFillOperator(
@@ -140,6 +141,14 @@ abstract class AbstractGapFillOperator implements 
ProcessOperator {
 
   private void resetTimeIterator() {
     currentTime = startTime;
+    timeIteratorExhausted = false;
+  }
+
+  private void advanceTimeIterator() {
+    long previousTime = currentTime;
+    nextTime();
+    // Saturation leaves no later timestamp to mark the consumed window across 
next() calls.
+    timeIteratorExhausted |= currentTime <= previousTime;
   }
 
   private void writeCurrentRow(TsBlock block, int rowIndex) {
@@ -153,7 +162,7 @@ abstract class AbstractGapFillOperator implements 
ProcessOperator {
         columnBuilder.write(column, rowIndex);
       }
     }
-    nextTime();
+    advanceTimeIterator();
   }
 
   private void fillGaps(TsBlock block, int rowIndex, long currentEndTime) {
@@ -162,13 +171,10 @@ abstract class AbstractGapFillOperator implements 
ProcessOperator {
 
   private void fillGaps(
       TsBlock block, int rowIndex, long currentEndTime, boolean 
endTimeInclusive) {
-    while (currentTime < currentEndTime || (endTimeInclusive && currentTime == 
currentEndTime)) {
-      long previousTime = currentTime;
+    while (!timeIteratorExhausted
+        && (currentTime < currentEndTime || (endTimeInclusive && currentTime 
== currentEndTime))) {
       gapFillRow(currentTime, block, rowIndex);
-      nextTime();
-      if (currentTime <= previousTime) {
-        break;
-      }
+      advanceTimeIterator();
     }
   }
 
@@ -224,7 +230,7 @@ abstract class AbstractGapFillOperator implements 
ProcessOperator {
   }
 
   private boolean hasRemainingGapInPreviousGroup() {
-    return lastGroupKey != null && currentTime < endTime;
+    return lastGroupKey != null && !timeIteratorExhausted && currentTime <= 
endTime;
   }
 
   @Override
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/process/gapfill/GapFillWoGroupWoMoOperatorTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/process/gapfill/GapFillWoGroupWoMoOperatorTest.java
index 0b08c7e2c62..b22cbca2d70 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/process/gapfill/GapFillWoGroupWoMoOperatorTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/process/gapfill/GapFillWoGroupWoMoOperatorTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.iotdb.db.queryengine.execution.operator.process.gapfill;
 
+import org.apache.iotdb.calc.execution.operator.CommonOperatorContext;
 import org.apache.iotdb.calc.execution.operator.Operator;
 import org.apache.iotdb.calc.execution.operator.process.MergeSortOperator;
 import 
org.apache.iotdb.calc.execution.operator.process.gapfill.GapFillWoGroupWoMoOperator;
@@ -42,12 +43,21 @@ import org.junit.AfterClass;
 import org.junit.Test;
 
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
 import java.util.concurrent.ExecutorService;
 
 import static 
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext.createFragmentInstanceContext;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class GapFillWoGroupWoMoOperatorTest {
 
@@ -72,6 +82,63 @@ public class GapFillWoGroupWoMoOperatorTest {
     instanceNotificationExecutor.shutdown();
   }
 
+  @Test
+  public void testFinalGapAfterChildExhausted() throws Exception {
+    assertFinalGapEmittedOnce(3L);
+  }
+
+  @Test
+  public void testFinalGapAtLongMaxValueEmittedOnce() throws Exception {
+    assertFinalGapEmittedOnce(Long.MAX_VALUE);
+  }
+
+  private void assertFinalGapEmittedOnce(long endTime) throws Exception {
+    List<TSDataType> dataTypes = Arrays.asList(TSDataType.TIMESTAMP, 
TSDataType.DOUBLE);
+    TsBlockBuilder builder = new TsBlockBuilder(dataTypes);
+    builder.getColumnBuilder(0).writeLong(endTime - 1);
+    builder.getColumnBuilder(1).writeDouble(2.0);
+    builder.declarePosition();
+    Iterator<TsBlock> blocks =
+        Collections.singletonList(
+                builder.build(
+                    new 
RunLengthEncodedColumn(CommonOperatorUtils.TIME_COLUMN_TEMPLATE, 1)))
+            .iterator();
+    Operator child = mock(Operator.class);
+    when(child.hasNextWithTimer()).thenAnswer(invocation -> blocks.hasNext());
+    when(child.nextWithTimer()).thenAnswer(invocation -> blocks.next());
+    when(child.isFinished()).thenAnswer(invocation -> !blocks.hasNext());
+
+    try (GapFillWoGroupWoMoOperator operator =
+        new GapFillWoGroupWoMoOperator(
+            mock(CommonOperatorContext.class), child, 0, endTime - 2, endTime, 
dataTypes, 1L)) {
+      assertTrue(operator.hasNext());
+      assertFalse(operator.isFinished());
+      TsBlock first = operator.next();
+      assertEquals(2, first.getPositionCount());
+      assertEquals(endTime - 2, first.getColumn(0).getLong(0));
+      assertTrue(first.getColumn(1).isNull(0));
+      assertEquals(endTime - 1, first.getColumn(0).getLong(1));
+      assertEquals(2.0, first.getColumn(1).getDouble(1), 0.0);
+
+      // The child is exhausted, but the inclusive final window still needs to 
be emitted.
+      assertFalse(child.hasNextWithTimer());
+      assertTrue(child.isFinished());
+      assertTrue(operator.hasNext());
+      assertFalse(operator.isFinished());
+      TsBlock last = operator.next();
+      assertEquals(1, last.getPositionCount());
+      assertEquals(endTime, last.getColumn(0).getLong(0));
+      assertTrue(last.getColumn(1).isNull(0));
+
+      assertFalse(operator.hasNext());
+      assertTrue(operator.isFinished());
+      assertNull(operator.next());
+      assertFalse(operator.hasNext());
+      assertTrue(operator.isFinished());
+      verify(child, times(1)).nextWithTimer();
+    }
+  }
+
   @Test
   public void testGapFillWGroupWoMoOperator1() {
     final double[] valueArray =

Reply via email to