Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push down ROUND(x) on decimal types #2492

Merged
merged 16 commits into from
Aug 6, 2021
48 changes: 48 additions & 0 deletions dbms/src/Common/toSafeUnsigned.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <common/types.h>

namespace DB
{

// toSafeUnsigned evaluates absolute value of argument `value` and cast it to unsigned type of `To`.
// it guarantees that no undefined behavior will occur and exact result can be represented by unsigned `To`.
template <typename To, typename From>
constexpr make_unsigned_t<To> toSafeUnsigned(const From & value)
{
static_assert(is_integer_v<From>, "type From must be integral");
static_assert(is_integer_v<To>, "type To must be integral");

using ReturnType = make_unsigned_t<To>;

static_assert(actual_size_v<ReturnType> >= actual_size_v<From>, "type unsigned To can't hold all values of type From");

if constexpr (is_signed_v<From>)
{
if constexpr (is_boost_number_v<ReturnType>)
{
// assert that negation of std::numeric_limits<From>::min() will not result in overflow.
// TODO: find credible source that describes numeric limits of boost multiprecision *checked* integers.
static_assert(-std::numeric_limits<From>::max() == std::numeric_limits<From>::min());
return static_cast<ReturnType>(boost::multiprecision::abs(value));
}
else
{
if (value < 0)
{
// both signed to unsigned conversion [1] and negation of unsigned integers [2] are well defined in C++.
//
// see:
// [1]: https://en.cppreference.com/w/c/language/conversion#Integer_conversions
// [2]: https://en.cppreference.com/w/cpp/language/operator_arithmetic#Unary_arithmetic_operators
return -static_cast<ReturnType>(value);
}
else
return static_cast<ReturnType>(value);
}
}
else
return static_cast<ReturnType>(value);
}

} // namespace DB
1 change: 1 addition & 0 deletions dbms/src/DataTypes/DataTypesNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DataTypeNumber final : public DataTypeNumberBase<T>
bool canBeUsedInBooleanContext() const override { return true; }
bool isNumber() const override { return true; }
bool isInteger() const override { return std::is_integral_v<T>; }
bool isFloatingPoint() const override { return std::is_floating_point_v<T>; }
bool canBeInsideNullable() const override { return true; }

public:
Expand Down
5 changes: 4 additions & 1 deletion dbms/src/DataTypes/IDataType.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ class IDataType : private boost::noncopyable
virtual bool isInteger() const { return false; };
virtual bool isUnsignedInteger() const { return false; };

/** Floating point values. Not Nullable. Not Enums. Not Date/DateTime.
*/
virtual bool isFloatingPoint() const { return false; }

/** Date, DateTime, MyDate, MyDateTime. Not Nullable.
*/
virtual bool isDateOrDateTime() const { return false; };
Expand Down Expand Up @@ -475,4 +479,3 @@ class IDataType : private boost::noncopyable


}

24 changes: 23 additions & 1 deletion dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,27 @@ static String buildBitwiseFunction(DAGExpressionAnalyzer * analyzer, const tipb:
return analyzer->applyFunction(func_name, argument_names, actions, nullptr);
}

static String buildRoundFunction(DAGExpressionAnalyzer * analyzer, const tipb::Expr & expr, ExpressionActionsPtr & actions)
{
// ROUND(x) -> ROUND(x, 0)

if (expr.children_size() != 1)
throw TiFlashException("Invalid arguments of ROUND function", Errors::Coprocessor::BadRequest);


auto input_arg_name = analyzer->getActions(expr.children(0), actions);

auto const_zero = tipb::Expr();
constructInt64LiteralTiExpr(const_zero, 0);
auto const_zero_arg_name = analyzer->getActions(const_zero, actions);

Names argument_names;
argument_names.push_back(std::move(input_arg_name));
argument_names.push_back(std::move(const_zero_arg_name));

return analyzer->applyFunction("tidbRoundWithFrac", argument_names, actions, getCollatorFromExpr(expr));
}

static String buildFunction(DAGExpressionAnalyzer * analyzer, const tipb::Expr & expr, ExpressionActionsPtr & actions)
{
const String & func_name = getFunctionName(expr);
Expand All @@ -354,7 +375,8 @@ static std::unordered_map<String, std::function<String(DAGExpressionAnalyzer *,
{"multiIf", buildMultiIfFunction}, {"tidb_cast", buildCastFunction}, {"and", buildLogicalFunction}, {"or", buildLogicalFunction},
{"xor", buildLogicalFunction}, {"not", buildLogicalFunction}, {"bitAnd", buildBitwiseFunction}, {"bitOr", buildBitwiseFunction},
{"bitXor", buildBitwiseFunction}, {"bitNot", buildBitwiseFunction}, {"leftUTF8", buildLeftUTF8Function},
{"date_add", buildDateAddOrSubFunction<DateAdd>}, {"date_sub", buildDateAddOrSubFunction<DateSub>}});
{"date_add", buildDateAddOrSubFunction<DateAdd>}, {"date_sub", buildDateAddOrSubFunction<DateSub>},
{"tidbRound", buildRoundFunction}});

