2021-12-21 13:41:53 +00:00
|
|
|
#include <Common/DateLUTImpl.h>
|
2019-02-11 11:59:17 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
2021-07-15 11:41:52 +00:00
|
|
|
#include <DataTypes/DataTypeDate32.h>
|
2019-02-11 11:59:17 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-11-04 14:06:22 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2019-02-11 11:59:17 +00:00
|
|
|
#include <DataTypes/DataTypeInterval.h>
|
|
|
|
#include <Functions/DateTimeTransforms.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-04-17 13:26:44 +00:00
|
|
|
#include <Functions/TransformDateTime64.h>
|
2019-02-11 11:59:17 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-03-09 03:38:43 +00:00
|
|
|
constexpr auto function_name = "toStartOfInterval";
|
2019-02-11 11:59:17 +00:00
|
|
|
|
2019-11-16 13:54:52 +00:00
|
|
|
template <IntervalKind::Kind unit>
|
2019-02-11 11:59:17 +00:00
|
|
|
struct Transform;
|
|
|
|
|
|
|
|
template <>
|
2019-11-16 13:54:52 +00:00
|
|
|
struct Transform<IntervalKind::Year>
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-05-12 01:28:53 +00:00
|
|
|
static constexpr auto name = function_name;
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt16 execute(UInt16 d, UInt64 years, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2021-11-08 07:19:31 +00:00
|
|
|
return time_zone.toStartOfYearInterval(DayNum(d), years);
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 11:41:52 +00:00
|
|
|
static UInt16 execute(Int32 d, UInt64 years, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfYearInterval(ExtendedDayNum(d), years);
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt16 execute(UInt32 t, UInt64 years, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfYearInterval(time_zone.toDayNum(t), years);
|
|
|
|
}
|
2020-04-17 13:26:44 +00:00
|
|
|
|
|
|
|
static UInt16 execute(Int64 t, UInt64 years, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfYearInterval(time_zone.toDayNum(t), years);
|
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2019-11-16 13:54:52 +00:00
|
|
|
struct Transform<IntervalKind::Quarter>
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-05-12 01:28:53 +00:00
|
|
|
static constexpr auto name = function_name;
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt16 execute(UInt16 d, UInt64 quarters, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2021-11-08 07:19:31 +00:00
|
|
|
return time_zone.toStartOfQuarterInterval(DayNum(d), quarters);
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 11:41:52 +00:00
|
|
|
static UInt16 execute(Int32 d, UInt64 quarters, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfQuarterInterval(ExtendedDayNum(d), quarters);
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt16 execute(UInt32 t, UInt64 quarters, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfQuarterInterval(time_zone.toDayNum(t), quarters);
|
|
|
|
}
|
2020-04-17 13:26:44 +00:00
|
|
|
|
|
|
|
static UInt16 execute(Int64 t, UInt64 quarters, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfQuarterInterval(time_zone.toDayNum(t), quarters);
|
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2019-11-16 13:54:52 +00:00
|
|
|
struct Transform<IntervalKind::Month>
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-05-12 01:28:53 +00:00
|
|
|
static constexpr auto name = function_name;
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt16 execute(UInt16 d, UInt64 months, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2021-11-08 07:19:31 +00:00
|
|
|
return time_zone.toStartOfMonthInterval(DayNum(d), months);
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 11:41:52 +00:00
|
|
|
static UInt16 execute(Int32 d, UInt64 months, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfMonthInterval(ExtendedDayNum(d), months);
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt16 execute(UInt32 t, UInt64 months, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfMonthInterval(time_zone.toDayNum(t), months);
|
|
|
|
}
|
2020-04-17 13:26:44 +00:00
|
|
|
|
|
|
|
static UInt16 execute(Int64 t, UInt64 months, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfMonthInterval(time_zone.toDayNum(t), months);
|
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2019-11-16 13:54:52 +00:00
|
|
|
struct Transform<IntervalKind::Week>
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-05-12 01:28:53 +00:00
|
|
|
static constexpr auto name = function_name;
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt16 execute(UInt16 d, UInt64 weeks, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2021-11-08 07:19:31 +00:00
|
|
|
return time_zone.toStartOfWeekInterval(DayNum(d), weeks);
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 11:41:52 +00:00
|
|
|
static UInt16 execute(Int32 d, UInt64 weeks, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfWeekInterval(ExtendedDayNum(d), weeks);
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt16 execute(UInt32 t, UInt64 weeks, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfWeekInterval(time_zone.toDayNum(t), weeks);
|
|
|
|
}
|
2020-04-17 13:26:44 +00:00
|
|
|
|
|
|
|
static UInt16 execute(Int64 t, UInt64 weeks, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfWeekInterval(time_zone.toDayNum(t), weeks);
|
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2019-11-16 13:54:52 +00:00
|
|
|
struct Transform<IntervalKind::Day>
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-05-12 01:28:53 +00:00
|
|
|
static constexpr auto name = function_name;
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt32 execute(UInt16 d, UInt64 days, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2020-04-17 13:26:44 +00:00
|
|
|
return time_zone.toStartOfDayInterval(ExtendedDayNum(d), days);
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 11:41:52 +00:00
|
|
|
static UInt32 execute(Int32 d, UInt64 days, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfDayInterval(ExtendedDayNum(d), days);
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt32 execute(UInt32 t, UInt64 days, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfDayInterval(time_zone.toDayNum(t), days);
|
|
|
|
}
|
2020-04-17 13:26:44 +00:00
|
|
|
|
|
|
|
static UInt32 execute(Int64 t, UInt64 days, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfDayInterval(time_zone.toDayNum(t), days);
|
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2019-11-16 13:54:52 +00:00
|
|
|
struct Transform<IntervalKind::Hour>
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-05-12 01:28:53 +00:00
|
|
|
static constexpr auto name = function_name;
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt32 execute(UInt16, UInt64, const DateLUTImpl &) { return dateIsNotSupported(function_name); }
|
2021-07-15 11:41:52 +00:00
|
|
|
static UInt32 execute(Int32, UInt64, const DateLUTImpl &) { return dateIsNotSupported(function_name); }
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt32 execute(UInt32 t, UInt64 hours, const DateLUTImpl & time_zone) { return time_zone.toStartOfHourInterval(t, hours); }
|
2020-04-17 13:26:44 +00:00
|
|
|
static UInt32 execute(Int64 t, UInt64 hours, const DateLUTImpl & time_zone) { return time_zone.toStartOfHourInterval(t, hours); }
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2019-11-16 13:54:52 +00:00
|
|
|
struct Transform<IntervalKind::Minute>
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-05-12 01:28:53 +00:00
|
|
|
static constexpr auto name = function_name;
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt32 execute(UInt16, UInt64, const DateLUTImpl &) { return dateIsNotSupported(function_name); }
|
|
|
|
|
2021-07-15 11:41:52 +00:00
|
|
|
static UInt32 execute(Int32, UInt64, const DateLUTImpl &) { return dateIsNotSupported(function_name); }
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt32 execute(UInt32 t, UInt64 minutes, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfMinuteInterval(t, minutes);
|
|
|
|
}
|
2020-04-17 13:26:44 +00:00
|
|
|
|
|
|
|
static UInt32 execute(Int64 t, UInt64 minutes, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfMinuteInterval(t, minutes);
|
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2019-11-16 13:54:52 +00:00
|
|
|
struct Transform<IntervalKind::Second>
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-05-12 01:28:53 +00:00
|
|
|
static constexpr auto name = function_name;
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt32 execute(UInt16, UInt64, const DateLUTImpl &) { return dateIsNotSupported(function_name); }
|
|
|
|
|
2021-07-15 11:41:52 +00:00
|
|
|
static UInt32 execute(Int32, UInt64, const DateLUTImpl &) { return dateIsNotSupported(function_name); }
|
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
static UInt32 execute(UInt32 t, UInt64 seconds, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfSecondInterval(t, seconds);
|
|
|
|
}
|
2020-04-17 13:26:44 +00:00
|
|
|
|
|
|
|
static Int64 execute(Int64 t, UInt64 seconds, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toStartOfSecondInterval(t, seconds);
|
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionToStartOfInterval : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2021-04-10 23:33:54 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionToStartOfInterval>(); }
|
2019-02-11 11:59:17 +00:00
|
|
|
|
|
|
|
static constexpr auto name = function_name;
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2019-02-12 14:18:00 +00:00
|
|
|
bool first_argument_is_date = false;
|
|
|
|
auto check_first_argument = [&]
|
|
|
|
{
|
2021-05-28 12:39:36 +00:00
|
|
|
if (!isDate(arguments[0].type) && !isDateTime(arguments[0].type) && !isDateTime64(arguments[0].type))
|
2019-02-11 11:59:17 +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);
|
2019-02-12 14:18:00 +00:00
|
|
|
first_argument_is_date = isDate(arguments[0].type);
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const DataTypeInterval * interval_type = nullptr;
|
2019-02-12 14:18:00 +00:00
|
|
|
bool result_type_is_date = false;
|
|
|
|
auto check_interval_argument = [&]
|
|
|
|
{
|
2019-02-11 11:59:17 +00:00
|
|
|
interval_type = checkAndGetDataType<DataTypeInterval>(arguments[1].type.get());
|
|
|
|
if (!interval_type)
|
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[1].type->getName() + " of argument of function " + getName()
|
|
|
|
+ ". Should be an interval of time",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2019-11-16 13:54:52 +00:00
|
|
|
result_type_is_date = (interval_type->getKind() == IntervalKind::Year)
|
|
|
|
|| (interval_type->getKind() == IntervalKind::Quarter) || (interval_type->getKind() == IntervalKind::Month)
|
|
|
|
|| (interval_type->getKind() == IntervalKind::Week);
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
2019-02-12 14:18:00 +00:00
|
|
|
auto check_timezone_argument = [&]
|
|
|
|
{
|
2019-02-11 11:59:17 +00:00
|
|
|
if (!WhichDataType(arguments[2].type).isString())
|
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[2].type->getName() + " of argument of function " + getName()
|
2019-02-12 14:18:00 +00:00
|
|
|
+ ". This argument is optional and must be a constant string with timezone name",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
if (first_argument_is_date && result_type_is_date)
|
2021-09-06 14:24:03 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"The timezone argument of function {} with interval type {} is allowed only when the 1st argument "
|
|
|
|
"has the type DateTime",
|
|
|
|
getName(), interval_type->getKind().toString());
|
2019-02-11 11:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2019-02-12 14:18:00 +00:00
|
|
|
check_first_argument();
|
2019-02-11 11:59:17 +00:00
|
|
|
check_interval_argument();
|
|
|
|
}
|
|
|
|
else if (arguments.size() == 3)
|
|
|
|
{
|
2019-02-12 14:18:00 +00:00
|
|
|
check_first_argument();
|
2019-02-11 11:59:17 +00:00
|
|
|
check_interval_argument();
|
|
|
|
check_timezone_argument();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size())
|
|
|
|
+ ", should be 2 or 3",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
}
|
|
|
|
|
2019-02-12 14:18:00 +00:00
|
|
|
if (result_type_is_date)
|
2021-07-01 15:13:43 +00:00
|
|
|
return std::make_shared<DataTypeDate>();
|
2019-02-12 14:18:00 +00:00
|
|
|
else
|
|
|
|
return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 2, 0));
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /* input_rows_count */) const override
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
const auto & time_column = arguments[0];
|
|
|
|
const auto & interval_column = arguments[1];
|
2020-04-17 13:26:44 +00:00
|
|
|
const auto & time_zone = extractTimeZoneFromFunctionArguments(arguments, 2, 0);
|
2019-02-11 11:59:17 +00:00
|
|
|
auto result_column = dispatchForColumns(time_column, interval_column, time_zone);
|
2020-10-19 15:27:41 +00:00
|
|
|
return result_column;
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasInformationAboutMonotonicity() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Monotonicity getMonotonicityForRange(const IDataType &, const Field &, const Field &) const override
|
|
|
|
{
|
2021-09-29 16:01:26 +00:00
|
|
|
return { .is_monotonic = true, .is_always_monotonic = true };
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ColumnPtr dispatchForColumns(
|
2020-07-21 13:58:07 +00:00
|
|
|
const ColumnWithTypeAndName & time_column, const ColumnWithTypeAndName & interval_column, const DateLUTImpl & time_zone) const
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
2019-10-22 07:43:14 +00:00
|
|
|
const auto & from_datatype = *time_column.type.get();
|
|
|
|
const auto which_type = WhichDataType(from_datatype);
|
|
|
|
if (which_type.isDateTime())
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
|
|
|
const auto * time_column_vec = checkAndGetColumn<ColumnUInt32>(time_column.column.get());
|
|
|
|
if (time_column_vec)
|
2019-10-22 07:43:14 +00:00
|
|
|
return dispatchForIntervalColumn(assert_cast<const DataTypeDateTime&>(from_datatype), *time_column_vec, interval_column, time_zone);
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
2019-10-22 07:43:14 +00:00
|
|
|
if (which_type.isDate())
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
|
|
|
const auto * time_column_vec = checkAndGetColumn<ColumnUInt16>(time_column.column.get());
|
|
|
|
if (time_column_vec)
|
2019-10-22 07:43:14 +00:00
|
|
|
return dispatchForIntervalColumn(assert_cast<const DataTypeDate&>(from_datatype), *time_column_vec, interval_column, time_zone);
|
|
|
|
}
|
2021-07-15 11:41:52 +00:00
|
|
|
if (which_type.isDate32())
|
|
|
|
{
|
|
|
|
const auto * time_column_vec = checkAndGetColumn<ColumnInt32>(time_column.column.get());
|
|
|
|
if (time_column_vec)
|
|
|
|
return dispatchForIntervalColumn(assert_cast<const DataTypeDate32&>(from_datatype), *time_column_vec, interval_column, time_zone);
|
|
|
|
}
|
2019-10-22 07:43:14 +00:00
|
|
|
if (which_type.isDateTime64())
|
|
|
|
{
|
|
|
|
const auto * time_column_vec = checkAndGetColumn<DataTypeDateTime64::ColumnType>(time_column.column.get());
|
|
|
|
if (time_column_vec)
|
|
|
|
return dispatchForIntervalColumn(assert_cast<const DataTypeDateTime64&>(from_datatype), *time_column_vec, interval_column, time_zone);
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
throw Exception(
|
|
|
|
"Illegal column for first argument of function " + getName() + ". Must contain dates or dates with time",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
2019-10-22 07:43:14 +00:00
|
|
|
template <typename ColumnType, typename FromDataType>
|
2019-02-11 11:59:17 +00:00
|
|
|
ColumnPtr dispatchForIntervalColumn(
|
2020-07-21 13:58:07 +00:00
|
|
|
const FromDataType & from, const ColumnType & time_column, const ColumnWithTypeAndName & interval_column, const DateLUTImpl & time_zone) const
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
|
|
|
const auto * interval_type = checkAndGetDataType<DataTypeInterval>(interval_column.type.get());
|
|
|
|
if (!interval_type)
|
|
|
|
throw Exception(
|
|
|
|
"Illegal column for second argument of function " + getName() + ", must be an interval of time.",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
const auto * interval_column_const_int64 = checkAndGetColumnConst<ColumnInt64>(interval_column.column.get());
|
|
|
|
if (!interval_column_const_int64)
|
|
|
|
throw Exception(
|
|
|
|
"Illegal column for second argument of function " + getName() + ", must be a const interval of time.", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
Int64 num_units = interval_column_const_int64->getValue<Int64>();
|
|
|
|
if (num_units <= 0)
|
|
|
|
throw Exception("Value for second argument of function " + getName() + " must be positive.", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
switch (interval_type->getKind())
|
|
|
|
{
|
2019-11-16 13:54:52 +00:00
|
|
|
case IntervalKind::Second:
|
2019-12-11 14:38:32 +00:00
|
|
|
return execute<FromDataType, UInt32, IntervalKind::Second>(from, time_column, num_units, time_zone);
|
2019-11-16 13:54:52 +00:00
|
|
|
case IntervalKind::Minute:
|
2019-12-11 14:38:32 +00:00
|
|
|
return execute<FromDataType, UInt32, IntervalKind::Minute>(from, time_column, num_units, time_zone);
|
2019-11-16 13:54:52 +00:00
|
|
|
case IntervalKind::Hour:
|
2019-12-11 14:38:32 +00:00
|
|
|
return execute<FromDataType, UInt32, IntervalKind::Hour>(from, time_column, num_units, time_zone);
|
2019-11-16 13:54:52 +00:00
|
|
|
case IntervalKind::Day:
|
2019-12-11 14:38:32 +00:00
|
|
|
return execute<FromDataType, UInt32, IntervalKind::Day>(from, time_column, num_units, time_zone);
|
2019-11-16 13:54:52 +00:00
|
|
|
case IntervalKind::Week:
|
2019-12-11 14:38:32 +00:00
|
|
|
return execute<FromDataType, UInt16, IntervalKind::Week>(from, time_column, num_units, time_zone);
|
2019-11-16 13:54:52 +00:00
|
|
|
case IntervalKind::Month:
|
2019-12-11 14:38:32 +00:00
|
|
|
return execute<FromDataType, UInt16, IntervalKind::Month>(from, time_column, num_units, time_zone);
|
2019-11-16 13:54:52 +00:00
|
|
|
case IntervalKind::Quarter:
|
2019-12-11 14:38:32 +00:00
|
|
|
return execute<FromDataType, UInt16, IntervalKind::Quarter>(from, time_column, num_units, time_zone);
|
2019-11-16 13:54:52 +00:00
|
|
|
case IntervalKind::Year:
|
2019-12-11 14:38:32 +00:00
|
|
|
return execute<FromDataType, UInt16, IntervalKind::Year>(from, time_column, num_units, time_zone);
|
2019-02-11 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:38:32 +00:00
|
|
|
|
|
|
|
template <typename FromDataType, typename ToType, IntervalKind::Kind unit, typename ColumnType>
|
2020-07-21 13:58:07 +00:00
|
|
|
ColumnPtr execute(const FromDataType & from_datatype, const ColumnType & time_column, UInt64 num_units, const DateLUTImpl & time_zone) const
|
2019-02-11 11:59:17 +00:00
|
|
|
{
|
|
|
|
const auto & time_data = time_column.getData();
|
|
|
|
size_t size = time_column.size();
|
|
|
|
auto result = ColumnVector<ToType>::create();
|
|
|
|
auto & result_data = result->getData();
|
|
|
|
result_data.resize(size);
|
2019-10-22 07:43:14 +00:00
|
|
|
|
|
|
|
if constexpr (std::is_same_v<FromDataType, DataTypeDateTime64>)
|
|
|
|
{
|
2020-05-06 12:56:03 +00:00
|
|
|
const auto transform = TransformDateTime64<Transform<unit>>{from_datatype.getScale()};
|
2019-10-22 07:43:14 +00:00
|
|
|
for (size_t i = 0; i != size; ++i)
|
|
|
|
result_data[i] = transform.execute(time_data[i], num_units, time_zone);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i != size; ++i)
|
|
|
|
result_data[i] = Transform<unit>::execute(time_data[i], num_units, time_zone);
|
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2019-02-11 11:59:17 +00:00
|
|
|
|
|
|
|
void registerFunctionToStartOfInterval(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionToStartOfInterval>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|