use relate_core::{Error, Result};
use super::vortex_error;
use vortex::array::arrays::struct_::{Struct, StructArrayExt};
use vortex::array::ArrayRef;
use vortex::file::VortexFile;
use vortex::io::runtime::BlockingRuntime;
pub(super) fn projected<R: BlockingRuntime>(
rt: &R,
vfile: &VortexFile,
name: &str,
) -> Result<ArrayRef> {
use vortex::array::stream::ArrayStreamExt;
use vortex::expr::{col, BoundExpression};
let projection: BoundExpression = col(name)
.bind(vfile.dtype())
.map_err(vortex_error)?
.optimize_recursive()
.map_err(vortex_error)?;
let want = projection.dtype().clone();
let arr = rt
.block_on(async {
let b = vfile.scan()?.with_projection(projection.clone());
b.into_array_stream()?.read_all().await
})
.map_err(vortex_error)?;
if arr.as_opt::<Struct>().is_none() && *arr.dtype() != want {
return Err(Error::storage(format!(
"projected scan of {name} yielded {} instead of the column",
arr.dtype()
)));
}
field_of(arr, name)
}
fn field_of(array: ArrayRef, name: &str) -> Result<ArrayRef> {
match array.as_opt::<Struct>() {
Some(view) => Ok(view
.unmasked_field_by_name(name)
.map_err(vortex_error)?
.clone()),
None => Ok(array),
}
}
pub(super) fn chunks(field: &ArrayRef) -> Vec<ArrayRef> {
use vortex::array::arrays::chunked::{Chunked, ChunkedArrayExt};
match field.as_opt::<Chunked>() {
Some(chunked) => (0..chunked.nchunks())
.map(|i| chunked.chunk(i).clone())
.collect(),
None => vec![field.clone()],
}
}