ClickHouse/src/Functions/now64.cpp

154 lines
4.9 KiB
C++
Raw Normal View History

#include <DataTypes/DataTypeDateTime64.h>
#include <Core/DecimalFunctions.h>
#include <Functions/IFunction.h>
#include <Functions/FunctionFactory.h>
2021-01-21 08:49:40 +00:00
#include <DataTypes/DataTypeNullable.h>
2019-11-03 05:42:48 +00:00
#include <Common/assert_cast.h>
#include <time.h>
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.)
class ExecutableFunctionNow64 : public IExecutableFunctionImpl
{
public:
2021-01-20 13:18:41 +00:00
explicit ExecutableFunctionNow64(Field time_) : time_value(time_) {}
String getName() const override { return "now64"; }
ColumnPtr execute(const ColumnsWithTypeAndName &, const DataTypePtr & result_type, size_t input_rows_count) const override
{
return result_type->createColumnConst(input_rows_count, time_value);
}
private:
Field time_value;
};
class FunctionBaseNow64 : public IFunctionBaseImpl
{
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;
}
2021-01-20 13:18:41 +00:00
ExecutableFunctionImplPtr prepare(const ColumnsWithTypeAndName &) const override
{
2021-01-20 13:18:41 +00:00
return std::make_unique<ExecutableFunctionNow64>(time_value);
}
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;
};
class Now64OverloadResolver : public IFunctionOverloadResolverImpl
{
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 FunctionOverloadResolverImplPtr create(ContextPtr) { return std::make_unique<Now64OverloadResolver>(); }
2021-01-20 13:18:41 +00:00
DataTypePtr getReturnType(const ColumnsWithTypeAndName & arguments) const override
{
UInt32 scale = DataTypeDateTime64::default_scale;
2021-01-20 13:18:41 +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)
{
const auto & argument = arguments[0];
if (!isInteger(argument.type) || !argument.column || !isColumnConst(*argument.column))
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);
}
return std::make_shared<DataTypeDateTime64>(scale);
}
2021-01-20 13:18:41 +00:00
FunctionBaseImplPtr build(const ColumnsWithTypeAndName &, 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-01-20 13:18:41 +00:00
return std::make_unique<FunctionBaseNow64>(nowSubsecond(scale), result_type);
}
};
2020-09-07 18:00:37 +00:00
}
void registerFunctionNow64(FunctionFactory & factory)
{
2021-01-20 13:18:41 +00:00
factory.registerFunction<Now64OverloadResolver>(FunctionFactory::CaseInsensitive);
}
}