ClickHouse/src/Functions/yesterday.cpp

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

89 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
{
2021-05-15 17:33:15 +00:00
class ExecutableFunctionYesterday : public IExecutableFunction
{
public:
2019-12-09 19:55:26 +00:00
explicit ExecutableFunctionYesterday(time_t time_) : day_value(time_) {}
String getName() const override { return "yesterday"; }
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 FunctionBaseYesterday : public IFunctionBase
{
public:
explicit FunctionBaseYesterday(DayNum day_value_) : day_value(day_value_), return_type(std::make_shared<DataTypeDate>()) {}
String getName() const override { return "yesterday"; }
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
{
2019-12-09 19:55:26 +00:00
return std::make_unique<ExecutableFunctionYesterday>(day_value);
}
bool isDeterministic() const override { return false; }
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 YesterdayOverloadResolver : public IFunctionOverloadResolver
{
public:
static constexpr auto name = "yesterday";
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<YesterdayOverloadResolver>(); }
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
{
auto day_num = DateLUT::instance().toDayNum(time(nullptr)) - 1;
2019-12-09 19:55:26 +00:00
return std::make_unique<FunctionBaseYesterday>(static_cast<DayNum>(day_num));
}
};
REGISTER_FUNCTION(Yesterday)
{
factory.registerFunction<YesterdayOverloadResolver>();
}
}