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