alamb commented on code in PR #11942: URL: https://github.com/apache/datafusion/pull/11942#discussion_r1715826438
########## 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: I agree -- removing it would simplify this PR -- 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