Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions vortex-spatial/src/extension/linestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 3 additions & 8 deletions vortex-spatial/src/extension/multilinestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 3 additions & 6 deletions vortex-spatial/src/extension/multipoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 3 additions & 6 deletions vortex-spatial/src/extension/multipolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 3 additions & 6 deletions vortex-spatial/src/extension/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions vortex-spatial/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod multilinestring;
mod multipoint;
mod multipolygon;
mod point;
mod polygon;
mod rect;
mod wkb;

Expand Down
115 changes: 115 additions & 0 deletions vortex-spatial/src/tests/polygon.rs
Original file line number Diff line number Diff line change
@@ -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<f64>], ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
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::<ExtensionArray>(&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()
}
Loading