DAGExpressionAnalyzer::DAGExpressionAnalyzer(std::vector<NameAndTypePair> && source_columns_, const Context & context_)
: source_columns(std::move(source_columns_)), context(context_), after_agg(false), implicit_cast_count(0)
Expand Down
10 changes: 6 additions & 4 deletions dbms/src/Flash/Coprocessor/DAGUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,12 @@ std::unordered_map<tipb::ScalarFuncSig, String> scalar_func_map({
{tipb::ScalarFuncSig::FloorIntToDec, "floor"}, {tipb::ScalarFuncSig::FloorIntToInt, "floor"},
{tipb::ScalarFuncSig::FloorDecToInt, "floorDecimalToInt"}, {tipb::ScalarFuncSig::FloorDecToDec, "floor"}, {tipb::ScalarFuncSig::FloorReal, "floor"},

{tipb::ScalarFuncSig::RoundReal, "round"}, {tipb::ScalarFuncSig::RoundInt, "round"}, {tipb::ScalarFuncSig::RoundDec, "round"},
//{tipb::ScalarFuncSig::RoundWithFracReal, "cast"},
//{tipb::ScalarFuncSig::RoundWithFracInt, "cast"},
//{tipb::ScalarFuncSig::RoundWithFracDec, "cast"},
{tipb::ScalarFuncSig::RoundReal, "tidbRound"},
{tipb::ScalarFuncSig::RoundInt, "tidbRound"},
{tipb::ScalarFuncSig::RoundDec, "tidbRound"},
Comment on lines +643 to +645
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original implementation of round from Clickhouse employed some SIMD computations, but tidbRoundWithFrac currently doesn't make use of them at all.

Maybe reset RoundReal and RoundInt to "round" in this PR for better performance?

// {tipb::ScalarFuncSig::RoundWithFracReal, "tidbRoundWithFrac"},
// {tipb::ScalarFuncSig::RoundWithFracInt, "tidbRoundWithFrac"},
// {tipb::ScalarFuncSig::RoundWithFracDec, "tidbRoundWithFrac"},

{tipb::ScalarFuncSig::Log1Arg, "log"},
{tipb::ScalarFuncSig::Log2Args, "log2args"},
Expand Down
38 changes: 3 additions & 35 deletions dbms/src/Functions/FunctionsArithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Columns/ColumnVector.h>
#include <Columns/ColumnsNumber.h>
#include <Common/FieldVisitors.h>
#include <Common/toSafeUnsigned.h>
#include <Common/typeid_cast.h>
#include <Core/AccurateComparison.h>
#include <DataTypes/DataTypeDate.h>
Expand Down Expand Up @@ -525,39 +526,6 @@ struct ModuloImpl<A, B, false>
{
using ResultType = typename NumberTraits::ResultOfModulo<A, B>::Type;

template <typename To, typename From>
static make_unsigned_t<To> to_unsigned(const From & value)
{
using ReturnType = make_unsigned_t<To>;

if constexpr (is_signed_v<From>)
{
if constexpr (is_boost_number_v<ReturnType>)
{
// assert that negation of std::numeric_limits<From>::min() will not result in overflow.
// TODO: find credible source that describes numeric limits of boost multiprecision *checked* integers.
static_assert(-std::numeric_limits<From>::max() == std::numeric_limits<From>::min());
return static_cast<ReturnType>(boost::multiprecision::abs(value));
}
else
{
if (value < 0)
{
// both signed to unsigned conversion [1] and negation of unsigned integers [2] are well defined in C++.
//
// see:
// [1]: https://en.cppreference.com/w/c/language/conversion#Integer_conversions
// [2]: https://en.cppreference.com/w/cpp/language/operator_arithmetic#Unary_arithmetic_operators
return -static_cast<ReturnType>(value);
}
else
return static_cast<ReturnType>(value);
}
}
else
return static_cast<ReturnType>(value);
}

template <typename Result = ResultType>
static inline Result apply(A a, B b)
{
Expand All @@ -581,8 +549,8 @@ struct ModuloImpl<A, B, false>
// convert to unsigned before computing.
// we have to prevent wrong result like UInt64(5) = UInt64(5) % Int64(-3).
// in MySQL, UInt64(5) % Int64(-3) evaluates to UInt64(2).
auto x = to_unsigned<Result>(a);
auto y = to_unsigned<Result>(b);
auto x = toSafeUnsigned<Result>(a);
auto y = toSafeUnsigned<Result>(b);

auto result = static_cast<Result>(x % y);

Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Functions/FunctionsRound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void registerFunctionsRound(FunctionFactory & factory)
/// Compatibility aliases.
factory.registerFunction<FunctionCeil>("ceiling", FunctionFactory::CaseInsensitive);
factory.registerFunction<FunctionTrunc>("truncate", FunctionFactory::CaseInsensitive);

factory.registerFunction<FunctionTiDBRoundWithFrac>();
}

}
Loading