use std::sync::Arc;
use arrow_buffer::NullBuffer;
use arrow_schema::DataType;
use arrow_schema::Field;
use vortex_array::ArrayRef;
use vortex_array::builtins::ArrayBuiltins;
use vortex_array::dtype::DType;
use vortex_array::scalar::Scalar;
use vortex_array::validity::Validity;
use vortex_error::VortexResult;
use vortex_error::vortex_panic;
use crate::convert::nulls;
pub(crate) fn import_leaf(
arrow_nulls: Option<&NullBuffer>,
nullable: bool,
import: impl FnOnce(Validity) -> ArrayRef,
) -> VortexResult<ArrayRef> {
let needs_fill = !nullable && arrow_nulls.is_some_and(|nulls| nulls.null_count() > 0);
let validity = nulls(arrow_nulls, nullable || needs_fill)?;
let array = import(validity);
if !needs_fill {
return Ok(array);
}
let zero = Scalar::zero_value(&array.dtype().as_nonnullable());
array.fill_null(zero)
}
pub fn level_validity(null_buffer: Option<&NullBuffer>, nullable: bool) -> VortexResult<Validity> {
if nullable {
nulls(null_buffer, true)
} else {
Ok(Validity::NonNullable)
}
}
pub(crate) fn storage_import_field(field: &Field, dtype: &DType) -> Field {
let field = field.clone().with_nullable(dtype.is_nullable());
let data_type = match (field.data_type(), dtype) {
(DataType::Struct(fields), DType::Struct(declared, _)) => DataType::Struct(
fields
.iter()
.zip(declared.fields())
.map(|(child, dtype)| storage_import_field(child, &dtype))
.collect(),
),
(DataType::List(child), DType::List(element, _)) => {
DataType::List(Arc::new(storage_import_field(child, element)))
}
(DataType::LargeList(child), DType::List(element, _)) => {
DataType::LargeList(Arc::new(storage_import_field(child, element)))
}
(DataType::ListView(child), DType::List(element, _)) => {
DataType::ListView(Arc::new(storage_import_field(child, element)))
}
(DataType::LargeListView(child), DType::List(element, _)) => {
DataType::LargeListView(Arc::new(storage_import_field(child, element)))
}
(DataType::FixedSizeList(child, size), DType::FixedSizeList(element, ..)) => {
DataType::FixedSizeList(Arc::new(storage_import_field(child, element)), *size)
}
(DataType::Map(entries, keys_sorted), DType::Map(map, _)) => {
let DataType::Struct(fields) = entries.data_type() else {
vortex_panic!(
"The Arrow storage dtype check requires struct map entries, got {}",
entries.data_type()
);
};
let fields = [
Arc::new(storage_import_field(&fields[0], &map.key_dtype())),
Arc::new(storage_import_field(&fields[1], &map.value_dtype())),
];
let entries = entries
.as_ref()
.clone()
.with_data_type(DataType::Struct(fields.into()));
DataType::Map(Arc::new(entries), *keys_sorted)
}
(DataType::Dictionary(key, value), _) => {
let value_field = field.clone().with_data_type(value.as_ref().clone());
let value = storage_import_field(&value_field, dtype);
DataType::Dictionary(key.clone(), Box::new(value.data_type().clone()))
}
(DataType::RunEndEncoded(ends, value), _) => DataType::RunEndEncoded(
Arc::clone(ends),
Arc::new(storage_import_field(value, dtype)),
),
_ => return field,
};
field.with_data_type(data_type)
}