2018-09-26 00:31:40 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-11-04 14:06:22 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2022-12-20 10:18:43 +00:00
|
|
|
#include <Common/IntervalKind.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2022-10-19 09:13:02 +00:00
|
|
|
#include <Columns/ColumnsDateTime.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2021-04-09 12:22:35 +00:00
|
|
|
#include <Columns/ColumnDecimal.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
#include <Functions/DateTimeTransforms.h>
|
2021-04-09 12:22:35 +00:00
|
|
|
#include <Functions/TransformDateTime64.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/find_symbols.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-12-20 10:18:43 +00:00
|
|
|
template <bool is_diff>
|
2022-10-28 21:06:32 +00:00
|
|
|
class DateDiffImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-10-31 13:03:13 +00:00
|
|
|
using ColumnDateTime64 = ColumnDecimal<DateTime64>;
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2022-10-31 13:03:13 +00:00
|
|
|
explicit DateDiffImpl(const String & name_) : name(name_) {}
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
template <typename Transform>
|
|
|
|
void dispatchForColumns(
|
|
|
|
const IColumn & x, const IColumn & y,
|
|
|
|
const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y,
|
2020-07-21 13:58:07 +00:00
|
|
|
ColumnInt64::Container & result) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-19 09:13:02 +00:00
|
|
|
if (const auto * x_vec_16 = checkAndGetColumn<ColumnDate>(&x))
|
2019-01-04 12:10:00 +00:00
|
|
|
dispatchForSecondColumn<Transform>(*x_vec_16, y, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * x_vec_32 = checkAndGetColumn<ColumnDateTime>(&x))
|
2019-01-04 12:10:00 +00:00
|
|
|
dispatchForSecondColumn<Transform>(*x_vec_32, y, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * x_vec_32_s = checkAndGetColumn<ColumnDate32>(&x))
|
2022-10-03 13:15:32 +00:00
|
|
|
dispatchForSecondColumn<Transform>(*x_vec_32_s, y, timezone_x, timezone_y, result);
|
2021-04-09 12:22:35 +00:00
|
|
|
else if (const auto * x_vec_64 = checkAndGetColumn<ColumnDateTime64>(&x))
|
|
|
|
dispatchForSecondColumn<Transform>(*x_vec_64, y, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * x_const_16 = checkAndGetColumnConst<ColumnDate>(&x))
|
2019-01-04 12:10:00 +00:00
|
|
|
dispatchConstForSecondColumn<Transform>(x_const_16->getValue<UInt16>(), y, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * x_const_32 = checkAndGetColumnConst<ColumnDateTime>(&x))
|
2019-01-04 12:10:00 +00:00
|
|
|
dispatchConstForSecondColumn<Transform>(x_const_32->getValue<UInt32>(), y, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * x_const_32_s = checkAndGetColumnConst<ColumnDate32>(&x))
|
2022-10-03 13:15:32 +00:00
|
|
|
dispatchConstForSecondColumn<Transform>(x_const_32_s->getValue<Int32>(), y, timezone_x, timezone_y, result);
|
2021-04-09 12:22:35 +00:00
|
|
|
else if (const auto * x_const_64 = checkAndGetColumnConst<ColumnDateTime64>(&x))
|
|
|
|
dispatchConstForSecondColumn<Transform>(x_const_64->getValue<DecimalField<DateTime64>>(), y, timezone_x, timezone_y, result);
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column for first argument of function {}, must be Date, Date32, DateTime or DateTime64",
|
2022-10-31 13:03:13 +00:00
|
|
|
name);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:22:35 +00:00
|
|
|
template <typename Transform, typename LeftColumnType>
|
2018-09-26 00:31:40 +00:00
|
|
|
void dispatchForSecondColumn(
|
2021-04-09 12:22:35 +00:00
|
|
|
const LeftColumnType & x, const IColumn & y,
|
2018-09-26 00:31:40 +00:00
|
|
|
const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y,
|
2020-07-21 13:58:07 +00:00
|
|
|
ColumnInt64::Container & result) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-19 09:13:02 +00:00
|
|
|
if (const auto * y_vec_16 = checkAndGetColumn<ColumnDate>(&y))
|
2020-03-23 02:12:31 +00:00
|
|
|
vectorVector<Transform>(x, *y_vec_16, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * y_vec_32 = checkAndGetColumn<ColumnDateTime>(&y))
|
2020-03-23 02:12:31 +00:00
|
|
|
vectorVector<Transform>(x, *y_vec_32, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * y_vec_32_s = checkAndGetColumn<ColumnDate32>(&y))
|
2022-10-03 13:15:32 +00:00
|
|
|
vectorVector<Transform>(x, *y_vec_32_s, timezone_x, timezone_y, result);
|
2021-04-09 12:22:35 +00:00
|
|
|
else if (const auto * y_vec_64 = checkAndGetColumn<ColumnDateTime64>(&y))
|
|
|
|
vectorVector<Transform>(x, *y_vec_64, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * y_const_16 = checkAndGetColumnConst<ColumnDate>(&y))
|
2020-03-23 02:12:31 +00:00
|
|
|
vectorConstant<Transform>(x, y_const_16->getValue<UInt16>(), timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * y_const_32 = checkAndGetColumnConst<ColumnDateTime>(&y))
|
2020-03-23 02:12:31 +00:00
|
|
|
vectorConstant<Transform>(x, y_const_32->getValue<UInt32>(), timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * y_const_32_s = checkAndGetColumnConst<ColumnDate32>(&y))
|
2022-10-03 13:15:32 +00:00
|
|
|
vectorConstant<Transform>(x, y_const_32_s->getValue<Int32>(), timezone_x, timezone_y, result);
|
2021-04-09 12:22:35 +00:00
|
|
|
else if (const auto * y_const_64 = checkAndGetColumnConst<ColumnDateTime64>(&y))
|
|
|
|
vectorConstant<Transform>(x, y_const_64->getValue<DecimalField<DateTime64>>(), timezone_x, timezone_y, result);
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column for second argument of function {}, must be Date, Date32, DateTime or DateTime64",
|
2022-10-31 13:03:13 +00:00
|
|
|
name);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Transform, typename T1>
|
|
|
|
void dispatchConstForSecondColumn(
|
|
|
|
T1 x, const IColumn & y,
|
|
|
|
const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y,
|
2020-07-21 13:58:07 +00:00
|
|
|
ColumnInt64::Container & result) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-19 09:13:02 +00:00
|
|
|
if (const auto * y_vec_16 = checkAndGetColumn<ColumnDate>(&y))
|
2020-03-23 02:12:31 +00:00
|
|
|
constantVector<Transform>(x, *y_vec_16, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * y_vec_32 = checkAndGetColumn<ColumnDateTime>(&y))
|
2020-03-23 02:12:31 +00:00
|
|
|
constantVector<Transform>(x, *y_vec_32, timezone_x, timezone_y, result);
|
2022-10-19 09:13:02 +00:00
|
|
|
else if (const auto * y_vec_32_s = checkAndGetColumn<ColumnDate32>(&y))
|
2022-10-13 13:35:14 +00:00
|
|
|
constantVector<Transform>(x, *y_vec_32_s, timezone_x, timezone_y, result);
|
2021-04-09 12:22:35 +00:00
|
|
|
else if (const auto * y_vec_64 = checkAndGetColumn<ColumnDateTime64>(&y))
|
|
|
|
constantVector<Transform>(x, *y_vec_64, timezone_x, timezone_y, result);
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column for second argument of function {}, must be Date, Date32, DateTime or DateTime64",
|
2022-10-31 13:03:13 +00:00
|
|
|
name);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:22:35 +00:00
|
|
|
template <typename Transform, typename LeftColumnType, typename RightColumnType>
|
2020-03-23 02:12:31 +00:00
|
|
|
void vectorVector(
|
2021-04-09 12:22:35 +00:00
|
|
|
const LeftColumnType & x, const RightColumnType & y,
|
2018-09-26 00:31:40 +00:00
|
|
|
const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y,
|
2020-07-21 13:58:07 +00:00
|
|
|
ColumnInt64::Container & result) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
const auto & x_data = x.getData();
|
|
|
|
const auto & y_data = y.getData();
|
2021-04-09 12:22:35 +00:00
|
|
|
|
|
|
|
const auto transform_x = TransformDateTime64<Transform>(getScale(x));
|
|
|
|
const auto transform_y = TransformDateTime64<Transform>(getScale(y));
|
2018-09-26 00:31:40 +00:00
|
|
|
for (size_t i = 0, size = x.size(); i < size; ++i)
|
2021-04-09 12:22:35 +00:00
|
|
|
result[i] = calculate(transform_x, transform_y, x_data[i], y_data[i], timezone_x, timezone_y);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:22:35 +00:00
|
|
|
template <typename Transform, typename LeftColumnType, typename T2>
|
2020-03-23 02:12:31 +00:00
|
|
|
void vectorConstant(
|
2021-04-09 12:22:35 +00:00
|
|
|
const LeftColumnType & x, T2 y,
|
2018-09-26 00:31:40 +00:00
|
|
|
const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y,
|
2020-07-21 13:58:07 +00:00
|
|
|
ColumnInt64::Container & result) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
const auto & x_data = x.getData();
|
2021-04-09 12:22:35 +00:00
|
|
|
const auto transform_x = TransformDateTime64<Transform>(getScale(x));
|
|
|
|
const auto transform_y = TransformDateTime64<Transform>(getScale(y));
|
|
|
|
const auto y_value = stripDecimalFieldValue(y);
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
for (size_t i = 0, size = x.size(); i < size; ++i)
|
2021-04-09 12:22:35 +00:00
|
|
|
result[i] = calculate(transform_x, transform_y, x_data[i], y_value, timezone_x, timezone_y);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:22:35 +00:00
|
|
|
template <typename Transform, typename T1, typename RightColumnType>
|
2020-03-23 02:12:31 +00:00
|
|
|
void constantVector(
|
2021-04-09 12:22:35 +00:00
|
|
|
T1 x, const RightColumnType & y,
|
2018-09-26 00:31:40 +00:00
|
|
|
const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y,
|
2020-07-21 13:58:07 +00:00
|
|
|
ColumnInt64::Container & result) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
const auto & y_data = y.getData();
|
2021-04-09 12:22:35 +00:00
|
|
|
const auto transform_x = TransformDateTime64<Transform>(getScale(x));
|
|
|
|
const auto transform_y = TransformDateTime64<Transform>(getScale(y));
|
|
|
|
const auto x_value = stripDecimalFieldValue(x);
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
for (size_t i = 0, size = y.size(); i < size; ++i)
|
2021-04-09 12:22:35 +00:00
|
|
|
result[i] = calculate(transform_x, transform_y, x_value, y_data[i], timezone_x, timezone_y);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:22:35 +00:00
|
|
|
template <typename TransformX, typename TransformY, typename T1, typename T2>
|
|
|
|
Int64 calculate(const TransformX & transform_x, const TransformY & transform_y, T1 x, T2 y, const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-12-20 10:18:43 +00:00
|
|
|
if constexpr (is_diff)
|
|
|
|
return static_cast<Int64>(transform_y.execute(y, timezone_y))
|
2022-11-08 16:39:40 +00:00
|
|
|
- static_cast<Int64>(transform_x.execute(x, timezone_x));
|
2022-12-20 10:18:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto res = static_cast<Int64>(transform_y.execute(y, timezone_y))
|
|
|
|
- static_cast<Int64>(transform_x.execute(x, timezone_x));
|
2023-07-16 18:07:22 +00:00
|
|
|
DateTimeComponentsWithFractionalPart a_comp;
|
|
|
|
DateTimeComponentsWithFractionalPart b_comp;
|
2022-12-20 10:18:43 +00:00
|
|
|
Int64 adjust_value;
|
2023-07-16 18:07:22 +00:00
|
|
|
auto x_microseconds = TransformDateTime64<ToRelativeSubsecondNumImpl<microsecond_multiplier>>(transform_x.getScaleMultiplier()).execute(x, timezone_x);
|
|
|
|
auto y_microseconds = TransformDateTime64<ToRelativeSubsecondNumImpl<microsecond_multiplier>>(transform_y.getScaleMultiplier()).execute(y, timezone_y);
|
|
|
|
|
|
|
|
if (x_microseconds <= y_microseconds)
|
2022-12-20 10:18:43 +00:00
|
|
|
{
|
|
|
|
a_comp = TransformDateTime64<ToDateTimeComponentsImpl>(transform_x.getScaleMultiplier()).execute(x, timezone_x);
|
|
|
|
b_comp = TransformDateTime64<ToDateTimeComponentsImpl>(transform_y.getScaleMultiplier()).execute(y, timezone_y);
|
|
|
|
adjust_value = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a_comp = TransformDateTime64<ToDateTimeComponentsImpl>(transform_y.getScaleMultiplier()).execute(y, timezone_y);
|
|
|
|
b_comp = TransformDateTime64<ToDateTimeComponentsImpl>(transform_x.getScaleMultiplier()).execute(x, timezone_x);
|
|
|
|
adjust_value = 1;
|
|
|
|
}
|
2023-06-20 18:31:04 +00:00
|
|
|
|
2023-07-16 18:07:22 +00:00
|
|
|
|
2022-12-20 10:18:43 +00:00
|
|
|
if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeYearNumImpl<ResultPrecision::Extended>>>)
|
|
|
|
{
|
2023-06-20 18:31:04 +00:00
|
|
|
if ((a_comp.date.month > b_comp.date.month)
|
|
|
|
|| ((a_comp.date.month == b_comp.date.month) && ((a_comp.date.day > b_comp.date.day)
|
|
|
|
|| ((a_comp.date.day == b_comp.date.day) && ((a_comp.time.hour > b_comp.time.hour)
|
|
|
|
|| ((a_comp.time.hour == b_comp.time.hour) && ((a_comp.time.minute > b_comp.time.minute)
|
2023-07-16 18:07:22 +00:00
|
|
|
|| ((a_comp.time.minute == b_comp.time.minute) && ((a_comp.time.second > b_comp.time.second)
|
|
|
|
|| ((a_comp.time.second == b_comp.time.second) && ((a_comp.millisecond > b_comp.millisecond)
|
|
|
|
|| ((a_comp.millisecond == b_comp.millisecond) && (a_comp.microsecond > b_comp.microsecond)))))))))))))
|
2022-12-20 10:18:43 +00:00
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeQuarterNumImpl<ResultPrecision::Extended>>>)
|
|
|
|
{
|
2023-06-20 18:31:04 +00:00
|
|
|
auto x_month_in_quarter = (a_comp.date.month - 1) % 3;
|
|
|
|
auto y_month_in_quarter = (b_comp.date.month - 1) % 3;
|
2022-12-20 10:18:43 +00:00
|
|
|
if ((x_month_in_quarter > y_month_in_quarter)
|
2023-06-20 18:31:04 +00:00
|
|
|
|| ((x_month_in_quarter == y_month_in_quarter) && ((a_comp.date.day > b_comp.date.day)
|
|
|
|
|| ((a_comp.date.day == b_comp.date.day) && ((a_comp.time.hour > b_comp.time.hour)
|
|
|
|
|| ((a_comp.time.hour == b_comp.time.hour) && ((a_comp.time.minute > b_comp.time.minute)
|
2023-07-16 18:07:22 +00:00
|
|
|
|| ((a_comp.time.minute == b_comp.time.minute) && ((a_comp.time.second > b_comp.time.second)
|
|
|
|
|| ((a_comp.time.second == b_comp.time.second) && ((a_comp.millisecond > b_comp.millisecond)
|
|
|
|
|| ((a_comp.millisecond == b_comp.millisecond) && (a_comp.microsecond > b_comp.microsecond)))))))))))))
|
2022-12-20 10:18:43 +00:00
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeMonthNumImpl<ResultPrecision::Extended>>>)
|
|
|
|
{
|
2023-06-20 18:31:04 +00:00
|
|
|
if ((a_comp.date.day > b_comp.date.day)
|
|
|
|
|| ((a_comp.date.day == b_comp.date.day) && ((a_comp.time.hour > b_comp.time.hour)
|
|
|
|
|| ((a_comp.time.hour == b_comp.time.hour) && ((a_comp.time.minute > b_comp.time.minute)
|
2023-07-16 18:07:22 +00:00
|
|
|
|| ((a_comp.time.minute == b_comp.time.minute) && ((a_comp.time.second > b_comp.time.second)
|
|
|
|
|| ((a_comp.time.second == b_comp.time.second) && ((a_comp.millisecond > b_comp.millisecond)
|
|
|
|
|| ((a_comp.millisecond == b_comp.millisecond) && (a_comp.microsecond > b_comp.microsecond)))))))))))
|
2022-12-20 10:18:43 +00:00
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeWeekNumImpl<ResultPrecision::Extended>>>)
|
|
|
|
{
|
2023-01-13 14:00:16 +00:00
|
|
|
auto x_day_of_week = TransformDateTime64<ToDayOfWeekImpl>(transform_x.getScaleMultiplier()).execute(x, 0, timezone_x);
|
|
|
|
auto y_day_of_week = TransformDateTime64<ToDayOfWeekImpl>(transform_y.getScaleMultiplier()).execute(y, 0, timezone_y);
|
2022-12-20 10:18:43 +00:00
|
|
|
if ((x_day_of_week > y_day_of_week)
|
2023-06-20 18:31:04 +00:00
|
|
|
|| ((x_day_of_week == y_day_of_week) && (a_comp.time.hour > b_comp.time.hour))
|
|
|
|
|| ((a_comp.time.hour == b_comp.time.hour) && ((a_comp.time.minute > b_comp.time.minute)
|
2023-07-16 18:07:22 +00:00
|
|
|
|| ((a_comp.time.minute == b_comp.time.minute) && ((a_comp.time.second > b_comp.time.second)
|
|
|
|
|| ((a_comp.time.second == b_comp.time.second) && ((a_comp.millisecond > b_comp.millisecond)
|
|
|
|
|| ((a_comp.millisecond == b_comp.millisecond) && (a_comp.microsecond > b_comp.microsecond)))))))))
|
2022-12-20 10:18:43 +00:00
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeDayNumImpl<ResultPrecision::Extended>>>)
|
|
|
|
{
|
2023-06-20 18:31:04 +00:00
|
|
|
if ((a_comp.time.hour > b_comp.time.hour)
|
|
|
|
|| ((a_comp.time.hour == b_comp.time.hour) && ((a_comp.time.minute > b_comp.time.minute)
|
2023-07-16 18:07:22 +00:00
|
|
|
|| ((a_comp.time.minute == b_comp.time.minute) && ((a_comp.time.second > b_comp.time.second)
|
|
|
|
|| ((a_comp.time.second == b_comp.time.second) && ((a_comp.millisecond > b_comp.millisecond)
|
|
|
|
|| ((a_comp.millisecond == b_comp.millisecond) && (a_comp.microsecond > b_comp.microsecond)))))))))
|
2022-12-20 10:18:43 +00:00
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeHourNumImpl<ResultPrecision::Extended>>>)
|
|
|
|
{
|
2023-06-20 18:31:04 +00:00
|
|
|
if ((a_comp.time.minute > b_comp.time.minute)
|
2023-07-16 18:07:22 +00:00
|
|
|
|| ((a_comp.time.minute == b_comp.time.minute) && ((a_comp.time.second > b_comp.time.second)
|
|
|
|
|| ((a_comp.time.second == b_comp.time.second) && ((a_comp.millisecond > b_comp.millisecond)
|
|
|
|
|| ((a_comp.millisecond == b_comp.millisecond) && (a_comp.microsecond > b_comp.microsecond)))))))
|
2022-12-20 10:18:43 +00:00
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeMinuteNumImpl<ResultPrecision::Extended>>>)
|
|
|
|
{
|
2023-07-16 18:07:22 +00:00
|
|
|
if ((a_comp.time.second > b_comp.time.second)
|
|
|
|
|| ((a_comp.time.second == b_comp.time.second) && ((a_comp.millisecond > b_comp.millisecond)
|
|
|
|
|| ((a_comp.millisecond == b_comp.millisecond) && (a_comp.microsecond > b_comp.microsecond)))))
|
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeSecondNumImpl<ResultPrecision::Extended>>>)
|
|
|
|
{
|
|
|
|
if ((a_comp.millisecond > b_comp.millisecond)
|
|
|
|
|| ((a_comp.millisecond == b_comp.millisecond) && (a_comp.microsecond > b_comp.microsecond)))
|
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<TransformX, TransformDateTime64<ToRelativeSubsecondNumImpl<1000>>>)
|
|
|
|
{
|
|
|
|
if (a_comp.microsecond > b_comp.microsecond)
|
2022-12-20 10:18:43 +00:00
|
|
|
res += adjust_value;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:22:35 +00:00
|
|
|
template <typename T>
|
|
|
|
static UInt32 getScale(const T & v)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, ColumnDateTime64>)
|
|
|
|
return v.getScale();
|
|
|
|
else if constexpr (std::is_same_v<T, DecimalField<DateTime64>>)
|
|
|
|
return v.getScale();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
static auto stripDecimalFieldValue(T && v)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2021-04-09 12:22:35 +00:00
|
|
|
if constexpr (std::is_same_v<std::decay_t<T>, DecimalField<DateTime64>>)
|
|
|
|
return v.getValue();
|
|
|
|
else
|
|
|
|
return v;
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2022-10-28 21:06:32 +00:00
|
|
|
private:
|
|
|
|
String name;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
/** dateDiff('unit', t1, t2, [timezone])
|
2022-12-20 10:18:43 +00:00
|
|
|
* age('unit', t1, t2, [timezone])
|
|
|
|
* t1 and t2 can be Date, Date32, DateTime or DateTime64
|
2018-09-26 00:31:40 +00:00
|
|
|
*
|
|
|
|
* If timezone is specified, it applied to both arguments.
|
|
|
|
* If not, timezones from datatypes t1 and t2 are used.
|
|
|
|
* If that timezones are not the same, the result is unspecified.
|
|
|
|
*
|
|
|
|
* Timezone matters because days can have different length.
|
|
|
|
*/
|
2022-12-20 10:18:43 +00:00
|
|
|
template <bool is_relative>
|
2018-09-26 00:31:40 +00:00
|
|
|
class FunctionDateDiff : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2022-12-20 10:18:43 +00:00
|
|
|
static constexpr auto name = is_relative ? "dateDiff" : "age";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionDateDiff>(); }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2018-09-26 00:31:40 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 3 && arguments.size() != 4)
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Number of arguments for function {} doesn't match: passed {}, should be 3 or 4",
|
2022-10-19 13:23:21 +00:00
|
|
|
getName(), arguments.size());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
if (!isString(arguments[0]))
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"First argument for function {} (unit) must be String",
|
|
|
|
getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2022-10-03 13:15:32 +00:00
|
|
|
if (!isDate(arguments[1]) && !isDate32(arguments[1]) && !isDateTime(arguments[1]) && !isDateTime64(arguments[1]))
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Second argument for function {} must be Date, Date32, DateTime or DateTime64",
|
|
|
|
getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2022-10-03 13:15:32 +00:00
|
|
|
if (!isDate(arguments[2]) && !isDate32(arguments[2]) && !isDateTime(arguments[2]) && !isDateTime64(arguments[2]))
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Third argument for function {} must be Date, Date32, DateTime or DateTime64",
|
|
|
|
getName()
|
|
|
|
);
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
if (arguments.size() == 4 && !isString(arguments[3]))
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Fourth argument for function {} (timezone) must be String",
|
|
|
|
getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeInt64>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 3}; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2020-10-17 21:41:50 +00:00
|
|
|
const auto * unit_column = checkAndGetColumnConst<ColumnString>(arguments[0].column.get());
|
2018-09-26 00:31:40 +00:00
|
|
|
if (!unit_column)
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"First argument for function {} must be constant String",
|
|
|
|
getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
String unit = Poco::toLower(unit_column->getValue<String>());
|
|
|
|
|
2020-10-17 21:41:50 +00:00
|
|
|
const IColumn & x = *arguments[1].column;
|
|
|
|
const IColumn & y = *arguments[2].column;
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
size_t rows = input_rows_count;
|
|
|
|
auto res = ColumnInt64::create(rows);
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
const auto & timezone_x = extractTimeZoneFromFunctionArguments(arguments, 3, 1);
|
|
|
|
const auto & timezone_y = extractTimeZoneFromFunctionArguments(arguments, 3, 2);
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2023-08-24 15:11:58 +00:00
|
|
|
if (unit == "year" || unit == "years" || unit == "yy" || unit == "yyyy")
|
2022-12-20 10:18:43 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeYearNumImpl<ResultPrecision::Extended>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "quarter" || unit == "quarters" || unit == "qq" || unit == "q")
|
2022-12-20 10:18:43 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeQuarterNumImpl<ResultPrecision::Extended>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "month" || unit == "months" || unit == "mm" || unit == "m")
|
2022-12-20 10:18:43 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeMonthNumImpl<ResultPrecision::Extended>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "week" || unit == "weeks" || unit == "wk" || unit == "ww")
|
2022-12-20 10:18:43 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeWeekNumImpl<ResultPrecision::Extended>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "day" || unit == "days" || unit == "dd" || unit == "d")
|
2022-12-20 10:18:43 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeDayNumImpl<ResultPrecision::Extended>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "hour" || unit == "hours" || unit == "hh" || unit == "h")
|
2022-12-20 10:18:43 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeHourNumImpl<ResultPrecision::Extended>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "minute" || unit == "minutes" || unit == "mi" || unit == "n")
|
2022-12-20 10:18:43 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeMinuteNumImpl<ResultPrecision::Extended>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "second" || unit == "seconds" || unit == "ss" || unit == "s")
|
2022-12-20 10:18:43 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeSecondNumImpl<ResultPrecision::Extended>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "millisecond" || unit == "milliseconds" || unit == "ms")
|
2023-07-16 18:07:22 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeSubsecondNumImpl<millisecond_multiplier>>(x, y, timezone_x, timezone_y, res->getData());
|
2023-08-24 15:11:58 +00:00
|
|
|
else if (unit == "microsecond" || unit == "microseconds" || unit == "us" || unit == "u")
|
2023-07-16 18:07:22 +00:00
|
|
|
impl.template dispatchForColumns<ToRelativeSubsecondNumImpl<microsecond_multiplier>>(x, y, timezone_x, timezone_y, res->getData());
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
2022-10-14 08:13:04 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
|
|
|
"Function {} does not support '{}' unit", getName(), unit);
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2020-10-17 21:41:50 +00:00
|
|
|
return res;
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
private:
|
2022-12-20 10:18:43 +00:00
|
|
|
DateDiffImpl<is_relative> impl{name};
|
2022-10-28 21:06:32 +00:00
|
|
|
};
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
|
2022-10-28 21:06:32 +00:00
|
|
|
/** TimeDiff(t1, t2)
|
|
|
|
* t1 and t2 can be Date or DateTime
|
|
|
|
*/
|
|
|
|
class FunctionTimeDiff : public IFunction
|
|
|
|
{
|
|
|
|
using ColumnDateTime64 = ColumnDecimal<DateTime64>;
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "TimeDiff";
|
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionTimeDiff>(); }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2022-10-28 21:06:32 +00:00
|
|
|
String getName() const override
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-28 21:06:32 +00:00
|
|
|
return name;
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 21:06:32 +00:00
|
|
|
bool isVariadic() const override { return false; }
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-28 21:06:32 +00:00
|
|
|
if (arguments.size() != 2)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Number of arguments for function {} doesn't match: passed {}, should be 2",
|
|
|
|
getName(), arguments.size());
|
2021-04-09 12:22:35 +00:00
|
|
|
|
2022-10-31 13:03:13 +00:00
|
|
|
if (!isDate(arguments[0]) && !isDate32(arguments[0]) && !isDateTime(arguments[0]) && !isDateTime64(arguments[0]))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"First argument for function {} must be Date, Date32, DateTime or DateTime64",
|
|
|
|
getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2022-10-31 13:03:13 +00:00
|
|
|
if (!isDate(arguments[1]) && !isDate32(arguments[1]) && !isDateTime(arguments[1]) && !isDateTime64(arguments[1]))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Second argument for function {} must be Date, Date32, DateTime or DateTime64",
|
|
|
|
getName()
|
|
|
|
);
|
2021-04-09 12:22:35 +00:00
|
|
|
|
2022-10-28 21:06:32 +00:00
|
|
|
return std::make_shared<DataTypeInt64>();
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 21:06:32 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {}; }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2022-10-28 21:06:32 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2021-04-09 12:22:35 +00:00
|
|
|
{
|
2022-10-28 21:06:32 +00:00
|
|
|
const IColumn & x = *arguments[0].column;
|
|
|
|
const IColumn & y = *arguments[1].column;
|
2021-04-09 12:22:35 +00:00
|
|
|
|
2022-10-28 21:06:32 +00:00
|
|
|
size_t rows = input_rows_count;
|
|
|
|
auto res = ColumnInt64::create(rows);
|
|
|
|
|
2022-10-29 08:33:05 +00:00
|
|
|
impl.dispatchForColumns<ToRelativeSecondNumImpl<ResultPrecision::Extended>>(x, y, DateLUT::instance(), DateLUT::instance(), res->getData());
|
2022-10-28 21:06:32 +00:00
|
|
|
|
|
|
|
return res;
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2022-10-28 21:06:32 +00:00
|
|
|
private:
|
2022-12-20 10:18:43 +00:00
|
|
|
DateDiffImpl<true> impl{name};
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(DateDiff)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-12-20 10:18:43 +00:00
|
|
|
factory.registerFunction<FunctionDateDiff<true>>({}, FunctionFactory::CaseInsensitive);
|
2023-04-06 14:01:05 +00:00
|
|
|
factory.registerAlias("date_diff", FunctionDateDiff<true>::name);
|
|
|
|
factory.registerAlias("DATE_DIFF", FunctionDateDiff<true>::name);
|
|
|
|
factory.registerAlias("timestampDiff", FunctionDateDiff<true>::name);
|
|
|
|
factory.registerAlias("timestamp_diff", FunctionDateDiff<true>::name);
|
|
|
|
factory.registerAlias("TIMESTAMP_DIFF", FunctionDateDiff<true>::name);
|
2022-11-08 16:39:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 21:06:32 +00:00
|
|
|
REGISTER_FUNCTION(TimeDiff)
|
|
|
|
{
|
2023-04-28 10:10:42 +00:00
|
|
|
factory.registerFunction<FunctionTimeDiff>(FunctionDocumentation{.description=R"(
|
2022-11-01 15:09:05 +00:00
|
|
|
Returns the difference between two dates or dates with time values. The difference is calculated in seconds units (see toRelativeSecondNum).
|
2022-11-01 17:29:15 +00:00
|
|
|
It is same as `dateDiff` and was added only for MySQL support. `dateDiff` is preferred.
|
2022-11-01 15:09:05 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
[example:typical]
|
|
|
|
)",
|
2023-04-28 10:10:42 +00:00
|
|
|
.examples{
|
|
|
|
{"typical", "SELECT timeDiff(UTCTimestamp(), now());", ""}},
|
|
|
|
.categories{"Dates and Times"}}, FunctionFactory::CaseInsensitive);
|
2022-10-28 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 10:18:43 +00:00
|
|
|
REGISTER_FUNCTION(Age)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionDateDiff<false>>({}, FunctionFactory::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|