Skip to content

Commit

Permalink
Merge pull request ClickHouse#51290 from arenadata/Fix-formatDateTime…
Browse files Browse the repository at this point in the history
…-with-fractional-negative-datetime64

Fix formatDateTime() with fractional negative datetime64
  • Loading branch information
pufit authored Jul 11, 2023
2 parents ea7ff2f + 2a7bbf5 commit 12432ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Functions/formatDateTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,16 @@ class FunctionFormatDateTimeImpl : public IFunction
{
if constexpr (std::is_same_v<DataType, DataTypeDateTime64>)
{
const auto c = DecimalUtils::split(vec[i], scale);
auto c = DecimalUtils::split(vec[i], scale);

// -1.123 splits to -1 / 0.123
if (vec[i].value < 0 && c.fractional)
{
using F = typename DataType::FieldType;
c.fractional = DecimalUtils::scaleMultiplier<F>(scale) + (c.whole ? F(-1) : F(1)) * c.fractional;
--c.whole;
}

for (auto & instruction : instructions)
instruction.perform(pos, static_cast<Int64>(c.whole), c.fractional, scale, time_zone);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/queries/0_stateless/00718_format_datetime_1.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1900-01-01 00:00:00.000
1962-12-08 18:11:29.123
1969-12-31 23:59:59.999
1970-01-01 00:00:00.000
1970-01-01 00:00:00.001
5 changes: 5 additions & 0 deletions tests/queries/0_stateless/00718_format_datetime_1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
select formatDateTime(toDateTime64('1900-01-01 00:00:00.000', 3, 'UTC'), '%F %T.%f');
select formatDateTime(toDateTime64('1962-12-08 18:11:29.123', 3, 'UTC'), '%F %T.%f');
select formatDateTime(toDateTime64('1969-12-31 23:59:59.999', 3, 'UTC'), '%F %T.%f');
select formatDateTime(toDateTime64('1970-01-01 00:00:00.000', 3, 'UTC'), '%F %T.%f');
select formatDateTime(toDateTime64('1970-01-01 00:00:00.001', 3, 'UTC'), '%F %T.%f');

0 comments on commit 12432ac

Please sign in to comment.