You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2022/05/19 13:24:49 UTC

[incubator-doris] branch master updated: [Bug] Fix timestamp_diff issue when timeunit is year and month (#9574)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0f9ef26576 [Bug] Fix timestamp_diff issue when timeunit is year and month (#9574)
0f9ef26576 is described below

commit 0f9ef265762a5b0392882f0bad03cd5df4df5bb1
Author: huangzhaowei <ca...@gmail.com>
AuthorDate: Thu May 19 21:24:43 2022 +0800

    [Bug] Fix timestamp_diff issue when timeunit is year and month (#9574)
---
 be/src/runtime/datetime_value.h                    | 14 +++--
 be/src/vec/runtime/vdatetime_value.h               | 14 +++--
 .../datetime_functions/test_timestampdiff.out      | 24 ++++++++
 .../datetime_functions/test_timestampdiff.groovy   | 66 ++++++++++++++++++++++
 4 files changed, 106 insertions(+), 12 deletions(-)

diff --git a/be/src/runtime/datetime_value.h b/be/src/runtime/datetime_value.h
index a970e615c1..21ed59b2d1 100644
--- a/be/src/runtime/datetime_value.h
+++ b/be/src/runtime/datetime_value.h
@@ -288,11 +288,11 @@ public:
         case YEAR: {
             int year = (ts_value2.year() - ts_value1.year());
             if (year > 0) {
-                year -= (ts_value2.to_int64() % 10000000000 - ts_value1.to_int64() % 10000000000) <
-                        0;
+                year -= (ts_value2.to_datetime_int64() % 10000000000 -
+                         ts_value1.to_datetime_int64() % 10000000000) < 0;
             } else if (year < 0) {
-                year += (ts_value2.to_int64() % 10000000000 - ts_value1.to_int64() % 10000000000) >
-                        0;
+                year += (ts_value2.to_datetime_int64() % 10000000000 -
+                         ts_value1.to_datetime_int64() % 10000000000) > 0;
             }
             return year;
         }
@@ -300,9 +300,11 @@ public:
             int month = (ts_value2.year() - ts_value1.year()) * 12 +
                         (ts_value2.month() - ts_value1.month());
             if (month > 0) {
-                month -= (ts_value2.to_int64() % 100000000 - ts_value1.to_int64() % 100000000) < 0;
+                month -= (ts_value2.to_datetime_int64() % 100000000 -
+                          ts_value1.to_datetime_int64() % 100000000) < 0;
             } else if (month < 0) {
-                month += (ts_value2.to_int64() % 100000000 - ts_value1.to_int64() % 100000000) > 0;
+                month += (ts_value2.to_datetime_int64() % 100000000 -
+                          ts_value1.to_datetime_int64() % 100000000) > 0;
             }
             return month;
         }
diff --git a/be/src/vec/runtime/vdatetime_value.h b/be/src/vec/runtime/vdatetime_value.h
index 3f2fa3a24b..126b36f5b4 100644
--- a/be/src/vec/runtime/vdatetime_value.h
+++ b/be/src/vec/runtime/vdatetime_value.h
@@ -288,11 +288,11 @@ public:
         case YEAR: {
             int year = (ts_value2.year() - ts_value1.year());
             if (year > 0) {
-                year -= (ts_value2.to_int64() % 10000000000 - ts_value1.to_int64() % 10000000000) <
-                        0;
+                year -= (ts_value2.to_datetime_int64() % 10000000000 -
+                         ts_value1.to_datetime_int64() % 10000000000) < 0;
             } else if (year < 0) {
-                year += (ts_value2.to_int64() % 10000000000 - ts_value1.to_int64() % 10000000000) >
-                        0;
+                year += (ts_value2.to_datetime_int64() % 10000000000 -
+                         ts_value1.to_datetime_int64() % 10000000000) > 0;
             }
             return year;
         }
@@ -300,9 +300,11 @@ public:
             int month = (ts_value2.year() - ts_value1.year()) * 12 +
                         (ts_value2.month() - ts_value1.month());
             if (month > 0) {
-                month -= (ts_value2.to_int64() % 100000000 - ts_value1.to_int64() % 100000000) < 0;
+                month -= (ts_value2.to_datetime_int64() % 100000000 -
+                          ts_value1.to_datetime_int64() % 100000000) < 0;
             } else if (month < 0) {
-                month += (ts_value2.to_int64() % 100000000 - ts_value1.to_int64() % 100000000) > 0;
+                month += (ts_value2.to_datetime_int64() % 100000000 -
+                          ts_value1.to_datetime_int64() % 100000000) > 0;
             }
             return month;
         }
