Skip to content

Commit

Permalink
feat: implement shiftRight function push down (pingcap#5156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Willendless authored and Lloyd-Pottiger committed Jul 19, 2022
1 parent 48558ba commit 25c2f82
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 3 deletions.
1 change: 1 addition & 0 deletions dbms/src/DataTypes/NumberTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ struct ResultOfAbs<Decimal<T>>
};

/** For bitwise operations, an integer is obtained with number of bits is equal to the maximum of the arguments.
* todo: note that MySQL handles only unsigned 64-bit integer argument and result values. We should refine the code.
*/
template <typename A, typename B>
struct ResultOfBit
Expand Down
1 change: 1 addition & 0 deletions dbms/src/Flash/Coprocessor/DAGExpressionAnalyzerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ DAGExpressionAnalyzerHelper::FunctionBuilderMap DAGExpressionAnalyzerHelper::fun
{"bitOr", DAGExpressionAnalyzerHelper::buildBitwiseFunction},
{"bitXor", DAGExpressionAnalyzerHelper::buildBitwiseFunction},
{"bitNot", DAGExpressionAnalyzerHelper::buildBitwiseFunction},
{"bitShiftRight", DAGExpressionAnalyzerHelper::buildBitwiseFunction},
{"leftUTF8", DAGExpressionAnalyzerHelper::buildLeftUTF8Function},
{"date_add", DAGExpressionAnalyzerHelper::buildDateAddOrSubFunction<DateAdd>},
{"date_sub", DAGExpressionAnalyzerHelper::buildDateAddOrSubFunction<DateSub>},
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Flash/Coprocessor/DAGUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ const std::unordered_map<tipb::ScalarFuncSig, String> scalar_func_map({
{tipb::ScalarFuncSig::DecimalIsFalseWithNull, "isFalseWithNull"},

//{tipb::ScalarFuncSig::LeftShift, "cast"},
//{tipb::ScalarFuncSig::RightShift, "cast"},
{tipb::ScalarFuncSig::RightShift, "bitShiftRight"},

//{tipb::ScalarFuncSig::BitCount, "cast"},
//{tipb::ScalarFuncSig::GetParamString, "cast"},
Expand Down
18 changes: 16 additions & 2 deletions dbms/src/Functions/bitShiftRight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// limitations under the License.

#include <Functions/FunctionBinaryArithmetic.h>
#include <common/types.h>

#include <limits>

namespace DB
{
Expand All @@ -29,7 +32,18 @@ struct BitShiftRightImpl<A, B, false>
template <typename Result = ResultType>
static Result apply(A a, B b)
{
return static_cast<Result>(a) >> static_cast<Result>(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<Result>(b) >= std::numeric_limits<decltype(static_cast<Result>(a))>::digits)
{
return static_cast<Result>(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<Result>(a) >> static_cast<Result>(b); // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult)
}
template <typename Result = ResultType>
static Result apply(A, B, UInt8 &)
Expand Down Expand Up @@ -87,4 +101,4 @@ void registerFunctionBitShiftRight(FunctionFactory & factory)
factory.registerFunction<FunctionBitShiftRight>();
}

} // namespace DB
} // namespace DB
Loading

0 comments on commit 25c2f82

Please sign in to comment.