2018-09-26 00:31:40 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2019-09-26 15:12:40 +00:00
|
|
|
#include <Core/DecimalFunctions.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2019-10-04 17:46:36 +00:00
|
|
|
#include <Core/Field.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2020-09-25 07:20:42 +00:00
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
|
2019-09-26 15:12:40 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
namespace DB
|
2019-09-30 14:59:04 +00:00
|
|
|
{
|
2020-09-25 07:20:42 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2019-09-30 14:59:04 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
/// Get the current time. (It is a constant, it is evaluated once for the entire query.)
|
2021-05-15 17:33:15 +00:00
|
|
|
class ExecutableFunctionNow : public IExecutableFunction
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-12-12 14:16:59 +00:00
|
|
|
explicit ExecutableFunctionNow(time_t time_) : time_value(time_) {}
|
2019-09-30 14:59:04 +00:00
|
|
|
|
|
|
|
String getName() const override { return "now"; }
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
2019-09-30 14:59:04 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
return DataTypeDateTime().createColumnConst(
|
2019-09-30 14:59:04 +00:00
|
|
|
input_rows_count,
|
|
|
|
static_cast<UInt64>(time_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
time_t time_value;
|
|
|
|
};
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
class FunctionBaseNow : public IFunctionBase
|
2019-09-30 14:59:04 +00:00
|
|
|
{
|
2019-09-30 17:36:03 +00:00
|
|
|
public:
|
2020-09-25 07:20:42 +00:00
|
|
|
explicit FunctionBaseNow(time_t time_, DataTypePtr return_type_) : time_value(time_), return_type(return_type_) {}
|
2019-09-30 14:59:04 +00:00
|
|
|
|
|
|
|
String getName() const override { return "now"; }
|
|
|
|
|
|
|
|
const DataTypes & getArgumentTypes() const override
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2019-09-30 14:59:04 +00:00
|
|
|
static const DataTypes argument_types;
|
|
|
|
return argument_types;
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
const DataTypePtr & getResultType() const override
|
2019-09-30 14:59:04 +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<ExecutableFunctionNow>(time_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-09-30 14:59:04 +00:00
|
|
|
private:
|
|
|
|
time_t time_value;
|
|
|
|
DataTypePtr return_type;
|
|
|
|
};
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
class NowOverloadResolver : public IFunctionOverloadResolver
|
2019-09-30 14:59:04 +00:00
|
|
|
{
|
2019-09-30 17:31:36 +00:00
|
|
|
public:
|
|
|
|
static constexpr auto name = "now";
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
2019-10-10 14:38:08 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
2020-09-25 07:20:42 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
|
2019-10-10 14:38:08 +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<NowOverloadResolver>(); }
|
2019-09-30 17:36:03 +00:00
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
2020-09-25 07:20:42 +00:00
|
|
|
{
|
|
|
|
if (arguments.size() > 1)
|
|
|
|
{
|
|
|
|
throw Exception("Arguments size of function " + getName() + " should be 0 or 1", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
}
|
|
|
|
if (arguments.size() == 1 && !isStringOrFixedString(arguments[0].type))
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Arguments of function " + getName() + " should be String or FixedString", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
if (arguments.size() == 1)
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 0, 0));
|
|
|
|
}
|
|
|
|
return std::make_shared<DataTypeDateTime>();
|
|
|
|
}
|
2019-09-30 14:59:04 +00:00
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &) const override
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2020-09-26 06:48:59 +00:00
|
|
|
if (arguments.size() > 1)
|
|
|
|
{
|
|
|
|
throw Exception("Arguments size of function " + getName() + " should be 0 or 1", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
}
|
|
|
|
if (arguments.size() == 1 && !isStringOrFixedString(arguments[0].type))
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Arguments of function " + getName() + " should be String or FixedString", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
2020-09-25 07:20:42 +00:00
|
|
|
if (arguments.size() == 1)
|
|
|
|
return std::make_unique<FunctionBaseNow>(
|
|
|
|
time(nullptr), std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 0, 0)));
|
|
|
|
return std::make_unique<FunctionBaseNow>(time(nullptr), std::make_shared<DataTypeDateTime>());
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
void registerFunctionNow(FunctionFactory & factory)
|
|
|
|
{
|
2019-12-12 14:16:59 +00:00
|
|
|
factory.registerFunction<NowOverloadResolver>(FunctionFactory::CaseInsensitive);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|