This is an automated email from the ASF dual-hosted git repository.
tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new 92d8ee682f More intuitive bool-to-string casting (#4666)
92d8ee682f is described below
commit 92d8ee682fa8a8d0afc053a871565e463646811d
Author: Faiaz Sanaulla <[email protected]>
AuthorDate: Wed Aug 9 17:10:02 2023 +0200
More intuitive bool-to-string casting (#4666)
* use more intuitive bool to string casting
* tests
---
arrow-cast/src/cast.rs | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/arrow-cast/src/cast.rs b/arrow-cast/src/cast.rs
index e7ca2d0ed4..c730452a8d 100644
--- a/arrow-cast/src/cast.rs
+++ b/arrow-cast/src/cast.rs
@@ -1220,7 +1220,7 @@ pub fn cast_with_options(
Ok(Arc::new(
array
.iter()
- .map(|value| value.map(|value| if value { "1" } else {
"0" }))
+ .map(|value| value.map(|value| if value { "true" }
else { "false" }))
.collect::<StringArray>(),
))
}
@@ -1229,7 +1229,7 @@ pub fn cast_with_options(
Ok(Arc::new(
array
.iter()
- .map(|value| value.map(|value| if value { "1" } else {
"0" }))
+ .map(|value| value.map(|value| if value { "true" }
else { "false" }))
.collect::<LargeStringArray>(),
))
}
@@ -4763,6 +4763,26 @@ mod tests {
assert!(!c.is_valid(2));
}
+ #[test]
+ fn test_cast_bool_to_utf8() {
+ let array = BooleanArray::from(vec![Some(true), Some(false), None]);
+ let b = cast(&array, &DataType::Utf8).unwrap();
+ let c = b.as_any().downcast_ref::<StringArray>().unwrap();
+ assert_eq!("true", c.value(0));
+ assert_eq!("false", c.value(1));
+ assert!(!c.is_valid(2));
+ }
+
+ #[test]
+ fn test_cast_bool_to_large_utf8() {
+ let array = BooleanArray::from(vec![Some(true), Some(false), None]);
+ let b = cast(&array, &DataType::LargeUtf8).unwrap();
+ let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
+ assert_eq!("true", c.value(0));
+ assert_eq!("false", c.value(1));
+ assert!(!c.is_valid(2));
+ }
+
#[test]
fn test_cast_bool_to_f64() {
let array = BooleanArray::from(vec![Some(true), Some(false), None]);