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 26fb544e20ee31..d79c9ce7642d03 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.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 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 686db80d81ef4a..1a5e30b8b88841 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 @@ -57,12 +57,12 @@ public static StringLiteral version() throws AnalysisException { /** * 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 9f444db9fa88ad..727a6635620d18 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 void divideDecimalV2Test() throws AnalysisException { 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 be0fb5fbd6a480..d94d26046d1cc0 100644 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -965,6 +965,7 @@ [['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 635488787909dc..856e6c645cd8cc 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 c008d346da4436..7df2dc3e6af301 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