Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Optimize CheckOverflow #852

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading