ClickHouse/src/Functions/intExp2.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
2.1 KiB
C++
Raw Normal View History

#include <Functions/FunctionFactory.h>
#include <Functions/FunctionUnaryArithmetic.h>
2021-06-14 04:13:35 +00:00
#include <Common/FieldVisitorConvertToNumber.h>
2018-12-27 00:02:11 +00:00
#include <Common/intExp.h>
namespace DB
{
2020-02-25 18:10:48 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int NOT_IMPLEMENTED;
2020-02-25 18:10:48 +00:00
}
2020-09-07 18:00:37 +00:00
namespace
{
template <typename A>
struct IntExp2Impl
{
using ResultType = UInt64;
2023-04-22 17:21:43 +00:00
static constexpr bool allow_string_or_fixed_string = false;
static inline ResultType apply([[maybe_unused]] A a)
{
if constexpr (is_big_int_v<A>)
throw DB::Exception(ErrorCodes::NOT_IMPLEMENTED, "intExp2 not implemented for big integers");
else
2022-09-11 03:38:59 +00:00
return intExp2(static_cast<int>(a));
}
#if USE_EMBEDDED_COMPILER
static constexpr bool compilable = true;
static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * arg, bool)
{
if (!arg->getType()->isIntegerTy())
throw Exception(ErrorCodes::LOGICAL_ERROR, "IntExp2Impl expected an integral type");
return b.CreateShl(llvm::ConstantInt::get(arg->getType(), 1), arg);
}
#endif
};
/// Assumed to be injective for the purpose of query optimization, but in fact it is not injective because of possible overflow.
struct NameIntExp2 { static constexpr auto name = "intExp2"; };
using FunctionIntExp2 = FunctionUnaryArithmetic<IntExp2Impl, NameIntExp2, true>;
2020-09-07 18:00:37 +00:00
}
template <> struct FunctionUnaryArithmeticMonotonicity<NameIntExp2>
{
static bool has() { return true; }
static IFunction::Monotonicity get(const Field & left, const Field & right)
{
Float64 left_float = left.isNull() ? -std::numeric_limits<Float64>::infinity() : applyVisitor(FieldVisitorConvertToNumber<Float64>(), left);
Float64 right_float = right.isNull() ? std::numeric_limits<Float64>::infinity() : applyVisitor(FieldVisitorConvertToNumber<Float64>(), right);
if (left_float < 0 || right_float > 63)
return {};
2022-09-22 15:49:27 +00:00
return { .is_monotonic = true, .is_strict = true, };
}
};
REGISTER_FUNCTION(IntExp2)
{
factory.registerFunction<FunctionIntExp2>();
}
}