Omega359 commented on code in PR #14367: URL: https://github.com/apache/datafusion/pull/14367#discussion_r1945207357
########## datafusion/functions/src/hash/xxhash.rs: ########## @@ -0,0 +1,476 @@ +// 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, BinaryArray, LargeBinaryArray, LargeStringArray, StringArray}; +use arrow::datatypes::DataType; +use arrow::datatypes::DataType::{Binary, Int64, LargeBinary, LargeUtf8, Utf8, Utf8View}; +use datafusion_common::DataFusionError; +use datafusion_common::{plan_err, Result, ScalarValue}; +use datafusion_expr::{ + ColumnarValue, Documentation, ScalarUDFImpl, Signature, TypeSignature, Volatility, +}; +use datafusion_macros::user_doc; +use std::any::Any; +use std::hash::Hasher; +use std::sync::Arc; +use twox_hash::{XxHash32, XxHash64}; + +#[user_doc( + doc_section(label = "Hashing Functions"), + description = "Computes the XXHash32 hash of a binary string.", + syntax_example = "xxhash32(expression)", + sql_example = r#"```sql +> select xxhash32('foo'); ++-------------------------------------------+ +| xxhash32(Utf8("foo")) | ++-------------------------------------------+ +| <xxhash32_result> | ++-------------------------------------------+ +```"#, + standard_argument(name = "expression", prefix = "String") +)] +#[derive(Debug)] +pub struct XxHash32Func { + signature: Signature, +} + +impl Default for XxHash32Func { + fn default() -> Self { + Self::new() + } +} + +impl XxHash32Func { + pub fn new() -> Self { + Self { + signature: Signature::one_of( + vec![ + TypeSignature::Exact(vec![Utf8View]), + TypeSignature::Exact(vec![Utf8]), + TypeSignature::Exact(vec![LargeUtf8]), + TypeSignature::Exact(vec![Binary]), + TypeSignature::Exact(vec![LargeBinary]), + TypeSignature::Exact(vec![Utf8View, Int64]), + TypeSignature::Exact(vec![Utf8, Int64]), + TypeSignature::Exact(vec![LargeUtf8, Int64]), + TypeSignature::Exact(vec![Binary, Int64]), + TypeSignature::Exact(vec![LargeBinary, Int64]), + ], + Volatility::Immutable, + ), + } + } + + pub fn hash_scalar(&self, value: &[u8]) -> Result<String> { + // let value_str = to_string_from_scalar(value)?; + hash_value(value, XxHash32::default(), HashType::U32) + } +} + +impl ScalarUDFImpl for XxHash32Func { + fn as_any(&self) -> &dyn Any { + self + } + + fn name(&self) -> &str { + "xxhash32" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { + use DataType::*; + Ok(match &arg_types[0] { + LargeUtf8 | LargeBinary => Utf8, + Utf8View | Utf8 | Binary => Utf8, + Null => Null, + Dictionary(_, t) => match **t { + LargeUtf8 | LargeBinary => Utf8, + Utf8 | Binary => Utf8, + Null => Null, + _ => { + return plan_err!( + "the xxhash32 can only accept strings but got {:?}", + **t + ); + } + }, + other => { + return plan_err!( + "The xxhash32 function can only accept strings. Got {other}" + ); + } + }) + } + + fn invoke_batch( + &self, + args: &[ColumnarValue], + _number_rows: usize, + ) -> Result<ColumnarValue> { + let input_data = &args[0]; + + let seed = if args.len() > 1 { + if let ColumnarValue::Scalar(ScalarValue::Int64(Some(seed))) = &args[1] { + if *seed >= 0 && *seed <= u32::MAX as i64 { + *seed as u32 + } else { + return Err(DataFusionError::Execution(format!( + "Seed value out of range for UInt32: {}", + seed + ))); + } + } else { + let actual_type = format!("{:?}", &args[1]); + return Err(DataFusionError::Execution(format!( + "Expected a Int64 seed value, but got {}", + actual_type + ))); + } + } else { + 0 // Default seed value + }; + + let result = match input_data { + ColumnarValue::Array(array) => { + let hash_results = + process_array(array, XxHash32::with_seed(seed), HashType::U32)?; + let hash_array = StringArray::from(hash_results); + Arc::new(hash_array) as Arc<dyn Array> + } + ColumnarValue::Scalar(scalar) => match scalar { + ScalarValue::Utf8(None) + | ScalarValue::Utf8View(None) + | ScalarValue::LargeUtf8(None) => { + let hash_array = StringArray::from(vec![String::new()]); + Arc::new(hash_array) as Arc<dyn Array> + } + ScalarValue::Utf8(Some(ref v)) + | ScalarValue::Utf8View(Some(ref v)) + | ScalarValue::LargeUtf8(Some(ref v)) => { + let hash_result = hash_value( + v.as_bytes(), + XxHash32::with_seed(seed), + HashType::U32, + )?; + let hash_array = StringArray::from(vec![hash_result]); + Arc::new(hash_array) as Arc<dyn Array> + } + ScalarValue::Binary(Some(ref v)) + | ScalarValue::LargeBinary(Some(ref v)) => { + let hash_result = + hash_value(v, XxHash32::with_seed(seed), HashType::U32)?; + let hash_array = StringArray::from(vec![hash_result]); + Arc::new(hash_array) as Arc<dyn Array> + } + _ => { + let actual_type = format!("{:?}", scalar); Review Comment: The extra variable seem unnecessary here. I think this should work: return internal_err!("Unsupported scalar type: {:?}", scalar); -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org