ClickHouse/src/Functions/today.cpp

93 lines
2.4 KiB
C++
Raw Normal View History

#include <Common/DateLUT.h>
2019-10-04 17:46:36 +00:00
#include <Core/Field.h>
#include <DataTypes/DataTypeDate.h>
2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
#include <Functions/FunctionFactory.h>
namespace DB
{
2020-09-07 18:00:37 +00:00
namespace
{
2021-05-15 17:33:15 +00:00
class ExecutableFunctionToday : public IExecutableFunction
{
public:
explicit ExecutableFunctionToday(time_t time_) : day_value(time_) {}
String getName() const override { return "today"; }
2021-05-15 17:33:15 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
{
2020-10-19 15:27:41 +00:00
return DataTypeDate().createColumnConst(input_rows_count, day_value);
}
private:
DayNum day_value;
};
2021-05-15 17:33:15 +00:00
class FunctionBaseToday : public IFunctionBase
{
public:
explicit FunctionBaseToday(DayNum day_value_) : day_value(day_value_), return_type(std::make_shared<DataTypeDate>()) {}
String getName() const override { return "today"; }
const DataTypes & getArgumentTypes() const override
{
static const DataTypes argument_types;
return argument_types;
}
2020-10-19 15:27:41 +00:00
const DataTypePtr & getResultType() const override
{
return return_type;
}
2021-05-15 17:33:15 +00:00
ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override
{
return std::make_unique<ExecutableFunctionToday>(day_value);
}
bool isDeterministic() const override { return false; }
bool isDeterministicInScopeOfQuery() const override { return true; }
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
private:
DayNum day_value;
DataTypePtr return_type;
};
2021-05-15 17:33:15 +00:00
class TodayOverloadResolver : public IFunctionOverloadResolver
{
public:
static constexpr auto name = "today";
String getName() const override { return name; }
bool isDeterministic() const override { return false; }
size_t getNumberOfArguments() const override { return 0; }
2021-06-01 12:20:52 +00:00
static FunctionOverloadResolverPtr create(ContextPtr) { return std::make_unique<TodayOverloadResolver>(); }
2021-05-15 17:33:15 +00:00
DataTypePtr getReturnTypeImpl(const DataTypes &) const override { return std::make_shared<DataTypeDate>(); }
2021-05-15 17:33:15 +00:00
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName &, const DataTypePtr &) const override
{
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
return std::make_unique<FunctionBaseToday>(DayNum(DateLUT::instance().toDayNum(time(nullptr)).toUnderType()));
}
};
2020-09-07 18:00:37 +00:00
}
REGISTER_FUNCTION(Today)
{
factory.registerFunction<TodayOverloadResolver>();
}
}