From e4918e955113aff2cc653b6ffb30b22c9f7d945e Mon Sep 17 00:00:00 2001 From: Zihan Dai Date: Tue, 3 Nov 2026 15:29:33 +1100 Subject: [PATCH] Fix library UDF time units h and d overflowing at nanosecond precision At nanosecond precision, Util.parseTime multiplied the hour and day constants as int and widened to long only at the last factor, so the product overflowed first. "1h" became negative and was rejected as an unknown unit, and "1d" became 500,654,080,000 ns, which made INTEGRAL with unit '1d' return 172.57 times the correct value. Start both products with a long factor. --- .../org/apache/iotdb/library/util/Util.java | 4 +- .../iotdb/library/UtilParseTimeTest.java | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 library-udf/src/test/java/org/apache/iotdb/library/UtilParseTimeTest.java diff --git a/library-udf/src/main/java/org/apache/iotdb/library/util/Util.java b/library-udf/src/main/java/org/apache/iotdb/library/util/Util.java index 76b0831060f48..4729718de4f55 100644 --- a/library-udf/src/main/java/org/apache/iotdb/library/util/Util.java +++ b/library-udf/src/main/java/org/apache/iotdb/library/util/Util.java @@ -300,10 +300,10 @@ public static long parseTime(String s, UDFParameters parameters) { unit = 60 * 1000 * 1000 * 1000L; s = s.substring(0, s.length() - 1); } else if (s.endsWith("h")) { - unit = 60 * 60 * 1000 * 1000 * 1000L; + unit = 60L * 60 * 1000 * 1000 * 1000; s = s.substring(0, s.length() - 1); } else if (s.endsWith("d")) { - unit = 24 * 60 * 60 * 1000 * 1000 * 1000L; + unit = 24L * 60 * 60 * 1000 * 1000 * 1000; s = s.substring(0, s.length() - 1); } } diff --git a/library-udf/src/test/java/org/apache/iotdb/library/UtilParseTimeTest.java b/library-udf/src/test/java/org/apache/iotdb/library/UtilParseTimeTest.java new file mode 100644 index 0000000000000..ca812f876a2cc --- /dev/null +++ b/library-udf/src/test/java/org/apache/iotdb/library/UtilParseTimeTest.java @@ -0,0 +1,60 @@ +/* + * 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.library; + +import org.apache.iotdb.library.util.Util; +import org.apache.iotdb.udf.api.customizer.parameter.UDFParameters; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class UtilParseTimeTest { + + private static UDFParameters parametersWithPrecision(String precision) { + Map systemAttributes = new HashMap<>(); + systemAttributes.put("timestampPrecision", precision); + return new UDFParameters( + Collections.emptyList(), Collections.emptyList(), new HashMap<>(), systemAttributes); + } + + @Test + public void testHourAndDayAtNanosecondPrecision() { + UDFParameters ns = parametersWithPrecision("ns"); + Assert.assertEquals(3_600_000_000_000L, Util.parseTime("1h", ns)); + Assert.assertEquals(86_400_000_000_000L, Util.parseTime("1d", ns)); + Assert.assertEquals(172_800_000_000_000L, Util.parseTime("2d", ns)); + } + + @Test + public void testUnitsScaleWithPrecision() { + UDFParameters ms = parametersWithPrecision("ms"); + UDFParameters us = parametersWithPrecision("us"); + UDFParameters ns = parametersWithPrecision("ns"); + for (String unit : new String[] {"1ms", "1s", "1m", "1h", "1d"}) { + long millis = Util.parseTime(unit, ms); + Assert.assertEquals(unit, millis * 1_000L, Util.parseTime(unit, us)); + Assert.assertEquals(unit, millis * 1_000_000L, Util.parseTime(unit, ns)); + } + } +}