// 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 #include namespace DB { namespace { template || IsDecimal> struct BitShiftRightImpl; template struct BitShiftRightImpl { using ResultType = typename NumberTraits::ResultOfBit::Type; template static Result apply(A a, B b) { // It is an undefined behavior for shift operation in c++ that the right operand is negative or greater than // or equal to the number of digits of the bits in the (promoted) left operand. // See https://en.cppreference.com/w/cpp/language/operator_arithmetic for details. if (static_cast(b) >= std::numeric_limits(a))>::digits) { return static_cast(0); } // Note that we do not consider the case that the right operand is negative, // since other types will all be cast to uint64 before shift operation // according to DAGExpressionAnalyzerHelper::buildBitwiseFunction. // Therefore, we simply suppress clang-tidy checking here. return static_cast(a) >> static_cast(b); // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult) } template static Result apply(A, B, UInt8 &) { throw Exception("Should not reach here"); } }; template struct BitShiftRightImpl { using ResultType = If || std::is_unsigned_v, uint64_t, int64_t>; template static Result apply(A a, B b) { Result x, y; if constexpr (IsDecimal) { x = static_cast(a.value); } else { x = static_cast(a); } if constexpr (IsDecimal) { y = static_cast(b.value); } else { y = static_cast(b); } return BitShiftRightImpl::apply(x, y); } template static Result apply(A, B, UInt8 &) { throw Exception("Should not reach here"); } }; // clang-format off struct NameBitShiftRight { static constexpr auto name = "bitShiftRight"; }; // clang-format on template using BitShiftRightImpl_t = BitShiftRightImpl; using FunctionBitShiftRight = FunctionBinaryArithmetic; } // namespace void registerFunctionBitShiftRight(FunctionFactory & factory) { factory.registerFunction(); } } // namespace DB