diff --git a/vortex-spatial/src/extension/linestring.rs b/vortex-spatial/src/extension/linestring.rs index 83e2f78f745..b2438b304d4 100644 --- a/vortex-spatial/src/extension/linestring.rs +++ b/vortex-spatial/src/extension/linestring.rs @@ -16,7 +16,6 @@ use arrow_schema::extension::ExtensionType; use geo_traits::to_geo::ToGeoGeometry; use geo_types::Geometry; use geoarrow::array::GeoArrowArrayAccessor; -use geoarrow::array::IntoArrow; use geoarrow::array::LineStringArray; use geoarrow::datatypes::CoordType; use geoarrow::datatypes::LineStringType; @@ -320,11 +319,9 @@ impl ArrowExtVTable for LineString { let arrow_storage = storage.execute_arrow_with_options(Some(&storage_field), options, ctx)?; - // Round-trip through GeoArrow's line-string array; `into_arrow` is concrete, so wrap in `Arc`. - let linestrings = LineStringArray::try_from((arrow_storage.as_ref(), linestring_meta)) - .map_err(|e| vortex_err!("failed to construct LineStringArray: {e}"))?; - - Ok(ArrowExport::Exported(Arc::new(linestrings.into_arrow()))) + // The validated target already shapes the storage. GeoArrow constructors can panic on + // valid Arrow lists that retain unused child values after filtering. + Ok(ArrowExport::Exported(arrow_storage)) } fn from_arrow_field( diff --git a/vortex-spatial/src/extension/multilinestring.rs b/vortex-spatial/src/extension/multilinestring.rs index dfdb4e89dd2..441f9863b70 100644 --- a/vortex-spatial/src/extension/multilinestring.rs +++ b/vortex-spatial/src/extension/multilinestring.rs @@ -17,7 +17,6 @@ use arrow_schema::extension::ExtensionType; use geo_traits::to_geo::ToGeoGeometry; use geo_types::Geometry; use geoarrow::array::GeoArrowArrayAccessor; -use geoarrow::array::IntoArrow; use geoarrow::array::MultiLineStringArray; use geoarrow::datatypes::CoordType; use geoarrow::datatypes::MultiLineStringType; @@ -248,13 +247,9 @@ impl ArrowExtVTable for MultiLineString { let arrow_storage = storage.execute_arrow_with_options(Some(&storage_field), options, ctx)?; - let multilinestrings = - MultiLineStringArray::try_from((arrow_storage.as_ref(), multilinestring_meta)) - .map_err(|e| vortex_err!("failed to construct MultiLineStringArray: {e}"))?; - - Ok(ArrowExport::Exported(Arc::new( - multilinestrings.into_arrow(), - ))) + // The validated target already shapes the storage. GeoArrow constructors can panic on + // valid Arrow lists that retain unused child values after filtering. + Ok(ArrowExport::Exported(arrow_storage)) } fn from_arrow_field( diff --git a/vortex-spatial/src/extension/multipoint.rs b/vortex-spatial/src/extension/multipoint.rs index 71ed85c0ff7..248d4a17601 100644 --- a/vortex-spatial/src/extension/multipoint.rs +++ b/vortex-spatial/src/extension/multipoint.rs @@ -18,7 +18,6 @@ use arrow_schema::extension::ExtensionType; use geo_traits::to_geo::ToGeoGeometry; use geo_types::Geometry; use geoarrow::array::GeoArrowArrayAccessor; -use geoarrow::array::IntoArrow; use geoarrow::array::MultiPointArray; use geoarrow::datatypes::CoordType; use geoarrow::datatypes::MultiPointType; @@ -237,11 +236,9 @@ impl ArrowExtVTable for MultiPoint { let arrow_storage = storage.execute_arrow_with_options(Some(&storage_field), options, ctx)?; - // Round-trip through GeoArrow's multipoint array; `into_arrow` is concrete, so wrap in `Arc`. - let multipoints = MultiPointArray::try_from((arrow_storage.as_ref(), multipoint_meta)) - .map_err(|e| vortex_err!("failed to construct MultiPointArray: {e}"))?; - - Ok(ArrowExport::Exported(Arc::new(multipoints.into_arrow()))) + // The validated target already shapes the storage. GeoArrow constructors can panic on + // valid Arrow lists that retain unused child values after filtering. + Ok(ArrowExport::Exported(arrow_storage)) } fn from_arrow_field( diff --git a/vortex-spatial/src/extension/multipolygon.rs b/vortex-spatial/src/extension/multipolygon.rs index ef14a8e645d..9e72ebd8b4d 100644 --- a/vortex-spatial/src/extension/multipolygon.rs +++ b/vortex-spatial/src/extension/multipolygon.rs @@ -18,7 +18,6 @@ use arrow_schema::extension::ExtensionType; use geo_traits::to_geo::ToGeoGeometry; use geo_types::Geometry; use geoarrow::array::GeoArrowArrayAccessor; -use geoarrow::array::IntoArrow; use geoarrow::array::MultiPolygonArray; use geoarrow::datatypes::CoordType; use geoarrow::datatypes::MultiPolygonType; @@ -248,11 +247,9 @@ impl ArrowExtVTable for MultiPolygon { let arrow_storage = storage.execute_arrow_with_options(Some(&storage_field), options, ctx)?; - let multipolygons = - MultiPolygonArray::try_from((arrow_storage.as_ref(), multipolygon_meta)) - .map_err(|e| vortex_err!("failed to construct MultiPolygonArray: {e}"))?; - - Ok(ArrowExport::Exported(Arc::new(multipolygons.into_arrow()))) + // The validated target already shapes the storage. GeoArrow constructors can panic on + // valid Arrow lists that retain unused child values after filtering. + Ok(ArrowExport::Exported(arrow_storage)) } fn from_arrow_field( diff --git a/vortex-spatial/src/extension/polygon.rs b/vortex-spatial/src/extension/polygon.rs index 51bf42ede88..d221cd9b81b 100644 --- a/vortex-spatial/src/extension/polygon.rs +++ b/vortex-spatial/src/extension/polygon.rs @@ -18,7 +18,6 @@ use geo::HasDimensions; use geo_traits::to_geo::ToGeoGeometry; use geo_types::Geometry; use geoarrow::array::GeoArrowArrayAccessor; -use geoarrow::array::IntoArrow; use geoarrow::array::PolygonArray; use geoarrow::datatypes::CoordType; use geoarrow::datatypes::PolygonType; @@ -309,11 +308,9 @@ impl ArrowExtVTable for Polygon { let arrow_storage = storage.execute_arrow_with_options(Some(&storage_field), options, ctx)?; - // Round-trip through GeoArrow's polygon array; `into_arrow` is concrete, so wrap in `Arc`. - let polygons = PolygonArray::try_from((arrow_storage.as_ref(), polygon_meta)) - .map_err(|e| vortex_err!("failed to construct PolygonArray: {e}"))?; - - Ok(ArrowExport::Exported(Arc::new(polygons.into_arrow()))) + // The validated target already shapes the storage. GeoArrow constructors can panic on + // valid Arrow lists that retain unused child values after filtering. + Ok(ArrowExport::Exported(arrow_storage)) } fn from_arrow_field( diff --git a/vortex-spatial/src/tests/mod.rs b/vortex-spatial/src/tests/mod.rs index c83a30d476d..2b26e3da39b 100644 --- a/vortex-spatial/src/tests/mod.rs +++ b/vortex-spatial/src/tests/mod.rs @@ -9,6 +9,7 @@ mod multilinestring; mod multipoint; mod multipolygon; mod point; +mod polygon; mod rect; mod wkb; diff --git a/vortex-spatial/src/tests/polygon.rs b/vortex-spatial/src/tests/polygon.rs new file mode 100644 index 00000000000..561e4b6571f --- /dev/null +++ b/vortex-spatial/src/tests/polygon.rs @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! Polygon export after selections that can retain unused coordinate children. +//! +//! Arrow allows those children, but reconstructing a GeoArrow polygon imposes a stricter length +//! constraint. The trailing null row keeps that distinction visible during batch selections. + +use geoarrow::datatypes::PolygonType; +use vortex_array::ArrayRef; +use vortex_array::ExecutionCtx; +use vortex_array::IntoArray; +use vortex_array::VortexSessionExecute; +use vortex_array::arrays::ExtensionArray; +use vortex_array::arrays::ListArray; +use vortex_array::arrays::PrimitiveArray; +use vortex_array::arrays::StructArray; +use vortex_array::assert_arrays_eq; +use vortex_array::validity::Validity; +use vortex_arrow::ArrowSessionExt; +use vortex_arrow::ExecuteArrowExt; +use vortex_error::VortexResult; +use vortex_mask::Mask; + +use super::SESSION; +use crate::extension::Dimension; +use crate::extension::Polygon; +use crate::extension::SpatialMetadata; + +/// Each present row is a closed XYM triangle with the given x origin. Null rows contain no rings. +fn polygon_column(source: &[Option], ctx: &mut ExecutionCtx) -> VortexResult { + let mut xs = Vec::new(); + let mut ys = Vec::new(); + let mut ring_offsets = vec![0i32]; + let mut polygon_offsets = vec![0i32]; + + for &origin in source { + if let Some(x) = origin { + xs.extend([x, x + 1.0, x, x]); + ys.extend([0.0, 0.0, 1.0, 0.0]); + ring_offsets.push(i32::try_from(xs.len())?); + } + polygon_offsets.push(i32::try_from(ring_offsets.len() - 1)?); + } + + let coordinate_count = xs.len(); + let coordinates = StructArray::from_fields(&[ + ("x", PrimitiveArray::from_iter(xs).into_array()), + ("y", PrimitiveArray::from_iter(ys).into_array()), + ( + "m", + PrimitiveArray::from_iter(vec![0.0f64; coordinate_count]).into_array(), + ), + ])? + .into_array(); + let rings = ListArray::try_new( + coordinates, + PrimitiveArray::from_iter(ring_offsets).into_array(), + Validity::NonNullable, + ctx, + )? + .into_array(); + let storage = ListArray::try_new( + rings, + PrimitiveArray::from_iter(polygon_offsets).into_array(), + Validity::from_iter(source.iter().map(Option::is_some)), + ctx, + )? + .into_array(); + + Ok(ExtensionArray::try_new_from_vtable( + Polygon, + SpatialMetadata::default().with_dimension(Dimension::Xym), + storage, + )? + .into_array()) +} + +#[track_caller] +fn check_selected_polygons() -> VortexResult<()> { + let mut ctx = SESSION.create_execution_ctx(); + let source = polygon_column(&[Some(0.0), Some(1.0), None], &mut ctx)?; + let field = PolygonType::new(Dimension::Xym.into(), Default::default()).to_field("value", true); + let selections = [ + ("slice", source.slice(1..3)?, vec![Some(1.0), None]), + ( + "take", + source.take(PrimitiveArray::from_iter([2u32, 2, 1, 1, 0, 0]).into_array())?, + vec![None, None, Some(1.0), Some(1.0), Some(0.0), Some(0.0)], + ), + ( + "filter", + source.filter(Mask::from_iter([true, false, true]))?, + vec![Some(0.0), None], + ), + ]; + + for (operation, selected, expected) in selections { + let selected = selected.execute::(&mut ctx)?.into_array(); + let exported = selected.execute_arrow(Some(&field), &mut ctx)?; + exported.to_data().validate_full()?; + assert_eq!(exported.data_type(), field.data_type(), "{operation} dtype"); + + let imported = SESSION.arrow().from_arrow_array(exported, &field)?; + let expected = polygon_column(&expected, &mut ctx)?; + assert_arrays_eq!(imported, expected, &mut ctx); + } + + Ok(()) +} + +#[test] +fn selected_polygons_export_with_unused_coordinate_children() -> VortexResult<()> { + check_selected_polygons() +}