From 8e3569ed9c0e7309edf64071fce2246356603c99 Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Mon, 31 Mar 2025 12:08:36 +0800 Subject: [PATCH] fix unit_timestamp --- .../executable/DateTimeExtractAndTransform.java | 8 +++++--- .../fold_constant_date_arithmatic.groovy | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java index e01896758eae91..5430efc43ce17c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java @@ -607,14 +607,16 @@ public static Expression unixTimestamp(StringLikeLiteral date, StringLikeLiteral private static String getTimestamp(LocalDateTime dateTime) { LocalDateTime specialUpperBound = LocalDateTime.of(2038, 1, 19, 3, 14, 7); LocalDateTime specialLowerBound = LocalDateTime.of(1970, 1, 1, 0, 0, 0); + dateTime = dateTime.atZone(DateUtils.getTimeZone()) + .toOffsetDateTime().atZoneSameInstant(ZoneId.of("UTC+0")) + .toLocalDateTime(); if (dateTime.isBefore(specialLowerBound) || dateTime.isAfter(specialUpperBound)) { return "0"; } Duration duration = Duration.between( specialLowerBound, - dateTime.atZone(DateUtils.getTimeZone()) - .toOffsetDateTime().atZoneSameInstant(ZoneId.of("UTC+0")) - .toLocalDateTime()); + dateTime + ); if (duration.getNano() == 0) { return String.valueOf(duration.getSeconds()); } else { diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy index a78067259dd251..5bd4084212e2c5 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy @@ -78,4 +78,21 @@ suite("fold_constant_date_arithmatic") { testFoldConst("select next_day('2020-02-29', 'MON');") // 2020-03-02 (leap year to next month) testFoldConst("select next_day('2019-02-26', 'THU');") // 2019-02-28 (non-leap year) testFoldConst("select next_day('2019-02-28', 'SUN');") // 2019-03-03 (non-leap year to next month) + + // test unix_timestamp + testFoldConst("select unix_timestamp('2023/04/31');") + testFoldConst("select unix_timestamp('1970/01/01 00:00:00');") + testFoldConst("select unix_timestamp('1970-01-01T00:00:00');") + testFoldConst("select unix_timestamp('1970-01-01');") + testFoldConst("select unix_timestamp('31/Apr/2023','%d/%b/%Y');") + testFoldConst("select unix_timestamp('00-00-0000');") + testFoldConst("select unix_timestamp('3000/02/29','%Y/%m/%d');") + testFoldConst("select unix_timestamp('01.Jan.1970','%d.%b.%Y');") + testFoldConst("select unix_timestamp('0000-00-00 00:00:00');") + testFoldConst("select unix_timestamp();") + testFoldConst("select unix_timestamp('2021-02-29', '%Y-%m-%d');") + testFoldConst("select unix_timestamp('2023/04/31', '%Y/%m/%d');") + testFoldConst("select unix_timestamp('2023-04-31 12:00:00');") + testFoldConst("select unix_timestamp('1970-01-01','%Y-%m-%d');") + testFoldConst("select unix_timestamp('0');") }