From 4b42ec2cd58123199fccae12fb231c78e0f6a707 Mon Sep 17 00:00:00 2001 From: Jiayu Liu Date: Fri, 11 Jun 2021 20:44:27 +0800 Subject: [PATCH] remove unnecessary wraps in sortk --- arrow/src/compute/kernels/sort.rs | 96 ++++++++++++++++--------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/arrow/src/compute/kernels/sort.rs b/arrow/src/compute/kernels/sort.rs index feef00cda24b..0055ce2c531b 100644 --- a/arrow/src/compute/kernels/sort.rs +++ b/arrow/src/compute/kernels/sort.rs @@ -163,7 +163,7 @@ pub fn sort_to_indices( let (v, n) = partition_validity(values); - match values.data_type() { + Ok(match values.data_type() { DataType::Boolean => sort_boolean(values, v, n, &options, limit), DataType::Int8 => { sort_primitive::(values, v, n, cmp, &options, limit) @@ -278,10 +278,12 @@ pub fn sort_to_indices( DataType::Float64 => { sort_list::(values, v, n, &options, limit) } - t => Err(ArrowError::ComputeError(format!( - "Sort not supported for list type {:?}", - t - ))), + t => { + return Err(ArrowError::ComputeError(format!( + "Sort not supported for list type {:?}", + t + ))) + } }, DataType::LargeList(field) => match field.data_type() { DataType::Int8 => sort_list::(values, v, n, &options, limit), @@ -304,10 +306,12 @@ pub fn sort_to_indices( DataType::Float64 => { sort_list::(values, v, n, &options, limit) } - t => Err(ArrowError::ComputeError(format!( - "Sort not supported for list type {:?}", - t - ))), + t => { + return Err(ArrowError::ComputeError(format!( + "Sort not supported for list type {:?}", + t + ))) + } }, DataType::FixedSizeList(field, _) => match field.data_type() { DataType::Int8 => sort_list::(values, v, n, &options, limit), @@ -330,10 +334,12 @@ pub fn sort_to_indices( DataType::Float64 => { sort_list::(values, v, n, &options, limit) } - t => Err(ArrowError::ComputeError(format!( - "Sort not supported for list type {:?}", - t - ))), + t => { + return Err(ArrowError::ComputeError(format!( + "Sort not supported for list type {:?}", + t + ))) + } }, DataType::Dictionary(key_type, value_type) if *value_type.as_ref() == DataType::Utf8 => @@ -363,17 +369,21 @@ pub fn sort_to_indices( DataType::UInt64 => { sort_string_dictionary::(values, v, n, &options, limit) } - t => Err(ArrowError::ComputeError(format!( - "Sort not supported for dictionary key type {:?}", - t - ))), + t => { + return Err(ArrowError::ComputeError(format!( + "Sort not supported for dictionary key type {:?}", + t + ))) + } } } - t => Err(ArrowError::ComputeError(format!( - "Sort not supported for data type {:?}", - t - ))), - } + t => { + return Err(ArrowError::ComputeError(format!( + "Sort not supported for data type {:?}", + t + ))) + } + }) } /// Options that define how sort kernels should behave @@ -396,14 +406,13 @@ impl Default for SortOptions { } /// Sort primitive values -#[allow(clippy::unnecessary_wraps)] fn sort_boolean( values: &ArrayRef, value_indices: Vec, null_indices: Vec, options: &SortOptions, limit: Option, -) -> Result { +) -> UInt32Array { let values = values .as_any() .downcast_ref::() @@ -469,11 +478,10 @@ fn sort_boolean( vec![], ); - Ok(UInt32Array::from(result_data)) + UInt32Array::from(result_data) } /// Sort primitive values -#[allow(clippy::unnecessary_wraps)] fn sort_primitive( values: &ArrayRef, value_indices: Vec, @@ -481,7 +489,7 @@ fn sort_primitive( cmp: F, options: &SortOptions, limit: Option, -) -> Result +) -> UInt32Array where T: ArrowPrimitiveType, T::Native: std::cmp::PartialOrd, @@ -549,7 +557,7 @@ where vec![], ); - Ok(UInt32Array::from(result_data)) + UInt32Array::from(result_data) } // insert valid and nan values in the correct order depending on the descending flag @@ -574,7 +582,7 @@ fn sort_string( null_indices: Vec, options: &SortOptions, limit: Option, -) -> Result { +) -> UInt32Array { let values = values .as_any() .downcast_ref::>() @@ -597,7 +605,7 @@ fn sort_string_dictionary( null_indices: Vec, options: &SortOptions, limit: Option, -) -> Result { +) -> UInt32Array { let values: &DictionaryArray = as_dictionary_array::(values); let keys: &PrimitiveArray = &values.keys_array(); @@ -620,7 +628,6 @@ fn sort_string_dictionary( /// shared implementation between dictionary encoded and plain string arrays #[inline] -#[allow(clippy::unnecessary_wraps)] fn sort_string_helper<'a, A: Array, F>( values: &'a A, value_indices: Vec, @@ -628,7 +635,7 @@ fn sort_string_helper<'a, A: Array, F>( options: &SortOptions, limit: Option, value_fn: F, -) -> Result +) -> UInt32Array where F: Fn(&'a A, u32) -> &str, { @@ -661,23 +668,22 @@ where if options.nulls_first { nulls.append(&mut valid_indices); nulls.truncate(len); - return Ok(UInt32Array::from(nulls)); + UInt32Array::from(nulls) + } else { + // no need to sort nulls as they are in the correct order already + valid_indices.append(&mut nulls); + valid_indices.truncate(len); + UInt32Array::from(valid_indices) } - - // no need to sort nulls as they are in the correct order already - valid_indices.append(&mut nulls); - valid_indices.truncate(len); - Ok(UInt32Array::from(valid_indices)) } -#[allow(clippy::unnecessary_wraps)] fn sort_list( values: &ArrayRef, value_indices: Vec, mut null_indices: Vec, options: &SortOptions, limit: Option, -) -> Result +) -> UInt32Array where S: OffsetSizeTrait, T: ArrowPrimitiveType, @@ -727,12 +733,12 @@ where if options.nulls_first { null_indices.append(&mut valid_indices); null_indices.truncate(len); - return Ok(UInt32Array::from(null_indices)); + UInt32Array::from(null_indices) + } else { + valid_indices.append(&mut null_indices); + valid_indices.truncate(len); + UInt32Array::from(valid_indices) } - - valid_indices.append(&mut null_indices); - valid_indices.truncate(len); - Ok(UInt32Array::from(valid_indices)) } /// Compare two `Array`s based on the ordering defined in [ord](crate::array::ord).