Lordworms commented on code in PR #11942: URL: https://github.com/apache/datafusion/pull/11942#discussion_r1714125884
########## datafusion/functions/src/unicode/rpad.rs: ########## @@ -77,96 +82,122 @@ 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::Utf8 | DataType::Utf8View => { + 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"), } } } -/// 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" - ); - } +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); + } - let length = if length < 0 { 0 } else { length as usize }; - if length == 0 { - Ok(Some("".to_string())) + 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 graphemes = string.graphemes(true).collect::<Vec<&str>>(); - if length < graphemes.len() { - Ok(Some(graphemes[..length].concat())) + let mut s = string.to_string(); + if !$is_view { Review Comment: I'll discard that, sorry for it ########## datafusion/functions/src/unicode/rpad.rs: ########## @@ -46,8 +48,11 @@ impl RPadFunc { signature: Signature::one_of( vec![ Exact(vec![Utf8, Int64]), Review Comment: Sure -- 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