ClickHouse/src/Functions/now64.cpp

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

176 lines
5.6 KiB
C++
Raw Normal View History

#include <DataTypes/DataTypeDateTime64.h>
#include <Core/DecimalFunctions.h>
#include <Functions/IFunction.h>
#include <Functions/FunctionFactory.h>
#include <Functions/extractTimeZoneFromFunctionArguments.h>
2021-01-21 08:49:40 +00:00
#include <DataTypes/DataTypeNullable.h>
#include <Interpreters/Context.h>
2019-11-03 05:42:48 +00:00
#include <Common/assert_cast.h>
#include <ctime>
namespace DB
{
namespace ErrorCodes
{
2020-02-25 18:02:41 +00:00
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int CANNOT_CLOCK_GETTIME;
2021-01-20 13:18:41 +00:00
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
2020-09-07 18:00:37 +00:00
namespace
{
2020-09-08 11:01:17 +00:00
Field nowSubsecond(UInt32 scale)
{
2019-12-19 13:12:13 +00:00
static constexpr Int32 fractional_scale = 9;
timespec spec{};
if (clock_gettime(CLOCK_REALTIME, &spec))
2019-12-19 13:12:13 +00:00
throwFromErrno("Cannot clock_gettime.", ErrorCodes::CANNOT_CLOCK_GETTIME);
Extended range of DateTime64 to years 1925 - 2238 The Year 1925 is a starting point because most of the timezones switched to saner (mostly 15-minutes based) offsets somewhere during 1924 or before. And that significantly simplifies implementation. 2238 is to simplify arithmetics for sanitizing LUT index access; there are less than 0x1ffff days from 1925. * Extended DateLUTImpl internal LUT to 0x1ffff items, some of which represent negative (pre-1970) time values. As a collateral benefit, Date now correctly supports dates up to 2149 (instead of 2106). * Added a new strong typedef ExtendedDayNum, which represents dates pre-1970 and post 2149. * Functions that used to return DayNum now return ExtendedDayNum. * Refactored DateLUTImpl to untie DayNum from the dual role of being a value and an index (due to negative time). Index is now a different type LUTIndex with explicit conversion functions from DatNum, time_t, and ExtendedDayNum. * Updated DateLUTImpl to properly support values close to epoch start (1970-01-01 00:00), including negative ones. * Reduced resolution of DateLUTImpl::Values::time_at_offset_change to multiple of 15-minutes to allow storing 64-bits of time_t in DateLUTImpl::Value while keeping same size. * Minor performance updates to DateLUTImpl when building month LUT by skipping non-start-of-month days. * Fixed extractTimeZoneFromFunctionArguments to work correctly with DateTime64. * New unit-tests and stateless integration tests for both DateTime and DateTime64.
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);
}
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
{
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;
}
2021-05-15 17:33:15 +00:00
ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override
{
2021-01-20 13:18:41 +00:00
return std::make_unique<ExecutableFunctionNow64>(time_value);
}
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; }
bool isVariadic() const override { return true; }
2021-01-20 13:18:41 +00:00
size_t getNumberOfArguments() const override { return 0; }
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)
{}
2021-05-15 17:33:15 +00:00
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
UInt32 scale = DataTypeDateTime64::default_scale;
String timezone_name;
if (arguments.size() > 2)
2021-01-20 13:18:41 +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())
{
const auto & argument = arguments[0];
if (!isInteger(argument.type) || !argument.column || !isColumnConst(*argument.column))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of 0 argument of function {}. "
"Expected const integer.", argument.type->getName(), getName());
scale = static_cast<UInt32>(argument.column->get64(0));
}
if (arguments.size() == 2)
{
timezone_name = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0, allow_nonconst_timezone_arguments);
}
return std::make_shared<DataTypeDateTime64>(scale, timezone_name);
}
2021-10-25 09:06:53 +00:00
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type) const override
{
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);
return std::make_unique<FunctionBaseNow64>(nowSubsecond(scale), std::move(arg_types), result_type);
}
private:
const bool allow_nonconst_timezone_arguments;
};
2020-09-07 18:00:37 +00:00
}
REGISTER_FUNCTION(Now64)
{
2022-08-27 20:06:03 +00:00
factory.registerFunction<Now64OverloadResolver>({}, FunctionFactory::CaseInsensitive);
}
}