Skip to content

Commit

Permalink
fix modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
loneylee committed Sep 12, 2024
1 parent f04be5b commit 7f51251
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,19 +383,40 @@ struct SparkDecimalBinaryOperation
const size_t & max_scale)
{
if constexpr (CalculateWith256)
return calculateImpl<Int256>(l, r, scale_left, scale_right, res, resultDataType, max_scale);
return calculateImpl<Int256>(
static_cast<Int256>(l),
static_cast<Int256>(r),
static_cast<Int256>(scale_left),
static_cast<Int256>(scale_right),
res,
resultDataType,
max_scale);
else if (is_division)
return calculateImpl<Int128>(l, r, scale_left, scale_right, res, resultDataType, max_scale);
return calculateImpl<Int128>(
static_cast<Int128>(l),
static_cast<Int128>(r),
static_cast<Int128>(scale_left),
static_cast<Int128>(scale_right),
res,
resultDataType,
max_scale);
else
return calculateImpl<NativeResultType>(l, r, scale_left, scale_right, res, resultDataType, max_scale);
return calculateImpl<NativeResultType>(
static_cast<NativeResultType>(l),
static_cast<NativeResultType>(r),
static_cast<NativeResultType>(scale_left),
static_cast<NativeResultType>(scale_right),
res,
resultDataType,
max_scale);
}

template <typename CalcType, typename LeftNativeType, typename RightNativeType, typename NativeResultType, typename ResultDataType>
template <typename CalcType, typename NativeResultType, typename ResultDataType>
static NO_SANITIZE_UNDEFINED bool calculateImpl(
LeftNativeType l,
RightNativeType r,
NativeResultType scale_left,
NativeResultType scale_right,
CalcType l,
CalcType r,
CalcType scale_left,
CalcType scale_right,
NativeResultType & res,
const ResultDataType & resultDataType,
const size_t & max_scale)
Expand All @@ -412,13 +433,19 @@ struct SparkDecimalBinaryOperation
auto scale_diff = max_scale - result_scale;
chassert(scale_diff >= 0);
if (scale_diff)
c_res = c_res / DecimalUtils::scaleMultiplier<NativeResultType>(scale_diff);
{
auto scaled_diff = DecimalUtils::scaleMultiplier<CalcType>(scale_diff);
DecimalDivideImpl::apply<CalcType>(c_res, scaled_diff, c_res);
}

auto max_value = intExp10OfSize<CalcType>(resultDataType.getPrecision());

// check overflow
if (c_res <= -max_value || c_res >= max_value)
return false;
if constexpr (std::is_same_v<CalcType, Int256> || is_division)
{
if (c_res <= -max_value || c_res >= max_value)
return false;
}

res = static_cast<NativeResultType>(c_res);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class FunctionParserModulo final : public FunctionParserBinaryArithmetic
return toFunctionNode(actions_dag, function_name, {left_arg, right_arg, type_node});
}

return toFunctionNode(actions_dag, "sparkDivide", {left_arg, right_arg});
return toFunctionNode(actions_dag, "modulo", {left_arg, right_arg});
}
};

Expand Down

0 comments on commit 7f51251

Please sign in to comment.