Skip to content

Commit b15c0f2

Browse files
Fix up tests
1 parent 20171ad commit b15c0f2

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

arrow-array/src/array/union_array.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,8 +1705,9 @@ mod tests {
17051705
.len(7)
17061706
.buffers(vec![type_ids, value_offsets])
17071707
.child_data(vec![
1708-
string_array.into_data(),
1708+
// Child arrays must be in sorted order by type ID: 4, 8, 9
17091709
int_array.into_data(),
1710+
string_array.into_data(),
17101711
float_array.into_data(),
17111712
])
17121713
.build()
@@ -1817,8 +1818,9 @@ mod tests {
18171818
.len(7)
18181819
.buffers(vec![type_ids, value_offsets])
18191820
.child_data(vec![
1820-
string_array.into_data(),
1821+
// Child arrays must be in sorted order by type ID: 4, 8, 9
18211822
int_array.into_data(),
1823+
string_array.into_data(),
18221824
float_array.into_data(),
18231825
])
18241826
.build()
@@ -1846,9 +1848,10 @@ mod tests {
18461848
],
18471849
)
18481850
.unwrap();
1851+
// Child arrays must be in sorted order by type ID: 2, 3
18491852
let children = vec![
1850-
Arc::new(StringArray::from_iter_values(["a", "b"])) as _,
18511853
Arc::new(StringArray::from_iter_values(["c", "d"])) as _,
1854+
Arc::new(StringArray::from_iter_values(["a", "b"])) as _,
18521855
];
18531856

18541857
let type_ids = vec![3, 3, 2].into();
@@ -1874,9 +1877,10 @@ mod tests {
18741877
"Invalid argument error: Type Ids values must match one of the field type ids"
18751878
);
18761879

1880+
// Child arrays must be in sorted order by type ID: 2, 3
18771881
let children = vec![
1878-
Arc::new(StringArray::from_iter_values(["a", "b"])) as _,
18791882
Arc::new(StringArray::from_iter_values(["c"])) as _,
1883+
Arc::new(StringArray::from_iter_values(["a", "b"])) as _,
18801884
];
18811885
let type_ids = ScalarBuffer::from(vec![3_i8, 3, 2]);
18821886
let offsets = Some(vec![0, 1, 0].into());

arrow-avro/src/reader/record.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,8 +3674,17 @@ mod tests {
36743674
avro_children.push(AvroDataType::new(codec, Default::default(), None));
36753675
fields.push(arrow_schema::Field::new(name, dt, true));
36763676
}
3677-
let union_fields = UnionFields::try_new(type_ids, fields).unwrap();
3678-
let union_codec = Codec::Union(avro_children.into(), union_fields, UnionMode::Dense);
3677+
let union_fields = UnionFields::try_new(type_ids.clone(), fields).unwrap();
3678+
3679+
// UnionFields are sorted by type_id, so we need to reorder avro_children to match
3680+
let mut sorted_indices: Vec<usize> = (0..type_ids.len()).collect();
3681+
sorted_indices.sort_by_key(|&i| type_ids[i]);
3682+
let sorted_avro_children: Vec<AvroDataType> = sorted_indices
3683+
.iter()
3684+
.map(|&i| avro_children[i].clone())
3685+
.collect();
3686+
3687+
let union_codec = Codec::Union(sorted_avro_children.into(), union_fields, UnionMode::Dense);
36793688
AvroDataType::new(union_codec, Default::default(), None)
36803689
}
36813690

@@ -3740,11 +3749,13 @@ mod tests {
37403749
vec![42, 7],
37413750
);
37423751
let mut dec = Decoder::try_new(&union_dt).unwrap();
3743-
let r1 = encode_avro_long(0);
3752+
// after sorting by type_id, schema order is [string(7), null(42)]
3753+
// to encode null, use branch 1; to encode string, use branch 0
3754+
let r1 = encode_avro_long(1);
37443755
let mut r2 = Vec::new();
3745-
r2.extend_from_slice(&encode_avro_long(1));
3756+
r2.extend_from_slice(&encode_avro_long(0));
37463757
r2.extend_from_slice(&encode_avro_bytes(b"abc"));
3747-
let r3 = encode_avro_long(0);
3758+
let r3 = encode_avro_long(1);
37483759
dec.decode(&mut AvroCursor::new(&r1)).unwrap();
37493760
dec.decode(&mut AvroCursor::new(&r2)).unwrap();
37503761
dec.decode(&mut AvroCursor::new(&r3)).unwrap();

arrow-schema/src/fields.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'a> IntoIterator for &'a Fields {
318318
}
319319

320320
/// A cheaply cloneable, owned collection of [`FieldRef`] and their corresponding type ids
321-
#[derive(Clone, Eq, Ord, PartialOrd)]
321+
#[derive(Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
322322
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
323323
#[cfg_attr(feature = "serde", serde(transparent))]
324324
pub struct UnionFields(Arc<[(i8, FieldRef)]>);
@@ -345,21 +345,6 @@ impl std::ops::Index<usize> for UnionFields {
345345
}
346346
}
347347

348-
impl PartialEq for UnionFields {
349-
fn eq(&self, other: &Self) -> bool {
350-
self.len() == other.len() && self.iter().all(|a| other.iter().any(|b| a == b))
351-
}
352-
}
353-
354-
impl Hash for UnionFields {
355-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
356-
let mut v = self.0.iter().collect::<Vec<_>>();
357-
v.sort_by_key(|(id, _)| *id);
358-
359-
v.hash(state);
360-
}
361-
}
362-
363348
impl UnionFields {
364349
/// Create a new [`UnionFields`] with no fields
365350
pub fn empty() -> Self {

0 commit comments

Comments
 (0)