taiyang-li commented on code in PR #8351:
URL: https://github.com/apache/incubator-gluten/pull/8351#discussion_r1905072886
##########
cpp-ch/local-engine/Functions/SparkFunctionCheckDecimalOverflow.cpp:
##########
@@ -192,20 +190,62 @@ class FunctionCheckDecimalOverflow : public IFunction
result_column = std::move(col_to);
}
- template <is_decimal FromFieldType, typename ToDataType>
+ template <typename FromDataType, typename ToDataType>
requires(IsDataTypeDecimal<ToDataType>)
static bool convertToDecimalImpl(
- const FromFieldType & decimal, UInt32 precision_to, UInt32 scale_from,
UInt32 scale_to, typename ToDataType::FieldType & result)
+ const FromDataType::FieldType & value, UInt32 precision_to, UInt32
scale_from, UInt32 scale_to, typename ToDataType::FieldType & result)
{
+ using FromFieldType = typename FromDataType::FieldType;
if constexpr (std::is_same_v<FromFieldType, Decimal32>)
- return convertDecimalsImpl<DataTypeDecimal<Decimal32>,
ToDataType>(decimal, precision_to, scale_from, scale_to, result);
-
+ return convertDecimalsImpl<DataTypeDecimal<Decimal32>,
ToDataType>(value, precision_to, scale_from, scale_to, result);
else if constexpr (std::is_same_v<FromFieldType, Decimal64>)
- return convertDecimalsImpl<DataTypeDecimal<Decimal64>,
ToDataType>(decimal, precision_to, scale_from, scale_to, result);
+ return convertDecimalsImpl<DataTypeDecimal<Decimal64>,
ToDataType>(value, precision_to, scale_from, scale_to, result);
else if constexpr (std::is_same_v<FromFieldType, Decimal128>)
- return convertDecimalsImpl<DataTypeDecimal<Decimal128>,
ToDataType>(decimal, precision_to, scale_from, scale_to, result);
+ return convertDecimalsImpl<DataTypeDecimal<Decimal128>,
ToDataType>(value, precision_to, scale_from, scale_to, result);
+ else if constexpr (std::is_same_v<FromFieldType, Decimal256>)
+ return convertDecimalsImpl<DataTypeDecimal<Decimal256>,
ToDataType>(value, precision_to, scale_from, scale_to, result);
+ else if constexpr (IsDataTypeNumber<FromDataType>)
+ return convertNumberToDecimalImpl<DataTypeNumber<FromFieldType>,
ToDataType>(value, precision_to, scale_to, result);
+ else
+ throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Convert from {} type
to decimal type is not implemented.", typeid(value).name());
+ }
+
+ template <typename FromDataType, typename ToDataType>
+ requires(IsDataTypeNumber<FromDataType> && IsDataTypeDecimal<ToDataType>)
+ static bool convertNumberToDecimalImpl(
+ const typename FromDataType::FieldType & value,
+ UInt32 precision,
+ UInt32 scale,
+ typename ToDataType::FieldType & result)
+ {
+ using FromFieldType = typename FromDataType::FieldType;
+ double int_part = 0;
+ if constexpr (std::is_same_v<FromFieldType, Float32> ||
std::is_same_v<FromFieldType, BFloat16>)
+ int_part = static_cast<int>(value);
+ else if constexpr (std::is_same_v<FromFieldType, Float64>)
+ int_part = static_cast<long>(value);
+ else
+ int_part = value;
+
+ int int_part_digits = int_part == 0 ? 1 :
+ int_part > 0 ?
static_cast<int>(std::log10(int_part)) + 1
+ :
static_cast<int>(std::log10(std::fabs(int_part))) + 1;
+ /// If the integer part's digits of the number is greater than
(precision - scale), e.g. cast(55 as decimal(2, 1)),
+ /// then we should return NULL or throw exceptions.
+ if (int_part_digits > precision - scale)
Review Comment:
if and else could be merged `return int_part_digits > precision - scale &&
tryConvertToDecimal<FromDataType, ToDataType>(value, scale, result);`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]