Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return reference from ListArray::values #3561

Merged
merged 1 commit into from Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions arrow-array/src/array/list_array.rs
Expand Up @@ -90,8 +90,8 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
};

/// Returns a reference to the values of this list.
pub fn values(&self) -> ArrayRef {
self.values.clone()
pub fn values(&self) -> &ArrayRef {
&self.values
}

/// Returns a clone of the value type of this list.
Expand Down
15 changes: 6 additions & 9 deletions arrow-cast/src/cast.rs
Expand Up @@ -4683,8 +4683,7 @@ mod tests {
assert_eq!(1, arr.value_length(2));
assert_eq!(1, arr.value_length(3));
assert_eq!(1, arr.value_length(4));
let values = arr.values();
let c = values.as_any().downcast_ref::<Int32Array>().unwrap();
let c = as_primitive_array::<Int32Type>(arr.values());
assert_eq!(5, c.value(0));
assert_eq!(6, c.value(1));
assert_eq!(7, c.value(2));
Expand All @@ -4710,8 +4709,8 @@ mod tests {
assert_eq!(1, arr.value_length(2));
assert_eq!(1, arr.value_length(3));
assert_eq!(1, arr.value_length(4));
let values = arr.values();
let c = values.as_any().downcast_ref::<Int32Array>().unwrap();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not possible before, as the temporary ArrayRef would not live for long enough.

let c = as_primitive_array::<Int32Type>(arr.values());
assert_eq!(1, c.null_count());
assert_eq!(5, c.value(0));
assert!(!c.is_valid(1));
Expand All @@ -4738,8 +4737,7 @@ mod tests {
assert_eq!(1, arr.value_length(1));
assert_eq!(1, arr.value_length(2));
assert_eq!(1, arr.value_length(3));
let values = arr.values();
let c = values.as_any().downcast_ref::<Float64Array>().unwrap();
let c = as_primitive_array::<Float64Type>(arr.values());
assert_eq!(1, c.null_count());
assert_eq!(7.0, c.value(0));
assert_eq!(8.0, c.value(1));
Expand Down Expand Up @@ -4888,9 +4886,8 @@ mod tests {
assert_eq!(2, array.value_length(2));

// expect 4 nulls: negative numbers and overflow
let values = array.values();
assert_eq!(4, values.null_count());
let u16arr = values.as_any().downcast_ref::<UInt16Array>().unwrap();
let u16arr = as_primitive_array::<UInt16Type>(array.values());
assert_eq!(4, u16arr.null_count());

// expect 4 nulls: negative numbers and overflow
let expected: UInt16Array =
Expand Down
4 changes: 2 additions & 2 deletions arrow-ipc/src/writer.rs
Expand Up @@ -223,7 +223,7 @@ impl IpcDataGenerator {
let list = as_list_array(column);
self.encode_dictionaries(
field,
&list.values(),
list.values(),
encoded_dictionaries,
dictionary_tracker,
write_options,
Expand All @@ -233,7 +233,7 @@ impl IpcDataGenerator {
let list = as_large_list_array(column);
self.encode_dictionaries(
field,
&list.values(),
list.values(),
encoded_dictionaries,
dictionary_tracker,
write_options,
Expand Down
44 changes: 14 additions & 30 deletions arrow-json/src/reader.rs
Expand Up @@ -1758,6 +1758,10 @@ impl<R: Read> Iterator for Reader<R> {
#[cfg(test)]
mod tests {
use super::*;
use arrow_array::cast::{
as_boolean_array, as_dictionary_array, as_primitive_array, as_string_array,
as_struct_array,
};
use arrow_buffer::ToByteSlice;
use arrow_schema::DataType::{Dictionary, List};
use flate2::read::GzDecoder;
Expand Down Expand Up @@ -2056,8 +2060,7 @@ mod tests {
.as_any()
.downcast_ref::<ListArray>()
.unwrap();
let bb = bb.values();
let bb = bb.as_any().downcast_ref::<Float64Array>().unwrap();
let bb = as_primitive_array::<Float64Type>(bb.values());
assert_eq!(9, bb.len());
assert_eq!(2.0, bb.value(0));
assert_eq!(-6.1, bb.value(5));
Expand All @@ -2068,8 +2071,7 @@ mod tests {
.as_any()
.downcast_ref::<ListArray>()
.unwrap();
let cc = cc.values();
let cc = cc.as_any().downcast_ref::<BooleanArray>().unwrap();
let cc = as_boolean_array(cc.values());
assert_eq!(6, cc.len());
assert!(!cc.value(0));
assert!(!cc.value(4));
Expand Down Expand Up @@ -2183,8 +2185,7 @@ mod tests {
.as_any()
.downcast_ref::<ListArray>()
.unwrap();
let bb = bb.values();
let bb = bb.as_any().downcast_ref::<Float64Array>().unwrap();
let bb = as_primitive_array::<Float64Type>(bb.values());
assert_eq!(10, bb.len());
assert_eq!(4.0, bb.value(9));

Expand All @@ -2198,8 +2199,7 @@ mod tests {
cc.data().buffers()[0],
Buffer::from_slice_ref([0i32, 2, 2, 4, 5])
);
let cc = cc.values();
let cc = cc.as_any().downcast_ref::<BooleanArray>().unwrap();
let cc = as_boolean_array(cc.values());
let cc_expected = BooleanArray::from(vec![
Some(false),
Some(true),
Expand All @@ -2219,8 +2219,8 @@ mod tests {
dd.data().buffers()[0],
Buffer::from_slice_ref([0i32, 1, 1, 2, 6])
);
let dd = dd.values();
let dd = dd.as_any().downcast_ref::<StringArray>().unwrap();

let dd = as_string_array(dd.values());
// values are 6 because a `d: null` is treated as a null slot
// and a list's null slot can be omitted from the child (i.e. same offset)
assert_eq!(6, dd.len());
Expand Down Expand Up @@ -2366,16 +2366,8 @@ mod tests {
// compare list null buffers
assert_eq!(read.data().null_buffer(), expected.data().null_buffer());
// build struct from list
let struct_values = read.values();
let struct_array: &StructArray = struct_values
.as_any()
.downcast_ref::<StructArray>()
.unwrap();
let expected_struct_values = expected.values();
let expected_struct_array = expected_struct_values
.as_any()
.downcast_ref::<StructArray>()
.unwrap();
let struct_array = as_struct_array(read.values());
let expected_struct_array = as_struct_array(expected.values());

assert_eq!(7, struct_array.len());
assert_eq!(1, struct_array.null_count());
Expand Down Expand Up @@ -2694,11 +2686,7 @@ mod tests {
.as_any()
.downcast_ref::<ListArray>()
.unwrap();
let evs_list = evs_list.values();
let evs_list = evs_list
.as_any()
.downcast_ref::<DictionaryArray<UInt64Type>>()
.unwrap();
let evs_list = as_dictionary_array::<UInt64Type>(evs_list.values());
assert_eq!(6, evs_list.len());
assert!(evs_list.is_valid(1));
assert_eq!(DataType::Utf8, evs_list.value_type());
Expand Down Expand Up @@ -2755,11 +2743,7 @@ mod tests {
.as_any()
.downcast_ref::<ListArray>()
.unwrap();
let evs_list = evs_list.values();
let evs_list = evs_list
.as_any()
.downcast_ref::<DictionaryArray<UInt64Type>>()
.unwrap();
let evs_list = as_dictionary_array::<UInt64Type>(evs_list.values());
assert_eq!(8, evs_list.len());
assert!(evs_list.is_valid(1));
assert_eq!(DataType::Utf8, evs_list.value_type());
Expand Down
2 changes: 1 addition & 1 deletion arrow-row/src/lib.rs
Expand Up @@ -512,7 +512,7 @@ impl Codec {
DataType::LargeList(_) => as_large_list_array(array).values(),
_ => unreachable!(),
};
let rows = converter.convert_columns(&[values])?;
let rows = converter.convert_columns(&[values.clone()])?;
Ok(Encoder::List(rows))
}
}
Expand Down
2 changes: 1 addition & 1 deletion arrow/src/util/data_gen.rs
Expand Up @@ -342,7 +342,7 @@ mod tests {
let col_c_values = col_c.values();
assert!(col_c_values.len() > size);
// col_c_values should be a list
let col_c_list = col_c_values.as_any().downcast_ref::<ListArray>().unwrap();
let col_c_list = as_list_array(col_c_values);
// Its values should be FixedSizeBinary(6)
let fsb = col_c_list.values();
assert_eq!(fsb.data_type(), &DataType::FixedSizeBinary(6));
Expand Down