Merge pull request #60763 from zvonand/zvonand-fix-toStartOf

Fix toStartOfInterval
This commit is contained in:
Yarik Briukhovetskyi 2024-03-06 13:55:21 +01:00 committed by GitHub
commit 2eb7fa297b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 3 deletions

View File

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

View File

@ -2,3 +2,6 @@
2023-10-09 10:11:12.001
2023-10-09 10:11:12.000
2023-10-09 10:11:12.000
2023-10-09 00:00:00.000000
2023-10-09 00:00:00.000
2023-10-09 00:00:00

View File

@ -2,3 +2,6 @@ SELECT toStartOfInterval(toDateTime64('2023-10-09 10:11:12.000999', 6), toInterv
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));
select toStartOfInterval(toDateTime64('2023-10-09 00:01:34', 9), toIntervalMicrosecond(100000000));
select toStartOfInterval(toDateTime64('2023-10-09 00:01:34', 9), toIntervalMillisecond(100000));
select toStartOfInterval(toDateTime64('2023-10-09 00:01:34', 9), toIntervalSecond(100));