2020-02-25 09:46:07 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-02-25 09:46:07 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct ModuloOrZeroImpl
|
|
|
|
{
|
|
|
|
using ResultType = typename NumberTraits::ResultOfModulo<A, B>::Type;
|
|
|
|
static const constexpr bool allow_fixed_string = false;
|
2021-08-30 06:37:23 +00:00
|
|
|
static const constexpr bool allow_string_integer = false;
|
2020-02-25 09:46:07 +00:00
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
2020-07-20 01:03:46 +00:00
|
|
|
if constexpr (std::is_floating_point_v<ResultType>)
|
|
|
|
{
|
2020-07-21 14:07:09 +00:00
|
|
|
/// This computation is similar to `fmod` but the latter is not inlined and has 40 times worse performance.
|
2020-07-21 14:06:40 +00:00
|
|
|
return ResultType(a) - trunc(ResultType(a) / ResultType(b)) * ResultType(b);
|
2020-07-20 01:03:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (unlikely(divisionLeadsToFPE(a, b)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ModuloImpl<A, B>::template apply<Result>(a, b);
|
|
|
|
}
|
2020-02-25 09:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = false; /// TODO implement the checks
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NameModuloOrZero { static constexpr auto name = "moduloOrZero"; };
|
2020-09-05 14:12:47 +00:00
|
|
|
using FunctionModuloOrZero = BinaryArithmeticOverloadResolver<ModuloOrZeroImpl, NameModuloOrZero>;
|
2020-02-25 09:46:07 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(ModuloOrZero)
|
2020-02-25 09:46:07 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionModuloOrZero>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|