-
Notifications
You must be signed in to change notification settings - Fork 335
chore: Move some utility methods to submodules of scalar_funcs #590
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // 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::expressions::scalar_funcs::hex::hex_strings; | ||
| use crate::execution::datafusion::spark_hash::{create_murmur3_hashes, create_xxhash64_hashes}; | ||
| use arrow_array::{ArrayRef, Int32Array, Int64Array, StringArray}; | ||
| use datafusion_common::cast::as_binary_array; | ||
| use datafusion_common::{exec_err, internal_err, DataFusionError, ScalarValue}; | ||
| use datafusion_expr::{ColumnarValue, ScalarFunctionImplementation}; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Spark compatible murmur3 hash in vectorized execution fashion | ||
| 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] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any chance here to be an index out of bounds?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. The seed is always provided in the Spark/JVM side. |
||
| .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 | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Spark compatible xxhash64 in vectorized execution fashion | ||
| pub fn spark_xxhash64(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its not this PR problem but we need a description to pub methods
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additionally, might consider limiting the scope. This function probably isn't needed outside of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I can add a description here.
Yeah, I originally limited it to |
||
| 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 | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(super) fn wrap_digest_result_as_hex_string( | ||
| args: &[ColumnarValue], | ||
| digest: ScalarFunctionImplementation, | ||
| ) -> Result<ColumnarValue, DataFusionError> { | ||
| let value = digest(args)?; | ||
| match value { | ||
| ColumnarValue::Array(array) => { | ||
| let binary_array = as_binary_array(&array)?; | ||
| let string_array: StringArray = binary_array | ||
| .iter() | ||
| .map(|opt| opt.map(hex_strings::<_>)) | ||
| .collect(); | ||
| Ok(ColumnarValue::Array(Arc::new(string_array))) | ||
| } | ||
| ColumnarValue::Scalar(ScalarValue::Binary(opt)) => Ok(ColumnarValue::Scalar( | ||
| ScalarValue::Utf8(opt.map(hex_strings::<_>)), | ||
| )), | ||
| _ => { | ||
| exec_err!( | ||
| "digest function should return binary value, but got: {:?}", | ||
| value.data_type() | ||
| ) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.