Jefffrey commented on code in PR #17674: URL: https://github.com/apache/datafusion/pull/17674#discussion_r2364989036
########## datafusion/spark/src/function/array/shuffle.rs: ########## @@ -0,0 +1,191 @@ +// 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::function::functions_nested_utils::make_scalar_function; +use arrow::array::{ + Array, ArrayRef, Capacities, FixedSizeListArray, GenericListArray, MutableArrayData, + OffsetSizeTrait, +}; +use arrow::buffer::OffsetBuffer; +use arrow::datatypes::DataType::{FixedSizeList, LargeList, List, Null}; +use arrow::datatypes::{DataType, FieldRef}; +use datafusion_common::cast::{ + as_fixed_size_list_array, as_large_list_array, as_list_array, +}; +use datafusion_common::{exec_err, utils::take_function_args, Result}; +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; +use rand::rng; +use rand::seq::SliceRandom; +use std::any::Any; +use std::sync::Arc; + +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct SparkShuffle { + signature: Signature, +} + +impl Default for SparkShuffle { + fn default() -> Self { + Self::new() + } +} + +impl SparkShuffle { + pub fn new() -> Self { + Self { + signature: Signature::any(1, Volatility::Volatile), + } + } +} + +impl ScalarUDFImpl for SparkShuffle { + fn as_any(&self) -> &dyn Any { + self + } + + fn name(&self) -> &str { + "shuffle" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { + Ok(arg_types[0].clone()) + } + + fn invoke_with_args( + &self, + args: datafusion_expr::ScalarFunctionArgs, + ) -> Result<ColumnarValue> { + make_scalar_function(array_shuffle_inner)(&args.args) + } +} + +/// array_shuffle SQL function +pub fn array_shuffle_inner(arg: &[ArrayRef]) -> Result<ArrayRef> { + let [input_array] = take_function_args("shuffle", arg)?; + match &input_array.data_type() { + List(field) => { + let array = as_list_array(input_array)?; + general_array_shuffle::<i32>(array, field) + } + LargeList(field) => { + let array = as_large_list_array(input_array)?; + general_array_shuffle::<i64>(array, field) + } + FixedSizeList(field, _) => { + let array = as_fixed_size_list_array(input_array)?; + fixed_size_array_shuffle(array, field) + } + Null => Ok(Arc::clone(input_array)), + array_type => exec_err!("shuffle does not support type '{array_type}'."), + } +} + +fn general_array_shuffle<O: OffsetSizeTrait + TryFrom<i64>>( Review Comment: ```suggestion fn general_array_shuffle<O: OffsetSizeTrait>( ``` ########## datafusion/sqllogictest/test_files/spark/array/shuffle.slt: ########## Review Comment: I think if we implement with a seed argument we can have deterministic tests for shuffle, without running it through sort or relying on the shuffled permutation not being equal to the sorted version ########## datafusion/spark/src/function/array/shuffle.rs: ########## @@ -0,0 +1,191 @@ +// 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::function::functions_nested_utils::make_scalar_function; +use arrow::array::{ + Array, ArrayRef, Capacities, FixedSizeListArray, GenericListArray, MutableArrayData, + OffsetSizeTrait, +}; +use arrow::buffer::OffsetBuffer; +use arrow::datatypes::DataType::{FixedSizeList, LargeList, List, Null}; +use arrow::datatypes::{DataType, FieldRef}; +use datafusion_common::cast::{ + as_fixed_size_list_array, as_large_list_array, as_list_array, +}; +use datafusion_common::{exec_err, utils::take_function_args, Result}; +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; +use rand::rng; +use rand::seq::SliceRandom; +use std::any::Any; +use std::sync::Arc; + +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct SparkShuffle { + signature: Signature, +} + +impl Default for SparkShuffle { + fn default() -> Self { + Self::new() + } +} + +impl SparkShuffle { + pub fn new() -> Self { + Self { + signature: Signature::any(1, Volatility::Volatile), Review Comment: ```suggestion signature: Signature::arrays(1, None, Volatility::Volatile), ``` Example: https://github.com/apache/datafusion/blob/44cd972546eaf8e946fa9d4576abade7daf7efab/datafusion/functions-nested/src/empty.rs#L72-L79 (using `arrays()` instead of `array()` to avoid the coercion from `FixedSizeList` to `List`) Although, looking at the [Spark doc](https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.shuffle.html) it says it accepts an optional seed argument; do we need to include that here? -- 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]
