2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2019-11-04 14:06:22 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
2018-11-10 20:09:07 +00:00
|
|
|
#include <Functions/DateTimeTransforms.h>
|
2020-04-17 13:26:44 +00:00
|
|
|
#include <Functions/TransformDateTime64.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// See DateTimeTransforms.h
|
|
|
|
template <typename ToDataType, typename Transform>
|
|
|
|
class FunctionDateOrDateTimeToSomething : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Transform::name;
|
2021-04-10 23:33:54 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionDateOrDateTimeToSomething>(); }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() == 1)
|
|
|
|
{
|
2021-07-15 07:53:35 +00:00
|
|
|
if (!isDate(arguments[0].type) && !isDateTime(arguments[0].type) && !isDateTime64(arguments[0].type))
|
2019-02-12 14:18:00 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[0].type->getName() + " of argument of function " + getName()
|
|
|
|
+ ". Should be a date or a date with time",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
else if (arguments.size() == 2)
|
|
|
|
{
|
2021-07-15 07:53:35 +00:00
|
|
|
if (!isDate(arguments[0].type) && !isDateTime(arguments[0].type) && !isDateTime64(arguments[0].type))
|
2019-02-12 14:18:00 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[0].type->getName() + " of argument of function " + getName()
|
|
|
|
+ ". Should be a date or a date with time",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
if (!isString(arguments[1].type))
|
2018-09-26 00:31:40 +00:00
|
|
|
throw Exception(
|
|
|
|
"Function " + getName() + " supports 1 or 2 arguments. The 1st argument "
|
2019-02-12 14:18:00 +00:00
|
|
|
"must be of type Date or DateTime. The 2nd argument (optional) must be "
|
|
|
|
"a constant string with timezone name",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2021-07-15 07:53:35 +00:00
|
|
|
if (isDate(arguments[0].type) && std::is_same_v<ToDataType, DataTypeDate>)
|
2019-02-12 14:18:00 +00:00
|
|
|
throw Exception(
|
|
|
|
"The timezone argument of function " + getName() + " is allowed only when the 1st argument has the type DateTime",
|
2018-09-26 00:31:40 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
else
|
2019-02-12 14:18:00 +00:00
|
|
|
throw Exception(
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size())
|
|
|
|
+ ", should be 1 or 2",
|
2018-09-26 00:31:40 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
/// For DateTime, 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
|
|
|
{
|
|
|
|
std::string time_zone = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0);
|
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())
|
2020-09-04 09:44:10 +00:00
|
|
|
throw Exception(
|
|
|
|
"Function " + getName() + " supports a 2nd argument (optional) that must be non-empty and be a valid time zone",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
return std::make_shared<ToDataType>(time_zone);
|
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
return std::make_shared<ToDataType>(scale, extractTimeZoneNameFromFunctionArguments(arguments, 1, 0));
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
|
|
|
return std::make_shared<ToDataType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
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
|
|
|
WhichDataType which(from_type);
|
|
|
|
|
|
|
|
if (which.isDate())
|
2020-10-17 21:41:50 +00:00
|
|
|
return DateTimeTransformImpl<DataTypeDate, ToDataType, Transform>::execute(arguments, result_type, input_rows_count);
|
2018-09-26 00:31:40 +00:00
|
|
|
else if (which.isDateTime())
|
2020-10-17 21:41:50 +00:00
|
|
|
return DateTimeTransformImpl<DataTypeDateTime, ToDataType, Transform>::execute(arguments, result_type, input_rows_count);
|
2019-05-04 12:13:59 +00:00
|
|
|
else if (which.isDateTime64())
|
2019-10-02 05:53:38 +00:00
|
|
|
{
|
|
|
|
const auto scale = static_cast<const DataTypeDateTime64 *>(from_type)->getScale();
|
2020-04-17 13:26:44 +00:00
|
|
|
|
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
|
2020-10-17 21:41:50 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0].type->getName() + " of argument of function " + getName(),
|
2018-09-26 00:31:40 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasInformationAboutMonotonicity() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Monotonicity getMonotonicityForRange(const IDataType & type, const Field & left, const Field & right) const override
|
|
|
|
{
|
|
|
|
IFunction::Monotonicity is_monotonic { true };
|
|
|
|
IFunction::Monotonicity is_not_monotonic;
|
|
|
|
|
|
|
|
if (std::is_same_v<typename Transform::FactorTransform, ZeroTransform>)
|
|
|
|
{
|
|
|
|
is_monotonic.is_always_monotonic = true;
|
|
|
|
return is_monotonic;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method is called only if the function has one argument. Therefore, we do not care about the non-local time zone.
|
|
|
|
const DateLUTImpl & date_lut = DateLUT::instance();
|
|
|
|
if (left.isNull() || right.isNull())
|
|
|
|
return is_not_monotonic;
|
|
|
|
|
|
|
|
/// The function is monotonous on the [left, right] segment, if the factor transformation returns the same values for them.
|
|
|
|
|
|
|
|
if (checkAndGetDataType<DataTypeDate>(&type))
|
|
|
|
{
|
|
|
|
return Transform::FactorTransform::execute(UInt16(left.get<UInt64>()), date_lut)
|
|
|
|
== Transform::FactorTransform::execute(UInt16(right.get<UInt64>()), date_lut)
|
|
|
|
? is_monotonic : is_not_monotonic;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Transform::FactorTransform::execute(UInt32(left.get<UInt64>()), date_lut)
|
|
|
|
== Transform::FactorTransform::execute(UInt32(right.get<UInt64>()), date_lut)
|
|
|
|
? is_monotonic : is_not_monotonic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|