-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(spark): implement spark hash function murmur/xxhash64 #17093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cab9154
feat: spark xxhash64/murmurhash
chenkovsky 0dbc721
Merge branch 'main' into feat/hash
chenkovsky c49bc12
fix
chenkovsky 967cc97
add comments
chenkovsky c6ad2f3
Merge branch 'main' into feat/hash
chenkovsky b5e6f78
update
chenkovsky a2b11ed
fix
chenkovsky 2396e68
remove bigendian
chenkovsky 6b7628d
update
chenkovsky 26056f0
update
chenkovsky d8f2dfb
update
chenkovsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,151 @@ | ||
| // 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, ArrowNativeTypeOp, PrimitiveArray}; | ||
| use arrow::datatypes::{DataType, Int32Type}; | ||
| use datafusion_common::Result; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, | ||
| }; | ||
| use datafusion_functions::utils::make_scalar_function; | ||
|
|
||
| use std::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::function::hash::utils::SparkHasher; | ||
|
|
||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#hash> | ||
| #[derive(Debug, Hash, Eq, PartialEq)] | ||
| pub struct SparkMurmur3Hash { | ||
| signature: Signature, | ||
| seed: i64, | ||
| } | ||
|
|
||
| impl Default for SparkMurmur3Hash { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkMurmur3Hash { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::variadic_any(Volatility::Immutable), | ||
| seed: 42, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkMurmur3Hash { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "hash" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(DataType::Int32) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| let func = |arr: &[ArrayRef]| { | ||
| let mut result = vec![self.seed as i32; arr[0].len()]; | ||
| Murmur3Hasher::hash_arrays(arr, &mut result)?; | ||
| Ok(Arc::new(PrimitiveArray::<Int32Type>::from(result)) as ArrayRef) | ||
| }; | ||
| make_scalar_function(func, vec![])(&args.args) | ||
| } | ||
| } | ||
|
|
||
| // The following code is copied from | ||
| // <https://github.com/apache/datafusion-comet/blob/main/native/spark-expr/src/hash_funcs/murmur3.rs> | ||
| #[inline] | ||
| pub fn murmur3_hash<T: AsRef<[u8]>>(data: T, seed: u32) -> u32 { | ||
| #[inline] | ||
| fn mix_k1(mut k1: i32) -> i32 { | ||
| k1 = k1.mul_wrapping(0xcc9e2d51u32 as i32); | ||
| k1 = k1.rotate_left(15); | ||
| k1 = k1.mul_wrapping(0x1b873593u32 as i32); | ||
| k1 | ||
| } | ||
|
|
||
| #[inline] | ||
| fn mix_h1(mut h1: i32, k1: i32) -> i32 { | ||
| h1 ^= k1; | ||
| h1 = h1.rotate_left(13); | ||
| h1 = h1.mul_wrapping(5).add_wrapping(0xe6546b64u32 as i32); | ||
| h1 | ||
| } | ||
|
|
||
| #[inline] | ||
| fn fmix(mut h1: i32, len: i32) -> i32 { | ||
| h1 ^= len; | ||
| h1 ^= (h1 as u32 >> 16) as i32; | ||
| h1 = h1.mul_wrapping(0x85ebca6bu32 as i32); | ||
| h1 ^= (h1 as u32 >> 13) as i32; | ||
| h1 = h1.mul_wrapping(0xc2b2ae35u32 as i32); | ||
| h1 ^= (h1 as u32 >> 16) as i32; | ||
| h1 | ||
| } | ||
|
|
||
| #[inline] | ||
| unsafe fn hash_bytes_by_int(data: &[u8], seed: u32) -> i32 { | ||
| // safety: data length must be aligned to 4 bytes | ||
| let mut h1 = seed as i32; | ||
| for i in (0..data.len()).step_by(4) { | ||
| let ints = data.as_ptr().add(i) as *const i32; | ||
| let half_word = ints.read_unaligned(); | ||
| h1 = mix_h1(h1, mix_k1(half_word)); | ||
| } | ||
| h1 | ||
| } | ||
| let data = data.as_ref(); | ||
| let len = data.len(); | ||
| let len_aligned = len - len % 4; | ||
|
|
||
| // safety: | ||
| // avoid boundary checking in performance critical codes. | ||
| // all operations are guaranteed to be safe | ||
| // data is &[u8] so we do not need to check for proper alignment | ||
| unsafe { | ||
| let mut h1 = if len_aligned > 0 { | ||
| hash_bytes_by_int(&data[0..len_aligned], seed) | ||
| } else { | ||
| seed as i32 | ||
| }; | ||
|
|
||
| for i in len_aligned..len { | ||
| let half_word = *data.get_unchecked(i) as i8 as i32; | ||
| h1 = mix_h1(h1, mix_k1(half_word)); | ||
| } | ||
| fmix(h1, len as i32) as u32 | ||
| } | ||
| } | ||
|
|
||
| struct Murmur3Hasher; | ||
|
|
||
| impl SparkHasher<i32> for Murmur3Hasher { | ||
| fn oneshot(seed: i32, data: &[u8]) -> i32 { | ||
| murmur3_hash(data, seed as u32) as i32 | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.