Skip to content

Commit

Permalink
[Bug] Fix timestamp_diff issue when timeunit is year and month (#9574)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintBacchus authored May 19, 2022
1 parent 73c4ec7 commit 0f9ef26
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 12 deletions.
14 changes: 8 additions & 6 deletions be/src/runtime/datetime_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,23 @@ class DateTimeValue {
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;
}
case MONTH: {
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;
}
Expand Down
14 changes: 8 additions & 6 deletions be/src/vec/runtime/vdatetime_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,23 @@ class VecDateTimeValue { // Now this type is a temp solution with little changes
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;
}
case MONTH: {
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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`"""
}

0 comments on commit 0f9ef26

Please sign in to comment.