2019-05-06 12:14:45 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-11-04 14:06:22 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2019-06-09 15:19:15 +00:00
|
|
|
#include <Functions/CustomWeekTransforms.h>
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2019-05-06 12:14:45 +00:00
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-09 15:19:15 +00:00
|
|
|
/// See CustomWeekTransforms.h
|
2019-05-06 12:14:45 +00:00
|
|
|
template <typename ToDataType, typename Transform>
|
2019-06-09 15:19:15 +00:00
|
|
|
class FunctionCustomWeekToSomething : public IFunction
|
2019-05-06 12:14:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Transform::name;
|
2019-06-09 15:19:15 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionCustomWeekToSomething>(); }
|
2019-05-06 12:14:45 +00:00
|
|
|
|
2019-05-07 11:27:20 +00:00
|
|
|
String getName() const override { return name; }
|
2019-05-06 12:14:45 +00:00
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() == 1)
|
|
|
|
{
|
|
|
|
if (!isDateOrDateTime(arguments[0].type))
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else if (arguments.size() == 2)
|
|
|
|
{
|
|
|
|
if (!isDateOrDateTime(arguments[0].type))
|
|
|
|
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);
|
2019-06-09 15:19:15 +00:00
|
|
|
if (!isUInt8(arguments[1].type))
|
2019-05-06 12:14:45 +00:00
|
|
|
throw Exception(
|
2019-05-07 11:27:20 +00:00
|
|
|
"Function " + getName()
|
|
|
|
+ " supports 1 or 2 or 3 arguments. The 1st argument "
|
2019-05-06 12:14:45 +00:00
|
|
|
"must be of type Date or DateTime. The 2nd argument (optional) must be "
|
2019-06-09 15:19:15 +00:00
|
|
|
"a constant UInt8 with week mode. The 3nd argument (optional) must be "
|
2019-05-06 12:14:45 +00:00
|
|
|
"a constant string with timezone name",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
else if (arguments.size() == 3)
|
|
|
|
{
|
|
|
|
if (!isDateOrDateTime(arguments[0].type))
|
|
|
|
throw Exception(
|
2019-05-07 11:27:20 +00:00
|
|
|
"Illegal type " + arguments[0].type->getName() + " of argument of function " + getName()
|
2019-05-06 12:14:45 +00:00
|
|
|
+ ". Should be a date or a date with time",
|
2019-05-07 11:27:20 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2019-06-09 15:19:15 +00:00
|
|
|
if (!isUInt8(arguments[1].type))
|
2019-05-06 12:14:45 +00:00
|
|
|
throw Exception(
|
2019-05-07 11:27:20 +00:00
|
|
|
"Function " + getName()
|
|
|
|
+ " supports 1 or 2 or 3 arguments. The 1st argument "
|
|
|
|
"must be of type Date or DateTime. The 2nd argument (optional) must be "
|
2019-06-09 15:19:15 +00:00
|
|
|
"a constant UInt8 with week mode. The 3nd argument (optional) must be "
|
2019-05-07 11:27:20 +00:00
|
|
|
"a constant string with timezone name",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2019-05-06 12:14:45 +00:00
|
|
|
if (!isString(arguments[2].type))
|
|
|
|
throw Exception(
|
2019-05-07 11:27:20 +00:00
|
|
|
"Function " + getName()
|
|
|
|
+ " supports 1 or 2 or 3 arguments. The 1st argument "
|
|
|
|
"must be of type Date or DateTime. The 2nd argument (optional) must be "
|
2019-06-09 15:19:15 +00:00
|
|
|
"a constant UInt8 with week mode. The 3nd argument (optional) must be "
|
2019-05-07 11:27:20 +00:00
|
|
|
"a constant string with timezone name",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2019-05-06 12:14:45 +00:00
|
|
|
if (isDate(arguments[0].type) && std::is_same_v<ToDataType, DataTypeDate>)
|
|
|
|
throw Exception(
|
2019-05-07 11:27:20 +00:00
|
|
|
"The timezone argument of function " + getName() + " is allowed only when the 1st argument has the type DateTime",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2019-05-06 12:14:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception(
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size())
|
|
|
|
+ ", should be 1 or 2 or 3",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2019-06-14 03:04:01 +00:00
|
|
|
return std::make_shared<ToDataType>();
|
2019-05-06 12:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override
|
2019-05-06 12:14:45 +00:00
|
|
|
{
|
|
|
|
const IDataType * from_type = block.getByPosition(arguments[0]).type.get();
|
|
|
|
WhichDataType which(from_type);
|
|
|
|
|
|
|
|
if (which.isDate())
|
2019-10-23 12:27:36 +00:00
|
|
|
CustomWeekTransformImpl<DataTypeDate, ToDataType>::execute(
|
|
|
|
block, arguments, result, input_rows_count, Transform{});
|
2019-05-06 12:14:45 +00:00
|
|
|
else if (which.isDateTime())
|
2019-10-23 12:27:36 +00:00
|
|
|
CustomWeekTransformImpl<DataTypeDateTime, ToDataType>::execute(
|
|
|
|
block, arguments, result, input_rows_count, Transform{});
|
|
|
|
else if (which.isDateTime64())
|
|
|
|
{
|
|
|
|
CustomWeekTransformImpl<DataTypeDateTime64, ToDataType>::execute(
|
|
|
|
block, arguments, result, input_rows_count,
|
2020-05-06 12:56:03 +00:00
|
|
|
TransformDateTime64<Transform>{assert_cast<const DataTypeDateTime64 *>(from_type)->getScale()});
|
2019-10-23 12:27:36 +00:00
|
|
|
}
|
2019-05-06 12:14:45 +00:00
|
|
|
else
|
2019-05-07 11:27:20 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + block.getByPosition(arguments[0]).type->getName() + " of argument of function " + getName(),
|
2019-05-06 12:14:45 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-07 11:27:20 +00:00
|
|
|
bool hasInformationAboutMonotonicity() const override { return true; }
|
2019-05-06 12:14:45 +00:00
|
|
|
|
|
|
|
Monotonicity getMonotonicityForRange(const IDataType & type, const Field & left, const Field & right) const override
|
|
|
|
{
|
2019-05-07 11:27:20 +00:00
|
|
|
IFunction::Monotonicity is_monotonic{true};
|
2019-05-06 12:14:45 +00:00
|
|
|
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))
|
|
|
|
{
|
2019-06-09 15:19:15 +00:00
|
|
|
return Transform::FactorTransform::execute(UInt16(left.get<UInt64>()), DEFAULT_WEEK_MODE, date_lut)
|
|
|
|
== Transform::FactorTransform::execute(UInt16(right.get<UInt64>()), DEFAULT_WEEK_MODE, date_lut)
|
2019-05-07 11:27:20 +00:00
|
|
|
? is_monotonic
|
|
|
|
: is_not_monotonic;
|
2019-05-06 12:14:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-09 15:19:15 +00:00
|
|
|
return Transform::FactorTransform::execute(UInt32(left.get<UInt64>()), DEFAULT_WEEK_MODE, date_lut)
|
|
|
|
== Transform::FactorTransform::execute(UInt32(right.get<UInt64>()), DEFAULT_WEEK_MODE, date_lut)
|
2019-05-07 11:27:20 +00:00
|
|
|
? is_monotonic
|
|
|
|
: is_not_monotonic;
|
2019-05-06 12:14:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|