Skip to content

Commit

Permalink
fix: Optimize CheckOverflow (apache#852)
Browse files Browse the repository at this point in the history
## Which issue does this PR close?

## Rationale for this change

This PR improves CheckOverflow performance

## What changes are included in this PR?

This PR directly calls arrow validation methods

## How are these changes tested?

Existing tests

(cherry picked from commit 829656c)
  • Loading branch information
kazuyukitanimura authored and huaxingao committed Aug 22, 2024
1 parent 0b78a1a commit 37c1c92
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions native/core/src/execution/datafusion/expressions/checkoverflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{
};

use arrow::{
array::{as_primitive_array, Array, ArrayRef, Decimal128Array, PrimitiveArray},
array::{as_primitive_array, Array, ArrayRef, Decimal128Array},
datatypes::{Decimal128Type, DecimalType},
record_batch::RecordBatch,
};
Expand Down Expand Up @@ -111,30 +111,14 @@ impl PhysicalExpr for CheckOverflow {

let casted_array = if self.fail_on_error {
// Returning error if overflow
let iter = decimal_array
.iter()
.map(|v| {
v.map(|v| {
Decimal128Type::validate_decimal_precision(v, *precision).map(|_| v)
})
.map_or(Ok(None), |r| r.map(Some))
})
.collect::<Result<Vec<_>, _>>()?
.into_iter();
unsafe { PrimitiveArray::<Decimal128Type>::from_trusted_len_iter(iter) }
decimal_array.validate_decimal_precision(*precision)?;
decimal_array
} else {
// Overflowing gets null value
let iter = decimal_array.iter().map(|v| {
v.and_then(|v| {
Decimal128Type::validate_decimal_precision(v, *precision)
.map(|_| v)
.ok()
})
});
unsafe { PrimitiveArray::<Decimal128Type>::from_trusted_len_iter(iter) }
&decimal_array.null_if_overflow_precision(*precision)
};

let new_array = Decimal128Array::from(casted_array.to_data())
let new_array = Decimal128Array::from(casted_array.into_data())
.with_precision_and_scale(*precision, *scale)
.map(|a| Arc::new(a) as ArrayRef)?;

Expand Down

0 comments on commit 37c1c92

Please sign in to comment.