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 76b0831060f4..4729718de4f5 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 000000000000..ca812f876a2c --- /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)); + } + } +}