2018-09-02 01:12:32 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <common/arithmeticOverflow.h>
|
|
|
|
#include <Core/Block.h>
|
|
|
|
#include <Core/AccurateComparison.h>
|
2018-09-03 10:46:58 +00:00
|
|
|
#include <Core/callOnTypeIndex.h>
|
2019-12-03 10:06:34 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2018-09-02 01:12:32 +00:00
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Functions/FunctionHelpers.h> /// TODO Core should not depend on Functions
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2018-09-02 01:12:32 +00:00
|
|
|
extern const int DECIMAL_OVERFLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2018-09-07 14:37:26 +00:00
|
|
|
inline bool allowDecimalComparison(const DataTypePtr & left_type, const DataTypePtr & right_type)
|
2018-09-02 01:12:32 +00:00
|
|
|
{
|
2019-10-07 15:19:18 +00:00
|
|
|
if (isColumnedAsDecimal(left_type))
|
2018-09-02 01:12:32 +00:00
|
|
|
{
|
2019-10-07 15:19:18 +00:00
|
|
|
if (isColumnedAsDecimal(right_type) || isNotDecimalButComparableToDecimal(right_type))
|
2018-09-02 01:12:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-10-07 15:19:18 +00:00
|
|
|
else if (isNotDecimalButComparableToDecimal(left_type) && isColumnedAsDecimal(right_type))
|
2018-09-02 01:12:32 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t > struct ConstructDecInt { using Type = Int32; };
|
|
|
|
template <> struct ConstructDecInt<8> { using Type = Int64; };
|
|
|
|
template <> struct ConstructDecInt<16> { using Type = Int128; };
|
2020-09-01 09:54:50 +00:00
|
|
|
template <> struct ConstructDecInt<48> { using Type = Int256; };
|
2018-09-02 01:12:32 +00:00
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
struct DecCompareInt
|
|
|
|
{
|
|
|
|
using Type = typename ConstructDecInt<(!IsDecimalNumber<U> || sizeof(T) > sizeof(U)) ? sizeof(T) : sizeof(U)>::Type;
|
|
|
|
using TypeA = Type;
|
|
|
|
using TypeB = Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
template <typename A, typename B, template <typename, typename> typename Operation, bool _check_overflow = true,
|
|
|
|
bool _actual = IsDecimalNumber<A> || IsDecimalNumber<B>>
|
|
|
|
class DecimalComparison
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using CompareInt = typename DecCompareInt<A, B>::Type;
|
|
|
|
using Op = Operation<CompareInt, CompareInt>;
|
|
|
|
using ColVecA = std::conditional_t<IsDecimalNumber<A>, ColumnDecimal<A>, ColumnVector<A>>;
|
|
|
|
using ColVecB = std::conditional_t<IsDecimalNumber<B>, ColumnDecimal<B>, ColumnVector<B>>;
|
2020-11-13 15:13:15 +00:00
|
|
|
|
2018-09-02 01:12:32 +00:00
|
|
|
using ArrayA = typename ColVecA::Container;
|
|
|
|
using ArrayB = typename ColVecB::Container;
|
|
|
|
|
2020-10-17 14:23:37 +00:00
|
|
|
static ColumnPtr apply(const ColumnWithTypeAndName & col_left, const ColumnWithTypeAndName & col_right)
|
2018-09-02 01:12:32 +00:00
|
|
|
{
|
|
|
|
if constexpr (_actual)
|
|
|
|
{
|
|
|
|
ColumnPtr c_res;
|
|
|
|
Shift shift = getScales<A, B>(col_left.type, col_right.type);
|
|
|
|
|
2020-10-17 14:23:37 +00:00
|
|
|
return applyWithScale(col_left.column, col_right.column, shift);
|
2018-09-02 01:12:32 +00:00
|
|
|
}
|
2020-10-17 14:23:37 +00:00
|
|
|
else
|
|
|
|
return nullptr;
|
2018-09-02 01:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool compare(A a, B b, UInt32 scale_a, UInt32 scale_b)
|
|
|
|
{
|
2020-08-19 11:52:17 +00:00
|
|
|
static const UInt32 max_scale = DecimalUtils::maxPrecision<Decimal256>();
|
2018-09-02 01:12:32 +00:00
|
|
|
if (scale_a > max_scale || scale_b > max_scale)
|
|
|
|
throw Exception("Bad scale of decimal field", ErrorCodes::DECIMAL_OVERFLOW);
|
|
|
|
|
|
|
|
Shift shift;
|
|
|
|
if (scale_a < scale_b)
|
2020-08-19 11:52:17 +00:00
|
|
|
shift.a = static_cast<CompareInt>(DecimalUtils::scaleMultiplier<B>(scale_b - scale_a));
|
2018-09-02 01:12:32 +00:00
|
|
|
if (scale_a > scale_b)
|
2020-08-19 11:52:17 +00:00
|
|
|
shift.b = static_cast<CompareInt>(DecimalUtils::scaleMultiplier<A>(scale_a - scale_b));
|
2018-09-02 01:12:32 +00:00
|
|
|
|
|
|
|
return applyWithScale(a, b, shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Shift
|
|
|
|
{
|
|
|
|
CompareInt a = 1;
|
|
|
|
CompareInt b = 1;
|
|
|
|
|
|
|
|
bool none() const { return a == 1 && b == 1; }
|
|
|
|
bool left() const { return a != 1; }
|
|
|
|
bool right() const { return b != 1; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
static auto applyWithScale(T a, U b, const Shift & shift)
|
|
|
|
{
|
|
|
|
if (shift.left())
|
|
|
|
return apply<true, false>(a, b, shift.a);
|
|
|
|
else if (shift.right())
|
|
|
|
return apply<false, true>(a, b, shift.b);
|
|
|
|
return apply<false, false>(a, b, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
static std::enable_if_t<IsDecimalNumber<T> && IsDecimalNumber<U>, Shift>
|
|
|
|
getScales(const DataTypePtr & left_type, const DataTypePtr & right_type)
|
|
|
|
{
|
2020-11-12 20:23:26 +00:00
|
|
|
const DataTypeDecimalBase<T> * decimal0 = checkDecimalBase<T>(*left_type);
|
|
|
|
const DataTypeDecimalBase<U> * decimal1 = checkDecimalBase<U>(*right_type);
|
2018-09-02 01:12:32 +00:00
|
|
|
|
|
|
|
Shift shift;
|
|
|
|
if (decimal0 && decimal1)
|
|
|
|
{
|
2020-11-12 20:23:26 +00:00
|
|
|
auto result_type = DecimalUtils::binaryOpResult<false, false>(*decimal0, *decimal1);
|
|
|
|
shift.a = static_cast<CompareInt>(result_type.scaleFactorFor(decimal0->getTrait(), false).value);
|
|
|
|
shift.b = static_cast<CompareInt>(result_type.scaleFactorFor(decimal1->getTrait(), false).value);
|
2018-09-02 01:12:32 +00:00
|
|
|
}
|
|
|
|
else if (decimal0)
|
2020-08-19 11:52:17 +00:00
|
|
|
shift.b = static_cast<CompareInt>(decimal0->getScaleMultiplier().value);
|
2018-09-02 01:12:32 +00:00
|
|
|
else if (decimal1)
|
2020-08-19 11:52:17 +00:00
|
|
|
shift.a = static_cast<CompareInt>(decimal1->getScaleMultiplier().value);
|
2018-09-02 01:12:32 +00:00
|
|
|
|
|
|
|
return shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
static std::enable_if_t<IsDecimalNumber<T> && !IsDecimalNumber<U>, Shift>
|
|
|
|
getScales(const DataTypePtr & left_type, const DataTypePtr &)
|
|
|
|
{
|
|
|
|
Shift shift;
|
2020-11-12 20:23:26 +00:00
|
|
|
const DataTypeDecimalBase<T> * decimal0 = checkDecimalBase<T>(*left_type);
|
2018-09-02 01:12:32 +00:00
|
|
|
if (decimal0)
|
2020-08-19 11:52:17 +00:00
|
|
|
shift.b = static_cast<CompareInt>(decimal0->getScaleMultiplier().value);
|
2018-09-02 01:12:32 +00:00
|
|
|
return shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
static std::enable_if_t<!IsDecimalNumber<T> && IsDecimalNumber<U>, Shift>
|
|
|
|
getScales(const DataTypePtr &, const DataTypePtr & right_type)
|
|
|
|
{
|
|
|
|
Shift shift;
|
2020-11-12 20:23:26 +00:00
|
|
|
const DataTypeDecimalBase<U> * decimal1 = checkDecimalBase<U>(*right_type);
|
2018-09-02 01:12:32 +00:00
|
|
|
if (decimal1)
|
2020-08-19 11:52:17 +00:00
|
|
|
shift.a = static_cast<CompareInt>(decimal1->getScaleMultiplier().value);
|
2018-09-02 01:12:32 +00:00
|
|
|
return shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool scale_left, bool scale_right>
|
|
|
|
static ColumnPtr apply(const ColumnPtr & c0, const ColumnPtr & c1, CompareInt scale)
|
|
|
|
{
|
|
|
|
auto c_res = ColumnUInt8::create();
|
|
|
|
|
|
|
|
if constexpr (_actual)
|
|
|
|
{
|
2019-06-27 19:28:52 +00:00
|
|
|
bool c0_is_const = isColumnConst(*c0);
|
|
|
|
bool c1_is_const = isColumnConst(*c1);
|
2018-09-02 01:12:32 +00:00
|
|
|
|
|
|
|
if (c0_is_const && c1_is_const)
|
|
|
|
{
|
|
|
|
const ColumnConst * c0_const = checkAndGetColumnConst<ColVecA>(c0.get());
|
|
|
|
const ColumnConst * c1_const = checkAndGetColumnConst<ColVecB>(c1.get());
|
|
|
|
|
|
|
|
A a = c0_const->template getValue<A>();
|
|
|
|
B b = c1_const->template getValue<B>();
|
|
|
|
UInt8 res = apply<scale_left, scale_right>(a, b, scale);
|
|
|
|
return DataTypeUInt8().createColumnConst(c0->size(), toField(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnUInt8::Container & vec_res = c_res->getData();
|
|
|
|
vec_res.resize(c0->size());
|
|
|
|
|
|
|
|
if (c0_is_const)
|
|
|
|
{
|
|
|
|
const ColumnConst * c0_const = checkAndGetColumnConst<ColVecA>(c0.get());
|
|
|
|
A a = c0_const->template getValue<A>();
|
|
|
|
if (const ColVecB * c1_vec = checkAndGetColumn<ColVecB>(c1.get()))
|
2020-03-23 02:12:31 +00:00
|
|
|
constantVector<scale_left, scale_right>(a, c1_vec->getData(), vec_res, scale);
|
2018-09-02 01:12:32 +00:00
|
|
|
else
|
|
|
|
throw Exception("Wrong column in Decimal comparison", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
else if (c1_is_const)
|
|
|
|
{
|
|
|
|
const ColumnConst * c1_const = checkAndGetColumnConst<ColVecB>(c1.get());
|
|
|
|
B b = c1_const->template getValue<B>();
|
|
|
|
if (const ColVecA * c0_vec = checkAndGetColumn<ColVecA>(c0.get()))
|
2020-03-23 02:12:31 +00:00
|
|
|
vectorConstant<scale_left, scale_right>(c0_vec->getData(), b, vec_res, scale);
|
2018-09-02 01:12:32 +00:00
|
|
|
else
|
|
|
|
throw Exception("Wrong column in Decimal comparison", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (const ColVecA * c0_vec = checkAndGetColumn<ColVecA>(c0.get()))
|
|
|
|
{
|
|
|
|
if (const ColVecB * c1_vec = checkAndGetColumn<ColVecB>(c1.get()))
|
2020-03-23 02:12:31 +00:00
|
|
|
vectorVector<scale_left, scale_right>(c0_vec->getData(), c1_vec->getData(), vec_res, scale);
|
2018-09-02 01:12:32 +00:00
|
|
|
else
|
|
|
|
throw Exception("Wrong column in Decimal comparison", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Wrong column in Decimal comparison", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool scale_left, bool scale_right>
|
|
|
|
static NO_INLINE UInt8 apply(A a, B b, CompareInt scale [[maybe_unused]])
|
|
|
|
{
|
2020-08-19 11:52:17 +00:00
|
|
|
CompareInt x;
|
2020-09-04 13:33:02 +00:00
|
|
|
if constexpr (IsDecimalNumber<A>)
|
2020-08-19 11:52:17 +00:00
|
|
|
x = a.value;
|
|
|
|
else
|
2020-09-04 13:33:02 +00:00
|
|
|
x = a;
|
2020-08-19 11:52:17 +00:00
|
|
|
|
|
|
|
CompareInt y;
|
2020-09-04 13:33:02 +00:00
|
|
|
if constexpr (IsDecimalNumber<B>)
|
2020-08-19 11:52:17 +00:00
|
|
|
y = b.value;
|
|
|
|
else
|
2020-09-04 13:33:02 +00:00
|
|
|
y = b;
|
2018-09-02 01:12:32 +00:00
|
|
|
|
|
|
|
if constexpr (_check_overflow)
|
|
|
|
{
|
|
|
|
bool overflow = false;
|
|
|
|
|
|
|
|
if constexpr (sizeof(A) > sizeof(CompareInt))
|
2020-09-04 13:33:02 +00:00
|
|
|
overflow |= (bigint_cast<A>(x) != a);
|
2018-09-02 01:12:32 +00:00
|
|
|
if constexpr (sizeof(B) > sizeof(CompareInt))
|
2020-09-04 13:33:02 +00:00
|
|
|
overflow |= (bigint_cast<B>(y) != b);
|
2019-11-02 05:55:06 +00:00
|
|
|
if constexpr (is_unsigned_v<A>)
|
2018-09-02 01:12:32 +00:00
|
|
|
overflow |= (x < 0);
|
2019-11-02 05:55:06 +00:00
|
|
|
if constexpr (is_unsigned_v<B>)
|
2018-09-02 01:12:32 +00:00
|
|
|
overflow |= (y < 0);
|
|
|
|
|
|
|
|
if constexpr (scale_left)
|
|
|
|
overflow |= common::mulOverflow(x, scale, x);
|
|
|
|
if constexpr (scale_right)
|
|
|
|
overflow |= common::mulOverflow(y, scale, y);
|
|
|
|
|
|
|
|
if (overflow)
|
|
|
|
throw Exception("Can't compare", ErrorCodes::DECIMAL_OVERFLOW);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if constexpr (scale_left)
|
|
|
|
x *= scale;
|
|
|
|
if constexpr (scale_right)
|
|
|
|
y *= scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Op::apply(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool scale_left, bool scale_right>
|
2020-03-23 02:12:31 +00:00
|
|
|
static void NO_INLINE vectorVector(const ArrayA & a, const ArrayB & b, PaddedPODArray<UInt8> & c,
|
2018-09-02 01:12:32 +00:00
|
|
|
CompareInt scale)
|
|
|
|
{
|
|
|
|
size_t size = a.size();
|
2018-09-02 03:00:04 +00:00
|
|
|
const A * a_pos = a.data();
|
|
|
|
const B * b_pos = b.data();
|
|
|
|
UInt8 * c_pos = c.data();
|
2018-09-02 01:12:32 +00:00
|
|
|
const A * a_end = a_pos + size;
|
|
|
|
|
|
|
|
while (a_pos < a_end)
|
|
|
|
{
|
|
|
|
*c_pos = apply<scale_left, scale_right>(*a_pos, *b_pos, scale);
|
|
|
|
++a_pos;
|
|
|
|
++b_pos;
|
|
|
|
++c_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool scale_left, bool scale_right>
|
2020-03-23 02:12:31 +00:00
|
|
|
static void NO_INLINE vectorConstant(const ArrayA & a, B b, PaddedPODArray<UInt8> & c, CompareInt scale)
|
2018-09-02 01:12:32 +00:00
|
|
|
{
|
|
|
|
size_t size = a.size();
|
2018-09-02 03:00:04 +00:00
|
|
|
const A * a_pos = a.data();
|
|
|
|
UInt8 * c_pos = c.data();
|
2018-09-02 01:12:32 +00:00
|
|
|
const A * a_end = a_pos + size;
|
|
|
|
|
|
|
|
while (a_pos < a_end)
|
|
|
|
{
|
|
|
|
*c_pos = apply<scale_left, scale_right>(*a_pos, b, scale);
|
|
|
|
++a_pos;
|
|
|
|
++c_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool scale_left, bool scale_right>
|
2020-03-23 02:12:31 +00:00
|
|
|
static void NO_INLINE constantVector(A a, const ArrayB & b, PaddedPODArray<UInt8> & c, CompareInt scale)
|
2018-09-02 01:12:32 +00:00
|
|
|
{
|
|
|
|
size_t size = b.size();
|
2018-09-02 03:00:04 +00:00
|
|
|
const B * b_pos = b.data();
|
|
|
|
UInt8 * c_pos = c.data();
|
2018-09-02 01:12:32 +00:00
|
|
|
const B * b_end = b_pos + size;
|
|
|
|
|
|
|
|
while (b_pos < b_end)
|
|
|
|
{
|
|
|
|
*c_pos = apply<scale_left, scale_right>(a, *b_pos, scale);
|
|
|
|
++b_pos;
|
|
|
|
++c_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|