comphead commented on code in PR #18569: URL: https://github.com/apache/datafusion/pull/18569#discussion_r2627883916
########## datafusion/spark/src/function/aggregate/try_sum.rs: ########## @@ -0,0 +1,650 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use arrow::array::{ArrayRef, ArrowNumericType, AsArray, BooleanArray, PrimitiveArray}; +use arrow::datatypes::{ + DECIMAL128_MAX_PRECISION, DataType, Decimal128Type, Field, FieldRef, Float64Type, + Int64Type, UInt64Type, +}; +use datafusion_common::{Result, ScalarValue, downcast_value, exec_err, not_impl_err}; +use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; +use datafusion_expr::utils::format_state_name; +use datafusion_expr::{Accumulator, AggregateUDFImpl, Signature, Volatility}; +use std::any::Any; +use std::fmt::{Debug, Formatter}; +use std::mem::size_of_val; + +#[derive(PartialEq, Eq, Hash)] +pub struct SparkTrySum { + signature: Signature, +} + +impl Default for SparkTrySum { + fn default() -> Self { + Self::new() + } +} + +impl SparkTrySum { + pub fn new() -> Self { + Self { + signature: Signature::user_defined(Volatility::Immutable), + } + } +} + +impl Debug for SparkTrySum { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SparkTrySum") + .field("signature", &self.signature) + .finish() + } +} + +/// Accumulator for try_sum that detects overflow +struct TrySumAccumulator<T: ArrowNumericType> { + sum: Option<T::Native>, + data_type: DataType, + failed: bool, + // Only used if data_type is Decimal128(p, s) + dec_precision: Option<u8>, +} + +impl<T: ArrowNumericType> Debug for TrySumAccumulator<T> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "TrySumAccumulator({})", self.data_type) + } +} + +impl<T: ArrowNumericType> TrySumAccumulator<T> { + fn new(data_type: DataType) -> Self { + let dec_precision = match &data_type { + DataType::Decimal128(p, _) => Some(*p), + _ => None, + }; + Self { + sum: None, + data_type, + failed: false, + dec_precision, + } + } +} + +impl<T: ArrowNumericType> Accumulator for TrySumAccumulator<T> { + fn state(&mut self) -> Result<Vec<ScalarValue>> { + Ok(vec![ + self.evaluate()?, + ScalarValue::Boolean(Some(self.failed)), + ]) + } + + fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> { + update_batch_internal(self, values) + } + + fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> { + // Check if any partition has failed + let failed_arr = downcast_value!(states[1], BooleanArray); Review Comment: maybe try more idiomatic ``` if downcast_value!(states[1], BooleanArray).iter().flatten().any(|&f| f) { self.failed = true; return Ok(()); } ``` ? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
