2019-10-14 08:38:03 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
|
|
|
|
|
|
|
#include <Core/DecimalFunctions.h>
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
2021-05-13 13:48:14 +00:00
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
2021-01-21 08:49:40 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2023-06-10 12:53:02 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2019-11-03 05:42:48 +00:00
|
|
|
#include <Common/assert_cast.h>
|
|
|
|
|
2022-05-08 17:01:47 +00:00
|
|
|
#include <ctime>
|
2019-10-14 08:38:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2019-12-19 09:55:10 +00:00
|
|
|
extern const int CANNOT_CLOCK_GETTIME;
|
2021-01-20 13:18:41 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-09-08 11:01:17 +00:00
|
|
|
Field nowSubsecond(UInt32 scale)
|
2019-10-14 08:38:03 +00:00
|
|
|
{
|
2019-12-19 13:12:13 +00:00
|
|
|
static constexpr Int32 fractional_scale = 9;
|
|
|
|
|
|
|
|
timespec spec{};
|
2019-12-19 09:55:10 +00:00
|
|
|
if (clock_gettime(CLOCK_REALTIME, &spec))
|
2019-12-19 13:12:13 +00:00
|
|
|
throwFromErrno("Cannot clock_gettime.", ErrorCodes::CANNOT_CLOCK_GETTIME);
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
DecimalUtils::DecimalComponents<DateTime64> components{spec.tv_sec, spec.tv_nsec};
|
2019-11-03 05:42:48 +00:00
|
|
|
|
|
|
|
// clock_gettime produces subsecond part in nanoseconds, but decimalFromComponents fractional is scale-dependent.
|
|
|
|
// Andjust fractional to scale, e.g. for 123456789 nanoseconds:
|
|
|
|
// if scale is 6 (miscoseconds) => divide by 9 - 6 = 3 to get 123456 microseconds
|
|
|
|
// if scale is 12 (picoseconds) => multiply by abs(9 - 12) = 3 to get 123456789000 picoseconds
|
|
|
|
const auto adjust_scale = fractional_scale - static_cast<Int32>(scale);
|
|
|
|
if (adjust_scale < 0)
|
|
|
|
components.fractional *= intExp10(std::abs(adjust_scale));
|
|
|
|
else if (adjust_scale > 0)
|
|
|
|
components.fractional /= intExp10(adjust_scale);
|
|
|
|
|
2019-12-17 10:19:21 +00:00
|
|
|
return DecimalField(DecimalUtils::decimalFromComponents<DateTime64>(components, scale),
|
|
|
|
scale);
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:18:41 +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 ExecutableFunctionNow64 : public IExecutableFunction
|
2019-10-14 08:38:03 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-20 13:18:41 +00:00
|
|
|
explicit ExecutableFunctionNow64(Field time_) : time_value(time_) {}
|
|
|
|
|
|
|
|
String getName() const override { return "now64"; }
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2021-01-20 13:18:41 +00:00
|
|
|
{
|
|
|
|
return result_type->createColumnConst(input_rows_count, time_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Field time_value;
|
|
|
|
};
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
class FunctionBaseNow64 : public IFunctionBase
|
2021-01-20 13:18:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-10-25 09:06:53 +00:00
|
|
|
explicit FunctionBaseNow64(Field time_, DataTypes argument_types_, DataTypePtr return_type_)
|
|
|
|
: time_value(time_), argument_types(std::move(argument_types_)), return_type(std::move(return_type_)) {}
|
2021-01-20 13:18:41 +00:00
|
|
|
|
|
|
|
String getName() const override { return "now64"; }
|
|
|
|
|
|
|
|
const DataTypes & getArgumentTypes() const override
|
|
|
|
{
|
|
|
|
return argument_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataTypePtr & getResultType() const override
|
|
|
|
{
|
|
|
|
return return_type;
|
|
|
|
}
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override
|
2019-10-14 08:38:03 +00:00
|
|
|
{
|
2021-01-20 13:18:41 +00:00
|
|
|
return std::make_unique<ExecutableFunctionNow64>(time_value);
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 19:00:11 +00:00
|
|
|
bool isDeterministic() const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo &) const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-20 13:18:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Field time_value;
|
2021-10-25 09:06:53 +00:00
|
|
|
DataTypes argument_types;
|
2021-01-20 13:18:41 +00:00
|
|
|
DataTypePtr return_type;
|
|
|
|
};
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
class Now64OverloadResolver : public IFunctionOverloadResolver
|
2021-01-20 13:18:41 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "now64";
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
2019-10-14 08:38:03 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
2021-01-20 13:18:41 +00:00
|
|
|
|
2019-10-14 08:38:03 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
2023-06-10 12:53:02 +00:00
|
|
|
static FunctionOverloadResolverPtr create(ContextPtr context) { return std::make_unique<Now64OverloadResolver>(context); }
|
|
|
|
explicit Now64OverloadResolver(ContextPtr context)
|
|
|
|
: allow_nonconst_timezone_arguments(context->getSettings().allow_nonconst_timezone_arguments)
|
|
|
|
{}
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
2019-10-14 08:38:03 +00:00
|
|
|
{
|
|
|
|
UInt32 scale = DataTypeDateTime64::default_scale;
|
2021-05-13 13:48:14 +00:00
|
|
|
String timezone_name;
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2021-05-13 13:48:14 +00:00
|
|
|
if (arguments.size() > 2)
|
2021-01-20 13:18:41 +00:00
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Arguments size of function {} should be 0, or 1, or 2", getName());
|
2021-01-20 13:18:41 +00:00
|
|
|
}
|
2021-05-13 17:15:24 +00:00
|
|
|
if (!arguments.empty())
|
2019-10-14 08:38:03 +00:00
|
|
|
{
|
|
|
|
const auto & argument = arguments[0];
|
2019-12-18 14:51:35 +00:00
|
|
|
if (!isInteger(argument.type) || !argument.column || !isColumnConst(*argument.column))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of 0 argument of function {}. "
|
|
|
|
"Expected const integer.", argument.type->getName(), getName());
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2022-10-07 10:46:45 +00:00
|
|
|
scale = static_cast<UInt32>(argument.column->get64(0));
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
2021-05-13 13:48:14 +00:00
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2023-06-10 12:53:02 +00:00
|
|
|
timezone_name = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0, allow_nonconst_timezone_arguments);
|
2021-05-13 13:48:14 +00:00
|
|
|
}
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2021-05-13 13:48:14 +00:00
|
|
|
return std::make_shared<DataTypeDateTime64>(scale, timezone_name);
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 09:06:53 +00:00
|
|
|
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type) const override
|
2019-10-14 08:38:03 +00:00
|
|
|
{
|
2021-01-21 08:41:32 +00:00
|
|
|
UInt32 scale = DataTypeDateTime64::default_scale;
|
2021-01-21 08:49:40 +00:00
|
|
|
auto res_type = removeNullable(result_type);
|
|
|
|
if (const auto * type = typeid_cast<const DataTypeDateTime64 *>(res_type.get()))
|
2021-01-21 08:41:32 +00:00
|
|
|
scale = type->getScale();
|
|
|
|
|
2021-10-25 09:06:53 +00:00
|
|
|
DataTypes arg_types;
|
|
|
|
arg_types.reserve(arguments.size());
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
arg_types.push_back(arg.type);
|
|
|
|
|
2022-03-02 17:22:12 +00:00
|
|
|
return std::make_unique<FunctionBaseNow64>(nowSubsecond(scale), std::move(arg_types), result_type);
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
2023-06-10 12:53:02 +00:00
|
|
|
private:
|
|
|
|
const bool allow_nonconst_timezone_arguments;
|
2019-10-14 08:38:03 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(Now64)
|
2019-10-14 08:38:03 +00:00
|
|
|
{
|
2022-08-27 20:06:03 +00:00
|
|
|
factory.registerFunction<Now64OverloadResolver>({}, FunctionFactory::CaseInsensitive);
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|