alamb commented on a change in pull request #9469:
URL: https://github.com/apache/arrow/pull/9469#discussion_r574762596
##########
File path: rust/arrow/src/array/array.rs
##########
@@ -409,4 +571,42 @@ mod tests {
assert_eq!(a.len(), 0);
assert_eq!(a.value_offsets()[0], 0i32);
}
+
+ #[test]
+ fn test_null_boolean() {
+ let array = new_null_array(&DataType::Boolean, 9);
+ let a = array.as_any().downcast_ref::<BooleanArray>().unwrap();
+ assert_eq!(a.len(), 9);
+ for i in 0..9 {
+ assert!(a.is_null(i));
+ }
+ }
+
+ #[test]
+ fn test_null_primitive() {
+ let array = new_null_array(&DataType::Int32, 9);
+ let a = array.as_any().downcast_ref::<Int32Array>().unwrap();
+ assert_eq!(a.len(), 9);
+ for i in 0..9 {
+ assert!(a.is_null(i));
+ }
+ }
+
+ #[test]
+ fn test_null_variable_sized() {
+ let array = new_null_array(&DataType::Utf8, 9);
+ let a = array.as_any().downcast_ref::<StringArray>().unwrap();
+ assert_eq!(a.len(), 9);
+ assert_eq!(a.value_offsets()[9], 0i32);
Review comment:
```suggestion
assert_eq!(a.value_offsets()[9], 0i32);
for i in 0..9 {
assert!(a.is_null(i));
}
```
##########
File path: rust/arrow/src/array/array.rs
##########
@@ -326,6 +327,167 @@ pub fn new_empty_array(data_type: &DataType) -> ArrayRef {
let data = ArrayData::new_empty(data_type);
make_array(Arc::new(data))
}
+/// Creates a new array of `data_type` of length `length` filled entirely of
`NULL` values
+pub fn new_null_array(data_type: &DataType, length: usize) -> ArrayRef {
+ match data_type {
+ DataType::Null => Arc::new(NullArray::new(length)),
+ DataType::Boolean => {
+ let null_buf: Buffer = MutableBuffer::new_null(length).into();
+ make_array(Arc::new(ArrayData::new(
+ data_type.clone(),
+ length,
+ Some(length),
+ Some(null_buf.clone()),
+ 0,
+ vec![null_buf],
+ vec![],
+ )))
+ }
+ DataType::Int8 | DataType::UInt8 => {
+ new_null_sized_array::<Int8Type>(data_type, length)
+ }
+ DataType::Int16 | DataType::UInt16 => {
+ new_null_sized_array::<Int16Type>(data_type, length)
+ }
+ DataType::Float16 => unreachable!(),
+ DataType::Int32
+ | DataType::UInt32
Review comment:
While it is probably ok, it took me a while to convince myself that
`new_null_sized_array::<Int32Type>` was ok to use for different types like
`Float32`
I wonder if it would be better to explicitly list out the types here
```
DataType::Float32 => new_null_sized_array::<Float32Type>(data_type, length),
DataType::Date32 => new_null_sized_array::<Int32Type>(data_type, length),
...
```
##########
File path: rust/datafusion/src/scalar.rs
##########
@@ -218,99 +205,90 @@ impl ScalarValue {
Some(value) => {
Arc::new(Float64Array::from_iter_values(repeat(*value).take(size)))
}
- None =>
Arc::new(repeat(None).take(size).collect::<Float64Array>()),
Review comment:
this is a nice cleanup
##########
File path: rust/arrow/src/array/array.rs
##########
@@ -409,4 +571,42 @@ mod tests {
assert_eq!(a.len(), 0);
assert_eq!(a.value_offsets()[0], 0i32);
}
+
+ #[test]
+ fn test_null_boolean() {
+ let array = new_null_array(&DataType::Boolean, 9);
+ let a = array.as_any().downcast_ref::<BooleanArray>().unwrap();
+ assert_eq!(a.len(), 9);
+ for i in 0..9 {
+ assert!(a.is_null(i));
+ }
+ }
+
+ #[test]
+ fn test_null_primitive() {
+ let array = new_null_array(&DataType::Int32, 9);
+ let a = array.as_any().downcast_ref::<Int32Array>().unwrap();
+ assert_eq!(a.len(), 9);
+ for i in 0..9 {
+ assert!(a.is_null(i));
+ }
+ }
+
+ #[test]
+ fn test_null_variable_sized() {
+ let array = new_null_array(&DataType::Utf8, 9);
+ let a = array.as_any().downcast_ref::<StringArray>().unwrap();
+ assert_eq!(a.len(), 9);
+ assert_eq!(a.value_offsets()[9], 0i32);
+ }
+
+ #[test]
+ fn test_null_list_primitive() {
+ let data_type =
+ DataType::List(Box::new(Field::new("item", DataType::Int32,
true)));
+ let array = new_null_array(&data_type, 9);
+ let a = array.as_any().downcast_ref::<ListArray>().unwrap();
+ assert_eq!(a.len(), 9);
+ assert_eq!(a.value_offsets()[9], 0i32);
+ }
Review comment:
```suggestion
}
#[test]
fn test_null_dictionary() {
let values = vec![
None, None, None, None, None, None, None, None, None
] as Vec<Option<&str>>;
let array : DictionaryArray<Int8Type> = values.into_iter().collect();
let array = Arc::new(array) as ArrayRef;
let null_array = new_null_array(array.data_type(), 9);
assert_eq!(&array, &null_array);
}
```
##########
File path: rust/arrow/src/array/array.rs
##########
@@ -409,4 +571,42 @@ mod tests {
assert_eq!(a.len(), 0);
assert_eq!(a.value_offsets()[0], 0i32);
}
+
+ #[test]
+ fn test_null_boolean() {
+ let array = new_null_array(&DataType::Boolean, 9);
+ let a = array.as_any().downcast_ref::<BooleanArray>().unwrap();
+ assert_eq!(a.len(), 9);
+ for i in 0..9 {
+ assert!(a.is_null(i));
+ }
+ }
+
+ #[test]
+ fn test_null_primitive() {
+ let array = new_null_array(&DataType::Int32, 9);
+ let a = array.as_any().downcast_ref::<Int32Array>().unwrap();
+ assert_eq!(a.len(), 9);
+ for i in 0..9 {
+ assert!(a.is_null(i));
+ }
+ }
+
+ #[test]
+ fn test_null_variable_sized() {
+ let array = new_null_array(&DataType::Utf8, 9);
+ let a = array.as_any().downcast_ref::<StringArray>().unwrap();
+ assert_eq!(a.len(), 9);
+ assert_eq!(a.value_offsets()[9], 0i32);
+ }
+
+ #[test]
+ fn test_null_list_primitive() {
+ let data_type =
+ DataType::List(Box::new(Field::new("item", DataType::Int32,
true)));
+ let array = new_null_array(&data_type, 9);
+ let a = array.as_any().downcast_ref::<ListArray>().unwrap();
+ assert_eq!(a.len(), 9);
+ assert_eq!(a.value_offsets()[9], 0i32);
Review comment:
```suggestion
assert_eq!(a.value_offsets()[9], 0i32);
for i in 0..9 {
assert!(a.is_null(i));
}
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]