Skip to content

Commit e22e550

Browse files
Remove ceremony from iterator of variants into VariantArray
1 parent 751b082 commit e22e550

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

parquet-variant-compute/src/variant_array.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
//! [`VariantArray`] implementation
1919
20+
use crate::VariantArrayBuilder;
2021
use crate::type_conversion::{generic_conversion_single_value, primitive_conversion_single_value};
2122
use arrow::array::{Array, ArrayRef, AsArray, BinaryViewArray, StructArray};
2223
use arrow::buffer::NullBuffer;
@@ -439,6 +440,22 @@ impl From<VariantArray> for ArrayRef {
439440
}
440441
}
441442

443+
impl<'m, 'v> FromIterator<Option<Variant<'m, 'v>>> for VariantArray {
444+
fn from_iter<T: IntoIterator<Item = Option<Variant<'m, 'v>>>>(iter: T) -> Self {
445+
let mut b = VariantArrayBuilder::new(0);
446+
b.extend(iter);
447+
b.build()
448+
}
449+
}
450+
451+
impl<'m, 'v> FromIterator<Variant<'m, 'v>> for VariantArray {
452+
fn from_iter<T: IntoIterator<Item = Variant<'m, 'v>>>(iter: T) -> Self {
453+
let mut b = VariantArrayBuilder::new(0);
454+
b.extend(iter.into_iter().map(Some));
455+
b.build()
456+
}
457+
}
458+
442459
/// An iterator over [`VariantArray`]
443460
///
444461
/// This iterator returns `Option<Option<Variant<'a, 'a>>>` where:
@@ -1141,6 +1158,7 @@ mod test {
11411158
use super::*;
11421159
use arrow::array::{BinaryViewArray, Int32Array};
11431160
use arrow_schema::{Field, Fields};
1161+
use parquet_variant::ShortString;
11441162

11451163
#[test]
11461164
fn invalid_not_a_struct_array() {
@@ -1405,4 +1423,48 @@ mod test {
14051423
assert!(i.next().is_none());
14061424
assert!(i.next_back().is_none());
14071425
}
1426+
1427+
#[test]
1428+
fn test_from_variant_opts_into_variant_array() {
1429+
let v = vec![None, Some(Variant::Null), Some(Variant::BooleanFalse), None];
1430+
1431+
let variant_array = VariantArray::from_iter(v);
1432+
1433+
assert_eq!(variant_array.len(), 4);
1434+
1435+
assert!(variant_array.is_null(0));
1436+
1437+
assert!(!variant_array.is_null(1));
1438+
assert_eq!(variant_array.value(1), Variant::Null);
1439+
1440+
assert!(!variant_array.is_null(2));
1441+
assert_eq!(variant_array.value(2), Variant::BooleanFalse);
1442+
1443+
assert!(variant_array.is_null(3));
1444+
}
1445+
1446+
#[test]
1447+
fn test_from_variants_into_variant_array() {
1448+
let v = vec![
1449+
Variant::Null,
1450+
Variant::BooleanFalse,
1451+
Variant::ShortString(ShortString::try_new("norm").unwrap()),
1452+
];
1453+
1454+
let variant_array = VariantArray::from_iter(v.into_iter());
1455+
1456+
assert_eq!(variant_array.len(), 3);
1457+
1458+
assert!(!variant_array.is_null(0));
1459+
assert_eq!(variant_array.value(0), Variant::Null);
1460+
1461+
assert!(!variant_array.is_null(1));
1462+
assert_eq!(variant_array.value(1), Variant::BooleanFalse);
1463+
1464+
assert!(!variant_array.is_null(3));
1465+
assert_eq!(
1466+
variant_array.value(2),
1467+
Variant::ShortString(ShortString::try_new("norm").unwrap())
1468+
);
1469+
}
14081470
}

0 commit comments

Comments
 (0)