2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
2020-08-07 23:01:05 +00:00
|
|
|
|
2019-07-15 14:45:56 +00:00
|
|
|
#include <numeric>
|
2020-08-07 23:01:05 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
constexpr T abs(T value) noexcept
|
|
|
|
{
|
|
|
|
if constexpr (std::is_signed_v<T>)
|
|
|
|
{
|
|
|
|
if (value >= 0 || value == std::numeric_limits<T>::min())
|
|
|
|
return value;
|
|
|
|
return -value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-07-15 14:45:56 +00:00
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-08-19 11:52:17 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
struct LCMImpl
|
|
|
|
{
|
|
|
|
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
|
2020-02-14 07:11:37 +00:00
|
|
|
static const constexpr bool allow_fixed_string = false;
|
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
template <typename Result = ResultType>
|
2020-09-04 13:33:02 +00:00
|
|
|
static inline std::enable_if_t<is_big_int_v<A> || is_big_int_v<B> || is_big_int_v<Result>, Result>
|
2020-08-19 11:52:17 +00:00
|
|
|
apply([[maybe_unused]] A a, [[maybe_unused]] B b)
|
|
|
|
{
|
|
|
|
throw Exception("LCM is not implemented for big integers", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
2020-09-04 13:33:02 +00:00
|
|
|
static inline std::enable_if_t<!is_big_int_v<A> && !is_big_int_v<B> && !is_big_int_v<Result>, Result>
|
2020-08-19 11:52:17 +00:00
|
|
|
apply([[maybe_unused]] A a, [[maybe_unused]] B b)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
|
|
|
|
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<B>::Type(b), typename NumberTraits::ToInteger<A>::Type(a));
|
2020-08-07 23:01:05 +00:00
|
|
|
|
|
|
|
/** It's tempting to use std::lcm function.
|
|
|
|
* But it has undefined behaviour on overflow.
|
|
|
|
* And assert in debug build.
|
|
|
|
* We need some well defined behaviour instead
|
|
|
|
* (example: throw an exception or overflow in implementation specific way).
|
|
|
|
*/
|
|
|
|
|
|
|
|
using Int = typename NumberTraits::ToInteger<Result>::Type;
|
2020-08-19 11:52:17 +00:00
|
|
|
using Unsigned = make_unsigned_t<Int>;
|
2020-08-07 23:01:05 +00:00
|
|
|
|
2020-08-08 02:02:36 +00:00
|
|
|
Unsigned val1 = abs<Int>(a) / std::gcd(Int(a), Int(b));
|
|
|
|
Unsigned val2 = abs<Int>(b);
|
2020-08-07 23:01:05 +00:00
|
|
|
|
|
|
|
/// Overflow in implementation specific way.
|
|
|
|
return Result(val1 * val2);
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = false; /// exceptions (and a non-trivial algorithm)
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NameLCM { static constexpr auto name = "lcm"; };
|
2020-09-05 14:12:47 +00:00
|
|
|
using FunctionLCM = BinaryArithmeticOverloadResolver<LCMImpl, NameLCM, false>;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
void registerFunctionLCM(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionLCM>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|