Skip to content

Commit 20ccf5f

Browse files
authored
[Variant] feat: Support typed_access for Boolean (#8346)
# Which issue does this PR close? - Closes #8329. # Rationale for this change # What changes are included in this PR? # Are these changes tested? Yes # Are there any user-facing changes? If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please call them out.
1 parent 6407c7e commit 20ccf5f

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

parquet-variant-compute/src/variant_array.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ impl StructArrayBuilder {
590590
/// returns the non-null element at index as a Variant
591591
fn typed_value_to_variant(typed_value: &ArrayRef, index: usize) -> Variant<'_, '_> {
592592
match typed_value.data_type() {
593+
DataType::Boolean => {
594+
let boolean_array = typed_value.as_boolean();
595+
let value = boolean_array.value(index);
596+
Variant::from(value)
597+
}
593598
DataType::Int8 => {
594599
primitive_conversion_single_value!(Int8Type, typed_value, index)
595600
}

parquet-variant-compute/src/variant_get.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,23 @@ mod test {
472472
numeric_partially_shredded_test!(f64, partially_shredded_float64_variant_array);
473473
}
474474

475+
#[test]
476+
fn get_variant_partially_shredded_bool_as_variant() {
477+
let array = partially_shredded_bool_variant_array();
478+
let options = GetOptions::new();
479+
let result = variant_get(&array, options).unwrap();
480+
481+
// expect the result is a VariantArray
482+
let result: &VariantArray = result.as_any().downcast_ref().unwrap();
483+
assert_eq!(result.len(), 4);
484+
485+
// Expect the values are the same as the original values
486+
assert_eq!(result.value(0), Variant::from(true));
487+
assert!(!result.is_valid(1));
488+
assert_eq!(result.value(2), Variant::from("n/a"));
489+
assert_eq!(result.value(3), Variant::from(false));
490+
}
491+
475492
/// Shredding: extract a value as an Int32Array
476493
#[test]
477494
fn get_variant_shredded_int32_as_int32_safe_cast() {
@@ -874,6 +891,52 @@ mod test {
874891
f64
875892
);
876893

894+
/// Return a VariantArray that represents a partially "shredded" variant for bool
895+
fn partially_shredded_bool_variant_array() -> ArrayRef {
896+
let (metadata, string_value) = {
897+
let mut builder = parquet_variant::VariantBuilder::new();
898+
builder.append_value("n/a");
899+
builder.finish()
900+
};
901+
902+
let nulls = NullBuffer::from(vec![
903+
true, // row 0 non null
904+
false, // row 1 is null
905+
true, // row 2 non null
906+
true, // row 3 non null
907+
]);
908+
909+
// metadata is the same for all rows
910+
let metadata = BinaryViewArray::from_iter_values(std::iter::repeat_n(&metadata, 4));
911+
912+
// See https://docs.google.com/document/d/1pw0AWoMQY3SjD7R4LgbPvMjG_xSCtXp3rZHkVp9jpZ4/edit?disco=AAABml8WQrY
913+
// about why row1 is an empty but non null, value.
914+
let values = BinaryViewArray::from(vec![
915+
None, // row 0 is shredded, so no value
916+
Some(b"" as &[u8]), // row 1 is null, so empty value (why?)
917+
Some(&string_value), // copy the string value "N/A"
918+
None, // row 3 is shredded, so no value
919+
]);
920+
921+
let typed_value = arrow::array::BooleanArray::from(vec![
922+
Some(true), // row 0 is shredded, so it has a value
923+
None, // row 1 is null, so no value
924+
None, // row 2 is a string, so no typed value
925+
Some(false), // row 3 is shredded, so it has a value
926+
]);
927+
928+
let struct_array = StructArrayBuilder::new()
929+
.with_field("metadata", Arc::new(metadata))
930+
.with_field("typed_value", Arc::new(typed_value))
931+
.with_field("value", Arc::new(values))
932+
.with_nulls(nulls)
933+
.build();
934+
935+
Arc::new(
936+
VariantArray::try_new(Arc::new(struct_array)).expect("should create variant array"),
937+
)
938+
}
939+
877940
/// Builds struct arrays from component fields
878941
///
879942
/// TODO: move to arrow crate

0 commit comments

Comments
 (0)