2014-11-28 14:08:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-09-14 14:40:51 +00:00
|
|
|
#include <Core/callOnTypeIndex.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2018-09-14 14:40:51 +00:00
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2018-09-14 14:40:51 +00:00
|
|
|
#include <Columns/ColumnDecimal.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2017-07-21 06:35:58 +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"
|
2016-01-14 01:44:21 +00:00
|
|
|
|
2014-11-28 14:08:38 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2017-06-13 02:06:53 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:08:38 +00:00
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
template <typename Impl>
|
|
|
|
class FunctionMathBinaryFloat64 : public IFunction
|
2014-11-28 14:08:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Impl::name;
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionMathBinaryFloat64>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
static_assert(Impl::rows_per_iteration > 0, "Impl must process at least one row per iteration");
|
2014-11-28 14:08:38 +00:00
|
|
|
|
2017-07-30 19:47:32 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2017-07-30 19:47:32 +00:00
|
|
|
|
2014-11-28 14:08:38 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2017-12-09 14:01:42 +00:00
|
|
|
const auto check_argument_type = [this] (const IDataType * arg)
|
|
|
|
{
|
2019-05-24 12:11:03 +00:00
|
|
|
if (!isNativeNumber(arg))
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Illegal type " + arg->getName() + " of argument of function " + getName(),
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
};
|
|
|
|
|
|
|
|
check_argument_type(arguments.front().get());
|
|
|
|
check_argument_type(arguments.back().get());
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeFloat64>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename LeftType, typename RightType>
|
2020-10-18 14:49:19 +00:00
|
|
|
ColumnPtr executeTyped(const ColumnConst * left_arg, const IColumn * right_arg) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const auto right_arg_typed = checkAndGetColumn<ColumnVector<RightType>>(right_arg))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-16 05:46:46 +00:00
|
|
|
auto dst = ColumnVector<Float64>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
LeftType left_src_data[Impl::rows_per_iteration];
|
2017-07-21 06:35:58 +00:00
|
|
|
std::fill(std::begin(left_src_data), std::end(left_src_data), left_arg->template getValue<LeftType>());
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & right_src_data = right_arg_typed->getData();
|
|
|
|
const auto src_size = right_src_data.size();
|
|
|
|
auto & dst_data = dst->getData();
|
|
|
|
dst_data.resize(src_size);
|
|
|
|
|
|
|
|
const auto rows_remaining = src_size % Impl::rows_per_iteration;
|
|
|
|
const auto rows_size = src_size - rows_remaining;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rows_size; i += Impl::rows_per_iteration)
|
|
|
|
Impl::execute(left_src_data, &right_src_data[i], &dst_data[i]);
|
|
|
|
|
|
|
|
if (rows_remaining != 0)
|
|
|
|
{
|
|
|
|
RightType right_src_remaining[Impl::rows_per_iteration];
|
2021-01-26 19:39:03 +00:00
|
|
|
memcpy(right_src_remaining, &right_src_data[rows_size], rows_remaining * sizeof(RightType));
|
|
|
|
memset(right_src_remaining + rows_remaining, 0, (Impl::rows_per_iteration - rows_remaining) * sizeof(RightType));
|
2017-04-01 07:20:54 +00:00
|
|
|
Float64 dst_remaining[Impl::rows_per_iteration];
|
|
|
|
|
|
|
|
Impl::execute(left_src_data, right_src_remaining, dst_remaining);
|
|
|
|
|
|
|
|
memcpy(&dst_data[rows_size], dst_remaining, rows_remaining * sizeof(Float64));
|
|
|
|
}
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return dst;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename LeftType, typename RightType>
|
2020-10-18 14:49:19 +00:00
|
|
|
ColumnPtr executeTyped(const ColumnVector<LeftType> * left_arg, const IColumn * right_arg) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const auto right_arg_typed = checkAndGetColumn<ColumnVector<RightType>>(right_arg))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-16 05:46:46 +00:00
|
|
|
auto dst = ColumnVector<Float64>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const auto & left_src_data = left_arg->getData();
|
|
|
|
const auto & right_src_data = right_arg_typed->getData();
|
|
|
|
const auto src_size = left_src_data.size();
|
|
|
|
auto & dst_data = dst->getData();
|
|
|
|
dst_data.resize(src_size);
|
|
|
|
|
|
|
|
const auto rows_remaining = src_size % Impl::rows_per_iteration;
|
|
|
|
const auto rows_size = src_size - rows_remaining;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rows_size; i += Impl::rows_per_iteration)
|
|
|
|
Impl::execute(&left_src_data[i], &right_src_data[i], &dst_data[i]);
|
|
|
|
|
|
|
|
if (rows_remaining != 0)
|
|
|
|
{
|
|
|
|
LeftType left_src_remaining[Impl::rows_per_iteration];
|
2021-01-26 19:39:03 +00:00
|
|
|
memcpy(left_src_remaining, &left_src_data[rows_size], rows_remaining * sizeof(LeftType));
|
|
|
|
memset(left_src_remaining + rows_remaining, 0, (Impl::rows_per_iteration - rows_remaining) * sizeof(LeftType));
|
2020-08-19 11:52:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
RightType right_src_remaining[Impl::rows_per_iteration];
|
2021-01-26 19:39:03 +00:00
|
|
|
memcpy(right_src_remaining, &right_src_data[rows_size], rows_remaining * sizeof(RightType));
|
|
|
|
memset(right_src_remaining + rows_remaining, 0, (Impl::rows_per_iteration - rows_remaining) * sizeof(RightType));
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Float64 dst_remaining[Impl::rows_per_iteration];
|
|
|
|
|
|
|
|
Impl::execute(left_src_remaining, right_src_remaining, dst_remaining);
|
|
|
|
|
|
|
|
memcpy(&dst_data[rows_size], dst_remaining, rows_remaining * sizeof(Float64));
|
|
|
|
}
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return dst;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2019-01-04 13:32:08 +00:00
|
|
|
if (const auto right_arg_typed = checkAndGetColumnConst<ColumnVector<RightType>>(right_arg))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-16 05:46:46 +00:00
|
|
|
auto dst = ColumnVector<Float64>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const auto & left_src_data = left_arg->getData();
|
|
|
|
RightType right_src_data[Impl::rows_per_iteration];
|
2017-07-21 06:35:58 +00:00
|
|
|
std::fill(std::begin(right_src_data), std::end(right_src_data), right_arg_typed->template getValue<RightType>());
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto src_size = left_src_data.size();
|
|
|
|
auto & dst_data = dst->getData();
|
|
|
|
dst_data.resize(src_size);
|
|
|
|
|
|
|
|
const auto rows_remaining = src_size % Impl::rows_per_iteration;
|
|
|
|
const auto rows_size = src_size - rows_remaining;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rows_size; i += Impl::rows_per_iteration)
|
|
|
|
Impl::execute(&left_src_data[i], right_src_data, &dst_data[i]);
|
|
|
|
|
|
|
|
if (rows_remaining != 0)
|
|
|
|
{
|
|
|
|
LeftType left_src_remaining[Impl::rows_per_iteration];
|
2021-01-26 19:39:03 +00:00
|
|
|
memcpy(left_src_remaining, &left_src_data[rows_size], rows_remaining * sizeof(LeftType));
|
|
|
|
memset(left_src_remaining + rows_remaining, 0, (Impl::rows_per_iteration - rows_remaining) * sizeof(LeftType));
|
2020-08-19 11:52:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Float64 dst_remaining[Impl::rows_per_iteration];
|
|
|
|
|
|
|
|
Impl::execute(left_src_remaining, right_src_data, dst_remaining);
|
|
|
|
|
|
|
|
memcpy(&dst_data[rows_size], dst_remaining, rows_remaining * sizeof(Float64));
|
|
|
|
}
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return dst;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const ColumnWithTypeAndName & col_left = arguments[0];
|
|
|
|
const ColumnWithTypeAndName & col_right = arguments[1];
|
|
|
|
ColumnPtr res;
|
2018-09-14 14:40:51 +00:00
|
|
|
|
|
|
|
auto call = [&](const auto & types) -> bool
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-09-14 14:40:51 +00:00
|
|
|
using Types = std::decay_t<decltype(types)>;
|
|
|
|
using LeftType = typename Types::LeftType;
|
|
|
|
using RightType = typename Types::RightType;
|
|
|
|
using ColVecLeft = ColumnVector<LeftType>;
|
|
|
|
|
|
|
|
const IColumn * left_arg = col_left.column.get();
|
|
|
|
const IColumn * right_arg = col_right.column.get();
|
|
|
|
|
|
|
|
if (const auto left_arg_typed = checkAndGetColumn<ColVecLeft>(left_arg))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
if ((res = executeTyped<LeftType, RightType>(left_arg_typed, right_arg)))
|
2018-09-14 14:40:51 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
throw Exception{"Illegal column " + right_arg->getName() + " of second argument of function " + getName(),
|
2017-07-21 06:35:58 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2019-01-04 13:32:08 +00:00
|
|
|
if (const auto left_arg_typed = checkAndGetColumnConst<ColVecLeft>(left_arg))
|
2017-07-30 19:47:32 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
if ((res = executeTyped<LeftType, RightType>(left_arg_typed, right_arg)))
|
2018-09-14 14:40:51 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
throw Exception{"Illegal column " + right_arg->getName() + " of second argument of function " + getName(),
|
2017-07-30 19:47:32 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-09-14 14:40:51 +00:00
|
|
|
return false;
|
|
|
|
};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-09-14 14:40:51 +00:00
|
|
|
TypeIndex left_index = col_left.type->getTypeId();
|
|
|
|
TypeIndex right_index = col_right.type->getTypeId();
|
|
|
|
|
2018-10-11 21:31:36 +00:00
|
|
|
if (!callOnBasicTypes<true, true, false, false>(left_index, right_index, call))
|
2018-09-14 14:40:51 +00:00
|
|
|
throw Exception{"Illegal column " + col_left.column->getName() + " of argument of function " + getName(),
|
2017-07-30 19:47:32 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2020-10-18 14:49:19 +00:00
|
|
|
|
|
|
|
return res;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-11-28 14:08:38 +00:00
|
|
|
};
|
|
|
|
|
2016-01-14 01:44:21 +00:00
|
|
|
|
2017-08-01 01:25:03 +00:00
|
|
|
template <typename Name, Float64(Function)(Float64, Float64)>
|
2022-05-23 10:45:16 +00:00
|
|
|
struct BinaryFunctionVectorized
|
2016-01-14 01:44:21 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
static constexpr auto rows_per_iteration = 1;
|
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
2017-07-21 06:35:58 +00:00
|
|
|
static void execute(const T1 * src_left, const T2 * src_right, Float64 * dst)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
dst[0] = static_cast<Float64>(Function(static_cast<Float64>(src_left[0]), static_cast<Float64>(src_right[0])));
|
|
|
|
}
|
2016-01-14 01:44:21 +00:00
|
|
|
};
|
|
|
|
|
2014-11-28 14:08:38 +00:00
|
|
|
}
|