fix int field to decimal conversion

This commit is contained in:
Alexander Tokmakov 2021-02-09 13:21:54 +03:00
parent ac477d9850
commit 4859657c42
3 changed files with 20 additions and 5 deletions

View File

@ -120,14 +120,17 @@ public:
return DecimalUtils::getFractionalPart(x, scale);
}
T maxWholeValue() const { return getScaleMultiplier(maxPrecision() - scale) - T(1); }
T maxWholeValue() const { return getScaleMultiplier(precision - scale) - T(1); }
bool canStoreWhole(T x) const
template<typename U>
bool canStoreWhole(U x) const
{
static_assert(std::is_signed_v<typename T::NativeType>);
T max = maxWholeValue();
if (x > max || x < -max)
return false;
return true;
if constexpr (std::is_signed_v<U>)
return -max <= x && x <= max;
else
return x <= static_cast<std::make_unsigned_t<typename T::NativeType>>(max.value);
}
/// @returns multiplier for U to become T with correct scale

View File

@ -0,0 +1,2 @@
9.00000000
10.00000000

View File

@ -0,0 +1,10 @@
select d from values('d Decimal(8, 8)', 0, 1) where d not in (-1, 0); -- { serverError 69 }
select d from values('d Decimal(8, 8)', 0, 2) where d not in (1, 0); -- { serverError 69 }
select d from values('d Decimal(9, 8)', 0, 3) where d not in (-9223372036854775808, 0); -- { serverError 69 }
select d from values('d Decimal(9, 8)', 0, 4) where d not in (18446744073709551615, 0); -- { serverError 69 }
select d from values('d Decimal(18, 8)', 0, 5) where d not in (-9223372036854775808, 0); -- { serverError 69 }
select d from values('d Decimal(18, 8)', 0, 6) where d not in (18446744073709551615, 0); -- { serverError 69 }
select d from values('d Decimal(26, 8)', 0, 7) where d not in (-9223372036854775808, 0); -- { serverError 69 }
select d from values('d Decimal(27, 8)', 0, 8) where d not in (18446744073709551615, 0); -- { serverError 69 }
select d from values('d Decimal(27, 8)', 0, 9) where d not in (-9223372036854775808, 0);
select d from values('d Decimal(28, 8)', 0, 10) where d not in (18446744073709551615, 0);