2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2021-04-10 23:33:54 +00:00
|
|
|
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/range.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
class FunctionNumericPredicate : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Impl::name;
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionNumericPredicate>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-04-29 14:48:26 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2019-05-24 12:11:03 +00:00
|
|
|
if (!isNativeNumber(arguments.front()))
|
2022-08-17 19:09:56 +00:00
|
|
|
throw Exception{"Argument for function " + getName() + " must be a number", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-08 22:04:39 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto * in = arguments.front().column.get();
|
|
|
|
|
|
|
|
ColumnPtr res;
|
2020-10-20 13:11:57 +00:00
|
|
|
if (!((res = execute<UInt8>(in))
|
2020-10-18 14:49:19 +00:00
|
|
|
|| (res = execute<UInt16>(in))
|
|
|
|
|| (res = execute<UInt32>(in))
|
|
|
|
|| (res = execute<UInt64>(in))
|
|
|
|
|| (res = execute<Int8>(in))
|
|
|
|
|| (res = execute<Int16>(in))
|
|
|
|
|| (res = execute<Int32>(in))
|
|
|
|
|| (res = execute<Int64>(in))
|
|
|
|
|| (res = execute<Float32>(in))
|
|
|
|
|| (res = execute<Float64>(in))))
|
2018-09-08 22:04:39 +00:00
|
|
|
throw Exception{"Illegal column " + in->getName() + " of first argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN};
|
2020-10-18 14:49:19 +00:00
|
|
|
|
|
|
|
return res;
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-10-18 14:49:19 +00:00
|
|
|
ColumnPtr execute(const IColumn * in_untyped) const
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
if (const auto in = checkAndGetColumn<ColumnVector<T>>(in_untyped))
|
|
|
|
{
|
|
|
|
const auto size = in->size();
|
|
|
|
|
|
|
|
auto out = ColumnUInt8::create(size);
|
|
|
|
|
|
|
|
const auto & in_data = in->getData();
|
|
|
|
auto & out_data = out->getData();
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto i : collections::range(0, size))
|
2018-09-08 22:04:39 +00:00
|
|
|
out_data[i] = Impl::execute(in_data[i]);
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return out;
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return nullptr;
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|