Skip to content

Commit

Permalink
feat: implement shiftLeft function push down (#5495)
Browse files Browse the repository at this point in the history
close #5099
  • Loading branch information
AnnieoftheStars committed Aug 4, 2022
1 parent 0bafed9 commit e17ef39
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 2 deletions.
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},
{"bitShiftLeft", DAGExpressionAnalyzerHelper::buildBitwiseFunction},
{"bitShiftRight", DAGExpressionAnalyzerHelper::buildBitwiseFunction},
{"leftUTF8", DAGExpressionAnalyzerHelper::buildLeftUTF8Function},
{"date_add", DAGExpressionAnalyzerHelper::buildDateAddOrSubFunction<DateAdd>},
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 @@ -331,7 +331,7 @@ const std::unordered_map<tipb::ScalarFuncSig, String> scalar_func_map({
{tipb::ScalarFuncSig::DecimalIsFalse, "isFalse"},
{tipb::ScalarFuncSig::DecimalIsFalseWithNull, "isFalseWithNull"},

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

//{tipb::ScalarFuncSig::BitCount, "cast"},
Expand Down
13 changes: 12 additions & 1 deletion dbms/src/Functions/bitShiftLeft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ struct BitShiftLeftImpl<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
Loading

0 comments on commit e17ef39

Please sign in to comment.