Merge pull request #58557 from yariks5s/to_start_of_interval_bug_fix

toStartOfInterval for milli- microsencods values rounding
This commit is contained in:
Ilya Yatsishin 2024-01-12 20:39:27 +04:00 committed by GitHub
commit 2c86b7ae6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 7 deletions

View File

@ -537,8 +537,8 @@ struct ToStartOfInterval<IntervalKind::Microsecond>
else if (scale_multiplier > 1000000)
{
Int64 scale_diff = scale_multiplier / static_cast<Int64>(1000000);
if (t >= 0) [[likely]]
return t / microseconds / scale_diff * microseconds;
if (t >= 0) [[likely]] /// When we divide the `t` value we should round the result
return (t / microseconds + scale_diff / 2) / scale_diff * microseconds;
else
return ((t + 1) / microseconds / scale_diff - 1) * microseconds;
}
@ -580,8 +580,8 @@ struct ToStartOfInterval<IntervalKind::Millisecond>
else if (scale_multiplier > 1000)
{
Int64 scale_diff = scale_multiplier / static_cast<Int64>(1000);
if (t >= 0) [[likely]]
return t / milliseconds / scale_diff * milliseconds;
if (t >= 0) [[likely]] /// When we divide the `t` value we should round the result
return (t / milliseconds + scale_diff / 2) / scale_diff * milliseconds;
else
return ((t + 1) / milliseconds / scale_diff - 1) * milliseconds;
}

View File

@ -10,14 +10,14 @@ test intervals
- test microseconds
1980-12-12 12:12:12.123456
1980-12-12 12:12:12.123400
1980-12-12 12:12:12.123456
1980-12-12 12:12:12.123456
1980-12-12 12:12:12.123457
1980-12-12 12:12:12.123457
1930-12-12 12:12:12.123456
1930-12-12 12:12:12.123400
1930-12-12 12:12:12.123456
2220-12-12 12:12:12.123456
2220-12-12 12:12:12.123400
2220-12-12 12:12:12.123456
2220-12-12 12:12:12.123457
- test milliseconds
1980-12-12 12:12:12.123
1980-12-12 12:12:12.120

View File

@ -0,0 +1,4 @@
2023-10-09 10:11:12.001
2023-10-09 10:11:12.001
2023-10-09 10:11:12.000
2023-10-09 10:11:12.000

View File

@ -0,0 +1,4 @@
SELECT toStartOfInterval(toDateTime64('2023-10-09 10:11:12.000999', 6), toIntervalMillisecond(1));
SELECT toStartOfInterval(toDateTime64('2023-10-09 10:11:12.000500', 6), toIntervalMillisecond(1));
SELECT toStartOfInterval(toDateTime64('2023-10-09 10:11:12.000499', 6), toIntervalMillisecond(1));
SELECT toStartOfInterval(toDateTime64('2023-10-09 10:11:12.000999', 6), toIntervalMillisecond(10));