From c5dde86cb0e1a95250f28876cc0ecf31532a5a90 Mon Sep 17 00:00:00 2001 From: Kazuyuki Tanimura Date: Mon, 19 Aug 2024 15:52:29 -0700 Subject: [PATCH 1/4] fix: Optimize CheckOverflow --- .../datafusion/expressions/checkoverflow.rs | 24 +++---------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/native/core/src/execution/datafusion/expressions/checkoverflow.rs b/native/core/src/execution/datafusion/expressions/checkoverflow.rs index 044b366e3..2f2608c47 100644 --- a/native/core/src/execution/datafusion/expressions/checkoverflow.rs +++ b/native/core/src/execution/datafusion/expressions/checkoverflow.rs @@ -110,28 +110,10 @@ impl PhysicalExpr for CheckOverflow { let decimal_array = as_primitive_array::(&array); 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::, _>>()? - .into_iter(); - unsafe { PrimitiveArray::::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::::from_trusted_len_iter(iter) } + &decimal_array.null_if_overflow_precision(*precision) }; let new_array = Decimal128Array::from(casted_array.to_data()) From 3eba4fb682d35360d1065fb44c7c321468ac66d9 Mon Sep 17 00:00:00 2001 From: Kazuyuki Tanimura Date: Mon, 19 Aug 2024 15:57:31 -0700 Subject: [PATCH 2/4] fix: Optimize CheckOverflow --- .../core/src/execution/datafusion/expressions/checkoverflow.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/native/core/src/execution/datafusion/expressions/checkoverflow.rs b/native/core/src/execution/datafusion/expressions/checkoverflow.rs index 2f2608c47..d0b372a2e 100644 --- a/native/core/src/execution/datafusion/expressions/checkoverflow.rs +++ b/native/core/src/execution/datafusion/expressions/checkoverflow.rs @@ -110,9 +110,11 @@ impl PhysicalExpr for CheckOverflow { let decimal_array = as_primitive_array::(&array); let casted_array = if self.fail_on_error { + // Returning error if overflow decimal_array.validate_decimal_precision(*precision)?; decimal_array } else { + // Overflowing gets null value &decimal_array.null_if_overflow_precision(*precision) }; From 59ba422797dacc54246c40e8a398cce1a09400a8 Mon Sep 17 00:00:00 2001 From: Kazuyuki Tanimura Date: Mon, 19 Aug 2024 16:29:32 -0700 Subject: [PATCH 3/4] fix: Optimize CheckOverflow --- .../core/src/execution/datafusion/expressions/checkoverflow.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/core/src/execution/datafusion/expressions/checkoverflow.rs b/native/core/src/execution/datafusion/expressions/checkoverflow.rs index d0b372a2e..c2f1e1053 100644 --- a/native/core/src/execution/datafusion/expressions/checkoverflow.rs +++ b/native/core/src/execution/datafusion/expressions/checkoverflow.rs @@ -118,7 +118,7 @@ impl PhysicalExpr for CheckOverflow { &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)?; From 494cf8dfbf75baa49f22ca16cdcf1c4652faf66e Mon Sep 17 00:00:00 2001 From: Kazuyuki Tanimura Date: Mon, 19 Aug 2024 17:02:38 -0700 Subject: [PATCH 4/4] fix: Optimize CheckOverflow --- .../core/src/execution/datafusion/expressions/checkoverflow.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/core/src/execution/datafusion/expressions/checkoverflow.rs b/native/core/src/execution/datafusion/expressions/checkoverflow.rs index c2f1e1053..e4f54a1b8 100644 --- a/native/core/src/execution/datafusion/expressions/checkoverflow.rs +++ b/native/core/src/execution/datafusion/expressions/checkoverflow.rs @@ -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, };