Copilot commented on code in PR #51169:
URL: https://github.com/apache/arrow/pull/51169#discussion_r4090753494
##########
cpp/src/gandiva/gdv_function_stubs.cc:
##########
@@ -204,11 +206,51 @@ CRC_FUNCTION(utf8)
CRC_FUNCTION(binary)
int32_t gdv_fn_dec_from_string(int64_t context, const char* in, int32_t
in_length,
- int32_t* precision_from_str, int32_t*
scale_from_str,
- int64_t* dec_high_from_str, uint64_t*
dec_low_from_str) {
+ int32_t out_scale, int32_t* precision_from_str,
+ int32_t* scale_from_str, int64_t*
dec_high_from_str,
+ uint64_t* dec_low_from_str) {
arrow::Decimal128 dec;
- auto status = arrow::Decimal128::FromString(std::string(in, in_length), &dec,
- precision_from_str,
scale_from_str);
+ const std::string_view input(in, in_length);
+ auto status =
+ arrow::Decimal128::FromString(input, &dec, precision_from_str,
scale_from_str);
+ if (!status.ok() ||
+ static_cast<int64_t>(*scale_from_str) - out_scale >
arrow::Decimal128::kMaxScale) {
+ arrow::internal::DecimalComponents components;
+ if (arrow::internal::ParseDecimalComponents(input.data(), input.size(),
+ &components)) {
+ std::string digits(components.whole_digits);
+ digits.append(components.fractional_digits);
+ digits.erase(0, digits.find_first_not_of('0'));
+ const int64_t num_digits =
+ static_cast<int64_t>(digits.size()) + out_scale -
+ static_cast<int64_t>(components.fractional_digits.size()) +
components.exponent;
+ bool round_up = false;
+ if (num_digits < 0 || num_digits > arrow::Decimal128::kMaxPrecision) {
+ digits = "0";
Review Comment:
This fallback runs for every non-OK result from `Decimal128::FromString`,
not only raw-limb overflow. A validly parsed but out-of-range exponent is
therefore converted to zero: for example, `1e2147483647` is rejected by the
parser because its required positive scale exceeds `kMaxScale`, but `num_digits
> kMaxPrecision` sets `digits` to `"0"`, the retry succeeds, and the cast
returns zero. The same issue occurs for exponent/scale arithmetic overflow such
as `1e-2147483648`; distinguish recoverable rescaling cases from parser range
errors before retrying.
--
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]