You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ks...@apache.org on 2018/09/09 08:42:00 UTC
[arrow] branch master updated: ARROW-3173: [Rust] dynamic_types
example does not run
This is an automated email from the ASF dual-hosted git repository.
kszucs pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 282bc52 ARROW-3173: [Rust] dynamic_types example does not run
282bc52 is described below
commit 282bc52c2cfdede7918f102b0e0f3f335949ff95
Author: Paddy Horan <pa...@hotmail.com>
AuthorDate: Sun Sep 9 10:41:50 2018 +0200
ARROW-3173: [Rust] dynamic_types example does not run
Author: Paddy Horan <pa...@hotmail.com>
Closes #2520 from paddyhoran/ARROW-3173 and squashes the following commits:
7a20aeec <Paddy Horan> Fixed lints.
095b432f <Paddy Horan> Expanded testing for `StructArray`
271c19ff <Paddy Horan> Add `len` method for `StructArray`
d102b2a8 <Paddy Horan> `RecordBatch` should use `len` from `Array` trait
---
rust/src/array.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++--
rust/src/record_batch.rs | 2 +-
2 files changed, 59 insertions(+), 3 deletions(-)
diff --git a/rust/src/array.rs b/rust/src/array.rs
index c42928f..b3488f9 100644
--- a/rust/src/array.rs
+++ b/rust/src/array.rs
@@ -520,15 +520,31 @@ impl Array for StructArray {
fn data_ref(&self) -> &ArrayDataRef {
&self.data
}
+
+ /// Returns the length (i.e., number of elements) of this array
+ fn len(&self) -> i64 {
+ self.boxed_fields[0].len()
+ }
}
impl From<Vec<(Field, ArrayRef)>> for StructArray {
fn from(v: Vec<(Field, ArrayRef)>) -> Self {
let (field_types, field_values): (Vec<_>, Vec<_>) = v.into_iter().unzip();
+
+ // Check the length of the child arrays
+ let length = field_values[0].len();
+ for i in 1..field_values.len() {
+ assert_eq!(
+ length,
+ field_values[i].len(),
+ "all child arrays of a StructArray must have the same length"
+ );
+ }
+
let data = ArrayData::builder(DataType::Struct(field_types))
.child_data(field_values.into_iter().map(|a| a.data()).collect())
.build();
- StructArray::from(data)
+ Self::from(data)
}
}
@@ -541,6 +557,7 @@ mod tests {
use buffer::Buffer;
use datatypes::{DataType, Field, ToByteSlice};
use memory;
+ use std::sync::Arc;
#[test]
fn test_primitive_array_from_vec() {
@@ -787,7 +804,7 @@ mod tests {
}
#[test]
- fn test_struct_array() {
+ fn test_struct_array_builder() {
let boolean_data = ArrayData::builder(DataType::Boolean)
.len(4)
.add_buffer(Buffer::from([false, false, true, true].to_byte_slice()))
@@ -810,6 +827,45 @@ mod tests {
}
#[test]
+ fn test_struct_array_from() {
+ let boolean_data = ArrayData::builder(DataType::Boolean)
+ .len(4)
+ .add_buffer(Buffer::from([false, false, true, true].to_byte_slice()))
+ .build();
+ let int_data = ArrayData::builder(DataType::Int32)
+ .len(4)
+ .add_buffer(Buffer::from([42, 28, 19, 31].to_byte_slice()))
+ .build();
+ let struct_array = StructArray::from(vec![
+ (
+ Field::new("b", DataType::Boolean, false),
+ Arc::new(PrimitiveArray::from(vec![false, false, true, true])) as Arc<Array>,
+ ),
+ (
+ Field::new("c", DataType::Int32, false),
+ Arc::new(PrimitiveArray::from(vec![42, 28, 19, 31])),
+ ),
+ ]);
+ assert_eq!(boolean_data, struct_array.column(0).data());
+ assert_eq!(int_data, struct_array.column(1).data());
+ }
+
+ #[test]
+ #[should_panic(expected = "all child arrays of a StructArray must have the same length")]
+ fn test_invalid_struct_child_array_lengths() {
+ StructArray::from(vec![
+ (
+ Field::new("b", DataType::Float64, false),
+ Arc::new(PrimitiveArray::from(vec![1.1])) as Arc<Array>,
+ ),
+ (
+ Field::new("c", DataType::Float64, false),
+ Arc::new(PrimitiveArray::from(vec![2.2, 3.3])),
+ ),
+ ]);
+ }
+
+ #[test]
#[should_panic(expected = "memory is not aligned")]
fn test_primitive_array_alignment() {
let ptr = memory::allocate_aligned(8).unwrap();
diff --git a/rust/src/record_batch.rs b/rust/src/record_batch.rs
index e3c4138..48856b6 100644
--- a/rust/src/record_batch.rs
+++ b/rust/src/record_batch.rs
@@ -37,7 +37,7 @@ impl RecordBatch {
for i in 1..columns.len() {
assert_eq!(
len,
- columns[i].data().len(),
+ columns[i].len(),
"all columns in a record batch must have the same length"
);
}