Skip to content

Commit

Permalink
fix: Parsing of fractions of a second in parse_datetime() (#11723)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #11723

A bug causes function to return wrong fractions of a second in some cases.

Reviewed By: gggrace14

Differential Revision: D66688586

fbshipit-source-id: 3a2308026c5484f9ec4f87679ec9cae3512d8fb4
  • Loading branch information
Sergey Pershin authored and facebook-github-bot committed Dec 3, 2024
1 parent a0bbea2 commit a94e870
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 1 addition & 3 deletions velox/functions/lib/DateTimeFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,7 @@ int32_t parseFromPattern(
int count = 0;
while (cur < end && cur < startPos + maxDigitConsume &&
characterIsDigit(*cur)) {
if (count < 3) {
number = number * 10 + (*cur - '0');
}
number = number * 10 + (*cur - '0');
++cur;
++count;
}
Expand Down
36 changes: 36 additions & 0 deletions velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3185,6 +3185,42 @@ TEST_F(DateTimeFunctionsTest, dateDiffTimestampWithTimezone) {
"2023-03-11 00:00:00 America/Los_Angeles"));
}

TEST_F(DateTimeFunctionsTest, parseDatetimeRoundtrip) {
const auto parseDatetimeRoundTrip =
[&](const std::optional<std::string>& input,
const std::optional<std::string>& format) {
return evaluateOnce<std::string>(
"cast(parse_datetime(c0, c1) as varchar)", input, format);
};

EXPECT_EQ(
"2024-01-20 01:00:30.127 UTC",
parseDatetimeRoundTrip(
"2024-01-20 01:00:30.12700", "yyyy-MM-dd HH:mm:ss.SSSSS"));
EXPECT_EQ(
"2024-01-20 01:00:30.459 UTC",
parseDatetimeRoundTrip(
"2024-01-20 01:00:30.45900000", "yyyy-MM-dd HH:mm:ss.SSSSSSSS"));
EXPECT_EQ(
"2024-01-20 01:00:30.617 UTC",
parseDatetimeRoundTrip(
"2024-01-20 01:00:30.6170", "yyyy-MM-dd HH:mm:ss.SSSSSSSS"));

EXPECT_EQ(
"2024-01-20 01:00:30.127 UTC",
parseDatetimeRoundTrip(
"2024-01-20 01:00:30.127149", "yyyy-MM-dd HH:mm:ss.SSSSSS"));
EXPECT_EQ(
"2024-01-20 01:00:30.127 UTC",
parseDatetimeRoundTrip(
"2024-01-20 01:00:30.127941", "yyyy-MM-dd HH:mm:ss.SSSSSS"));

VELOX_ASSERT_THROW(
parseDatetimeRoundTrip(
"2024-01-20 01:00:30.6170", "yyyy-MM-dd HH:mm:ss.SSS"),
"Invalid date format");
}

TEST_F(DateTimeFunctionsTest, parseDatetime) {
const auto parseDatetime = [&](const std::optional<std::string>& input,
const std::optional<std::string>& format) {
Expand Down

0 comments on commit a94e870

Please sign in to comment.