2018-12-03 03:17:06 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Core/callOnTypeIndex.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnDecimal.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-12-03 03:17:06 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2021-10-27 23:10:39 +00:00
|
|
|
#include "config_functions.h"
|
2018-12-03 03:17:06 +00:00
|
|
|
|
2019-08-09 21:58:17 +00:00
|
|
|
/** FastOps is a fast vector math library from Mikhail Parakhin (former Yandex CTO),
|
2019-08-01 00:29:32 +00:00
|
|
|
* Enabled by default.
|
|
|
|
*/
|
|
|
|
#if USE_FASTOPS
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <fastops/fastops.h>
|
2019-08-01 00:29:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-12-03 03:17:06 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2018-12-03 03:17:06 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Impl>
|
2019-08-01 00:29:32 +00:00
|
|
|
class FunctionMathUnary : public IFunction
|
2018-12-03 03:17:06 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Impl::name;
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionMathUnary>(); }
|
2018-12-03 03:17:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-27 12:49:58 +00:00
|
|
|
|
2018-12-03 03:17:06 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
const auto & arg = arguments.front();
|
2019-05-24 12:11:03 +00:00
|
|
|
if (!isNumber(arg))
|
2018-12-03 03:17:06 +00:00
|
|
|
throw Exception{"Illegal type " + arg->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
/// Integers are converted to Float64.
|
|
|
|
if (Impl::always_returns_float64 || !isFloat(arg))
|
|
|
|
return std::make_shared<DataTypeFloat64>();
|
|
|
|
else
|
|
|
|
return arg;
|
2018-12-03 03:17:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
template <typename T, typename ReturnType>
|
|
|
|
static void executeInIterations(const T * src_data, ReturnType * dst_data, size_t size)
|
2018-12-03 03:17:06 +00:00
|
|
|
{
|
2019-08-01 00:29:32 +00:00
|
|
|
if constexpr (Impl::rows_per_iteration == 0)
|
|
|
|
{
|
|
|
|
/// Process all data as a whole and use FastOps implementation
|
|
|
|
|
|
|
|
/// If the argument is integer, convert to Float64 beforehand
|
|
|
|
if constexpr (!std::is_floating_point_v<T>)
|
|
|
|
{
|
|
|
|
PODArray<Float64> tmp_vec(size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2020-08-19 11:52:17 +00:00
|
|
|
tmp_vec[i] = static_cast<Float64>(src_data[i]);
|
2019-08-01 00:29:32 +00:00
|
|
|
|
|
|
|
Impl::execute(tmp_vec.data(), size, dst_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Impl::execute(src_data, size, dst_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const size_t rows_remaining = size % Impl::rows_per_iteration;
|
|
|
|
const size_t rows_size = size - rows_remaining;
|
2018-12-03 03:17:06 +00:00
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
for (size_t i = 0; i < rows_size; i += Impl::rows_per_iteration)
|
|
|
|
Impl::execute(&src_data[i], &dst_data[i]);
|
2018-12-03 03:17:06 +00:00
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
if (rows_remaining != 0)
|
|
|
|
{
|
|
|
|
T src_remaining[Impl::rows_per_iteration];
|
2021-01-26 19:39:03 +00:00
|
|
|
memcpy(src_remaining, &src_data[rows_size], rows_remaining * sizeof(T));
|
|
|
|
memset(src_remaining + rows_remaining, 0, (Impl::rows_per_iteration - rows_remaining) * sizeof(T));
|
2019-08-01 00:29:32 +00:00
|
|
|
ReturnType dst_remaining[Impl::rows_per_iteration];
|
2018-12-03 03:17:06 +00:00
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
Impl::execute(src_remaining, dst_remaining);
|
2018-12-03 03:17:06 +00:00
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
if constexpr (is_big_int_v<T> || std::is_same_v<T, Decimal256>)
|
|
|
|
for (size_t i = 0; i < rows_remaining; i++)
|
|
|
|
dst_data[rows_size + i] = dst_remaining[i];
|
|
|
|
else
|
|
|
|
memcpy(&dst_data[rows_size], dst_remaining, rows_remaining * sizeof(ReturnType));
|
2019-08-01 00:29:32 +00:00
|
|
|
}
|
2018-12-03 03:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
template <typename T, typename ReturnType>
|
2020-10-17 15:22:42 +00:00
|
|
|
static ColumnPtr execute(const ColumnVector<T> * col)
|
2018-12-03 03:17:06 +00:00
|
|
|
{
|
|
|
|
const auto & src_data = col->getData();
|
|
|
|
const size_t size = src_data.size();
|
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
auto dst = ColumnVector<ReturnType>::create();
|
2018-12-03 03:17:06 +00:00
|
|
|
auto & dst_data = dst->getData();
|
|
|
|
dst_data.resize(size);
|
|
|
|
|
|
|
|
executeInIterations(src_data.data(), dst_data.data(), size);
|
|
|
|
|
2020-10-17 15:22:42 +00:00
|
|
|
return dst;
|
2018-12-03 03:17:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
template <typename T, typename ReturnType>
|
2020-10-17 15:22:42 +00:00
|
|
|
static ColumnPtr execute(const ColumnDecimal<T> * col)
|
2018-12-03 03:17:06 +00:00
|
|
|
{
|
|
|
|
const auto & src_data = col->getData();
|
|
|
|
const size_t size = src_data.size();
|
|
|
|
UInt32 scale = src_data.getScale();
|
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
auto dst = ColumnVector<ReturnType>::create();
|
2018-12-03 03:17:06 +00:00
|
|
|
auto & dst_data = dst->getData();
|
|
|
|
dst_data.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2020-08-26 12:26:12 +00:00
|
|
|
dst_data[i] = DecimalUtils::convertTo<ReturnType>(src_data[i], scale);
|
2018-12-03 03:17:06 +00:00
|
|
|
|
|
|
|
executeInIterations(dst_data.data(), dst_data.data(), size);
|
|
|
|
|
2020-10-17 15:22:42 +00:00
|
|
|
return dst;
|
2018-12-03 03:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2018-12-03 03:17:06 +00:00
|
|
|
{
|
2020-10-17 15:22:42 +00:00
|
|
|
const ColumnWithTypeAndName & col = arguments[0];
|
|
|
|
ColumnPtr res;
|
2018-12-03 03:17:06 +00:00
|
|
|
|
|
|
|
auto call = [&](const auto & types) -> bool
|
|
|
|
{
|
|
|
|
using Types = std::decay_t<decltype(types)>;
|
|
|
|
using Type = typename Types::RightType;
|
2019-08-01 00:29:32 +00:00
|
|
|
using ReturnType = std::conditional_t<Impl::always_returns_float64 || !std::is_floating_point_v<Type>, Float64, Type>;
|
2021-09-10 11:49:22 +00:00
|
|
|
using ColVecType = ColumnVectorOrDecimal<Type>;
|
2018-12-03 03:17:06 +00:00
|
|
|
|
|
|
|
const auto col_vec = checkAndGetColumn<ColVecType>(col.column.get());
|
2020-10-20 13:11:57 +00:00
|
|
|
return (res = execute<Type, ReturnType>(col_vec)) != nullptr;
|
2018-12-03 03:17:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!callOnBasicType<void, true, true, true, false>(col.type->getTypeId(), call))
|
|
|
|
throw Exception{"Illegal column " + col.column->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2020-10-17 15:22:42 +00:00
|
|
|
|
|
|
|
return res;
|
2018-12-03 03:17:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Name, Float64(Function)(Float64)>
|
|
|
|
struct UnaryFunctionPlain
|
|
|
|
{
|
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
static constexpr auto rows_per_iteration = 1;
|
2019-08-01 00:29:32 +00:00
|
|
|
static constexpr bool always_returns_float64 = true;
|
2018-12-03 03:17:06 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void execute(const T * src, Float64 * dst)
|
|
|
|
{
|
|
|
|
dst[0] = static_cast<Float64>(Function(static_cast<Float64>(src[0])));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define UnaryFunctionVectorized UnaryFunctionPlain
|
|
|
|
|
|
|
|
}
|