forked from jorgecarleitao/arrow2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunion.rs
119 lines (93 loc) · 3.22 KB
/
union.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use arrow2::{
array::{
growable::{Growable, GrowableUnion},
*,
},
datatypes::*,
error::Result,
};
#[test]
fn sparse() -> Result<()> {
let fields = vec![
Field::new("a", DataType::Int32, true),
Field::new("b", DataType::Utf8, true),
];
let data_type = DataType::Union(fields, None, UnionMode::Sparse);
let types = vec![0, 0, 1].into();
let fields = vec![
Int32Array::from(&[Some(1), None, Some(2)]).boxed(),
Utf8Array::<i32>::from([Some("a"), Some("b"), Some("c")]).boxed(),
];
let array = UnionArray::new(data_type, types, fields, None);
for length in 1..2 {
for index in 0..(array.len() - length + 1) {
let mut a = GrowableUnion::new(vec![&array], 10);
a.extend(0, index, length);
let expected = array.slice(index, length);
let result: UnionArray = a.into();
assert_eq!(expected, result);
}
}
Ok(())
}
#[test]
fn dense() -> Result<()> {
let fields = vec![
Field::new("a", DataType::Int32, true),
Field::new("b", DataType::Utf8, true),
];
let data_type = DataType::Union(fields, None, UnionMode::Dense);
let types = vec![0, 0, 1].into();
let fields = vec![
Int32Array::from(&[Some(1), None, Some(2)]).boxed(),
Utf8Array::<i32>::from([Some("c")]).boxed(),
];
let offsets = Some(vec![0, 1, 0].into());
let array = UnionArray::new(data_type, types, fields, offsets);
for length in 1..2 {
for index in 0..(array.len() - length + 1) {
let mut a = GrowableUnion::new(vec![&array], 10);
a.extend(0, index, length);
let expected = array.slice(index, length);
let result: UnionArray = a.into();
assert_eq!(expected, result);
}
}
Ok(())
}
#[test]
fn complex_dense() -> Result<()> {
let fields = vec![
Field::new("a", DataType::Int32, true),
Field::new("b", DataType::Utf8, true),
];
let data_type = DataType::Union(fields, None, UnionMode::Dense);
let types = vec![0, 0, 1].into();
let fields = vec![
Int32Array::from(&[Some(1), Some(2)]).boxed(),
Utf8Array::<i32>::from([Some("c")]).boxed(),
];
let offsets = Some(vec![0, 1, 0].into());
let array1 = UnionArray::new(data_type.clone(), types, fields, offsets);
let types = vec![1, 1, 0].into();
let fields = vec![
Int32Array::from(&[Some(6)]).boxed(),
Utf8Array::<i32>::from([Some("d"), Some("e")]).boxed(),
];
let offsets = Some(vec![0, 1, 0].into());
let array2 = UnionArray::new(data_type.clone(), types, fields, offsets);
let mut a = GrowableUnion::new(vec![&array1, &array2], 10);
// Effective concat
a.extend(0, 0, 3);
a.extend(1, 0, 3);
let result: UnionArray = a.into();
let types = vec![0, 0, 1, 1, 1, 0].into();
let fields = vec![
Int32Array::from(&[Some(1), Some(2), Some(6)]).boxed(),
Utf8Array::<i32>::from([Some("c"), Some("d"), Some("e")]).boxed(),
];
let offsets = Some(vec![0, 1, 0, 1, 2, 2].into());
let expected = UnionArray::new(data_type.clone(), types, fields, offsets);
assert_eq!(expected, result);
Ok(())
}