2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2022-08-24 15:44:53 +00:00
|
|
|
#include <Functions/IFunctionDateOrDateTime.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-09-09 14:17:46 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2023-07-04 15:38:53 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2022-09-09 14:17:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
/// See DateTimeTransforms.h
|
|
|
|
template <typename ToDataType, typename Transform>
|
2022-08-24 15:44:53 +00:00
|
|
|
class FunctionDateOrDateTimeToSomething : public IFunctionDateOrDateTime<Transform>
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-08-24 15:44:53 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionDateOrDateTimeToSomething>(); }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2023-09-22 13:13:55 +00:00
|
|
|
constexpr bool result_is_date_or_date32 = (std::is_same_v<ToDataType, DataTypeDate> || std::is_same_v<ToDataType, DataTypeDate32>);
|
|
|
|
this->checkArguments(arguments, result_is_date_or_date32);
|
2022-09-15 10:56:05 +00:00
|
|
|
|
2023-09-22 13:13:55 +00:00
|
|
|
/// For DateTime results, if time zone is specified, attach it to type.
|
2020-09-05 01:42:06 +00:00
|
|
|
/// If the time zone is specified but empty, throw an exception.
|
2019-10-02 05:53:38 +00:00
|
|
|
if constexpr (std::is_same_v<ToDataType, DataTypeDateTime>)
|
2020-09-04 09:44:10 +00:00
|
|
|
{
|
2023-06-10 12:53:02 +00:00
|
|
|
std::string time_zone = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0, false);
|
2020-09-25 18:03:16 +00:00
|
|
|
/// only validate the time_zone part if the number of arguments is 2. This is mainly
|
|
|
|
/// to accommodate functions like toStartOfDay(today()), toStartOfDay(yesterday()) etc.
|
|
|
|
if (arguments.size() == 2 && time_zone.empty())
|
2022-09-29 14:07:02 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Function {} supports a 2nd argument (optional) that must be a valid time zone",
|
|
|
|
this->getName());
|
2020-09-04 09:44:10 +00:00
|
|
|
return std::make_shared<ToDataType>(time_zone);
|
|
|
|
}
|
2023-09-22 13:13:55 +00:00
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
if constexpr (std::is_same_v<ToDataType, DataTypeDateTime64>)
|
2020-05-06 12:56:03 +00:00
|
|
|
{
|
|
|
|
Int64 scale = DataTypeDateTime64::default_scale;
|
|
|
|
if (const auto * dt64 = checkAndGetDataType<DataTypeDateTime64>(arguments[0].type.get()))
|
|
|
|
scale = dt64->getScale();
|
2022-02-13 14:54:03 +00:00
|
|
|
auto source_scale = scale;
|
|
|
|
|
|
|
|
if constexpr (std::is_same_v<ToStartOfMillisecondImpl, Transform>)
|
|
|
|
scale = std::max(source_scale, static_cast<Int64>(3));
|
|
|
|
else if constexpr (std::is_same_v<ToStartOfMicrosecondImpl, Transform>)
|
|
|
|
scale = std::max(source_scale, static_cast<Int64>(6));
|
|
|
|
else if constexpr (std::is_same_v<ToStartOfNanosecondImpl, Transform>)
|
|
|
|
scale = std::max(source_scale, static_cast<Int64>(9));
|
2020-05-06 12:56:03 +00:00
|
|
|
|
2023-06-10 12:53:02 +00:00
|
|
|
return std::make_shared<ToDataType>(scale, extractTimeZoneNameFromFunctionArguments(arguments, 1, 0, false));
|
2020-05-06 12:56:03 +00:00
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
|
|
|
return std::make_shared<ToDataType>();
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2020-10-17 21:41:50 +00:00
|
|
|
const IDataType * from_type = arguments[0].type.get();
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2023-09-22 13:13:55 +00:00
|
|
|
if (isDate(from_type))
|
2020-10-17 21:41:50 +00:00
|
|
|
return DateTimeTransformImpl<DataTypeDate, ToDataType, Transform>::execute(arguments, result_type, input_rows_count);
|
2023-09-22 13:13:55 +00:00
|
|
|
else if (isDate32(from_type))
|
2021-07-15 11:41:52 +00:00
|
|
|
return DateTimeTransformImpl<DataTypeDate32, ToDataType, Transform>::execute(arguments, result_type, input_rows_count);
|
2023-09-22 13:13:55 +00:00
|
|
|
else if (isDateTime(from_type))
|
2020-10-17 21:41:50 +00:00
|
|
|
return DateTimeTransformImpl<DataTypeDateTime, ToDataType, Transform>::execute(arguments, result_type, input_rows_count);
|
2023-09-22 13:13:55 +00:00
|
|
|
else if (isDateTime64(from_type))
|
2019-10-02 05:53:38 +00:00
|
|
|
{
|
|
|
|
const auto scale = static_cast<const DataTypeDateTime64 *>(from_type)->getScale();
|
2020-05-06 12:56:03 +00:00
|
|
|
const TransformDateTime64<Transform> transformer(scale);
|
2020-10-17 21:41:50 +00:00
|
|
|
return DateTimeTransformImpl<DataTypeDateTime64, ToDataType, decltype(transformer)>::execute(arguments, result_type, input_rows_count, transformer);
|
2019-10-02 05:53:38 +00:00
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
2022-09-29 14:07:02 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of argument of function {}",
|
|
|
|
arguments[0].type->getName(), this->getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 15:38:53 +00:00
|
|
|
bool hasInformationAboutPreimage() const override { return Transform::hasPreimage(); }
|
|
|
|
|
2023-10-14 02:32:25 +00:00
|
|
|
OptionalFieldInterval getPreimage(const IDataType & type, const Field & point) const override
|
2023-07-04 15:38:53 +00:00
|
|
|
{
|
|
|
|
if constexpr (Transform::hasPreimage())
|
|
|
|
return Transform::getPreimage(type, point);
|
|
|
|
else
|
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED,
|
|
|
|
"Function {} has no information about its preimage",
|
|
|
|
Transform::name);
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|