diff --git a/src/Functions/FunctionsConversion.h b/src/Functions/FunctionsConversion.h index 74d8d853dcb..2e2a4ce9cfa 100644 --- a/src/Functions/FunctionsConversion.h +++ b/src/Functions/FunctionsConversion.h @@ -516,6 +516,25 @@ struct ToDateTime64TransformSigned return DecimalUtils::decimalFromComponentsWithMultiplier(from, 0, scale_multiplier); } }; +template +struct ToDateTime64TransformFloat +{ + static constexpr auto name = "toDateTime64"; + + const UInt32 scale = 1; + + ToDateTime64TransformFloat(UInt32 scale_ = 0) + : scale(scale_) + {} + + inline NO_SANITIZE_UNDEFINED DateTime64::NativeType execute(FromType from, const DateLUTImpl &) const + { + if (from < 0) + return 0; + from = std::min(from, FromType(0xFFFFFFFF)); + return convertToDecimal(from, scale); + } +}; template struct ConvertImpl : DateTimeTransformImpl> {}; @@ -528,9 +547,9 @@ template struct ConvertImpl struct ConvertImpl : DateTimeTransformImpl> {}; template struct ConvertImpl - : DateTimeTransformImpl> {}; + : DateTimeTransformImpl> {}; template struct ConvertImpl - : DateTimeTransformImpl> {}; + : DateTimeTransformImpl> {}; /** Conversion of DateTime64 to Date or DateTime: discards fractional part. */ diff --git a/tests/queries/0_stateless/01734_datetime64_from_float.reference b/tests/queries/0_stateless/01734_datetime64_from_float.reference new file mode 100644 index 00000000000..32e7d2736c6 --- /dev/null +++ b/tests/queries/0_stateless/01734_datetime64_from_float.reference @@ -0,0 +1,7 @@ +-- { echo } +SELECT CAST(1111111111.222 AS DateTime64(3)); +2005-03-18 04:58:31.222 +SELECT toDateTime(1111111111.222, 3); +2005-03-18 04:58:31.222 +SELECT toDateTime64(1111111111.222, 3); +2005-03-18 04:58:31.222 diff --git a/tests/queries/0_stateless/01734_datetime64_from_float.sql b/tests/queries/0_stateless/01734_datetime64_from_float.sql new file mode 100644 index 00000000000..b6be65cb7c2 --- /dev/null +++ b/tests/queries/0_stateless/01734_datetime64_from_float.sql @@ -0,0 +1,4 @@ +-- { echo } +SELECT CAST(1111111111.222 AS DateTime64(3)); +SELECT toDateTime(1111111111.222, 3); +SELECT toDateTime64(1111111111.222, 3);