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) #2611

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -343,6 +343,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 @@ -430,4 +434,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 @@ -329,6 +329,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 @@ -347,7 +368,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 @@ -635,10 +635,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"},
// {tipb::ScalarFuncSig::RoundWithFracReal, "tidbRoundWithFrac"},
// {tipb::ScalarFuncSig::RoundWithFracInt, "tidbRoundWithFrac"},
// {tipb::ScalarFuncSig::RoundWithFracDec, "tidbRoundWithFrac"},

{tipb::ScalarFuncSig::Log1Arg, "log"},
//{tipb::ScalarFuncSig::Log2Args, "cast"},
Expand Down
41 changes: 38 additions & 3 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 @@ -528,9 +529,43 @@ struct ModuloImpl<A,B,false>
template <typename Result = ResultType>
static inline Result apply(A a, B b)
{
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
return static_cast<Result>( typename NumberTraits::ToInteger<A>::Type(a)
% typename NumberTraits::ToInteger<B>::Type(b));
if constexpr (std::is_floating_point_v<Result>)
{
auto x = static_cast<Result>(a);
auto y = static_cast<Result>(b);

// assert no infinite or NaN values.
assert(std::isfinite(x) && std::isfinite(y));

// C++ does not allow operator% between floating point
// values, so we call into std::fmod.
return std::fmod(x, y);
}
else // both A and B are integrals.
{
// decimals are expected to be converted to integers or floating point values before computations.
static_assert(is_integer_v<Result>);

// 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 = toSafeUnsigned<Result>(a);
auto y = toSafeUnsigned<Result>(b);

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

// in MySQL, the sign of a % b is the same as that of a.
// e.g. 5 % -3 = 2, -5 % 3 = -2.
if constexpr (is_signed_v<Result>)
{
if (a < 0)
return -result;
else
return result;
}
else
return result;
}
}
template <typename Result = ResultType>
static inline Result apply(A , B , UInt8 &)
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