Skip to content

Commit

Permalink
Fix from_unixtime for NaN input (#2936)
Browse files Browse the repository at this point in the history
Summary:
from_unixtime(NaN, '<timezone>') should return zero. Before the fix it was crashing under UBSAN:

```
velox/functions/prestosql/FromUnixTime.cpp:24:10: runtime error: nan is outside the range of representable values of type 'long'

 UndefinedBehaviorSanitizer: float-cast-overflow velox/functions/prestosql/FromUnixTime.cpp:24:10 in
```

Fixes #2791

Pull Request resolved: #2936

Reviewed By: xiaoxmeng

Differential Revision: D40648988

Pulled By: mbasmanova

fbshipit-source-id: 752068a1e17da540f031c98367bd9a4baaa6a604
  • Loading branch information
mbasmanova authored and facebook-github-bot committed Oct 24, 2022
1 parent fdd27d9 commit 1ae9dc0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions velox/functions/prestosql/FromUnixTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace facebook::velox::functions {
namespace {

inline int64_t toMillis(double unixtime) {
if (UNLIKELY(std::isnan(unixtime))) {
return 0;
}
return std::floor(unixtime * 1'000);
}

Expand Down
27 changes: 27 additions & 0 deletions velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ TEST_F(DateTimeFunctionsTest, fromUnixtimeRountTrip) {
}

TEST_F(DateTimeFunctionsTest, fromUnixtimeWithTimeZone) {
static const double kNan = std::numeric_limits<double>::quiet_NaN();

vector_size_t size = 37;

auto unixtimeAt = [](vector_size_t row) -> double {
Expand All @@ -295,6 +297,17 @@ TEST_F(DateTimeFunctionsTest, fromUnixtimeWithTimeZone) {
makeConstant((int16_t)900, size),
});
assertEqualVectors(expected, result);

// NaN timestamp.
result = evaluate<RowVector>(
"from_unixtime(c0, '+01:00')",
makeRowVector({makeFlatVector<double>({kNan, kNan})}));
ASSERT_TRUE(isTimestampWithTimeZoneType(result->type()));
expected = makeRowVector({
makeFlatVector<int64_t>({0, 0}),
makeFlatVector<int16_t>({900, 900}),
});
assertEqualVectors(expected, result);
}

// Variable timezone parameter.
Expand All @@ -317,6 +330,20 @@ TEST_F(DateTimeFunctionsTest, fromUnixtimeWithTimeZone) {
size, [&](auto row) { return timezoneIds[row % 5]; }),
});
assertEqualVectors(expected, result);

// NaN timestamp.
result = evaluate<RowVector>(
"from_unixtime(c0, c1)",
makeRowVector({
makeFlatVector<double>({kNan, kNan}),
makeNullableFlatVector<StringView>({"+01:00", "+02:00"}),
}));
ASSERT_TRUE(isTimestampWithTimeZoneType(result->type()));
expected = makeRowVector({
makeFlatVector<int64_t>({0, 0}),
makeFlatVector<int16_t>({900, 960}),
});
assertEqualVectors(expected, result);
}
}

Expand Down

0 comments on commit 1ae9dc0

Please sign in to comment.