findepi commented on code in PR #16856: URL: https://github.com/apache/datafusion/pull/16856#discussion_r2226889191
########## 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: > nput like `i64::MIN + 2i64` will be first cast to `2u32` and then become a valid UTF-8 char, which IMO is invalid very good point. it's because of the `as`. if you happen to be on ASF slack, i posted a comment about this there -- https://the-asf.slack.com/archives/C04RJ0C85UZ/p1753279450090609 > So I add a negative checker to make sure this won't happen. thanks! -- 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