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 688bb958ef9 [Bug] Fix date_bin month bins counting intra-second
milliseconds twice
688bb958ef9 is described below
commit 688bb958ef946f7afc84b851fd7d201968a63d5e
Author: DaZuiZui <[email protected]>
AuthorDate: Mon Nov 16 17:31:56 2026 +0800
[Bug] Fix date_bin month bins counting intra-second milliseconds twice
---
.../scalar/DateBinFunctionColumnTransformer.java | 20 +++---
.../column/unary/scalar/DateBinFunctionTest.java | 71 ++++++++++++++++++++++
2 files changed, 83 insertions(+), 8 deletions(-)
diff --git
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/column/unary/scalar/DateBinFunctionColumnTransformer.java
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/column/unary/scalar/DateBinFunctionColumnTransformer.java
index 445ca0560ad..5eb0d5a5281 100644
---
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/column/unary/scalar/DateBinFunctionColumnTransformer.java
+++
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/column/unary/scalar/DateBinFunctionColumnTransformer.java
@@ -90,19 +90,23 @@ public class DateBinFunctionColumnTransformer extends
UnaryColumnTransformer {
}
private static long convertToTimestamp(LocalDateTime dateTime, ZoneId
zoneId) {
- // Converting LocalDateTime to Seconds since epoch
- long epochMilliSecond = dateTime.atZone(zoneId).toInstant().toEpochMilli();
- // Get the nanoseconds section
- long nanoAdjustment = dateTime.getNano();
+ // getEpochSecond() gives the whole seconds and getNano() the nanos inside
the second, so the
+ // milliseconds inside the second are counted exactly once. TimeUnit
saturates at the long
+ // bounds; the sub-second part must use the same saturating add, or a bin
past the long range
+ // wraps to the opposite end.
+ Instant instant = dateTime.atZone(zoneId).toInstant();
+ long epochSecond = instant.getEpochSecond();
+ long nanoAdjustment = instant.getNano();
switch
(CommonDescriptor.getInstance().getConfig().getTimestampPrecision()) {
case "ms":
- return epochMilliSecond + nanoAdjustment / NANOSECONDS_IN_MILLISECOND;
+ return saturatedAdd(
+ TimeUnit.SECONDS.toMillis(epochSecond), nanoAdjustment /
NANOSECONDS_IN_MILLISECOND);
case "us":
- return TimeUnit.MILLISECONDS.toMicros(epochMilliSecond)
- + nanoAdjustment / NANOSECONDS_IN_MICROSECOND;
+ return saturatedAdd(
+ TimeUnit.SECONDS.toMicros(epochSecond), nanoAdjustment /
NANOSECONDS_IN_MICROSECOND);
case "ns":
- return TimeUnit.MILLISECONDS.toNanos(epochMilliSecond) +
nanoAdjustment;
+ return saturatedAdd(TimeUnit.SECONDS.toNanos(epochSecond),
nanoAdjustment);
default:
throw new IllegalArgumentException(
CalcMessages.EXCEPTION_UNKNOWN_PRECISION_0119BEB0
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/transformation/dag/column/unary/scalar/DateBinFunctionTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/transformation/dag/column/unary/scalar/DateBinFunctionTest.java
index 013e8c84290..1548f279701 100644
---
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/transformation/dag/column/unary/scalar/DateBinFunctionTest.java
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/transformation/dag/column/unary/scalar/DateBinFunctionTest.java
@@ -19,6 +19,11 @@
package org.apache.iotdb.db.queryengine.transformation.dag.column.unary.scalar;
+import
org.apache.iotdb.calc.transformation.dag.column.unary.scalar.DateBinFunctionColumnTransformer;
+import org.apache.iotdb.commons.conf.CommonDescriptor;
+
+import org.apache.tsfile.read.common.type.TimestampType;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -28,6 +33,8 @@ import java.time.ZoneId;
import java.util.TimeZone;
import static
org.apache.iotdb.calc.transformation.dag.column.unary.scalar.DateBinFunctionColumnTransformer.dateBin;
+import static
org.apache.iotdb.calc.transformation.dag.column.unary.scalar.DateBinFunctionColumnTransformer.nextDateBin;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class DateBinFunctionTest {
@@ -126,4 +133,68 @@ public class DateBinFunctionTest {
zoneId);
assertEquals(getTimestamp("1969-12-31 00:00:00.000"), result);
}
+
+ @Test
+ public void testMonthIntervalWithSubSecondOrigin() throws ParseException {
+ // 500 ms is 1970-01-01T00:00:00.500Z, 2678400500 ms is also exactly one
month later
+ assertEquals(500, dateBin(500, 500, 1, 0, zoneId));
+ assertEquals(2678400500L, dateBin(2678400500L, 500, 1, 0, zoneId));
+ // a point 200 ms into the next bin must still map to the start of that bin
+ assertEquals(2678400500L, dateBin(2678400700L, 500, 1, 0, zoneId));
+ // an origin far from the epoch must keep its sub-second part too
+ long origin = getTimestamp("2024-03-15 00:00:00.500");
+ assertEquals(origin, dateBin(origin, origin, 1, 0, zoneId));
+ assertEquals(origin, dateBin(origin + MILLIS_IN_DAY, origin, 1, 0,
zoneId));
+ // before 1970, getEpochSecond() rounds toward negative infinity and
getNano() stays
+ // non-negative, so the sub-second origin is preserved on both sides of
the epoch
+ assertEquals(-500L, dateBin(-500L, -500L, 1, 0, zoneId));
+ assertEquals(-2678400500L, dateBin(-2678399500L, -500L, 1, 0, zoneId));
+ }
+
+ @Test
+ public void testMonthIntervalStartEndWithSubSecondOrigin() {
+ DateBinFunctionColumnTransformer transformer =
+ new DateBinFunctionColumnTransformer(TimestampType.TIMESTAMP, 1, 0,
null, 500, zoneId);
+ assertArrayEquals(
+ new long[] {2678400500L, 5097600500L},
transformer.dateBinStartEnd(2678400700L));
+ }
+
+ @Test
+ public void testNextDateBinWithSubSecondOrigin() {
+ assertEquals(2678400500L, nextDateBin(1, zoneId, 500));
+ }
+
+ @Test
+ public void testMonthBinPastLongRangeSaturates() {
+ DateBinFunctionColumnTransformer transformer =
+ new DateBinFunctionColumnTransformer(TimestampType.TIMESTAMP, 1, 0,
null, 500, zoneId);
+ long[] startEnd = transformer.dateBinStartEnd(Long.MAX_VALUE - 1000);
+ assertEquals(9223372035446400500L, startEnd[0]);
+ assertEquals(Long.MAX_VALUE, startEnd[1]);
+ assertEquals(Long.MAX_VALUE, nextDateBin(1, zoneId, startEnd[0]));
+ }
+
+ @Test
+ public void testMonthIntervalWithSubSecondOriginInMicrosecondPrecision() {
+ setTimestampPrecision("us");
+ assertEquals(1500, dateBin(1500, 1500, 1, 0, zoneId));
+ // 2678400000000 us is one month after the epoch
+ assertEquals(2678400001500L, dateBin(2678400001500L, 1500, 1, 0, zoneId));
+ assertEquals(-1500L, dateBin(-1500L, -1500L, 1, 0, zoneId));
+ }
+
+ @Test
+ public void testMonthIntervalWithSubSecondOriginInNanosecondPrecision() {
+ setTimestampPrecision("ns");
+ assertEquals(1_500_000L, dateBin(1_500_000L, 1_500_000L, 1, 0, zoneId));
+ }
+
+ private static void setTimestampPrecision(String precision) {
+
CommonDescriptor.getInstance().getConfig().setTimestampPrecision(precision);
+ }
+
+ @After
+ public void after() {
+ setTimestampPrecision("ms");
+ }
}