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>
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2019-11-03 05:42:48 +00:00
|
|
|
#include <Common/assert_cast.h>
|
|
|
|
|
2019-10-14 08:38:03 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
explicit FunctionBaseNow64(Field time_, DataTypePtr return_type_) : time_value(time_), return_type(return_type_) {}
|
|
|
|
|
|
|
|
String getName() const override { return "now64"; }
|
|
|
|
|
|
|
|
const DataTypes & getArgumentTypes() const override
|
|
|
|
{
|
|
|
|
static const DataTypes argument_types;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-20 13:18:41 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
bool isDeterministicInScopeOfQuery() const override { return true; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Field time_value;
|
|
|
|
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; }
|
2021-05-15 17:33:15 +00:00
|
|
|
static FunctionOverloadResolverPtr create(ContextPtr) { return std::make_unique<Now64OverloadResolver>(); }
|
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
|
|
|
{
|
2021-05-13 13:48:14 +00:00
|
|
|
throw Exception("Arguments size of function " + getName() + " should be 0, or 1, or 2", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
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))
|
2019-10-14 08:38:03 +00:00
|
|
|
throw Exception("Illegal type " + argument.type->getName() +
|
|
|
|
" of 0" +
|
|
|
|
" argument of function " + getName() +
|
|
|
|
". Expected const integer.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
scale = argument.column->get64(0);
|
|
|
|
}
|
2021-05-13 13:48:14 +00:00
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
|
|
|
timezone_name = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0);
|
|
|
|
}
|
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-05-15 17:33:15 +00:00
|
|
|
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName &, 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-01-20 13:18:41 +00:00
|
|
|
return std::make_unique<FunctionBaseNow64>(nowSubsecond(scale), result_type);
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 08:38:03 +00:00
|
|
|
void registerFunctionNow64(FunctionFactory & factory)
|
|
|
|
{
|
2021-01-20 13:18:41 +00:00
|
|
|
factory.registerFunction<Now64OverloadResolver>(FunctionFactory::CaseInsensitive);
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|