2020-05-08 14:59:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-05-08 14:59:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2021-05-04 17:26:09 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2020-05-08 14:59:58 +00:00
|
|
|
|
|
|
|
#include <common/arithmeticOverflow.h>
|
|
|
|
|
2021-05-04 17:26:09 +00:00
|
|
|
|
2020-05-08 14:59:58 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2021-05-04 17:26:09 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2020-05-08 14:59:58 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int DECIMAL_OVERFLOW;
|
|
|
|
}
|
|
|
|
|
2021-05-04 17:26:09 +00:00
|
|
|
/// Cast DateTime64 to Int64 representation narrowed down (or scaled up) to any scale value defined in Impl.
|
|
|
|
class FunctionToUnixTimestamp64 : public IFunction
|
2020-05-08 14:59:58 +00:00
|
|
|
{
|
2021-05-04 17:26:09 +00:00
|
|
|
private:
|
|
|
|
size_t target_scale;
|
|
|
|
const char * name;
|
2020-05-08 14:59:58 +00:00
|
|
|
public:
|
2021-05-04 17:26:09 +00:00
|
|
|
FunctionToUnixTimestamp64(size_t target_scale_, const char * name_)
|
|
|
|
: target_scale(target_scale_), name(name_)
|
2020-05-08 14:59:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
2021-05-04 17:26:09 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool isVariadic() const override { return false; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2020-08-01 10:27:38 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2020-05-08 14:59:58 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2021-05-04 17:26:09 +00:00
|
|
|
if (!isDateTime64(arguments[0].type))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "The only argument for function {} must be DateTime64", name);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeInt64>();
|
2020-05-08 14:59:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 17:26:09 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2020-05-08 14:59:58 +00:00
|
|
|
{
|
2020-10-17 21:41:50 +00:00
|
|
|
const auto & src = arguments[0];
|
2020-05-08 14:59:58 +00:00
|
|
|
const auto & col = *src.column;
|
|
|
|
|
2021-05-07 01:45:29 +00:00
|
|
|
auto res_column = ColumnInt64::create(input_rows_count);
|
2021-05-04 17:26:09 +00:00
|
|
|
auto & result_data = res_column->getData();
|
2020-05-08 14:59:58 +00:00
|
|
|
|
2021-05-06 23:17:39 +00:00
|
|
|
const auto & source_data = typeid_cast<const ColumnDecimal<DateTime64> &>(col).getData();
|
2020-05-08 14:59:58 +00:00
|
|
|
|
2021-05-04 17:26:09 +00:00
|
|
|
Int32 scale_diff = typeid_cast<const DataTypeDateTime64 &>(*src.type).getScale() - target_scale;
|
2020-05-08 14:59:58 +00:00
|
|
|
if (scale_diff == 0)
|
|
|
|
{
|
2021-05-06 23:17:39 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
result_data[i] = source_data[i];
|
2020-05-08 14:59:58 +00:00
|
|
|
}
|
|
|
|
else if (scale_diff < 0)
|
|
|
|
{
|
2021-05-04 17:26:09 +00:00
|
|
|
const Int64 scale_multiplier = DecimalUtils::scaleMultiplier<Int64>(-scale_diff);
|
2021-05-06 23:17:39 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2020-05-08 14:59:58 +00:00
|
|
|
{
|
2021-05-06 23:17:39 +00:00
|
|
|
Int64 result_value = source_data[i];
|
2020-05-08 14:59:58 +00:00
|
|
|
if (common::mulOverflow(result_value, scale_multiplier, result_value))
|
|
|
|
throw Exception("Decimal overflow in " + getName(), ErrorCodes::DECIMAL_OVERFLOW);
|
|
|
|
|
2021-05-06 23:17:39 +00:00
|
|
|
result_data[i] = result_value;
|
2020-05-08 14:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const Int64 scale_multiplier = DecimalUtils::scaleMultiplier<Int64>(scale_diff);
|
2021-05-06 23:17:39 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
result_data[i] = Int64(source_data[i]) / scale_multiplier;
|
2020-05-08 14:59:58 +00:00
|
|
|
}
|
2020-10-17 21:41:50 +00:00
|
|
|
|
|
|
|
return res_column;
|
2020-05-08 14:59:58 +00:00
|
|
|
}
|
2021-05-04 17:26:09 +00:00
|
|
|
};
|
|
|
|
|
2020-05-08 14:59:58 +00:00
|
|
|
|
2021-05-04 17:26:09 +00:00
|
|
|
class FunctionFromUnixTimestamp64 : public IFunction
|
|
|
|
{
|
2020-05-08 14:59:58 +00:00
|
|
|
private:
|
2021-05-04 17:26:09 +00:00
|
|
|
size_t target_scale;
|
|
|
|
const char * name;
|
|
|
|
public:
|
|
|
|
FunctionFromUnixTimestamp64(size_t target_scale_, const char * name_)
|
|
|
|
: target_scale(target_scale_), name(name_)
|
2020-05-08 14:59:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-04 17:26:09 +00:00
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
bool isVariadic() const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-05-04 17:26:09 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
2020-05-08 14:59:58 +00:00
|
|
|
{
|
2021-05-04 17:26:09 +00:00
|
|
|
if (arguments.size() < 1 || arguments.size() > 2)
|
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} takes one or two arguments", name);
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeInt64 *>(arguments[0].type.get()))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "The first argument for function {} must be Int64", name);
|
|
|
|
|
|
|
|
std::string timezone;
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
timezone = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeDateTime64>(target_scale, timezone);
|
2020-05-08 14:59:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 17:26:09 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2020-05-08 14:59:58 +00:00
|
|
|
{
|
2021-05-04 17:26:09 +00:00
|
|
|
const auto & src = arguments[0];
|
|
|
|
const auto & col = *src.column;
|
|
|
|
|
2021-05-06 23:17:39 +00:00
|
|
|
auto res_column = ColumnDecimal<DateTime64>::create(input_rows_count, target_scale);
|
2021-05-04 17:26:09 +00:00
|
|
|
auto & result_data = res_column->getData();
|
|
|
|
|
|
|
|
const auto & source_data = typeid_cast<const ColumnInt64 &>(col).getData();
|
|
|
|
|
2021-05-06 23:17:39 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
result_data[i] = source_data[i];
|
2021-05-04 17:26:09 +00:00
|
|
|
|
|
|
|
return res_column;
|
2020-05-08 14:59:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|