waynexia commented on code in PR #16856:
URL: https://github.com/apache/datafusion/pull/16856#discussion_r2226354689
##########
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 Rust. Input out of the valid UTF-8 range will report an error, but
input like `i64::MIN + 2i64` will be first cast to `2u32` and then become a
valid UTF-8 char, which IMO is invalid. So I add a negative checker to make
sure this won't happen.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]