2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2022-02-15 19:04:59 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <Core/AccurateComparison.h>
|
2023-08-11 05:47:46 +00:00
|
|
|
#include <Core/DecimalFunctions.h>
|
2021-12-21 13:41:53 +00:00
|
|
|
#include <Common/DateLUTImpl.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
2021-07-15 11:41:52 +00:00
|
|
|
#include <DataTypes/DataTypeDate32.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-11-04 14:06:22 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2023-10-19 21:52:23 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2020-02-24 15:42:40 +00:00
|
|
|
#include <Columns/ColumnsNumber.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>
|
2020-02-24 15:42:40 +00:00
|
|
|
#include <Functions/castTypeToEither.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
#include <IO/ReadBufferFromString.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2023-10-19 21:52:23 +00:00
|
|
|
#include <IO/parseDateTimeBestEffort.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2022-09-12 04:32:31 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2022-02-16 14:15:07 +00:00
|
|
|
extern const int DECIMAL_OVERFLOW;
|
2018-09-26 00:31:40 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2022-02-16 14:15:07 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 13:49:40 +00:00
|
|
|
/// Type of first argument of 'execute' function overload defines what INPUT DataType it is used for.
|
|
|
|
/// Return type defines what is the OUTPUT (return) type of the CH function.
|
|
|
|
/// Corresponding types:
|
|
|
|
/// - UInt16 => DataTypeDate
|
|
|
|
/// - UInt32 => DataTypeDateTime
|
2023-08-11 05:47:46 +00:00
|
|
|
/// - Int32 => DataTypeDate32
|
2019-10-10 13:49:40 +00:00
|
|
|
/// - DateTime64 => DataTypeDateTime64
|
2023-08-11 05:47:46 +00:00
|
|
|
/// - Int8 => error
|
2019-10-10 13:49:40 +00:00
|
|
|
/// Please note that INPUT and OUTPUT types may differ, e.g.:
|
|
|
|
/// - 'AddSecondsImpl::execute(UInt32, ...) -> UInt32' is available to the ClickHouse users as 'addSeconds(DateTime, ...) -> DateTime'
|
|
|
|
/// - 'AddSecondsImpl::execute(UInt16, ...) -> UInt32' is available to the ClickHouse users as 'addSeconds(Date, ...) -> DateTime'
|
|
|
|
|
2022-02-07 18:44:14 +00:00
|
|
|
struct AddNanosecondsImpl
|
|
|
|
{
|
|
|
|
static constexpr auto name = "addNanoseconds";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-18 10:45:47 +00:00
|
|
|
Int64 multiplier = DecimalUtils::scaleMultiplier<DateTime64>(9 - scale);
|
2023-08-11 05:47:46 +00:00
|
|
|
return DateTime64(DecimalUtils::multiplyAdd(t.value, multiplier, delta));
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(UInt32 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2022-02-13 23:52:56 +00:00
|
|
|
Int64 multiplier = DecimalUtils::scaleMultiplier<DateTime64>(9);
|
2023-08-11 05:47:46 +00:00
|
|
|
return DateTime64(DecimalUtils::multiplyAdd(static_cast<Int64>(t), multiplier, delta));
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int8 execute(UInt16, Int64, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2023-08-11 05:47:46 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "addNanoseconds() cannot be used with Date");
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int8 execute(Int32, Int64, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2023-08-11 05:47:46 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "addNanoseconds() cannot be used with Date32");
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2022-02-07 18:44:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AddMicrosecondsImpl
|
|
|
|
{
|
|
|
|
static constexpr auto name = "addMicroseconds";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-18 10:45:47 +00:00
|
|
|
Int64 multiplier = DecimalUtils::scaleMultiplier<DateTime64>(std::abs(6 - scale));
|
2023-08-11 05:47:46 +00:00
|
|
|
return DateTime64(scale <= 6
|
|
|
|
? DecimalUtils::multiplyAdd(t.value, multiplier, delta)
|
|
|
|
: DecimalUtils::multiplyAdd(delta, multiplier, t.value));
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(UInt32 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2022-02-13 23:52:56 +00:00
|
|
|
Int64 multiplier = DecimalUtils::scaleMultiplier<DateTime64>(6);
|
2023-08-11 05:47:46 +00:00
|
|
|
return DateTime64(DecimalUtils::multiplyAdd(static_cast<Int64>(t), multiplier, delta));
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int8 execute(UInt16, Int64, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2023-08-11 05:47:46 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "addMicroseconds() cannot be used with Date");
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int8 execute(Int32, Int64, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2023-08-11 05:47:46 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "addMicroseconds() cannot be used with Date32");
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2022-02-07 18:44:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AddMillisecondsImpl
|
|
|
|
{
|
|
|
|
static constexpr auto name = "addMilliseconds";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-18 10:45:47 +00:00
|
|
|
Int64 multiplier = DecimalUtils::scaleMultiplier<DateTime64>(std::abs(3 - scale));
|
2023-08-11 05:47:46 +00:00
|
|
|
return DateTime64(scale <= 3
|
|
|
|
? DecimalUtils::multiplyAdd(t.value, multiplier, delta)
|
|
|
|
: DecimalUtils::multiplyAdd(delta, multiplier, t.value));
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(UInt32 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2022-02-13 23:52:56 +00:00
|
|
|
Int64 multiplier = DecimalUtils::scaleMultiplier<DateTime64>(3);
|
2023-08-11 05:47:46 +00:00
|
|
|
return DateTime64(DecimalUtils::multiplyAdd(static_cast<Int64>(t), multiplier, delta));
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int8 execute(UInt16, Int64, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2023-08-11 05:47:46 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "addMilliseconds() cannot be used with Date");
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int8 execute(Int32, Int64, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2022-02-07 18:44:14 +00:00
|
|
|
{
|
2023-08-11 05:47:46 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "addMilliseconds() cannot be used with Date32");
|
2022-02-07 18:44:14 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2022-02-07 18:44:14 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
struct AddSecondsImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "addSeconds";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2023-08-11 05:47:46 +00:00
|
|
|
return DateTime64(DecimalUtils::multiplyAdd(delta, DecimalUtils::scaleMultiplier<DateTime64>(scale), t.value));
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt32 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(t + delta);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int64 execute(Int32 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2021-07-15 11:41:52 +00:00
|
|
|
{
|
2022-02-13 23:52:56 +00:00
|
|
|
// use default datetime64 scale
|
2023-10-20 15:46:25 +00:00
|
|
|
static_assert(DataTypeDateTime64::default_scale == 3);
|
2022-02-13 23:52:56 +00:00
|
|
|
return (time_zone.fromDayNum(ExtendedDayNum(d)) + delta) * 1000;
|
2021-07-15 11:41:52 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(time_zone.fromDayNum(DayNum(d)) + delta);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
struct AddMinutesImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "addMinutes";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-18 11:53:22 +00:00
|
|
|
return t + 60 * delta * DecimalUtils::scaleMultiplier<DateTime64>(scale);
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt32 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(t + delta * 60);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int64 execute(Int32 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2021-07-15 11:41:52 +00:00
|
|
|
{
|
2022-02-13 23:52:56 +00:00
|
|
|
// use default datetime64 scale
|
2023-10-20 15:46:25 +00:00
|
|
|
static_assert(DataTypeDateTime64::default_scale == 3);
|
2022-02-13 23:52:56 +00:00
|
|
|
return (time_zone.fromDayNum(ExtendedDayNum(d)) + delta * 60) * 1000;
|
2021-07-15 11:41:52 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(time_zone.fromDayNum(DayNum(d)) + delta * 60);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
struct AddHoursImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "addHours";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-18 11:53:22 +00:00
|
|
|
return t + 3600 * delta * DecimalUtils::scaleMultiplier<DateTime64>(scale);
|
2020-04-17 13:26:44 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt32 t, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(t + delta * 3600);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int64 execute(Int32 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2021-07-15 11:41:52 +00:00
|
|
|
{
|
2022-02-13 23:52:56 +00:00
|
|
|
// use default datetime64 scale
|
2023-10-20 15:46:25 +00:00
|
|
|
static_assert(DataTypeDateTime64::default_scale == 3);
|
2022-02-13 23:52:56 +00:00
|
|
|
return (time_zone.fromDayNum(ExtendedDayNum(d)) + delta * 3600) * 1000;
|
2021-07-15 11:41:52 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(time_zone.fromDayNum(DayNum(d)) + delta * 3600);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
struct AddDaysImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "addDays";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-15 23:43:08 +00:00
|
|
|
auto multiplier = DecimalUtils::scaleMultiplier<DateTime64>(scale);
|
|
|
|
auto d = std::div(t, multiplier);
|
2022-02-18 11:53:22 +00:00
|
|
|
return time_zone.addDays(d.quot, delta) * multiplier + d.rem;
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt32 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(time_zone.addDays(t, delta));
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt16 execute(UInt16 d, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
return d + delta;
|
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int32 execute(Int32 d, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2021-07-15 11:41:52 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<Int32>(d + delta);
|
2021-07-15 11:41:52 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
struct AddWeeksImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "addWeeks";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16 scale)
|
2020-04-17 13:26:44 +00:00
|
|
|
{
|
2022-02-15 23:43:08 +00:00
|
|
|
auto multiplier = DecimalUtils::scaleMultiplier<DateTime64>(scale);
|
|
|
|
auto d = std::div(t, multiplier);
|
2022-02-18 11:53:22 +00:00
|
|
|
return time_zone.addDays(d.quot, delta * 7) * multiplier + d.rem;
|
2020-04-17 13:26:44 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt32 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(time_zone.addWeeks(t, delta));
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt16 execute(UInt16 d, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2021-07-15 11:41:52 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt16>(d + delta * 7);
|
2021-07-15 11:41:52 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int32 execute(Int32 d, Int64 delta, const DateLUTImpl &, const DateLUTImpl &, UInt16)
|
2021-06-17 08:03:31 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<Int32>(d + delta * 7);
|
2021-06-17 08:03:31 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
struct AddMonthsImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "addMonths";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-15 23:43:08 +00:00
|
|
|
auto multiplier = DecimalUtils::scaleMultiplier<DateTime64>(scale);
|
|
|
|
auto d = std::div(t, multiplier);
|
2022-02-18 11:53:22 +00:00
|
|
|
return time_zone.addMonths(d.quot, delta) * multiplier + d.rem;
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt32 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(time_zone.addMonths(t, delta));
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt16 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2021-11-08 07:19:31 +00:00
|
|
|
return time_zone.addMonths(DayNum(d), delta);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int32 execute(Int32 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2021-07-15 11:41:52 +00:00
|
|
|
{
|
|
|
|
return time_zone.addMonths(ExtendedDayNum(d), delta);
|
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
struct AddQuartersImpl
|
2018-12-18 13:16:48 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "addQuarters";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-15 23:43:08 +00:00
|
|
|
auto multiplier = DecimalUtils::scaleMultiplier<DateTime64>(scale);
|
|
|
|
auto d = std::div(t, multiplier);
|
2022-02-18 11:53:22 +00:00
|
|
|
return time_zone.addQuarters(d.quot, delta) * multiplier + d.rem;
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static UInt32 execute(UInt32 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-12-18 13:16:48 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(time_zone.addQuarters(t, delta));
|
2018-12-18 13:16:48 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static UInt16 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2021-07-15 11:41:52 +00:00
|
|
|
{
|
2021-11-08 07:19:31 +00:00
|
|
|
return time_zone.addQuarters(DayNum(d), delta);
|
2021-07-15 11:41:52 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static Int32 execute(Int32 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2021-06-17 08:03:31 +00:00
|
|
|
{
|
|
|
|
return time_zone.addQuarters(ExtendedDayNum(d), delta);
|
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-12-18 13:16:48 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
struct AddYearsImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "addYears";
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED DateTime64 execute(DateTime64 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16 scale)
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-02-15 23:43:08 +00:00
|
|
|
auto multiplier = DecimalUtils::scaleMultiplier<DateTime64>(scale);
|
|
|
|
auto d = std::div(t, multiplier);
|
2022-02-18 11:53:22 +00:00
|
|
|
return time_zone.addYears(d.quot, delta) * multiplier + d.rem;
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt32 execute(UInt32 t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<UInt32>(time_zone.addYears(t, delta));
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED UInt16 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2021-11-08 07:19:31 +00:00
|
|
|
return time_zone.addYears(DayNum(d), delta);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED Int32 execute(Int32 d, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl &, UInt16)
|
2021-07-15 11:41:52 +00:00
|
|
|
{
|
|
|
|
return time_zone.addYears(ExtendedDayNum(d), delta);
|
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
static DateTime64 execute(std::string_view s, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(s);
|
|
|
|
DateTime64 t;
|
|
|
|
parseDateTime64BestEffort(t, scale, buf, time_zone, utc_time_zone);
|
|
|
|
return execute(t, delta, time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Transform>
|
2019-10-10 13:49:40 +00:00
|
|
|
struct SubtractIntervalImpl : public Transform
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2019-10-10 13:49:40 +00:00
|
|
|
using Transform::Transform;
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2019-10-09 13:18:38 +00:00
|
|
|
template <typename T>
|
2023-10-19 21:52:23 +00:00
|
|
|
NO_SANITIZE_UNDEFINED auto execute(T t, Int64 delta, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone, UInt16 scale) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2021-01-21 12:01:24 +00:00
|
|
|
/// Signed integer overflow is Ok.
|
2023-10-19 21:52:23 +00:00
|
|
|
return Transform::execute(t, -delta, time_zone, utc_time_zone, scale);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-09 07:28:15 +00:00
|
|
|
struct SubtractNanosecondsImpl : SubtractIntervalImpl<AddNanosecondsImpl> { static constexpr auto name = "subtractNanoseconds"; };
|
|
|
|
struct SubtractMicrosecondsImpl : SubtractIntervalImpl<AddMicrosecondsImpl> { static constexpr auto name = "subtractMicroseconds"; };
|
|
|
|
struct SubtractMillisecondsImpl : SubtractIntervalImpl<AddMillisecondsImpl> { static constexpr auto name = "subtractMilliseconds"; };
|
2018-09-26 00:31:40 +00:00
|
|
|
struct SubtractSecondsImpl : SubtractIntervalImpl<AddSecondsImpl> { static constexpr auto name = "subtractSeconds"; };
|
|
|
|
struct SubtractMinutesImpl : SubtractIntervalImpl<AddMinutesImpl> { static constexpr auto name = "subtractMinutes"; };
|
|
|
|
struct SubtractHoursImpl : SubtractIntervalImpl<AddHoursImpl> { static constexpr auto name = "subtractHours"; };
|
|
|
|
struct SubtractDaysImpl : SubtractIntervalImpl<AddDaysImpl> { static constexpr auto name = "subtractDays"; };
|
|
|
|
struct SubtractWeeksImpl : SubtractIntervalImpl<AddWeeksImpl> { static constexpr auto name = "subtractWeeks"; };
|
|
|
|
struct SubtractMonthsImpl : SubtractIntervalImpl<AddMonthsImpl> { static constexpr auto name = "subtractMonths"; };
|
2018-12-18 13:16:48 +00:00
|
|
|
struct SubtractQuartersImpl : SubtractIntervalImpl<AddQuartersImpl> { static constexpr auto name = "subtractQuarters"; };
|
2018-09-26 00:31:40 +00:00
|
|
|
struct SubtractYearsImpl : SubtractIntervalImpl<AddYearsImpl> { static constexpr auto name = "subtractYears"; };
|
|
|
|
|
|
|
|
|
2019-10-09 13:18:38 +00:00
|
|
|
template <typename Transform>
|
2023-10-20 15:46:25 +00:00
|
|
|
struct Processor
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2019-10-09 13:18:38 +00:00
|
|
|
const Transform transform;
|
|
|
|
|
2023-10-20 15:46:25 +00:00
|
|
|
explicit Processor(Transform transform_)
|
2019-10-09 13:18:38 +00:00
|
|
|
: transform(std::move(transform_))
|
|
|
|
{}
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
template <typename FromColumnType, typename ToColumnType>
|
2024-07-26 10:28:01 +00:00
|
|
|
void NO_INLINE vectorConstant(const FromColumnType & col_from, ToColumnType & col_to, Int64 delta, const DateLUTImpl & time_zone, UInt16 scale, size_t input_rows_count) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2023-10-19 21:52:23 +00:00
|
|
|
static const DateLUTImpl & utc_time_zone = DateLUT::instance("UTC");
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
if constexpr (std::is_same_v<FromColumnType, ColumnString>)
|
|
|
|
{
|
|
|
|
auto & vec_to = col_to.getData();
|
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
vec_to.resize(input_rows_count);
|
2023-10-19 21:52:23 +00:00
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
for (size_t i = 0 ; i < input_rows_count; ++i)
|
2023-10-19 21:52:23 +00:00
|
|
|
{
|
|
|
|
std::string_view from = col_from.getDataAt(i).toView();
|
|
|
|
vec_to[i] = transform.execute(from, checkOverflow(delta), time_zone, utc_time_zone, scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto & vec_from = col_from.getData();
|
|
|
|
auto & vec_to = col_to.getData();
|
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
vec_to.resize(input_rows_count);
|
2023-10-19 21:52:23 +00:00
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2023-10-19 21:52:23 +00:00
|
|
|
vec_to[i] = transform.execute(vec_from[i], checkOverflow(delta), time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
template <typename FromColumnType, typename ToColumnType>
|
2024-07-26 10:28:01 +00:00
|
|
|
void vectorVector(const FromColumnType & col_from, ToColumnType & col_to, const IColumn & delta, const DateLUTImpl & time_zone, UInt16 scale, size_t input_rows_count) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2020-02-24 15:42:40 +00:00
|
|
|
castTypeToEither<
|
|
|
|
ColumnUInt8, ColumnUInt16, ColumnUInt32, ColumnUInt64,
|
|
|
|
ColumnInt8, ColumnInt16, ColumnInt32, ColumnInt64,
|
|
|
|
ColumnFloat32, ColumnFloat64>(
|
2024-07-26 10:28:01 +00:00
|
|
|
&delta, [&](const auto & column){ vectorVector(col_from, col_to, column, time_zone, scale, input_rows_count); return true; });
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
template <typename FromType, typename ToColumnType>
|
2024-07-26 10:28:01 +00:00
|
|
|
void constantVector(const FromType & from, ToColumnType & col_to, const IColumn & delta, const DateLUTImpl & time_zone, UInt16 scale, size_t input_rows_count) const
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2020-02-24 15:42:40 +00:00
|
|
|
castTypeToEither<
|
|
|
|
ColumnUInt8, ColumnUInt16, ColumnUInt32, ColumnUInt64,
|
|
|
|
ColumnInt8, ColumnInt16, ColumnInt32, ColumnInt64,
|
|
|
|
ColumnFloat32, ColumnFloat64>(
|
2024-07-26 10:28:01 +00:00
|
|
|
&delta, [&](const auto & column){ constantVector(from, col_to, column, time_zone, scale, input_rows_count); return true; });
|
2020-02-24 15:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-02-15 19:04:59 +00:00
|
|
|
template <typename Value>
|
|
|
|
static Int64 checkOverflow(Value val)
|
|
|
|
{
|
|
|
|
Int64 result;
|
2022-02-16 14:15:07 +00:00
|
|
|
if (accurate::convertNumeric<Value, Int64, false>(val, result))
|
2022-02-15 19:04:59 +00:00
|
|
|
return result;
|
2023-01-23 21:13:58 +00:00
|
|
|
throw DB::Exception(ErrorCodes::DECIMAL_OVERFLOW, "Numeric overflow");
|
2022-02-15 19:04:59 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
template <typename FromColumnType, typename ToColumnType, typename DeltaColumnType>
|
2021-03-23 23:07:13 +00:00
|
|
|
NO_INLINE NO_SANITIZE_UNDEFINED void vectorVector(
|
2024-07-26 10:28:01 +00:00
|
|
|
const FromColumnType & col_from, ToColumnType & col_to, const DeltaColumnType & delta, const DateLUTImpl & time_zone, UInt16 scale, size_t input_rows_count) const
|
2020-02-24 15:42:40 +00:00
|
|
|
{
|
2023-10-19 21:52:23 +00:00
|
|
|
static const DateLUTImpl & utc_time_zone = DateLUT::instance("UTC");
|
|
|
|
|
|
|
|
if constexpr (std::is_same_v<FromColumnType, ColumnString>)
|
|
|
|
{
|
|
|
|
auto & vec_to = col_to.getData();
|
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
vec_to.resize(input_rows_count);
|
2023-10-19 21:52:23 +00:00
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
for (size_t i = 0 ; i < input_rows_count; ++i)
|
2023-10-19 21:52:23 +00:00
|
|
|
{
|
|
|
|
std::string_view from = col_from.getDataAt(i).toView();
|
|
|
|
vec_to[i] = transform.execute(from, checkOverflow(delta.getData()[i]), time_zone, utc_time_zone, scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto & vec_from = col_from.getData();
|
|
|
|
auto & vec_to = col_to.getData();
|
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
vec_to.resize(input_rows_count);
|
2023-10-19 21:52:23 +00:00
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2023-10-19 21:52:23 +00:00
|
|
|
vec_to[i] = transform.execute(vec_from[i], checkOverflow(delta.getData()[i]), time_zone, utc_time_zone, scale);
|
|
|
|
}
|
2020-02-24 15:42:40 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 21:52:23 +00:00
|
|
|
template <typename FromType, typename ToColumnType, typename DeltaColumnType>
|
2021-03-23 23:07:13 +00:00
|
|
|
NO_INLINE NO_SANITIZE_UNDEFINED void constantVector(
|
2024-07-26 10:28:01 +00:00
|
|
|
const FromType & from, ToColumnType & col_to, const DeltaColumnType & delta, const DateLUTImpl & time_zone, UInt16 scale, size_t input_rows_count) const
|
2020-02-24 15:42:40 +00:00
|
|
|
{
|
2023-10-19 21:52:23 +00:00
|
|
|
static const DateLUTImpl & utc_time_zone = DateLUT::instance("UTC");
|
|
|
|
|
2023-10-23 14:07:14 +00:00
|
|
|
auto & vec_to = col_to.getData();
|
2023-10-19 21:52:23 +00:00
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
vec_to.resize(input_rows_count);
|
2023-10-19 21:52:23 +00:00
|
|
|
|
2024-07-26 10:28:01 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2023-10-23 14:07:14 +00:00
|
|
|
vec_to[i] = transform.execute(from, checkOverflow(delta.getData()[i]), time_zone, utc_time_zone, scale);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-10-09 13:18:38 +00:00
|
|
|
template <typename FromDataType, typename ToDataType, typename Transform>
|
2018-09-26 00:31:40 +00:00
|
|
|
struct DateTimeAddIntervalImpl
|
|
|
|
{
|
2024-07-26 10:28:01 +00:00
|
|
|
static ColumnPtr execute(Transform transform, const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, UInt16 scale, size_t input_rows_count)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2019-10-09 13:18:38 +00:00
|
|
|
using FromValueType = typename FromDataType::FieldType;
|
|
|
|
using FromColumnType = typename FromDataType::ColumnType;
|
|
|
|
using ToColumnType = typename ToDataType::ColumnType;
|
|
|
|
|
2023-10-20 15:46:25 +00:00
|
|
|
const IColumn & source_column = *arguments[0].column;
|
|
|
|
const IColumn & delta_column = *arguments[1].column;
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2020-10-17 15:22:42 +00:00
|
|
|
const DateLUTImpl & time_zone = extractTimeZoneFromFunctionArguments(arguments, 2, 0);
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2020-10-17 15:22:42 +00:00
|
|
|
auto result_col = result_type->createColumn();
|
2019-10-09 13:18:38 +00:00
|
|
|
auto col_to = assert_cast<ToColumnType *>(result_col.get());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2023-10-20 15:46:25 +00:00
|
|
|
auto processor = Processor<Transform>{std::move(transform)};
|
|
|
|
|
|
|
|
if (const auto * sources = checkAndGetColumn<FromColumnType>(&source_column))
|
2019-10-09 13:18:38 +00:00
|
|
|
{
|
2018-09-26 00:31:40 +00:00
|
|
|
if (const auto * delta_const_column = typeid_cast<const ColumnConst *>(&delta_column))
|
2024-07-26 10:28:01 +00:00
|
|
|
processor.vectorConstant(*sources, *col_to, delta_const_column->getInt(0), time_zone, scale, input_rows_count);
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
2024-07-26 10:28:01 +00:00
|
|
|
processor.vectorVector(*sources, *col_to, delta_column, time_zone, scale, input_rows_count);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2023-10-20 15:46:25 +00:00
|
|
|
else if (const auto * sources_const = checkAndGetColumnConst<FromColumnType>(&source_column))
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2023-10-20 15:46:25 +00:00
|
|
|
processor.constantVector(
|
2020-06-28 20:20:49 +00:00
|
|
|
sources_const->template getValue<FromValueType>(),
|
2024-07-26 10:28:01 +00:00
|
|
|
*col_to, delta_column, time_zone, scale, input_rows_count);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-15 19:04:59 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}",
|
|
|
|
arguments[0].column->getName(), Transform::name);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2019-10-09 13:18:38 +00:00
|
|
|
|
2020-10-17 15:22:42 +00:00
|
|
|
return result_col;
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-10 13:49:40 +00:00
|
|
|
namespace date_and_time_type_details
|
|
|
|
{
|
|
|
|
// Compile-time mapping of value (DataType::FieldType) types to corresponding DataType
|
|
|
|
template <typename FieldType> struct ResultDataTypeMap {};
|
|
|
|
template <> struct ResultDataTypeMap<UInt16> { using ResultDataType = DataTypeDate; };
|
|
|
|
template <> struct ResultDataTypeMap<UInt32> { using ResultDataType = DataTypeDateTime; };
|
2021-07-15 11:41:52 +00:00
|
|
|
template <> struct ResultDataTypeMap<Int32> { using ResultDataType = DataTypeDate32; };
|
2019-10-10 13:49:40 +00:00
|
|
|
template <> struct ResultDataTypeMap<DateTime64> { using ResultDataType = DataTypeDateTime64; };
|
2020-04-17 13:26:44 +00:00
|
|
|
template <> struct ResultDataTypeMap<Int64> { using ResultDataType = DataTypeDateTime64; };
|
2023-10-20 15:46:25 +00:00
|
|
|
template <> struct ResultDataTypeMap<Int8> { using ResultDataType = DataTypeInt8; }; // error
|
2019-10-10 13:49:40 +00:00
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
template <typename Transform>
|
|
|
|
class FunctionDateOrDateTimeAddInterval : 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<FunctionDateOrDateTimeAddInterval>(); }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2023-10-20 15:46:25 +00:00
|
|
|
String getName() const override { return name; }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
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; }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 2 && arguments.size() != 3)
|
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 or 3",
|
|
|
|
getName(), arguments.size());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2023-10-19 21:52:23 +00:00
|
|
|
if (!isDateOrDate32OrDateTimeOrDateTime64(arguments[0].type) && !isString(arguments[0].type))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of first argument of function {}. "
|
2023-10-19 21:52:23 +00:00
|
|
|
"Must be a date, a date with time or a String", arguments[0].type->getName(), getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-21 13:47:29 +00:00
|
|
|
if (!WhichDataType(arguments[0].type).isDateTimeOrDateTime64())
|
2023-10-20 15:46:25 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of first argument of function {}. "
|
2024-03-21 13:47:29 +00:00
|
|
|
"Must be a DateTime/DateTime64", arguments[0].type->getName(), getName());
|
2023-10-20 15:46:25 +00:00
|
|
|
|
|
|
|
if (!WhichDataType(arguments[2].type).isString())
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of third argument of function {}. "
|
|
|
|
"The 3rd argument must be a constant string with a timezone name. "
|
2023-01-23 21:13:58 +00:00
|
|
|
"The timezone argument is allowed only when the 1st argument has the type DateTime",
|
2023-10-20 15:46:25 +00:00
|
|
|
arguments[2].type->getName(), getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 15:46:25 +00:00
|
|
|
if (!isNativeNumber(arguments[1].type))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Second argument for function {} must be a number", getName());
|
|
|
|
|
2019-10-09 13:18:38 +00:00
|
|
|
switch (arguments[0].type->getTypeId())
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2019-10-09 13:18:38 +00:00
|
|
|
case TypeIndex::Date:
|
2019-10-10 13:49:40 +00:00
|
|
|
return resolveReturnType<DataTypeDate>(arguments);
|
2021-07-15 11:41:52 +00:00
|
|
|
case TypeIndex::Date32:
|
|
|
|
return resolveReturnType<DataTypeDate32>(arguments);
|
2019-10-09 13:18:38 +00:00
|
|
|
case TypeIndex::DateTime:
|
2019-10-10 13:49:40 +00:00
|
|
|
return resolveReturnType<DataTypeDateTime>(arguments);
|
2019-10-09 13:18:38 +00:00
|
|
|
case TypeIndex::DateTime64:
|
2022-02-13 23:52:56 +00:00
|
|
|
return resolveReturnType<DataTypeDateTime64>(arguments);
|
2023-10-19 21:52:23 +00:00
|
|
|
case TypeIndex::String:
|
|
|
|
return resolveReturnType<DataTypeString>(arguments);
|
2019-10-09 13:18:38 +00:00
|
|
|
default:
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Invalid type of 1st argument of function {}: "
|
|
|
|
"{}, expected: Date, DateTime or DateTime64.", getName(), arguments[0].type->getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
2019-10-09 13:18:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 20:20:49 +00:00
|
|
|
/// Helper templates to deduce return type based on argument type, since some overloads may promote or denote types,
|
|
|
|
/// e.g. addSeconds(Date, 1) => DateTime
|
2019-10-09 13:18:38 +00:00
|
|
|
template <typename FieldType>
|
2023-10-19 21:52:23 +00:00
|
|
|
using TransformExecuteReturnType = decltype(std::declval<Transform>().execute(FieldType(), 0, std::declval<DateLUTImpl>(), std::declval<DateLUTImpl>(), 0));
|
2019-10-09 13:18:38 +00:00
|
|
|
|
2020-08-08 00:47:03 +00:00
|
|
|
// Deduces RETURN DataType from INPUT DataType, based on return type of Transform{}.execute(INPUT_TYPE, UInt64, DateLUTImpl).
|
2019-10-09 13:18:38 +00:00
|
|
|
// e.g. for Transform-type that has execute()-overload with 'UInt16' input and 'UInt32' return,
|
|
|
|
// argument type is expected to be 'Date', and result type is deduced to be 'DateTime'.
|
2019-10-10 13:53:53 +00:00
|
|
|
template <typename FromDataType>
|
|
|
|
using TransformResultDataType = typename date_and_time_type_details::ResultDataTypeMap<TransformExecuteReturnType<typename FromDataType::FieldType>>::ResultDataType;
|
2019-10-09 13:18:38 +00:00
|
|
|
|
2019-10-10 13:49:40 +00:00
|
|
|
template <typename FromDataType>
|
2019-10-09 13:18:38 +00:00
|
|
|
DataTypePtr resolveReturnType(const ColumnsWithTypeAndName & arguments) const
|
|
|
|
{
|
2019-10-10 13:53:53 +00:00
|
|
|
using ResultDataType = TransformResultDataType<FromDataType>;
|
2019-10-09 13:18:38 +00:00
|
|
|
|
|
|
|
if constexpr (std::is_same_v<ResultDataType, DataTypeDate>)
|
|
|
|
return std::make_shared<DataTypeDate>();
|
2021-07-15 11:41:52 +00:00
|
|
|
else if constexpr (std::is_same_v<ResultDataType, DataTypeDate32>)
|
|
|
|
return std::make_shared<DataTypeDate32>();
|
2019-10-09 13:18:38 +00:00
|
|
|
else if constexpr (std::is_same_v<ResultDataType, DataTypeDateTime>)
|
2023-06-10 12:53:02 +00:00
|
|
|
return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 2, 0, false));
|
2022-02-13 23:52:56 +00:00
|
|
|
else if constexpr (std::is_same_v<ResultDataType, DataTypeDateTime64>)
|
|
|
|
{
|
2022-12-14 20:35:38 +00:00
|
|
|
static constexpr auto target_scale = std::invoke(
|
|
|
|
[]() -> std::optional<UInt32>
|
|
|
|
{
|
|
|
|
if constexpr (std::is_base_of_v<AddNanosecondsImpl, Transform>)
|
|
|
|
return 9;
|
|
|
|
else if constexpr (std::is_base_of_v<AddMicrosecondsImpl, Transform>)
|
|
|
|
return 6;
|
|
|
|
else if constexpr (std::is_base_of_v<AddMillisecondsImpl, Transform>)
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
|
2023-06-10 12:53:02 +00:00
|
|
|
auto timezone = extractTimeZoneNameFromFunctionArguments(arguments, 2, 0, false);
|
2022-12-14 20:35:38 +00:00
|
|
|
if (const auto* datetime64_type = typeid_cast<const DataTypeDateTime64 *>(arguments[0].type.get()))
|
2022-02-13 23:52:56 +00:00
|
|
|
{
|
2022-12-14 20:35:38 +00:00
|
|
|
const auto from_scale = datetime64_type->getScale();
|
|
|
|
return std::make_shared<DataTypeDateTime64>(std::max(from_scale, target_scale.value_or(from_scale)), std::move(timezone));
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 20:35:38 +00:00
|
|
|
return std::make_shared<DataTypeDateTime64>(target_scale.value_or(DataTypeDateTime64::default_scale), std::move(timezone));
|
2022-02-13 23:52:56 +00:00
|
|
|
}
|
2023-08-11 05:47:46 +00:00
|
|
|
else if constexpr (std::is_same_v<ResultDataType, DataTypeInt8>)
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "{} cannot be used with {}", getName(), arguments[0].type->getName());
|
2019-11-11 04:15:24 +00:00
|
|
|
|
2022-09-12 04:32:31 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unexpected result type in datetime add interval function");
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {2}; }
|
|
|
|
|
2024-07-26 10:28:01 +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 15:22:42 +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())
|
2024-07-26 10:28:01 +00:00
|
|
|
return DateTimeAddIntervalImpl<DataTypeDate, TransformResultDataType<DataTypeDate>, Transform>::execute(Transform{}, arguments, result_type, 0, input_rows_count);
|
2021-07-15 11:41:52 +00:00
|
|
|
if (which.isDate32())
|
2024-07-26 10:28:01 +00:00
|
|
|
return DateTimeAddIntervalImpl<DataTypeDate32, TransformResultDataType<DataTypeDate32>, Transform>::execute(
|
|
|
|
Transform{}, arguments, result_type, 0, input_rows_count);
|
2018-09-26 00:31:40 +00:00
|
|
|
if (which.isDateTime())
|
2024-07-26 10:28:01 +00:00
|
|
|
return DateTimeAddIntervalImpl<DataTypeDateTime, TransformResultDataType<DataTypeDateTime>, Transform>::execute(
|
|
|
|
Transform{}, arguments, result_type, 0, input_rows_count);
|
2023-10-19 21:52:23 +00:00
|
|
|
if (which.isDateTime64())
|
2019-10-09 13:18:38 +00:00
|
|
|
{
|
2023-10-19 21:52:23 +00:00
|
|
|
const auto * datetime64_type = assert_cast<const DataTypeDateTime64 *>(from_type);
|
2022-02-13 23:52:56 +00:00
|
|
|
auto from_scale = datetime64_type->getScale();
|
2024-07-26 10:28:01 +00:00
|
|
|
return DateTimeAddIntervalImpl<DataTypeDateTime64, TransformResultDataType<DataTypeDateTime64>, Transform>::execute(
|
|
|
|
Transform{}, arguments, result_type, from_scale, input_rows_count);
|
2019-10-09 13:18:38 +00:00
|
|
|
}
|
2023-10-19 21:52:23 +00:00
|
|
|
if (which.isString())
|
2024-07-26 10:28:01 +00:00
|
|
|
return DateTimeAddIntervalImpl<DataTypeString, DataTypeDateTime64, Transform>::execute(
|
|
|
|
Transform{}, arguments, result_type, 3, input_rows_count);
|
2023-10-20 15:46:25 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of first argument of function {}",
|
|
|
|
arguments[0].type->getName(),
|
|
|
|
getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|