diff --git a/regression-test/data/query/sql_functions/datetime_functions/test_timestampdiff.out b/regression-test/data/query/sql_functions/datetime_functions/test_timestampdiff.out
new file mode 100644
index 0000000000..d991bb42ce
--- /dev/null
+++ b/regression-test/data/query/sql_functions/datetime_functions/test_timestampdiff.out
@@ -0,0 +1,24 @@
+-- This file is automatically generated. You should know what you did if you want to edit this
+-- !select --
+40	40	40	40
+
+-- !select --
+40	40	41	41
+
+-- !select --
+24	24	24	24
+
+-- !select --
+23	23	23	23
+
+-- !select --
+40	40	40	40
+
+-- !select --
+40	40	41	41
+
+-- !select --
+24	24	24	24
+
+-- !select --
+23	23	23	23
diff --git a/regression-test/suites/query/sql_functions/datetime_functions/test_timestampdiff.groovy b/regression-test/suites/query/sql_functions/datetime_functions/test_timestampdiff.groovy
new file mode 100644
index 0000000000..8f7e3ee48f
--- /dev/null
+++ b/regression-test/suites/query/sql_functions/datetime_functions/test_timestampdiff.groovy
@@ -0,0 +1,66 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("test_timestampdiff", "query") {
+    // non vectorized
+    sql """ set enable_vectorized_engine = false """
+
+    qt_select """SELECT TIMESTAMPDIFF(YEAR,DATE('1981-09-11'),'2022-04-28') AS `date-str`,
+                        TIMESTAMPDIFF(YEAR,'1981-09-11','2022-04-28') AS `str-str`,
+                        TIMESTAMPDIFF(YEAR,DATE('1981-09-11'),DATE('2022-04-28')) AS `date-date`,
+                        TIMESTAMPDIFF(YEAR,'1981-09-11',DATE('2022-04-28')) AS `str-date`"""
+
+    qt_select """SELECT TIMESTAMPDIFF(YEAR,DATE('1981-09-11'),'2022-04-28') AS `date-str`,
+                        TIMESTAMPDIFF(YEAR,'1981-09-11','2022-04-28') AS `str-str`,
+                        TIMESTAMPDIFF(YEAR,DATE('1981-04-11'),DATE('2022-04-28')) AS `date-date`,
+                        TIMESTAMPDIFF(YEAR,'1981-04-11',DATE('2022-04-28')) AS `str-date`"""
+
+
+    qt_select """SELECT TIMESTAMPDIFF(MONTH,DATE('2020-04-27'),'2022-04-28') AS `date-str`,
+                        TIMESTAMPDIFF(MONTH,'2020-04-27','2022-04-28') AS `str-str`,
+                        TIMESTAMPDIFF(MONTH,DATE('2020-04-27'),DATE('2022-04-28')) AS `date-date`,
+                        TIMESTAMPDIFF(MONTH,'2020-04-27',DATE('2022-04-28')) AS `str-date`"""
+
+    qt_select """SELECT TIMESTAMPDIFF(MONTH,DATE('2020-04-29'),'2022-04-28') AS `date-str`,
+                        TIMESTAMPDIFF(MONTH,'2020-04-29','2022-04-28') AS `str-str`,
+                        TIMESTAMPDIFF(MONTH,DATE('2020-04-29'),DATE('2022-04-28')) AS `date-date`,
+                        TIMESTAMPDIFF(MONTH,'2020-04-29',DATE('2022-04-28')) AS `str-date`"""
+
+    // vectorized
+    sql """ set enable_vectorized_engine = true """
+
+    qt_select """SELECT TIMESTAMPDIFF(YEAR,DATE('1981-09-11'),'2022-04-28') AS `date-str`,
+                        TIMESTAMPDIFF(YEAR,'1981-09-11','2022-04-28') AS `str-str`,
+                        TIMESTAMPDIFF(YEAR,DATE('1981-09-11'),DATE('2022-04-28')) AS `date-date`,
+                        TIMESTAMPDIFF(YEAR,'1981-09-11',DATE('2022-04-28')) AS `str-date`"""
+
+    qt_select """SELECT TIMESTAMPDIFF(YEAR,DATE('1981-09-11'),'2022-04-28') AS `date-str`,
+                        TIMESTAMPDIFF(YEAR,'1981-09-11','2022-04-28') AS `str-str`,
+                        TIMESTAMPDIFF(YEAR,DATE('1981-04-11'),DATE('2022-04-28')) AS `date-date`,
+                        TIMESTAMPDIFF(YEAR,'1981-04-11',DATE('2022-04-28')) AS `str-date`"""
+
+
+    qt_select """SELECT TIMESTAMPDIFF(MONTH,DATE('2020-04-27'),'2022-04-28') AS `date-str`,
+                        TIMESTAMPDIFF(MONTH,'2020-04-27','2022-04-28') AS `str-str`,
+                        TIMESTAMPDIFF(MONTH,DATE('2020-04-27'),DATE('2022-04-28')) AS `date-date`,
+                        TIMESTAMPDIFF(MONTH,'2020-04-27',DATE('2022-04-28')) AS `str-date`"""
+
+    qt_select """SELECT TIMESTAMPDIFF(MONTH,DATE('2020-04-29'),'2022-04-28') AS `date-str`,
+                        TIMESTAMPDIFF(MONTH,'2020-04-29','2022-04-28') AS `str-str`,
+                        TIMESTAMPDIFF(MONTH,DATE('2020-04-29'),DATE('2022-04-28')) AS `date-date`,
+                        TIMESTAMPDIFF(MONTH,'2020-04-29',DATE('2022-04-28')) AS `str-date`"""
+}


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