2018-11-26 16:20:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-04-22 14:45:38 +00:00
|
|
|
#include <Columns/ColumnDecimal.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
2023-04-22 17:21:43 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2023-04-22 14:45:38 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2020-02-14 08:17:32 +00:00
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
2022-10-14 02:16:12 +00:00
|
|
|
#include <DataTypes/DataTypeInterval.h>
|
2023-04-22 17:21:43 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2023-04-22 14:45:38 +00:00
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2018-11-26 16:20:40 +00:00
|
|
|
#include <DataTypes/Native.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
2023-04-22 14:45:38 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-09-08 00:59:13 +00:00
|
|
|
#include <Functions/IsOperation.h>
|
2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/castTypeToEither.h>
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2022-09-28 08:45:15 +00:00
|
|
|
#include "config.h"
|
2022-05-17 11:53:20 +00:00
|
|
|
#include <Common/TargetSpecific.h>
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <llvm/IR/IRBuilder.h>
|
2018-11-26 16:20:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename A, typename Op>
|
|
|
|
struct UnaryOperationImpl
|
|
|
|
{
|
|
|
|
using ResultType = typename Op::ResultType;
|
2021-09-10 11:49:22 +00:00
|
|
|
using ColVecA = ColumnVectorOrDecimal<A>;
|
|
|
|
using ColVecC = ColumnVectorOrDecimal<ResultType>;
|
2018-11-26 16:20:40 +00:00
|
|
|
using ArrayA = typename ColVecA::Container;
|
|
|
|
using ArrayC = typename ColVecC::Container;
|
|
|
|
|
2022-06-06 01:29:47 +00:00
|
|
|
MULTITARGET_FUNCTION_AVX512BW_AVX512F_AVX2_SSE42(
|
2022-05-24 12:25:29 +00:00
|
|
|
MULTITARGET_FUNCTION_HEADER(static void NO_INLINE), vectorImpl, MULTITARGET_FUNCTION_BODY((const ArrayA & a, ArrayC & c) /// NOLINT
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = Op::apply(a[i]);
|
2022-05-17 11:53:20 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
static void NO_INLINE vector(const ArrayA & a, ArrayC & c)
|
|
|
|
{
|
|
|
|
#if USE_MULTITARGET_CODE
|
2022-06-06 01:29:47 +00:00
|
|
|
if (isArchSupported(TargetArch::AVX512BW))
|
|
|
|
{
|
|
|
|
vectorImplAVX512BW(a, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArchSupported(TargetArch::AVX512F))
|
|
|
|
{
|
|
|
|
vectorImplAVX512F(a, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-17 11:53:20 +00:00
|
|
|
if (isArchSupported(TargetArch::AVX2))
|
|
|
|
{
|
|
|
|
vectorImplAVX2(a, c);
|
|
|
|
return;
|
|
|
|
}
|
2022-06-06 01:29:47 +00:00
|
|
|
|
|
|
|
if (isArchSupported(TargetArch::SSE42))
|
2022-05-17 11:53:20 +00:00
|
|
|
{
|
|
|
|
vectorImplSSE42(a, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vectorImpl(a, c);
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void constant(A a, ResultType & c)
|
|
|
|
{
|
|
|
|
c = Op::apply(a);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-02-14 08:17:32 +00:00
|
|
|
template <typename Op>
|
|
|
|
struct FixedStringUnaryOperationImpl
|
|
|
|
{
|
2022-06-06 01:29:47 +00:00
|
|
|
MULTITARGET_FUNCTION_AVX512BW_AVX512F_AVX2_SSE42(
|
2022-05-24 12:25:29 +00:00
|
|
|
MULTITARGET_FUNCTION_HEADER(static void NO_INLINE), vectorImpl, MULTITARGET_FUNCTION_BODY((const ColumnFixedString::Chars & a, /// NOLINT
|
|
|
|
ColumnFixedString::Chars & c)
|
2020-02-14 08:17:32 +00:00
|
|
|
{
|
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = Op::apply(a[i]);
|
2022-05-17 11:53:20 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
static void NO_INLINE vector(const ColumnFixedString::Chars & a, ColumnFixedString::Chars & c)
|
|
|
|
{
|
|
|
|
#if USE_MULTITARGET_CODE
|
2022-06-06 01:29:47 +00:00
|
|
|
if (isArchSupported(TargetArch::AVX512BW))
|
|
|
|
{
|
|
|
|
vectorImplAVX512BW(a, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArchSupported(TargetArch::AVX512F))
|
|
|
|
{
|
|
|
|
vectorImplAVX512F(a, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-17 11:53:20 +00:00
|
|
|
if (isArchSupported(TargetArch::AVX2))
|
|
|
|
{
|
|
|
|
vectorImplAVX2(a, c);
|
|
|
|
return;
|
|
|
|
}
|
2022-06-06 01:29:47 +00:00
|
|
|
|
|
|
|
if (isArchSupported(TargetArch::SSE42))
|
2022-05-17 11:53:20 +00:00
|
|
|
{
|
|
|
|
vectorImplSSE42(a, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vectorImpl(a, c);
|
2020-02-14 08:17:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-22 14:17:31 +00:00
|
|
|
template <typename Op>
|
|
|
|
struct FixedStringUnaryOperationReduceImpl
|
|
|
|
{
|
|
|
|
MULTITARGET_FUNCTION_AVX512BW_AVX512F_AVX2_SSE42(
|
|
|
|
MULTITARGET_FUNCTION_HEADER(static UInt64 NO_INLINE),
|
|
|
|
vectorImpl,
|
|
|
|
MULTITARGET_FUNCTION_BODY((const UInt8 * start, const UInt8 * end) { /// NOLINT
|
|
|
|
UInt64 res = 0;
|
|
|
|
while (start < end)
|
|
|
|
res += Op::apply(*start++);
|
|
|
|
return res;
|
|
|
|
}))
|
|
|
|
|
|
|
|
static UInt64 NO_INLINE vector(const UInt8 * start, const UInt8 * end)
|
|
|
|
{
|
|
|
|
#if USE_MULTITARGET_CODE
|
|
|
|
if (isArchSupported(TargetArch::AVX512BW))
|
|
|
|
{
|
|
|
|
return vectorImplAVX512BW(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArchSupported(TargetArch::AVX512F))
|
|
|
|
{
|
|
|
|
return vectorImplAVX512F(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArchSupported(TargetArch::AVX2))
|
|
|
|
{
|
|
|
|
return vectorImplAVX2(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArchSupported(TargetArch::SSE42))
|
|
|
|
{
|
|
|
|
return vectorImplSSE42(start, end);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return vectorImpl(start, end);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
template <typename FunctionName>
|
|
|
|
struct FunctionUnaryArithmeticMonotonicity;
|
|
|
|
|
|
|
|
/// Used to indicate undefined operation
|
|
|
|
struct InvalidType;
|
|
|
|
|
|
|
|
|
|
|
|
template <template <typename> class Op, typename Name, bool is_injective>
|
|
|
|
class FunctionUnaryArithmetic : public IFunction
|
|
|
|
{
|
2021-01-26 11:26:15 +00:00
|
|
|
static constexpr bool allow_decimal = IsUnaryOperation<Op>::negate || IsUnaryOperation<Op>::abs || IsUnaryOperation<Op>::sign;
|
2023-04-22 17:21:43 +00:00
|
|
|
static constexpr bool allow_string_or_fixed_string = Op<UInt8>::allow_string_or_fixed_string;
|
|
|
|
static constexpr bool is_bit_count = IsUnaryOperation<Op>::bit_count;
|
2021-01-26 11:26:15 +00:00
|
|
|
static constexpr bool is_sign_function = IsUnaryOperation<Op>::sign;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
2021-08-30 17:13:41 +00:00
|
|
|
ContextPtr context;
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
template <typename F>
|
|
|
|
static bool castType(const IDataType * type, F && f)
|
|
|
|
{
|
|
|
|
return castTypeToEither<
|
|
|
|
DataTypeUInt8,
|
|
|
|
DataTypeUInt16,
|
|
|
|
DataTypeUInt32,
|
|
|
|
DataTypeUInt64,
|
2021-05-03 16:12:28 +00:00
|
|
|
DataTypeUInt128,
|
2020-08-19 11:52:17 +00:00
|
|
|
DataTypeUInt256,
|
2018-11-26 16:20:40 +00:00
|
|
|
DataTypeInt8,
|
|
|
|
DataTypeInt16,
|
|
|
|
DataTypeInt32,
|
|
|
|
DataTypeInt64,
|
2020-08-19 11:52:17 +00:00
|
|
|
DataTypeInt128,
|
|
|
|
DataTypeInt256,
|
2018-11-26 16:20:40 +00:00
|
|
|
DataTypeFloat32,
|
|
|
|
DataTypeFloat64,
|
|
|
|
DataTypeDecimal<Decimal32>,
|
|
|
|
DataTypeDecimal<Decimal64>,
|
2020-02-14 08:17:32 +00:00
|
|
|
DataTypeDecimal<Decimal128>,
|
2020-08-19 11:52:17 +00:00
|
|
|
DataTypeDecimal<Decimal256>,
|
2022-10-14 02:16:12 +00:00
|
|
|
DataTypeFixedString,
|
2023-04-22 17:21:43 +00:00
|
|
|
DataTypeString,
|
|
|
|
DataTypeInterval>(type, std::forward<F>(f));
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 17:13:41 +00:00
|
|
|
static FunctionOverloadResolverPtr
|
|
|
|
getFunctionForTupleArithmetic(const DataTypePtr & type, ContextPtr context)
|
|
|
|
{
|
|
|
|
if (!isTuple(type))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
/// Special case when the function is negate, argument is tuple.
|
|
|
|
/// We construct another function (example: tupleNegate) and call it.
|
|
|
|
|
|
|
|
if constexpr (!IsUnaryOperation<Op>::negate)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return FunctionFactory::instance().get("tupleNegate", context);
|
|
|
|
}
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionUnaryArithmetic>(); }
|
2018-11-26 16:20:40 +00:00
|
|
|
|
2021-08-30 17:13:41 +00:00
|
|
|
FunctionUnaryArithmetic() = default;
|
|
|
|
|
|
|
|
explicit FunctionUnaryArithmetic(ContextPtr context_) : context(context_) {}
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-10-09 07:41:28 +00:00
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override { return is_injective; }
|
2023-04-22 17:21:43 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2021-08-30 17:13:41 +00:00
|
|
|
return getReturnTypeImplStatic(arguments, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DataTypePtr getReturnTypeImplStatic(const DataTypes & arguments, ContextPtr context)
|
|
|
|
{
|
|
|
|
/// Special case when the function is negate, argument is tuple.
|
|
|
|
if (auto function_builder = getFunctionForTupleArithmetic(arguments[0], context))
|
|
|
|
{
|
|
|
|
ColumnsWithTypeAndName new_arguments(1);
|
|
|
|
|
|
|
|
new_arguments[0].type = arguments[0];
|
|
|
|
|
|
|
|
auto function = function_builder->build(new_arguments);
|
|
|
|
return function->getResultType();
|
|
|
|
}
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
DataTypePtr result;
|
|
|
|
bool valid = castType(arguments[0].get(), [&](const auto & type)
|
|
|
|
{
|
|
|
|
using DataType = std::decay_t<decltype(type)>;
|
2020-02-14 08:17:32 +00:00
|
|
|
if constexpr (std::is_same_v<DataTypeFixedString, DataType>)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2023-04-22 17:21:43 +00:00
|
|
|
if constexpr (!allow_string_or_fixed_string)
|
2018-11-26 16:20:40 +00:00
|
|
|
return false;
|
2023-04-22 14:17:31 +00:00
|
|
|
/// For `bitCount`, when argument is FixedString, it's return type
|
|
|
|
/// should be integer instead of FixedString, the return value is
|
|
|
|
/// the sum of `bitCount` apply to each chars.
|
|
|
|
else
|
|
|
|
{
|
2023-04-22 17:21:43 +00:00
|
|
|
if constexpr (is_bit_count)
|
2023-04-22 14:17:31 +00:00
|
|
|
result = std::make_shared<DataTypeUInt64>();
|
|
|
|
else
|
|
|
|
result = std::make_shared<DataType>(type.getN());
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
2023-04-22 17:21:43 +00:00
|
|
|
else if constexpr (std::is_same_v<DataTypeString, DataType>)
|
|
|
|
{
|
|
|
|
if constexpr (!allow_string_or_fixed_string)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if constexpr (is_bit_count)
|
|
|
|
result = std::make_shared<DataTypeUInt64>();
|
|
|
|
else
|
|
|
|
result = std::make_shared<DataType>();
|
|
|
|
}
|
|
|
|
}
|
2022-10-14 02:16:12 +00:00
|
|
|
else if constexpr (std::is_same_v<DataTypeInterval, DataType>)
|
|
|
|
{
|
|
|
|
if constexpr (!IsUnaryOperation<Op>::negate)
|
|
|
|
return false;
|
|
|
|
result = std::make_shared<DataTypeInterval>(type.getKind());
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
else
|
2020-02-14 08:17:32 +00:00
|
|
|
{
|
|
|
|
using T0 = typename DataType::FieldType;
|
|
|
|
|
2021-01-26 11:26:15 +00:00
|
|
|
if constexpr (IsDataTypeDecimal<DataType> && !is_sign_function)
|
2020-02-14 08:17:32 +00:00
|
|
|
{
|
|
|
|
if constexpr (!allow_decimal)
|
|
|
|
return false;
|
|
|
|
result = std::make_shared<DataType>(type.getPrecision(), type.getScale());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = std::make_shared<DataTypeNumber<typename Op<T0>::ResultType>>();
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (!valid)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}",
|
|
|
|
arguments[0]->getName(), String(name));
|
2018-11-26 16:20:40 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-30 17:13:41 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2021-08-30 17:13:41 +00:00
|
|
|
/// Special case when the function is negate, argument is tuple.
|
|
|
|
if (auto function_builder = getFunctionForTupleArithmetic(arguments[0].type, context))
|
|
|
|
{
|
|
|
|
return function_builder->build(arguments)->execute(arguments, result_type, input_rows_count);
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:23:37 +00:00
|
|
|
ColumnPtr result_column;
|
|
|
|
bool valid = castType(arguments[0].type.get(), [&](const auto & type)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
using DataType = std::decay_t<decltype(type)>;
|
|
|
|
|
2020-02-14 08:17:32 +00:00
|
|
|
if constexpr (std::is_same_v<DataTypeFixedString, DataType>)
|
|
|
|
{
|
2023-04-22 17:21:43 +00:00
|
|
|
if constexpr (allow_string_or_fixed_string)
|
2020-02-14 08:17:32 +00:00
|
|
|
{
|
2020-10-17 14:23:37 +00:00
|
|
|
if (const auto * col = checkAndGetColumn<ColumnFixedString>(arguments[0].column.get()))
|
2020-02-14 08:17:32 +00:00
|
|
|
{
|
2023-04-22 17:21:43 +00:00
|
|
|
if constexpr (is_bit_count)
|
2023-04-22 14:17:31 +00:00
|
|
|
{
|
|
|
|
auto size = col->size();
|
|
|
|
|
|
|
|
auto col_res = ColumnUInt64::create(size);
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
|
|
|
|
const auto & chars = col->getChars();
|
|
|
|
auto n = col->getN();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
vec_res[i] = FixedStringUnaryOperationReduceImpl<Op<UInt8>>::vector(
|
|
|
|
chars.data() + n * i, chars.data() + n * (i + 1));
|
|
|
|
}
|
|
|
|
result_column = std::move(col_res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto col_res = ColumnFixedString::create(col->getN());
|
|
|
|
auto & vec_res = col_res->getChars();
|
|
|
|
vec_res.resize(col->size() * col->getN());
|
|
|
|
FixedStringUnaryOperationImpl<Op<UInt8>>::vector(col->getChars(), vec_res);
|
|
|
|
result_column = std::move(col_res);
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-14 08:17:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 17:21:43 +00:00
|
|
|
else if constexpr (std::is_same_v<DataTypeString, DataType>)
|
|
|
|
{
|
|
|
|
if constexpr (allow_string_or_fixed_string)
|
|
|
|
{
|
|
|
|
if (const auto * col = checkAndGetColumn<ColumnString>(arguments[0].column.get()))
|
|
|
|
{
|
|
|
|
if constexpr (is_bit_count)
|
|
|
|
{
|
|
|
|
auto size = col->size();
|
|
|
|
|
|
|
|
auto col_res = ColumnUInt64::create(size);
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
|
|
|
|
const auto & chars = col->getChars();
|
|
|
|
const auto & offsets = col->getOffsets();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
vec_res[i] = FixedStringUnaryOperationReduceImpl<Op<UInt8>>::vector(
|
|
|
|
chars.data() + offsets[i], chars.data() + offsets[i + 1]);
|
|
|
|
}
|
|
|
|
result_column = std::move(col_res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto col_res = ColumnString::create();
|
|
|
|
auto & vec_res = col_res->getChars();
|
|
|
|
auto & offset_res = col_res->getOffsets();
|
|
|
|
|
|
|
|
const auto & vec_col_str = col->getChars();
|
|
|
|
const auto & offset_col_str = col->getOffsets();
|
|
|
|
|
|
|
|
vec_res.resize(vec_col_str.size());
|
|
|
|
offset_res.resize(offset_col_str.size());
|
|
|
|
memcpy(offset_res.data(), offset_col_str.data(), offset_res.size() * sizeof(UInt64));
|
|
|
|
|
|
|
|
FixedStringUnaryOperationImpl<Op<UInt8>>::vector(vec_col_str, vec_res);
|
|
|
|
result_column = std::move(col_res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 08:17:32 +00:00
|
|
|
else if constexpr (IsDataTypeDecimal<DataType>)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2020-02-14 08:17:32 +00:00
|
|
|
using T0 = typename DataType::FieldType;
|
2018-11-26 16:20:40 +00:00
|
|
|
if constexpr (allow_decimal)
|
|
|
|
{
|
2020-10-17 14:23:37 +00:00
|
|
|
if (auto col = checkAndGetColumn<ColumnDecimal<T0>>(arguments[0].column.get()))
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2021-01-26 11:26:15 +00:00
|
|
|
if constexpr (is_sign_function)
|
|
|
|
{
|
|
|
|
auto col_res = ColumnVector<typename Op<T0>::ResultType>::create();
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(col->getData().size());
|
|
|
|
UnaryOperationImpl<T0, Op<T0>>::vector(col->getData(), vec_res);
|
|
|
|
result_column = std::move(col_res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto col_res = ColumnDecimal<typename Op<T0>::ResultType>::create(0, type.getScale());
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(col->getData().size());
|
|
|
|
UnaryOperationImpl<T0, Op<T0>>::vector(col->getData(), vec_res);
|
|
|
|
result_column = std::move(col_res);
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-14 08:17:32 +00:00
|
|
|
using T0 = typename DataType::FieldType;
|
2020-10-17 14:23:37 +00:00
|
|
|
if (auto col = checkAndGetColumn<ColumnVector<T0>>(arguments[0].column.get()))
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
auto col_res = ColumnVector<typename Op<T0>::ResultType>::create();
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(col->getData().size());
|
|
|
|
UnaryOperationImpl<T0, Op<T0>>::vector(col->getData(), vec_res);
|
2020-10-17 14:23:37 +00:00
|
|
|
result_column = std::move(col_res);
|
2018-11-26 16:20:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (!valid)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "{}'s argument does not match the expected data type", getName());
|
2020-10-17 14:23:37 +00:00
|
|
|
|
|
|
|
return result_column;
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
bool isCompilableImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2020-08-26 16:49:19 +00:00
|
|
|
if (1 != arguments.size())
|
|
|
|
return false;
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
return castType(arguments[0].get(), [&](const auto & type)
|
|
|
|
{
|
|
|
|
using DataType = std::decay_t<decltype(type)>;
|
2020-02-14 08:17:32 +00:00
|
|
|
if constexpr (std::is_same_v<DataTypeFixedString, DataType>)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return !IsDataTypeDecimal<DataType> && Op<typename DataType::FieldType>::compilable;
|
2018-11-26 16:20:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-03 22:34:40 +00:00
|
|
|
llvm::Value * compileImpl(llvm::IRBuilderBase & builder, const DataTypes & types, Values values) const override
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2020-08-26 16:49:19 +00:00
|
|
|
assert(1 == types.size() && 1 == values.size());
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
llvm::Value * result = nullptr;
|
|
|
|
castType(types[0].get(), [&](const auto & type)
|
|
|
|
{
|
|
|
|
using DataType = std::decay_t<decltype(type)>;
|
2020-02-14 08:17:32 +00:00
|
|
|
if constexpr (std::is_same_v<DataTypeFixedString, DataType>)
|
|
|
|
return false;
|
|
|
|
else
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2020-02-14 08:17:32 +00:00
|
|
|
using T0 = typename DataType::FieldType;
|
|
|
|
using T1 = typename Op<T0>::ResultType;
|
|
|
|
if constexpr (!std::is_same_v<T1, InvalidType> && !IsDataTypeDecimal<DataType> && Op<T0>::compilable)
|
|
|
|
{
|
|
|
|
auto & b = static_cast<llvm::IRBuilder<> &>(builder);
|
2021-05-03 22:34:40 +00:00
|
|
|
auto * v = nativeCast(b, types[0], values[0], std::make_shared<DataTypeNumber<T1>>());
|
2020-02-14 08:17:32 +00:00
|
|
|
result = Op<T0>::compile(b, v, is_signed_v<T1>);
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool hasInformationAboutMonotonicity() const override
|
|
|
|
{
|
|
|
|
return FunctionUnaryArithmeticMonotonicity<Name>::has();
|
|
|
|
}
|
|
|
|
|
|
|
|
Monotonicity getMonotonicityForRange(const IDataType &, const Field & left, const Field & right) const override
|
|
|
|
{
|
|
|
|
return FunctionUnaryArithmeticMonotonicity<Name>::get(left, right);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PositiveMonotonicity
|
|
|
|
{
|
|
|
|
static bool has() { return true; }
|
|
|
|
static IFunction::Monotonicity get(const Field &, const Field &)
|
|
|
|
{
|
2021-09-29 16:01:26 +00:00
|
|
|
return { .is_monotonic = true };
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|