// Copyright 2023 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include #include namespace DB { namespace { template || IsDecimal> struct GCDImpl; template struct GCDImpl { using ResultType = typename NumberTraits::ResultOfAdditionMultiplication::Type; template static Result apply(A a, B b) { throwIfDivisionLeadsToFPE( typename NumberTraits::ToInteger::Type(a), typename NumberTraits::ToInteger::Type(b)); throwIfDivisionLeadsToFPE( typename NumberTraits::ToInteger::Type(b), typename NumberTraits::ToInteger::Type(a)); return boost::integer::gcd( typename NumberTraits::ToInteger::Type(a), typename NumberTraits::ToInteger::Type(b)); } template static Result apply(A, B, UInt8 &) { throw Exception("Should not reach here"); } }; template struct GCDImpl { using ResultType = If || std::is_unsigned_v, uint64_t, int64_t>; template static Result apply(A a, B b) { return GCDImpl::apply(static_cast(a), static_cast(b)); } template static Result apply(A, B, UInt8 &) { throw Exception("Should not reach here"); } }; // clang-format off struct NameGCD { static constexpr auto name = "gcd"; }; // clang-format on template using GCDImpl_t = GCDImpl; using FunctionGCD = FunctionBinaryArithmetic; } // namespace void registerFunctionGCD(FunctionFactory & factory) { factory.registerFunction(); } } // namespace DB