-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Move some utility methods to submodules of scalar_funcs
- Loading branch information
1 parent
28309a4
commit a298162
Showing
4 changed files
with
178 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
core/src/execution/datafusion/expressions/scalar_funcs/hash_expressions.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// 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 crate::execution::datafusion::spark_hash::{create_murmur3_hashes, create_xxhash64_hashes}; | ||
use arrow_array::{ArrayRef, Int32Array, Int64Array}; | ||
use datafusion_common::{internal_err, DataFusionError, ScalarValue}; | ||
use datafusion_expr::ColumnarValue; | ||
use std::sync::Arc; | ||
|
||
pub fn spark_murmur3_hash(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> { | ||
let length = args.len(); | ||
let seed = &args[length - 1]; | ||
match seed { | ||
ColumnarValue::Scalar(ScalarValue::Int32(Some(seed))) => { | ||
// iterate over the arguments to find out the length of the array | ||
let num_rows = args[0..args.len() - 1] | ||
.iter() | ||
.find_map(|arg| match arg { | ||
ColumnarValue::Array(array) => Some(array.len()), | ||
ColumnarValue::Scalar(_) => None, | ||
}) | ||
.unwrap_or(1); | ||
let mut hashes: Vec<u32> = vec![0_u32; num_rows]; | ||
hashes.fill(*seed as u32); | ||
let arrays = args[0..args.len() - 1] | ||
.iter() | ||
.map(|arg| match arg { | ||
ColumnarValue::Array(array) => array.clone(), | ||
ColumnarValue::Scalar(scalar) => { | ||
scalar.clone().to_array_of_size(num_rows).unwrap() | ||
} | ||
}) | ||
.collect::<Vec<ArrayRef>>(); | ||
create_murmur3_hashes(&arrays, &mut hashes)?; | ||
if num_rows == 1 { | ||
Ok(ColumnarValue::Scalar(ScalarValue::Int32(Some( | ||
hashes[0] as i32, | ||
)))) | ||
} else { | ||
let hashes: Vec<i32> = hashes.into_iter().map(|x| x as i32).collect(); | ||
Ok(ColumnarValue::Array(Arc::new(Int32Array::from(hashes)))) | ||
} | ||
} | ||
_ => { | ||
internal_err!( | ||
"The seed of function murmur3_hash must be an Int32 scalar value, but got: {:?}.", | ||
seed | ||
) | ||
} | ||
} | ||
} | ||
|
||
pub fn spark_xxhash64(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> { | ||
let length = args.len(); | ||
let seed = &args[length - 1]; | ||
match seed { | ||
ColumnarValue::Scalar(ScalarValue::Int64(Some(seed))) => { | ||
// iterate over the arguments to find out the length of the array | ||
let num_rows = args[0..args.len() - 1] | ||
.iter() | ||
.find_map(|arg| match arg { | ||
ColumnarValue::Array(array) => Some(array.len()), | ||
ColumnarValue::Scalar(_) => None, | ||
}) | ||
.unwrap_or(1); | ||
let mut hashes: Vec<u64> = vec![0_u64; num_rows]; | ||
hashes.fill(*seed as u64); | ||
let arrays = args[0..args.len() - 1] | ||
.iter() | ||
.map(|arg| match arg { | ||
ColumnarValue::Array(array) => array.clone(), | ||
ColumnarValue::Scalar(scalar) => { | ||
scalar.clone().to_array_of_size(num_rows).unwrap() | ||
} | ||
}) | ||
.collect::<Vec<ArrayRef>>(); | ||
create_xxhash64_hashes(&arrays, &mut hashes)?; | ||
if num_rows == 1 { | ||
Ok(ColumnarValue::Scalar(ScalarValue::Int64(Some( | ||
hashes[0] as i64, | ||
)))) | ||
} else { | ||
let hashes: Vec<i64> = hashes.into_iter().map(|x| x as i64).collect(); | ||
Ok(ColumnarValue::Array(Arc::new(Int64Array::from(hashes)))) | ||
} | ||
} | ||
_ => { | ||
internal_err!( | ||
"The seed of function xxhash64 must be an Int64 scalar value, but got: {:?}.", | ||
seed | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.