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

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git

commit a954bab81af2435bf73611fe972630b9c7f3ca14
Author: Mryange <59914473+mrya...@users.noreply.github.com>
AuthorDate: Fri Jan 26 10:29:09 2024 +0800

    [fix](function) fix error result in time_to_sec and timediff (#30248)
---
 .../trees/expressions/functions/scalar/TimeToSec.java |  4 +++-
 .../java/org/apache/doris/rewrite/FEFunctions.java    |  6 +++---
 .../org/apache/doris/rewrite/FEFunctionsTest.java     | 19 -------------------
 gensrc/script/doris_builtins_functions.py             |  1 +
 .../data/correctness/test_time_function.out           |  9 ++++++++-
 .../suites/correctness/test_time_function.groovy      |  9 +++++++++
 6 files changed, 24 insertions(+), 24 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/TimeToSec.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/TimeToSec.java
index 26fb544e20e..d79c9ce7642 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/TimeToSec.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/TimeToSec.java
@@ -25,6 +25,7 @@ import 
org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
 import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
 import org.apache.doris.nereids.types.IntegerType;
 import org.apache.doris.nereids.types.TimeType;
+import org.apache.doris.nereids.types.TimeV2Type;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
@@ -38,7 +39,8 @@ public class TimeToSec extends ScalarFunction
         implements UnaryExpression, ExplicitlyCastableSignature, 
PropagateNullable {
 
     public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
-            
FunctionSignature.ret(IntegerType.INSTANCE).args(TimeType.INSTANCE));
+            
FunctionSignature.ret(IntegerType.INSTANCE).args(TimeType.INSTANCE),
+            
FunctionSignature.ret(IntegerType.INSTANCE).args(TimeV2Type.INSTANCE));
 
     /**
      * constructor with 1 argument.
diff --git a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java 
b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
index 6ab1c4f8c6d..7a29d7e1b0f 100755
--- a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
@@ -58,12 +58,12 @@ public class FEFunctions {
     /**
      * date and time function
      */
-    @FEFunction(name = "timediff", argTypes = { "DATETIME", "DATETIME" }, 
returnType = "TIME")
+    @FEFunction(name = "timediff", argTypes = { "DATETIME", "DATETIME" }, 
returnType = "TIMEV2")
     public static FloatLiteral timeDiff(LiteralExpr first, LiteralExpr second) 
throws AnalysisException {
         long firstTimestamp = ((DateLiteral) 
first).unixTimestamp(TimeUtils.getTimeZone());
         long secondTimestamp = ((DateLiteral) 
second).unixTimestamp(TimeUtils.getTimeZone());
-        return new FloatLiteral((double) (firstTimestamp - secondTimestamp) / 
1000,
-            FloatLiteral.getDefaultTimeType(Type.TIME));
+        return new FloatLiteral((double) (firstTimestamp - secondTimestamp) * 
1000,
+                FloatLiteral.getDefaultTimeType(Type.TIMEV2));
     }
 
     @FEFunction(name = "datediff", argTypes = { "DATETIME", "DATETIME" }, 
returnType = "INT")
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
index 9f444db9fa8..727a6635620 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
@@ -705,25 +705,6 @@ public class FEFunctionsTest {
         Assert.assertEquals(expectedResult, actualResult);
     }
 
-    @Test
-    public void timeDiffTest() throws AnalysisException {
-        DateLiteral d1 = new DateLiteral("1019-02-28 00:00:00", Type.DATETIME);
-        DateLiteral d2 = new DateLiteral("2019-02-28 00:00:00", Type.DATETIME);
-        DateLiteral d3 = new DateLiteral("2019-03-28 00:00:00", Type.DATETIME);
-        Assert.assertEquals(31556995543L, FEFunctions.timeDiff(d2, 
d1).getLongValue());
-        Assert.assertEquals(31559414743L, FEFunctions.timeDiff(d3, 
d1).getLongValue());
-        Assert.assertEquals(2419200, FEFunctions.timeDiff(d3, 
d2).getLongValue());
-    }
-
-    @Test
-    public void timeDiffTest2() throws AnalysisException {
-        DateLiteral d1 = new DateLiteral("1019-02-28 00:00:00", 
Type.DATETIMEV2);
-        DateLiteral d2 = new DateLiteral("2019-02-28 00:00:00", Type.DATETIME);
-        DateLiteral d3 = new DateLiteral("2019-03-28 00:00:00", 
Type.DATETIMEV2);
-        Assert.assertEquals(31556995543L, FEFunctions.timeDiff(d2, 
d1).getLongValue());
-        Assert.assertEquals(31559414743L, FEFunctions.timeDiff(d3, 
d1).getLongValue());
-        Assert.assertEquals(2419200, FEFunctions.timeDiff(d3, 
d2).getLongValue());
-    }
 
     @Test
     public void timeNowTest() throws AnalysisException {
diff --git a/gensrc/script/doris_builtins_functions.py 
b/gensrc/script/doris_builtins_functions.py
index 103a4b2bb1f..970c50beae7 100644
--- a/gensrc/script/doris_builtins_functions.py
+++ b/gensrc/script/doris_builtins_functions.py
@@ -1015,6 +1015,7 @@ visible_functions = {
 
         [['to_days'], 'INT', ['DATEV2'], ''],
         [['time_to_sec'], 'INT', ['TIME'], ''],
+        [['time_to_sec'], 'INT', ['TIMEV2'], ''],
         [['sec_to_time'], 'TIME', ['INT'], ''],
 
         [['year'], 'SMALLINT', ['DATETIMEV2'], ''],
diff --git a/regression-test/data/correctness/test_time_function.out 
b/regression-test/data/correctness/test_time_function.out
index 63548878790..856e6c645cd 100644
--- a/regression-test/data/correctness/test_time_function.out
+++ b/regression-test/data/correctness/test_time_function.out
@@ -44,6 +44,9 @@
 -- !select --
 \N
 
+-- !select --
+604800
+
 -- !select --
 16:32:18
 
@@ -87,4 +90,8 @@
 \N
 
 -- !select --
-\N
\ No newline at end of file
+\N
+
+-- !select --
+604800
+
diff --git a/regression-test/suites/correctness/test_time_function.groovy 
b/regression-test/suites/correctness/test_time_function.groovy
index c008d346da4..7df2dc3e6af 100644
--- a/regression-test/suites/correctness/test_time_function.groovy
+++ b/regression-test/suites/correctness/test_time_function.groovy
@@ -73,6 +73,10 @@ suite("test_time_function") {
         select sec_to_time(time_to_sec(cast("61" as time)));
     """
 
+    qt_select """
+        select time_to_sec(timediff('2024-01-22', '2024-01-15')) as seconds;
+    """
+
     sql """
         set enable_nereids_planner=false
     """
@@ -130,4 +134,9 @@ suite("test_time_function") {
     qt_select """
         select sec_to_time(time_to_sec(cast("61" as time)));
     """
+    
+    qt_select """
+        select time_to_sec(timediff('2024-01-22', '2024-01-15')) as seconds;
+    """
+
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to