From b41a13457139b0e4531ba446f28a33db7e2227a6 Mon Sep 17 00:00:00 2001 From: LiBinfeng <1204975323@qq.com> Date: Thu, 17 Oct 2024 16:05:34 +0800 Subject: [PATCH 1/2] [Fix](Nereids) fix date and date time arithmatic --- .../executable/DateTimeArithmetic.java | 40 +++++++++++++++++++ .../fold_constant/fold_constant_by_fe.groovy | 2 + 2 files changed, 42 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeArithmetic.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeArithmetic.java index 033bff2afd33dd..54add9565ca9b6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeArithmetic.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeArithmetic.java @@ -174,6 +174,16 @@ public static Expression daysAdd(DateTimeV2Literal date, IntegerLiteral day) { /** * datetime arithmetic function hours-add. */ + @ExecFunction(name = "hours_add", argTypes = {"DATE", "INT"}, returnType = "DATE") + public static Expression hoursAdd(DateLiteral date, IntegerLiteral hour) { + return date.toBeginOfTheDay().plusHours(hour.getValue()); + } + + @ExecFunction(name = "hours_add", argTypes = {"DATEV2", "INT"}, returnType = "DATEV2") + public static Expression hoursAdd(DateV2Literal date, IntegerLiteral hour) { + return date.toBeginOfTheDay().plusHours(hour.getValue()); + } + @ExecFunction(name = "hours_add", argTypes = {"DATETIME", "INT"}, returnType = "DATETIME") public static Expression hoursAdd(DateTimeLiteral date, IntegerLiteral hour) { return date.plusHours(hour.getValue()); @@ -187,6 +197,16 @@ public static Expression hoursAdd(DateTimeV2Literal date, IntegerLiteral hour) { /** * datetime arithmetic function minutes-add. */ + @ExecFunction(name = "minutes_add", argTypes = {"DATE", "INT"}, returnType = "DATE") + public static Expression minutesAdd(DateLiteral date, IntegerLiteral minute) { + return date.toBeginOfTheDay().plusMinutes(minute.getValue()); + } + + @ExecFunction(name = "minutes_add", argTypes = {"DATEV2", "INT"}, returnType = "DATEV2") + public static Expression minutesAdd(DateV2Literal date, IntegerLiteral minute) { + return date.toBeginOfTheDay().plusMinutes(minute.getValue()); + } + @ExecFunction(name = "minutes_add", argTypes = {"DATETIME", "INT"}, returnType = "DATETIME") public static Expression minutesAdd(DateTimeLiteral date, IntegerLiteral minute) { return date.plusMinutes(minute.getValue()); @@ -200,6 +220,16 @@ public static Expression minutesAdd(DateTimeV2Literal date, IntegerLiteral minut /** * datetime arithmetic function seconds-add. */ + @ExecFunction(name = "seconds_add", argTypes = {"DATE", "INT"}, returnType = "DATE") + public static Expression secondsAdd(DateLiteral date, IntegerLiteral second) { + return date.toBeginOfTheDay().plusSeconds(second.getValue()); + } + + @ExecFunction(name = "seconds_add", argTypes = {"DATEV2", "INT"}, returnType = "DATEV2") + public static Expression secondsAdd(DateV2Literal date, IntegerLiteral second) { + return date.toBeginOfTheDay().plusSeconds(second.getValue()); + } + @ExecFunction(name = "seconds_add", argTypes = {"DATETIME", "INT"}, returnType = "DATETIME") public static Expression secondsAdd(DateTimeLiteral date, IntegerLiteral second) { return date.plusSeconds(second.getValue()); @@ -380,4 +410,14 @@ public static Expression dateDiff(DateTimeV2Literal date1, DateTimeV2Literal dat private static int dateDiff(LocalDateTime date1, LocalDateTime date2) { return ((int) ChronoUnit.DAYS.between(date2.toLocalDate(), date1.toLocalDate())); } + + @ExecFunction(name = "to_days", argTypes = {"DATE"}, returnType = "INT") + public static Expression toDays(DateLiteral date) { + return new IntegerLiteral((int) date.getDay()); + } + + @ExecFunction(name = "to_days", argTypes = {"DATEV2"}, returnType = "INT") + public static Expression toDays(DateV2Literal date) { + return new IntegerLiteral((int) date.getDay()); + } } diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy index 33b267f8f752d4..1d7fd7d397614d 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy @@ -161,4 +161,6 @@ suite("test_fold_constant_by_fe") { res = sql """explain select "12" like '%123%'""" assertTrue(res.contains("like")) + testFoldConst("select DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY) + INTERVAL 3600 SECOND") + } From 9f70d0d9790d7169cb5bfcb243d8214f125ce1e3 Mon Sep 17 00:00:00 2001 From: LiBinfeng <1204975323@qq.com> Date: Mon, 21 Oct 2024 11:34:18 +0800 Subject: [PATCH 2/2] fix p0 --- .../expression/fold_constant/fold_constant_by_fe.groovy | 2 -- 1 file changed, 2 deletions(-) diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy index 1d7fd7d397614d..33b267f8f752d4 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy @@ -161,6 +161,4 @@ suite("test_fold_constant_by_fe") { res = sql """explain select "12" like '%123%'""" assertTrue(res.contains("like")) - testFoldConst("select DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY) + INTERVAL 3600 SECOND") - }