2019-10-14 08:38:03 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
|
|
|
|
|
|
|
#include <Core/DecimalFunctions.h>
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
|
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;
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:19:21 +00:00
|
|
|
static 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
|
|
|
|
2019-12-06 21:05:31 +00:00
|
|
|
DecimalUtils::DecimalComponents<DateTime64::NativeType> 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
|
|
|
}
|
|
|
|
|
|
|
|
class FunctionNow64 : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "now64";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionNow64>(); }
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return ColumnNumbers{0}; }
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
|
|
|
// Return type depends on argument value.
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
UInt32 scale = DataTypeDateTime64::default_scale;
|
|
|
|
|
|
|
|
// Type check is similar to the validateArgumentType, trying to keep error codes and messages as close to the said function as possible.
|
2020-03-09 03:38:43 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeDateTime64>(scale);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:42:48 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & /*arguments*/, size_t result, size_t input_rows_count) override
|
2019-10-14 08:38:03 +00:00
|
|
|
{
|
2019-11-03 05:42:48 +00:00
|
|
|
auto & result_col = block.getByPosition(result);
|
2019-12-18 14:51:35 +00:00
|
|
|
const UInt32 scale = assert_cast<const DataTypeDateTime64 *>(result_col.type.get())->getScale();
|
2019-10-14 08:38:03 +00:00
|
|
|
|
2019-12-18 14:51:35 +00:00
|
|
|
result_col.column = result_col.type->createColumnConst(input_rows_count, nowSubsecond(scale));
|
2019-10-14 08:38:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void registerFunctionNow64(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionNow64>(FunctionFactory::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|