Jefffrey commented on code in PR #17093: URL: https://github.com/apache/datafusion/pull/17093#discussion_r2364983787
########## datafusion/spark/src/function/hash/utils.rs: ########## @@ -0,0 +1,614 @@ +// 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::{ + Array, ArrayAccessor, ArrayIter, ArrayRef, ArrowPrimitiveType, BinaryViewArray, + BooleanArray, Date32Array, Date64Array, Decimal128Array, Decimal256Array, + DictionaryArray, DurationMicrosecondArray, DurationMillisecondArray, + DurationNanosecondArray, DurationSecondArray, FixedSizeBinaryArray, Float32Array, + Float64Array, GenericBinaryArray, Int16Array, Int32Array, Int64Array, Int8Array, + IntervalDayTimeArray, IntervalMonthDayNanoArray, IntervalYearMonthArray, + LargeListArray, LargeListViewArray, LargeStringArray, ListArray, ListViewArray, + MapArray, PrimitiveArray, StringArray, StringViewArray, StructArray, + Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray, + Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray, + TimestampNanosecondArray, TimestampSecondArray, UInt16Array, UInt32Array, + UInt64Array, UInt8Array, UnionArray, +}; +use arrow::datatypes::{ + i256, ArrowDictionaryKeyType, DataType, Int16Type, Int32Type, Int64Type, Int8Type, + IntervalDayTime, IntervalMonthDayNano, IntervalUnit, TimeUnit, UInt16Type, + UInt32Type, UInt64Type, UInt8Type, +}; +use bigdecimal::num_traits::{Float, ToBytes}; +use datafusion_common::{exec_err, DataFusionError, Result}; +use std::sync::Arc; + +fn hash_impl<T, H: Copy>( + arr: ArrayIter<impl ArrayAccessor<Item = T>>, + seed: &mut [H], + func: impl Fn(H, T) -> H, +) -> Result<()> { + let len = arr.len(); + if len != seed.len() { + return exec_err!("Array length mismatch: {} != {}", len, seed.len()); + } + for (hash, elem) in seed.iter_mut().zip(arr) { + if let Some(elem) = elem { + *hash = func(*hash, elem); + } + } + Ok(()) +} + +fn try_hash_impl<T, H: Copy>( + arr: ArrayIter<impl ArrayAccessor<Item = T>>, + seed: &mut [H], + func: impl Fn(H, T) -> Result<H>, +) -> Result<()> { + let len = arr.len(); + if len != seed.len() { + return exec_err!("Array length mismatch: {} != {}", len, seed.len()); + } + for (hash, elem) in seed.iter_mut().zip(arr) { + if let Some(elem) = elem { + *hash = func(*hash, elem)?; + } + } + Ok(()) +} Review Comment: From what I can tell, result is only needed for: - Checking array length mismatch with seed length, which I feel can be omitted (at worst we can debug_assert it since we control the invocations of the function) - Special case for Decimal128 which can fit in i64 Yet because of using Result here, all the functions must return Result; I'm wondering if the complexity is worth it or if we can remove these checks/special case for a more simplified flow. -- 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]
