Omega359 commented on code in PR #11942: URL: https://github.com/apache/datafusion/pull/11942#discussion_r1715564886
########## datafusion/functions/src/unicode/rpad.rs: ########## @@ -76,97 +84,168 @@ impl ScalarUDFImpl for RPadFunc { } fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { - match args[0].data_type() { - DataType::Utf8 => make_scalar_function(rpad::<i32>, vec![])(args), - DataType::LargeUtf8 => make_scalar_function(rpad::<i64>, vec![])(args), - other => exec_err!("Unsupported data type {other:?} for function rpad"), + match args.len() { + 2 => match args[0].data_type() { + DataType::Utf8 | DataType::Utf8View => { + make_scalar_function(rpad::<i32, i32>, vec![])(args) + } + DataType::LargeUtf8 => { + make_scalar_function(rpad::<i64, i64>, vec![])(args) + } + other => exec_err!("Unsupported data type {other:?} for function rpad"), + }, + 3 => match (args[0].data_type(), args[2].data_type()) { + ( + DataType::Utf8 | DataType::Utf8View, + DataType::Utf8 | DataType::Utf8View, + ) => make_scalar_function(rpad::<i32, i32>, vec![])(args), + (DataType::LargeUtf8, DataType::LargeUtf8) => { + make_scalar_function(rpad::<i64, i64>, vec![])(args) + } + (DataType::LargeUtf8, DataType::Utf8View | DataType::Utf8) => { + make_scalar_function(rpad::<i64, i32>, vec![])(args) + } + (DataType::Utf8View | DataType::Utf8, DataType::LargeUtf8) => { + make_scalar_function(rpad::<i32, i64>, vec![])(args) + } + (first_type, last_type) => { + exec_err!("unsupported arguments type for rpad, first argument type is {}, last argument type is {}", first_type, last_type) + } + }, + number => { + exec_err!("unsupported arguments number {} for rpad", number) + } } } } -/// Extends the string to length 'length' by appending the characters fill (a space by default). If the string is already longer than length then it is truncated. -/// rpad('hi', 5, 'xy') = 'hixyx' -pub fn rpad<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { - match args.len() { - 2 => { - let string_array = as_generic_string_array::<T>(&args[0])?; - let length_array = as_int64_array(&args[1])?; - - let result = string_array - .iter() - .zip(length_array.iter()) - .map(|(string, length)| match (string, length) { - (Some(string), Some(length)) => { - if length > i32::MAX as i64 { - return exec_err!( - "rpad requested length {length} too large" - ); - } - - let length = if length < 0 { 0 } else { length as usize }; - if length == 0 { - Ok(Some("".to_string())) - } else { - let graphemes = string.graphemes(true).collect::<Vec<&str>>(); - if length < graphemes.len() { - Ok(Some(graphemes[..length].concat())) - } else { - let mut s = string.to_string(); - s.push_str(" ".repeat(length - graphemes.len()).as_str()); - Ok(Some(s)) - } - } +macro_rules! process_rpad { + // For the two-argument case + ($string_array:expr, $length_array:expr, $is_view:expr) => {{ + $string_array + .iter() + .zip($length_array.iter()) + .map(|(string, length)| match (string, length) { + (Some(string), Some(length)) => { + if length > i32::MAX as i64 { + return exec_err!("rpad requested length {} too large", length); } - _ => Ok(None), - }) - .collect::<Result<GenericStringArray<T>>>()?; - Ok(Arc::new(result) as ArrayRef) - } - 3 => { - let string_array = as_generic_string_array::<T>(&args[0])?; - let length_array = as_int64_array(&args[1])?; - let fill_array = as_generic_string_array::<T>(&args[2])?; - - let result = string_array - .iter() - .zip(length_array.iter()) - .zip(fill_array.iter()) - .map(|((string, length), fill)| match (string, length, fill) { - (Some(string), Some(length), Some(fill)) => { - if length > i32::MAX as i64 { - return exec_err!( - "rpad requested length {length} too large" - ); - } - let length = if length < 0 { 0 } else { length as usize }; + let length = if length < 0 { 0 } else { length as usize }; + if length == 0 { + Ok(Some("".to_string())) + } else { let graphemes = string.graphemes(true).collect::<Vec<&str>>(); - let fill_chars = fill.chars().collect::<Vec<char>>(); - if length < graphemes.len() { Ok(Some(graphemes[..length].concat())) - } else if fill_chars.is_empty() { - Ok(Some(string.to_string())) } else { let mut s = string.to_string(); - let mut char_vector = - Vec::<char>::with_capacity(length - graphemes.len()); - for l in 0..length - graphemes.len() { - char_vector - .push(*fill_chars.get(l % fill_chars.len()).unwrap()); - } - s.push_str(char_vector.iter().collect::<String>().as_str()); + s.push_str(" ".repeat(length - graphemes.len()).as_str()); Ok(Some(s)) } } - _ => Ok(None), - }) - .collect::<Result<GenericStringArray<T>>>()?; + } + _ => Ok(None), + }) + .collect::<Result<GenericStringArray<StringArrayLen>>>() + }}; + // For the three-argument case + ($string_array:expr, $length_array:expr, $fill_array:expr, $is_view:expr) => {{ Review Comment: is_view looks to be unused and it always passed in true. Should be removed unless I missed something. ########## datafusion/functions/src/unicode/rpad.rs: ########## @@ -76,97 +84,168 @@ impl ScalarUDFImpl for RPadFunc { } fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { - match args[0].data_type() { - DataType::Utf8 => make_scalar_function(rpad::<i32>, vec![])(args), - DataType::LargeUtf8 => make_scalar_function(rpad::<i64>, vec![])(args), - other => exec_err!("Unsupported data type {other:?} for function rpad"), + match args.len() { + 2 => match args[0].data_type() { + DataType::Utf8 | DataType::Utf8View => { + make_scalar_function(rpad::<i32, i32>, vec![])(args) + } + DataType::LargeUtf8 => { + make_scalar_function(rpad::<i64, i64>, vec![])(args) + } + other => exec_err!("Unsupported data type {other:?} for function rpad"), + }, + 3 => match (args[0].data_type(), args[2].data_type()) { + ( + DataType::Utf8 | DataType::Utf8View, + DataType::Utf8 | DataType::Utf8View, + ) => make_scalar_function(rpad::<i32, i32>, vec![])(args), + (DataType::LargeUtf8, DataType::LargeUtf8) => { + make_scalar_function(rpad::<i64, i64>, vec![])(args) + } + (DataType::LargeUtf8, DataType::Utf8View | DataType::Utf8) => { + make_scalar_function(rpad::<i64, i32>, vec![])(args) + } + (DataType::Utf8View | DataType::Utf8, DataType::LargeUtf8) => { + make_scalar_function(rpad::<i32, i64>, vec![])(args) + } + (first_type, last_type) => { + exec_err!("unsupported arguments type for rpad, first argument type is {}, last argument type is {}", first_type, last_type) + } + }, + number => { + exec_err!("unsupported arguments number {} for rpad", number) + } } } } -/// Extends the string to length 'length' by appending the characters fill (a space by default). If the string is already longer than length then it is truncated. -/// rpad('hi', 5, 'xy') = 'hixyx' -pub fn rpad<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { - match args.len() { - 2 => { - let string_array = as_generic_string_array::<T>(&args[0])?; - let length_array = as_int64_array(&args[1])?; - - let result = string_array - .iter() - .zip(length_array.iter()) - .map(|(string, length)| match (string, length) { - (Some(string), Some(length)) => { - if length > i32::MAX as i64 { - return exec_err!( - "rpad requested length {length} too large" - ); - } - - let length = if length < 0 { 0 } else { length as usize }; - if length == 0 { - Ok(Some("".to_string())) - } else { - let graphemes = string.graphemes(true).collect::<Vec<&str>>(); - if length < graphemes.len() { - Ok(Some(graphemes[..length].concat())) - } else { - let mut s = string.to_string(); - s.push_str(" ".repeat(length - graphemes.len()).as_str()); - Ok(Some(s)) - } - } +macro_rules! process_rpad { + // For the two-argument case + ($string_array:expr, $length_array:expr, $is_view:expr) => {{ Review Comment: is_view looks to be unused and it always passed in true. Should be removed unless I missed something. -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org