ClickHouse/src/Functions/toStartOfInterval.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

267 lines
12 KiB
C++
Raw Normal View History

#include <base/arithmeticOverflow.h>
#include <Common/DateLUTImpl.h>
#include <Columns/ColumnsDateTime.h>
2019-02-11 11:59:17 +00:00
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDate32.h>
2019-02-11 11:59:17 +00:00
#include <DataTypes/DataTypeDateTime.h>
#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>
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
{
2019-02-11 11:59:17 +00:00
class FunctionToStartOfInterval : public IFunction
{
public:
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionToStartOfInterval>(); }
2019-02-11 11:59:17 +00:00
2023-09-29 16:52:48 +00:00
static constexpr auto name = "toStartOfInterval";
2019-02-11 11:59:17 +00:00
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; }
2023-09-29 16:52:48 +00:00
bool useDefaultImplementationForConstants() const override { return true; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
bool hasInformationAboutMonotonicity() const override { return true; }
Monotonicity getMonotonicityForRange(const IDataType &, const Field &, const Field &) const override
{
return { .is_monotonic = true, .is_always_monotonic = true };
}
2019-02-11 11:59:17 +00:00
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
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))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}. "
"Should be a date or a date with time", arguments[0].type->getName(), getName());
first_argument_is_date = isDate(arguments[0].type);
2019-02-11 11:59:17 +00:00
};
const DataTypeInterval * interval_type = nullptr;
bool result_type_is_date = false;
2022-02-15 23:43:08 +00:00
bool result_type_is_datetime = false;
2023-09-29 16:52:48 +00:00
bool result_type_is_datetime_64 = 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(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}. "
"Should be an interval of time", arguments[1].type->getName(), getName());
2023-09-29 16:52:48 +00:00
switch (interval_type->getKind())
{
case IntervalKind::Nanosecond:
case IntervalKind::Microsecond:
case IntervalKind::Millisecond:
result_type_is_datetime_64 = true;
break;
case IntervalKind::Second:
case IntervalKind::Minute:
case IntervalKind::Hour:
case IntervalKind::Day:
result_type_is_datetime = true;
break;
case IntervalKind::Week:
case IntervalKind::Month:
case IntervalKind::Quarter:
case IntervalKind::Year:
result_type_is_date = true;
break;
}
2019-02-11 11:59:17 +00:00
};
auto check_timezone_argument = [&]
{
2023-09-29 16:52:48 +00:00
if (!isString(arguments[2].type))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}. "
"This argument is optional and must be a constant string with timezone name",
arguments[2].type->getName(), getName());
if (first_argument_is_date && result_type_is_date)
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 or DateTime64",
getName(), interval_type->getKind().toString());
2019-02-11 11:59:17 +00:00
};
if (arguments.size() == 2)
{
check_first_argument();
2019-02-11 11:59:17 +00:00
check_interval_argument();
}
else if (arguments.size() == 3)
{
check_first_argument();
2019-02-11 11:59:17 +00:00
check_interval_argument();
check_timezone_argument();
}
else
{
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());
2019-02-11 11:59:17 +00:00
}
if (result_type_is_date)
2021-07-01 15:13:43 +00:00
return std::make_shared<DataTypeDate>();
2022-02-15 23:43:08 +00:00
else if (result_type_is_datetime)
return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 2, 0, false));
2023-09-29 16:52:48 +00:00
else if (result_type_is_datetime_64)
2022-02-15 23:43:08 +00:00
{
auto scale = 0;
if (interval_type->getKind() == IntervalKind::Nanosecond)
scale = 9;
else if (interval_type->getKind() == IntervalKind::Microsecond)
scale = 6;
else if (interval_type->getKind() == IntervalKind::Millisecond)
scale = 3;
return std::make_shared<DataTypeDateTime64>(scale, extractTimeZoneNameFromFunctionArguments(arguments, 2, 0, false));
2022-02-15 23:43:08 +00:00
}
2023-09-29 16:52:48 +00:00
UNREACHABLE();
2019-02-11 11:59:17 +00:00
}
2022-02-15 23:43:08 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, 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];
Extended range of DateTime64 to years 1925 - 2238 The Year 1925 is a starting point because most of the timezones switched to saner (mostly 15-minutes based) offsets somewhere during 1924 or before. And that significantly simplifies implementation. 2238 is to simplify arithmetics for sanitizing LUT index access; there are less than 0x1ffff days from 1925. * Extended DateLUTImpl internal LUT to 0x1ffff items, some of which represent negative (pre-1970) time values. As a collateral benefit, Date now correctly supports dates up to 2149 (instead of 2106). * Added a new strong typedef ExtendedDayNum, which represents dates pre-1970 and post 2149. * Functions that used to return DayNum now return ExtendedDayNum. * Refactored DateLUTImpl to untie DayNum from the dual role of being a value and an index (due to negative time). Index is now a different type LUTIndex with explicit conversion functions from DatNum, time_t, and ExtendedDayNum. * Updated DateLUTImpl to properly support values close to epoch start (1970-01-01 00:00), including negative ones. * Reduced resolution of DateLUTImpl::Values::time_at_offset_change to multiple of 15-minutes to allow storing 64-bits of time_t in DateLUTImpl::Value while keeping same size. * Minor performance updates to DateLUTImpl when building month LUT by skipping non-start-of-month days. * Fixed extractTimeZoneFromFunctionArguments to work correctly with DateTime64. * New unit-tests and stateless integration tests for both DateTime and DateTime64.
2020-04-17 13:26:44 +00:00
const auto & time_zone = extractTimeZoneFromFunctionArguments(arguments, 2, 0);
2023-09-29 16:52:48 +00:00
auto result_column = dispatchForTimeColumn(time_column, interval_column, result_type, time_zone);
2020-10-19 15:27:41 +00:00
return result_column;
2019-02-11 11:59:17 +00:00
}
private:
2023-09-29 16:52:48 +00:00
ColumnPtr dispatchForTimeColumn(
2022-02-15 23:43:08 +00:00
const ColumnWithTypeAndName & time_column, const ColumnWithTypeAndName & interval_column, const DataTypePtr & result_type, 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();
2022-02-15 23:43:08 +00:00
2023-09-29 16:52:48 +00:00
if (isDateTime64(from_datatype))
2022-02-15 23:43:08 +00:00
{
const auto * time_column_vec = checkAndGetColumn<ColumnDateTime64>(time_column.column.get());
2022-02-15 23:43:08 +00:00
auto scale = assert_cast<const DataTypeDateTime64 &>(from_datatype).getScale();
if (time_column_vec)
2023-09-29 16:52:48 +00:00
return dispatchForIntervalColumn(assert_cast<const DataTypeDateTime64 &>(from_datatype), *time_column_vec, interval_column, result_type, time_zone, scale);
2022-02-15 23:43:08 +00:00
}
2023-09-29 16:52:48 +00:00
if (isDateTime(from_datatype))
2019-02-11 11:59:17 +00:00
{
const auto * time_column_vec = checkAndGetColumn<ColumnDateTime>(time_column.column.get());
2019-02-11 11:59:17 +00:00
if (time_column_vec)
2023-09-29 16:52:48 +00:00
return dispatchForIntervalColumn(assert_cast<const DataTypeDateTime &>(from_datatype), *time_column_vec, interval_column, result_type, time_zone);
2019-02-11 11:59:17 +00:00
}
2023-09-29 16:52:48 +00:00
if (isDate(from_datatype))
2019-02-11 11:59:17 +00:00
{
const auto * time_column_vec = checkAndGetColumn<ColumnDate>(time_column.column.get());
2019-02-11 11:59:17 +00:00
if (time_column_vec)
2023-09-29 16:52:48 +00:00
return dispatchForIntervalColumn(assert_cast<const DataTypeDate &>(from_datatype), *time_column_vec, interval_column, result_type, time_zone);
2019-10-22 07:43:14 +00:00
}
2023-09-29 16:52:48 +00:00
if (isDate32(from_datatype))
{
const auto * time_column_vec = checkAndGetColumn<ColumnDate32>(time_column.column.get());
if (time_column_vec)
2023-09-29 16:52:48 +00:00
return dispatchForIntervalColumn(assert_cast<const DataTypeDate32 &>(from_datatype), *time_column_vec, interval_column, result_type, time_zone);
2019-02-11 11:59:17 +00:00
}
2023-09-29 16:52:48 +00:00
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal column for first argument of function {}. Must contain dates or dates with time", getName());
2019-02-11 11:59:17 +00:00
}
2023-09-29 16:52:48 +00:00
template <typename TimeColumnType, typename TimeDataType>
2019-02-11 11:59:17 +00:00
ColumnPtr dispatchForIntervalColumn(
2023-09-29 16:52:48 +00:00
const TimeDataType & time_data_type, const TimeColumnType & time_column, const ColumnWithTypeAndName & interval_column,
2022-02-15 23:43:08 +00:00
const DataTypePtr & result_type, const DateLUTImpl & time_zone, const UInt16 scale = 1) const
2019-02-11 11:59:17 +00:00
{
const auto * interval_type = checkAndGetDataType<DataTypeInterval>(interval_column.type.get());
if (!interval_type)
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column for second argument of function {}, must be an interval of time.", getName());
2023-09-29 16:52:48 +00:00
2019-02-11 11:59:17 +00:00
const auto * interval_column_const_int64 = checkAndGetColumnConst<ColumnInt64>(interval_column.column.get());
if (!interval_column_const_int64)
2023-09-29 16:52:48 +00:00
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column for second argument of function {}, must be a const interval of time.", getName());
2019-02-11 11:59:17 +00:00
Int64 num_units = interval_column_const_int64->getValue<Int64>();
if (num_units <= 0)
throw Exception(ErrorCodes::ARGUMENT_OUT_OF_BOUND, "Value for second argument of function {} must be positive.", getName());
2019-02-11 11:59:17 +00:00
switch (interval_type->getKind())
{
case IntervalKind::Nanosecond:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDateTime64, IntervalKind::Nanosecond>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Microsecond:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDateTime64, IntervalKind::Microsecond>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Millisecond:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDateTime64, IntervalKind::Millisecond>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Second:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDateTime, IntervalKind::Second>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Minute:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDateTime, IntervalKind::Minute>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Hour:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDateTime, IntervalKind::Hour>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Day:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDateTime, IntervalKind::Day>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Week:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDate, IntervalKind::Week>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Month:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDate, IntervalKind::Month>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Quarter:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDate, IntervalKind::Quarter>(time_data_type, time_column, num_units, result_type, time_zone, scale);
case IntervalKind::Year:
2023-09-29 16:52:48 +00:00
return execute<TimeDataType, DataTypeDate, IntervalKind::Year>(time_data_type, time_column, num_units, result_type, time_zone, scale);
2019-02-11 11:59:17 +00:00
}
UNREACHABLE();
2019-02-11 11:59:17 +00:00
}
2023-09-29 16:52:48 +00:00
template <typename TimeDataType, typename ToDataType, IntervalKind::Kind unit, typename ColumnType>
2023-10-08 14:26:31 +00:00
ColumnPtr execute(const TimeDataType &, const ColumnType & time_column_type, Int64 num_units, const DataTypePtr & result_type, const DateLUTImpl & time_zone, const UInt16 scale) const
2019-02-11 11:59:17 +00:00
{
2022-02-15 23:43:08 +00:00
using ToColumnType = typename ToDataType::ColumnType;
using ToFieldType = typename ToDataType::FieldType;
2022-02-15 23:43:08 +00:00
const auto & time_data = time_column_type.getData();
size_t size = time_data.size();
auto result_col = result_type->createColumn();
2022-02-16 16:23:10 +00:00
auto *col_to = assert_cast<ToColumnType *>(result_col.get());
2022-02-15 23:43:08 +00:00
auto & result_data = col_to->getData();
2019-02-11 11:59:17 +00:00
result_data.resize(size);
2019-10-22 07:43:14 +00:00
2022-02-15 23:43:08 +00:00
Int64 scale_multiplier = DecimalUtils::scaleMultiplier<DateTime64>(scale);
for (size_t i = 0; i != size; ++i)
2023-09-29 16:52:48 +00:00
result_data[i] = static_cast<ToFieldType>(ToStartOfInterval<unit>::execute(time_data[i], num_units, time_zone, scale_multiplier));
2022-02-15 23:43:08 +00:00
return result_col;
2019-02-11 11:59:17 +00:00
}
};
2020-09-07 18:00:37 +00:00
}
2019-02-11 11:59:17 +00:00
REGISTER_FUNCTION(ToStartOfInterval)
2019-02-11 11:59:17 +00:00
{
factory.registerFunction<FunctionToStartOfInterval>();
}
}