findepi commented on code in PR #16856: URL: https://github.com/apache/datafusion/pull/16856#discussion_r2225398089
########## datafusion/functions/src/string/chr.rs: ########## @@ -132,3 +134,79 @@ impl ScalarUDFImpl for ChrFunc { self.doc() } } + +#[cfg(test)] +mod tests { + use super::*; + use arrow::array::{Array, Int64Array, StringArray}; + use datafusion_common::assert_contains; + + #[test] + fn test_chr_normal() { + let input = Arc::new(Int64Array::from(vec![ + Some(65), // A + Some(66), // B + Some(67), // C + Some(128640), // 🚀 + Some(8364), // € + Some(945), // α + None, // NULL + Some(32), // space + Some(10), // newline + Some(9), // tab + ])); + let result = chr(&[input]).unwrap(); + let string_array = result.as_any().downcast_ref::<StringArray>().unwrap(); + let expected = ["A", "B", "C", "🚀", "€", "α", "", " ", "\n", "\t"]; + + assert_eq!(string_array.len(), 10); + for (i, e) in expected.iter().enumerate() { + assert_eq!(string_array.value(i), *e); + } + } + + #[test] + fn test_chr_error() { + // chr(0) returns an error + let input = Arc::new(Int64Array::from(vec![0])); + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "null character not permitted" + ); + + // invalid Unicode code points (too large) Review Comment: Maybe add a test with input being a surrogate codepoint ########## datafusion/functions/src/string/chr.rs: ########## @@ -132,3 +134,79 @@ impl ScalarUDFImpl for ChrFunc { self.doc() } } + +#[cfg(test)] +mod tests { + use super::*; + use arrow::array::{Array, Int64Array, StringArray}; + use datafusion_common::assert_contains; + + #[test] + fn test_chr_normal() { + let input = Arc::new(Int64Array::from(vec![ + Some(65), // A + Some(66), // B + Some(67), // C + Some(128640), // 🚀 + Some(8364), // € + Some(945), // α + None, // NULL + Some(32), // space + Some(10), // newline + Some(9), // tab + ])); + let result = chr(&[input]).unwrap(); + let string_array = result.as_any().downcast_ref::<StringArray>().unwrap(); + let expected = ["A", "B", "C", "🚀", "€", "α", "", " ", "\n", "\t"]; + + assert_eq!(string_array.len(), 10); + for (i, e) in expected.iter().enumerate() { + assert_eq!(string_array.value(i), *e); + } + } + + #[test] + fn test_chr_error() { + // chr(0) returns an error + let input = Arc::new(Int64Array::from(vec![0])); + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "null character not permitted" + ); + + // invalid Unicode code points (too large) + let input = Arc::new(Int64Array::from(vec![i64::MAX])); + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "requested character too large for encoding" + ); + + // negative input + let input = Arc::new(Int64Array::from(vec![i64::MIN + 2i64])); // will be 2 if cast to u32 Review Comment: Cast in SQL sense or Rust sense? `i64::MIN + 2i64` is outside range of values representable as u32, so "cast to u32" should be error instead, Also, maybe add a test case with a negative value closer to 0, such as `-1` ########## datafusion/functions/src/string/chr.rs: ########## @@ -132,3 +134,79 @@ impl ScalarUDFImpl for ChrFunc { self.doc() } } + +#[cfg(test)] +mod tests { + use super::*; + use arrow::array::{Array, Int64Array, StringArray}; + use datafusion_common::assert_contains; + + #[test] + fn test_chr_normal() { + let input = Arc::new(Int64Array::from(vec![ + Some(65), // A + Some(66), // B + Some(67), // C + Some(128640), // 🚀 + Some(8364), // € + Some(945), // α + None, // NULL + Some(32), // space + Some(10), // newline + Some(9), // tab + ])); + let result = chr(&[input]).unwrap(); + let string_array = result.as_any().downcast_ref::<StringArray>().unwrap(); + let expected = ["A", "B", "C", "🚀", "€", "α", "", " ", "\n", "\t"]; + + assert_eq!(string_array.len(), 10); + for (i, e) in expected.iter().enumerate() { + assert_eq!(string_array.value(i), *e); + } + } + + #[test] + fn test_chr_error() { + // chr(0) returns an error + let input = Arc::new(Int64Array::from(vec![0])); + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "null character not permitted" + ); + + // invalid Unicode code points (too large) + let input = Arc::new(Int64Array::from(vec![i64::MAX])); + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "requested character too large for encoding" + ); + + // negative input + let input = Arc::new(Int64Array::from(vec![i64::MIN + 2i64])); // will be 2 if cast to u32 + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "negative input not permitted" + ); + + // one error with valid values after + let input = Arc::new(Int64Array::from(vec![65, 0, 66])); // A, NULL_CHAR, B + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "null character not permitted" Review Comment: actually why 0 is not ok? ########## datafusion/functions/src/string/chr.rs: ########## @@ -49,6 +49,8 @@ pub fn chr(args: &[ArrayRef]) -> Result<ArrayRef> { Some(integer) => { if integer == 0 { return exec_err!("null character not permitted."); + } else if integer < 0 { + return exec_err!("negative input not permitted."); Review Comment: ❤️ ########## datafusion/functions/src/string/chr.rs: ########## @@ -132,3 +134,79 @@ impl ScalarUDFImpl for ChrFunc { self.doc() } } + +#[cfg(test)] +mod tests { + use super::*; + use arrow::array::{Array, Int64Array, StringArray}; + use datafusion_common::assert_contains; + + #[test] + fn test_chr_normal() { + let input = Arc::new(Int64Array::from(vec![ + Some(65), // A + Some(66), // B + Some(67), // C + Some(128640), // 🚀 + Some(8364), // € + Some(945), // α + None, // NULL + Some(32), // space + Some(10), // newline + Some(9), // tab + ])); + let result = chr(&[input]).unwrap(); + let string_array = result.as_any().downcast_ref::<StringArray>().unwrap(); + let expected = ["A", "B", "C", "🚀", "€", "α", "", " ", "\n", "\t"]; + + assert_eq!(string_array.len(), 10); + for (i, e) in expected.iter().enumerate() { + assert_eq!(string_array.value(i), *e); + } + } + + #[test] + fn test_chr_error() { + // chr(0) returns an error + let input = Arc::new(Int64Array::from(vec![0])); + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "null character not permitted" + ); + + // invalid Unicode code points (too large) + let input = Arc::new(Int64Array::from(vec![i64::MAX])); + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "requested character too large for encoding" + ); + + // negative input + let input = Arc::new(Int64Array::from(vec![i64::MIN + 2i64])); // will be 2 if cast to u32 + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "negative input not permitted" + ); + + // one error with valid values after + let input = Arc::new(Int64Array::from(vec![65, 0, 66])); // A, NULL_CHAR, B + let result = chr(&[input]); + assert!(result.is_err()); + assert_contains!( + result.err().unwrap().to_string(), + "null character not permitted" + ); + + // empty input array + let input = Arc::new(Int64Array::from(Vec::<i64>::new())); + let result = chr(&[input]).unwrap(); + let string_array = result.as_any().downcast_ref::<StringArray>().unwrap(); + assert_eq!(string_array.len(), 0); Review Comment: That's not an error, so maybe move out of `test_chr_error` function -- 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