geoarrow-rs icon indicating copy to clipboard operation
geoarrow-rs copied to clipboard

WrapArray trait

Open kylebarron opened this issue 8 months ago • 1 comments

Oh that's a valid point about defining the trait in geoarrow-array. We can have:

use arrow_array::Array;
use geoarrow_schema::PointType;

use crate::error::Result;
use crate::GeoArrowArray;

pub trait WrapArray {
    type Output: GeoArrowArray;

    fn wrap(&self, array: &dyn Array) -> Result<Self::Output>;
}

impl WrapArray for PointType {
    type Output = PointArray;

    fn wrap(&self, array: &dyn Array) -> Result<Self::Output> {
        (array, self.clone()).try_into()
    }
}

fn example(typ: PointType, array: Arc<dyn Array>) {
    let point_array: PointArray = typ.wrap(array.as_ref()).unwrap();
}

Originally posted by @kylebarron in https://github.com/geoarrow/geoarrow-rs/issues/1023#issuecomment-2784833501

cc @paleolimbot

kylebarron avatar Apr 08 '25 00:04 kylebarron

Oh nice, I didn't know the trick about setting the output type!

For what it's worth, this comes from Arrow C++/pyarrow, where "extension types" have a wrap_array()/WrapArray() method. It also creates some dependency issues for geoarrow-pyarrow because we want the same kind of setup there too (array depends on schema).

paleolimbot avatar Apr 08 '25 14:04 paleolimbot

While it's not explicitly called a "WrapArray" trait, there's equivalent functionality in geoarrow-array to achieve this. Every geometry array type has several TryFrom implementations that convert data from arrow arrays into geoarrow arrays: https://docs.rs/geoarrow-array/latest/geoarrow_array/array/struct.PointArray.html#impl-TryFrom%3C(%26StructArray,+PointType)%3E-for-PointArray

kylebarron avatar Aug 07 '25 03:08 kylebarron

One downside of the current from_arrow_array top-level function is that it always uses GeoArrowType::from_arrow_field (interpreting any geometry-like column, loosely) instead of using GeoArrowType::from_extension_field (actively requiring GeoArrow extension metadata to be set on the field to allow the conversion).

Perhaps it would be worth it to have this WrapArray trait so that users could choose which method to use. GeoArrowType::from_extension_field(field).unwrap().wrap_array(&array_ref)?

kylebarron avatar Aug 12 '25 19:08 kylebarron

Perhaps we should have a wrap_array method both on each extension type, so that PointType::wrap_array returns a PointArray. But then also implement that on GeoArrowType which returns an Arc<dyn GeoArrowArray>.

kylebarron avatar Aug 12 '25 19:08 kylebarron