Skip to content

Commit

Permalink
[fix](function) fix error result in time_to_sec and timediff (apache#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mryange authored and caiconghui1 committed Feb 22, 2024
1 parent 3cf6b73 commit 0ad2757
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'], ''],
Expand Down
9 changes: 8 additions & 1 deletion regression-test/data/correctness/test_time_function.out
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
-- !select --
\N

-- !select --
604800

-- !select --
16:32:18

Expand Down Expand Up @@ -87,4 +90,8 @@
\N

-- !select --
\N
\N

-- !select --
604800

9 changes: 9 additions & 0 deletions regression-test/suites/correctness/test_time_function.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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;
"""

}

0 comments on commit 0ad2757

Please sign in to comment.