Skip to content

Commit

Permalink
2
Browse files Browse the repository at this point in the history
  • Loading branch information
zclllyybb committed Mar 27, 2024
1 parent 4ac0c90 commit 054a037
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
14 changes: 11 additions & 3 deletions be/src/vec/functions/function_timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@ struct UnixTimeStampImpl {
return (Int32)timestamp;
}

static std::pair<Int32, Int32> trim_timestamp(std::pair<Int64, Int64> timestamp) {
if (timestamp.first < 0 || timestamp.first > INT_MAX) {
return {0, 0};
}
return std::make_pair((Int32)timestamp.first, (Int32)timestamp.second);
}

static DataTypes get_variadic_argument_types() { return {}; }

static DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) {
Expand Down Expand Up @@ -846,16 +853,17 @@ struct UnixTimeStampStrImpl {

std::pair<int64_t, int64_t> timestamp {};
if (!ts_value.unix_timestamp(&timestamp, context->state()->timezone_obj())) {
null_map_data[i] = true;
null_map_data[i] = true; // impossible now
} else {
null_map_data[i] = false;

auto& [sec, ms] = timestamp;
sec = UnixTimeStampImpl::trim_timestamp(sec);
auto [sec, ms] = UnixTimeStampImpl::trim_timestamp(timestamp);
// trailing ms
auto ms_str = std::to_string(ms).substr(0, 6);
if (ms_str.empty()) {
ms_str = "0";
}

col_result_data[i] = Decimal64::from_int_frac(sec, std::stoll(ms_str), 6).value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion be/src/vec/runtime/vdatetime_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3265,7 +3265,7 @@ bool DateV2Value<T>::unix_timestamp(std::pair<int64_t, int64_t>* timestamp,
ctz);
timestamp->first = tp.time_since_epoch().count();
timestamp->second = date_v2_value_.microsecond_;
} else {
} else { // just make compiler happy
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,17 +687,35 @@ true
1694966400.000000 1694966400.000000

-- !sql_varchar1 --
1605024000.000000
1607702400.000000
1607788800.000000
0000-00-00 %Y-%m-%d \N
9999-12-31 23:59:59.999999 %Y-%m-%d %H:%i:%s.%f 0.000000
9999-12-31 23:59:59.9999999 %Y-%m-%d %H:%i:%s.%f 0.000000
0000-01-01 %Y-%m-%d 0.000000
9999-12-31 23:59:59 %Y-%m-%d %H:%i:%s 0.000000
1999-12-31 23:59:59.9999999 %Y-%m-%d %H:%i:%s.%f 946655999.999999
20201111 %Y%m%d 1605024000.000000
2020-12-12 %Y-%m-%d 1607702400.000000
202012-13 %Y%m-%d 1607788800.000000

-- !sql_varchar1 --
\N
\N
1607702400.000000
20201111 \N
0000-00-00 \N
202012-13 \N
0000-01-01 0.000000
9999-12-31 23:59:59.9999999 0.000000
9999-12-31 23:59:59.999999 0.000000
9999-12-31 23:59:59 0.000000
1999-12-31 23:59:59.9999999 946569600.000000
2020-12-12 1607702400.000000

-- !sql_varchar1 --
\N
\N
660931200.000000
%Y%m-%d \N
%Y%m%d \N
%Y-%m-%d 660931200.000000
%Y-%m-%d %H:%i:%s.%f 660931200.000000
%Y-%m-%d %H:%i:%s.%f 660931200.000000
%Y-%m-%d 660931200.000000
%Y-%m-%d 660931200.000000
%Y-%m-%d %H:%i:%s.%f 660931200.000000
%Y-%m-%d %H:%i:%s 660931200.000000

Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ suite("test_date_function") {
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('20230918', '%Y%m%d'), 'yyyy-MM-dd HH:mm:ss') AS `a`
)t """

sql """ drop table if exists date_varchar """
sql """
create table date_varchar(
dt varchar null,
Expand All @@ -743,8 +744,11 @@ suite("test_date_function") {
DISTRIBUTED BY HASH(`dt`) BUCKETS 1
properties("replication_num" = "1");
"""
sql """ insert into date_varchar values ("2020-12-12", "%Y-%m-%d"), ("20201111", "%Y%m%d"), ("202012-13", "%Y%m-%d"); """
qt_sql_varchar1 """ select unix_timestamp(dt, fmt) as k1 from date_varchar order by k1; """
qt_sql_varchar1 """ select unix_timestamp(dt, "%Y-%m-%d") as k1 from date_varchar order by k1; """
qt_sql_varchar1 """ select unix_timestamp("1990-12-12", fmt) as k1 from date_varchar order by k1; """
sql """ insert into date_varchar values ("2020-12-12", "%Y-%m-%d"), ("20201111", "%Y%m%d"), ("202012-13", "%Y%m-%d"),
("0000-00-00", "%Y-%m-%d"),("0000-01-01", "%Y-%m-%d"),("9999-12-31 23:59:59", "%Y-%m-%d %H:%i:%s"),
("9999-12-31 23:59:59.999999", "%Y-%m-%d %H:%i:%s.%f"), ("9999-12-31 23:59:59.9999999", "%Y-%m-%d %H:%i:%s.%f"),
("1999-12-31 23:59:59.9999999", "%Y-%m-%d %H:%i:%s.%f"); """
qt_sql_varchar1 """ select dt, fmt, unix_timestamp(dt, fmt) as k1 from date_varchar order by k1; """
qt_sql_varchar1 """ select dt, unix_timestamp(dt, "%Y-%m-%d") as k1 from date_varchar order by k1; """
qt_sql_varchar1 """ select fmt, unix_timestamp("1990-12-12", fmt) as k1 from date_varchar order by k1; """
}

0 comments on commit 054a037

Please sign in to comment.