Skip to content

Commit

Permalink
Fix overflow for min calculation in strings::from_timestamps (#9793)
Browse files Browse the repository at this point in the history
This fixes #9790 

When converting a timestamp to a String it is possible for the %M min calculation to overflow an int32_t part way through casting. This moves that result to be an int64_t which avoids the overflow issues.

Authors:
  - Robert (Bobby) Evans (https://github.com/revans2)

Approvers:
  - Bradley Dice (https://github.com/bdice)
  - David Wendt (https://github.com/davidwendt)

URL: #9793
  • Loading branch information
revans2 authored Dec 1, 2021
1 parent 11c3dfe commit 1904d1a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cpp/src/strings/convert/convert_datetime.cu
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,9 @@ struct from_timestamp_base {
* scale( 61,60) -> 1
* @endcode
*/
__device__ int32_t scale_time(int64_t time, int64_t base) const
__device__ int64_t scale_time(int64_t time, int64_t base) const
{
return static_cast<int32_t>((time - ((time < 0) * (base - 1L))) / base);
return (time - ((time < 0) * (base - 1L))) / base;
};

__device__ time_components get_time_components(int64_t tstamp) const
Expand Down
8 changes: 5 additions & 3 deletions cpp/tests/strings/datetime_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,21 +311,23 @@ TEST_F(StringsDatetimeTest, FromTimestampAmPm)
TEST_F(StringsDatetimeTest, FromTimestampMillisecond)
{
cudf::test::fixed_width_column_wrapper<cudf::timestamp_ms, cudf::timestamp_ms::rep> timestamps_ms{
1530705600123, 1582934461007, 1451430122421, 1318302183999, -6106017600047};
1530705600123, 1582934461007, 1451430122421, 1318302183999, -6106017600047, 128849018880000};
auto results = cudf::strings::from_timestamps(timestamps_ms, "%Y-%m-%d %H:%M:%S.%3f");
cudf::test::strings_column_wrapper expected_ms{"2018-07-04 12:00:00.123",
"2020-02-29 00:01:01.007",
"2015-12-29 23:02:02.421",
"2011-10-11 03:03:03.999",
"1776-07-04 11:59:59.953"};
"1776-07-04 11:59:59.953",
"6053-01-23 02:08:00.000"};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected_ms);

results = cudf::strings::from_timestamps(timestamps_ms, "%Y-%m-%d %H:%M:%S.%f");
cudf::test::strings_column_wrapper expected_ms_6f{"2018-07-04 12:00:00.123000",
"2020-02-29 00:01:01.007000",
"2015-12-29 23:02:02.421000",
"2011-10-11 03:03:03.999000",
"1776-07-04 11:59:59.953000"};
"1776-07-04 11:59:59.953000",
"6053-01-23 02:08:00.000000"};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected_ms_6f);

cudf::test::fixed_width_column_wrapper<cudf::timestamp_ns, cudf::timestamp_ns::rep> timestamps_ns{
Expand Down

0 comments on commit 1904d1a

Please sign in to comment.