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>
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2018-12-03 03:17:06 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2020-04-16 12:31:57 +00:00
|
|
|
|
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include "config_functions.h"
|
|
|
|
#endif
|
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;
|
2019-08-01 00:29:32 +00:00
|
|
|
static FunctionPtr create(const Context &) { 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; }
|
|
|
|
|
|
|
|
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)
|
|
|
|
tmp_vec[i] = src_data[i];
|
|
|
|
|
|
|
|
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];
|
|
|
|
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));
|
|
|
|
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
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
memcpy(&dst_data[rows_size], dst_remaining, rows_remaining * sizeof(ReturnType));
|
|
|
|
}
|
2018-12-03 03:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
template <typename T, typename ReturnType>
|
2018-12-03 03:17:06 +00:00
|
|
|
static bool execute(Block & block, const ColumnVector<T> * col, const size_t result)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(dst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-01 00:29:32 +00:00
|
|
|
template <typename T, typename ReturnType>
|
2018-12-03 03:17:06 +00:00
|
|
|
static bool execute(Block & block, const ColumnDecimal<T> * col, const size_t result)
|
|
|
|
{
|
|
|
|
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)
|
2019-08-01 00:29:32 +00:00
|
|
|
dst_data[i] = convertFromDecimal<DataTypeDecimal<T>, DataTypeNumber<ReturnType>>(src_data[i], scale);
|
2018-12-03 03:17:06 +00:00
|
|
|
|
|
|
|
executeInIterations(dst_data.data(), dst_data.data(), size);
|
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(dst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2018-12-03 03:17:06 +00:00
|
|
|
{
|
|
|
|
const ColumnWithTypeAndName & col = block.getByPosition(arguments[0]);
|
|
|
|
|
|
|
|
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>;
|
2018-12-03 03:17:06 +00:00
|
|
|
using ColVecType = std::conditional_t<IsDecimalNumber<Type>, ColumnDecimal<Type>, ColumnVector<Type>>;
|
|
|
|
|
|
|
|
const auto col_vec = checkAndGetColumn<ColVecType>(col.column.get());
|
2019-08-01 00:29:32 +00:00
|
|
|
return execute<Type, ReturnType>(block, col_vec, result);
|
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};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|